Hide/show individual activities in any section
[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) {
f82c2d42 91 execute_sql(" ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` ");
7beb45d8 92 require_once("$CFG->dirroot/mod/forum/lib.php");
93 require_once("$CFG->dirroot/course/lib.php");
94
95 if (! $module = get_record("modules", "name", "forum")) {
96 notify("Could not find forum module!!");
97 return false;
98 }
99
100 // First upgrade the site forums
101 if ($site = get_site()) {
102 print_heading("Making News forums editable for main site (moving to section 1)...");
103 if ($news = forum_get_course_forum($site->id, "news")) {
104 $mod->course = $site->id;
105 $mod->module = $module->id;
106 $mod->instance = $news->id;
107 $mod->section = 1;
108 if (! $mod->coursemodule = add_course_module($mod) ) {
109 notify("Could not add a new course module to the site");
110 return false;
111 }
112 if (! $sectionid = add_mod_to_section($mod) ) {
113 notify("Could not add the new course module to that section");
114 return false;
115 }
116 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
117 notify("Could not update the course module with the correct section");
118 return false;
119 }
120 }
121 }
122
123
124 // Now upgrade the courses.
125 if ($courses = get_records_sql("SELECT * FROM course WHERE category > 0")) {
126 print_heading("Making News and Social forums editable for each course (moving to section 0)...");
127 foreach ($courses as $course) {
128 if ($course->format == "social") { // we won't touch them
129 continue;
130 }
131 if ($news = forum_get_course_forum($course->id, "news")) {
132 $mod->course = $course->id;
133 $mod->module = $module->id;
134 $mod->instance = $news->id;
135 $mod->section = 0;
136 if (! $mod->coursemodule = add_course_module($mod) ) {
137 notify("Could not add a new course module to the course '$course->fullname'");
138 return false;
139 }
140 if (! $sectionid = add_mod_to_section($mod) ) {
141 notify("Could not add the new course module to that section");
142 return false;
143 }
144 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
145 notify("Could not update the course module with the correct section");
146 return false;
147 }
148 }
149 if ($social = forum_get_course_forum($course->id, "social")) {
150 $mod->course = $course->id;
151 $mod->module = $module->id;
152 $mod->instance = $social->id;
153 $mod->section = 0;
154 if (! $mod->coursemodule = add_course_module($mod) ) {
155 notify("Could not add a new course module to the course '$course->fullname'");
156 return false;
157 }
158 if (! $sectionid = add_mod_to_section($mod) ) {
159 notify("Could not add the new course module to that section");
160 return false;
161 }
162 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
163 notify("Could not update the course module with the correct section");
164 return false;
165 }
166 }
167 }
168 }
169 }
170
d897cae4 171 if ($oldversion < 2002111003) {
172 execute_sql(" ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` ");
173 if ($courses = get_records_sql("SELECT * FROM course")) {
174 require_once("$CFG->dirroot/course/lib.php");
175 foreach ($courses as $course) {
176
177 $modinfo = serialize(get_array_of_activities($course->id));
178
179 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
180 notify("Could not cache module information for course '$course->fullname'!");
181 }
182 }
183 }
184 }
185
3635d82f 186 if ($oldversion < 2002111100) {
187 print_simple_box_start("CENTER", "", "#FFCCCC");
188 echo "<FONT SIZE=+1>";
189 echo "<P>Changes have been made to all built-in themes, to add the new popup navigation menu.";
190 echo "<P>If you have customised themes, you will need to edit theme/xxxx/header.html as follows:";
191 echo "<UL><LI>Change anywhere it says <B>$"."button</B> to say <B>$"."menu</B>";
192 echo "<LI>Add <B>$"."button</B> elsewhere (eg at the end of the navigation bar)</UL>";
193 echo "<P>See the standard themes for examples, eg: theme/standard/header.html";
194 print_simple_box_end();
195 }
196
13beeacb 197 if ($oldversion < 2002111200) {
198 execute_sql(" ALTER TABLE `course` ADD `showrecent` TINYINT(5) UNSIGNED DEFAULT '1' NOT NULL AFTER `numsections` ");
199 }
200
f8ef7d66 201 if ($oldversion < 2002111400) {
202 // Rebuild all course caches, because some may not be done in new installs (eg site page)
203 if ($courses = get_records_sql("SELECT * FROM course")) {
204 require_once("$CFG->dirroot/course/lib.php");
205 foreach ($courses as $course) {
206
207 $modinfo = serialize(get_array_of_activities($course->id));
208
209 if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) {
210 notify("Could not cache module information for course '$course->fullname'!");
211 }
212 }
213 }
214 }
215
277d2d9f 216 if ($oldversion < 2002112000) {
217 set_config("guestloginbutton", 1);
218 }
219
ebc3bd2b 220 if ($oldversion < 2002122300) {
221 execute_sql("ALTER TABLE `log` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
222 execute_sql("ALTER TABLE `user_admins` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
223 execute_sql("ALTER TABLE `user_students` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
224 execute_sql("ALTER TABLE `user_teachers` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
225 execute_sql("ALTER TABLE `user_students` CHANGE `start` `timestart` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
226 execute_sql("ALTER TABLE `user_students` CHANGE `end` `timeend` INT(10) UNSIGNED DEFAULT '0' NOT NULL ");
227 }
228
9d378732 229 if ($oldversion < 2002122700) {
05a6c03e 230 if (! record_exists("log_display", "module", "user", "action", "view")) {
a3eea09c 231 execute_sql("INSERT INTO {$CFG->prefix}log_display VALUES ('user', 'view', 'user', 'CONCAT(firstname,' ',lastname)') ");
232 }
233 }
db70b54b 234 if ($oldversion < 2003010101) {
14e4a496 235 delete_records("log_display", "module", "user");
236 $new->module = "user";
237 $new->action = "view";
238 $new->mtable = "user";
db70b54b 239 $new->field = "CONCAT(firstname,\" \",lastname)";
14e4a496 240 insert_record("log_display", $new);
241
242 delete_records("log_display", "module", "course");
243 $new->module = "course";
244 $new->action = "view";
245 $new->mtable = "course";
246 $new->field = "fullname";
247 insert_record("log_display", $new);
248 $new->action = "update";
249 insert_record("log_display", $new);
db70b54b 250 $new->action = "enrol";
251 insert_record("log_display", $new);
14e4a496 252 }
d897cae4 253
811c88b3 254 if ($oldversion < 2003012200) {
255 execute_sql(" ALTER TABLE `log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
256 }
257
2fdb54b8 258 if ($oldversion < 2003032500) {
259 modify_database("", "CREATE TABLE `prefix_user_coursecreators` (
260 `id` int(10) unsigned NOT NULL auto_increment,
261 `userid` int(10) unsigned NOT NULL default '0',
262 PRIMARY KEY (`id`),
263 UNIQUE KEY `id` (`id`)
264 ) TYPE=MyISAM COMMENT='One record per course creator';");
1924074c 265 }
264d4e0b 266 if ($oldversion < 2003032602) {
267 // Redoing it because of no prefix last time
268 execute_sql(" ALTER TABLE `{$CFG->prefix}log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
269 // Add some indexes for speed
270 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(course) ");
271 execute_sql(" ALTER TABLE `{$CFG->prefix}log` ADD INDEX(userid) ");
272 }
1acfbce5 273
274 if ($oldversion < 2003041400) {
275 table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score");
276 }
1924074c 277
31f0900c 278 return true;
279}
280
281?>