public ItemInfo SaveContent(int course_id, string new_html, string question_id, string answer_id, bool comment) { switch (type) { case "Page": CanvasPage new_page = CanvasApi.PostNewPageContent(course_id, page.url, new_html); page = new_page; break; case "Discussion": CanvasDiscussionTopic new_topic = CanvasApi.PostNewDiscussionMessage(course_id, discussion.id, new_html); discussion = new_topic; break; case "Assignment": CanvasAssignment new_assignment = CanvasApi.PostNewAssignmentDescription(course_id, assignment.id, new_html); assignment = new_assignment; break; case "Quiz": if (question_id == "") { CanvasQuiz new_quiz = CanvasApi.PostNewQuizDescription(course_id, quiz.id, new_html); quiz = new_quiz; break; } var q = quiz_questions[int.Parse(question_id)]; if (answer_id == "") { CanvasQuizQuesiton new_q = CanvasApi.PostNewQuizQuestionText(course_id, quiz.id, q.id, new_html); q = new_q; break; } if (comment) { q.answers[int.Parse(answer_id)].answer_comments = new_html; CanvasQuizQuesiton new_q = CanvasApi.PostNewQuizQuestionAnswerComment(course_id, quiz.id, q.id, q.answers); quiz_questions[int.Parse(question_id)] = new_q; } else { q.answers[int.Parse(answer_id)].html = new_html; CanvasQuizQuesiton new_q = CanvasApi.PostNewQuizQuestionAnswer(course_id, quiz.id, q.id, q.answers); quiz_questions[int.Parse(question_id)] = new_q; } break; default: break; } return(this); }
public CourseInfo(int course_id) { //Constructor for a canvas course ID number this.CourseIdOrPath = course_id; //Get the course information with API CanvasCourse course_info = CanvasApi.GetCanvasCourse(course_id); CourseName = course_info.name; CourseCode = course_info.course_code; //Need to make sure the HtmlList is initialized so we can store all of the info PageHtmlList = new List <Dictionary <string, string> >(); PageInfoList = new List <Dictionary <string, ItemInfo> >(); var options = new ParallelOptions { MaxDegreeOfParallelism = -1 }; //Begin to loop through all modules of the course Parallel.ForEach(CanvasApi.GetCanvasModules(course_id), options, module => { //Console.WriteLine(module.name); //Loop through all the items for each module Parallel.ForEach(CanvasApi.GetCanvasModuleItems(course_id, module.id), options, item => { wait_for_rate_limit: //The object to connect the item location and its HTML body Dictionary <string, string> LocationAndBody = new Dictionary <string, string>(); Dictionary <string, ItemInfo> PageInfoItem = new Dictionary <string, ItemInfo>(); if (item.url == null) { return; } PageInfoItem[item.url] = new ItemInfo(); PageInfoItem[item.url].type = item.type; //Console.WriteLine(item.title); try //This try block is just in case we are not authroized to access any of these pages { switch (item.type) { //Need to see what type of item it is to determine request needed case "Page": CanvasPage page = CanvasApi.GetCanvasPage(course_id, item.page_url); LocationAndBody[item.url] = page.body; PageInfoItem[item.url].page = page; break; case "Discussion": CanvasDiscussionTopic discussion = CanvasApi.GetCanvasDiscussionTopics(course_id, item.content_id); LocationAndBody[item.url] = discussion.message; PageInfoItem[item.url].discussion = discussion; break; case "Assignment": CanvasAssignment assignment = CanvasApi.GetCanvasAssignments(course_id, item.content_id); LocationAndBody[item.url] = assignment.description; PageInfoItem[item.url].assignment = assignment; break; case "Quiz": CanvasQuiz quiz = CanvasApi.GetCanvasQuizzes(course_id, item.content_id); LocationAndBody[item.url] = quiz.description; PageInfoItem[item.url].quiz = quiz; PageInfoItem[item.url].quiz_questions = new List <CanvasQuizQuesiton>(); try { //Quizes require more as we need to gather question and answer info //Again may be able to see basic quiz but not authorized for quiz questions, this the try block. //Loop through all questions for specific quiz int question_number = 0; foreach (CanvasQuizQuesiton question in CanvasApi.GetCanvasQuizQuesitons(course_id, item.content_id)) { PageInfoItem[item.url].quiz_questions.Add(question); Dictionary <string, string> LAndB = new Dictionary <string, string>(); LAndB[item.url + $"?question_num={question_number}"] = question.question_text; lock (PageHtmlList) { PageHtmlList.Add(LAndB); } int answer_num = 0; int answer_comment_num = 0; //Loop through all answers in the quiz foreach (CanvasQuizQuestionAnswers answer in question.answers) { Dictionary <string, string> answerDict = new Dictionary <string, string>(); answerDict[item.url + $"?question_num={question_number}&answer_num={answer_num}"] = "\n" + answer?.html; lock (PageHtmlList) { PageHtmlList.Add(answerDict); } Dictionary <string, string> commentDict = new Dictionary <string, string>(); commentDict[item.url + $"?question_num={question_number}&answer_comment={answer_comment_num}"] = "\n" + answer?.comments_html; lock (PageHtmlList) { PageHtmlList.Add(commentDict); } answer_comment_num++; answer_num++; } question_number++; } } catch (Exception e) { //Check if the exception was an unauthorized request if (e.Message.Contains("Unauthorized")) { //Console.WriteLine("ERROR: (401) Unauthorized, can not search quiz questions. Skipping..."); } else { //Console.WriteLine("{0}", e); } } break; default: //Console.WriteLine($"Not Supported:\n{item.type}"); LocationAndBody["Empty"] = null; break; } //Add the location and HTML body to the List lock (PageHtmlList) { PageHtmlList.Add(LocationAndBody); } lock (PageInfoList) { PageInfoList.Add(PageInfoItem); } } catch (Exception e) { //Check if it was unauthorized if (e.Message.Contains("Unauthorized")) { //Console.WriteLine($"ERROR: (401) Unauthorized, can not search:\n{item.title}\n{item.type}"); } else if (e.Message.Contains("403")) { Console.WriteLine($"ERROR: (403) Forbidden (Rate Limit Exceedd)"); goto wait_for_rate_limit; } else { //Console.WriteLine("{0}", e); } } }); }); }