MDL-15901 completion disabled by default
[moodle.git] / lib / db / upgradelib.php
CommitLineData
42ff9ce6 1<?php //$Id$
2
3/*
4 * This file is used for special upgrade functions - for example groups and gradebook.
56a1a882 5 * These functions must use SQL and database related functions only- no other Moodle API,
42ff9ce6 6 * because it might depend on db structures that are not yet present during upgrade.
7 * (Do not use functions from accesslib.php, grades classes or group functions at all!)
8 */
9
863850d2 10function upgrade_fix_category_depths() {
f33e1ed4 11 global $CFG, $DB;
863850d2 12
13 // first fix incorrect parents
14 $sql = "SELECT c.id
f33e1ed4 15 FROM {course_categories} c
16 WHERE c.parent > 0 AND c.parent NOT IN (SELECT pc.id FROM {course_categories} pc)";
17 if ($rs = $DB->get_recordset_sql($sql)) {
18 foreach ($rs as $cat) {
863850d2 19 $cat->depth = 1;
20 $cat->path = '/'.$cat->id;
21 $cat->parent = 0;
f33e1ed4 22 $DB->update_record('course_categories', $cat);
863850d2 23 }
f33e1ed4 24 $rs->close();
863850d2 25 }
26
27 // now add path and depth to top level categories
f33e1ed4 28 $sql = "UPDATE {course_categories}
245ac557 29 SET depth = 1, path = ".$DB->sql_concat("'/'", "id")."
863850d2 30 WHERE parent = 0";
f33e1ed4 31 $DB->execute($sql);
863850d2 32
33 // now fix all other levels - slow but works in all supported dbs
34 $parentdepth = 1;
f33e1ed4 35 while ($DB->record_exists('course_categories', array('depth'=>0))) {
863850d2 36 $sql = "SELECT c.id, pc.path
f33e1ed4 37 FROM {course_categories} c, {course_categories} pc
38 WHERE c.parent=pc.id AND c.depth=0 AND pc.depth=?";
39 if ($rs = $DB->get_recordset_sql($sql, array($parentdepth))) {
40 $DB->set_debug(false);
41 foreach ($rs as $cat) {
863850d2 42 $cat->depth = $parentdepth+1;
43 $cat->path = $cat->path.'/'.$cat->id;
f33e1ed4 44 $DB->update_record('course_categories', $cat);
863850d2 45 }
f33e1ed4 46 $rs->close();
47 $DB->set_debug(false);
863850d2 48 }
49 $parentdepth++;
50 if ($parentdepth > 100) {
51 //something must have gone wrong - nobody can have more than 100 levels of categories, right?
52 debugging('Unknown error fixing category depths');
53 break;
54 }
55 }
863850d2 56}
57
42ff9ce6 58?>