-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
170 lines (140 loc) · 4.4 KB
/
ajax.php
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
<?php require_once('query.php');
$sql = new chooser_query();
header('Content-Type: application/json; charset=utf-8');
//Disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
$req = $_POST['req'] ?? $_GET['req'];
switch ($req) {
//=========
// CLASSES
//=========
case 'updateclassinfo':
$response = $sql->edit_class($_GET['class'], $_GET['k'], $_GET['v']);
if ($response) echo $response;
else http_response_code(403);
break;
case 'schemae':
//Figure out the pattern a schema has to fit
$events = $sql->get_events_by_class($_GET['class']);
$values = [];
foreach ($events as $event) $values["$event->result"] = true;
$schemae = $sql->get_available_schemae();
$result = [];
foreach ($schemae as $schema) $result[] = [
'name' => $schema->name,
'compatible' => $schema->contains_values(array_keys($values))
];
echo json_encode($result);
break;
//Basically the same as updateclassinfo, but returns schema info too
case 'editschema':
$updated = $sql->edit_class($_GET['class'], 'schema', $_GET['schema']);
if ($updated) {
$schema = $sql->get_schema($_GET['schema']);
echo json_encode([
'weights' => $schema->items,
'css' => $schema->output_css(false, false)
]);
} else echo 0;
break;
case 'getschemabuttons':
$schema = $sql->get_schema($_GET['schema']);
echo $schema->output_buttons(true);
break;
//==========
// STUDENTS
//==========
case 'editstudent':
$response = $sql->edit_student($_GET['student'], $_GET['fname'], $_GET['lname'], $_GET['note']);
if ($response) echo $response;
else http_response_code(403);
break;
case 'addstudent':
$id = $sql->add_student($_GET['classid'], $_GET['fname'], $_GET['lname'], $_GET['note']);
if ($id) echo $id;
else http_response_code(403);
break;
case 'deletestudent':
echo $sql->delete_student($_GET['id']);
break;
case 'studentexcused':
echo $sql->student_excused($_GET['id'], $_GET['excused'] ?: null);
break;
case 'uploadroster':
$i=0;
$added = [];
$rows = preg_split('/\r\n|\r|\n/', $_POST['csv']);
foreach ($rows as $row) {
if (!$row) continue;
$row = str_getcsv($row);
foreach ($row as &$cell) $cell = trim($cell);
if (!$i) {
$fnkey = array_search('fname', $row);
$lnkey = array_search('lname', $row);
$notekey = array_search('note', $row);
//Invalid CSV
if ($fnkey===false || $lnkey===false) {
echo 'false';
exit;
}
} else {
$note = $notekey!==false ? $row[$notekey] : null;
$id = $sql->add_student($_POST['class'], $row[$fnkey], $row[$lnkey], $note);
if ($id) $added[] = ['id'=>$id, 'fname'=>$row[$fnkey], 'lname'=>$row[$lnkey], 'note'=>$note];
}
$i++;
}
echo json_encode($added);
break;
case 'searchstudent':
echo json_encode($sql->student_search($_GET['phrase']));
break;
//========
// EVENTS
//========
case 'events':
echo json_encode($sql->get_events($_GET['student']));
break;
case 'writeevent':
echo $sql->new_event($_GET['rosterid'], $_GET['result']);
break;
case 'updateevent':
echo $sql->edit_event($_GET['event'], $_GET['result']);
break;
case 'deleteevent':
echo $sql->delete_event($_GET['event']);
break;
//========
// USERS
//========
case 'userexists':
$fields = [];
if (isset($_GET['username'])) $fields['username'] = ($sql->get_user_by('username', $_GET['username']) ? 1 : 0);
if (isset($_GET['email'])) $fields['email'] = ($sql->get_user_by('email', $_GET['email']) ? 1 : 0);
echo json_encode($fields);
break;
case 'edituser':
if (!in_array($_GET['k'], ['email'])) $response = False;
else $response = $sql->edit_user($_GET['k'], $_GET['v']);
if ($response) echo json_encode($response);
if (!$response || !is_numeric($response)) http_response_code(403);
break;
case 'editpw':
echo json_encode($sql->edit_pw($_GET['current'], $_GET['new']));
break;
case 'deleteorcid':
if (!$sql->current_user()->password) return false; //Don't allow disconnection unless a password is set
$result = $sql->edit_user('orcid', null);
$sql->user_add_option('orcid_data', null);
echo json_encode($result);
break;
case 'resetpwlink':
$result = $sql->generate_reset_link($_GET['username']);
if (!is_numeric($result)) http_response_code(500);
echo $result;
break;
}
// sleep(1); //Simulate slow network