MDL-24276 improved coding style
[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');
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);
341 //now lets change the memory limit to something unique below 128M==134217728
1ed8e660 342 if (empty($CFG->extramemorylimit)) {
7022dd39 343 raise_memory_limit('128M');
344 } else {
1ed8e660 345 raise_memory_limit($CFG->extramemorylimit);
7022dd39 346 }
8a91d7fc 347 $testmemlimit = get_real_size(@ini_get('memory_limit'));
348 //verify the change had any effect at all
349 if ($oldmemlimit == $testmemlimit) {
350 //memory limit can not be changed - is it big enough then?
351 if ($oldmemlimit < get_real_size('128M')) {
352 return true;
353 } else {
354 return false;
355 }
527d9cdd 356 }
7022dd39 357 reduce_memory_limit($oldmemlimit);
527d9cdd 358 return false;
359 }
360 function severity() {
8a91d7fc 361 return SEVERITY_NOTICE;
527d9cdd 362 }
363 function description() {
8a91d7fc 364 return 'The settings for PHP on your server do not allow a script to request more memory during its execution. '.
365 'This means that there is a hard limit of '.@ini_get('memory_limit').' for each script. '.
366 'It is possible that certain operations within Moodle will require more than this amount in order '.
367 'to complete successfully, especially if there are lots of data to be processed.';
527d9cdd 368 }
369 function solution() {
8a91d7fc 370 return 'It is recommended that you contact your web server administrator to address this issue.';
527d9cdd 371 }
372}
373
b70b8fe1 374class problem_000009 extends problem_base {
375 function title() {
376 return 'SQL: using account without password';
377 }
378 function exists() {
379 global $CFG;
380 return empty($CFG->dbpass);
381 }
382 function severity() {
383 return SEVERITY_CRITICAL;
384 }
385 function description() {
386 global $CFG;
387 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>');
388 }
389 function solution() {
390 global $CFG;
391 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.');
392 }
393}
c5b26d78 394/* // not implemented in 2.0 yet
39ba6d58 395class problem_000010 extends problem_base {
396 function title() {
397 return 'Uploaded files: slasharguments disabled or not working';
398 }
399 function exists() {
400 if (!$this->is_enabled()) {
eef868d1 401 return true;
39ba6d58 402 }
48283ff6 403 if ($this->status() < 1) {
39ba6d58 404 return true;
405 }
406 return false;
407 }
408 function severity() {
409 if ($this->is_enabled() and $this->status() == 0) {
410 return SEVERITY_SIGNIFICANT;
eef868d1 411 } else {
39ba6d58 412 return SEVERITY_ANNOYANCE;
413 }
414 }
415 function description() {
48283ff6 416 global $CFG;
39ba6d58 417 $desc = 'Slasharguments are needed for relative linking in uploaded resources:<ul>';
418 if (!$this->is_enabled()) {
eef868d1 419 $desc .= '<li>slasharguments are <strong>disabled</strong> in Moodle configuration</li>';
39ba6d58 420 } else {
eef868d1 421 $desc .= '<li>slasharguments are enabled in Moodle configuration</li>';
39ba6d58 422 }
48283ff6 423 if ($this->status() == -1) {
6544f8d8 424 $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 425 } else if ($this->status() == 0) {
39ba6d58 426 $desc .= '<li>slashargument test <strong>failed</strong>, please check server configuration</li>';
427 } else {
428 $desc .= '<li>slashargument test passed</li>';
429 }
430 $desc .= '</ul>';
431 return $desc;
432 }
433 function solution() {
48283ff6 434 global $CFG;
39ba6d58 435 $enabled = $this->is_enabled();
436 $status = $this->status();
437 $solution = '';
438 if ($enabled and ($status == 0)) {
439 $solution .= 'Slasharguments are enabled, but the test failed. Please disable slasharguments in Moodle configuration or fix the server configuration.<hr />';
440 } else if ((!$enabled) and ($status == 0)) {
441 $solution .= 'Slasharguments are disabled and the test failed. You may try to fix the server configuration.<hr />';
48283ff6 442 } else if ($enabled and ($status == -1)) {
443 $solution .= 'Slasharguments are enabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
444 } else if ((!$enabled) and ($status == -1)) {
445 $solution .= 'Slasharguments are disabled, <a href="'.$CFG->wwwroot.'/file.php/testslasharguments">automatic testing</a> not possible.<hr />';
39ba6d58 446 } else if ((!$enabled) and ($status > 0)) {
447 $solution .= 'Slasharguments are disabled though the iternal test is OK. You should enable slasharguments in Moodle configuration.';
448 } else if ($enabled and ($status > 0)) {
449 $solution .= 'Congratulations - everything seems OK now :-D';
450 }
48283ff6 451 if ($status < 1) {
eef868d1 452 $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>';
453 $solution .= '<p>Apache 1:<ul><li>try to add <code>cgi.fix_pathinfo=1</code> to php.ini</li></ul></p>';
454 $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 455 }
456 return $solution;
457 }
458 function is_enabled() {
459 global $CFG;
460 return !empty($CFG->slasharguments);
461 }
462 function status() {
463 global $CFG;
48283ff6 464 $handle = @fopen($CFG->wwwroot.'/file.php?file=/testslasharguments', "r");
9bbb40d6 465 $contents = @trim(fread($handle, 10));
48283ff6 466 @fclose($handle);
467 if ($contents != 'test -1') {
468 return -1;
469 }
470 $handle = @fopen($CFG->wwwroot.'/file.php/testslasharguments', "r");
9bbb40d6 471 $contents = trim(@fread($handle, 10));
39ba6d58 472 @fclose($handle);
473 switch ($contents) {
48283ff6 474 case 'test 1': return 1;
475 case 'test 2': return 2;
39ba6d58 476 default: return 0;
48283ff6 477 }
39ba6d58 478 }
c5b26d78 479}*/
39ba6d58 480
ff5328a2 481class problem_000012 extends problem_base {
482 function title() {
483 return 'Random questions data consistency';
484 }
485 function exists() {
486 global $DB;
487 return $DB->record_exists_select('question', "qtype = 'random' AND parent <> id", array());
488 }
489 function severity() {
490 return SEVERITY_ANNOYANCE;
491 }
492 function description() {
493 return '<p>For random questions, question.parent should equal question.id. ' .
494 'There are some questions in your database for which this is not true. ' .
495 'One way that this could have happened is for random questions restored from backup before ' .
496 '<a href="http://tracker.moodle.org/browse/MDL-5482">MDL-5482</a> was fixed.</p>';
497 }
498 function solution() {
499 global $CFG;
500 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the SQL</p>' .
501 '<pre>UPDATE ' . $CFG->prefix . 'question SET parent = id WHERE qtype = \'random\' and parent &lt;> id;</pre>';
502 }
503}
504
505class problem_000013 extends problem_base {
506 function title() {
507 return 'Multi-answer questions data consistency';
508 }
509 function exists() {
510 global $DB;
20207b82 511 $positionexpr = $DB->sql_position($DB->sql_concat("','", "q.id", "','"),
ff5328a2 512 $DB->sql_concat("','", "qma.sequence", "','"));
513 return $DB->record_exists_sql("
514 SELECT * FROM {question} q
515 JOIN {question_multianswer} qma ON $positionexpr > 0
516 WHERE qma.question <> q.parent") ||
517 $DB->record_exists_sql("
518 SELECT * FROM {question} q
519 JOIN {question} parent_q ON parent_q.id = q.parent
520 WHERE q.category <> parent_q.category");
521 }
522 function severity() {
523 return SEVERITY_ANNOYANCE;
524 }
525 function description() {
526 return '<p>For each sub-question whose id is listed in ' .
527 'question_multianswer.sequence, its question.parent field should equal ' .
528 'question_multianswer.question; and each sub-question should be in the same ' .
529 'category as its parent. There are questions in your database for ' .
530 'which this is not the case. One way that this could have happened is ' .
531 'for multi-answer questions restored from backup before ' .
532 '<a href="http://tracker.moodle.org/browse/MDL-14750">MDL-14750</a> was fixed.</p>';
533 }
534 function solution() {
535 return '<p>Upgrade to Moodle 1.9.1 or later, or manually execute the ' .
536 'code in question_multianswer_fix_subquestion_parents_and_categories in ' .
537 '<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' .
538 'from the 1.9 stable branch</a>.</p>';
539 }
540}
541
542class problem_000014 extends problem_base {
543 function title() {
544 return 'Only multianswer and random questions should be the parent of another question';
545 }
546 function exists() {
547 global $DB;
548 return $DB->record_exists_sql("
549 SELECT * FROM {question} q
550 JOIN {question} parent_q ON parent_q.id = q.parent
551 WHERE parent_q.qtype NOT IN ('random', 'multianswer')");
552 }
553 function severity() {
554 return SEVERITY_ANNOYANCE;
555 }
556 function description() {
557 return '<p>You have questions that violate this in your databse. ' .
558 'You will need to investigate to determine how this happened.</p>';
559 }
560 function solution() {
561 return '<p>It is impossible to give a solution without knowing more about ' .
562 ' how the problem was caused. You may be able to get help from the ' .
563 '<a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a>.</p>';
564 }
565}
566
567class problem_000015 extends problem_base {
568 function title() {
569 return 'Question categories should belong to a valid context';
570 }
571 function exists() {
572 global $DB;
573 return $DB->record_exists_sql("
574 SELECT qc.*, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
575 FROM {question_categories} qc
576 LEFT JOIN {context} con ON qc.contextid = con.id
577 WHERE con.id IS NULL");
578 }
579 function severity() {
580 return SEVERITY_ANNOYANCE;
581 }
582 function description() {
583 global $DB;
584 $problemcategories = $DB->get_records_sql("
585 SELECT qc.id, qc.name, qc.contextid, (SELECT COUNT(1) FROM {question} q WHERE q.category = qc.id) AS numquestions
586 FROM {question_categories} qc
587 LEFT JOIN {context} con ON qc.contextid = con.id
588 WHERE con.id IS NULL
589 ORDER BY numquestions DESC, qc.name");
590 $table = '<table><thead><tr><th>Cat id</th><th>Category name</th>' .
591 "<th>Context id</th><th>Num Questions</th></tr></thead><tbody>\n";
ff5328a2 592 foreach ($problemcategories as $cat) {
af2f92d0 593 $table .= "<tr><td>$cat->id</td><td>" . s($cat->name) . "</td><td>" .
ff5328a2 594 $cat->contextid ."</td><td>$cat->numquestions</td></tr>\n";
ff5328a2 595 }
596 $table .= '</tbody></table>';
597 return '<p>All question categories are linked to a context id, and, ' .
598 'the context they are linked to must exist. The following categories ' .
599 'belong to a non-existant category:</p>' . $table . '<p>Any of these ' .
600 'categories that contain no questions can just be deleted form the database. ' .
601 'Other categories will require more thought.</p>';
602 }
603 function solution() {
604 global $CFG;
605 return '<p>You can delete the empty categories by executing the following SQL:</p><pre>
9f64eef7 606DELETE FROM ' . $CFG->prefix . 'question_categories
ff5328a2 607WHERE
9f64eef7 608 NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'question q WHERE q.category = ' . $CFG->prefix . 'question_categories.id)
609AND NOT EXISTS (SELECT * FROM ' . $CFG->prefix . 'context con WHERE contextid = con.id)
ff5328a2 610 </pre><p>Any remaining categories that contain questions will require more thought. ' .
611 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
612 }
613}
614
615class problem_000016 extends problem_base {
616 function title() {
617 return 'Question categories should belong to the same context as their parent';
618 }
619 function exists() {
620 global $DB;
621 return $DB->record_exists_sql("
622 SELECT parent_qc.id AS parent, child_qc.id AS child, child_qc.contextid
623 FROM {question_categories} child_qc
624 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
625 WHERE child_qc.contextid <> parent_qc.contextid");
626 }
627 function severity() {
628 return SEVERITY_ANNOYANCE;
629 }
630 function description() {
631 global $DB;
632 $problemcategories = $DB->get_records_sql("
633 SELECT
634 parent_qc.id AS parentid, parent_qc.name AS parentname, parent_qc.contextid AS parentcon,
635 child_qc.id AS childid, child_qc.name AS childname, child_qc.contextid AS childcon
636 FROM {question_categories} child_qc
637 JOIN {question_categories} parent_qc ON child_qc.parent = parent_qc.id
638 WHERE child_qc.contextid <> parent_qc.contextid");
639 $table = '<table><thead><tr><th colspan="3">Child category</th><th colspan="3">Parent category</th></tr><tr>' .
640 '<th>Id</th><th>Name</th><th>Context id</th>' .
641 '<th>Id</th><th>Name</th><th>Context id</th>' .
642 "</tr></thead><tbody>\n";
643 foreach ($problemcategories as $cat) {
af2f92d0 644 $table .= "<tr><td>$cat->childid</td><td>" . s($cat->childname) .
645 "</td><td>$cat->childcon</td><td>$cat->parentid</td><td>" . s($cat->parentname) .
ff5328a2 646 "</td><td>$cat->parentcon</td></tr>\n";
647 }
648 $table .= '</tbody></table>';
649 return '<p>When one question category is the parent of another, then they ' .
650 'should both belong to the same context. This is not true for the following categories:</p>' .
651 $table;
652 }
653 function solution() {
654 return '<p>An automated solution is difficult. It depends whether the ' .
655 'parent or child category is in the wrong pace.' .
656 'People in the <a href="http://moodle.org/mod/forum/view.php?f=121">Quiz forum</a> may be able to help.</p>';
657 }
658}
527d9cdd 659
68f0aac8 660class problem_000017 extends problem_base {
661 function title() {
662 return 'Question categories tree structure';
663 }
664 function find_problems() {
665 global $DB;
666 static $answer = null;
667
668 if (is_null($answer)) {
669 $categories = $DB->get_records('question_categories', array(), 'id');
670
671 // Look for missing parents.
672 $missingparent = array();
673 foreach ($categories as $category) {
674 if ($category->parent != 0 && !array_key_exists($category->parent, $categories)) {
675 $missingparent[$category->id] = $category;
676 }
677 }
678
679 // Look for loops.
680 $loops = array();
681 while (!empty($categories)) {
682 $current = array_pop($categories);
683 $thisloop = array($current->id => $current);
684 while (true) {
685 if (isset($thisloop[$current->parent])) {
686 // Loop detected
687 $loops[$current->id] = $thisloop;
688 break;
689 } else if (!isset($categories[$current->parent])) {
690 // Got to the top level, or a category we already know is OK.
691 break;
692 } else {
693 // Continue following the path.
694 $current = $categories[$current->parent];
695 $thisloop[$current->id] = $current;
696 unset($categories[$current->id]);
697 }
698 }
699 }
700
701 $answer = array($missingparent, $loops);
702 }
703
704 return $answer;
705 }
706 function exists() {
707 list($missingparent, $loops) = $this->find_problems();
708 return !empty($missingparent) || !empty($loops);
709 }
710 function severity() {
711 return SEVERITY_ANNOYANCE;
712 }
713 function description() {
714 list($missingparent, $loops) = $this->find_problems();
715
716 $description = '<p>The question categories should be arranged into tree ' .
717 ' structures by the question_categories.parent field. Sometimes ' .
718 ' this tree structure gets messed up.</p>';
719
720 if (!empty($missingparent)) {
721 $description .= '<p>The following categories are missing their parents:</p><ul>';
722 foreach ($missingparent as $cat) {
723 $description .= "<li>Category $cat->id: " . s($cat->name) . "</li>\n";
724 }
725 $description .= "</ul>\n";
726 }
727
728 if (!empty($loops)) {
729 $description .= '<p>The following categories form a loop of parents:</p><ul>';
730 foreach ($loops as $loop) {
731 $description .= "<li><ul>\n";
732 foreach ($loop as $cat) {
733 $description .= "<li>Category $cat->id: " . s($cat->name) . " has parent $cat->parent</li>\n";
734 }
735 $description .= "</ul></li>\n";
736 }
737 $description .= "</ul>\n";
738 }
739
740 return $description;
741 }
742 function solution() {
743 global $CFG;
744 list($missingparent, $loops) = $this->find_problems();
745
746 $solution = '<p>Consider executing the following SQL queries. These fix ' .
747 'the problem by moving some categories to the top level.</p>';
748
749 if (!empty($missingparent)) {
750 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
751 " SET parent = 0\n" .
752 " WHERE id IN (" . implode(',', array_keys($missingparent)) . ");</pre>\n";
753 }
754
755 if (!empty($loops)) {
756 $solution .= "<pre>UPDATE " . $CFG->prefix . "question_categories\n" .
757 " SET parent = 0\n" .
758 " WHERE id IN (" . implode(',', array_keys($loops)) . ");</pre>\n";
759 }
760
761 return $solution;
762 }
763}
764
527d9cdd 765class problem_00000x extends problem_base {
b70b8fe1 766 function title() {
767 return '';
768 }
527d9cdd 769 function exists() {
770 return false;
771 }
772 function severity() {
773 return SEVERITY_SIGNIFICANT;
774 }
775 function description() {
776 return '';
777 }
778 function solution() {
779 global $CFG;
780 return '';
781 }
782}
783
784/*
785
786TODO:
787
788 session.save_path -- it doesn't really matter because we are already IN a session, right?
e25f766d 789 detect unsupported characters in $CFG->wwwroot - see bug Bug #6091 - relative vs absolute path during backup/restore process
48283ff6 790
527d9cdd 791*/
792
f578af59 793