Exemplo n.º 1
0
        public JsonResult CreateCourse(int crsuniq, int funiq, int schuniq)
        {
            course  sisCourse     = SISdb.courses.Find(crsuniq);
            school  sisSchool     = SISdb.schools.Find(schuniq);
            facdemo courseTeacher = SISdb.facdemoes.Find(funiq);
            int     schoolYear    = Convert.ToInt32(SISdb.schools.Max(s => s.schyear));
            string  sisCourseId   = schoolYear + ":" + crsuniq + ":" + funiq;

            //get the account id
            int accountId;

            AccountIDs.TryGetValue(sisSchool.schoolc, out accountId);

            //make sure the course doesn't exist
            CanvasCourseModel vCourse = GetCourse(crsuniq, funiq, schuniq);

            if (vCourse != null)
            {
                //the course exists return the id
                return(Json(new
                {
                    success = "true",
                    courseId = vCourse.id
                }));
            }

            CanvasCourseModel vSisCourse = GetCourseBySISId(sisCourseId);

            if (vSisCourse != null)
            {
                //the course exists return the id
                return(Json(new
                {
                    success = "true",
                    courseId = vSisCourse.id
                }));
            }


            string courseJson = "{ \"account_id\": \"" + accountId + "\"," +
                                "\"course\": {" +
                                "\"name\": \"" + sisCourse.descript + " : " + courseTeacher.lastname.Trim() + " " + courseTeacher.firstname.Trim() + "\"," +
                                "\"course_code\": \"" + sisCourse.descript + " : " + courseTeacher.lastname.Trim() + " " + courseTeacher.firstname.Trim() + "\"," +
                                "\"sis_course_id\": \"" + sisCourseId + "\"}}";


            //send the data
            string createCourseURL = CanvasUrl + "accounts/" + accountId + "/courses?access_token=" + OAuthKey;
            var    httpWebRequest  = (HttpWebRequest)WebRequest.Create(createCourseURL);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            CanvasCourseModel newCourse = new CanvasCourseModel();

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(courseJson);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var serializer = new JsonSerializer();
                    using (var jsonTextReader = new JsonTextReader(streamReader))
                    {
                        newCourse = serializer.Deserialize <CanvasCourseModel>(jsonTextReader);
                    }
                }
            }

            //attach the teacher
            AssignTeacherToCourse(funiq, newCourse.id, schuniq);

            return(Json(new
            {
                success = "true",
                courseId = newCourse.id
            }));
        }
Exemplo n.º 2
0
        private CanvasUserModel CreateCanvasTeacher(int funiq, int schuniq)
        {
            school sisSchool = SISdb.schools.Find(schuniq);

            //get the account id
            int accountId;

            AccountIDs.TryGetValue(sisSchool.schoolc, out accountId);

            //make sure the user doesn't exist
            CanvasUserModel user = GetUserBySISId("F" + funiq);

            if (user != null)
            {
                //the user exists
                return(user);
            }

            //the user does not exist
            facdemo teacher  = SISdb.facdemoes.Find(funiq);
            string  password = teacher.firstname.Substring(0, 1).ToUpper() +
                               teacher.lastname.Substring(0, 2).ToLower() + funiq;
            string userJson = "{\"user\": {" +
                              "\"name\": \"" + teacher.firstname.Trim() + " " + teacher.lastname.Trim() + "\"," +
                              "\"short_name\": \"" + teacher.firstname.Trim() + "\"," +
                              "\"sortable_name\": \"" + teacher.lastname.Trim() + ", " + teacher.firstname.Trim() + "\"}," +
                              "\"pseudonym\": {" +
                              "\"unique_id\": \"" + teacher.emailaddr + "\"," +
                              "\"password\": \"" + password + "\"," +
                              "\"sis_user_id\": \"F" + funiq + "\"," +
                              //this should be set to true on live
                              "\"send_confirmation\": \"false\"}," +
                              "\"communication_channel\": {" +
                              "\"type\":\"email\"," +
                              "\"address\": \"" + teacher.emailaddr + "\"," +
                              "\"skip_confirmation\": \"true\"}}";


            //send the data
            string createUserURL  = CanvasUrl + "accounts/" + accountId + "/users?access_token=" + OAuthKey;
            var    httpWebRequest = (HttpWebRequest)WebRequest.Create(createUserURL);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            CanvasUserModel newUser = new CanvasUserModel();

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(userJson);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var serializer = new JsonSerializer();
                    using (var jsonTextReader = new JsonTextReader(streamReader))
                    {
                        newUser = serializer.Deserialize <CanvasUserModel>(jsonTextReader);
                    }
                }
            }

            return(newUser);
        }