MDL-29029 move blog upgrade to admin tools
[moodle.git] / admin / health.php
CommitLineData
f578af59 1<?php
527d9cdd 2
9bbb40d6 3 ob_start(); //for whitespace test
527d9cdd 4 require_once('../config.php');
5
5ec8a4f0 6 // extra whitespace test - intentionally breaks cookieless mode
7 $extraws = '';
e691a373 8 while (ob_get_level()) {
9bbb40d6 9 $extraws .= ob_get_contents();
e691a373 10 ob_end_clean();
9bbb40d6 11 }
12
c9a49cea 13 require_once($CFG->libdir.'/adminlib.php');
1ae083e4 14
15 admin_externalpage_setup('healthcenter');
c9a49cea 16
527d9cdd 17 define('SEVERITY_NOTICE', 'notice');
18 define('SEVERITY_ANNOYANCE', 'annoyance');
19 define('SEVERITY_SIGNIFICANT', 'significant');
20 define('SEVERITY_CRITICAL', 'critical');
21
6544f8d8 22 $solution = optional_param('solution', 0, PARAM_SAFEDIR); //in fact it is class name alhanumeric and _
993ef10d 23
527d9cdd 24 require_login();
957f6fc9 25 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
527d9cdd 26
27 $site = get_site();
527d9cdd 28
61ef8f9f 29 echo $OUTPUT->header();
527d9cdd 30
31echo <<<STYLES
32<style type="text/css">
33div#healthnoproblemsfound {
34 width: 60%;
35 margin: auto;
36 padding: 1em;
37 border: 1px black solid;
38 -moz-border-radius: 6px;
39}
40dl.healthissues {
41 width: 60%;
42 margin: auto;
43}
44dl.critical dt, dl.critical dd {
45 background-color: #a71501;
46}
47dl.significant dt, dl.significant dd {
48 background-color: #d36707;
49}
50dl.annoyance dt, dl.annoyance dd {
51 background-color: #dba707;
52}
53dl.notice dt, dl.notice dd {
54 background-color: #e5db36;
55}
56dt.solution, dd.solution, div#healthnoproblemsfound {
57 background-color: #5BB83E !important;
58}
59dl.healthissues dt, dl.healthissues dd {
60 margin: 0px;
61 padding: 1em;
62 border: 1px black solid;
63}
64dl.healthissues dt {
65 font-weight: bold;
66 border-bottom: none;
67 padding-bottom: 0.5em;
68}
69dl.healthissues dd {
70 border-top: none;
71 padding-top: 0.5em;
72 margin-bottom: 10px;
73}
74dl.healthissues dd form {
75 margin-top: 0.5em;
76 text-align: right;
77}
78form#healthformreturn {
79 text-align: center;
80 margin: 2em;
81}
82dd.solution p {
83 padding: 0px;
84 margin: 1em 0px;
85}
86dd.solution li {
87 margin-top: 1em;
88}
89
90</style>
91STYLES;
92
045e9e24 93 if(strpos($solution, 'problem_') === 0 && class_exists($solution)) {
527d9cdd 94 health_print_solution($solution);
95 }
96 else {
97 health_find_problems();
98 }
99
100
73d6f52f 101 echo $OUTPUT->footer();
527d9cdd 102
103
104function health_find_problems() {
395a095e 105 global $OUTPUT;
527d9cdd 106
2fff8846 107 echo $OUTPUT->heading(get_string('healthcenter'));
527d9cdd 108
109 $issues = array(
110 SEVERITY_CRITICAL => array(),
111 SEVERITY_SIGNIFICANT => array(),
112 SEVERITY_ANNOYANCE => array(),
113 SEVERITY_NOTICE => array(),
114 );
115 $problems = 0;
116
117 for($i = 1; $i < 1000000; ++$i) {
118 $classname = sprintf('problem_%06d', $i);
119 if(!class_exists($classname)) {
120 break;
121 }
122 $problem = new $classname;
123 if($problem->exists()) {
124 $severity = $problem->severity();
125 $issues[$severity][$classname] = array(
126 'severity' => $severity,
127 'description' => $problem->description(),
128 'title' => $problem->title()
129 );
130 ++$problems;
131 }
132 unset($problem);
133 }
134
135 if($problems == 0) {
136 echo '<div id="healthnoproblemsfound">';
137 echo get_string('healthnoproblemsfound');
138 echo '</div>';
139 }
140 else {
2fff8846 141 echo $OUTPUT->heading(get_string('healthproblemsdetected'));
527d9cdd 142 $severities = array(SEVERITY_CRITICAL, SEVERITY_SIGNIFICANT, SEVERITY_ANNOYANCE, SEVERITY_NOTICE);
143 foreach($severities as $severity) {
144 if(!empty($issues[$severity])) {
145 echo '<dl class="healthissues '.$severity.'">';
146 foreach($issues[$severity] as $classname => $data) {
147 echo '<dt id="'.$classname.'">'.$data['title'].'</dt>';
148 echo '<dd>'.$data['description'];
149 echo '<form action="health.php#solution" method="get">';
150 echo '<input type="hidden" name="solution" value="'.$classname.'" /><input type="submit" value="'.get_string('viewsolution').'" />';
151 echo '</form></dd>';
152 }
153 echo '</dl>';
154 }
155 }
156 }
157}
158
159function health_print_solution($classname) {
2fff8846 160 global $OUTPUT;
527d9cdd 161 $problem = new $classname;
162 $data = array(
163 'title' => $problem->title(),
164 'severity' => $problem->severity(),
165 'description' => $problem->description(),
166 'solution' => $problem->solution()
167 );
168
2fff8846 169 echo $OUTPUT->heading(get_string('healthcenter'));
170 echo $OUTPUT->heading(get_string('healthproblemsolution'));
527d9cdd 171 echo '<dl class="healthissues '.$data['severity'].'">';
172 echo '<dt>'.$data['title'].'</dt>';
173 echo '<dd>'.$data['description'].'</dd>';
174 echo '<dt id="solution" class="solution">'.get_string('healthsolution').'</dt>';
175 echo '<dd class="solution">'.$data['solution'].'</dd></dl>';
176 echo '<form id="healthformreturn" action="health.php#'.$classname.'" method="get">';
177 echo '<input type="submit" value="'.get_string('healthreturntomain').'" />';
178 echo '</form>';
179}
180
181class problem_base {
182 function exists() {
183 return false;
184 }
185 function title() {
186 return '???';
187 }
188 function severity() {
189 return SEVERITY_NOTICE;
190 }
191 function description() {
192 return '';
193 }
194 function solution() {
195 return '';
196 }
197}
198
527d9cdd 199class problem_000002 extends problem_base {
200 function title() {
9bbb40d6 201 return 'Extra characters at the end of config.php or other library function';
527d9cdd 202 }
203 function exists() {
9bbb40d6 204 global $extraws;
205
206 if($extraws === '') {
207 return false;
527d9cdd 208 }
209 return true;
210 }
211 function severity() {
212 return SEVERITY_SIGNIFICANT;
213 }
214 function description() {
9bbb40d6 215 return 'Your Moodle configuration file config.php or another library file, contains some characters after the closing PHP tag (?>). This causes Moodle to exhibit several kinds of problems (such as broken downloaded files) and must be fixed.';
527d9cdd 216 }
217 function solution() {
218 global $CFG;
9bbb40d6 219 return 'You need to edit <strong>'.$CFG->dirroot.'/config.php</strong> and remove all characters (including spaces and returns) after the ending ?> tag. These two characters should be the very last in that file. The extra trailing whitespace may be also present in other PHP files that are included from lib/setup.php.';
527d9cdd 220 }
221}
222
223class problem_000003 extends problem_base {
224 function title() {
225 return '$CFG->dataroot does not exist or does not have write permissions';
226 }
227 function exists() {
228 global $CFG;
229 if(!is_dir($CFG->dataroot) || !is_writable($CFG->dataroot)) {
230 return true;
231 }
232 return false;
233 }
234 function severity() {
235 return SEVERITY_SIGNIFICANT;
236 }
237 function description() {
238 global $CFG;
239 return 'Your <strong>config.php</strong> says that your "data root" directory is <strong>'.$CFG->dataroot.'</strong>. However, this directory either does not exist or cannot be written to by Moodle. This means that a variety of problems will be present, such as users not being able to log in and not being able to upload any files. It is imperative that you address this problem for Moodle to work correctly.';
240 }
241 function solution() {
242 global $CFG;
243 return 'First of all, make sure that the directory <strong>'.$CFG->dataroot.'</strong> exists. If the directory does exist, then you must make sure that Moodle is able to write to it. Contact your web server administrator and request that he gives write permissions for that directory to the user that the web server process is running as.';
244 }
245}
246
247class problem_000004 extends problem_base {
248 function title() {
249 return 'cron.php is not set up to run automatically';
250 }
251 function exists() {
1d8bf5f0 252 global $DB;
253 $lastcron = $DB->get_field_sql('SELECT max(lastcron) FROM {modules}');
527d9cdd 254 return (time() - $lastcron > 3600 * 24);
255 }
256 function severity() {
257 return SEVERITY_SIGNIFICANT;
258 }
259 function description() {
260 return 'The cron.php mainenance script has not been run in the past 24 hours. This probably means that your server is not configured to automatically run this script in regular time intervals. If this is the case, then Moodle will mostly work as it should but some operations (notably sending email to users) will not be carried out at all.';
261 }
262 function solution() {
263 global $CFG;
264 return 'For detailed instructions on how to enable cron, see <a href="'.$CFG->wwwroot.'/doc/?file=install.html#cron">this section</a> of the installation manual.';
265 }
266}
267
268class problem_000005 extends problem_base {
269 function title() {
270 return 'PHP: session.auto_start is enabled';
271 }
272 function exists() {
273 return ini_get_bool('session.auto_start');
274 }
275 function severity() {
276 return SEVERITY_CRITICAL;
277 }
278 function description() {
279 return 'Your PHP configuration includes an enabled setting, session.auto_start, that <strong>must be disabled</strong> in order for Moodle to work correctly. Notable symptoms arising from this misconfiguration include fatal errors and/or blank pages when trying to log in.';
280 }
281 function solution() {
282 global $CFG;
283 return '<p>There are two ways you can solve this problem:</p><ol><li>If you have access to your main <strong>php.ini</strong> file, then find the line that looks like this: <pre>session.auto_start = 1</pre> and change it to <pre>session.auto_start = 0</pre> and then restart your web server. Be warned that this, as any other PHP setting change, might affect other web applications running on the server.</li><li>Finally, you may be able to change this setting just for your site by creating or editing the file <strong>'.$CFG->dirroot.'/.htaccess</strong> to contain this line: <pre>php_value session.auto_start "0"</pre></li></ol>';
284 }
285}
286
287class problem_000006 extends problem_base {
288 function title() {
289 return 'PHP: magic_quotes_runtime is enabled';
290 }
291 function exists() {
292 return (ini_get_bool('magic_quotes_runtime'));
293 }
294 function severity() {
295 return SEVERITY_SIGNIFICANT;
296 }
297 function description() {
298 return 'Your PHP configuration includes an enabled setting, magic_quotes_runtime, that <strong>must be disabled</strong> in order for Moodle to work correctly. Notable symptoms arising from this misconfiguration include strange display errors whenever a text field that includes single or double quotes is processed.';
299 }
300 function solution() {
301 global $CFG;
302 return '<p>There are two ways you can solve this problem:</p><ol><li>If you have access to your main <strong>php.ini</strong> file, then find the line that looks like this: <pre>magic_quotes_runtime = On</pre> and change it to <pre>magic_quotes_runtime = Off</pre> and then restart your web server. Be warned that this, as any other PHP setting change, might affect other web applications running on the server.</li><li>Finally, you may be able to change this setting just for your site by creating or editing the file <strong>'.$CFG->dirroot.'/.htaccess</strong> to contain this line: <pre>php_value magic_quotes_runtime "Off"</pre></li></ol>';
303 }
304}
305
306class problem_000007 extends problem_base {
307 function title() {
308 return 'PHP: file_uploads is disabled';
309 }
310 function exists() {
311 return !ini_get_bool('file_uploads');
312 }
313 function severity() {
314 return SEVERITY_SIGNIFICANT;
315 }
316 function description() {
317 return 'Your PHP configuration includes a disabled setting, file_uploads, that <strong>must be enabled</strong> to let Moodle offer its full functionality. Until this setting is enabled, it will not be possible to upload any files into Moodle. This includes, for example, course content and user pictures.';
318 }
319 function solution() {
320 global $CFG;
321 return '<p>There are two ways you can solve this problem:</p><ol><li>If you have access to your main <strong>php.ini</strong> file, then find the line that looks like this: <pre>file_uploads = Off</pre> and change it to <pre>file_uploads = On</pre> and then restart your web server. Be warned that this, as any other PHP setting change, might affect other web applications running on the server.</li><li>Finally, you may be able to change this setting just for your site by creating or editing the file <strong>'.$CFG->dirroot.'/.htaccess</strong> to contain this line: <pre>php_value file_uploads "On"</pre></li></ol>';
322 }
323}
324
325class problem_000008 extends problem_base {
326 function title() {
327 return 'PHP: memory_limit cannot be controlled by Moodle';
328 }
329 function exists() {
395a095e
PS
330 global $CFG;
331
8a91d7fc 332 $oldmemlimit = @ini_get('memory_limit');
346c5887 333 if (empty($oldmemlimit)) {
527d9cdd 334 // PHP not compiled with memory limits, this means that it's
8a91d7fc 335 // probably limited to 8M or in case of Windows not at all.
336 // We can ignore it for now - there is not much to test anyway
337 // TODO: add manual test that fills memory??
338 return false;
339 }
340 $oldmemlimit = get_real_size($oldmemlimit);
346c5887
PS
341 //now lets change the memory limit to something higher
342 $newmemlimit = ($oldmemlimit + 1024*1024*5);
343 raise_memory_limit($newmemlimit);
8a91d7fc 344 $testmemlimit = get_real_size(@ini_get('memory_limit'));
345 //verify the change had any effect at all
346 if ($oldmemlimit == $testmemlimit) {
347 //memory limit can not be changed - is it big enough then?
348 if ($oldmemlimit < get_real_size('128M')) {
349 return true;
350 } else {
351 return false;
352 }
527d9cdd 353 }
7022dd39 354 reduce_memory_limit($oldmemlimit);
527d9cdd 355 return false;
356 }
357 function severity() {
8a91d7fc 358 return SEVERITY_NOTICE;
527d9cdd 359 }
360 function description() {
8a91d7fc 361 return 'The settings for PHP on your server do not allow a script to request more memory during its execution. '.
362 'This means that there is a hard limit of '.@ini_get('memory_limit').' for each script. '.
363 'It is possible that certain operations within Moodle will require more than this amount in order '.
364 'to complete successfully, especially if there are lots of data to be processed.';
527d9cdd 365 }
366 function solution() {
8a91d7fc 367 return 'It is recommended that you contact your web server administrator to address this issue.';
527d9cdd 368 }
369}
370
b70b8fe1 371class problem_000009 extends problem_base {
372 function title() {
373 return 'SQL: using account without password';
374 }
375 function exists() {
376 global $CFG;
377 return empty($CFG->dbpass);
378 }
379 function severity() {
380 return SEVERITY_CRITICAL;
381 }
382 function description() {
383 global $CFG;
384 return 'The user account your are connecting to the database server with is set up without a password. This is a very big security risk and is only somewhat lessened if your database is configured to not accept connections from any hosts other than the server Moodle is running on. Unless you use a strong password to connect to the database, you risk unauthorized access to and manipulation of your data.'.($CFG->dbuser != 'root'?'':' <strong>This is especially alarming because such access to the database would be as the superuser (root)!</strong>');
385 }
386 function solution() {
387 global $CFG;
388 return 'You should change the password of the user <strong>'.$CFG->dbuser.'</strong> both in your database and in your Moodle <strong>config.php</strong> immediately!'.($CFG->dbuser != 'root'?'':' It would also be a good idea to change the user account from root to something else, because this would lessen the impact in the event that your database is compromised anyway.');
389 }
390}
c5b26d78 391/* // not implemented in 2.0 yet
39ba6d58 392class problem_000010 extends problem_base {
393 function title() {
394 return 'Uploaded files: slasharguments disabled or not working';
395 }
396 function exists() {
397 if (!$this->is_enabled()) {
eef868d1 398 return true;
39ba6d58 399 }
48283ff6 400 if ($this->status() < 1) {
39ba6d58 401 return true;
402 }
403 return false;
404 }
405 function severity() {
406 if ($this->is_enabled() and $this->status() == 0) {
407 return SEVERITY_SIGNIFICANT;
eef868d1 408 } else {
39ba6d58 409 return SEVERITY_ANNOYANCE;
410 }
411 }
412 function description() {
48283ff6 413 global $CFG;
39ba6d58 414 $desc = 'Slasharguments are needed for relative linking in uploaded resources:<ul>';
415 if (!$this->is_enabled()) {
eef868d1 416 $desc .= '<li>slasharguments are <strong>disabled</strong> in Moodle configuration</li>';
39ba6d58 417 } else {
eef868d1 418 $desc .= '<li>slasharguments are enabled in Moodle configuration</li>';
39ba6d58 419 }
48283ff6 420 if ($this->status() == -1) {
6544f8d8 421 $desc .= '<li>can not run automatic test, you can verify it <a href="'.$CFG->wwwroot.'/file.php/testslasharguments" target="_blank">here</a> manually</li>';
48283ff6 422 } else if ($this->status() == 0) {
39ba6d58 423 $desc .= '<li>slashargument test <strong>failed</strong>, please check server configuration</li>';
424 } else {
425 $desc .= '<li>slashargument test passed</li>';
426 }
427 $desc .= '</ul>';
428 return $desc;
429 }
430 function solution() {
48283ff6 431 global $CFG;
39ba6d58 432 $enabled = $this->is_enabled();
433 $status = $this->status();
434 $solution = '';
435 if ($enabled and ($status == 0)) {
436 $solution .= 'Slasharguments are enabled, but the test failed. Please disable slasharguments in Moodle configuration or fix the server configuration.<hr />';
437 } else if ((!$enabled) and ($status == 0)) {
438 $solution .= 'Slasharguments are disabled and the test failed. You may try to fix the server configuration.<hr />';
48283ff6 439 } else if ($enabled and ($status == -1)) {
440 $solution .= 'Slasharguments are enabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
441 } else if ((!$enabled) and ($status == -1)) {
442 $solution .= 'Slasharguments are disabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
39ba6d58 443 } else if ((!$enabled) and ($status > 0)) {
444 $solution .= 'Slasharguments are disabled though the iternal test is OK. You should enable slasharguments in Moodle configuration.';
445 } else if ($enabled and ($status > 0)) {
446 $solution .= 'Congratulations - everything seems OK now :-D';
447 }
48283ff6 448 if ($status < 1) {
eef868d1 449 $solution .= '<p>IIS:<ul><li>try to add <code>cgi.fix_pathinfo=1</code> to php.ini</li><li>do NOT enable AllowPathInfoForScriptMappings !!!</li><li>slasharguments may not work when using ISAPI and PHP 4.3.10 and older</li></ul></p>';
450 $solution .= '<p>Apache 1:<ul><li>try to add <code>cgi.fix_pathinfo=1</code> to php.ini</li></ul></p>';
451 $solution .= '<p>Apache 2:<ul><li>you must add <code>AcceptPathInfo on</code> to php.ini or .htaccess</li><li>try to add <code>cgi.fix_pathinfo=1</code> to php.ini</li></ul></p>';
39ba6d58 452 }
453 return $solution;
454 }
455 function is_enabled() {
456 global $CFG;
457 return !empty($CFG->slasharguments);
458 }
459 function status() {
460 global $CFG;
48283ff6 461 $handle = @fopen($CFG->wwwroot.'/file.php?file=/testslasharguments', "r");
9bbb40d6 462 $contents = @trim(fread($handle, 10));
48283ff6 463 @fclose($handle);
464 if ($contents != 'test -1') {
465 return -1;
466 }
467 $handle = @fopen($CFG->wwwroot.'/file.php/testslasharguments', "r");
9bbb40d6 468 $contents = trim(@fread($handle, 10));
39ba6d58 469 @fclose($handle);
470 switch ($contents) {
48283ff6 471 case 'test 1': return 1;
472 case 'test 2': return 2;
39ba6d58 473 default: return 0;
48283ff6 474 }
39ba6d58 475 }
c5b26d78 476}*/
39ba6d58 477
ff5328a2 478class problem_000012 extends problem_base {
479 function title() {
480 return 'Random questions data consistency';
481 }
482 function exists() {
483 global $DB;
484 return $DB->record_exists_select('question', "qtype = 'random' AND parent <> id", array());
485 }
486 function severity() {
487 return SEVERITY_ANNOYANCE;
488 }
489 function description() {
490 return '<p>For random questions, question.parent should equal question.id. ' .
491 'There are some questions in your database for which this is not true. ' .
492 'One way that this could have happened is for random questions restored from backup before ' .
493 '<a href="http://tracker.moodle.org/browse/MDL-5482">MDL-5482</a> was fixed.</p>';
494 }
495 function solution() {
496 global $CFG;
497 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the SQL</p>' .
498 '<pre>UPDATE ' . $CFG->prefix . 'question SET parent = id WHERE qtype = \'random\' and parent &lt;> id;</pre>';
499 }
500}
501
502class problem_000013 extends problem_base {
503 function title() {
504 return 'Multi-answer questions data consistency';
505 }
506 function exists() {
507 global $DB;
20207b82 508 $positionexpr = $DB->sql_position($DB->sql_concat("','", "q.id", "','"),
ff5328a2 509 $DB->sql_concat("','", "qma.sequence", "','"));
510 return $DB->record_exists_sql("
511 SELECT * FROM {question} q
512 JOIN {question_multianswer} qma ON $positionexpr > 0
513 WHERE qma.question <> q.parent") ||
514 $DB->record_exists_sql("
515 SELECT * FROM {question} q
516 JOIN {question} parent_q ON parent_q.id = q.parent
517 WHERE q.category <> parent_q.category");
518 }
519 function severity() {
520 return SEVERITY_ANNOYANCE;
521 }
522 function description() {
523 return '<p>For each sub-question whose id is listed in ' .
524 'question_multianswer.sequence, its question.parent field should equal ' .
525 'question_multianswer.question; and each sub-question should be in the same ' .
526 'category as its parent. There are questions in your database for ' .
527 'which this is not the case. One way that this could have happened is ' .
528 'for multi-answer questions restored from backup before ' .
529 '<a href="http://tracker.moodle.org/browse/MDL-14750">MDL-14750</a> was fixed.</p>';
530 }
531 function solution() {
532 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the ' .
533 'code in question_multianswer_fix_subquestion_parents_and_categories in ' .
534 '<a href="http://cvs.moodle.org/moodle/question/type/multianswer/db/upgrade.php?revision=1.1.10.2&amp;view=markup">/question/type/multianswer/db/upgrade.php' .
535 'from the 1.9 stable branch</a>.</p>';
536 }
537}
538
539class problem_000014 extends problem_base {
540 function title() {
541 return 'Only multianswer and random questions should be the parent of another question';
542 }
543 function exists() {
544 global $DB;
545 return $DB->record_exists_sql("
546 SELECT * FROM {question} q
547 JOIN {question} parent_q ON parent_q.id = q.parent
548 WHERE parent_q.qtype NOT IN ('random', 'multianswer')");
549 }
550 function severity() {
551 return SEVERITY_ANNOYANCE;
552 }
553 function description() {
554 return '<p>You have questions that violate this in your databse. ' .
555 'You will need to investigate to determine how this happened.</p>';
556 }
557 function solution() {
558 return '<p>It is impossible to give a solution without knowing more about ' .
559 ' how the problem was caused. You may be able to get help from the ' .
560 '<a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a>.</p>';
561 }
562}
563
564class problem_000015 extends problem_base {
565 function title() {
566 return 'Question categories should belong to a valid context';
567 }
568 function exists() {
569 global $DB;
570 return $DB->record_exists_sql("
571 SELECT qc.*, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
572 FROM {question_categories} qc
573 LEFT JOIN {context} con ON qc.contextid = con.id
574 WHERE con.id IS NULL");
575 }
576 function severity() {
577 return SEVERITY_ANNOYANCE;
578 }
579 function description() {
580 global $DB;
581 $problemcategories = $DB->get_records_sql("
582 SELECT qc.id, qc.name, qc.contextid, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
583 FROM {question_categories} qc
584 LEFT JOIN {context} con ON qc.contextid = con.id
585 WHERE con.id IS NULL
586 ORDER BY numquestions DESC, qc.name");
587 $table = '<table><thead><tr><th>Cat id</th><th>Category name</th>' .
588 "<th>Context id</th><th>Num Questions</th></tr></thead><tbody>\n";
ff5328a2 589 foreach ($problemcategories as $cat) {
af2f92d0 590 $table .= "<tr><td>$cat->id</td><td>" . s($cat->name) . "</td><td>" .
ff5328a2 591 $cat->contextid ."</td><td>$cat->numquestions</td></tr>\n";
ff5328a2 592 }
593 $table .= '</tbody></table>';
594 return '<p>All question categories are linked to a context id, and, ' .
595 'the context they are linked to must exist. The following categories ' .
596 'belong to a non-existant category:</p>' . $table . '<p>Any of these ' .
597 'categories that contain no questions can just be deleted form the database. ' .
598 'Other categories will require more thought.</p>';
599 }
600 function solution() {
601 global $CFG;
602 return '<p>You can delete the empty categories by executing the following SQL:</p><pre>
9f64eef7 603DELETE FROM ' . $CFG->prefix . 'question_categories
ff5328a2 604WHERE
9f64eef7 605 NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'question q WHERE q.category = ' . $CFG->prefix . 'question_categories.id)
606AND NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'context con WHERE contextid = con.id)
ff5328a2 607 </pre><p>Any remaining categories that contain questions will require more thought. ' .
608 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
609 }
610}
611
612class problem_000016 extends problem_base {
613 function title() {
614 return 'Question categories should belong to the same context as their parent';
615 }
616 function exists() {
617 global $DB;
618 return $DB->record_exists_sql("
619 SELECT parent_qc.id AS parent, child_qc.id AS child, child_qc.contextid
620 FROM {question_categories} child_qc
621 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
622 WHERE child_qc.contextid <> parent_qc.contextid");
623 }
624 function severity() {
625 return SEVERITY_ANNOYANCE;
626 }
627 function description() {
628 global $DB;
629 $problemcategories = $DB->get_records_sql("
630 SELECT
631 parent_qc.id AS parentid, parent_qc.name AS parentname, parent_qc.contextid AS parentcon,
632 child_qc.id AS childid, child_qc.name AS childname, child_qc.contextid AS childcon
633 FROM {question_categories} child_qc
634 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
635 WHERE child_qc.contextid <> parent_qc.contextid");
636 $table = '<table><thead><tr><th colspan="3">Child category</th><th colspan="3">Parent category</th></tr><tr>' .
637 '<th>Id</th><th>Name</th><th>Context id</th>' .
638 '<th>Id</th><th>Name</th><th>Context id</th>' .
639 "</tr></thead><tbody>\n";
640 foreach ($problemcategories as $cat) {
af2f92d0 641 $table .= "<tr><td>$cat->childid</td><td>" . s($cat->childname) .
642 "</td><td>$cat->childcon</td><td>$cat->parentid</td><td>" . s($cat->parentname) .
ff5328a2 643 "</td><td>$cat->parentcon</td></tr>\n";
644 }
645 $table .= '</tbody></table>';
646 return '<p>When one question category is the parent of another, then they ' .
647 'should both belong to the same context. This is not true for the following categories:</p>' .
648 $table;
649 }
650 function solution() {
651 return '<p>An automated solution is difficult. It depends whether the ' .
652 'parent or child category is in the wrong pace.' .
653 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
654 }
655}
527d9cdd 656
68f0aac8 657class problem_000017 extends problem_base {
658 function title() {
659 return 'Question categories tree structure';
660 }
661 function find_problems() {
662 global $DB;
663 static $answer = null;
664
665 if (is_null($answer)) {
666 $categories = $DB->get_records('question_categories', array(), 'id');
667
668 // Look for missing parents.
669 $missingparent = array();
670 foreach ($categories as $category) {
671 if ($category->parent != 0 && !array_key_exists($category->parent, $categories)) {
672 $missingparent[$category->id] = $category;
673 }
674 }
675
676 // Look for loops.
677 $loops = array();
678 while (!empty($categories)) {
679 $current = array_pop($categories);
680 $thisloop = array($current->id => $current);
681 while (true) {
682 if (isset($thisloop[$current->parent])) {
683 // Loop detected
684 $loops[$current->id] = $thisloop;
685 break;
686 } else if (!isset($categories[$current->parent])) {
687 // Got to the top level, or a category we already know is OK.
688 break;
689 } else {
690 // Continue following the path.
691 $current = $categories[$current->parent];
692 $thisloop[$current->id] = $current;
693 unset($categories[$current->id]);
694 }
695 }
696 }
697
698 $answer = array($missingparent, $loops);
699 }
700
701 return $answer;
702 }
703 function exists() {
704 list($missingparent, $loops) = $this->find_problems();
705 return !empty($missingparent) || !empty($loops);
706 }
707 function severity() {
708 return SEVERITY_ANNOYANCE;
709 }
710 function description() {
711 list($missingparent, $loops) = $this->find_problems();
712
713 $description = '<p>The question categories should be arranged into tree ' .
714 ' structures by the question_categories.parent field. Sometimes ' .
715 ' this tree structure gets messed up.</p>';
716
717 if (!empty($missingparent)) {
718 $description .= '<p>The following categories are missing their parents:</p><ul>';
719 foreach ($missingparent as $cat) {
720 $description .= "<li>Category $cat->id: " . s($cat->name) . "</li>\n";
721 }
722 $description .= "</ul>\n";
723 }
724
725 if (!empty($loops)) {
726 $description .= '<p>The following categories form a loop of parents:</p><ul>';
727 foreach ($loops as $loop) {
728 $description .= "<li><ul>\n";
729 foreach ($loop as $cat) {
730 $description .= "<li>Category $cat->id: " . s($cat->name) . " has parent $cat->parent</li>\n";
731 }
732 $description .= "</ul></li>\n";
733 }
734 $description .= "</ul>\n";
735 }
736
737 return $description;
738 }
739 function solution() {
740 global $CFG;
741 list($missingparent, $loops) = $this->find_problems();
742
743 $solution = '<p>Consider executing the following SQL queries. These fix ' .
744 'the problem by moving some categories to the top level.</p>';
745
746 if (!empty($missingparent)) {
747 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
748 " SET parent = 0\n" .
749 " WHERE id IN (" . implode(',', array_keys($missingparent)) . ");</pre>\n";
750 }
751
752 if (!empty($loops)) {
753 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
754 " SET parent = 0\n" .
755 " WHERE id IN (" . implode(',', array_keys($loops)) . ");</pre>\n";
756 }
757
758 return $solution;
759 }
760}
761
527d9cdd 762class problem_00000x extends problem_base {
b70b8fe1 763 function title() {
764 return '';
765 }
527d9cdd 766 function exists() {
767 return false;
768 }
769 function severity() {
770 return SEVERITY_SIGNIFICANT;
771 }
772 function description() {
773 return '';
774 }
775 function solution() {
776 global $CFG;
777 return '';
778 }
779}
780
781/*
782
783TODO:
784
785 session.save_path -- it doesn't really matter because we are already IN a session, right?
e25f766d 786 detect unsupported characters in $CFG->wwwroot - see bug Bug #6091 - relative vs absolute path during backup/restore process
48283ff6 787
527d9cdd 788*/
789
f578af59 790