-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_main.js
More file actions
289 lines (266 loc) · 7.89 KB
/
client_main.js
File metadata and controls
289 lines (266 loc) · 7.89 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
// <script>
// Gets the anon ids from the crossfilter
function getIDs() {
var ids = [];
for (i = 0; i < $('.anon-student').length; i++) {
ids.push($('.anon-student')[i].innerHTML);
}
return ids;
}
// Toggles the dropdown
function toggleDropdown() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Closes the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
};
// Updates the cross filter and compose email upon a selection of a dropdown option
// Store old subject for updates
var old_subject;
function optSelected (response) {
var r = JSON.parse(response);
old_subject = r.subject;
filter([r.comp, r.attr, r.cert]);
$('#email-subject').attr('value', r.subject);
$('#email-body').attr('value', r.body);
$('#reply-to').attr('value', r.reply);
$('#from-name').attr('value', r.from);
if (r.auto === "true") {
$('#automated').attr('checked', true);
}
else {
$('#automated').attr('checked', false);
}
if (r.analytics === "true") {
$('#saveChanges').css('display', 'inline-block');
}
}
// Clears the dropdown and appends on a disabled Load Past Communications option
function clear_drop() {
var myDropdown = document.getElementById('myDropdown');
myDropdown.innerHTML = '';
var load = document.createElement("option");
load.selected = true;
load.disabled = true;
load.text = "Load Past Communications";
myDropdown.appendChild(load);
return myDropdown;
}
// Creates the name for the dropdown which includes the date and subject
function make_name(timestamp, subject) {
var formatted_date = new Date(timestamp).toDateString().split(" ");
return formatted_date[1] + " " + formatted_date[2] + " " + formatted_date[3] + " - " + subject;
}
// ---------- API CALLS ---------------------
function get_analytics () {
var settings = {
"async": true,
"crossDomain": true,
"url": "https://cahl.berkeley.edu:1337/api/analytics",
"method": "GET",
success: function(response) {
// Clear dropdown and populate
var d = clear_drop();
for (var p in response) {
var policy = document.createElement("option");
var name = make_name(response[p].timestamp, response[p].subject);
if (response[p].auto === "true") {
name = "(Active) " + name;
}
policy.innerHTML = name;
policy.setAttribute("value", JSON.stringify(response[p]));
d.appendChild(policy);
}
}
};
$.ajax(settings);
}
function get_all () {
var settings = {
"async": true,
"crossDomain": true,
"url": "https://cahl.berkeley.edu:1337/api/all",
"method": "GET",
success: function(response) {
// Clear dropdown and populate
var d = clear_drop();
for (var p in response) {
var policy = document.createElement("option");
var name = make_name(response[p].timestamp, response[p].subject);
policy.innerHTML = name;
policy.setAttribute("value", JSON.stringify(response[p]));
d.appendChild(policy);
}
}
};
$.ajax(settings);
}
function send_emails () {
var ids = getIDs();
var ann = ($('#allRadio').attr("checked") === "checked");
var course = window.location.href.split("+")[1];
console.log(course);
var settings = {
"async": true,
"crossDomain": true,
"url": "https://cahl.berkeley.edu:1337/api/email",
"method": "POST",
"data":
{
"ids": ids,
"subject": $('#email-subject').attr('value'),
"body": $('#email-body').attr('value'),
"reply": $('#reply-to').attr('value'),
"from": $('#from-name').attr('value'),
"pass": "sadfvkn88asVLS891",
"ann": ann,
"course": course
},
success: function(response) {
alert("Successfully sent!");
}
};
$.ajax(settings);
}
function send_policy () {
var ids = getIDs();
var comp = window.filterLimits["completion-chart"];
var attr = window.filterLimits["attrition-chart"];
var cert = window.filterLimits["certification-chart"];
var automated = ($('#automated').attr("checked") === "checked");
var analytics = ($('#analyticsRadio').attr("checked") === "checked");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://cahl.berkeley.edu:1337/api/save",
"method": "POST",
"data":
{
"ids": ids,
"from": $('#from-name').attr('value'),
"reply": $('#reply-to').attr('value'),
"subject": $('#email-subject').attr('value'),
"body": $('#email-body').attr('value'),
"comp": comp,
"attr": attr,
"cert": cert,
"auto": automated,
"analytics": analytics,
"timestamp": new Date()
},
success: function(response) {
console.log("Policy Successfully sent!");
}
};
$.ajax(settings);
}
function save_changes () {
var ids = getIDs();
var comp = window.filterLimits["completion-chart"];
var attr = window.filterLimits["attrition-chart"];
var cert = window.filterLimits["certification-chart"];
var automated = ($('#automated').attr("checked") === "checked");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://cahl.berkeley.edu:1337/api/changes",
"method": "POST",
"data":
{
"old_subject": old_subject,
"ids": ids,
"from": $('#from-name').attr('value'),
"reply": $('#reply-to').attr('value'),
"subject": $('#email-subject').attr('value'),
"body": $('#email-body').attr('value'),
"comp": comp,
"attr": attr,
"cert": cert,
"auto": automated
},
success: function(response) {
console.log("Policy Successfully Saved!");
}
};
$.ajax(settings);
}
window.onload = function() {
drawGraphs("https://cahl.berkeley.edu:1337/api/predictions");
get_analytics();
$('#emailButton').on('click', function() {
if ($('#reply-to').attr('value') === "" || !($('#reply-to').attr('value').includes("@"))) {
alert("You have entered an invalid Instructor Email");
}
else {
if ($('#analyticsRadio').attr("checked") === "checked") {
if (confirm("Are you sure you want to send this email to " + window.selectedStudents.length + " students?")) {
send_emails();
send_policy();
get_analytics();
}
}
else {
if (confirm("Are you sure you want to send this email to " + $('#total')[0].innerHTML + " students?")) {
send_emails();
send_policy();
get_all();
}
}
}
});
$('#comp-no-cert').on('click', function() {
filter([[80, 100], null, [0, 20]]);
});
$('#attr-no-comp-cert').on('click', function() {
filter([[0,70], [80, 100], [0, 70]]);
});
$('#allRadio').on('click', function() {
get_all();
$('#analytics').css('display', 'none');
$('#automated').css('display', 'none');
$('#automated2').css('display', 'none');
$('#all-recipients').css('display', 'block');
$('#recipients').css('display', 'none');
$('#saveChanges').css('display', 'none');
});
$('#analyticsRadio').on('click', function() {
get_analytics();
$('#analytics').css('display', 'block');
$('#automated').css('display', 'inline');
$('#automated').attr('checked', false);
$('#automated2').css('display', 'inline');
$('#all-recipients').css('display', 'none');
$('#recipients').css('display', 'block');
});
$('#saveChanges').on('click', function() {
save_changes();
get_analytics();
});
$("#automated").hover(
function() {
$("#tip").show();
},
function() {
$("#tip").hide();
}
);
$('#test').on('click', function() {
send_policy();
get_all();
});
$('#test2').on('click', function() {
send_policy();
get_analytics();
});
};
// </script>