-
Notifications
You must be signed in to change notification settings - Fork 0
/
quizCount.js
87 lines (78 loc) · 2.89 KB
/
quizCount.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require('dotenv').config()
const Canvas = require('@kth/canvas-api')
const ObjectsToCsv = require('objects-to-csv')
const token = process.env.CANVAS_API_TOKEN
const url = process.env.CANVAS_API_URL
const canvas = Canvas(url, token)
const output = './quiz-courseList-2021.csv'
const courseArr = []
const exclArr = [104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116]
const config = {
year: 2021,
account: '1'
}
async function saveUniqueToCsv (arr, file) {
const uArr = arr.reduce((unique, o) => {
if (!unique.some(obj => obj.CourseSIS === o.CourseSIS)) {
unique.push(o)
}
return unique
}, [])
const csv = new ObjectsToCsv(uArr)
await csv.toDisk(file)
console.log('-------------------------------------------------------')
console.log('Done printing contents of ' + file + '.')
console.log('-------------------------------------------------------')
console.log('')
}
async function searchCourses () {
const courseSearchUrl = `accounts/${config.account}/courses`
try {
for await (const course of canvas.list(courseSearchUrl)) {
const yearStarted = (new Date(course.start_at)).getFullYear()
if (yearStarted >= config.year || course.start_at === null) {
if (course.workflow_state === 'available' || course.workflow_state === 'completed') {
if (!exclArr.includes(course.account_id)) {
if (course.sis_course_id != null) {
for await (const quiz of canvas.list(`courses/${course.id}/quizzes`)) {
if (!quiz.unpublishable) {
for await (const user of canvas.list(`courses/${course.id}/users?enrollment_role_id=9`)) {
if (user !== 'undefined') {
const courseObj = {
CourseID: course.id,
CourseSIS: course.sis_course_id,
CourseName: course.name,
CourseResponsibleName: user.name,
CourseResponsibleEmail: user.email
}
courseArr.push(courseObj)
console.log(courseObj)
} else {
const courseObj = {
CourseID: course.id,
CourseSIS: course.sis_course_id,
CourseName: course.name,
CourseResponsibleName: 'no course responsible',
CourseResponsibleEmail: 'no course responsible'
}
courseArr.push(courseObj)
console.log(courseObj)
}
}
break
}
}
}
}
}
}
}
} catch (e) {
console.log('CAUGHT AN ERROR: ' + e + '. Continuing...')
}
}
async function start () {
await searchCourses()
await saveUniqueToCsv(courseArr, output)
}
start()