-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCanvas Course Event Manager.user.js
363 lines (362 loc) · 14.5 KB
/
Canvas Course Event Manager.user.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
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
// ==UserScript==
// @name Canvas Course Event Manager
// @namespace https://github.com/sukotsuchido/CanvasUserScripts
// @version 0.55
// @description A Canvas UserScript to manage course events
// @author Chad Scott ([email protected])
// @include https://*.instructure.com/calendar*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
var assocRegex3 = new RegExp('^/calendar');
var errors = [];
var courseId = '';
var termId = '';
var courses = [];
var allCourses = [];
var wholeName = '';
var array =[];
var user = '';
/* role setup */
var roles = ENV.current_user_roles;
var buttonRoles = ["admin", "teacher", "root_admin"];
var test1 = buttonRoles.some(el => roles.includes(el));
if( (test1 === true) && (assocRegex3.test(window.location.pathname))){
add_button();
}
function add_button() {
var parent = document.querySelector('aside#right-side');
if (parent) {
var el = parent.querySelector('#manage_events');
if (!el) {
el = document.createElement('button');
el.classList.add('Button');
el.type = 'button';
el.id = 'manage_events';
var icon = document.createElement('i');
icon.classList.add('icon-edit');
el.appendChild(icon);
var txt = document.createTextNode(' Manage Course Events');
el.appendChild(txt);
el.addEventListener('click', openDialog);
parent.appendChild(el);
}
}
}
function getCourses(){
// Reset global variable errors
errors= [];
var url = "/api/v1/users/self/courses?include[]=term&per_page=75"; // change self to specific user number for testing
$.ajax({
'async': true,
'type': "GET",
'global': true,
'dataType': 'JSON',
'data': JSON.stringify(courses),
'contentType': "application/json",
'url': url,
'success': function(courses){
allCourses = Array.from(courses.reduce((m, t) => m.set(t.id, t), new Map()).values());
var toAppend = '';
var select = document.getElementById('event_course');
select.options.length = 0; // clear out existing items
$.each(allCourses, function(i, o){
if(o.name !== undefined){
toAppend += '<option value="'+o.id+'">'+o.name+'</option>';
}
});
var blank ='';
blank += '<option value="">Please select</option>';
$('#event_course').append(blank);
$('#event_course').append(toAppend);
}
});
}
function getEvents(){
// Reset global variable errors
errors= [];
courseId = document.getElementById('event_course').value;
var url = "/api/v1/users/self/calendar_events?per_page=150&all_events=true&context_codes[]=course_"+courseId;
$.ajax({
'async': true,
'type': "GET",
'global': true,
'dataType': 'JSON',
'data': JSON.stringify(courses),
'contentType': "application/json",
'url': url,
'success': function (data, textStatus, request) {
var toAppend;
var clear = document.getElementById('inner_table');
if (clear.innerHTML !== null){
clear.innerHTML = "";
}
$.each(data,function(i,o){
//start date
var date = new Date(o.start_at);
var day = date.getDate();
var year = date.getFullYear();
var month = date.getMonth()+1;
var dateStr = month+"/"+day+"/"+year;
//end date
var date2 = new Date(o.end_at);
var day2 = date2.getDate();
var year2 = date2.getFullYear();
var month2 = date2.getMonth()+1;
var dateStr2 = month2+"/"+day2+"/"+year2;
toAppend += '<tr><td><input type="checkbox" id="'+o.id+'" name="events" value="'+o.id+'"></td><td>'+o.title+'</td><td>'+dateStr+'</td><td>'+dateStr2+'</td></tr>';
});
$('#table_header').append(toAppend);
}
});
}
function deleteEvents(){
$.each(array, function(index,item){
var eventId = item;
var url = "/api/v1/calendar_events/"+eventId;
$.ajax({
'async': true,
'type': "DELETE",
'global': true,
'dataType': 'JSON',
'data': JSON.stringify(),
'contentType': "application/json",
'url': url,
'success': open_success_dialog
});
});
window.location.reload(true);
}
function removeDates(){
$.each(array, function(index,item){
var eventId = item;
var url = "/api/v1/calendar_events/"+eventId+"?calendar_event[start_at]=null&calendar_event[end_at]=null";
$.ajax({
'async': true,
'type': "PUT",
'global': true,
'dataType': 'JSON',
'data': JSON.stringify(),
'contentType': "application/json",
'url': url,
'success': open_success_dialog
});
});
window.location.reload(true);
}
function createDialog() {
var el = document.querySelector('#events_dialog');
if (!el) {
el = document.createElement('div');
el.id = 'events_dialog';
//Parent Course selection
var el2 = document.createElement('div');
el2.classList.add('ic-Form-control');
el.appendChild(el2);
var label = document.createElement('label');
label.htmlFor = 'event_course';
label.textContent = 'Step 1: Select Course:';
label.classList.add('ic-Label');
el2.appendChild(label);
var select = document.createElement('select');
select.id = 'event_course';
select.classList.add('ic-Input');
select.onchange = getEvents;
el2.appendChild(select);
//Events Table
var table = document.createElement('TABLE');
table.id = 'table_header';
table.style.width = '100%';
table.classList.add("ic-Table", "ic-Table--hover-row", "ic-Table--striped");
el.appendChild(table);
var tr = document.createElement('TR');
table.appendChild(tr);
var th = document.createElement('TH');
var input = document.createElement('input');
input.type = 'checkbox';
input.name = 'select-all';
input.id = 'select-all';
input.onchange = setEvents;
th.appendChild(input);
th.classList.add('ic-Checkbox-group');
tr.appendChild(th);
th = document.createElement('TH');
th.textContent = 'Event Title';
tr.appendChild(th);
th = document.createElement('TH');
th.textContent = 'Start Date';
tr.appendChild(th);
th = document.createElement('TH');
th.textContent = 'End Date';
tr.appendChild(th);
var tbody = document.createElement('tbody');
tbody.id = 'inner_table';
tbody.onchange= setEvents;
table.appendChild(tbody);
var notice = document.createElement('div');
notice.id = 'app';
/*notice.id = 'notice';
notice.style = 'text-align: right';
notice.style.padding = '5px';
notice.style.font ='bold 14px arial,serif';
notice.textContent ='First 100 Events Only';
*/
el.appendChild(notice);
//HR
var hr = document.createElement('HR');
el.appendChild(hr);
//selected action list
var el3 = document.createElement('div');
el3.innerHTML ='<fieldset class="ic-Fieldset ic-Fieldset--radio-checkbox"><legend class="ic-Legend">For Selected Events:</legend><div class="ic-Form-control ic-Form-control--radio ic-Form-control--radio-inline"><div class="ic-Radio"><input id="remove_dates" type="radio" value="remove_dates" name="action" checked><label for="remove_dates" class="ic-Label">Remove Dates</label></div><div class="ic-Radio"><input id="delete_events" type="radio" value="delete_events" name="action"><label for="delete_events" class="ic-Label">Delete Events</label></div></div></fieldset>';
el.append(el3);
//message flash
var msg = document.createElement('div');
msg.id = 'events_msg';
//msg.classList.add('ic-flash-warning');
msg.style.display = 'none';
el.appendChild(msg);
var parent = document.querySelector('body');
parent.appendChild(el);
}
$('#select-all').click(function(event) {
var state = this.checked; $(':checkbox').each(function() { this.checked = state; });
});
}
function setEvents() {
array = $.map($('input[name="events"]:checked'), function(c){return c.value; });
}
function openDialog() {
try {
createDialog();
$('#events_dialog').dialog({
'title' : 'Manage Course Events',
'autoOpen' : false,
'closeOnEscape': false,
'open': getCourses(), function () { $(".ui-dialog-titlebar-close").hide(); $(".ui-dialog").css("top", "10px");},
'buttons' : [ {
'text' : 'Cancel',
'click' : function() {
$(this).dialog('destroy').remove();
errors = [];
updateMsgs();
}
},{
'text' : 'Submit',
'class': 'Button Button--primary',
'click' : submitButton
} ],
'modal' : true,
'resizable' : false,
'height' : '600',
'width' : '40%',
'scrollable' : true
});
if (!$('#events_dialog').dialog('isOpen')) {
$('#events_dialog').dialog('open');
}
} catch (e) {
console.log(e);
}
}
function submitButton(){
var action = document.querySelector('input[name="action"]:checked').value;
if(action == "delete_events"){
deleteEvents();
}else{
removeDates();
}
}
function successDialog(){
var el = document.querySelector('#success_dialog');
if (!el) {
el = document.createElement('div');
el.id = 'success_dialog';
var div1 = document.createElement('div');
div1.classList.add('ic-flash-success');
el.appendChild(div1);
var div2 = document.createElement('div');
div2.classList.add('ic-flash__icon');
div2.classList.add('aria-hidden="true"');
div1.appendChild(div2);
var icon = document.createElement('i');
icon.classList.add('icon-check');
div2.appendChild(icon);
var msg = document.createTextNode("The action completed successfully!");
div1.appendChild(msg);
var button = document.createElement('button');
button.type = 'button';
button.classList.add("Button", "Button--icon-action", "close_link");
el.appendChild(button);
icon = document.createElement('i');
icon.classList.add('ic-icon-x');
icon.classList.add('aria-hidden="true"');
button.appendChild(icon);
var parent = document.querySelector('body');
parent.appendChild(el);
}
}
function open_success_dialog(){
try {
successDialog();
$('#success_dialog').dialog({
'autoOpen' : false,
'closeOnEscape': false,
'open': function () { $(".ui-dialog-titlebar").hide(); $(".ui-widget-content").css("background", "rgba(255, 255, 255, 0)"); $(".ui-dialog.ui-widget-content").css("box-shadow", "none");},
'modal' : true,
'resizable' : false,
'height' : 'auto',
'width' : '40%',
});
if (!$('#success_dialog').dialog('isOpen')) {
$('#success_dialog').dialog('open');
}
} catch (e) {
console.log(e);
}
}
function updateMsgs() {
var msg = document.getElementById('events_msg');
if (!msg) {
return;
}
if (msg.hasChildNodes()) {
msg.removeChild(msg.childNodes[0]);
}
if (typeof errors === 'undefined' || errors.length === 0) {
msg.style.display = 'none';
} else {
var div1 = document.createElement('div');
div1.classList.add('ic-flash-error');
var div2;
div2 = document.createElement('div');
div2.classList.add('ic-flash__icon');
div2.classList.add('aria-hidden="true"');
div1.appendChild(div2);
var icon;
icon = document.createElement('i');
icon.classList.add('icon-warning');
div2.appendChild(icon);
var ul = document.createElement('ul');
for (var i = 0; i < errors.length; i++) {
var li;
li = document.createElement('li');
li.textContent = errors[i];
ul.appendChild(li);
}
div1.appendChild(ul);
var button;
button = document.createElement('button');
button.type = 'button';
button.classList.add("Button", "Button--icon-action", "close_link");
div1.appendChild(button);
icon = document.createElement('i');
icon.classList.add('ic-icon-x');
icon.classList.add('aria-hidden="true"');
button.appendChild(icon);
msg.appendChild(div1);
msg.style.display = 'inline-block';
}
}
})();