-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheditclass.js
389 lines (333 loc) · 11.1 KB
/
editclass.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
$(function(){
var class_urn;
var class_name;
var class_members;
var teacherid;
var teachername;
var teacherorg
function n2(n){
return n > 9 ? "" + n: "0" + n;
}
function now(){
var today = new Date();
return today.getFullYear() + "-" + n2(today.getMonth()+1) + "-" + n2(today.getDate()) + "_" + n2(today.getHours()) + ":" + n2(today.getMinutes());
}
function td(x){
return($("<td>").text(x).attr("data-value", x || 0));
}
function first(obj) {
for (var a in obj) return a;
}
function readcsvfile(target, handler){
if(!target.files[0]){
alert("No Roster file selected");
return false;
}
var filereader = new FileReader();
filereader.onload = function(e){
//replace is because LAUSD csv files has incorrect line endings
handler(d3.csv.parse(e.target.result.replace(/[\n\r]+/g, '\n')));
}
filereader.readAsText(target.files[0]);
}
function createaccounts(classrecords){
var currentstudents = $.map(class_members, function(rec){return rec["personal_id"]});
var newstudents = [];
var requests1 = [];
$(".progress .bar").css("width", "0%")
$(".progress").addClass("progress-success").show()
var n = 0;
$.each(classrecords, function(i, rec){
//add new students
var index = currentstudents.indexOf(rec.id);
if(index < 0){
requests1.push(oh.user.setup(rec.firstname, rec.lastname, teacherorg, rec.id, class_urn, function(data){
newstudents.push(rec.id);
n++
rec.username = data.username;
rec.password = data.password;
}).always(function(){
$(".progress .bar").css("width", (n/classrecords.length) * 100 + "%")
}));
} else {
currentstudents.splice(index, 1);
}
});
$.when.apply($, requests1).always(function() {
//$.when.apply($, requests2).always(function() {
//at this point, 'currentstudents' contains class members that were not in the latest roster
//repopulate the html table
updatemembers(function(){
loadtable(currentstudents, newstudents);
});
//report added students
$("#usercount").text(n);
$("#donealert").show();
//reset the upload field
$("#inputRoster").val("");
//save the doc
//savedoc(classrecords);
});
//});
}
function savedoc(classrecords){
var classdb = $.map( classrecords, function(val, i) {
return {
"Student ID" : val["Student ID"],
"Student Name" : val["Student Name"],
"username" : val["username"],
"password" : val["password"]
}
});
classdb.sort(function (a, b) {
if (a["Student ID"] > b["Student ID"])
return 1;
if (a["Student ID"] < b["Student ID"])
return -1;
return 0;
});
//save the doc
var doctitle = class_urn + "_" + now() + "_students.csv"
oh.document.create(doctitle, d3.csv.format(classdb), class_urn);
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function addrow(userdata, isdropped, isadded){
//update Aug 20: only show students
if(!userdata.password && userdata["username"] != teacherid){
console.log("Skipping non-student user: " + userdata["username"])
return;
}
//for printing slip:
$("#studentable tbody").append('<tr class="printtitlerow"><th>Student ID</th><th>First</th><th>Last</th><th>Username</th><th class="noprint">role</th><th>Password</th><th class="noprint"></th><th class="noprint"></th></tr>');
var mytr = $("<tr />").appendTo("#studentable tbody");
td(userdata["personal_id"]).appendTo(mytr);
td(userdata["first_name"]).appendTo(mytr);
td(userdata["last_name"]).appendTo(mytr);
td(userdata["username"]).appendTo(mytr);
td(userdata["role"]).addClass("noprint").appendTo(mytr);
//password field
var pwfield = td("");
pwfield.appendTo(mytr);
//these are student accounts
if(userdata.password){
//set if this user is a student
if(isdropped){
mytr.addClass("error")
}
if(isadded){
mytr.addClass("success")
}
//check for username collisions
if(userdata.permissions.new_account){
//only display the initial password if new_account is true
pwfield.text(userdata.password).attr("data-value", userdata.password);
} else {
pwfield.text("<changed>");
}
//add the deletebutton
var delbtn = $('<button class="btn btn-danger btn-small"> Remove </button>').on("click", function(){
delbtn.attr("disabled", "disabled")
oh.class.removeuser(class_urn, userdata["username"], function(){
mytr.fadeOut();
})
})
$("<td>").addClass("noprint").append(delbtn).appendTo(mytr);
//add the reset password button
var resetbtn = $('<button class="btn btn-warning btn-small"> Change Passwd </button>').on("click", function(){
$("#usernamepass").text(userdata["username"]);
$(".modal a.btn").off("click");
$(".modal a.btn").on("click", function(e){
e.preventDefault();
if($("#newpassword").val().length < 8){
alert("Student password must be at least 8 characters.");
return;
}
$(".modal a.btn").attr("disabled", "disabled")
oh.user.password(teacherid, $("#teacherpassword").val(), userdata["username"], $("#newpassword").val(), function(){
pwfield.text("<changed>");
$(".modal").modal('hide');
}).always(function(){
$(".modal a.btn").removeAttr("disabled")
});
});
$("#newpassword").val("");
$(".modal").modal();
})
$("<td>").addClass("noprint").append(resetbtn).appendTo(mytr);
} else {
//these are accounts with no student id. Note sure what to do with them.
pwfield.text("");
td("").addClass("noprint").appendTo(mytr);
td("").addClass("noprint").appendTo(mytr);
}
//for later reference
userdata.tablerow = mytr;
}
//this function updates the global variable class_members which contains the current class members and their password
function updatemembers(cb, nosetup){
if(!nosetup){
$(".progress .bar").css("width", "0%")
$(".progress").removeClass("progress-success").show();
}
oh.class.read(class_urn, function(classlist){
oh.user.read(Object.keys(classlist[class_urn].users).toString(), function(userlist){
var requests = [];
var n = 0;
$.each(userlist, function(id, rec){
//store role and username in record
rec.role = classlist[class_urn].users[id];
rec.username = id;
//shortcut
if(nosetup){
return;
}
//call user setup for each user to get the initial password
if(rec["first_name"] && rec["last_name"] && rec["personal_id"] && rec["organization"]){
requests.push(oh.user.setup(rec["first_name"], rec["last_name"], rec["organization"], rec["personal_id"], "", function(data){
if(data.username != rec.username){
alert("Username collision detected: " + data.username + ", " + rec.username);
} else {
rec.password = data.password;
}
}).always(function(){
$(".progress .bar").css("width", (n++/Object.keys(userlist).length) * 100 + "%")
}));
}
});
$.when.apply($, requests).always(function() {
$(".progress").hide();
class_members = userlist;
cb && cb();
});
}).fail(function(){
$(".progress").hide();
});
}).fail(function(){
$(".progress").hide();
});
}
function loadtable(droppedstudents, addedstudents){
$("#studentable tbody").empty();
var count = Object.keys(class_members).filter(function(studentusername){
return studentusername.substring(0, 6) == "lausd-";
}).length;
$("#urntitle").text(class_name + " (" + count + " students)");
$.each(class_members, function(username, userdata){
var isdropped = droppedstudents && (droppedstudents.indexOf(userdata["personal_id"]) > -1);
var isadded = addedstudents && (addedstudents.indexOf(userdata["personal_id"]) > -1);
addrow(userdata, isdropped, isadded);
});
if($("#studentable tbody tr.error").length){
$("#deletealart").show();
}
}
//init page
class_urn = getParameterByName("class");
if(!class_urn){
window.location = "index.html"
} else {
$("#urntitle").text(class_urn);
}
oh.ping(function(){
oh.user.whoami(function(x){
teacherid = x;
oh.user.read(x, function(data){
var thisname = data[x].last_name;
var thisorg = data[x].organization;
if(!thisname){
alert("ERROR: this account has no last name set. Please contact mobilize support.")
}
if(!thisorg){
alert("ERROR: this account has no organization set. Please contact mobilize support.")
}
teachername = utf2ascii(thisname || "empty" );
teacherorg = utf2ascii(thisorg || "empty" );
});
});
oh.user.info(function(res){
var userdata = res.data[first(res.data)];
if(Object.keys(userdata.classes).indexOf(class_urn) < 0){
alert("Class: " + class_urn + " does not belong to current user.");
window.location = "index.html";
} else {
class_name = userdata.classes[class_urn]
updatemembers(loadtable);
}
});
oh.keepalive();
});
$("#inputRoster").on("change", function loadfile(e){
if(!$("#inputRoster").val()){
return;
}
updatemembers(function(){
e.preventDefault();
readcsvfile($("#inputRoster")[0], function(records){
classrecords = [];
$.each(records, function(i, rec){
try {
if(rec["StudentCode"]){
rec.id = rec["StudentCode"]
rec.firstname = rec["StudentName1"].split(",")[1].trim();
rec.lastname = rec["StudentName1"].split(",")[0].trim();
} else if(rec["Student ID"]){
rec.id = rec["Student ID"];
rec.firstname = rec["Student Name"].split(",")[1].trim();
rec.lastname = rec["Student Name"].split(",")[0].trim();
} else if(rec["students_id"]){
rec.id = rec["students_id"];
rec.firstname = rec["students_firstname"].trim();
rec.lastname = rec["students_lastname"].trim();
} else {
//new format has white lines, so these are expected
alert("Failed to read line " + i + "\n" + JSON.stringify(rec));
return;
}
//add student
rec.tr = $("<tr>").append(td(rec.id)).append(td(rec.firstname)).append(td(rec.lastname));
classrecords.push(rec);
} catch(err) {
alert("CSV parsing error. Failed to read line " + i + "\n\n" + JSON.stringify(rec));
}
});
if(classrecords.length > 0){
createaccounts(classrecords);
} else {
alert("No valid records found in this CSV file. Either formatting is incorrect or CSV file is empty.");
}
});
}, true)
});
$('.alert .close').on('click', function () {
$(this).parent().hide();
})
$("#printbutton").on("click", function(){
$("#wrap").toggleClass("printstyle");
if($("#wrap").hasClass("printstyle")){
window.print();
}
});
$("#signoutbutton").on("click", function(e){
e.preventDefault();
oh.logout(function(){
location.reload(true);
});
});
$("#genbutton").on("click", function(e){
e.preventDefault();
$("#genbutton").attr("disabled", "disabled")
$.get("/password/simple/", function(data){
$("#newpassword").val(data);
}).always(function(){
$("#genbutton").removeAttr("disabled")
}).fail(function(){
alert("Failed to generate random password.")
});
return false;
});
});