2002-10-05 17:09:31 +00:00
< ? PHP //$Id$
//
// This file keeps track of upgrades to Moodle.
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// Versions are defined by /version.php
//
// This file is tailored to MySQL
2002-10-23 04:10:10 +00:00
function main_upgrade ( $oldversion = 0 ) {
2002-10-05 17:09:31 +00:00
2002-10-25 08:47:53 +00:00
global $CFG ;
2003-04-25 03:54:21 +00:00
$result = true ;
2002-10-05 17:09:31 +00:00
if ( $oldversion == 0 ) {
execute_sql ( "
CREATE TABLE `config` (
`id` int ( 10 ) unsigned NOT NULL auto_increment ,
`name` varchar ( 255 ) NOT NULL default '' ,
`value` varchar ( 255 ) NOT NULL default '' ,
PRIMARY KEY ( `id` ),
UNIQUE KEY `name` ( `name` )
) COMMENT = 'Moodle configuration variables' ; " );
notify ( " Created a new table 'config' to hold configuration data " );
}
if ( $oldversion < 2002073100 ) {
execute_sql ( " DELETE FROM `modules` WHERE `name` = 'chat' " );
}
if ( $oldversion < 2002080200 ) {
execute_sql ( " ALTER TABLE `modules` DROP `fullname` " );
execute_sql ( " ALTER TABLE `modules` DROP `search` " );
}
if ( $oldversion < 2002080300 ) {
execute_sql ( " ALTER TABLE `log_display` CHANGE `table` `mtable` VARCHAR( 20 ) NOT NULL " );
execute_sql ( " ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 3 ) DEFAULT '3' NOT NULL " );
}
if ( $oldversion < 2002082100 ) {
execute_sql ( " ALTER TABLE `course` CHANGE `guest` `guest` TINYINT(2) UNSIGNED DEFAULT '0' NOT NULL " );
}
if ( $oldversion < 2002082101 ) {
execute_sql ( " ALTER TABLE `user` ADD `maildisplay` TINYINT(2) UNSIGNED DEFAULT '2' NOT NULL AFTER `mailformat` " );
}
if ( $oldversion < 2002090100 ) {
execute_sql ( " ALTER TABLE `course_sections` CHANGE `summary` `summary` TEXT NOT NULL " );
}
if ( $oldversion < 2002090701 ) {
execute_sql ( " ALTER TABLE `user_teachers` CHANGE `authority` `authority` TINYINT( 10 ) DEFAULT '3' NOT NULL " );
execute_sql ( " ALTER TABLE `user_teachers` ADD `role` VARCHAR(40) NOT NULL AFTER `authority` " );
}
if ( $oldversion < 2002090800 ) {
execute_sql ( " ALTER TABLE `course` ADD `teachers` VARCHAR( 100 ) DEFAULT 'Teachers' NOT NULL AFTER `teacher` " );
execute_sql ( " ALTER TABLE `course` ADD `students` VARCHAR( 100 ) DEFAULT 'Students' NOT NULL AFTER `student` " );
}
if ( $oldversion < 2002091000 ) {
execute_sql ( " ALTER TABLE `user` CHANGE `personality` `secret` VARCHAR( 15 ) DEFAULT NULL " );
}
if ( $oldversion < 2002091400 ) {
execute_sql ( " ALTER TABLE `user` ADD `lang` VARCHAR( 3 ) DEFAULT 'en' NOT NULL AFTER `country` " );
}
if ( $oldversion < 2002091900 ) {
notify ( " Most Moodle configuration variables have been moved to the database and can now be edited via the admin page. " );
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. " );
}
if ( $oldversion < 2002092000 ) {
execute_sql ( " ALTER TABLE `user` CHANGE `lang` `lang` VARCHAR(5) DEFAULT 'en' NOT NULL " );
}
if ( $oldversion < 2002092100 ) {
execute_sql ( " ALTER TABLE `user` ADD `deleted` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `confirmed` " );
}
2002-10-10 07:26:10 +00:00
if ( $oldversion < 2002101001 ) {
execute_sql ( " ALTER TABLE `user` ADD `htmleditor` TINYINT(1) UNSIGNED DEFAULT '1' NOT NULL AFTER `maildisplay` " );
}
2002-10-17 14:03:59 +00:00
if ( $oldversion < 2002101701 ) {
execute_sql ( " ALTER TABLE `reading` RENAME `resource` " ); // Small line with big consequences!
execute_sql ( " DELETE FROM `log_display` WHERE module = 'reading' " );
execute_sql ( " INSERT INTO log_display VALUES ('resource', 'view', 'resource', 'name') " );
execute_sql ( " UPDATE log SET module = 'resource' WHERE module = 'reading' " );
execute_sql ( " UPDATE modules SET name = 'resource' WHERE name = 'reading' " );
}
2002-10-05 17:09:31 +00:00
2002-10-25 08:47:53 +00:00
if ( $oldversion < 2002102503 ) {
2003-03-12 07:36:22 +00:00
execute_sql ( " ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` " );
2002-10-25 08:47:53 +00:00
require_once ( " $CFG->dirroot /mod/forum/lib.php " );
require_once ( " $CFG->dirroot /course/lib.php " );
if ( ! $module = get_record ( " modules " , " name " , " forum " )) {
notify ( " Could not find forum module!! " );
return false ;
}
// First upgrade the site forums
if ( $site = get_site ()) {
print_heading ( " Making News forums editable for main site (moving to section 1)... " );
if ( $news = forum_get_course_forum ( $site -> id , " news " )) {
$mod -> course = $site -> id ;
$mod -> module = $module -> id ;
$mod -> instance = $news -> id ;
$mod -> section = 1 ;
if ( ! $mod -> coursemodule = add_course_module ( $mod ) ) {
notify ( " Could not add a new course module to the site " );
return false ;
}
if ( ! $sectionid = add_mod_to_section ( $mod ) ) {
notify ( " Could not add the new course module to that section " );
return false ;
}
if ( ! set_field ( " course_modules " , " section " , $sectionid , " id " , $mod -> coursemodule )) {
notify ( " Could not update the course module with the correct section " );
return false ;
}
}
}
// Now upgrade the courses.
if ( $courses = get_records_sql ( " SELECT * FROM course WHERE category > 0 " )) {
print_heading ( " Making News and Social forums editable for each course (moving to section 0)... " );
foreach ( $courses as $course ) {
if ( $course -> format == " social " ) { // we won't touch them
continue ;
}
if ( $news = forum_get_course_forum ( $course -> id , " news " )) {
$mod -> course = $course -> id ;
$mod -> module = $module -> id ;
$mod -> instance = $news -> id ;
$mod -> section = 0 ;
if ( ! $mod -> coursemodule = add_course_module ( $mod ) ) {
notify ( " Could not add a new course module to the course ' $course->fullname ' " );
return false ;
}
if ( ! $sectionid = add_mod_to_section ( $mod ) ) {
notify ( " Could not add the new course module to that section " );
return false ;
}
if ( ! set_field ( " course_modules " , " section " , $sectionid , " id " , $mod -> coursemodule )) {
notify ( " Could not update the course module with the correct section " );
return false ;
}
}
if ( $social = forum_get_course_forum ( $course -> id , " social " )) {
$mod -> course = $course -> id ;
$mod -> module = $module -> id ;
$mod -> instance = $social -> id ;
$mod -> section = 0 ;
if ( ! $mod -> coursemodule = add_course_module ( $mod ) ) {
notify ( " Could not add a new course module to the course ' $course->fullname ' " );
return false ;
}
if ( ! $sectionid = add_mod_to_section ( $mod ) ) {
notify ( " Could not add the new course module to that section " );
return false ;
}
if ( ! set_field ( " course_modules " , " section " , $sectionid , " id " , $mod -> coursemodule )) {
notify ( " Could not update the course module with the correct section " );
return false ;
}
}
}
}
}
2002-11-10 07:37:15 +00:00
if ( $oldversion < 2002111003 ) {
execute_sql ( " ALTER TABLE `course` ADD `modinfo` TEXT NOT NULL AFTER `format` " );
if ( $courses = get_records_sql ( " SELECT * FROM course " )) {
require_once ( " $CFG->dirroot /course/lib.php " );
foreach ( $courses as $course ) {
$modinfo = serialize ( get_array_of_activities ( $course -> id ));
if ( ! set_field ( " course " , " modinfo " , $modinfo , " id " , $course -> id )) {
notify ( " Could not cache module information for course ' $course->fullname '! " );
}
}
}
}
2002-11-11 08:25:41 +00:00
if ( $oldversion < 2002111100 ) {
print_simple_box_start ( " CENTER " , " " , " #FFCCCC " );
echo " <FONT SIZE=+1> " ;
echo " <P>Changes have been made to all built-in themes, to add the new popup navigation menu. " ;
echo " <P>If you have customised themes, you will need to edit theme/xxxx/header.html as follows: " ;
echo " <UL><LI>Change anywhere it says <B> $ " . " button</B> to say <B> $ " . " menu</B> " ;
echo " <LI>Add <B> $ " . " button</B> elsewhere (eg at the end of the navigation bar)</UL> " ;
echo " <P>See the standard themes for examples, eg: theme/standard/header.html " ;
print_simple_box_end ();
}
2002-11-12 04:26:16 +00:00
if ( $oldversion < 2002111200 ) {
execute_sql ( " ALTER TABLE `course` ADD `showrecent` TINYINT(5) UNSIGNED DEFAULT '1' NOT NULL AFTER `numsections` " );
}
2002-11-14 03:14:10 +00:00
if ( $oldversion < 2002111400 ) {
// Rebuild all course caches, because some may not be done in new installs (eg site page)
if ( $courses = get_records_sql ( " SELECT * FROM course " )) {
require_once ( " $CFG->dirroot /course/lib.php " );
foreach ( $courses as $course ) {
$modinfo = serialize ( get_array_of_activities ( $course -> id ));
if ( ! set_field ( " course " , " modinfo " , $modinfo , " id " , $course -> id )) {
notify ( " Could not cache module information for course ' $course->fullname '! " );
}
}
}
}
2002-11-20 15:03:06 +00:00
if ( $oldversion < 2002112000 ) {
set_config ( " guestloginbutton " , 1 );
}
2002-12-23 09:39:26 +00:00
if ( $oldversion < 2002122300 ) {
execute_sql ( " ALTER TABLE `log` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
execute_sql ( " ALTER TABLE `user_admins` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
execute_sql ( " ALTER TABLE `user_students` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
execute_sql ( " ALTER TABLE `user_teachers` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
execute_sql ( " ALTER TABLE `user_students` CHANGE `start` `timestart` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
execute_sql ( " ALTER TABLE `user_students` CHANGE `end` `timeend` INT(10) UNSIGNED DEFAULT '0' NOT NULL " );
}
2002-12-29 04:17:32 +00:00
if ( $oldversion < 2002122700 ) {
2002-12-26 17:02:45 +00:00
if ( ! record_exists ( " log_display " , " module " , " user " , " action " , " view " )) {
2002-12-23 11:47:17 +00:00
execute_sql ( " INSERT INTO { $CFG -> prefix } log_display VALUES ('user', 'view', 'user', 'CONCAT(firstname,' ',lastname)') " );
}
}
2003-01-01 08:15:55 +00:00
if ( $oldversion < 2003010101 ) {
2003-01-01 08:08:44 +00:00
delete_records ( " log_display " , " module " , " user " );
$new -> module = " user " ;
$new -> action = " view " ;
$new -> mtable = " user " ;
2003-01-01 08:15:55 +00:00
$new -> field = " CONCAT(firstname, \" \" ,lastname) " ;
2003-01-01 08:08:44 +00:00
insert_record ( " log_display " , $new );
delete_records ( " log_display " , " module " , " course " );
$new -> module = " course " ;
$new -> action = " view " ;
$new -> mtable = " course " ;
$new -> field = " fullname " ;
insert_record ( " log_display " , $new );
$new -> action = " update " ;
insert_record ( " log_display " , $new );
2003-01-01 08:15:55 +00:00
$new -> action = " enrol " ;
insert_record ( " log_display " , $new );
2003-01-01 08:08:44 +00:00
}
2002-11-10 07:37:15 +00:00
2003-02-16 09:24:25 +00:00
if ( $oldversion < 2003012200 ) {
2003-04-21 07:21:33 +00:00
// execute_sql(" ALTER TABLE `log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL ");
// Commented out - see below where it's done properly
2003-02-16 09:24:25 +00:00
}
2003-02-25 03:53:50 +00:00
if ( $oldversion < 2003032500 ) {
modify_database ( " " , " CREATE TABLE `prefix_user_coursecreators` (
`id` int ( 10 ) unsigned NOT NULL auto_increment ,
`userid` int ( 10 ) unsigned NOT NULL default '0' ,
PRIMARY KEY ( `id` ),
UNIQUE KEY `id` ( `id` )
) TYPE = MyISAM COMMENT = 'One record per course creator' ; " );
2003-02-24 18:48:55 +00:00
}
2003-03-26 02:17:46 +00:00
if ( $oldversion < 2003032602 ) {
// Redoing it because of no prefix last time
execute_sql ( " ALTER TABLE ` { $CFG -> prefix } log_display` CHANGE `module` `module` VARCHAR( 20 ) NOT NULL " );
// Add some indexes for speed
execute_sql ( " ALTER TABLE ` { $CFG -> prefix } log` ADD INDEX(course) " );
execute_sql ( " ALTER TABLE ` { $CFG -> prefix } log` ADD INDEX(userid) " );
}
2003-04-14 15:11:09 +00:00
if ( $oldversion < 2003041400 ) {
table_column ( " course_modules " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " , " not null " , " score " );
}
2003-02-24 18:48:55 +00:00
2003-04-21 07:21:33 +00:00
if ( $oldversion < 2003042104 ) { // Try to update permissions of all files
if ( $files = get_directory_list ( $CFG -> dataroot )) {
echo " Attempting to update permissions for all files... ignore any errors. " ;
foreach ( $files as $file ) {
echo " $CFG->dataroot / $file <br> " ;
2003-04-21 07:26:23 +00:00
@ chmod ( " $CFG->dataroot / $file " , $CFG -> directorypermissions );
2003-04-21 07:21:33 +00:00
}
}
}
2003-04-23 17:02:24 +00:00
if ( $oldversion < 2003042400 ) {
// Rebuild all course caches, because of changes to do with visible variable
if ( $courses = get_records_sql ( " SELECT * FROM { $CFG -> prefix } course " )) {
require_once ( " $CFG->dirroot /course/lib.php " );
foreach ( $courses as $course ) {
$modinfo = serialize ( get_array_of_activities ( $course -> id ));
if ( ! set_field ( " course " , " modinfo " , $modinfo , " id " , $course -> id )) {
notify ( " Could not cache module information for course ' $course->fullname '! " );
}
}
}
}
2003-04-25 03:54:21 +00:00
if ( $oldversion < 2003042500 ) {
// Convert all usernames to lowercase.
$users = get_records_sql ( " SELECT id, username FROM { $CFG -> prefix } user " );
$cerrors = " " ;
$rarray = array ();
foreach ( $users as $user ) { // Check for possible conflicts
$lcname = trim ( moodle_strtolower ( $user -> username ));
if ( in_array ( $lcname , $rarray )) {
$cerrors .= $user -> id . " -> " . $lcname . '<br/>' ;
} else {
array_push ( $rarray , $lcname );
}
}
if ( $cerrors != '' ) {
notify ( " Error: Cannot convert usernames to lowercase.
Following usernames would overlap ( id -> username ) :< br /> $cerrors .
Please resolve overlapping errors . " );
$result = false ;
}
$cerrors = " " ;
echo " Checking userdatabase:<br> " ;
foreach ( $users as $user ) {
$lcname = trim ( moodle_strtolower ( $user -> username ));
if ( $lcname != $user -> username ) {
$convert = set_field ( " user " , " username " , $lcname , " id " , $user -> id );
if ( ! $convert ) {
if ( $cerrors ){
$cerrors .= " , " ;
}
$cerrors .= $item ;
} else {
echo " . " ;
}
}
}
if ( $cerrors != '' ) {
notify ( " There were errors when converting following usernames to lowercase.
'$cerrors' . Sorry , but you will need to fix your database by hand . " );
$result = false ;
}
}
return $result ;
2002-10-05 17:09:31 +00:00
}
?>