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