-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge.php
127 lines (112 loc) · 4.92 KB
/
purge.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* TODO describe file purge
*
* @package local_purgeoldassignments
* @copyright 2025 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
$id = required_param('id', PARAM_INT);
$purge = optional_param('purge', null, PARAM_INT);
$component = optional_param('component', '', PARAM_COMPONENT);
$confirm = optional_param('confirm', 0, PARAM_INT);
list ($course, $cm) = get_course_and_cm_from_cmid($id, 'assign');
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('local/purgeoldassignments:purgeassignments', $context);
$url = new moodle_url('/local/purgeoldassignments/purge.php', ['id' => $id, 'purge' => $purge, 'component' => $component]);
$PAGE->set_url($url);
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
if (!empty($purge)) {
$purgeoptions = [1, 2, 3];
if (!in_array($purge, $purgeoptions)) {
echo "invalid purge option";
die;
}
if ($confirm && confirm_sesskey()) {
// Schedule deletion task.
$task = new \local_purgeoldassignments\task\purge();
// Add custom data.
$task->set_custom_data([
'contextid' => $context->id,
'component' => $component,
'purge' => $purge
]);
// Queue it.
\core\task\manager::queue_adhoc_task($task);
echo $OUTPUT->notification(get_string("purgetriggered", 'local_purgeoldassignments'));
} else {
$cancelurl = new moodle_url('/local/purgeoldassignments/purge.php', ['id' => $id]);
$url->param('confirm', 1);
echo $OUTPUT->confirm(get_string('areyousure', 'local_purgeoldassignments', $component), $url, $cancelurl);
}
} else {
// Get pending ad-hoc tasks.
$sql = "SELECT *
FROM {task_adhoc}
WHERE (component = 'local_purgeoldassignments' or classname = '\\local_purgeoldassignments\\task\\purge')
AND customdata like '%contextid\":{$context->id},%'
AND faildelay = 0";
$adhoctasks = $DB->get_records_sql($sql);
$tasksrunning = [];
if (!empty($adhoctasks)) {
foreach ($adhoctasks as $task) {
$customdata = json_decode($task->customdata);
if ($customdata->contextid == $context->id) {
$time = !empty($task->timecreated) ? $task->timecreated : $task->nextruntime;
$tasksrunning[$customdata->component] = $time;
}
}
}
// Get Total size of current areas.
$filesizes = local_purgeoldassignments_get_stats($context->id);
foreach ($filesizes as $component => $filesize) {
if (!empty($filesize->total)) {
echo $OUTPUT->heading($component);
echo "<p>" . get_string('componentcurrentsize', 'local_purgeoldassignments', display_size($filesize->total)) ."</p>";
if (!empty($filesize->olderthan1)) {
echo "<p>" .get_string('componentolderthan1', 'local_purgeoldassignments',
display_size($filesize->olderthan1)) ."</p>";
}
if (!empty($filesize->olderthan2)) {
echo "<p>" .get_string('componentolderthan2', 'local_purgeoldassignments',
display_size($filesize->olderthan2)) ."</p>";
}
if (!empty($filesize->olderthan3)) {
echo "<p>" .get_string('componentolderthan3', 'local_purgeoldassignments',
display_size($filesize->olderthan3)) ."</p>";
}
if (!empty($tasksrunning[$component])) {
echo "<div>".get_string("taskpending", "local_purgeoldassignments",
userdate($tasksrunning[$component]))."</div>";
} else {
$select = [
1 => '1 year',
2 => '2 years',
3 => '3 years'
];
echo "<div>" . get_string("purgefilesolderthan", "local_purgeoldassignments");
$url->param('component', $component);
echo $OUTPUT->single_select(new moodle_url($url), 'purge', $select);
echo "</div>";
}
}
}
}
echo $OUTPUT->footer();