Tidied up some of Petri's code :-/
[moodle.git] / lib / db / mysql.php
CommitLineData
31f0900c 1<?PHP //$Id$
2//
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
9// The upgrade function in this file will attempt
10// to perform all the necessary actions to upgrade
11// your older installtion to the current version.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// Versions are defined by /version.php
17//
18// This file is tailored to MySQL
19
e7311a0a 20function main_upgrade($oldversion=0) {
31f0900c 21
7beb45d8 22 global $CFG;
23
31f0900c 24 if ($oldversion == 0) {
25 execute_sql("
26 CREATE TABLE `config` (
27 `id` int(10) unsigned NOT NULL auto_increment,
28 `name` varchar(255) NOT NULL default '',
29 `value` varchar(255) NOT NULL default '',
30 PRIMARY KEY (`id`),
31 UNIQUE KEY `name` (`name`)
32 ) COMMENT='Moodle configuration variables';");
33 notify("Created a new table 'config' to hold configuration data");
34 }
35 if ($oldversion < 2002073100) {
36 execute_sql(" DELETE FROM `modules` WHERE `name` = 'chat' ");
37 }
38 if ($oldversion < 2002080200) {
39 execute_sql(" ALTER TABLE `modules` DROP `fullname` ");
40 execute_sql(" ALTER TABLE `modules` DROP `search` ");
41 }
42 if ($oldversion < 2002080300) {
43 execute_sql(" ALTER TABLE `log_display` CHANGE `table` `mtable` VARCHAR( 20 ) NOT NULL ");
44 execute_sql(" ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 3 ) DEFAULT '3' NOT NULL ");
45 }
46 if ($oldversion < 2002082100) {
47 execute_sql(" ALTER TABLE `course` CHANGE `guest` `guest` TINYINT(2) UNSIGNED DEFAULT '0' NOT NULL ");
48 }
49 if ($oldversion < 2002082101) {
50 execute_sql(" ALTER TABLE `user` ADD `maildisplay` TINYINT(2) UNSIGNED DEFAULT '2' NOT NULL AFTER `mailformat` ");
51 }
52 if ($oldversion < 2002090100) {
53 execute_sql(" ALTER TABLE `course_sections` CHANGE `summary` `summary` TEXT NOT NULL ");
54 }
55 if ($oldversion < 2002090701) {
56 execute_sql(" ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 10 ) DEFAULT '3' NOT NULL ");
57 execute_sql(" ALTER TABLE `user_teachers` ADD `role` VARCHAR(40) NOT NULL AFTER `authority` ");
58 }
59 if ($oldversion < 2002090800) {
60 execute_sql(" ALTER TABLE `course` ADD `teachers` VARCHAR( 100 ) DEFAULT 'Teachers' NOT NULL AFTER `teacher` ");
61 execute_sql(" ALTER TABLE `course` ADD `students` VARCHAR( 100 ) DEFAULT 'Students' NOT NULL AFTER `student` ");
62 }
63 if ($oldversion < 2002091000) {
64 execute_sql(" ALTER TABLE `user` CHANGE `personality` `secret` VARCHAR( 15 ) DEFAULT NULL ");
65 }
66 if ($oldversion < 2002091400) {
67 execute_sql(" ALTER TABLE `user` ADD `lang` VARCHAR( 3 ) DEFAULT 'en' NOT NULL AFTER `country` ");
68 }
69 if ($oldversion < 2002091900) {
70 notify("Most Moodle configuration variables have been moved to the database and can now be edited via the admin page.");
71 notify("Although it is not vital that you do so, you might want to edit <U>config.php</U> and remove all the unused settings (except the database, URL and directory definitions). See <U>config-dist.php</U> for an example of how your new slim config.php should look.");
72 }
73 if ($oldversion < 2002092000) {
74 execute_sql(" ALTER TABLE `user` CHANGE `lang` `lang` VARCHAR(5) DEFAULT 'en' NOT NULL ");
75 }
76 if ($oldversion < 2002092100) {
77 execute_sql(" ALTER TABLE `user` ADD `deleted` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `confirmed` ");
78 }
0095d5cd 79 if ($oldversion < 2002101001) {
80 execute_sql(" ALTER TABLE `user` ADD `htmleditor` TINYINT(1) UNSIGNED DEFAULT '1' NOT NULL AFTER `maildisplay` ");
81 }
2a439ba7 82 if ($oldversion < 2002101701) {
83 execute_sql(" ALTER TABLE `reading` RENAME `resource` "); // Small line with big consequences!
84 execute_sql(" DELETE FROM `log_display` WHERE module = 'reading'");
85 execute_sql(" INSERT INTO log_display VALUES ('resource', 'view', 'resource', 'name') ");
86 execute_sql(" UPDATE log SET module = 'resource' WHERE module = 'reading' ");
87 execute_sql(" UPDATE modules SET name = 'resource' WHERE name = 'reading' ");
88 }
31f0900c 89
7beb45d8 90 if ($oldversion < 2002102503) {
91 require_once("$CFG->dirroot/mod/forum/lib.php");
92 require_once("$CFG->dirroot/course/lib.php");
93
94 if (! $module = get_record("modules", "name", "forum")) {
95 notify("Could not find forum module!!");
96 return false;
97 }
98
99 // First upgrade the site forums
100 if ($site = get_site()) {
101 print_heading("Making News forums editable for main site (moving to section 1)...");
102 if ($news = forum_get_course_forum($site->id, "news")) {
103 $mod->course = $site->id;
104 $mod->module = $module->id;
105 $mod->instance = $news->id;
106 $mod->section = 1;
107 if (! $mod->coursemodule = add_course_module($mod) ) {
108 notify("Could not add a new course module to the site");
109 return false;
110 }
111 if (! $sectionid = add_mod_to_section($mod) ) {
112 notify("Could not add the new course module to that section");
113 return false;
114 }
115 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
116 notify("Could not update the course module with the correct section");
117 return false;
118 }
119 }
120 }
121
122
123 // Now upgrade the courses.
124 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0")) {
125 print_heading("Making News and Social forums editable for each course (moving to section 0)...");
126 foreach ($courses as $course) {
127 if ($course->format == "social") { // we won't touch them
128 continue;
129 }
130 if ($news = forum_get_course_forum($course->id, "news")) {
131 $mod->course = $course->id;
132 $mod->module = $module->id;
133 $mod->instance = $news->id;
134 $mod->section = 0;
135 if (! $mod->coursemodule = add_course_module($mod) ) {
136 notify("Could not add a new course module to the course '$course->fullname'");
137 return false;
138 }
139 if (! $sectionid = add_mod_to_section($mod) ) {
140 notify("Could not add the new course module to that section");
141 return false;
142 }
143 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
144 notify("Could not update the course module with the correct section");
145 return false;
146 }
147 }
148 if ($social = forum_get_course_forum($course->id, "social")) {
149 $mod->course = $course->id;
150 $mod->module = $module->id;
151 $mod->instance = $social->id;
152 $mod->section = 0;
153 if (! $mod->coursemodule = add_course_module($mod) ) {
154 notify("Could not add a new course module to the course '$course->fullname'");
155 return false;
156 }
157 if (! $sectionid = add_mod_to_section($mod) ) {
158 notify("Could not add the new course module to that section");
159 return false;
160 }
161 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
162 notify("Could not update the course module with the correct section");
163 return false;
164 }
165 }
166 }
167 }
168 }
169
d897cae4 170 if ($oldversion < 2002111003) {
171 execute_sql(" ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` ");
172 if ($courses = get_records_sql("SELECT * FROM course")) {
173 require_once("$CFG->dirroot/course/lib.php");
174 foreach ($courses as $course) {
175
176 $modinfo = serialize(get_array_of_activities($course->id));
177
178 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
179 notify("Could not cache module information for course '$course->fullname'!");
180 }
181 }
182 }
183 }
184
3635d82f 185 if ($oldversion < 2002111100) {
186 print_simple_box_start("CENTER", "", "#FFCCCC");
187 echo "<FONT SIZE=+1>";
188 echo "<P>Changes have been made to all built-in themes, to add the new popup navigation menu.";
189 echo "<P>If you have customised themes, you will need to edit theme/xxxx/header.html as follows:";
190 echo "<UL><LI>Change anywhere it says <B>$"."button</B> to say <B>$"."menu</B>";
191 echo "<LI>Add <B>$"."button</B> elsewhere (eg at the end of the navigation bar)</UL>";
192 echo "<P>See the standard themes for examples, eg: theme/standard/header.html";
193 print_simple_box_end();
194 }
195
13beeacb 196 if ($oldversion < 2002111200) {
197 execute_sql(" ALTER TABLE `course` ADD `showrecent` TINYINT(5) UNSIGNED DEFAULT '1' NOT NULL AFTER `numsections` ");
198 }
199
f8ef7d66 200 if ($oldversion < 2002111400) {
201 // Rebuild all course caches, because some may not be done in new installs (eg site page)
202 if ($courses = get_records_sql("SELECT * FROM course")) {
203 require_once("$CFG->dirroot/course/lib.php");
204 foreach ($courses as $course) {
205
206 $modinfo = serialize(get_array_of_activities($course->id));
207
208 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
209 notify("Could not cache module information for course '$course->fullname'!");
210 }
211 }
212 }
213 }
214
277d2d9f 215 if ($oldversion < 2002112000) {
216 set_config("guestloginbutton", 1);
217 }
218
ebc3bd2b 219 if ($oldversion < 2002122300) {
220 execute_sql("ALTER TABLE `log` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
221 execute_sql("ALTER TABLE `user_admins` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
222 execute_sql("ALTER TABLE `user_students` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
223 execute_sql("ALTER TABLE `user_teachers` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
224 execute_sql("ALTER TABLE `user_students` CHANGE `start` `timestart` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
225 execute_sql("ALTER TABLE `user_students` CHANGE `end` `timeend` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
226 }
227
9d378732 228 if ($oldversion < 2002122700) {
05a6c03e 229 if (! record_exists("log_display", "module", "user", "action", "view")) {
a3eea09c 230 execute_sql("INSERT INTO {$CFG->prefix}log_display VALUES ('user', 'view', 'user', 'CONCAT(firstname,' ',lastname)') ");
231 }
232 }
db70b54b 233 if ($oldversion < 2003010101) {
14e4a496 234 delete_records("log_display", "module", "user");
235 $new->module = "user";
236 $new->action = "view";
237 $new->mtable = "user";
db70b54b 238 $new->field = "CONCAT(firstname,\" \",lastname)";
14e4a496 239 insert_record("log_display", $new);
240
241 delete_records("log_display", "module", "course");
242 $new->module = "course";
243 $new->action = "view";
244 $new->mtable = "course";
245 $new->field = "fullname";
246 insert_record("log_display", $new);
247 $new->action = "update";
248 insert_record("log_display", $new);
db70b54b 249 $new->action = "enrol";
250 insert_record("log_display", $new);
14e4a496 251 }
d897cae4 252
811c88b3 253 if ($oldversion < 2003012200) {
254 execute_sql(" ALTER TABLE `log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
255 }
256
1924074c 257 if ($oldversion < 2003032400) {
258 execute_sql("CREATE TABLE `$CFG->prefix_user_coursecreators` (
259 `id` int(10) unsigned NOT NULL auto_increment,
260 `userid` int(10) unsigned NOT NULL default '0',
261 PRIMARY KEY (`id`),
262 UNIQUE KEY `id` (`id`)
263 ) TYPE=MyISAM COMMENT='One record per course creator'");
264 }
265
31f0900c 266 return true;
267}
268
269?>