-
Notifications
You must be signed in to change notification settings - Fork 17
/
Canvas Crosslisting.user.js
199 lines (188 loc) · 5.55 KB
/
Canvas Crosslisting.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
// ==UserScript==
// @name Canvas Crosslisting
// @namespace http://tampermonkey.net/
// @version 1.0
// @description try to save our time
// @author Chad Scott
// @include https://*.instructure.com/accounts/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var assocRegex = new RegExp('^/accounts/([0-9]+)$');
var errors = [];
var childS = [];
var parentC = [];
if (assocRegex.test(window.location.pathname)) {
add_button();
}
function getCsrfToken() {
var csrfRegex = new RegExp('^_csrf_token=(.*)$');
var csrf;
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
var match = csrfRegex.exec(cookie);
if (match) {
csrf = decodeURIComponent(match[1]);
break;
}
}
return csrf;
}
function add_button() {
var parent = document.querySelector('aside#right-side');
if (parent) {
var el = parent.querySelector('#jj_cross');
if (!el) {
el = document.createElement('a');
el.classList.add('btn', 'button-sidebar-wide');
el.id = 'jj_cross';
var icon = document.createElement('i');
icon.classList.add('icon-import');
el.appendChild(icon);
var txt = document.createTextNode(' Crosslist Courses');
el.appendChild(txt);
el.addEventListener('click', openDialog);
parent.appendChild(el);
}
}
}
function createDialog() {
var el = document.querySelector('#jj_cross_dialog');
if (!el) {
//field1
el = document.createElement('div');
el.id = 'jj_cross_dialog';
el.classList.add('ic-Form-control');
var label = document.createElement('label');
label.htmlFor = 'jj_cross_title';
label.textContent = 'Parent Course Number:';
label.classList.add('ic-Label');
el.appendChild(label);
var input = document.createElement('input');
input.id = 'jj_cross_title';
input.classList.add('ic-Input');
input.type = 'text';
input.placeholder = 'Enter Parent (bucket) Course Number:';
el.appendChild(input);
//field2
label = document.createElement('label');
label.htmlFor = 'jj_cross_child';
label.textContent = 'Child Course Number: ';
label.classList.add('ic-Label');
el.appendChild(label);
input = document.createElement('input');
input.id = 'jj_cross_child';
input.classList.add('ic-Input');
input.type = 'text';
input.placeholder = 'Enter Child Course Number:';
el.appendChild(input);
//message flash
var msg = document.createElement('div');
msg.id = 'jj_cross_msg';
msg.classList.add('ic-flash-warning');
msg.style.display = 'none';
el.appendChild(msg);
var parent = document.querySelector('body');
parent.appendChild(el);
}
}
function openDialog() {
try {
createDialog();
$('#jj_cross_dialog').dialog({
'title' : 'Crosslist Courses',
'autoOpen' : false,
'buttons' : [ {
'text' : 'Crosslist',
'click' : processDialog
}, {
'text' : 'Cancel',
'click' : function() {
$(this).dialog('close');
errors = [];
updateMsgs();
}
} ],
'modal' : true,
'height' : 'auto',
'width' : '80%'
});
if (!$('#jj_cross_dialog').dialog('isOpen')) {
$('#jj_cross_dialog').dialog('open');
}
} catch (e) {
console.log(e);
}
}
function processDialog() {
// Reset global variable errors
errors = [];
var parentCourse, childCourse;
var el = document.getElementById('jj_cross_title');
if (el.value && el.value.trim() !== '') {
parentCourse = el.value;
parentC = parentCourse;
} else {
errors.push('You must provide a parent course.');
}
el = document.getElementById('jj_cross_child');
if (el.value && el.value.trim() !== '') {
childCourse = el.value;
var childSection;
var url = "/api/v1/courses/" + childCourse + "/sections?";
$.ajax({
'async': true,
'type': "GET",
'global': true,
'dataType': 'JSON',
'data': JSON.stringify(childSection),
'contentType': "application/json",
'url': url,
'success': function (data) {
childSection = data[0].id;
childS = childSection;
var url2 = "/api/v1/sections/" + childS + "/crosslist/" + parentC +"?";
$.ajax({
'cache' : false,
'url' : url2 ,
'type' : 'POST',
}).done(function() {
updateMsgs();
$('#jj_cross_dialog').dialog('close');
window.location.reload(true);
}).fail(function() {
errors.push('All the information was supplied correctly, but there was an error crosslisting courses in Canvas.');
updateMsgs();
});
}
});
} else {
errors.push('You must provide a child course.');
}
updateMsgs();
}
function updateMsgs() {
var msg = document.getElementById('jj_cross_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 ul = document.createElement('ul');
var li;
for (var i = 0; i < errors.length; i++) {
li = document.createElement('li');
li.textContent = errors[i];
ul.appendChild(li);
}
msg.appendChild(ul);
msg.style.display = 'inline-block';
}
}
})();