-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetAssignment.php
More file actions
executable file
·67 lines (61 loc) · 1.98 KB
/
GetAssignment.php
File metadata and controls
executable file
·67 lines (61 loc) · 1.98 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
<?php
header('Content-Type: application/json');
require __DIR__ . '/Credentials.php';
require __DIR__ . '/MakeAssignment.php';
// Connect to the database
$Conn = new mysqli($Servername, $Username, $Password, $Dbname);
if ($Conn->connect_error) {
die('Failed to connect to database;');
}
// Unpack input
$Input = json_decode(file_get_contents('php://input'), true);
if (!$Input) {
// If using MATLAB's webwrite function
$Input = $_POST;
}
$SubjectId = $Input['SubjectId'];
$SubjectId = mysqli_real_escape_string($Conn, $SubjectId);
if (!boolval($SubjectId)) {
die('SubjectId not set in call to GetAssignment.php;');
}
// Check whether an assignment has been made yet
$Sql00 = "SELECT * FROM Register WHERE SubjectId = '$SubjectId'";
$QueryRes00 = mysqli_query($Conn, $Sql00);
$SubjectFound = false;
$GroupId = null;
$ImgPerm = null;
if ($QueryRes00 === false) {
die("Sql00 failed to execute successfully!");
} else {
while ($Row = mysqli_fetch_assoc($QueryRes00)) {
$SubjectFound = true;
$GroupId = $Row["GroupId"];
$ImgPerm = json_decode($Row["ImgPerm"], true);
$Assignment = array();
$Assignment["GroupId"] = $GroupId;
$Assignment["ImgPerm"] = $ImgPerm;
}
}
// If either the GroupId or ImgPerm are unset
$MadeAss = false;
if ((!boolval($GroupId)) || (!boolval($ImgPerm))) {
$Assignment = MakeAssignment($SubjectId);
$GroupId = $Assignment["GroupId"];
$ImgPerm = $Assignment["ImgPerm"];
$MadeAss = true;
}
// If we have just made an assignment ...
// ... and the SubjectId is already recorded in the Register table ...
if ($SubjectFound && $MadeAss) {
$ImgPerm = json_encode($ImgPerm);
$Sql01 = "UPDATE Register SET
GroupId = '$GroupId',
ImgPerm = '$ImgPerm'
WHERE SubjectId = '$SubjectId'";
if ($Conn->query($Sql01) === false) {
die('Sql01 failed to execute successfully!');
}
}
// Close the database connection and return the result
$Conn->close();
echo(json_encode($Assignment));