Last active
July 21, 2022 21:27
-
-
Save acbart/1c7c9cd4a296fa6f41b26358292a8c1e to your computer and use it in GitHub Desktop.
Parses uploaded submissions on Canvas to determine group mates, and then resubmits on the students' behalf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.getMultiScripts=function(arr,path){function executeInOrder(scr,code,resolve){ | |
| scr==arr[0]?(arr.shift(),eval(code),resolve(),console.log("executed",scr)): | |
| setTimeout(function(){executeInOrder(scr,code,resolve)},50)} | |
| var _arr=$.map(arr,function(e){return new Promise(r=>{jQuery.ajax({ | |
| type:"GET",url:(path||"")+e,dataType:"text",success:function(c){ | |
| console.log("loaded ",e),executeInOrder(e,c,r)},cache:!0})})}); | |
| return _arr.push($.Deferred(function(e){$(e.resolve)})), | |
| $.when.apply($,_arr)}; | |
| // Load ChartJS | |
| async function loadLibraries() { | |
| return $.getMultiScripts([ | |
| "jszip", | |
| ], 'https://cdn.jsdelivr.net/npm/'); | |
| } | |
| await loadLibraries(); | |
| async function downloadWord(url) { | |
| return fetch(url) | |
| .then(function (response) { // 2) filter on 200 OK | |
| if (response.status === 200 || response.status === 0) { | |
| return Promise.resolve(response.blob()); | |
| } else { | |
| return Promise.reject(new Error(response.statusText)); | |
| } | |
| }) | |
| .then(JSZip.loadAsync); | |
| } | |
| const course_id = `5022987`; | |
| const students = await $.get(`https://canvas.instructure.com/api/v1/courses/${course_id}/students`); | |
| const emailMap = Object.fromEntries(students.map(u => [u.email, u])); | |
| subs = await $.get(`https://canvas.instructure.com/api/v1/courses/${course_id}/students/submissions`, {workflow_state: "submitted", student_ids: "all"}); | |
| const FIND_UD_EMAILS = /([a-z0-9!#$%&'*+\/=?^_`{|}~-]+@udel.edu)/gm; | |
| subs.forEach(async sub => { | |
| if (!sub.attachments) return; | |
| sub.attachments.forEach(async att => { | |
| const zip = await downloadWord(att.url); | |
| console.log(zip); | |
| const mainFile = await zip.file('word/document.xml').async("string"); | |
| const emails = [...mainFile.matchAll(FIND_UD_EMAILS)].map(u => u[0]); | |
| emails.forEach(async email => { | |
| const user = emailMap[email]; | |
| if (!user) {console.error("User not found: ", email); return } | |
| const teammateId = user.id; | |
| const resub = await $.post(`https://canvas.instructure.com/api/v1/courses/${course_id}/assignments/${sub.assignment_id}/submissions`, { | |
| 'submission[submission_type]': 'online_text_entry', | |
| 'submission[body]': att.url, | |
| 'submission[user_id]': teammateId, | |
| 'submission[submitted_at]': sub.submitted_at, | |
| 'comment[text_comment]': "Uploaded automatically by Ada Bot." | |
| }); | |
| console.log(resub); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment