Skip to content

Instantly share code, notes, and snippets.

@acbart
Created August 23, 2023 15:15
Show Gist options
  • Save acbart/e63fba46621ca98d23b424e6bcb00035 to your computer and use it in GitHub Desktop.
Save acbart/e63fba46621ca98d23b424e6bcb00035 to your computer and use it in GitHub Desktop.
Simple snippet for downloading the email/name/UDID/canvas id of students
(async function(){
// https://stackoverflow.com/questions/8735792/how-to-parse-link-header-from-github-api
let linkParser = (linkHeader)=>{
let re = /,[\s]*<(.*?)>;[\s]*rel="next"/g;
let result = re.exec(linkHeader);
if (result == null) {
return null;
}
return result[1];
}
function downloadBlob(content, filename, contentType) {
// Create a blob
var blob = new Blob([content], { type: contentType });
var url = URL.createObjectURL(blob);
// Create a link to download it
var pom = document.createElement('a');
pom.href = url;
pom.setAttribute('download', filename);
pom.click();
}
let course = ENV.context_asset_string;
let courseId = course.split("_")[1];
let base = ENV.DEEP_LINKING_POST_MESSAGE_ORIGIN;
let api = `${base}/api/v1`;
let courseUrl = `${api}/courses/${courseId}`;
let noApiCourseUrl = `${base}/courses/${courseId}`;
// Get all the students
async function getAllStudents() {
let all = [];
let parameters = {
};
let next = courseUrl+'/users/?per_page=100';
do {
let response = await fetch(next, parameters);
all.push(...await response.json());
let links = response.headers.get('link');
next = linkParser(links);
} while (next != null);
return await all;
}
let students = await getAllStudents();
const filename = `emails_${courseId}.csv`;
let defaultHeaders = ['name', 'cid', 'sid', 'email'];
const csv = [defaultHeaders.join(","),
...students.map((user => {
return [user.sortable_name, user.id, user.login_id, user.email]
.map(String) // convert every value to String
.map(v => v.replaceAll('"', '""')) // escape double colons
.map(v => `"${v}"`) // quote it
.join(','); // comma-separated
}))].join("\n");
downloadBlob(csv, filename, "text/csv;charset=utf-8;")
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment