| j | 1c53f82 | 2020-06-07 15:32:06 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <title>JSDoc: Source: scripts/canvas-lms.js</title> |
| 6 | |
| 7 | <script src="scripts/prettify/prettify.js"> </script> |
| 8 | <script src="scripts/prettify/lang-css.js"> </script> |
| 9 | <!--[if lt IE 9]> |
| 10 | <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> |
| 11 | <![endif]--> |
| 12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> |
| 13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> |
| 14 | </head> |
| 15 | |
| 16 | <body> |
| 17 | |
| 18 | <div id="main"> |
| 19 | |
| 20 | <h1 class="page-title">Source: scripts/canvas-lms.js</h1> |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | <section> |
| 28 | <article> |
| 29 | <pre class="prettyprint source linenums"><code>/* Power+ Canvas LMS Integration |
| 30 | Copyright (c) 2018-2020 jottocraft. All rights reserved. |
| 31 | Licenced under the GNU General Public License v2.0 (https://github.com/jottocraft/dtps/blob/master/LICENSE) |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Common headers used for Canvas web requests. |
| 36 | * This variable is specific to Canvas LMS integration in DTPS and is not required for other LMS integrations. |
| 37 | */ |
| 38 | dtps.lms.commonHeaders = { Accept: "application/json+canvas-string-ids, application/json" }; |
| 39 | |
| 40 | /** |
| 41 | * Gets user data from Canvas |
| 42 | * |
| 43 | * @return {Promise<User>} A promise which resolves to a User object |
| 44 | */ |
| 45 | dtps.lms.fetchUser = function () { |
| 46 | return new Promise(function (resolve, reject) { |
| 47 | jQuery.ajax({ |
| 48 | url: "/api/v1/users/self", |
| 49 | type: "GET", |
| 50 | headers: dtps.lms.commonHeaders, |
| 51 | success: function (data) { |
| 52 | resolve({ |
| 53 | name: data.name, |
| 54 | id: data.id, |
| 55 | photoURL: data.avatar_url |
| 56 | }); |
| 57 | }, |
| 58 | error: function (err) { |
| 59 | reject(err); |
| 60 | } |
| 61 | }); |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Fetch course data from the LMS |
| 67 | * |
| 68 | * @return {Promise<Class[]>} A promise which resolves to an array of Class objects |
| 69 | */ |
| 70 | dtps.lms.fetchClasses = function () { |
| 71 | return new Promise(function (resolve, reject) { |
| 72 | jQuery.ajax({ |
| 73 | url: "/api/v1/users/self/colors", |
| 74 | type: "GET", |
| 75 | headers: dtps.lms.commonHeaders, |
| 76 | success: function (colorData) { |
| 77 | jQuery.ajax({ |
| 78 | url: "/api/v1/users/self/dashboard_positions", |
| 79 | type: "GET", |
| 80 | headers: dtps.lms.commonHeaders, |
| 81 | success: function (dashboardData) { |
| 82 | jQuery.ajax({ |
| 83 | url: "/api/v1/users/self/courses?per_page=100&enrollment_state=active&include[]=term&include[]=total_scores&include[]=public_description&include[]=total_students&include[]=account&include[]=teachers&include[]=course_image&include[]=syllabus_body&include[]=tabs", |
| 84 | type: "GET", |
| 85 | headers: dtps.lms.commonHeaders, |
| 86 | success: function (courseData) { |
| 87 | //All fetches have been completed successfully |
| 88 | |
| 89 | var courses = []; |
| 90 | |
| 91 | //Add courses from canvas to courses array as a DTPS course object |
| 92 | courseData.forEach((course, index) => { |
| 93 | var dtpsCourse = { |
| 94 | name: course.course_code, |
| 95 | id: course.id, |
| 96 | num: index, |
| 97 | subject: course.course_code.split(" - ")[0], |
| 98 | syllabus: course.syllabus_body, |
| 99 | description: course.public_description, |
| 100 | numStudents: course.total_students, |
| 101 | term: course.course_code.split(" - ")[1], |
| 102 | color: colorData.custom_colors["course_" + course.id], |
| 103 | grade: course.enrollments[0].computed_current_score, |
| 104 | letter: course.enrollments[0].computed_current_grade, |
| 105 | image: course.image_download_url |
| 106 | }; |
| 107 | |
| 108 | if (course.teachers[0]) { |
| 109 | dtpsCourse.teacher = { |
| 110 | name: course.teachers[0] && course.teachers[0].display_name, |
| 111 | id: course.teachers[0] && course.teachers[0].id, |
| 112 | photoURL: course.teachers[0] && course.teachers[0].avatar_image_url |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | courses.push(dtpsCourse); |
| 117 | }); |
| 118 | |
| 119 | //Sort courses array |
| 120 | courses.sort(function (a, b) { |
| 121 | var keyA = dashboardData.dashboard_positions["course_" + a.id], |
| 122 | keyB = dashboardData.dashboard_positions["course_" + b.id]; |
| 123 | |
| 124 | if (keyA < keyB) return -1; |
| 125 | if (keyA > keyB) return 1; |
| 126 | return 0; |
| 127 | }); |
| 128 | |
| 129 | resolve(courses); |
| 130 | }, |
| 131 | error: function (err) { |
| 132 | reject(err); |
| 133 | } |
| 134 | }); |
| 135 | }, |
| 136 | error: function (err) { |
| 137 | reject(err); |
| 138 | } |
| 139 | }); |
| 140 | }, |
| 141 | error: function (err) { |
| 142 | reject(err); |
| 143 | } |
| 144 | }); |
| 145 | }) |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Fetch assignment data for a course from the LMS |
| 150 | * |
| 151 | * @param {string} classID The class ID to fetch assignments for |
| 152 | * @return {Promise<Assignment[]>} A promise which resolves to an array of Assignment objects |
| 153 | */ |
| 154 | dtps.lms.fetchAssignments = function (classID) { |
| 155 | return new Promise(function (resolve, reject) { |
| 156 | jQuery.ajax({ |
| 157 | url: "/api/v1/courses/" + classID + "/outcome_results?per_page=100&include[]=outcomes&user_ids[]=" + dtps.user.id, |
| 158 | type: "GET", |
| 159 | headers: dtps.lms.commonHeaders, |
| 160 | success: function (outcomeData) { |
| 161 | jQuery.ajax({ |
| 162 | url: "/api/v1/courses/" + classID + "/assignment_groups?per_page=100&include[]=assignments&include[]=submissions&include[]=submission", |
| 163 | type: "GET", |
| 164 | headers: dtps.lms.commonHeaders, |
| 165 | success: function (assignmentGroupData) { |
| 166 | //All fetches have been completed successfully |
| 167 | var assignments = []; |
| 168 | |
| 169 | //Add assignments from canvas to assignments array as a DTPS assignment object |
| 170 | assignmentGroupData.forEach(group => { |
| 171 | group.assignments.forEach((assignment, index) => { |
| 172 | //Define dtpsAssignment |
| 173 | var dtpsAssignment = { |
| 174 | title: assignment.name, |
| 175 | body: assignment.description, |
| 176 | id: assignment.id, |
| 177 | dueAt: assignment.due_at, |
| 178 | url: assignment.html_url, |
| 179 | turnedIn: (assignment.submission ? (assignment.submission.submission_type !== null) : false), |
| 180 | late: assignment.submission && assignment.submission.late, |
| 181 | missing: assignment.submission && assignment.submission.missing, |
| 182 | locked: assignment.locked_for_user, |
| 183 | category: assignmentGroupData.length == 1 ? null : group.name, |
| 184 | publishedAt: assignment.created_at, |
| 185 | gradedAt: assignment.submission && assignment.submission.graded_at, |
| 186 | grade: 100, |
| 187 | letter: "A", |
| 188 | value: assignment.points_possible |
| 189 | }; |
| 190 | |
| 191 | //Save score names to an array temporarily |
| 192 | //This is because the scoreNames can only be found in the rubric data from Canvas |
| 193 | //And we don't know which name to use until the outcome score data is processed |
| 194 | var temporaryScoreNames = {}; |
| 195 | |
| 196 | //Add rubric data from Canvas to the dtpsAssignment |
| 197 | if (assignment.rubric) { |
| 198 | assignment.rubric.forEach(canvasRubric => { |
| 199 | //Add rubric array to assignment if it doesn't exist yet |
| 200 | if (!dtpsAssignment.rubric) dtpsAssignment.rubric = []; |
| 201 | |
| 202 | dtpsAssignment.rubric.push({ |
| 203 | title: canvasRubric.description, |
| 204 | description: canvasRubric.long_description, |
| 205 | id: canvasRubric.outcome_id, |
| 206 | value: canvasRubric.points |
| 207 | }); |
| 208 | |
| 209 | temporaryScoreNames[canvasRubric.outcome_id] = {}; |
| 210 | canvasRubric.ratings.forEach(canvasRating => { |
| 211 | temporaryScoreNames[canvasRubric.outcome_id][canvasRating.points] = canvasRating.description; |
| 212 | }); |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | //Add outcome assessment score data from Canvas to the dtpsAssignment |
| 217 | outcomeData.outcome_results.forEach(outcomeResult => { |
| 218 | if (outcomeResult.links.assignment.replace("assignment_", "") == assignment.id) { |
| 219 | //Find which rubric this score belongs to |
| 220 | var rubricIndex = dtpsAssignment.rubric.map(dtpsRubric => dtpsRubric.id).indexOf(outcomeResult.links.learning_outcome); |
| 221 | var dtpsRubric = dtpsAssignment.rubric[rubricIndex]; |
| 222 | |
| 223 | if (dtpsRubric) { |
| 224 | //Now that we know the score, find the scoreName from temporaryScoreNames |
| 225 | var scoreName = undefined; |
| 226 | if (temporaryScoreNames[outcomeResult.links.learning_outcome]) { |
| 227 | //Score names for this outcome exist |
| 228 | scoreName = dtps.cblName(temporaryScoreNames[outcomeResult.links.learning_outcome][outcomeResult.score]); |
| 229 | } |
| 230 | |
| 231 | dtpsRubric.score = outcomeResult.score; |
| 232 | dtpsRubric.scoreName = scoreName; |
| 233 | } |
| 234 | } |
| 235 | }); |
| 236 | |
| 237 | //Add assignment to results array |
| 238 | assignments.push(dtpsAssignment); |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | resolve(assignments); |
| 243 | }, |
| 244 | error: function (err) { |
| 245 | reject(err); |
| 246 | } |
| 247 | }); |
| 248 | }, |
| 249 | error: function (err) { |
| 250 | reject(err); |
| 251 | } |
| 252 | }); |
| 253 | }) |
| 254 | }</code></pre> |
| 255 | </article> |
| 256 | </section> |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | </div> |
| 262 | |
| 263 | <nav> |
| 264 | <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="dtps.html">dtps</a></li><li><a href="dtps.gradeCalc.html">gradeCalc</a></li><li><a href="dtps.lms.html">lms</a></li></ul><h3><a href="global.html">Global</a></h3> |
| 265 | </nav> |
| 266 | |
| 267 | <br class="clear"> |
| 268 | |
| 269 | <footer> |
| 270 | Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Mon May 11 2020 15:31:37 GMT-0700 (Pacific Daylight Time) |
| 271 | </footer> |
| 272 | |
| 273 | <script> prettyPrint(); </script> |
| 274 | <script src="scripts/linenumber.js"> </script> |
| 275 | </body> |
| 276 | </html> |