-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstructActions.php
More file actions
executable file
·110 lines (98 loc) · 3.03 KB
/
InstructActions.php
File metadata and controls
executable file
·110 lines (98 loc) · 3.03 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
<?php
header('Content-Type: application/json');
require __DIR__ . '/Credentials.php';
require __DIR__ . '/FormatDateTimeStr.php';
require __DIR__ . '/GetTimeInterval.php';
require __DIR__ . '/GetTargetUrl.php';
// Connect to the database:
$Conn = new mysqli($Servername, $Username, $Password, $Dbname);
if ($Conn->connect_error) {
die("Database connection failed: " . $Conn->connect_error);
}
// Unpack the inputs ...
$Input = json_decode(file_get_contents('php://input'), true);
// SubjectId
$SubjectId = $Input['SubjectId'];
$SubjectId = mysqli_real_escape_string($Conn, $SubjectId);
// DateTime_Start
$DateTime_Start = FormatDateTimeStr($Input['DateTime_Start']);
$StartTime = new DateTimeImmutable(
$DateTime_Start,
new DateTimeZone('Europe/London')
);
// DateTime_Instruct (Now)
$Now = new DateTimeImmutable("now", new DateTimeZone('Europe/London'));
$DateTime_Instruct = $Now->format('Y-m-d\TH:i:s');
// Interval between Now and Start
$Interval = GetTimeInterval($StartTime, $Now);
// ClientTimeZone
$ClientTimeZone = $Input['ClientTimeZone'];
$ClientTimeZone = mysqli_real_escape_string($Conn, $ClientTimeZone);
// TaskId
$TaskId = $Input['TaskId'];
$TaskId = mysqli_real_escape_string($Conn, $TaskId);
// Test to see if enough time has passed
$EnoughTime = false;
switch ($TaskId) {
case 'TItrain':
if ($Interval > 81) {
$EnoughTime = true;
}
break;
case 'TIprobe':
if ($Interval > 44) {
$EnoughTime = true;
}
break;
default:
$Conn->close();
die('Bad TaskId!');
break;
}
// Get the State
$Sql00 = "SELECT * FROM Register WHERE SubjectId = '$SubjectId'";
$QueryRes00 = mysqli_query($Conn, $Sql00);
if ($QueryRes00 === false) {
$Conn->close();
die("Query Sql00 failed to execute successfully!");
} else {
while ($Row = mysqli_fetch_assoc($QueryRes00)) {
$State = $Row["State"];
}
}
$Result = array();
if ($EnoughTime) {
// They are good to continue...
$State++;
if ($TaskId == 'TItrain') {
$Sql01 = "UPDATE Register SET
State = $State,
DateTime_TIinstr = '$DateTime_Instruct'
WHERE SubjectId ='$SubjectId'";
} else {
$Sql01 = "UPDATE Register SET
State = $State
WHERE SubjectId ='$SubjectId'";
}
if ($Conn->query($Sql01) === true) {
$Url = GetTargetUrl($Conn, $SubjectId);
$Result['TargetUrl'] = $Url;
} else {
$Conn->close();
die('Query Sql01 failed to execute successfully;');
}
} else {
// If they jumped the gun...
$Sql02 = "INSERT INTO InstructNaughtiness
(SubjectId, State, TaskId, DateTime_Naughty)
VALUES ('$SubjectId', $State, '$TaskId', '$DateTime_Instruct')";
if ($Conn->query($Sql02) === true) {
$Result['TargetUrl'] = "./Instruct.html?" .
"SubjectId=$SubjectId&TaskId=$TaskId&Warn=true#";
} else {
$Conn->close();
die('Query Sql02 failed to execute successfully;');
}
}
$Conn->close();
echo json_encode($Result);