-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstudentmatrix-matrixtemplate.js
More file actions
338 lines (301 loc) · 13.9 KB
/
studentmatrix-matrixtemplate.js
File metadata and controls
338 lines (301 loc) · 13.9 KB
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
/**
* @file
* Allows copying a matrix template file to students, and setting access to the
* copies.
*/
StudentMatrix.plugins.matrixtemplate = {
name : 'Matrix template plugin',
description : 'Allows creating and setting access to student sheets.',
version : '1.3',
updateUrl : 'https://raw.github.com/Itangalo/studentmatrix/3.x/studentmatrix-matrixtemplate.js',
cell : 'D4',
dependencies : {
core : '3.1',
plugins : {
mainsheet : '1.0',
},
},
// Column used for ID and link to student sheets.
columns : {
studentSheetID : 'Student sheet ID',
studentSheetUrl : 'Student sheet url',
studentFolderViewable : 'ID for folder viewable by student',
},
settings : {
matrixtemplate : {
group : 'Student sheet setup',
options : {
templateID : '',
teacherEmails : '',
},
optionsBuilder : function(handler, container, defaults) {
var app = UiApp.getActiveApplication();
try {
var template = SpreadsheetApp.openById(defaults.templateID);
container.add(app.createHTML('File used for matrix template: '));
container.add(app.createAnchor(template.getName(), template.getUrl()));
}
catch(e) {
container.add(app.createHTML('No template selected. Please use button below to select one.'));
}
var textBox = app.createTextBox().setName('templateID').setId('templateID').setText(defaults.templateID);
container.add(textBox);
handler.addCallbackElement(textBox);
var fileHandler = StudentMatrix.addPluginHandler('matrixtemplate', 'showFilePicker');
container.add(app.createButton('Select template file', fileHandler));
container.add(app.createHTML('Emails for teachers, which should have edit access to all student sheets. (One per line.)'));
var teacherEmails = app.createTextArea().setName('teacherEmails').setWidth('100%').setText(defaults.teacherEmails);
container.add(teacherEmails);
handler.addCallbackElement(teacherEmails);
},
},
sheetColors : {
group : 'Updating student sheets',
options : {
assessmentColors : ['#ffffff', '#ff0000', '#ffff00', '#00ff00'],
assessmentNames : ['Not assessed', 'Not yet ok', 'On its way', 'Ok'],
},
optionsBuilder : function(handler, container, defaults) {
var app = UiApp.getActiveApplication();
var assessmentColors = defaults.assessmentColors;
var assessmentNames = defaults.assessmentNames;
var colorGrid = app.createGrid(assessmentColors.length, 1).setBorderWidth(1).setCellPadding(2).setId('colorGrid');
for (var i in assessmentColors) {
colorGrid.setText(parseInt(i), 0, assessmentNames[i]);
colorGrid.setStyleAttributes(parseInt(i), 0, {background : assessmentColors[i]});
}
container.add(colorGrid);
container.add(app.createLabel('Use the button below to read the content and background colors of the current selection in the spreadsheet, using them for setting new colors.'));
var setAssessmentColors = StudentMatrix.addPluginHandler('matrixtemplate', 'setAssessmentColors');
var colorButton = app.createButton('Read new names and colors from current selection', setAssessmentColors);
container.add(colorButton);
},
},
},
studentActions : {
copySheet : {
name : 'Create student sheets from matrix template',
group : 'Student sheet setup',
description : 'Makes a copy of the matrix template for each student. Can also be used for changing access settings to existing student sheets.',
processor : function(row, options) {
// Fetch the relevant data for this row.
var matrixFileCell = StudentMatrix.components.fetchers.studentColumnCell(row, 'studentSheetID');
var matrixUrlCell = StudentMatrix.components.fetchers.studentColumnCell(row, 'studentSheetUrl');
var studentEmail = StudentMatrix.components.fetchers.studentColumnValue(row, 'studentMail');
var fileName = StudentMatrix.replaceColumnTokens(options.fileName, row);
var folderID = StudentMatrix.components.fetchers.studentColumnValue(row, 'studentFolderID');
// Create the student sheet if it doesn't already exist. In any case, load it into the varible 'copy'.
if (matrixFileCell.getValue() == '') {
var copy = SpreadsheetApp.openById(StudentMatrix.getProperty('templateID')).copy(fileName);
var newlyCreated = true;
matrixFileCell.setFormula('=hyperlink("' + copy.getUrl() + '";"' + copy.getId() + '")');
}
else {
var copy = SpreadsheetApp.openById(matrixFileCell.getValue());
var newlyCreated = false;
}
if (matrixUrlCell.getValue() == '') {
matrixUrlCell.setValue(copy.getUrl());
}
// Go through the options to set permissions (and some other things), one at a time.
if (options.rewriteNames == 'true' && newlyCreated == false) {
copy.rename(fileName);
}
if (options.resetPermissions == 'true') {
copy.setAnonymousAccess(false, false);
var editors = copy.getEditors();
for (var editor in editors) {
if (editors[editor] != '') {
copy.removeEditor(editors[editor]);
}
}
var viewers = copy.getViewers();
for (var viewer in viewers) {
if (viewers[viewer] != '') {
copy.removeViewer(viewers[viewer]);
}
}
}
if (options.addTeachers == 'true') {
if (StudentMatrix.getProperty('teacherEmails') != '') {
copy.addEditors(StudentMatrix.getProperty('teacherEmails').split("\n"));
}
}
if (options.addStudentView == 'true') {
copy.addViewer(studentEmail);
}
if (options.addStudentEdit == 'true') {
copy.addEditor(studentEmail);
}
if (options.addAllView == 'true') {
copy.setAnonymousAccess(true, false);
}
// Note that this final part of the checks changes the copy.
if (options.moveToFolder == 'true' && folderID != false) {
var folder = DocsList.getFolderById(folderID);
copy = DocsList.getFileById(copy.getId());
copy.addToFolder(folder);
copy.removeFromFolder(DocsList.getRootFolder());
}
},
options : {
fileName : 'Student sheet for [column-2]',
rewriteNames : false,
resetPermissions : false,
addTeachers : true,
addStudentView : true,
addStudentEdit : false,
addAllView : false,
moveToFolder : true,
},
// This is just a helper, to reduce code repeat.
descriptions : {
rewriteNames : 'Also rewrite the names of existing student sheets (if any).',
resetPermissions : 'Remove all permissions on existing student sheets, then set new permissions.',
addTeachers : 'Add edit permissions to all accounts specified in the teacher emails box in the settings.',
addStudentView : 'Add student view permission to the student sheet.',
addStudentEdit : 'Add student edit permission to the student sheet.',
addAllView : 'Make the student sheet public, so that anyone can view it.',
moveToFolder : 'Move the student sheet to any folder specified in the "student folder ID" column.',
},
optionsBuilder : function(handler, container) {
var app = UiApp.getActiveApplication();
container.add(app.createLabel('Name for new student sheets:'));
var fileName = app.createTextBox().setName('fileName').setWidth('100%').setText('Student sheet for [column-2]');
container.add(fileName);
handler.addCallbackElement(fileName);
container.add(app.createLabel('(Tokens like "[column-2]" will be replaced with values in student rows.)'));
container.add(app.createHTML('<br />'))
var checkboxes = {};
for (var checkbox in this.descriptions) {
checkboxes[checkbox] = app.createCheckBox(this.descriptions[checkbox]).setName(checkbox).setValue(this.options[checkbox]);
container.add(checkboxes[checkbox]).add(app.createHTML('<br />'));
handler.addCallbackElement(checkboxes[checkbox]);
}
},
validator : function() {
if (StudentMatrix.getProperty('StudentMatrixColumns', 'studentSheetID') == undefined) {
return 'You must set up the columns used for student sheets before running this action. Visit the global actions to do this.';
}
if (StudentMatrix.getProperty('templateID') == undefined) {
return 'You must set a matrix template in the settings before running this action.';
}
try {
var template = SpreadsheetApp.openById(StudentMatrix.getProperty('templateID'));
}
catch(e) {
return 'The matrix template could not be loaded. Please verify that it is set correctly in the global settings.';
}
},
},
},
fetchers : {
studentSpreadsheet : function(row) {
var fileID = StudentMatrix.components.fetchers.studentColumnValue(row, 'studentSheetID');
if (fileID == false) {
return false;
}
return SpreadsheetApp.openById(fileID);
},
studentRange : function(row, sheetName, a1Notation) {
var sheet = StudentMatrix.components.fetchers.studentSpreadsheet(row);
if (sheet == false) {
return false;
}
return sheet.getSheetByName(sheetName).getRange(a1Notation);
},
},
globalActions : {
addPushSheet : {
name : 'Add a sheet to push updates to student sheets',
group : 'Update student sheets',
description : 'Adds a "push sheet", used to make changes that will be pushed to selected student sheets. If your student sheets have more than one tab, the push sheet will update the same tab as it was created from.',
validator : function() {
try {
SpreadsheetApp.openById(StudentMatrix.getProperty('templateID'));
}
catch(e) {
return 'Could not open the matrix template. Please make sure one is set in the settings.';
}
},
options : {
tabID : false,
newName : 'new push sheet',
},
optionsBuilder : function(handler, container) {
var app = UiApp.getActiveApplication();
var template = SpreadsheetApp.openById(StudentMatrix.getProperty('templateID'));
container.add(app.createLabel('Which tab to you want to use to create a push sheet?'));
var tabID = app.createListBox().setName('tabID');
handler.addCallbackElement(tabID);
for (var tab in template.getSheets()) {
tabID.addItem(template.getSheets()[tab].getSheetName(), template.getSheets()[tab].getSheetId());
}
container.add(tabID);
container.add(app.createLabel('What should the tab be called here in the master sheet?'));
var newName = app.createTextBox().setName('newName').setWidth('100%').setText('new push sheet');
handler.addCallbackElement(newName);
container.add(newName);
return app;
},
processor : function(options) {
var template = SpreadsheetApp.openById(StudentMatrix.getProperty('templateID'));
var sheet = StudentMatrix.plugins.mainsheet.getSheetByID(template, options.tabID);
sheet.copyTo(SpreadsheetApp.getActiveSpreadsheet()).activate().setName(options.newName);
var copy = SpreadsheetApp.getActiveSpreadsheet();
copy.moveActiveSheet(2);
StudentMatrix.setProperty(options.tabID, 'StudentMatrixPushMapping', copy.getSheetId().toString());
},
},
},
handlers : {
showFilePicker : function(eventInfo) {
var app = UiApp.getActiveApplication();
var handler = StudentMatrix.addPluginHandler('matrixtemplate', 'closeFilePicker');
app.createDocsListDialog().setDialogTitle('Select spreadsheet to use as matrix template').addSelectionHandler(handler).setOAuthToken(ScriptApp.getOAuthToken()).showDocsPicker();
return app;
},
closeFilePicker : function(eventInfo) {
var app = UiApp.getActiveApplication();
app.getElementById('templateID').setText(eventInfo.parameter.items[0].id);
return app;
},
setAssessmentColors : function(eventInfo) {
var assessmentColors = [];
var assessmentNames = [];
var colors = SpreadsheetApp.getActiveRange().getBackgrounds();
var names = SpreadsheetApp.getActiveRange().getValues();
for (var row in colors) {
for (var column in colors[row]) {
assessmentColors.push(colors[row][column]);
assessmentNames.push(names[row][column]);
}
}
StudentMatrix.setProperty(assessmentColors, 'assessmentColors');
StudentMatrix.setProperty(assessmentNames, 'assessmentNames');
var app = UiApp.getActiveApplication();
var colorGrid = app.getElementById('colorGrid').resize(assessmentColors.length, 1);
for (var i in assessmentColors) {
colorGrid.setText(parseInt(i), 0, assessmentNames[i]);
colorGrid.setStyleAttributes(parseInt(i), 0, {background : assessmentColors[i]});
}
StudentMatrix.toast('Stored new assessment colors.');
return app;
},
},
// Helper function to get the name of the tab in the student sheets corresponding to the active push sheet.
getTargetSheetName : function() {
var tabID = SpreadsheetApp.getActiveSheet().getSheetId();
var targetID = StudentMatrix.getProperty('StudentMatrixPushMapping', tabID.toString());
if (targetID == undefined) {
return false;
}
var template = SpreadsheetApp.openById(StudentMatrix.getProperty('templateID'));
// Sometimes this function is called before all plugins are declared, thus
// this check. @TODO: Make plugins load before code is run.
if (StudentMatrix.plugins.mainsheet == undefined) {
return;
}
return StudentMatrix.plugins.mainsheet.getSheetByID(template, targetID).getSheetName();
},
};