-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadHistory.php
79 lines (72 loc) · 3.01 KB
/
loadHistory.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
<?php
$request = json_decode(file_get_contents('php://input'),true);
require_once "shared/connect.php";
require_once "shared/common.inc.php";
if (!$config->testMode->active && (!isset($request['sToken']) || !isset($request['sPlatform']))) {
echo json_encode(array('bSuccess' => false, 'sError' => 'missing sToken or sPlatform POST variable.'));
exit;
}
$params = getPlatformTokenParams($request['sToken'], $request['sPlatform'], $request['taskId'], $db);
function listItems($params, $db) {
$stmt = $db->prepare("
SELECT history_tm_source_codes.*, tm_submissions.iScore
FROM history_tm_source_codes
LEFT JOIN tm_submissions ON tm_submissions.ID = history_tm_source_codes.sName
WHERE history_tm_source_codes.idUser = :idUser
AND history_tm_source_codes.idTask = :idTask
AND history_tm_source_codes.idPlatform = :idPlatform
AND bDeleted = 0
AND (iNextVersion != history_tm_source_codes.iVersion OR iNextVersion IS NULL)
AND sSource != ''
ORDER BY sDate DESC;");
$stmt->execute(['idUser' => $params['idUser'], 'idTask' => $params['idTaskLocal'], 'idPlatform' => $params['idPlatform']]);
$historyItems = array();
$sourcesSeen = [];
while($row = $stmt->fetch()) {
$item = [
'ID' => $row['historyID'],
'date' => $row['sDate'],
'name' => $row['sName'],
'length' => strlen($row['sSource']),
'submission' => $row['bSubmission'] == 1,
'score' => $row['iScore'],
'lang' => 'inconnu'
];
try {
$itemParams = json_decode($row['sParams'], true);
$item['lang'] = $itemParams['sLangProg'];
} catch(Exception $e) {}
if(isset($sourcesSeen[$row['bSubmission'] . '-' . $item['lang'] . ':' . $row['sSource']])) { continue; }
$sourcesSeen[$row['bSubmission'] . '-' . $item['lang'] . ':' . $row['sSource']] = true;
$historyItems[] = $item;
}
echo json_encode(['bSuccess' => true, 'historyItems' => $historyItems]);
}
function loadItem($params, $db, $idItem) {
$stmt = $db->prepare("
SELECT *
FROM history_tm_source_codes
WHERE idUser = :idUser
AND idTask = :idTask
AND idPlatform = :idPlatform
AND historyID = :idItem;");
$stmt->execute(['idUser' => $params['idUser'], 'idTask' => $params['idTaskLocal'], 'idPlatform' => $params['idPlatform'], 'idItem' => $idItem]);
$row = $stmt->fetch();
if($row) {
$lang = 'c';
try {
$itemParams = json_decode($row['sParams'], true);
$lang = $itemParams['sLangProg'];
} catch(Exception $e) {}
echo json_encode(['bSuccess' => true, 'name' => $row['sName'], 'lang' => $lang, 'source' => $row['sSource']]);
} else {
echo json_encode(['bSuccess' => false, 'sError' => 'item not found.']);
}
}
if($request['action'] == 'list') {
listItems($params, $db);
} elseif($request['action'] == 'load') {
loadItem($params, $db, $request['idItem']);
} else {
echo json_encode(array('bSuccess' => false, 'sError' => 'missing action.'));
}