| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 1 | /**
|
| 2 | * @file DTPS Canvas LMS Integration
|
| 3 | * @author jottocraft
|
| 4 | *
|
| j | 6e1a7bf | 2026-10-13 03:37:04 +0000 | [diff] [blame] | 5 | * @copyright Copyright (c) jottocraft 2020
|
| j | 40db027 | 2022-01-14 15:18:29 -0800 | [diff] [blame] | 6 | * @license MIT
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 7 | *
|
| 8 | * JSDoc documentation for these LMS functions can be found near the end of core.js
|
| 9 | */
|
| 10 |
|
| 11 | //DTPS LMS configuration for Canvas
|
| 12 | var dtpsLMS = {
|
| 13 | name: "Canvas LMS",
|
| 14 | shortName: "Canvas",
|
| 15 | legalName: "Canvas LMS or Instructure Inc.",
|
| 16 | description: "Power+ integration for Canvas LMS",
|
| 17 | url: "https://www.instructure.com/canvas/",
|
| j | 450fbd4 | 2022-09-01 11:13:11 -0700 | [diff] [blame] | 18 | inboxURL: "/conversations",
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 19 | logo: "https://i.imgur.com/rGjNVoc.png",
|
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 20 | source: "https://src.jottocraft.com/browser/dtps/+/refs/heads/main/scripts/lms/canvas.js"
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 21 | };
|
| 22 |
|
| 23 |
|
| 24 | /**
|
| 25 | * Common headers used for Canvas web requests.
|
| 26 | * This variable is specific to Canvas LMS integration in DTPS and is not required for other LMS integrations.
|
| 27 | */
|
| j | 6984308 | 2023-08-22 12:26:55 -0700 | [diff] [blame] | 28 | dtpsLMS.commonHeaders = { Accept: "application/json+canvas-string-ids, application/json", "From": "jottocraft/dtps <[email protected]>" };
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 29 |
|
| 30 | /**
|
| 31 | * List of teachers from dtpsLMS.fetchClasses for use in other methods
|
| 32 | * This variable is specific to Canvas LMS integration in DTPS and is not required for other LMS integrations.
|
| 33 | */
|
| 34 | dtpsLMS.teacherCache = {};
|
| 35 |
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 36 | /**
|
| 37 | * A queue for API requests
|
| 38 | */
|
| 39 | dtpsLMS.fetchQueue = [];
|
| 40 |
|
| 41 | /**
|
| 42 | * A function that behaves the same way as fetch, although it adds the request to the queue for spacing
|
| 43 | */
|
| 44 | dtpsLMS.fetchWrapper = function () {
|
| 45 | //Start a queue interval if it isn't already started
|
| 46 | if (!dtpsLMS.fetchInterval) {
|
| j | 608526f | 2022-04-26 20:32:47 -0700 | [diff] [blame] | 47 | dtpsLMS.fetchInterval = setInterval(function () {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 48 | //Run request at the start of the queue
|
| 49 | if (dtpsLMS.fetchQueue[0]) {
|
| 50 | dtpsLMS.fetchQueue[0]();
|
| 51 | }
|
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 52 | }, 25);
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 53 | }
|
| 54 |
|
| 55 | return new Promise((resolve, reject) => {
|
| 56 | dtpsLMS.fetchQueue.push(() => {
|
| 57 | dtpsLMS.fetchQueue.shift();
|
| 58 | fetch(...arguments).then(function (d) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 59 | resolve(...arguments);
|
| 60 | }).catch(reject);
|
| 61 | });
|
| 62 | });
|
| 63 | }
|
| 64 |
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 65 | //Fetch userdata from Canvas
|
| 66 | dtpsLMS.fetchUser = function () {
|
| 67 | return new Promise(function (resolve, reject) {
|
| j | 6984308 | 2023-08-22 12:26:55 -0700 | [diff] [blame] | 68 | dtpsLMS.fetchWrapper("/api/v1/users/self", { headers: dtpsLMS.commonHeaders }).then(response => {
|
| 69 | if (response.status !== 200) reject({ action: "login", redirectURL: "/?dtpsLogin=true" });
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 70 |
|
| j | 6984308 | 2023-08-22 12:26:55 -0700 | [diff] [blame] | 71 | return response.json();
|
| 72 | }).then(userData => {
|
| 73 | var user = {
|
| 74 | name: userData.name,
|
| 75 | id: userData.id,
|
| 76 | photoURL: userData.avatar_url
|
| 77 | };
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 78 |
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 79 | dtpsLMS.fetchWrapper("/api/v1/users/self/observees?include[]=avatar_url", { headers: dtpsLMS.commonHeaders }).then(response => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 80 | return response.json();
|
| 81 | }).then(childrenData => {
|
| 82 | if (childrenData && childrenData.length) {
|
| 83 | //Parent account
|
| 84 | user.children = childrenData.map(child => {
|
| 85 | return {
|
| 86 | name: child.name.split(" ")[0],
|
| 87 | id: child.id,
|
| 88 | photoURL: child.avatar_url
|
| 89 | }
|
| 90 | });
|
| 91 | }
|
| 92 |
|
| 93 | resolve(user);
|
| 94 | }).catch(reject);
|
| j | 6984308 | 2023-08-22 12:26:55 -0700 | [diff] [blame] | 95 | });
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 96 | })
|
| 97 | }
|
| 98 |
|
| j | 450fbd4 | 2022-09-01 11:13:11 -0700 | [diff] [blame] | 99 | //Fetch unread message count from Canvas
|
| 100 | dtpsLMS.fetchUnreadMessageCount = function () {
|
| 101 | return new Promise(function (resolve, reject) {
|
| 102 | dtpsLMS.fetchWrapper("/api/v1/conversations/unread_count", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| 103 | resolve(data.unread_count);
|
| 104 | }).catch(reject);
|
| 105 | });
|
| 106 | }
|
| 107 |
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 108 | //Fetch class data from Canvas
|
| 109 | dtpsLMS.fetchClasses = function (userID) {
|
| 110 | return new Promise(function (resolve, reject) {
|
| 111 | Promise.all([
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 112 | dtpsLMS.fetchWrapper("/api/v1/users/" + dtps.user.id + "/colors", { headers: dtpsLMS.commonHeaders }),
|
| 113 | dtpsLMS.fetchWrapper("/api/v1/users/" + userID + "/dashboard_positions", { headers: dtpsLMS.commonHeaders }),
|
| 114 | dtpsLMS.fetchWrapper("/api/v1/users/" + userID + "/courses?per_page=100&enrollment_state=active&include[]=term&include[]=total_scores&include[]=account&include[]=teachers&include[]=course_image&include[]=tabs&include[]=sections", { headers: dtpsLMS.commonHeaders })
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 115 | ]).then(responses => {
|
| 116 | return Promise.all(responses.map(r => r.json()));
|
| 117 | }).then(data => {
|
| 118 | var [colorData, dashboardData, courseData] = data;
|
| 119 | var courses = [];
|
| 120 |
|
| 121 | //Add courses from canvas to courses array as a DTPS course object
|
| 122 | courseData.forEach((course, index) => {
|
| j | 608526f | 2022-04-26 20:32:47 -0700 | [diff] [blame] | 123 | const forceAllClasses = fluid.get("pref-showAllClasses") == "true";
|
| 124 | if (!forceAllClasses) {
|
| 125 | if (course.end_at && (new Date() > new Date(course.end_at))) return;
|
| 126 | if (course.term?.end_at && (new Date() > new Date(course.term?.end_at))) return;
|
| 127 | }
|
| 128 |
|
| j | 6831cfe | 2021-04-19 21:06:38 -0700 | [diff] [blame] | 129 | var termSegments = course.course_code.split(" - ");
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 130 | var dtpsCourse = {
|
| 131 | name: course.course_code,
|
| 132 | id: course.id,
|
| 133 | lmsID: course.id,
|
| 134 | people: !dtps.user.parent,
|
| 135 | userID: userID,
|
| 136 | period: course.sections && course.sections[0] && (course.sections.find(section => /[0-9](?=\(A)/.test(section.name)) || course.sections[0]).name,
|
| j | d80b6e0 | 2021-04-19 20:16:38 -0700 | [diff] [blame] | 137 | section: course.sections && course.sections[0] && (course.sections.find(section => /[0-9](?=\(A)/.test(section.name)) || course.sections[0]).name,
|
| j | 7aa22df | 2021-01-08 10:03:59 -0800 | [diff] [blame] | 138 | subject: window.localStorage["pref-fullNames"] == "true" ? course.course_code : (course.original_name ? course.name : course.course_code.split(" - ")[0]),
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 139 | homepage: course.default_view == "wiki",
|
| j | 6831cfe | 2021-04-19 21:06:38 -0700 | [diff] [blame] | 140 | term: termSegments[termSegments.length - 2],
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 141 | color: colorData.custom_colors["course_" + course.id],
|
| 142 | grade: course.enrollments[0].computed_current_score,
|
| 143 | letter: course.enrollments[0].computed_current_grade,
|
| 144 | image: course.image_download_url,
|
| 145 | newDiscussionThreadURL: '/courses/' + course.id + '/discussion_topics/new',
|
| 146 | pages: course.tabs.map(tab => tab.id).includes("pages"),
|
| 147 | modules: course.tabs.map(tab => tab.id).includes("modules"),
|
| 148 | discussions: true || course.tabs.map(tab => tab.id).includes("discussions"),
|
| j | 324e5f1 | 2021-01-26 13:43:49 -0800 | [diff] [blame] | 149 | endDate: course.end_at,
|
| j | 2566c4e | 2021-10-01 15:25:49 -0700 | [diff] [blame] | 150 | startDate: course.start_at,
|
| 151 | termEndDate: course.term?.end_at,
|
| 152 | termStartDate: course.term?.start_at
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 153 | };
|
| 154 |
|
| 155 | //Save teachers in cache
|
| 156 | dtpsLMS.teacherCache[course.id] = course.teachers;
|
| 157 |
|
| j | d80b6e0 | 2021-04-19 20:16:38 -0700 | [diff] [blame] | 158 | if (course.teachers.length == 1) {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 159 | dtpsCourse.teacher = {
|
| 160 | name: course.teachers[0] && course.teachers[0].display_name,
|
| 161 | id: course.teachers[0] && course.teachers[0].id,
|
| 162 | photoURL: course.teachers[0] && course.teachers[0].avatar_image_url
|
| 163 | };
|
| j | d80b6e0 | 2021-04-19 20:16:38 -0700 | [diff] [blame] | 164 | } else {
|
| 165 | var matches = 0;
|
| 166 | course.teachers.forEach(teacher => {
|
| 167 | teacher.display_name.split(" ").forEach((fragment) => {
|
| 168 | if (dtpsCourse.name.includes(fragment)) {
|
| 169 | dtpsCourse.teacher = {
|
| 170 | name: teacher.display_name,
|
| 171 | id: teacher.id,
|
| 172 | photoURL: teacher.avatar_image_url
|
| 173 | };
|
| 174 | matches++;
|
| 175 | }
|
| 176 | });
|
| 177 | });
|
| 178 | if (matches > 1) delete dtpsCourse.teacher;
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 179 | }
|
| 180 |
|
| 181 | courses.push(dtpsCourse);
|
| 182 | });
|
| 183 |
|
| 184 | //Sort courses array
|
| 185 | courses.sort(function (a, b) {
|
| 186 | var keyA = dashboardData.dashboard_positions["course_" + a.id],
|
| 187 | keyB = dashboardData.dashboard_positions["course_" + b.id];
|
| 188 |
|
| 189 | if (keyA < keyB) return -1;
|
| 190 | if (keyA > keyB) return 1;
|
| 191 | return 0;
|
| 192 | });
|
| 193 |
|
| 194 | resolve(courses);
|
| 195 | }).catch(reject);
|
| 196 | })
|
| 197 | }
|
| 198 |
|
| 199 | //Fetches assignment data from Canvas
|
| 200 | dtpsLMS.fetchAssignments = function (userID, classID) {
|
| 201 | return new Promise(function (resolve, reject) {
|
| 202 | Promise.all([
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 203 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/students/submissions?include[]=rubric_assessment&include[]=submission_comments&per_page=100&student_ids[]=" + userID, { headers: dtpsLMS.commonHeaders }),
|
| 204 | dtpsLMS.fetchWrapper("/api/v1/users/" + userID + "/courses/" + classID + "/assignments?per_page=100", { headers: dtpsLMS.commonHeaders })
|
| 205 | ]).then((responses) => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 206 | return Promise.all(responses.map(r => r.json()));
|
| 207 | }).then(data => {
|
| 208 | var [submissionData, assignmentData] = data;
|
| 209 |
|
| 210 | //All fetches have been completed successfully
|
| 211 | var assignments = [];
|
| 212 |
|
| 213 | //Add assignments from canvas to assignments array as a DTPS assignment object
|
| 214 | assignmentData.forEach((assignment, index) => {
|
| 215 | //Define dtpsAssignment
|
| 216 | var dtpsAssignment = {
|
| 217 | title: assignment.name,
|
| 218 | body: assignment.description,
|
| 219 | id: assignment.id,
|
| 220 | dueAt: assignment.due_at,
|
| 221 | url: assignment.html_url,
|
| 222 | locked: assignment.locked_for_user,
|
| 223 | publishedAt: assignment.created_at,
|
| 224 | value: assignment.points_possible
|
| 225 | };
|
| 226 |
|
| 227 | //Save score names to an array temporarily
|
| 228 | //This is because the scoreNames can only be found in the rubric data from Canvas
|
| 229 | //And we don't know which name to use until the outcome score data is processed
|
| 230 | var temporaryScoreNames = {};
|
| 231 |
|
| 232 | //Add rubric data from Canvas to the dtpsAssignment
|
| 233 | if (assignment.rubric) {
|
| 234 | assignment.rubric.forEach(canvasRubric => {
|
| 235 | //Add rubric array to assignment if it doesn't exist yet
|
| 236 | if (!dtpsAssignment.rubric) dtpsAssignment.rubric = [];
|
| 237 |
|
| 238 | dtpsAssignment.rubric.push({
|
| 239 | title: canvasRubric.description,
|
| 240 | description: canvasRubric.long_description,
|
| 241 | id: canvasRubric.id,
|
| 242 | value: canvasRubric.points,
|
| 243 | outcome: canvasRubric.outcome_id,
|
| 244 | assignmentTitle: assignment.name,
|
| j | 4d933ce | 2022-04-08 21:49:55 +0000 | [diff] [blame] | 245 | assignmentID: assignment.id,
|
| 246 | assignmentDue: assignment.due_at
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 247 | });
|
| 248 |
|
| 249 | temporaryScoreNames[canvasRubric.id] = {};
|
| 250 | canvasRubric.ratings.forEach(canvasRating => {
|
| 251 | temporaryScoreNames[canvasRubric.id][canvasRating.points] = canvasRating.description;
|
| 252 | });
|
| 253 | })
|
| 254 | }
|
| 255 |
|
| 256 | //Add submission data from Canvas to the dtpsAssignment
|
| 257 | submissionData.forEach(submission => {
|
| 258 | if (submission.assignment_id == assignment.id) {
|
| 259 | //Add scores from this submission to the rubric
|
| 260 | if (submission.rubric_assessment) {
|
| j | 2566c4e | 2021-10-01 15:25:49 -0700 | [diff] [blame] | 261 | let matches = 0;
|
| 262 | if (dtpsAssignment.rubric) {
|
| 263 | dtpsAssignment.rubric.forEach(rubric => {
|
| 264 | if (submission.rubric_assessment[rubric.id]) {
|
| 265 | rubric.score = submission.rubric_assessment[rubric.id].points;
|
| 266 | rubric.scoreName = temporaryScoreNames[rubric.id][rubric.score];
|
| 267 | matches++;
|
| 268 | }
|
| 269 | });
|
| 270 | }
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 271 | }
|
| 272 |
|
| 273 | //Check for turned in, late, missing, gradedAt, and feedback
|
| 274 | dtpsAssignment.turnedIn = submission.submission_type !== null;
|
| j | 4d933ce | 2022-04-08 21:49:55 +0000 | [diff] [blame] | 275 | if (dtpsAssignment.turnedIn && dtpsAssignment.rubric) dtpsAssignment.rubric.forEach(r => r.assignmentSubmitted = true);
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 276 | dtpsAssignment.late = submission.late;
|
| 277 | dtpsAssignment.missing = submission.missing;
|
| 278 | dtpsAssignment.gradedAt = submission.graded_at;
|
| 279 | dtpsAssignment.grade = submission.score;
|
| 280 | var letter = submission.grade;
|
| 281 | if (letter == "complete") letter = "✔️";
|
| 282 | if (letter == "incomplete") letter = "❌";
|
| 283 | if (isNaN(letter)) dtpsAssignment.letter = letter; //letter cannot be a number
|
| 284 |
|
| 285 | //Check for submission comments
|
| 286 | if (submission.submission_comments) {
|
| 287 | //Add feedback array to assignment
|
| 288 | dtpsAssignment.feedback = [];
|
| 289 |
|
| 290 | //Add each comment to feedback array
|
| 291 | submission.submission_comments.forEach(comment => {
|
| 292 | var feedback = {
|
| 293 | comment: comment.comment
|
| 294 | };
|
| 295 |
|
| 296 | //Add author to feedback if found
|
| 297 | if (comment.author) {
|
| 298 | feedback.author = {
|
| 299 | name: comment.author.display_name,
|
| 300 | id: comment.author.id,
|
| 301 | photoURL: comment.author.avatar_image_url
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | dtpsAssignment.feedback.push(feedback)
|
| 306 | });
|
| 307 | }
|
| 308 | }
|
| 309 | });
|
| 310 |
|
| 311 | //Add assignment to results array
|
| 312 | assignments.push(dtpsAssignment);
|
| 313 | });
|
| 314 |
|
| 315 | resolve(assignments);
|
| 316 | }).catch(reject);
|
| 317 | });
|
| 318 | }
|
| 319 |
|
| 320 | //Fetches modules data from Canvas
|
| 321 | dtpsLMS.fetchModules = function (userID, classID) {
|
| 322 | return new Promise(function (resolve, reject) {
|
| 323 | Promise.all([
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 324 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/modules?include[]=items&include[]=content_details", { headers: dtpsLMS.commonHeaders }),
|
| 325 | dtpsLMS.fetchWrapper("/courses/" + classID + "/modules/progressions", { headers: dtpsLMS.commonHeaders })
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 326 | ]).then(responses => {
|
| 327 | return Promise.all(responses.map(r => r.json()));
|
| 328 | }).then(data => {
|
| 329 | var [modulesData, progressionData] = data;
|
| 330 |
|
| 331 | //Get collapsed modules from progression data
|
| 332 | var collapsedModules = {};
|
| 333 |
|
| 334 | //Loop over progression data
|
| 335 | progressionData.forEach(prog => {
|
| 336 | //Store collapsed state
|
| 337 | collapsedModules[prog.context_module_progression.context_module_id] = prog.context_module_progression.collapsed;
|
| 338 | })
|
| 339 |
|
| 340 | //Parse data from Canvas
|
| 341 | var dtpsModules = modulesData.map(module => {
|
| 342 | //Create module items array
|
| 343 | var moduleItems = [];
|
| 344 |
|
| 345 | //Add module items to array
|
| 346 | module.items.forEach(item => {
|
| 347 | if (item.type.toUpperCase() == "ASSIGNMENT") {
|
| 348 | moduleItems.push({
|
| 349 | type: "assignment",
|
| 350 | title: item.title,
|
| 351 | id: item.content_id,
|
| 352 | indent: item.indent,
|
| 353 | url: item.html_url,
|
| 354 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 355 | })
|
| 356 | } else if (item.type.toUpperCase() == "PAGE") {
|
| 357 | moduleItems.push({
|
| 358 | type: "page",
|
| 359 | title: item.title,
|
| 360 | id: item.page_url,
|
| 361 | indent: item.indent,
|
| 362 | url: item.html_url,
|
| 363 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 364 | })
|
| 365 | } else if (item.type.toUpperCase() == "DISCUSSION") {
|
| 366 | moduleItems.push({
|
| 367 | type: "discussion",
|
| 368 | title: item.title,
|
| 369 | id: item.content_id,
|
| 370 | indent: item.indent,
|
| 371 | url: item.html_url,
|
| 372 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 373 | })
|
| 374 | } else if (item.type.toUpperCase() == "EXTERNALURL") {
|
| 375 | moduleItems.push({
|
| 376 | type: "url",
|
| 377 | title: item.title,
|
| 378 | url: item.external_url,
|
| 379 | indent: item.indent,
|
| 380 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 381 | })
|
| 382 | } else if (item.type.toUpperCase() == "EXTERNALTOOL") {
|
| 383 | moduleItems.push({
|
| 384 | type: "embed",
|
| 385 | title: item.title,
|
| 386 | url: item.html_url,
|
| 387 | indent: item.indent,
|
| 388 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 389 | });
|
| 390 | } else if (item.type.toUpperCase() == "SUBHEADER") {
|
| 391 | moduleItems.push({
|
| 392 | type: "header",
|
| 393 | title: item.title,
|
| 394 | indent: item.indent,
|
| 395 | completed: item.completion_requirement && item.completion_requirement.completed
|
| 396 | })
|
| 397 | }
|
| 398 | })
|
| 399 |
|
| 400 | return {
|
| 401 | id: module.id,
|
| 402 | title: module.name,
|
| 403 | collapsed: collapsedModules[module.id] || false,
|
| 404 | items: moduleItems
|
| 405 | }
|
| 406 | })
|
| 407 |
|
| 408 | //Resolve with module data
|
| 409 | resolve(dtpsModules);
|
| 410 | }).catch(reject);
|
| 411 | });
|
| 412 | }
|
| 413 |
|
| 414 | //Collapses a module in Canvas
|
| 415 | dtpsLMS.collapseModule = function (classID, modID, collapsed) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 416 | return dtpsLMS.fetchWrapper("/courses/" + classID + "/modules/" + modID + "/collapse", {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 417 | method: "POST",
|
| 418 | headers: {
|
| 419 | "Accept": "application/json, text/javascript, application/json+canvas-string-ids, */*; q=0.01",
|
| 420 | "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
| 421 | "X-CSRF-Token": decodeURIComponent(document.cookie).split("_csrf_token=")[1].split(";")[0]
|
| 422 | },
|
| 423 | body: "_method=POST&collapse=" + (collapsed ? 1 : 0) + "&authenticity_token=" + decodeURIComponent(document.cookie).split("_csrf_token=")[1].split(";")[0]
|
| 424 | });
|
| 425 | }
|
| 426 |
|
| 427 | //Collapses all modules in Canvas
|
| 428 | dtpsLMS.collapseAllModules = function (classID, collapsed) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 429 | return dtpsLMS.fetchWrapper("/courses/" + classID + "/collapse_all_modules", {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 430 | method: "POST",
|
| 431 | headers: {
|
| 432 | "Accept": "application/json, text/javascript, application/json+canvas-string-ids, */*; q=0.01",
|
| 433 | "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
| 434 | "X-CSRF-Token": decodeURIComponent(document.cookie).split("_csrf_token=")[1].split(";")[0]
|
| 435 | },
|
| 436 | body: "_method=POST&collapse=" + (collapsed ? 1 : 0) + "&authenticity_token=" + decodeURIComponent(document.cookie).split("_csrf_token=")[1].split(";")[0]
|
| 437 | });
|
| 438 | }
|
| 439 |
|
| 440 | //Fetches announcement data from Canvas
|
| 441 | dtpsLMS.fetchAnnouncements = function (classID) {
|
| 442 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 443 | dtpsLMS.fetchWrapper("/api/v1/announcements?context_codes[]=course_" + classID, { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 444 | var dtpsAnnouncements = data.map(function (announcement) {
|
| 445 | return {
|
| 446 | title: announcement.title,
|
| 447 | postedAt: announcement.created_at,
|
| 448 | body: announcement.message,
|
| 449 | url: announcement.html_url
|
| 450 | }
|
| 451 | });
|
| 452 |
|
| 453 | resolve(dtpsAnnouncements);
|
| 454 | }).catch(reject);
|
| 455 | });
|
| 456 | }
|
| 457 |
|
| 458 | //Fetches user data from Canvas
|
| 459 | dtpsLMS.fetchUsers = function (classID) {
|
| 460 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 461 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/sections?include[]=avatar_url&include[]=students&per_page=99", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 462 | var teachers = dtpsLMS.teacherCache[classID];
|
| 463 | var sections = [{
|
| 464 | title: "Teachers",
|
| 465 | id: "lms.dtps.canvas-teachers",
|
| 466 | users: []
|
| 467 | }];
|
| 468 |
|
| 469 | teachers.forEach(teacher => {
|
| 470 | sections[0].users.push({
|
| 471 | name: teacher.display_name,
|
| 472 | id: teacher.id,
|
| 473 | photoURL: teacher.avatar_image_url,
|
| 474 | url: "/courses/" + classID + "/users/" + teacher.id
|
| 475 | });
|
| 476 | });
|
| 477 |
|
| 478 | data.forEach(section => {
|
| 479 | if (!section.students) return;
|
| 480 | var users = [];
|
| 481 | section.students.forEach(student => {
|
| 482 | users.push({
|
| 483 | name: student.short_name,
|
| 484 | id: student.id,
|
| 485 | photoURL: student.avatar_url,
|
| 486 | url: "/courses/" + classID + "/users/" + student.id
|
| 487 | });
|
| 488 | });
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 489 | sections.push({
|
| 490 | title: section.name,
|
| 491 | id: section.id,
|
| 492 | users
|
| 493 | });
|
| 494 | });
|
| 495 | resolve(sections);
|
| 496 | }).catch(reject);
|
| 497 | });
|
| 498 | }
|
| 499 |
|
| 500 | //Fetches homepage data from Canvas
|
| 501 | dtpsLMS.fetchHomepage = function (classID) {
|
| 502 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 503 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/front_page", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 504 | resolve(data.body);
|
| 505 | }).catch(reject);
|
| 506 | });
|
| 507 | }
|
| 508 |
|
| 509 | //Fetches discussion thread data from Canvas
|
| 510 | dtpsLMS.fetchDiscussionThreads = function (classID) {
|
| 511 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 512 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/discussion_topics", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 513 | var dtpsDiscussionThreads = data.map(function (thread) {
|
| 514 | return {
|
| 515 | title: thread.title,
|
| 516 | id: thread.id,
|
| 517 | locked: thread.locked_for_user
|
| 518 | }
|
| 519 | });
|
| 520 |
|
| 521 | resolve(dtpsDiscussionThreads);
|
| 522 | }).catch(reject);
|
| 523 | })
|
| 524 | }
|
| 525 |
|
| 526 | //Fetches discussion thread posts from Canvas
|
| 527 | dtpsLMS.fetchDiscussionPosts = function (classID, threadID) {
|
| 528 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 529 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/discussion_topics/" + threadID + "/", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(threadData => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 530 | //Check if this discussion is a group discussion
|
| 531 | if (threadData.group_topic_children && threadData.group_topic_children.length) {
|
| 532 | //This discussion is probably a group discussion, check groups, then fetch posts
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 533 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/groups?only_own_groups=true", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(groupData => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 534 | //Get array of group IDs
|
| 535 | var myGroups = groupData.map(group => group.id);
|
| 536 |
|
| 537 | //Set default group and discussion ID variables
|
| 538 | var groupID = null;
|
| 539 | var groupDiscussionID = null;
|
| 540 |
|
| 541 | //Check every group this discussion has and see if one of them is in myGroups, set that group as the group to fetch posts for
|
| 542 | threadData.group_topic_children.forEach(group => {
|
| 543 | if (myGroups.includes(group.group_id)) {
|
| 544 | groupID = group.group_id;
|
| 545 | groupDiscussionID = group.id;
|
| 546 | }
|
| 547 | });
|
| 548 |
|
| 549 | if (!groupID || !groupDiscussionID) {
|
| 550 | //Couldn't find a group match, fetch class discussion
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 551 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/discussion_topics/" + threadID + "/view", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(responsesData => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 552 | parseResponse(responsesData, "/courses/" + classID + "/discussion_topics/" + threadID);
|
| 553 | });
|
| 554 | } else {
|
| 555 | //Group match found, fetch group discussion
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 556 | dtpsLMS.fetchWrapper("/api/v1/groups/" + groupID + "/discussion_topics/" + groupDiscussionID + "/view", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(responsesData => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 557 | parseResponse(responsesData, "/groups/" + groupID + "/discussion_topics/" + groupDiscussionID);
|
| 558 | });
|
| 559 | }
|
| 560 | });
|
| 561 | } else {
|
| 562 | //Not a group discussion, directly fetch posts
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 563 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/discussion_topics/" + threadID + "/view", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(responsesData => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 564 | parseResponse(responsesData, "/courses/" + classID + "/discussion_topics/" + threadID);
|
| 565 | });
|
| 566 | }
|
| 567 |
|
| 568 |
|
| 569 | //This function handles the response from Canavs and returns the data to DTPS
|
| 570 | //BaseURL is the baseURL of the thread, since this might be different for group discussions
|
| 571 | function parseResponse(responsesData, baseURL) {
|
| 572 | //Define discussions post array
|
| 573 | var dtpsDiscussionPosts = [];
|
| 574 |
|
| 575 | //Define the initial post
|
| 576 | var initialPost = {
|
| 577 | id: threadID,
|
| 578 | body: threadData.message,
|
| 579 | postedAt: threadData.created_at,
|
| 580 | replyURL: baseURL
|
| 581 | };
|
| 582 |
|
| 583 | //Check for author
|
| 584 | if (threadData.author && threadData.author.display_name) {
|
| 585 | initialPost.author = {
|
| 586 | name: threadData.author.display_name,
|
| 587 | id: threadData.author.id,
|
| 588 | photoURL: threadData.author.avatar_image_url
|
| 589 | };
|
| 590 | }
|
| 591 |
|
| 592 | //Add initial post to array
|
| 593 | dtpsDiscussionPosts.push(initialPost);
|
| 594 |
|
| 595 | if (responsesData.view) {
|
| 596 | //Get thread author information
|
| 597 | var people = {};
|
| 598 | if (responsesData.participants) {
|
| 599 | responsesData.participants.forEach(participant => {
|
| 600 | people[participant.id] = participant;
|
| 601 | })
|
| 602 | }
|
| 603 |
|
| 604 | //If there are posts found from the second request, add those as well
|
| 605 | responsesData.view.forEach(function (post) {
|
| 606 | if (!post.deleted) {
|
| 607 | var replies = [];
|
| 608 |
|
| 609 | //Get replies for this post
|
| 610 | if (post.replies) {
|
| 611 | //If this post has replies, flatten them into a single array
|
| 612 | function addReplies(arr, depth) {
|
| 613 | //Loop over replies to add
|
| 614 | arr.forEach(reply => {
|
| 615 | //Add this reply to the array
|
| 616 | if (!reply.deleted) {
|
| 617 | //Define reply object
|
| 618 | var dtpsReply = {
|
| 619 | id: reply.id,
|
| 620 | body: reply.message,
|
| 621 | postedAt: reply.created_at,
|
| 622 | replyURL: baseURL + "/entry-" + reply.id,
|
| 623 | depth: depth
|
| 624 | };
|
| 625 |
|
| 626 | //Check for reply author
|
| 627 | if (people[reply.user_id]) {
|
| 628 | dtpsReply.author = {
|
| 629 | name: people[reply.user_id].display_name,
|
| 630 | id: reply.user_id,
|
| 631 | photoURL: people[reply.user_id].avatar_image_url
|
| 632 | }
|
| 633 | }
|
| 634 |
|
| 635 | //Add reply to flattened array
|
| 636 | replies.push(dtpsReply);
|
| 637 | }
|
| 638 |
|
| 639 | //Add nested replies to array
|
| 640 | if (reply.replies) addReplies(reply.replies, depth + 1);
|
| 641 | });
|
| 642 | }
|
| 643 |
|
| 644 | addReplies(post.replies, 0);
|
| 645 | }
|
| 646 |
|
| 647 | //Define post object
|
| 648 | var dtpsPost = {
|
| 649 | id: post.id,
|
| 650 | body: post.message,
|
| 651 | postedAt: post.created_at,
|
| 652 | replies: replies,
|
| 653 | replyURL: baseURL + "/entry-" + post.id
|
| 654 | };
|
| 655 |
|
| 656 | //Check for post author
|
| 657 | if (people[post.user_id]) {
|
| 658 | dtpsPost.author = {
|
| 659 | name: people[post.user_id].display_name,
|
| 660 | id: post.user_id,
|
| 661 | photoURL: people[post.user_id].avatar_image_url
|
| 662 | }
|
| 663 | }
|
| 664 |
|
| 665 | //Add post to array
|
| 666 | dtpsDiscussionPosts.push(dtpsPost);
|
| 667 | }
|
| 668 | });
|
| 669 | }
|
| 670 |
|
| 671 | resolve({
|
| 672 | title: threadData.title,
|
| 673 | id: threadData.id,
|
| 674 | locked: threadData.locked_for_user,
|
| 675 | posts: dtpsDiscussionPosts
|
| 676 | });
|
| 677 | }
|
| 678 | }).catch(reject);
|
| 679 | });
|
| 680 | }
|
| 681 |
|
| 682 | //Fetches pages list data from Canvas
|
| 683 | dtpsLMS.fetchPages = function (classID) {
|
| 684 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 685 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/pages?per_page=100", { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 686 | var dtpsPages = data.map(function (page) {
|
| 687 | var dtpsPage = {
|
| 688 | title: page.title,
|
| 689 | id: page.url
|
| 690 | };
|
| 691 |
|
| 692 | return dtpsPage;
|
| 693 | });
|
| 694 |
|
| 695 | resolve(dtpsPages);
|
| 696 | }).catch(reject);
|
| 697 | })
|
| 698 | }
|
| 699 |
|
| 700 | //Fetches pages list data from Canvas
|
| 701 | dtpsLMS.fetchPageContent = function (classID, pageID) {
|
| 702 | return new Promise(function (resolve, reject) {
|
| j | 5847199 | 2021-01-17 19:29:37 -0800 | [diff] [blame] | 703 | dtpsLMS.fetchWrapper("/api/v1/courses/" + classID + "/pages/" + pageID, { headers: dtpsLMS.commonHeaders }).then(response => response.json()).then(data => {
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 704 | //Resolve with full page object
|
| 705 | var dtpsPage = {
|
| 706 | title: data.title,
|
| 707 | id: data.url,
|
| 708 | updatedAt: data.updated_at,
|
| 709 | content: data.body
|
| 710 | };
|
| 711 |
|
| 712 | //Check for page author
|
| 713 | if (data.last_edited_by) {
|
| 714 | dtpsPage.author = {
|
| 715 | name: data.last_edited_by.display_name,
|
| 716 | id: data.last_edited_by.id,
|
| 717 | photoURL: data.last_edited_by.avatar_image_url
|
| 718 | }
|
| 719 | }
|
| 720 |
|
| 721 | resolve(dtpsPage);
|
| 722 | }).catch(reject);
|
| 723 | })
|
| 724 | }
|
| 725 |
|
| j | 7940eec | 2020-12-28 14:00:41 -0600 | [diff] [blame] | 726 | //Load Power+
|
| j | 2fcf4e5 | 2021-03-11 14:37:26 -0800 | [diff] [blame] | 727 | jQuery.getScript(window.dtpsBaseURL + "/scripts/core.js"); |