-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.gs
478 lines (419 loc) · 13.3 KB
/
utility.gs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* converts column number to letter.
*/
function columnToLetter(column)
{
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
/**
* Gets all the courses from the Classroom account.
*/
function getAllCourses() {
var pageToken = null;
var optionalArgs = {
teacherId : "me",
pageToken: pageToken,
pageSize: 100
};
var courses = [];
while (true) {
var response = Classroom.Courses.list(optionalArgs);
courses = courses.concat(response.courses);
if (!pageToken) {
break;
}
}
return courses;
}
/**
* Gets all the course work associated with a course.
*/
function getAllCourseWork(courseId) {
var pageToken = null;
var optionalArgs = {
pageToken: pageToken,
pageSize: 100
};
var courseWork = [];
//var response = Classroom.Courses.CourseWork.list(courseId);
while (true) {
var response = Classroom.Courses.CourseWork.list(courseId, optionalArgs);
courseWork = courseWork.concat(response.courseWork);
if (!pageToken) {
break;
}
}
courseWork = response.courseWork;
if (courseWork && courseWork.length > 0) {
Logger.log("coursework for course %s, length %s", courseId, courseWork.length);
for (var i = 0; i < courseWork.length; i++) {
Logger.log(courseWork[i].title);
}}
return courseWork;
}
/**
* Deletes all individual course sheets.
*/
function deleteIndividualSheets() {
var classroomSS = SpreadsheetApp.getActiveSpreadsheet();
var sheets = classroomSS.getSheets();
if (sheets.length <= 1) return "Cannot delete the last sheet";
var courses = getAllCourses();
messageToReturn = "Done.";
if (courses && courses.length > 0) {
for (i = 0; i < courses.length; i++) {
sheetToDelete = classroomSS.getSheetByName(courses[i].name);
if (sheetToDelete != null) {
classroomSS.deleteSheet(sheetToDelete);
} else {
messageToReturn = "One or more sheets were not found."
}
}
return messageToReturn;
} else {
return "No courses in classroom.";
}
}
/**
* Deletes the course summary sheet.
*/
function deleteCourseSummarySheet() {
var classroomSS = SpreadsheetApp.getActiveSpreadsheet();
var sheets = classroomSS.getSheets();
if (sheets.length <= 1) return "Cannot delete the last sheet";
sheetToDelete = classroomSS.getSheetByName("Course Summary");
if (sheetToDelete != null) {
classroomSS.deleteSheet(sheetToDelete);
return "Done.";
} else {
return "Sheet does not exist.";
}
}
/**
* Creates a sheet in the given spreadsheet with the given name.
*/
function createSheet(classroomSS, name) {
var courseSheet = classroomSS.getSheetByName(name);
if (courseSheet != null) {
courseSheet.clear();
var charts = courseSheet.getCharts();
for (var i in charts) {
courseSheet.removeChart(charts[i]);
}
}
else {
courseSheet = classroomSS.insertSheet();
courseSheet.setName(name);
}
return courseSheet;
}
/**
* Count the rate at which the students submitted submissions for a course.
*/
function getSubmissionRate(courseId, courseWorkId) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var total = 0;
var totalTurned = 0;
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (j = 0; j < submissions.length; j++) {
if (submissions[j].state == "TURNED_IN" || submissions[j].state == "RETURNED") {
totalTurned++;
}
total++;
}
}
if (total == 0) return "N/A";
var rate = Math.round(totalTurned*100/total) + "%";
return rate;
}
/**
* get median time of submission from when the coursework is created for a course.
*/
function getAverageSubmissionTime(courseId, courseWorkId) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var totalTime = 0;
var totalTurned = 0;
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (var j = 0; j < submissions.length; j++) {
if (submissions[j].state == "TURNED_IN" || submissions[j].state == "RETURNED") {
var timeDiff = 0;
var timeTurned = 0;
var timeCreated = new Date(submissions[j].creationTime);
totalTurned++;
var submissionHistory = submissions[j].submissionHistory;
if (submissionHistory && submissionHistory.length > 0) {
for (z = 0; z < submissionHistory.length; z++) {
if (submissionHistory[z].gradeHistory) {
break;
}
if (submissionHistory[z].stateHistory.state == "TURNED_IN") {
timeTurned = new Date(submissionHistory[z].stateHistory.stateTimestamp);
}
}
if (timeTurned != 0) {
timeDiff = ((timeTurned.getTime() - timeCreated.getTime()) / (1000 * 60 * 60 * 24));
totalTime = totalTime + timeDiff;
}
}
}
}
}
if (totalTurned == 0) return "N/A";
return Math.round(totalTime/totalTurned);
}
/**
* get median time of grading from when submission is turned in to when it is graded
*/
function getAverageGradeTime(courseId, courseWorkId) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var totalTime = 0;
var totalTurned = 0;
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (var j = 0; j < submissions.length; j++) {
if (submissions[j].state == "TURNED_IN" || submissions[j].state == "RETURNED") {
var timeDiff = 0;
var timeGraded = 0;
var timeTurned = 0;
totalTurned++;
var submissionHistory = submissions[j].submissionHistory;
if (submissionHistory && submissionHistory.length > 0) {
for (z = 0; z < submissionHistory.length; z++) {
if (submissionHistory[z].stateHistory && submissionHistory[z].stateHistory.state == "TURNED_IN") {
timeTurned = new Date(submissionHistory[z].stateHistory.stateTimestamp);
}
if (submissionHistory[z].gradeHistory) {
timeGraded = new Date(submissionHistory[z].gradeHistory.gradeTimestamp);
}
}
if (timeGraded != 0 && timeTurned != 0) {
timeDiff = ((timeGraded.getTime() - timeTurned.getTime()) / (1000 * 60 * 60 * 24));
totalTime = totalTime + timeDiff;
}
}
}
}
}
if (totalTurned == 0) return "N/A";
var median = Math.round(totalTime/totalTurned);
return median;
}
/**
* get average grade for a course
*/
function getAverageGrade(courseId, courseWorkId) {
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var submissions = response.studentSubmissions;
var total = 0;
if (submissions && submissions.length > 0) {
for (var i = 0; i < submissions.length; i++) {
if (submissions[i].assignedGrade) {
total = total + submissions[i].assignedGrade;
}
else if (submissions[i].draftGrade) {
total = total + submissions[i].draftGrade;
}
}
return Math.round((total/submissions.length * 100))/100;
} else {
return "N/A";
}
}
/**
* Gets average student grade for a course.
*/
function getAverageStudentGrade(courseId, courseWork, studentId) {
var optionalArgs = {
userId: studentId
};
var total = 0;
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, "-", optionalArgs);
var submissions = response.studentSubmissions;
if (courseWork && courseWork.length > 0) {
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
total = total + submissions[t].assignedGrade;
}
else if (submissions[t].draftGrade) {
total = total + submissions[t].draftGrade;
}
}
return Math.round((total/courseWork.length * 100))/100;
} else {
return 0;
}
} else {
return 'N/A';
}
}
/**
* Gets grades of each coursework for a course.
*/
function getcourseWorkStudentGrade(courseId, courseWork, studentId) {
var optionalArgs = {
userId: studentId
};
var total = 0;
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, "-", optionalArgs);
var submissions = response.studentSubmissions;
var studentcourseWorkGrades = [];
studentcourseWorkGrades[0] = total;
if (courseWork && courseWork.length > 0) {
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
studentcourseWorkGrades[t+1] = submissions[t].assignedGrade;
total = total + studentcourseWorkGrades[t+1];
}
else if (submissions[t].draftGrade) {
studentcourseWorkGrades[t+1] = submissions[t].draftGrade;
total = total + studentcourseWorkGrades[t+1];
}
}
studentcourseWorkGrades[0] = total;
//studentcourseWorkGrades[courseWork.length] = total;
//Browser.msgBox(studentcourseWorkGrades);
return studentcourseWorkGrades;
} else {
return 0;
}
} else {
return 'N/A';
}
//logger.log(studentcourseWorkgrades);
}
/**
* Gets total student grade for a course.
*/
function gettotalStudentGrade(courseId, courseWork, studentId) {
var optionalArgs = {
userId: studentId
};
var total = 0;
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, "-", optionalArgs);
var submissions = response.studentSubmissions;
if (courseWork && courseWork.length > 0) {
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
total = total + submissions[t].assignedGrade;
}
else if (submissions[t].draftGrade) {
total = total + submissions[t].draftGrade;
}
}
return Math.round((total * 100))/100;
} else {
return 0;
}
} else {
return 'N/A';
}
}
/**
* Gets maximum student grade for a course.
*/
function getMaximumGrade(courseId, courseWorkId) {
var max = 0;
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
if (submissions[t].assignedGrade > max) max = submissions[t].assignedGrade;
}
else if (submissions[t].draftGrade) {
if (submissions[t].draftGrade > max) max = submissions[t].draftGrade;
}
}
return max;
}
else {
return "N/A";
}
}
/**
* Gets minimum student grade for a course.
*/
function getMinimumGrade(courseId, courseWorkId) {
var min = 0;
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
if (submissions[t].assignedGrade < min) min = submissions[t].assignedGrade;
}
else if (submissions[t].draftGrade) {
if (submissions[t].draftGrade < min) min = submissions[t].draftGrade;
}
}
return min;
}
else {
return "N/A";
}
}
/**
* Gets median student grade for a course
*/
function getMedianGrade(courseId, courseWorkId) {
var grades = [];
var response = Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, courseWorkId);
var submissions = response.studentSubmissions;
if (submissions && submissions.length > 0) {
for (t = 0; t < submissions.length; t++) {
if (submissions[t].assignedGrade) {
grades.push(submissions[t].assignedGrade);
}
else if (submissions[t].draftGrade) {
grades.push(submissions[t].draftGrade);
}
}
grades.sort();
var median = 0;
var numLength = grades.length;
if (numLength % 2 === 0) {
median = (grades[numLength / 2 - 1] + grades[numLength / 2]) / 2;
} else {
median = grades[(numLength - 1) / 2];
}
return median;
}
else {
return "N/A";
}
}
/**
* Formats the header of the given sheet with the given range.
*/
function formatHeader(sheet, headerRowRange) {
var rangeList = sheet.getRangeList([headerRowRange]);
rangeList.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP);
rangeList.setBackground("#c2dbed");
rangeList.setFontColor("#212b33");
rangeList.setBorder(true, true, true, true, true, true, '#324d63', SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
rangeList.setHorizontalAlignment("center");
rangeList.setVerticalAlignment("middle");
}
/**
* Formats the rows in the table.
*/
function formatTableRows(sheet, rowsRange) {
var rangeList = sheet.getRangeList([rowsRange]);
rangeList.setVerticalAlignment("middle");
rangeList.setFontColor("#212b33");
rangeList.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP);
rangeList.setBorder(true, true, true, true, true, false, '#324d63', SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
sheet.autoResizeColumns(1, 1);
}