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 PostgreSQL 7
2002-10-23 04:10:10 +00:00
function main_upgrade ( $oldversion = 0 ) {
2003-05-09 17:24:17 +00:00
2003-10-27 12:38:00 +00:00
global $CFG , $THEME , $db ;
2003-05-09 17:24:17 +00:00
2003-04-24 18:48:09 +00:00
$result = true ;
2003-05-09 17:24:17 +00:00
2003-01-01 08:15:55 +00:00
if ( $oldversion < 2003010101 ) {
delete_records ( " log_display " , " module " , " user " );
$new -> module = " user " ;
$new -> action = " view " ;
$new -> mtable = " user " ;
$new -> field = " CONCAT(firstname, \" \" ,lastname) " ;
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 );
$new -> action = " enrol " ;
insert_record ( " log_display " , $new );
}
2003-02-24 18:48:55 +00:00
2003-04-25 03:54:21 +00:00
//support user based course creating
2003-02-24 18:48:55 +00:00
if ( $oldversion < 2003032400 ) {
2003-04-28 16:56:57 +00:00
execute_sql ( " CREATE TABLE { $CFG -> prefix } user_coursecreators (
2003-02-24 18:48:55 +00:00
id int8 SERIAL PRIMARY KEY ,
userid int8 NOT NULL default '0'
) " );
2003-04-25 03:54:21 +00:00
}
2003-04-14 15:11:09 +00:00
2003-04-25 03:54:21 +00:00
if ( $oldversion < 2003041400 ) {
2003-04-14 15:11:09 +00:00
table_column ( " course_modules " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " , " not null " , " score " );
}
2003-04-21 07:21:33 +00:00
2003-04-25 03:54:21 +00:00
if ( $oldversion < 2003042104 ) { // Try to update permissions of all files
2003-04-21 07:21:33 +00:00
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 '! " );
}
}
}
}
2002-10-05 17:09:31 +00:00
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 )) {
2003-04-24 18:48:09 +00:00
$cerrors .= $user -> id . " -> " . $lcname . '<br/>' ;
2003-04-25 03:54:21 +00:00
} else {
array_push ( $rarray , $lcname );
}
2003-04-24 18:48:09 +00:00
}
2003-04-25 03:54:21 +00:00
2003-04-24 18:48:09 +00:00
if ( $cerrors != '' ) {
2003-04-25 03:54:21 +00:00
notify ( " Error: Cannot convert usernames to lowercase.
Following usernames would overlap ( id -> username ) :< br /> $cerrors .
Please resolve overlapping errors . " );
2003-04-24 18:48:09 +00:00
$result = false ;
2003-04-25 03:54:21 +00:00
}
$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 ) {
2003-04-24 18:48:09 +00:00
if ( $cerrors ){
$cerrors .= " , " ;
}
$cerrors .= $item ;
2003-04-25 03:54:21 +00:00
} else {
echo " . " ;
2003-04-24 18:48:09 +00:00
}
}
2003-04-25 03:54:21 +00:00
}
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 ;
2003-04-24 18:48:09 +00:00
}
}
2003-04-25 03:54:21 +00:00
2003-04-27 09:54:03 +00:00
if ( $oldversion < 2003042700 ) {
/// Changing to multiple indexes
2003-05-03 06:44:03 +00:00
execute_sql ( " CREATE INDEX { $CFG -> prefix } log_coursemoduleaction_idx ON { $CFG -> prefix } log (course,module,action) " );
execute_sql ( " CREATE INDEX { $CFG -> prefix } log_courseuserid_idx ON { $CFG -> prefix } log (course,userid) " );
2003-04-27 09:54:03 +00:00
}
2003-04-28 13:29:26 +00:00
if ( $oldversion < 2003042801 ) {
execute_sql ( " CREATE TABLE { $CFG -> prefix } course_display (
id SERIAL PRIMARY KEY ,
course integer NOT NULL default '0' ,
userid integer NOT NULL default '0' ,
display integer NOT NULL default '0'
) " );
2003-05-03 06:44:03 +00:00
execute_sql ( " CREATE INDEX { $CFG -> prefix } course_display_courseuserid_idx ON { $CFG -> prefix } course_display (course,userid) " );
2003-04-28 13:29:26 +00:00
}
2003-05-04 07:59:46 +00:00
if ( $oldversion < 2003050400 ) {
table_column ( " course_sections " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " , " " , " " );
}
2003-04-27 09:54:03 +00:00
2003-05-04 16:05:59 +00:00
if ( $oldversion < 2003050401 ) {
table_column ( " user " , " " , " lang " , " VARCHAR " , " 5 " , " " , " $CFG->lang " , " NOT NULL " , " " );
}
2003-05-09 13:32:27 +00:00
if ( $oldversion < 2003050900 ) {
table_column ( " modules " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " , " " , " " );
}
2003-05-09 17:24:17 +00:00
if ( $oldversion < 2003050902 ) {
if ( get_records ( " modules " , " name " , " pgassignment " )) {
print_simple_box ( " Note: the pgassignment module will soon be deleted from CVS! Go to the new 'Manage Modules' page and DELETE IT from your system " , " center " , " 50% " , " $THEME->cellheading " , " 20 " , " noticebox " );
}
}
2003-05-15 17:26:59 +00:00
if ( $oldversion < 2003051600 ) {
print_simple_box ( " Thanks for upgrading!<p>There are many changes since the last release. Please read the release notes carefully. If you are using CUSTOM themes you will need to edit them. You will also need to check your site's config.php file. " , " center " , " 50% " , " $THEME->cellheading " , " 20 " , " noticebox " );
}
2003-05-23 05:32:40 +00:00
if ( $oldversion < 2003052300 ) {
table_column ( " user " , " " , " autosubscribe " , " integer " , " 1 " , " unsigned " , " 1 " , " " , " htmleditor " );
}
2003-07-21 07:31:01 +00:00
if ( $oldversion < 2003072100 ) {
table_column ( " course " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " , " " , " marker " );
}
2003-07-21 14:28:04 +00:00
if ( $oldversion < 2003072101 ) {
table_column ( " course_sections " , " sequence " , " sequence " , " text " , " " , " " , " " , " " , " " );
}
2003-07-28 02:51:56 +00:00
if ( $oldversion < 2003072800 ) {
print_simple_box ( " The following database index improves performance, but can be quite large - if you are upgrading and you have problems with a limited quota you may want to delete this index later from the ' { $CFG -> prefix } log' table in your database " , " center " , " 50% " , " $THEME->cellheading " , " 20 " , " noticebox " );
2003-07-28 03:28:43 +00:00
flush ();
2003-07-28 02:51:56 +00:00
execute_sql ( " CREATE INDEX { $CFG -> prefix } log_timecoursemoduleaction_idx ON { $CFG -> prefix } log (time,course,module,action) " );
execute_sql ( " CREATE INDEX { $CFG -> prefix } user_students_courseuserid_idx ON { $CFG -> prefix } user_students (course,userid) " );
execute_sql ( " CREATE INDEX { $CFG -> prefix } user_teachers_courseuserid_idx ON { $CFG -> prefix } user_teachers (course,userid) " );
}
2003-07-30 13:02:45 +00:00
if ( $oldversion < 2003072802 ) {
table_column ( " course_categories " , " " , " description " , " text " , " " , " " , " " );
table_column ( " course_categories " , " " , " parent " , " integer " , " 10 " , " unsigned " );
table_column ( " course_categories " , " " , " sortorder " , " integer " , " 10 " , " unsigned " );
table_column ( " course_categories " , " " , " courseorder " , " text " , " " , " " , " " );
table_column ( " course_categories " , " " , " visible " , " integer " , " 1 " , " unsigned " , " 1 " );
table_column ( " course_categories " , " " , " timemodified " , " integer " , " 10 " , " unsigned " );
}
2003-08-07 16:01:31 +00:00
if ( $oldversion < 2003080400 ) {
2003-09-14 12:22:13 +00:00
notify ( " If the following command fails you may want to change the type manually, from TEXT to INTEGER. Moodle should keep working even if you don't. " );
2003-08-07 16:01:31 +00:00
table_column ( " course_categories " , " courseorder " , " courseorder " , " integer " , " 10 " , " unsigned " );
table_column ( " course " , " " , " sortorder " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " category " );
}
2003-08-15 14:17:15 +00:00
if ( $oldversion < 2003081502 ) {
execute_sql ( " CREATE TABLE { $CFG -> prefix } scale (
id SERIAL PRIMARY KEY ,
courseid integer NOT NULL default '0' ,
userid integer NOT NULL default '0' ,
name varchar ( 255 ) NOT NULL default '' ,
scale text ,
description text ,
timemodified integer NOT NULL default '0'
) " );
}
2003-08-15 14:30:30 +00:00
if ( $oldversion < 2003081503 ) {
2003-08-16 02:30:23 +00:00
table_column ( " forum " , " " , " scale " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " assessed " );
2003-08-15 14:30:30 +00:00
get_scales_menu ( 0 ); // Just to force the default scale to be created
}
2003-08-16 05:19:24 +00:00
if ( $oldversion < 2003081600 ) {
table_column ( " user_teachers " , " " , " editall " , " integer " , " 1 " , " unsigned " , " 1 " , " " , " role " );
table_column ( " user_teachers " , " " , " timemodified " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " editall " );
}
2003-08-10 14:36:52 +00:00
2003-08-19 09:26:36 +00:00
if ( $oldversion < 2003081900 ) {
table_column ( " course_categories " , " courseorder " , " coursecount " , " integer " , " 10 " , " unsigned " , " 0 " );
}
2004-01-14 18:13:26 +00:00
if ( $oldversion < 2003080700 ) {
notify ( " Cleaning up categories and course ordering... " );
if ( $categories = get_categories ()) {
foreach ( $categories as $category ) {
fix_course_sortorder ( $category -> id );
}
}
}
2003-08-20 07:32:24 +00:00
if ( $oldversion < 2003082001 ) {
table_column ( " course " , " " , " showgrades " , " integer " , " 2 " , " unsigned " , " 1 " , " " , " format " );
}
2003-08-21 08:14:56 +00:00
if ( $oldversion < 2003082101 ) {
execute_sql ( " CREATE INDEX { $CFG -> prefix } course_category_idx ON { $CFG -> prefix } course (category) " );
}
2003-08-27 09:33:12 +00:00
if ( $oldversion < 2003082702 ) {
execute_sql ( " INSERT INTO { $CFG -> prefix } log_display (module, action, mtable, field) VALUES ('course', 'user report', 'user', 'CONCAT(firstname, \" \" ,lastname)') " );
}
2003-08-21 08:14:56 +00:00
2003-09-10 13:48:19 +00:00
if ( $oldversion < 2003091000 ) {
# Old field that was never added!
table_column ( " course " , " " , " showrecent " , " integer " , " 10 " , " unsigned " , " 1 " , " " , " numsections " );
}
2003-09-14 16:31:33 +00:00
if ( $oldversion < 2003091400 ) {
table_column ( " course_modules " , " " , " indent " , " integer " , " 5 " , " unsigned " , " 0 " , " " , " score " );
}
2003-10-06 18:02:35 +00:00
if ( $oldversion < 2003092900 ) {
table_column ( " course " , " " , " maxbytes " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " marker " );
}
2003-09-14 16:31:33 +00:00
2003-10-27 12:38:00 +00:00
if ( $oldversion < 2003102700 ) {
table_column ( " user_students " , " " , " timeaccess " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " time " );
table_column ( " user_teachers " , " " , " timeaccess " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " timemodified " );
$db -> debug = false ;
2003-10-27 13:38:08 +00:00
$CFG -> debug = 0 ;
notify ( " Calculating access times. Please wait - this may take a long time on big sites... " , " green " );
flush ();
if ( $courses = get_records_select ( " course " , " category > 0 " )) {
foreach ( $courses as $course ) {
notify ( " Processing $course->fullname ... " , " green " );
flush ();
if ( $users = get_records_select ( " user_teachers " , " course = ' $course->id ' " ,
" id " , " id, userid, timeaccess " )) {
foreach ( $users as $user ) {
$loginfo = get_record_sql ( " SELECT id, time FROM { $CFG -> prefix } log WHERE course = ' $course->id ' and userid = ' $user->userid ' ORDER by time DESC " );
if ( empty ( $loginfo -> time )) {
$loginfo -> time = 0 ;
}
execute_sql ( " UPDATE { $CFG -> prefix } user_teachers SET timeaccess = ' $loginfo->time '
WHERE userid = '$user->userid' AND course = '$course->id' " , false);
}
}
if ( $users = get_records_select ( " user_students " , " course = ' $course->id ' " ,
" id " , " id, userid, timeaccess " )) {
foreach ( $users as $user ) {
$loginfo = get_record_sql ( " SELECT id, time FROM { $CFG -> prefix } log
WHERE course = '$course->id' and userid = '$user->userid'
ORDER by time DESC " );
if ( empty ( $loginfo -> time )) {
$loginfo -> time = 0 ;
}
execute_sql ( " UPDATE { $CFG -> prefix } user_students
SET timeaccess = '$loginfo->time'
WHERE userid = '$user->userid' AND course = '$course->id' " , false);
}
}
}
2003-10-27 12:38:00 +00:00
}
2003-10-27 13:38:08 +00:00
notify ( " All courses complete. " , " green " );
2003-10-27 12:38:00 +00:00
$db -> debug = true ;
}
2003-10-31 05:57:30 +00:00
if ( $oldversion < 2003103100 ) {
table_column ( " course " , " " , " showreports " , " integer " , " 4 " , " unsigned " , " 0 " , " " , " maxbytes " );
}
2003-10-27 12:38:00 +00:00
2003-12-30 17:18:06 +00:00
if ( $oldversion < 2003121600 ) {
2003-12-30 18:07:09 +00:00
execute_sql ( " CREATE TABLE { $CFG -> prefix } groups (
2003-12-30 17:18:06 +00:00
id SERIAL PRIMARY KEY ,
courseid integer NOT NULL default '0' ,
name varchar ( 255 ) NOT NULL default '' ,
description text ,
lang varchar ( 10 ) NOT NULL default '' ,
picture integer NOT NULL default '0' ,
timecreated integer NOT NULL default '0' ,
timemodified integer NOT NULL default '0'
) " );
2003-12-30 18:07:09 +00:00
execute_sql ( " CREATE INDEX { $CFG -> prefix } groups_idx ON { $CFG -> prefix } groups (courseid) " );
2003-12-30 17:18:06 +00:00
2003-12-30 18:07:09 +00:00
execute_sql ( " CREATE TABLE { $CFG -> prefix } groups_members (
2003-12-30 17:18:06 +00:00
id SERIAL PRIMARY KEY ,
groupid integer NOT NULL default '0' ,
userid integer NOT NULL default '0' ,
timeadded integer NOT NULL default '0'
) " );
2003-12-30 18:07:09 +00:00
execute_sql ( " CREATE INDEX { $CFG -> prefix } groups_members_idx ON { $CFG -> prefix } groups_members (groupid) " );
2003-12-30 17:18:06 +00:00
}
if ( $oldversion < 2003122600 ) {
table_column ( " course " , " " , " groupmode " , " integer " , " 4 " , " unsigned " , " 0 " , " " , " visible " );
table_column ( " course " , " " , " groupmodeforce " , " integer " , " 4 " , " unsigned " , " 0 " , " " , " groupmode " );
}
2004-01-11 17:44:09 +00:00
if ( $oldversion < 2004010900 ) {
table_column ( " course_modules " , " " , " groupmode " , " integer " , " 4 " , " unsigned " , " 0 " , " " , " visible " );
}
2004-01-17 09:47:45 +00:00
if ( $oldversion < 2004011700 ) {
modify_database ( " " , " CREATE TABLE prefix_event (
id SERIAL PRIMARY KEY ,
name varchar ( 255 ) NOT NULL default '' ,
description text ,
courseid integer NOT NULL default '0' ,
groupid integer NOT NULL default '0' ,
userid integer NOT NULL default '0' ,
modulename varchar ( 20 ) NOT NULL default '' ,
instance integer NOT NULL default '0' ,
eventtype varchar ( 20 ) NOT NULL default '' ,
timestart integer NOT NULL default '0' ,
timeduration integer NOT NULL default '0' ,
timemodified integer NOT NULL default '0'
); " );
modify_database ( " " , " CREATE INDEX prefix_event_courseid_idx ON prefix_event (courseid); " );
modify_database ( " " , " CREATE INDEX prefix_event_userid_idx ON prefix_event (userid); " );
}
2004-01-28 14:14:19 +00:00
if ( $oldversion < 2004012800 ) {
modify_database ( " " , " CREATE TABLE prefix_user_preferences (
id SERIAL PRIMARY KEY ,
userid integer NOT NULL default '0' ,
name varchar ( 50 ) NOT NULL default '' ,
value varchar ( 255 ) NOT NULL default ''
); " );
modify_database ( " " , " CREATE INDEX prefix_user_preferences_useridname_idx ON prefix_user_preferences (userid,name); " );
}
2004-01-29 04:13:55 +00:00
if ( $oldversion < 2004012900 ) {
table_column ( " config " , " value " , " value " , " text " , " " , " " , " " );
}
2004-01-31 14:47:57 +00:00
if ( $oldversion < 2004013101 ) {
table_column ( " log " , " " , " cmid " , " integer " , " 10 " , " unsigned " , " 0 " , " " , " module " );
2004-02-05 09:55:50 +00:00
set_config ( " upgrade " , " logs " );
2004-01-31 14:47:57 +00:00
}
2004-02-03 04:56:22 +00:00
2004-02-09 07:31:04 +00:00
if ( $oldversion < 2004020900 ) {
table_column ( " course " , " " , " lang " , " varchar " , " 5 " , " " , " " , " " , " groupmodeforce " );
}
2004-02-09 13:10:19 +00:00
if ( $oldversion < 2004020903 ) {
modify_database ( " " , " CREATE TABLE prefix_cache_text (
2004-02-09 12:27:34 +00:00
id SERIAL PRIMARY KEY ,
md5key varchar ( 32 ) NOT NULL default '' ,
formattedtext text ,
timemodified integer NOT NULL default '0'
); " );
}
2004-02-10 05:05:28 +00:00
if ( $oldversion < 2004021000 ) {
$textfilters = array ();
for ( $i = 1 ; $i <= 10 ; $i ++ ) {
$variable = " textfilter $i " ;
if ( ! empty ( $CFG -> $variable )) { /// No more filters
if ( is_readable ( " $CFG->dirroot / " . $CFG -> $variable )) {
$textfilters [] = $CFG -> $variable ;
}
}
}
$textfilters = implode ( ',' , $textfilters );
if ( empty ( $textfilters )) {
$textfilters = 'mod/glossary/dynalink.php' ;
}
set_config ( 'textfilters' , $textfilters );
}
2004-02-12 07:06:07 +00:00
if ( $oldversion < 2004021201 ) {
modify_database ( " " , " CREATE TABLE prefix_cache_filters (
id SERIAL PRIMARY KEY ,
filter varchar ( 32 ) NOT NULL default '' ,
version integer NOT NULL default '0' ,
md5key varchar ( 32 ) NOT NULL default '' ,
rawtext text ,
timemodified integer NOT NULL default '0'
); " );
modify_database ( " " , " CREATE INDEX prefix_cache_filters_filtermd5key_idx ON prefix_cache_filters (filter,md5key); " );
modify_database ( " " , " CREATE INDEX prefix_cache_text_md5key_idx ON prefix_cache_text (md5key); " );
}
2004-02-10 05:05:28 +00:00
2004-02-15 07:29:55 +00:00
if ( $oldversion < 2004021500 ) {
table_column ( " groups " , " " , " hidepicture " , " integer " , " 2 " , " unsigned " , " 0 " , " " , " picture " );
}
2004-02-17 05:28:52 +00:00
if ( $oldversion < 2004021700 ) {
if ( ! empty ( $CFG -> textfilters )) {
$CFG -> textfilters = str_replace ( " tex_filter.php " , " filter.php " , $CFG -> textfilters );
$CFG -> textfilters = str_replace ( " multilang.php " , " filter.php " , $CFG -> textfilters );
$CFG -> textfilters = str_replace ( " censor.php " , " filter.php " , $CFG -> textfilters );
$CFG -> textfilters = str_replace ( " mediaplugin.php " , " filter.php " , $CFG -> textfilters );
$CFG -> textfilters = str_replace ( " algebra_filter.php " , " filter.php " , $CFG -> textfilters );
set_config ( " textfilters " , $CFG -> textfilters );
}
}
2003-04-24 18:48:09 +00:00
return $result ;
2004-02-03 04:56:22 +00:00
2003-04-24 18:48:09 +00:00
}
2003-12-30 17:18:06 +00:00
2004-02-03 04:56:22 +00:00
?>