forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.php
341 lines (317 loc) · 15.6 KB
/
dependencies.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack 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.
//
// Stack 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 lets the user find questions with particular features
* and extract statistics about the use of some other things.
*
* This thing leverages the compiledcache as it can be used to query
* the whole logic in a fast way. Thus this does not work for questions
* that have not been compiled.
*
* @copyright 2022 Aalto University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
require_once(__DIR__ . '/../locallib.php');
require_once(__DIR__ . '/../../../engine/lib.php');
require_once(__DIR__ . '/../stack/utils.class.php');
require_once(__DIR__ . '/../stack/options.class.php');
require_once(__DIR__ . '/../stack/maximaparser/utils.php');
require_once(__DIR__ . '/../vle_specific.php');
require_login();
$context = context_system::instance();
require_capability('qtype/stack:usediagnostictools', $context);
$urlparams = [];
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url('/question/type/stack/adminui/dependencies.php', $urlparams);
$title = 'Dependency checker';
$PAGE->set_title($title);
// Figure out the number of questions that can be explored.
// In Moodle 4+ hidden questions occur when they are included in a quiz, but then are deleted from the question bank.
// In this case the database sets the field `status` to `'hidden'` within the question versions database.
$query = 'SELECT count(*) as notcompiled FROM {question} q, ' .
'{qtype_stack_options} o, {question_versions} v WHERE q.id = o.questionid AND q.id = v.id ' . '
AND NOT v.status = "hidden" AND o.compiledcache = ?;';
$notcompiled = $DB->get_recordset_sql($query, ['{}']);
$nnotcompiled = 0;
$ncompiled = 0;
foreach ($notcompiled as $item) {
$nnotcompiled = $item->notcompiled;
}
$notcompiled->close();
$query = 'SELECT count(*) as compiled FROM {question} q, ' .
'{qtype_stack_options} o, {question_versions} v WHERE q.id = o.questionid AND q.id = v.id ' . '
AND NOT v.status = "hidden" AND o.compiledcache = ?;';
$compiled = $DB->get_recordset_sql($query, ['{}']);
foreach ($compiled as $item) {
$ncompiled = $item->compiled;
}
$compiled->close();
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
echo '<p>This tool only acts on succesfully compiled questions, to compile questions run the bulk tester ' .
'or preview/use those questions.</p><p>';
if ($ncompiled !== $notcompiled) {
echo stack_string_error('errors') . '. ';
}
echo 'Currently there are ' . $ncompiled . ' compiled questions and ' . $nnotcompiled .
' questions that have not been succesfully compiled.</p>';
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['includes' => 1, 'sesskey' => sesskey()]),
'Find "includes"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['jsxgraphs' => 1, 'sesskey' => sesskey()]),
'Find "jsxgraphs"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['geogebras' => 1, 'sesskey' => sesskey()]),
'Find "geogebra"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['script' => 1, 'sesskey' => sesskey()]),
'Find "<script"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['PLUGINFILE' => 1, 'sesskey' => sesskey()]),
'Find "@@PLUGINFILE@@"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['langs' => 1, 'sesskey' => sesskey()]),
'Find "langs"');
echo $OUTPUT->single_button(
new moodle_url($PAGE->url, ['todo' => 1, 'sesskey' => sesskey()]),
'Find "todo"');
if (data_submitted() && optional_param('includes', false, PARAM_BOOL)) {
/*
* Search for questions that have any includes, both keyval and CASText.
* Both are noted in the compiled cache as important meta.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg') . ';', ['trg' => '%"includes"%']);
echo '<h4>Questions using includes</h4>';
echo '<table><thead><tr><th>Question</th><th>Keyval includes</th><th>Castext includes</th></tr></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
// Confirm that it does have these.
if (isset($q->compiledcache['includes']) && (
(isset($q->compiledcache['includes']['keyval']) && count($q->compiledcache['includes']['keyval']) > 0) ||
(isset($q->compiledcache['includes']['castext']) && count($q->compiledcache['includes']['castext']) > 0))) {
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td>';
echo '<td>';
if (isset($q->compiledcache['includes']['keyval'])) {
foreach ($q->compiledcache['includes']['keyval'] as $url) {
echo html_writer::start_tag('a', ['href' => $url]);
echo $url;
echo html_writer::end_tag('a');
}
}
echo '</td>';
echo '<td>';
if (isset($q->compiledcache['includes']['castext'])) {
foreach ($q->compiledcache['includes']['castext'] as $url) {
echo html_writer::start_tag('a', ['href' => $url]);
echo $url;
echo html_writer::end_tag('a');
}
}
echo '</td></tr>';
}
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('jsxgraphs', false, PARAM_BOOL)) {
/*
* JSXGraphs are spotted from the compiled cache, finding '["jsxgraph",'
* means that there are STACK block based JSXGraphs. '</jsxgraph>' would
* mean that the official filter is in play, if we find "jsxgraph" in any other
* form then we probably have something else in play or a "TO-DO" note.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg', false) . ';', ['trg' => '%jsxgraph%']);
echo '<h4>Questions containing JSXGraph related terms</h4>';
echo '<table><thead><tr><th>Question</th>' .
'<th>[[jsxgraph]]</th><th><jsxgraph</th><th>Other</th></tr></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
$block = 'false';
$filter = 'false';
$other = 'false';
$json = json_encode($q->compiledcache);
if (mb_strpos($json, 'STACK JSXGraph') !== false && mb_strpos($json, 'JSXGRAPH_COUNT') !== false) {
$block = 'true';
$json = str_replace('STACK JSXGraph', '', $json);
$json = str_replace('JSXGRAPH_COUNT', '', $json);
}
if (mb_strpos($json, '</jsxgraph>') !== false) {
$filter = 'true';
$json = str_replace('</jsxgraph>', '', $json);
$json = str_replace('<jsxgraph', '', $json);
}
if (mb_stripos($json, 'jsxgraph') !== false) {
// This currentyl provides false positives, as it cannot separate loaded
// scripts from those loaded through the block.
$other = 'true';
if ($block === 'true') {
$other = 'maybe';
}
}
// Confirm that it does have these.
if ($block || $filter || $other) {
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td>';
echo "<td>$block</td><td>$filter</td><td>$other</td></tr>";
}
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('geogebras', false, PARAM_BOOL)) {
/*
* GeoGebra Graphs are spotted from the compiled cache, finding '["geogebra",'
* means that there are STACK block based GeoGebra. '</geogebra>' would
* mean that the official filter is in play, if we find "geogebra" in any other
* form then we probably have something else in play or a "TO-DO" note.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg', false) . ';', ['trg' => '%geogebra%']);
echo '<h4>Questions containing GeogGebra related terms</h4>';
echo '<table><thead><tr><th>Question</th>' .
'<th>[[geogebra]]</th><th><geogebra</th><th>Other</th></tr></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
$block = 'false';
$filter = 'false';
$other = 'false';
$json = json_encode($q->compiledcache);
if (mb_strpos($json, '[\\"geogebra\\",') !== false) {
$block = 'true';
$json = str_replace('[\\"geogebra\\",', '', $json);
}
if (mb_strpos($json, '</geogebra>') !== false) {
$filter = 'true';
$json = str_replace('</geogebra>', '', $json);
$json = str_replace('<geogebra', '', $json);
}
if (mb_stripos($json, 'geogebra') !== false) {
$other = 'true';
}
// Confirm that it does have these.
if ($block || $filter || $other) {
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td>';
echo "<td>$block</td><td>$filter</td><td>$other</td></tr>";
}
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('script', false, PARAM_BOOL)) {
/*
* <script present in the question
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg', false) . ';', ['trg' => '%<script%']);
echo '<h4>Questions containing script tags</h4>';
echo '<table><thead><tr><th>Question</th></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td></tr>';
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('PLUGINFILE', false, PARAM_BOOL)) {
/*
* @@PLUGINFILE@@ present in the question.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg') . ';', ['trg' => '%@@PLUGINFILE@@%']);
echo '<h4>Questions containing attached files handled by Moodle</h4>';
echo '<table><thead><tr><th>Question</th></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td></tr>';
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('langs', false, PARAM_BOOL)) {
/*
* Questions that have localisation.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' . $DB->sql_like('o.compiledcache', ':trg') . ' AND NOT ' .
$DB->sql_like('o.compiledcache', ':other') . ';', ['trg' => '%"langs":[%', 'other' => '%"langs":[]%']);
echo '<h4>Questions containing localisation using means we understand.</h4>';
echo '<table><thead><tr><th>Question</th><th>Langs</th></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) . '</td><td>';
echo implode(', ', $q->get_cached('langs'));
echo '</td></tr>';
}
echo '</tbody></table>';
}
if (data_submitted() && optional_param('todo', false, PARAM_BOOL)) {
/*
* Todo blocks present in the question.
*/
$qs = $DB->get_recordset_sql('SELECT q.id as questionid FROM {question} q, {qtype_stack_options} o WHERE ' .
'q.id = o.questionid AND ' .
$DB->sql_like('o.compiledcache', ':trg') . ';', ['trg' => '%stack_todo%']);
echo '<h4>Questions containing [[todo]] blocks</h4>';
echo '<table><thead><tr><th>Question</th><th>Tags</th></thead><tbody>';
// Load the whole question, simpler to get the contexts correct that way.
foreach ($qs as $item) {
$q = question_bank::load_question($item->questionid);
$tags = $q->get_question_todos();
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($q);
$qurl = qbank_previewquestion\helper::question_preview_url($item->questionid,
null, null, null, null, $context);
echo "<tr><td>" . $q->name . ' ' .
$OUTPUT->action_icon($qurl, new pix_icon('t/preview', get_string('preview'))) .
'</td><td>' . implode(', ', $tags). '<td></tr>';
}
echo '</tbody></table>';
}
echo $OUTPUT->footer();