示例#1
0
        private async Task <object> GetCourseDiscussion(CanvasCourseModel course)
        {
            if (course != null)
            {
                canvasLogger.Log(string.Format("Getting discussions for subject {0}.", course.name));
                string sResponse = await this.cm.GetCourseDiscussionsTopics(course.id) as string;

                if (!string.IsNullOrEmpty(sResponse))
                {
                    List <CanvasCourseDiscussionTopicModel> lstDiscussions = sResponse.FromJson <List <CanvasCourseDiscussionTopicModel> >();
                    if (lstDiscussions != null && lstDiscussions.Count > 0)
                    {
                        canvasLogger.Log(string.Format("{0} discussion(s) found against course {1}.", lstDiscussions.Count, course.name));
                        List <Task> lstDiscussionEntries = new List <Task>();
                        foreach (var aDiscussion in lstDiscussions)
                        {
                            Task aTask = new Task(() => {
                                ProcessDiscussionTopicEntries(course, aDiscussion);
                            });
                            lstDiscussionEntries.Add(aTask);
                            aTask.Start();
                        }
                        Task.WaitAll(lstDiscussionEntries.ToArray());
                    }
                    else
                    {
                        canvasLogger.Log(string.Format("No discussion(s) found against course {0}.", course.name));
                    }
                }
            }
            return(null);
        }
示例#2
0
        private async Task <string> CreateNewCourse(CanvasCourseDetailModel courseDetail)
        {
            if (courseDetail != null)
            {
                Courses.Add Add = new Courses.Add()
                {
                    CourseDescription = courseDetail.CourseDescription
                    ,
                    CourseName = courseDetail.CourseName
                    ,
                    EndDate = courseDetail.EndDate
                    ,
                    StartDate = courseDetail.StartDate
                };
                string oResponse = await base.canvasManager.CreateCourse(Add) as string;

                if (!string.IsNullOrEmpty(oResponse))
                {
                    CanvasCourseModel newCourse = oResponse.FromJson <CanvasCourseModel>();
                    if (newCourse != null)
                    {
                        Guid  courseId = Guid.NewGuid();
                        Cours c        = new Cours()
                        {
                            CanvasId            = newCourse.id
                            , CourseCreateDate  = DateTime.Now
                            , CourseDescription = courseDetail.CourseDescription
                            , CourseName        = courseDetail.CourseName
                            , CourseId          = courseId
                        };


                        try
                        {
                            base.db.Courses.Add(c);
                            db.SaveChanges();
                            SaveCourseSchedule(courseDetail);
                            SaveCourseProviders(courseDetail);
                            SaveCourseUniversities(courseDetail);
                        }
                        catch (Exception ex)
                        {
                            return(base.serializer.Serialize(new { Success = false, Message = ex.Message }));
                        }

                        return(base.serializer.Serialize(new { Success = true, Message = string.Empty }));
                    }
                }
            }
            return(base.serializer.Serialize(new { Success = false, Message = "Error" }));
        }
示例#3
0
        public JsonResult lookUpCourse(int crsuniq, int funiq, int schuniq)
        {
            CanvasCourseModel course = GetCourse(crsuniq, funiq, schuniq);

            if (course != null)
            {
                return(Json(new
                {
                    success = "true",
                    course = new
                    {
                        name = course.name,
                        id = course.id,
                        sis_course_id = course.sis_course_id,
                        course_code = course.course_code
                    }
                }));
            }
            else
            {
                //check by sis course id
                int    schoolYear  = Convert.ToInt32(SISdb.schools.Max(s => s.schyear));
                string sisCourseId = schoolYear + ":" + crsuniq + ":" + funiq;
                course = GetCourseBySISId(sisCourseId);
                if (course != null)
                {
                    return(Json(new
                    {
                        success = "true",
                        course = new
                        {
                            name = course.name,
                            id = course.id,
                            sis_course_id = course.sis_course_id,
                            course_code = course.course_code
                        }
                    }));
                }
                else
                {
                    return(Json(new
                    {
                        success = "false",
                        message = "Course does not exist!"
                    }));
                }
            }
        }
示例#4
0
        private CanvasCourseModel GetCourseById(string courseId)
        {
            CanvasCourseModel course = new CanvasCourseModel();

            using (var client = new WebClient())
            {
                try
                {
                    string getCourseURL = CanvasUrl + "courses/" + courseId + "?access_token=" + OAuthKey;
                    var    json         = client.DownloadString(getCourseURL);
                    var    serializer   = new JavaScriptSerializer();
                    course = serializer.Deserialize <CanvasCourseModel>(json);
                    return(course);
                }
                catch (Exception e)
                {
                    return(null);
                }
            }
        }
示例#5
0
        public JsonResult CreateCourseSection(string courseId, string mstUniq)
        {
            mstsched period = SISdb.mstscheds.Find(Convert.ToInt32(mstUniq));
            //calculate the school year
            int schoolYear = Convert.ToInt32(SISdb.schools.Max(s => s.schyear));

            if (period != null)
            {
                //check to see that the section isn't already created
                CanvasSectionModel section = GetSectionBySISId(schoolYear + ":" + period.mstuniq + ":" + period.funiq);

                if (section != null)
                {
                    return(Json(new
                    {
                        success = "true",
                        sectionId = section.id
                    }));
                }

                //get the course
                CanvasCourseModel course = GetCourseById(courseId);
                if (course != null)
                {
                    string sectionJson = "{ \"course_section\": {" +
                                         "\"name\": \"" + period.trkcr.course.descript + " : " + period.facdemo.lastname.Trim() + " " + period.facdemo.firstname.Trim() + " : " + period.mstmeets.First().periodn + "\"," +
                                         "\"sis_section_id\": \"" + schoolYear + ":" + period.mstuniq + ":" + period.funiq + "\"}}";

                    string createSectionUrl = CanvasUrl + "courses/" + courseId + "/sections?access_token=" + OAuthKey;
                    var    httpWebRequest   = (HttpWebRequest)WebRequest.Create(createSectionUrl);
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method      = "POST";

                    CanvasSectionModel newSection = new CanvasSectionModel();

                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        streamWriter.Write(sectionJson);
                        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))
                            {
                                newSection = serializer.Deserialize <CanvasSectionModel>(jsonTextReader);
                            }
                        }
                    }

                    return(Json(new
                    {
                        success = "true",
                        sectionId = newSection.id,
                        mstUniq = mstUniq
                    }));
                }

                return(Json(new
                {
                    success = "false",
                    message = "Could not get course!"
                }));
            }

            return(Json(new
            {
                success = "false",
                message = "Could not get mstsched!"
            }));
        }
示例#6
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
            }));
        }
示例#7
0
        public async Task <ActionResult> Create(Courses.Add Add)
        {
            if (ModelState.IsValid)
            {
                string oResponse = await base.canvasManager.CreateCourse(Add) as string;

                int iCanvasCourseId = 0;
                if (!string.IsNullOrEmpty(oResponse))
                {
                    CanvasCourseModel newCourse = oResponse.FromJson <CanvasCourseModel>();
                    iCanvasCourseId = newCourse.id;
                }

                var   courceID = Guid.NewGuid();
                Cours c        = new Cours();
                if (Add.CourseId != new Guid() && Add.CourseId != null)
                {
                    c = db.Courses.FirstOrDefault(e => e.CourseId == Add.CourseId);
                }
                if (c == null || c.CourseId == new Guid())
                {
                    c                   = new Cours();
                    c.CourseId          = courceID;
                    c.CourseCreateDate  = DateTime.Now;
                    c.CourseDescription = Add.CourseDescription;
                    c.CourseName        = Add.CourseName;
                    c.CanvasId          = iCanvasCourseId;
                    db.Courses.Add(c);
                }
                else
                {
                    courceID            = c.CourseId;
                    c.CourseDescription = Add.CourseDescription;
                    c.CourseName        = Add.CourseName;
                }

                var schedule = db.CourseSchedules.FirstOrDefault(ee => ee.CourseId == Add.CourseId);
                if (schedule != null)
                {
                    db.CourseSchedules.Remove(schedule);
                }

                schedule            = new CourseSchedule();
                schedule.CourseId   = courceID;
                schedule.EndDate    = Add.EndDate;
                schedule.ScheduleId = Guid.NewGuid();
                schedule.StartDate  = Add.StartDate;
                db.CourseSchedules.Add(schedule);

                var courceProvider = db.CourseProviders.Where(ee => ee.CourseId == courceID).ToList();
                if (courceProvider != null && courceProvider.Any())
                {
                    db.CourseProviders.RemoveRange(courceProvider);
                }

                if (Add.ProviderId != null && Add.ProviderId.Any())
                {
                    foreach (var item in Add.ProviderId)
                    {
                        db.CourseProviders.Add(new CourseProvider()
                        {
                            CourseId   = courceID,
                            Id         = Guid.NewGuid(),
                            ProviderId = item
                        });
                    }
                }

                var CourceUniversity = db.CourseUniversities.Where(ee => ee.CourseId == courceID).ToList();
                if (CourceUniversity != null && CourceUniversity.Any())
                {
                    db.CourseUniversities.RemoveRange(CourceUniversity);
                }

                if (Add.UniversityId != null && Add.UniversityId.Any())
                {
                    foreach (var item in Add.UniversityId)
                    {
                        db.CourseUniversities.Add(new CourseUniversity()
                        {
                            CourseId     = courceID,
                            Id           = Guid.NewGuid(),
                            UniversityId = item
                        });
                    }
                }


                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBags();
            return(View(Add));
        }
示例#8
0
        private async Task <string> UpdateCourse(CanvasCourseDetailModel courseDetail)
        {
            if (courseDetail != null)
            {
                int?iCanvasId = (from c in base.db.Courses
                                 where c.CourseId == courseDetail.CourseId
                                 select c.CanvasId
                                 ).FirstOrDefault();
                if (iCanvasId != null)
                {
                    Courses.Add Add = new Courses.Add()
                    {
                        CourseDescription = courseDetail.CourseDescription
                        ,
                        CourseName = courseDetail.CourseName
                        ,
                        EndDate = courseDetail.EndDate
                        ,
                        StartDate = courseDetail.StartDate
                    };
                    string oResponse = await base.canvasManager.UpdateCourse(Add, iCanvasId.Value) as string;

                    if (!string.IsNullOrEmpty(oResponse))
                    {
                        try
                        {
                            CanvasCourseModel updateCourse = oResponse.FromJson <CanvasCourseModel>();
                            Cours             c            = (from co in base.db.Courses
                                                              where (co.CourseId == courseDetail.CourseId)
                                                              select co)
                                                             .FirstOrDefault();
                            if (c != null)
                            {
                                c.CourseName           = courseDetail.CourseName;
                                c.CourseDescription    = courseDetail.CourseDescription;
                                base.db.Entry(c).State = System.Data.Entity.EntityState.Modified;
                                base.db.SaveChanges();
                                SaveCourseSchedule(courseDetail);
                                SaveCourseProviders(courseDetail);
                                SaveCourseUniversities(courseDetail);
                            }
                            //Cours c = new Cours()
                            //{
                            //    CanvasId = updateCourse.id
                            //    ,
                            //    CourseCreateDate = DateTime.Now
                            //    ,
                            //    CourseDescription = courseDetail.CourseDescription
                            //    ,
                            //    CourseName = courseDetail.CourseName
                            //    ,
                            //    CourseId = Add.CourseId
                            //};
                        }
                        catch (Exception ex)
                        {
                            return(base.serializer.Serialize(new { Success = false, Message = ex.Message }));
                        }
                        return(base.serializer.Serialize(new { Success = true, Message = string.Empty }));
                    }
                }
            }
            return(base.serializer.Serialize(new { Success = false, Message = "Error" }));
        }
示例#9
0
        private async Task <object> ProcessDiscussionTopicEntries(CanvasCourseModel oCourse, CanvasCourseDiscussionTopicModel oDiscussionTopics)
        {
            if (oCourse != null && oDiscussionTopics != null)
            {
                if (oDiscussionTopics.unread_count > 0)
                {
                    string sTopicsEntries = await cm.GetCourseDiscussionEntries(oCourse.id, oDiscussionTopics.id) as string;

                    if (!string.IsNullOrEmpty(sTopicsEntries))
                    {
                        List <CanvasDiscussionTopicEntriesModel> lstTopicEntries = sTopicsEntries.FromJson <List <CanvasDiscussionTopicEntriesModel> >();
                        if (lstTopicEntries != null && lstTopicEntries.Count > 0)
                        {
                            int iTopicsCount = lstTopicEntries.Count;
                            canvasLogger.Log(string.Format("Discussion topic '{0}' of '{1}' has {2} Entrie(s).", oDiscussionTopics.title, oCourse.name, lstTopicEntries.Count));
                            lstTopicEntries = (from t in lstTopicEntries
                                               where (t.read_state == "unread")
                                               select t)
                                              .ToList();
                            if (lstTopicEntries.Count > 0)
                            {
                                canvasLogger.Log(string.Format("Total unread entrie(s) for discussion topic '{0}' of course {1} : {2}", oDiscussionTopics.title, oCourse.name, lstTopicEntries.Count));
                                List <int> lstDiscussionRules = (from dr in db.CanvasRespondRules
                                                                 where (dr.CanvasRuleForId == ((int)AutoResponderRuleTypes.DISCUSSION) &&
                                                                        (dr.Active != null && dr.Active.Value == true))
                                                                 select dr.CanvasRuleId)
                                                                .ToList();
                                if (lstDiscussionRules.Count > 0) /* If any discussion rule found */
                                {
                                    List <CanvasRespondRuleQuestion> lstQuestions = (from q in db.CanvasRespondRuleQuestions
                                                                                     where (q.Active != null && q.Active.Value == true) &&
                                                                                     lstDiscussionRules.Contains(q.CanvasRuleId)
                                                                                     select q)
                                                                                    .ToList();
                                    if (lstQuestions != null && lstQuestions.Count > 0) /* If any discussion question found */
                                    {
                                        foreach (var aEntry in lstTopicEntries)
                                        {
                                            if (string.IsNullOrEmpty(aEntry.message))
                                            {
                                                continue;
                                            }

                                            bool isAnswerFound = false;
                                            foreach (var aQuestion in lstQuestions)
                                            {
                                                CanvasAnsweringRules aAnswer = null;
                                                if (string.IsNullOrEmpty(aQuestion.CanvasQuestion))
                                                {
                                                    continue;
                                                }
                                                if (aQuestion.MatchType != null)
                                                {
                                                    if (Enum.IsDefined(typeof(AutoResponderConditionType), (int)aQuestion.MatchType.Value))
                                                    {
                                                        AutoResponderConditionType condition = (AutoResponderConditionType)aQuestion.MatchType.Value;
                                                        string sPlainText = RemoveHtmlTags(aEntry.message);
                                                        if (!string.IsNullOrEmpty(sPlainText))
                                                        {
                                                            switch (condition)
                                                            {
                                                            case AutoResponderConditionType.CONTAINS_ANY_WORD:

                                                                break;

                                                            case AutoResponderConditionType.CONTAINS_STRING:
                                                                isAnswerFound = sPlainText.Trim().ToLower().IndexOf(aQuestion.CanvasQuestion.Trim().ToLower()) != -1;
                                                                break;

                                                            case AutoResponderConditionType.MATCH_STRING:
                                                                isAnswerFound = sPlainText.Trim().ToLower().Equals(aQuestion.CanvasQuestion.Trim().ToLower(), StringComparison.InvariantCultureIgnoreCase);
                                                                break;
                                                            }
                                                        }
                                                        if (isAnswerFound)
                                                        {
                                                            var answerFound = (from a in db.CanvasRespondRules
                                                                               where a.CanvasRuleId == aQuestion.CanvasRuleId
                                                                               select a)
                                                                              .FirstOrDefault();
                                                            if (answerFound != null && (!string.IsNullOrEmpty(answerFound.CanvasAnswer)))
                                                            {
                                                                canvasLogger.Log(string.Format("Marking entry '{0}' as read.", aEntry.message));
                                                                await cm.MarkEntryAsRead(oCourse.id, oDiscussionTopics.id, aEntry.id);

                                                                canvasLogger.Log(string.Format("Entry '{0}' marked as read.", aEntry.message));

                                                                canvasLogger.Log(string.Format("Replying to question '{0}' with answer '{1}'.", aEntry.message, answerFound.CanvasAnswer));
                                                                string sResponse = await cm.ReplyToTopicEntry(oCourse.id, oDiscussionTopics.id, aEntry.id, answerFound.CanvasAnswer) as string;

                                                                canvasLogger.Log(string.Format("Question '{0}' replied with answer '{1}'.", aEntry.message, answerFound.CanvasAnswer));
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                canvasLogger.Log(string.Format("No unread topic(s) for discussion '{0}' of course {1} was found", oDiscussionTopics.title, oCourse.name));
                            }
                        }
                    }
                }
            }

            return(null);
        }