forked from UCBoulder/canvas-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_columns.user.js
99 lines (94 loc) · 3.75 KB
/
custom_columns.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
// ==UserScript==
// @name Custom Column Manager
// @namespace https://github.com/UCBoulder
// @description Add buttons to create and delete custom gradebook columns in Canvas LMS.
// @match https://*/courses/*/gradebook
// @grant none
// @run-at document-idle
// @version 1.1.3
// ==/UserScript==
/* globals $ */
// wait until the window jQuery is loaded
function defer(method) {
if (typeof $ !== 'undefined') {
method();
}
else {
setTimeout(function() { defer(method); }, 100);
}
}
function rmColumn() {
var colName = prompt("Enter the name of the column you wish to delete.\n\nWARNING: All data in this column will be permanently lost!", "");
if (colName) {
//extract course id from url
var courseId = window.location.href.split('/')[4];
//build the url to the custom columns api endpoint
var colUrl = "/api/v1/courses/" + courseId + "/custom_gradebook_columns";
//get a list of existing custom columns
$.ajax({url: colUrl, dataType: "text"}).done(function(data) {
var columns = JSON.parse(data.substr(data.indexOf('['),data.length));
for (var i = 0; i < columns.length; i++) {
//Match the column that should be deleted by name
if (columns[i].title === colName) {
//delete the column
$.ajax({
url: colUrl + "/" + columns[i].id,
type: "DELETE",
dataType: "text"
}).done(function(data) {
alert('"' + columns[i].title + '" deleted!');
location.reload();
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Error when deleting column: " + errorThrown);
});
return;
}
}
//for loop completed without finding the column
alert('Could not find a column named "' + colName + '".');
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Error when checking column names: " + errorThrown);
});
}
}
function addColumn() {
var colName = prompt("Enter the name of the new column you wish to create.", "");
if (colName) {
//extract course id from url
var courseId = window.location.href.split('/')[4];
//build the url to the custom columns api endpoint
var colUrl = "/api/v1/courses/" + courseId + "/custom_gradebook_columns";
//get a list of existing columns
$.get(colUrl, function(data) {
for (var col in data) {
//if a column with this name already exists, quit
if (col.title === colName) {
alert('A column named "' + colName + '" already exists.');
return;
}
}
});
//create the column
$.ajax({
url: colUrl,
type: "POST",
data: {"column[title]": colName},
dataType: "text"
}).done(function(data) {
alert('"' + colName + '" created!');
location.reload();
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Error when creating column: " + errorThrown);
});
}
}
defer(function() {
'use strict';
var colDiv = $(`<div>
<button class="btn" role="button" aria-label="Add Custom Column" id="addColumnBtn"><i class="icon-plus"/> Column</button>
<button class="btn" role="button" aria-label="Delete Custom Column" id="rmColumnBtn">Delete Column</button>
</div>`);
$("#gradebook-actions").append(colDiv);
$("#rmColumnBtn").click(rmColumn);
$("#addColumnBtn").click(addColumn);
});