MDL-35297 book: replay some steps lost when book landed to core
[moodle.git] / mod / book / db / upgrade.php
CommitLineData
28d16c16 1<?php
b15ef0b0 2// This file is part of 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/>.
0f0ba611
PS
16/**
17 * Book module upgrade code
18 *
3542c4f4
EL
19 * @package mod_book
20 * @copyright 2009-2011 Petr Skoda {@link http://skodak.org}
0f0ba611
PS
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
5db9c35d 23
0f0ba611 24defined('MOODLE_INTERNAL') || die;
5db9c35d 25
3542c4f4
EL
26/**
27 * Book module upgrade task
28 *
29 * @param int $oldversion the version we are upgrading from
30 * @return bool always true
31 */
0f0ba611
PS
32function xmldb_book_upgrade($oldversion) {
33 global $CFG, $DB;
5db9c35d 34
0f0ba611 35 $dbman = $DB->get_manager();
5db9c35d 36
b9a36354
EL
37 // Moodle v2.2.0 release upgrade line
38 // Put any upgrade step following this
0f0ba611 39
b574c078
AB
40 // Moodle v2.3.0 release upgrade line
41 // Put any upgrade step following this
42
8be3d5ab
EL
43 // Note: The next steps (up to 2012061710 included, are a "replay" of old upgrade steps,
44 // because some sites updated to Moodle 2.3 didn't have the latest contrib mod_book
45 // installed, so some required changes were missing.
46 //
47 // All the steps are run conditionally so sites upgraded from latest contrib mod_book or
48 // new (2.3 and upwards) sites won't get affected.
49 //
50 // See MDL-35297 and commit msg for more information.
51
52 if ($oldversion < 2012061703) {
53 // Rename field summary on table book to intro
54 $table = new xmldb_table('book');
55 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
56
57 // Launch rename field summary
58 if ($dbman->field_exists($table, $field)) {
59 $dbman->rename_field($table, $field, 'intro');
60 }
61
62 // book savepoint reached
63 upgrade_mod_savepoint(true, 2012061703, 'book');
64 }
65
66 if ($oldversion < 2012061704) {
67 // Define field introformat to be added to book
68 $table = new xmldb_table('book');
69 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');
70
71 // Launch add field introformat
72 if (!$dbman->field_exists($table, $field)) {
73 $dbman->add_field($table, $field);
74 // Conditionally migrate to html format in intro
75 // Si está activo el htmleditor!!!!!
76 if ($CFG->texteditors !== 'textarea') {
77 $rs = $DB->get_recordset('book', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
78 foreach ($rs as $b) {
79 $b->intro = text_to_html($b->intro, false, false, true);
80 $b->introformat = FORMAT_HTML;
81 $DB->update_record('book', $b);
82 upgrade_set_timeout();
83 }
84 unset($b);
85 $rs->close();
86 }
87 }
88
89 // book savepoint reached
90 upgrade_mod_savepoint(true, 2012061704, 'book');
91 }
92
93 if ($oldversion < 2012061705) {
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', null, 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 $DB->set_field('book_chapters', 'contentformat', FORMAT_HTML, array());
103 }
104
105 // book savepoint reached
106 upgrade_mod_savepoint(true, 2012061705, 'book');
107 }
108
109 if ($oldversion < 2012061706) {
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 = context_module::instance($book->cmid);
129
130 mod_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/");
136 @rmdir("$CFG->dataroot/$book->course/");
137 }
138 $rs->close();
139 }
140
141 // book savepoint reached
142 upgrade_mod_savepoint(true, 2012061706, 'book');
143 }
144
145 if ($oldversion < 2012061707) {
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, 2012061707, 'book');
157 }
158
159 if ($oldversion < 2012061708) {
160 unset_config('book_tocwidth');
161
162 // book savepoint reached
163 upgrade_mod_savepoint(true, 2012061708, 'book');
164 }
165
166 if ($oldversion < 2012061709) {
167 require_once("$CFG->dirroot/mod/book/db/upgradelib.php");
168
169 mod_book_migrate_all_areas();
170
171 upgrade_mod_savepoint(true, 2012061709, 'book');
172 }
173
174 if ($oldversion < 2012061710) {
175
176 // Define field revision to be added to book
177 $table = new xmldb_table('book');
178 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'customtitles');
179
180 // Conditionally launch add field revision
181 if (!$dbman->field_exists($table, $field)) {
182 $dbman->add_field($table, $field);
183 }
184
185 // book savepoint reached
186 upgrade_mod_savepoint(true, 2012061710, 'book');
187 }
188 // End of MDL-35297 "replayed" steps.
b574c078 189
0f0ba611 190 return true;
5db9c35d 191}