Fixed missing 'throw' keyword
[moodle.git] / db / upgrade.php
CommitLineData
28d16c16 1<?php
e6e946c1 2// This file is part of Book module for Moodle - http://moodle.org/
5db9c35d 3//
0f0ba611
PS
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
5db9c35d 8//
0f0ba611
PS
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
5db9c35d 13//
0f0ba611
PS
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
5db9c35d 16
0f0ba611
PS
17/**
18 * Book module upgrade code
19 *
20 * @package mod
21 * @subpackage book
5dc5d035 22 * @copyright 2009-2011 Petr Skoda {@link http://skodak.org}
0f0ba611
PS
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
5db9c35d 25
0f0ba611 26defined('MOODLE_INTERNAL') || die;
5db9c35d 27
0f0ba611
PS
28function xmldb_book_upgrade($oldversion) {
29 global $CFG, $DB;
5db9c35d 30
0f0ba611 31 $dbman = $DB->get_manager();
5db9c35d 32
ffe9d57f 33 if ($oldversion < 2004081100) {
33b8e0f0 34 throw new upgrade_exception('mod_book', $oldversion, 'Can not upgrade such an old book module, sorry, you should have upgraded it long time ago in 1.9 already.');
ffe9d57f
PS
35 }
36
0f0ba611 37 if ($oldversion < 2007052001) {
7bf645e7
PS
38
39 /// Changing type of field importsrc on table book_chapters to char
0f0ba611
PS
40 $table = new xmldb_table('book_chapters');
41 $field = new xmldb_field('importsrc', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'timemodified');
7bf645e7
PS
42
43 /// Launch change of type for field importsrc
0f0ba611
PS
44 $dbman->change_field_type($table, $field);
45
46 upgrade_mod_savepoint(true, 2007052001, 'book');
47 }
48
49//===== 1.9.0 upgrade line ======//
50
51 if ($oldversion < 2010120801) {
52 // Rename field summary on table book to intro
53 $table = new xmldb_table('book');
54 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'name');
55
56 // Launch rename field summary
58aa4b57
PS
57 if ($dbman->field_exists($table, $field)) {
58 $dbman->rename_field($table, $field, 'intro');
59 }
0f0ba611
PS
60
61 // book savepoint reached
62 upgrade_mod_savepoint(true, 2010120801, 'book');
7bf645e7 63 }
5db9c35d 64
0f0ba611 65 if ($oldversion < 2010120802) {
bd4eba67
PS
66 // Rename field summary on table book to intro
67 $table = new xmldb_table('book');
be28c4b5 68 $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'name');
bd4eba67
PS
69
70 // Launch rename field summary
71 $dbman->change_field_precision($table, $field);
72
73 // book savepoint reached
74 upgrade_mod_savepoint(true, 2010120802, 'book');
75 }
76
77 if ($oldversion < 2010120803) {
0f0ba611
PS
78 // Define field introformat to be added to book
79 $table = new xmldb_table('book');
80 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
81
82 // Launch add field introformat
83 if (!$dbman->field_exists($table, $field)) {
84 $dbman->add_field($table, $field);
85 }
86
2c1e98e6 87 $DB->set_field('book', 'introformat', FORMAT_HTML, array());
0f0ba611
PS
88
89 // book savepoint reached
bd4eba67 90 upgrade_mod_savepoint(true, 2010120803, 'book');
0f0ba611
PS
91 }
92
bd4eba67
PS
93 if ($oldversion < 2010120804) {
94 // Define field introformat to be added to book
95 $table = new xmldb_table('book_chapters');
96 $field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'content');
97
98 // Launch add field introformat
99 if (!$dbman->field_exists($table, $field)) {
100 $dbman->add_field($table, $field);
101 }
102
103 $DB->set_field('book_chapters', 'contentformat', FORMAT_HTML, array());
104
105 // book savepoint reached
106 upgrade_mod_savepoint(true, 2010120804, 'book');
107 }
108
109 if ($oldversion < 2010120805) {
e6e946c1
PS
110 require_once("$CFG->dirroot/mod/book/db/upgradelib.php");
111
112 $sqlfrom = "FROM {book} b
113 JOIN {modules} m ON m.name = 'book'
114 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = b.id)";
115
116 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
117
118 if ($rs = $DB->get_recordset_sql("SELECT b.id, b.course, cm.id AS cmid $sqlfrom ORDER BY b.course, b.id")) {
119
120 $pbar = new progress_bar('migratebookfiles', 500, true);
121
122 $i = 0;
123 foreach ($rs as $book) {
124 $i++;
125 upgrade_set_timeout(360); // set up timeout, may also abort execution
126 $pbar->update($i, $count, "Migrating book files - $i/$count.");
127
128 $context = get_context_instance(CONTEXT_MODULE, $book->cmid);
129
130 book_migrate_moddata_dir_to_legacy($book, $context, '/');
131
132 // remove dirs if empty
133 @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/$book->id/");
134 @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/");
135 @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/");
8708eb63 136 @rmdir("$CFG->dataroot/$book->course/");
e6e946c1
PS
137 }
138 $rs->close();
139 }
140
bd4eba67
PS
141 // book savepoint reached
142 upgrade_mod_savepoint(true, 2010120805, 'book');
143 }
73861c80 144
5dc5d035
PS
145 if ($oldversion < 2011011600) {
146 // Define field disableprinting to be dropped from book
147 $table = new xmldb_table('book');
148 $field = new xmldb_field('disableprinting');
149
150 // Conditionally launch drop field disableprinting
151 if ($dbman->field_exists($table, $field)) {
152 $dbman->drop_field($table, $field);
153 }
154
155 // book savepoint reached
156 upgrade_mod_savepoint(true, 2011011600, 'book');
157 }
73861c80 158
ad1c11bd
PS
159 if ($oldversion < 2011011601) {
160 unset_config('book_tocwidth');
161
162 // book savepoint reached
163 upgrade_mod_savepoint(true, 2011011601, 'book');
164 }
165
73861c80 166
bd4eba67 167 //TODO: migrate the legacy file.php links to new pluginfile.php and file areas per chapter
e6e946c1 168
0f0ba611
PS
169
170 return true;
5db9c35d 171}