Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / admin / health.php
blob5ff49fba52853cd27759039646951d7d6eaf9e8e
1 <?php
3 ob_start(); //for whitespace test
4 require_once('../config.php');
5 $extraws = ob_get_clean();
7 require_once($CFG->libdir.'/adminlib.php');
9 admin_externalpage_setup('healthcenter');
11 define('SEVERITY_NOTICE', 'notice');
12 define('SEVERITY_ANNOYANCE', 'annoyance');
13 define('SEVERITY_SIGNIFICANT', 'significant');
14 define('SEVERITY_CRITICAL', 'critical');
16 $solution = optional_param('solution', 0, PARAM_SAFEDIR); //in fact it is class name alhanumeric and _
18 require_login();
19 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
21 $site = get_site();
23 echo $OUTPUT->header();
25 echo <<<STYLES
26 <style type="text/css">
27 div#healthnoproblemsfound {
28 width: 60%;
29 margin: auto;
30 padding: 1em;
31 border: 1px black solid;
32 -moz-border-radius: 6px;
34 dl.healthissues {
35 width: 60%;
36 margin: auto;
38 dl.critical dt, dl.critical dd {
39 background-color: #a71501;
41 dl.significant dt, dl.significant dd {
42 background-color: #d36707;
44 dl.annoyance dt, dl.annoyance dd {
45 background-color: #dba707;
47 dl.notice dt, dl.notice dd {
48 background-color: #e5db36;
50 dt.solution, dd.solution, div#healthnoproblemsfound {
51 background-color: #5BB83E !important;
53 dl.healthissues dt, dl.healthissues dd {
54 margin: 0px;
55 padding: 1em;
56 border: 1px black solid;
58 dl.healthissues dt {
59 font-weight: bold;
60 border-bottom: none;
61 padding-bottom: 0.5em;
63 dl.healthissues dd {
64 border-top: none;
65 padding-top: 0.5em;
66 margin-bottom: 10px;
68 dl.healthissues dd form {
69 margin-top: 0.5em;
70 text-align: right;
72 form#healthformreturn {
73 text-align: center;
74 margin: 2em;
76 dd.solution p {
77 padding: 0px;
78 margin: 1em 0px;
80 dd.solution li {
81 margin-top: 1em;
84 </style>
85 STYLES;
87 if(strpos($solution, 'problem_') === 0 && class_exists($solution)) {
88 health_print_solution($solution);
90 else {
91 health_find_problems();
95 echo $OUTPUT->footer();
98 function health_find_problems() {
99 global $OUTPUT;
101 echo $OUTPUT->heading(get_string('healthcenter'));
103 $issues = array(
104 SEVERITY_CRITICAL => array(),
105 SEVERITY_SIGNIFICANT => array(),
106 SEVERITY_ANNOYANCE => array(),
107 SEVERITY_NOTICE => array(),
109 $problems = 0;
111 for($i = 1; $i < 1000000; ++$i) {
112 $classname = sprintf('problem_%06d', $i);
113 if(!class_exists($classname)) {
114 break;
116 $problem = new $classname;
117 if($problem->exists()) {
118 $severity = $problem->severity();
119 $issues[$severity][$classname] = array(
120 'severity' => $severity,
121 'description' => $problem->description(),
122 'title' => $problem->title()
124 ++$problems;
126 unset($problem);
129 if($problems == 0) {
130 echo '<div id="healthnoproblemsfound">';
131 echo get_string('healthnoproblemsfound');
132 echo '</div>';
134 else {
135 echo $OUTPUT->heading(get_string('healthproblemsdetected'));
136 $severities = array(SEVERITY_CRITICAL, SEVERITY_SIGNIFICANT, SEVERITY_ANNOYANCE, SEVERITY_NOTICE);
137 foreach($severities as $severity) {
138 if(!empty($issues[$severity])) {
139 echo '<dl class="healthissues '.$severity.'">';
140 foreach($issues[$severity] as $classname => $data) {
141 echo '<dt id="'.$classname.'">'.$data['title'].'</dt>';
142 echo '<dd>'.$data['description'];
143 echo '<form action="health.php#solution" method="get">';
144 echo '<input type="hidden" name="solution" value="'.$classname.'" /><input type="submit" value="'.get_string('viewsolution').'" />';
145 echo '</form></dd>';
147 echo '</dl>';
153 function health_print_solution($classname) {
154 global $OUTPUT;
155 $problem = new $classname;
156 $data = array(
157 'title' => $problem->title(),
158 'severity' => $problem->severity(),
159 'description' => $problem->description(),
160 'solution' => $problem->solution()
163 echo $OUTPUT->heading(get_string('healthcenter'));
164 echo $OUTPUT->heading(get_string('healthproblemsolution'));
165 echo '<dl class="healthissues '.$data['severity'].'">';
166 echo '<dt>'.$data['title'].'</dt>';
167 echo '<dd>'.$data['description'].'</dd>';
168 echo '<dt id="solution" class="solution">'.get_string('healthsolution').'</dt>';
169 echo '<dd class="solution">'.$data['solution'].'</dd></dl>';
170 echo '<form id="healthformreturn" action="health.php#'.$classname.'" method="get">';
171 echo '<input type="submit" value="'.get_string('healthreturntomain').'" />';
172 echo '</form>';
175 class problem_base {
176 function exists() {
177 return false;
179 function title() {
180 return '???';
182 function severity() {
183 return SEVERITY_NOTICE;
185 function description() {
186 return '';
188 function solution() {
189 return '';
193 class problem_000002 extends problem_base {
194 function title() {
195 return 'Extra characters at the end of config.php or other library function';
197 function exists() {
198 global $extraws;
200 if($extraws === '') {
201 return false;
203 return true;
205 function severity() {
206 return SEVERITY_SIGNIFICANT;
208 function description() {
209 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.';
211 function solution() {
212 global $CFG;
213 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.';
217 class problem_000003 extends problem_base {
218 function title() {
219 return '$CFG->dataroot does not exist or does not have write permissions';
221 function exists() {
222 global $CFG;
223 if(!is_dir($CFG->dataroot) || !is_writable($CFG->dataroot)) {
224 return true;
226 return false;
228 function severity() {
229 return SEVERITY_SIGNIFICANT;
231 function description() {
232 global $CFG;
233 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.';
235 function solution() {
236 global $CFG;
237 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.';
241 class problem_000004 extends problem_base {
242 function title() {
243 return 'cron.php is not set up to run automatically';
245 function exists() {
246 global $DB;
247 $lastcron = $DB->get_field_sql('SELECT max(lastcron) FROM {modules}');
248 return (time() - $lastcron > 3600 * 24);
250 function severity() {
251 return SEVERITY_SIGNIFICANT;
253 function description() {
254 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.';
256 function solution() {
257 global $CFG;
258 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.';
262 class problem_000005 extends problem_base {
263 function title() {
264 return 'PHP: session.auto_start is enabled';
266 function exists() {
267 return ini_get_bool('session.auto_start');
269 function severity() {
270 return SEVERITY_CRITICAL;
272 function description() {
273 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.';
275 function solution() {
276 global $CFG;
277 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>';
281 class problem_000006 extends problem_base {
282 function title() {
283 return 'PHP: magic_quotes_runtime is enabled';
285 function exists() {
286 return (ini_get_bool('magic_quotes_runtime'));
288 function severity() {
289 return SEVERITY_SIGNIFICANT;
291 function description() {
292 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.';
294 function solution() {
295 global $CFG;
296 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>';
300 class problem_000007 extends problem_base {
301 function title() {
302 return 'PHP: file_uploads is disabled';
304 function exists() {
305 return !ini_get_bool('file_uploads');
307 function severity() {
308 return SEVERITY_SIGNIFICANT;
310 function description() {
311 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.';
313 function solution() {
314 global $CFG;
315 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>';
319 class problem_000008 extends problem_base {
320 function title() {
321 return 'PHP: memory_limit cannot be controlled by Moodle';
323 function exists() {
324 global $CFG;
326 $oldmemlimit = @ini_get('memory_limit');
327 if (empty($oldmemlimit)) {
328 // PHP not compiled with memory limits, this means that it's
329 // probably limited to 8M or in case of Windows not at all.
330 // We can ignore it for now - there is not much to test anyway
331 // TODO: add manual test that fills memory??
332 return false;
334 $oldmemlimit = get_real_size($oldmemlimit);
335 //now lets change the memory limit to something higher
336 $newmemlimit = ($oldmemlimit + 1024*1024*5);
337 raise_memory_limit($newmemlimit);
338 $testmemlimit = get_real_size(@ini_get('memory_limit'));
339 //verify the change had any effect at all
340 if ($oldmemlimit == $testmemlimit) {
341 //memory limit can not be changed - is it big enough then?
342 if ($oldmemlimit < get_real_size('128M')) {
343 return true;
344 } else {
345 return false;
348 reduce_memory_limit($oldmemlimit);
349 return false;
351 function severity() {
352 return SEVERITY_NOTICE;
354 function description() {
355 return 'The settings for PHP on your server do not allow a script to request more memory during its execution. '.
356 'This means that there is a hard limit of '.@ini_get('memory_limit').' for each script. '.
357 'It is possible that certain operations within Moodle will require more than this amount in order '.
358 'to complete successfully, especially if there are lots of data to be processed.';
360 function solution() {
361 return 'It is recommended that you contact your web server administrator to address this issue.';
365 class problem_000009 extends problem_base {
366 function title() {
367 return 'SQL: using account without password';
369 function exists() {
370 global $CFG;
371 return empty($CFG->dbpass);
373 function severity() {
374 return SEVERITY_CRITICAL;
376 function description() {
377 global $CFG;
378 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>');
380 function solution() {
381 global $CFG;
382 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.');
385 /* // not implemented in 2.0 yet
386 class problem_000010 extends problem_base {
387 function title() {
388 return 'Uploaded files: slasharguments disabled or not working';
390 function exists() {
391 if (!$this->is_enabled()) {
392 return true;
394 if ($this->status() < 1) {
395 return true;
397 return false;
399 function severity() {
400 if ($this->is_enabled() and $this->status() == 0) {
401 return SEVERITY_SIGNIFICANT;
402 } else {
403 return SEVERITY_ANNOYANCE;
406 function description() {
407 global $CFG;
408 $desc = 'Slasharguments are needed for relative linking in uploaded resources:<ul>';
409 if (!$this->is_enabled()) {
410 $desc .= '<li>slasharguments are <strong>disabled</strong> in Moodle configuration</li>';
411 } else {
412 $desc .= '<li>slasharguments are enabled in Moodle configuration</li>';
414 if ($this->status() == -1) {
415 $desc .= '<li>can not run automatic test, you can verify it <a href="'.$CFG->wwwroot.'/file.php/testslasharguments" target="_blank">here</a> manually</li>';
416 } else if ($this->status() == 0) {
417 $desc .= '<li>slashargument test <strong>failed</strong>, please check server configuration</li>';
418 } else {
419 $desc .= '<li>slashargument test passed</li>';
421 $desc .= '</ul>';
422 return $desc;
424 function solution() {
425 global $CFG;
426 $enabled = $this->is_enabled();
427 $status = $this->status();
428 $solution = '';
429 if ($enabled and ($status == 0)) {
430 $solution .= 'Slasharguments are enabled, but the test failed. Please disable slasharguments in Moodle configuration or fix the server configuration.<hr />';
431 } else if ((!$enabled) and ($status == 0)) {
432 $solution .= 'Slasharguments are disabled and the test failed. You may try to fix the server configuration.<hr />';
433 } else if ($enabled and ($status == -1)) {
434 $solution .= 'Slasharguments are enabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
435 } else if ((!$enabled) and ($status == -1)) {
436 $solution .= 'Slasharguments are disabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
437 } else if ((!$enabled) and ($status > 0)) {
438 $solution .= 'Slasharguments are disabled though the iternal test is OK. You should enable slasharguments in Moodle configuration.';
439 } else if ($enabled and ($status > 0)) {
440 $solution .= 'Congratulations - everything seems OK now :-D';
442 if ($status < 1) {
443 $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>';
444 $solution .= '<p>Apache 1:<ul><li>try to add <code>cgi.fix_pathinfo=1</code> to php.ini</li></ul></p>';
445 $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>';
447 return $solution;
449 function is_enabled() {
450 global $CFG;
451 return !empty($CFG->slasharguments);
453 function status() {
454 global $CFG;
455 $handle = @fopen($CFG->wwwroot.'/file.php?file=/testslasharguments', "r");
456 $contents = @trim(fread($handle, 10));
457 @fclose($handle);
458 if ($contents != 'test -1') {
459 return -1;
461 $handle = @fopen($CFG->wwwroot.'/file.php/testslasharguments', "r");
462 $contents = trim(@fread($handle, 10));
463 @fclose($handle);
464 switch ($contents) {
465 case 'test 1': return 1;
466 case 'test 2': return 2;
467 default: return 0;
472 class problem_000012 extends problem_base {
473 function title() {
474 return 'Random questions data consistency';
476 function exists() {
477 global $DB;
478 return $DB->record_exists_select('question', "qtype = 'random' AND parent <> id", array());
480 function severity() {
481 return SEVERITY_ANNOYANCE;
483 function description() {
484 return '<p>For random questions, question.parent should equal question.id. ' .
485 'There are some questions in your database for which this is not true. ' .
486 'One way that this could have happened is for random questions restored from backup before ' .
487 '<a href="http://tracker.moodle.org/browse/MDL-5482">MDL-5482</a> was fixed.</p>';
489 function solution() {
490 global $CFG;
491 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the SQL</p>' .
492 '<pre>UPDATE ' . $CFG->prefix . 'question SET parent = id WHERE qtype = \'random\' and parent &lt;> id;</pre>';
496 class problem_000013 extends problem_base {
497 function title() {
498 return 'Multi-answer questions data consistency';
500 function exists() {
501 global $DB;
502 $positionexpr = $DB->sql_position($DB->sql_concat("','", "q.id", "','"),
503 $DB->sql_concat("','", "qma.sequence", "','"));
504 return $DB->record_exists_sql("
505 SELECT * FROM {question} q
506 JOIN {question_multianswer} qma ON $positionexpr > 0
507 WHERE qma.question <> q.parent") ||
508 $DB->record_exists_sql("
509 SELECT * FROM {question} q
510 JOIN {question} parent_q ON parent_q.id = q.parent
511 WHERE q.category <> parent_q.category");
513 function severity() {
514 return SEVERITY_ANNOYANCE;
516 function description() {
517 return '<p>For each sub-question whose id is listed in ' .
518 'question_multianswer.sequence, its question.parent field should equal ' .
519 'question_multianswer.question; and each sub-question should be in the same ' .
520 'category as its parent. There are questions in your database for ' .
521 'which this is not the case. One way that this could have happened is ' .
522 'for multi-answer questions restored from backup before ' .
523 '<a href="http://tracker.moodle.org/browse/MDL-14750">MDL-14750</a> was fixed.</p>';
525 function solution() {
526 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the ' .
527 'code in question_multianswer_fix_subquestion_parents_and_categories in ' .
528 '<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' .
529 'from the 1.9 stable branch</a>.</p>';
533 class problem_000014 extends problem_base {
534 function title() {
535 return 'Only multianswer and random questions should be the parent of another question';
537 function exists() {
538 global $DB;
539 return $DB->record_exists_sql("
540 SELECT * FROM {question} q
541 JOIN {question} parent_q ON parent_q.id = q.parent
542 WHERE parent_q.qtype NOT IN ('random', 'multianswer')");
544 function severity() {
545 return SEVERITY_ANNOYANCE;
547 function description() {
548 return '<p>You have questions that violate this in your databse. ' .
549 'You will need to investigate to determine how this happened.</p>';
551 function solution() {
552 return '<p>It is impossible to give a solution without knowing more about ' .
553 ' how the problem was caused. You may be able to get help from the ' .
554 '<a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a>.</p>';
558 class problem_000015 extends problem_base {
559 function title() {
560 return 'Question categories should belong to a valid context';
562 function exists() {
563 global $DB;
564 return $DB->record_exists_sql("
565 SELECT qc.*, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
566 FROM {question_categories} qc
567 LEFT JOIN {context} con ON qc.contextid = con.id
568 WHERE con.id IS NULL");
570 function severity() {
571 return SEVERITY_ANNOYANCE;
573 function description() {
574 global $DB;
575 $problemcategories = $DB->get_records_sql("
576 SELECT qc.id, qc.name, qc.contextid, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
577 FROM {question_categories} qc
578 LEFT JOIN {context} con ON qc.contextid = con.id
579 WHERE con.id IS NULL
580 ORDER BY numquestions DESC, qc.name");
581 $table = '<table><thead><tr><th>Cat id</th><th>Category name</th>' .
582 "<th>Context id</th><th>Num Questions</th></tr></thead><tbody>\n";
583 foreach ($problemcategories as $cat) {
584 $table .= "<tr><td>$cat->id</td><td>" . s($cat->name) . "</td><td>" .
585 $cat->contextid ."</td><td>$cat->numquestions</td></tr>\n";
587 $table .= '</tbody></table>';
588 return '<p>All question categories are linked to a context id, and, ' .
589 'the context they are linked to must exist. The following categories ' .
590 'belong to a non-existant category:</p>' . $table . '<p>Any of these ' .
591 'categories that contain no questions can just be deleted form the database. ' .
592 'Other categories will require more thought.</p>';
594 function solution() {
595 global $CFG;
596 return '<p>You can delete the empty categories by executing the following SQL:</p><pre>
597 DELETE FROM ' . $CFG->prefix . 'question_categories
598 WHERE
599 NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'question q WHERE q.category = ' . $CFG->prefix . 'question_categories.id)
600 AND NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'context con WHERE contextid = con.id)
601 </pre><p>Any remaining categories that contain questions will require more thought. ' .
602 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
606 class problem_000016 extends problem_base {
607 function title() {
608 return 'Question categories should belong to the same context as their parent';
610 function exists() {
611 global $DB;
612 return $DB->record_exists_sql("
613 SELECT parent_qc.id AS parent, child_qc.id AS child, child_qc.contextid
614 FROM {question_categories} child_qc
615 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
616 WHERE child_qc.contextid <> parent_qc.contextid");
618 function severity() {
619 return SEVERITY_ANNOYANCE;
621 function description() {
622 global $DB;
623 $problemcategories = $DB->get_records_sql("
624 SELECT
625 parent_qc.id AS parentid, parent_qc.name AS parentname, parent_qc.contextid AS parentcon,
626 child_qc.id AS childid, child_qc.name AS childname, child_qc.contextid AS childcon
627 FROM {question_categories} child_qc
628 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
629 WHERE child_qc.contextid <> parent_qc.contextid");
630 $table = '<table><thead><tr><th colspan="3">Child category</th><th colspan="3">Parent category</th></tr><tr>' .
631 '<th>Id</th><th>Name</th><th>Context id</th>' .
632 '<th>Id</th><th>Name</th><th>Context id</th>' .
633 "</tr></thead><tbody>\n";
634 foreach ($problemcategories as $cat) {
635 $table .= "<tr><td>$cat->childid</td><td>" . s($cat->childname) .
636 "</td><td>$cat->childcon</td><td>$cat->parentid</td><td>" . s($cat->parentname) .
637 "</td><td>$cat->parentcon</td></tr>\n";
639 $table .= '</tbody></table>';
640 return '<p>When one question category is the parent of another, then they ' .
641 'should both belong to the same context. This is not true for the following categories:</p>' .
642 $table;
644 function solution() {
645 return '<p>An automated solution is difficult. It depends whether the ' .
646 'parent or child category is in the wrong pace.' .
647 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
651 class problem_000017 extends problem_base {
652 function title() {
653 return 'Question categories tree structure';
655 function find_problems() {
656 global $DB;
657 static $answer = null;
659 if (is_null($answer)) {
660 $categories = $DB->get_records('question_categories', array(), 'id');
662 // Look for missing parents.
663 $missingparent = array();
664 foreach ($categories as $category) {
665 if ($category->parent != 0 && !array_key_exists($category->parent, $categories)) {
666 $missingparent[$category->id] = $category;
670 // Look for loops.
671 $loops = array();
672 while (!empty($categories)) {
673 $current = array_pop($categories);
674 $thisloop = array($current->id => $current);
675 while (true) {
676 if (isset($thisloop[$current->parent])) {
677 // Loop detected
678 $loops[$current->id] = $thisloop;
679 break;
680 } else if (!isset($categories[$current->parent])) {
681 // Got to the top level, or a category we already know is OK.
682 break;
683 } else {
684 // Continue following the path.
685 $current = $categories[$current->parent];
686 $thisloop[$current->id] = $current;
687 unset($categories[$current->id]);
692 $answer = array($missingparent, $loops);
695 return $answer;
697 function exists() {
698 list($missingparent, $loops) = $this->find_problems();
699 return !empty($missingparent) || !empty($loops);
701 function severity() {
702 return SEVERITY_ANNOYANCE;
704 function description() {
705 list($missingparent, $loops) = $this->find_problems();
707 $description = '<p>The question categories should be arranged into tree ' .
708 ' structures by the question_categories.parent field. Sometimes ' .
709 ' this tree structure gets messed up.</p>';
711 if (!empty($missingparent)) {
712 $description .= '<p>The following categories are missing their parents:</p><ul>';
713 foreach ($missingparent as $cat) {
714 $description .= "<li>Category $cat->id: " . s($cat->name) . "</li>\n";
716 $description .= "</ul>\n";
719 if (!empty($loops)) {
720 $description .= '<p>The following categories form a loop of parents:</p><ul>';
721 foreach ($loops as $loop) {
722 $description .= "<li><ul>\n";
723 foreach ($loop as $cat) {
724 $description .= "<li>Category $cat->id: " . s($cat->name) . " has parent $cat->parent</li>\n";
726 $description .= "</ul></li>\n";
728 $description .= "</ul>\n";
731 return $description;
733 function solution() {
734 global $CFG;
735 list($missingparent, $loops) = $this->find_problems();
737 $solution = '<p>Consider executing the following SQL queries. These fix ' .
738 'the problem by moving some categories to the top level.</p>';
740 if (!empty($missingparent)) {
741 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
742 " SET parent = 0\n" .
743 " WHERE id IN (" . implode(',', array_keys($missingparent)) . ");</pre>\n";
746 if (!empty($loops)) {
747 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
748 " SET parent = 0\n" .
749 " WHERE id IN (" . implode(',', array_keys($loops)) . ");</pre>\n";
752 return $solution;
756 class problem_00000x extends problem_base {
757 function title() {
758 return '';
760 function exists() {
761 return false;
763 function severity() {
764 return SEVERITY_SIGNIFICANT;
766 function description() {
767 return '';
769 function solution() {
770 global $CFG;
771 return '';
777 TODO:
779 session.save_path -- it doesn't really matter because we are already IN a session, right?
780 detect unsupported characters in $CFG->wwwroot - see bug Bug #6091 - relative vs absolute path during backup/restore process