|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\TrackEExercises; |
|
6
|
|
|
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log; |
|
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
8
|
|
|
|
|
9
|
|
|
require_once __DIR__.'/../../../main/inc/global.inc.php'; |
|
10
|
|
|
|
|
11
|
|
|
if ('cli' !== PHP_SAPI) { |
|
12
|
|
|
exit('For security reasons, this script can only be launched from cron or from the command line'); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
exit; |
|
16
|
|
|
|
|
17
|
|
|
$plugin = ExerciseMonitoringPlugin::create(); |
|
18
|
|
|
$em = Database::getManager(); |
|
19
|
|
|
$repo = $em->getRepository(Log::class); |
|
20
|
|
|
$trackExeRepo = $em->getRepository(TrackEExercises::class); |
|
21
|
|
|
|
|
22
|
|
|
$lifetimeDays = (int) $plugin->get(ExerciseMonitoringPlugin::SETTING_SNAPSHOTS_LIFETIME); |
|
23
|
|
|
|
|
24
|
|
|
if (empty($lifetimeDays)) { |
|
25
|
|
|
logging("There is no set time limit"); |
|
26
|
|
|
exit; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$timeLimit = api_get_utc_datetime(null, false, true); |
|
30
|
|
|
$timeLimit->modify("-$lifetimeDays day"); |
|
31
|
|
|
|
|
32
|
|
|
logging( |
|
33
|
|
|
sprintf("Deleting snapshots taken before than %s", $timeLimit->format('Y-m-d H:i:s')) |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$fs = new Filesystem(); |
|
37
|
|
|
|
|
38
|
|
|
$logs = findLogsBeforeThan($timeLimit); |
|
39
|
|
|
|
|
40
|
|
|
foreach ($logs as $log) { |
|
41
|
|
|
$sysPath = ExerciseMonitoringPlugin::generateSnapshotUrl( |
|
42
|
|
|
$log['exe_user_id'], |
|
43
|
|
|
$log['image_filename'], |
|
44
|
|
|
SYS_UPLOAD_PATH |
|
|
|
|
|
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
if (!file_exists($sysPath)) { |
|
48
|
|
|
logging( |
|
49
|
|
|
sprintf("File %s not exists", $sysPath) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$fs->remove($sysPath); |
|
56
|
|
|
|
|
57
|
|
|
Database::update( |
|
58
|
|
|
'plugin_exercisemonitoring_log', |
|
59
|
|
|
['removed' => true], |
|
60
|
|
|
['id = ?' => $log['log_id']] |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
logging( |
|
64
|
|
|
sprintf( |
|
65
|
|
|
"From exe_id %s; deleting filename %s created at %s", |
|
66
|
|
|
$log['exe_id'], |
|
67
|
|
|
$sysPath, |
|
68
|
|
|
$log['created_at'] |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
function findLogsBeforeThan(DateTime $timeLimit): array |
|
74
|
|
|
{ |
|
75
|
|
|
$sql = "SELECT tee.exe_id, l.id AS log_id, l.image_filename, tee.exe_user_id |
|
76
|
|
|
FROM plugin_exercisemonitoring_log l |
|
77
|
|
|
INNER JOIN chamilo.track_e_exercises tee on l.exe_id = tee.exe_id |
|
78
|
|
|
WHERE l.created_at <= '".$timeLimit->format('Y-m-d H:i:s')."' |
|
79
|
|
|
AND l.removed IS FALSE"; |
|
80
|
|
|
|
|
81
|
|
|
$result = Database::query($sql); |
|
82
|
|
|
|
|
83
|
|
|
$rows = []; |
|
84
|
|
|
|
|
85
|
|
|
while ($row = Database::fetch_assoc($result)) { |
|
86
|
|
|
$rows[] = $row; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $rows; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function logging(string $message) |
|
93
|
|
|
{ |
|
94
|
|
|
$time = time(); |
|
95
|
|
|
|
|
96
|
|
|
printf("[%s] %s \n", $time, $message); |
|
97
|
|
|
} |
|
98
|
|
|
|