Automatically generated installer lang files
[moodle.git] / admin / health.php
blobe18a052c0590a6fa7ec47d5bc3d75363f660e415
1 <?php
3 ob_start(); //for whitespace test
4 require_once('../config.php');
6 // extra whitespace test - intentionally breaks cookieless mode
7 $extraws = '';
8 while (ob_get_level()) {
9 $extraws .= ob_get_contents();
10 ob_end_clean();
13 require_once($CFG->libdir.'/adminlib.php');
15 admin_externalpage_setup('healthcenter');
17 define('SEVERITY_NOTICE', 'notice');
18 define('SEVERITY_ANNOYANCE', 'annoyance');
19 define('SEVERITY_SIGNIFICANT', 'significant');
20 define('SEVERITY_CRITICAL', 'critical');
22 $solution = optional_param('solution', 0, PARAM_SAFEDIR); //in fact it is class name alhanumeric and _
24 require_login();
25 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
27 $site = get_site();
29 echo $OUTPUT->header();
31 echo <<<STYLES
32 <style type="text/css">
33 div#healthnoproblemsfound {
34 width: 60%;
35 margin: auto;
36 padding: 1em;
37 border: 1px black solid;
38 -moz-border-radius: 6px;
40 dl.healthissues {
41 width: 60%;
42 margin: auto;
44 dl.critical dt, dl.critical dd {
45 background-color: #a71501;
47 dl.significant dt, dl.significant dd {
48 background-color: #d36707;
50 dl.annoyance dt, dl.annoyance dd {
51 background-color: #dba707;
53 dl.notice dt, dl.notice dd {
54 background-color: #e5db36;
56 dt.solution, dd.solution, div#healthnoproblemsfound {
57 background-color: #5BB83E !important;
59 dl.healthissues dt, dl.healthissues dd {
60 margin: 0px;
61 padding: 1em;
62 border: 1px black solid;
64 dl.healthissues dt {
65 font-weight: bold;
66 border-bottom: none;
67 padding-bottom: 0.5em;
69 dl.healthissues dd {
70 border-top: none;
71 padding-top: 0.5em;
72 margin-bottom: 10px;
74 dl.healthissues dd form {
75 margin-top: 0.5em;
76 text-align: right;
78 form#healthformreturn {
79 text-align: center;
80 margin: 2em;
82 dd.solution p {
83 padding: 0px;
84 margin: 1em 0px;
86 dd.solution li {
87 margin-top: 1em;
90 </style>
91 STYLES;
93 if(strpos($solution, 'problem_') === 0 && class_exists($solution)) {
94 health_print_solution($solution);
96 else {
97 health_find_problems();
101 echo $OUTPUT->footer();
104 function health_find_problems() {
105 global $OUTPUT;
107 echo $OUTPUT->heading(get_string('healthcenter'));
109 $issues = array(
110 SEVERITY_CRITICAL => array(),
111 SEVERITY_SIGNIFICANT => array(),
112 SEVERITY_ANNOYANCE => array(),
113 SEVERITY_NOTICE => array(),
115 $problems = 0;
117 for($i = 1; $i < 1000000; ++$i) {
118 $classname = sprintf('problem_%06d', $i);
119 if(!class_exists($classname)) {
120 break;
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()
130 ++$problems;
132 unset($problem);
135 if($problems == 0) {
136 echo '<div id="healthnoproblemsfound">';
137 echo get_string('healthnoproblemsfound');
138 echo '</div>';
140 else {
141 echo $OUTPUT->heading(get_string('healthproblemsdetected'));
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>';
153 echo '</dl>';
159 function health_print_solution($classname) {
160 global $OUTPUT;
161 $problem = new $classname;
162 $data = array(
163 'title' => $problem->title(),
164 'severity' => $problem->severity(),
165 'description' => $problem->description(),
166 'solution' => $problem->solution()
169 echo $OUTPUT->heading(get_string('healthcenter'));
170 echo $OUTPUT->heading(get_string('healthproblemsolution'));
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>';
181 class problem_base {
182 function exists() {
183 return false;
185 function title() {
186 return '???';
188 function severity() {
189 return SEVERITY_NOTICE;
191 function description() {
192 return '';
194 function solution() {
195 return '';
199 class problem_000002 extends problem_base {
200 function title() {
201 return 'Extra characters at the end of config.php or other library function';
203 function exists() {
204 global $extraws;
206 if($extraws === '') {
207 return false;
209 return true;
211 function severity() {
212 return SEVERITY_SIGNIFICANT;
214 function description() {
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.';
217 function solution() {
218 global $CFG;
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.';
223 class problem_000003 extends problem_base {
224 function title() {
225 return '$CFG->dataroot does not exist or does not have write permissions';
227 function exists() {
228 global $CFG;
229 if(!is_dir($CFG->dataroot) || !is_writable($CFG->dataroot)) {
230 return true;
232 return false;
234 function severity() {
235 return SEVERITY_SIGNIFICANT;
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.';
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.';
247 class problem_000004 extends problem_base {
248 function title() {
249 return 'cron.php is not set up to run automatically';
251 function exists() {
252 global $DB;
253 $lastcron = $DB->get_field_sql('SELECT max(lastcron) FROM {modules}');
254 return (time() - $lastcron > 3600 * 24);
256 function severity() {
257 return SEVERITY_SIGNIFICANT;
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.';
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.';
268 class problem_000005 extends problem_base {
269 function title() {
270 return 'PHP: session.auto_start is enabled';
272 function exists() {
273 return ini_get_bool('session.auto_start');
275 function severity() {
276 return SEVERITY_CRITICAL;
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.';
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>';
287 class problem_000006 extends problem_base {
288 function title() {
289 return 'PHP: magic_quotes_runtime is enabled';
291 function exists() {
292 return (ini_get_bool('magic_quotes_runtime'));
294 function severity() {
295 return SEVERITY_SIGNIFICANT;
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.';
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>';
306 class problem_000007 extends problem_base {
307 function title() {
308 return 'PHP: file_uploads is disabled';
310 function exists() {
311 return !ini_get_bool('file_uploads');
313 function severity() {
314 return SEVERITY_SIGNIFICANT;
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.';
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>';
325 class problem_000008 extends problem_base {
326 function title() {
327 return 'PHP: memory_limit cannot be controlled by Moodle';
329 function exists() {
330 global $CFG;
332 $oldmemlimit = @ini_get('memory_limit');
333 if (empty($oldmemlimit)) {
334 // PHP not compiled with memory limits, this means that it's
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;
340 $oldmemlimit = get_real_size($oldmemlimit);
341 //now lets change the memory limit to something higher
342 $newmemlimit = ($oldmemlimit + 1024*1024*5);
343 raise_memory_limit($newmemlimit);
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;
354 reduce_memory_limit($oldmemlimit);
355 return false;
357 function severity() {
358 return SEVERITY_NOTICE;
360 function description() {
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.';
366 function solution() {
367 return 'It is recommended that you contact your web server administrator to address this issue.';
371 class problem_000009 extends problem_base {
372 function title() {
373 return 'SQL: using account without password';
375 function exists() {
376 global $CFG;
377 return empty($CFG->dbpass);
379 function severity() {
380 return SEVERITY_CRITICAL;
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>');
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.');
391 /* // not implemented in 2.0 yet
392 class problem_000010 extends problem_base {
393 function title() {
394 return 'Uploaded files: slasharguments disabled or not working';
396 function exists() {
397 if (!$this->is_enabled()) {
398 return true;
400 if ($this->status() < 1) {
401 return true;
403 return false;
405 function severity() {
406 if ($this->is_enabled() and $this->status() == 0) {
407 return SEVERITY_SIGNIFICANT;
408 } else {
409 return SEVERITY_ANNOYANCE;
412 function description() {
413 global $CFG;
414 $desc = 'Slasharguments are needed for relative linking in uploaded resources:<ul>';
415 if (!$this->is_enabled()) {
416 $desc .= '<li>slasharguments are <strong>disabled</strong> in Moodle configuration</li>';
417 } else {
418 $desc .= '<li>slasharguments are enabled in Moodle configuration</li>';
420 if ($this->status() == -1) {
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>';
422 } else if ($this->status() == 0) {
423 $desc .= '<li>slashargument test <strong>failed</strong>, please check server configuration</li>';
424 } else {
425 $desc .= '<li>slashargument test passed</li>';
427 $desc .= '</ul>';
428 return $desc;
430 function solution() {
431 global $CFG;
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 />';
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 />';
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';
448 if ($status < 1) {
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>';
453 return $solution;
455 function is_enabled() {
456 global $CFG;
457 return !empty($CFG->slasharguments);
459 function status() {
460 global $CFG;
461 $handle = @fopen($CFG->wwwroot.'/file.php?file=/testslasharguments', "r");
462 $contents = @trim(fread($handle, 10));
463 @fclose($handle);
464 if ($contents != 'test -1') {
465 return -1;
467 $handle = @fopen($CFG->wwwroot.'/file.php/testslasharguments', "r");
468 $contents = trim(@fread($handle, 10));
469 @fclose($handle);
470 switch ($contents) {
471 case 'test 1': return 1;
472 case 'test 2': return 2;
473 default: return 0;
478 class problem_000012 extends problem_base {
479 function title() {
480 return 'Random questions data consistency';
482 function exists() {
483 global $DB;
484 return $DB->record_exists_select('question', "qtype = 'random' AND parent <> id", array());
486 function severity() {
487 return SEVERITY_ANNOYANCE;
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>';
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>';
502 class problem_000013 extends problem_base {
503 function title() {
504 return 'Multi-answer questions data consistency';
506 function exists() {
507 global $DB;
508 $positionexpr = $DB->sql_position($DB->sql_concat("','", "q.id", "','"),
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");
519 function severity() {
520 return SEVERITY_ANNOYANCE;
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>';
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>';
539 class problem_000014 extends problem_base {
540 function title() {
541 return 'Only multianswer and random questions should be the parent of another question';
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')");
550 function severity() {
551 return SEVERITY_ANNOYANCE;
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>';
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>';
564 class problem_000015 extends problem_base {
565 function title() {
566 return 'Question categories should belong to a valid context';
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");
576 function severity() {
577 return SEVERITY_ANNOYANCE;
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";
589 foreach ($problemcategories as $cat) {
590 $table .= "<tr><td>$cat->id</td><td>" . s($cat->name) . "</td><td>" .
591 $cat->contextid ."</td><td>$cat->numquestions</td></tr>\n";
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>';
600 function solution() {
601 global $CFG;
602 return '<p>You can delete the empty categories by executing the following SQL:</p><pre>
603 DELETE FROM ' . $CFG->prefix . 'question_categories
604 WHERE
605 NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'question q WHERE q.category = ' . $CFG->prefix . 'question_categories.id)
606 AND NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'context con WHERE contextid = con.id)
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>';
612 class problem_000016 extends problem_base {
613 function title() {
614 return 'Question categories should belong to the same context as their parent';
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");
624 function severity() {
625 return SEVERITY_ANNOYANCE;
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) {
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) .
643 "</td><td>$cat->parentcon</td></tr>\n";
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;
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>';
657 class problem_000017 extends problem_base {
658 function title() {
659 return 'Question categories tree structure';
661 function find_problems() {
662 global $DB;
663 static $answer = null;
665 if (is_null($answer)) {
666 $categories = $DB->get_records('question_categories', array(), 'id');
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;
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]);
698 $answer = array($missingparent, $loops);
701 return $answer;
703 function exists() {
704 list($missingparent, $loops) = $this->find_problems();
705 return !empty($missingparent) || !empty($loops);
707 function severity() {
708 return SEVERITY_ANNOYANCE;
710 function description() {
711 list($missingparent, $loops) = $this->find_problems();
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>';
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";
722 $description .= "</ul>\n";
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";
732 $description .= "</ul></li>\n";
734 $description .= "</ul>\n";
737 return $description;
739 function solution() {
740 global $CFG;
741 list($missingparent, $loops) = $this->find_problems();
743 $solution = '<p>Consider executing the following SQL queries. These fix ' .
744 'the problem by moving some categories to the top level.</p>';
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";
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";
758 return $solution;
762 class problem_00000x extends problem_base {
763 function title() {
764 return '';
766 function exists() {
767 return false;
769 function severity() {
770 return SEVERITY_SIGNIFICANT;
772 function description() {
773 return '';
775 function solution() {
776 global $CFG;
777 return '';
783 TODO:
785 session.save_path -- it doesn't really matter because we are already IN a session, right?
786 detect unsupported characters in $CFG->wwwroot - see bug Bug #6091 - relative vs absolute path during backup/restore process