forked from trampgeek/moodle-qtype_coderunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachepurgeindex.php
More file actions
175 lines (160 loc) · 7.55 KB
/
cachepurgeindex.php
File metadata and controls
175 lines (160 loc) · 7.55 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
// This file is part of CodeRunner - http://coderunner.org.nz
//
// CodeRunner 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.
//
// CodeRunner 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 Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* This script provides an index for purging grading cache entries by course.
*
* @package qtype_coderunner
* @copyright 2024 Paul McKeown, The University of Canterbury
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace qtype_coderunner;
use context_system;
use context;
use html_writer;
use moodle_url;
use cache_config_writer;
use qtype_coderunner;
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
const GREENY = 'border: 1px solid #F0F0F0; background-color:rgb(232, 249, 213); padding: 2px 2px 0px 2px;';
const ORANGY = 'border: 1px solid #F0F0F0; background-color:rgb(249, 242, 213); padding: 2px 2px 0px 2px;';
define('OLDBUTTONTEXT', get_string('purgeoldcachekeysbutton', 'qtype_coderunner')); // For button to purge old entries.
define('ALLBUTTONTEXT', get_string('purgeallcachekeysbutton', 'qtype_coderunner')); // For button to purge old entries.
// Login and check permissions.
$context = context_system::instance();
require_login();
cache_config_writer::update_definitions();
$PAGE->set_url('/question/type/coderunner/cachepurgeindex.php');
$PAGE->set_context($context);
$PAGE->set_title(get_string('cachepurgeindextitle', 'qtype_coderunner'));
function echo_cache_purge_header() {
echo html_writer::tag('p', get_string('cachepurgeindexinfo', 'qtype_coderunner'));
$ttl = abs(get_config('qtype_coderunner', 'gradecachettl'));
$ttldays = round($ttl / 60 / 60 / 24, 4);
echo html_writer::tag('p', get_String('currentttlinfo', 'qtype_coderunner', ['seconds' => $ttl, 'days' => $ttldays]));
}
function link_url_button(int $contextid, int $usettl): string {
$buttonstyle = $usettl ? GREENY : ORANGY;
$buttontext = $usettl ? OLDBUTTONTEXT : ALLBUTTONTEXT;
$url = new moodle_url('/question/type/coderunner/cachepurge.php', ['contextid' => $contextid, 'usettl' => $usettl]);
$link = html_writer::link(
$url,
$buttontext,
['title' => $buttontext,
'style' => $buttonstyle]
);
return $link;
}
/** Echos a list item for context, if it has any cache keys.
* The item has the context name, the key count and buttons/links
* for purge with ttl and purge all.
*/
function echo_line_for_context(int $contextid, string $contextname, int $keycount): void {
if ($keycount > 0) {
$purgeusingttllink = link_url_button($contextid, usettl: 1);
$purgealllink = link_url_button($contextid, usettl : 0);
$litext = "[{$contextid}] " .
$contextname .
' (cache size=<b>' .
$keycount .
'</b>) ' .
$purgeusingttllink .
' ' .
$purgealllink;
$class = 'cachepurge coderunner context normal';
echo html_writer::start_tag('li', ['class' => $class]);
echo $litext;
echo html_writer::end_tag('li');
}
}
// Display.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('coderunnercontexts', 'qtype_coderunner'));
echo '<hr>';
// Gets key counts for contexts the user has suitable rights to.
// NOTE: Should probably echo out the 'Uncategorized' and 'Unknown' category totals.
$keycountsbycontextid = cache_purger::key_counts_for_available_contextids();
// List all contexts available to the user.
if (count($keycountsbycontextid) == 0) {
echo html_writer::tag('p', get_string('noquestionstopurge', 'qtype_coderunner'));
} else {
echo_cache_purge_header();
$oldskool = !(\qtype_coderunner_util::using_mod_qbank()); // No qbanks in Moodle versions less than 5.0.
if (!$oldskool) {
echo html_writer::start_tag('ul');
echo html_writer::tag('li', "Moodle >= 5.0 detected. Listing by course then qbank contexts.");
$allcourses = bulk_tester::get_all_courses();
echo html_writer::tag('li', "Displaying all courses you have access to.");
echo html_writer::tag('li', "Courses are displayed as: <em>[context_id] Course Name (course_id)</em>");
echo html_writer::tag(
'li',
"Qbanks and other question containing contexts are displayed as: <em>[context_id] Context prefix:Context name </em>"
);
echo '<hr>';
echo html_writer::end_tag('ul');
foreach ($allcourses as $courseid => $course) {
$coursecontext = \context_course::instance($courseid);
// Only list for courses that are visible to user.
if (has_capability('moodle/question:editall', $coursecontext)) {
echo $OUTPUT->heading("[{$coursecontext->id}] {$course->name} ({$courseid})", 4);
$allbanks = bulk_tester::get_all_qbanks_for_course($courseid);
if (count($allbanks) > 0) {
echo html_writer::start_tag('ul');
$totalkeysforcourse = 0;
foreach ($allbanks as $qbank) {
$contextid = $qbank->contextid;
$context = \context::instance_by_id($contextid);
$name = $context->get_context_name(true, true);
$keycount = $keycountsbycontextid[$contextid] ?? 0;
$totalkeysforcourse += $keycount;
echo_line_for_context($contextid, $name, $keycount);
} // For each qbank
if ($totalkeysforcourse == 0) {
echo html_writer::tag('li', '<em>No grading cache entries for course.</em>');
}
echo html_writer::end_tag('ul');
}
}
}
} else { // We're going old skool.
// Old skool method does this for all contexts with questions.
// These contexts will typically be courses or course contexts.
echo html_writer::tag('p', 'NOTE: Legacy mode (Moodle ver < 5.0) so only courses with grade cache entries will be listed.');
echo html_writer::start_tag('ul');
foreach ($keycountsbycontextid as $contextid => $keycount) {
$context = context::instance_by_id($contextid);
$name = $context->get_context_name(true, true);
echo_line_for_context($contextid, $name, $keycount);
}
echo html_writer::end_tag('ul');
}
// Display link to admin->cache settings in case someone wants to fully purge grading cache.
echo html_writer::tag(
'p',
"Use link below to open Moodle cache admin page so you can purge the whole coderunner_grading_cache."
);
if (has_capability('moodle/site:config', context_system::instance())) {
$link = html_writer::link(
new moodle_url('/cache/admin.php'),
"Open admin-cache page - for purging whole grading cache.",
['class' => 'link-to-cache-admin',
'data-contextid' => 0,
'style' => ORANGY . ";cursor:pointer;"]
);
echo html_writer::tag('p', $link);
}
}
echo $OUTPUT->footer();