diff --git a/admin/cron.php b/admin/cron.php index b6e6a81b91d..bf078167e73 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -99,19 +99,6 @@ } flush(); - - /// Delete duplicate enrolments (don't know what causes these yet - expired sessions?) - - if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ". - "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) { - foreach ($users as $user) { - delete_records_select("user_students", "userid = '$user->userid' ". - "AND course = '$user->course' AND id <> '$user->id'"); - } - } - flush(); - - /// Delete old logs to save space (this might need a timer to slow it down...) if (!empty($CFG->loglifetime)) { // value in days diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 20765236992..b570656c7e6 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -976,6 +976,35 @@ function main_upgrade($oldversion=0) { execute_sql("ALTER TABLE `{$CFG->prefix}user_teachers` ADD INDEX enrol (enrol);"); } + if ($oldversion < 2004112400) { + /// Delete duplicate enrolments + /// and then tell the database course,userid is a unique combination + if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ". + "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) { + foreach ($users as $user) { + delete_records_select("user_students", "userid = '$user->userid' ". + "AND course = '$user->course' AND id <> '$user->id'"); + } + } + flush(); + + modify_database('','ALTER TABLE prefix_user_students DROP INDEX courseuserid;'); + modify_database('','ALTER TABLE prefix_user_students ADD UNIQUE INDEX courseuserid(course,userid);'); + + /// Delete duplicate teacher enrolments + /// and then tell the database course,userid is a unique combination + if ($users = get_records_select("user_teachers", "userid > 0 GROUP BY course, userid ". + "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) { + foreach ($users as $user) { + delete_records_select("user_teachers", "userid = '$user->userid' ". + "AND course = '$user->course' AND id <> '$user->id'"); + } + } + flush(); + modify_database('','ALTER TABLE prefix_user_teachers DROP INDEX courseuserid;'); + modify_database('','ALTER TABLE prefix_user_teachers ADD UNIQUE INDEX courseuserid(course,userid);'); + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index e85a028e280..b511c12d98d 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -431,7 +431,7 @@ CREATE TABLE `prefix_user_students` ( `enrol` varchar(20) NOT NULL default '', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), - KEY `courseuserid` (course,userid), + UNIQUE KEY `courseuserid` (course,userid), KEY `userid` (userid), KEY `enrol` (enrol), KEY `timeaccess` (timeaccess) @@ -455,7 +455,7 @@ CREATE TABLE `prefix_user_teachers` ( `timeaccess` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), - KEY `courseuserid` (course,userid), + KEY UNIQUE `courseuserid` (course,userid), KEY `userid` (userid) ) TYPE=MyISAM COMMENT='One record per teacher per course'; diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index 3cc2ad51454..e2e454322ba 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -722,6 +722,43 @@ function main_upgrade($oldversion=0) { execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='course' AND action='user report' AND mtable='user'"); } + if ($oldversion < 2004112400) { + + /// Delete duplicate enrolments + /// and then tell the database course,userid is a unique combination + if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ". + "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) { + foreach ($users as $user) { + delete_records_select("user_students", "userid = '$user->userid' ". + "AND course = '$user->course' AND id <> '$user->id'"); + } + } + flush(); + + // drop some indexes quietly -- they may or may not exist depending on what version + // the user upgrades from + execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_idx ", false); + execute_sql("DROP INDEX {$CFG->prefix}user_students_courseuserid_uk ", false); + modify_database('','CREATE UNIQUE INDEX prefix_user_students_courseuserid_uk ON prefix_user_students (course,userid);'); + + /// Delete duplicate teacher enrolments + /// and then tell the database course,userid is a unique combination + if ($users = get_records_select("user_teachers", "userid > 0 GROUP BY course, userid ". + "HAVING count(*) > 1", "", "max(id) as id, userid, course ,count(*)")) { + foreach ($users as $user) { + delete_records_select("user_teachers", "userid = '$user->userid' ". + "AND course = '$user->course' AND id <> '$user->id'"); + } + } + flush(); + + // drop some indexes quietly -- they may or may not exist depending on what version + // the user upgrades from + execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_idx ", false); + execute_sql("DROP INDEX {$CFG->prefix}user_teachers_courseuserid_uk ", false); + modify_database('','CREATE UNIQUE INDEX prefix_user_teachers_courseuserid_uk ON prefix_user_teachers (course,userid);'); + } + return $result; } diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 05da57a4715..824de0bf3f9 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -297,7 +297,7 @@ CREATE TABLE prefix_user_students ( enrol varchar (20) NOT NULL default '' ); -CREATE INDEX prefix_user_students_courseuserid_idx ON prefix_user_students (course,userid); +CREATE UNIQUE INDEX prefix_user_students_courseuserid_uk ON prefix_user_students (course,userid); CREATE INDEX prefix_user_students_userid_idx ON prefix_user_students (userid); CREATE INDEX prefix_user_students_enrol_idx ON prefix_user_students (enrol); @@ -315,7 +315,7 @@ CREATE TABLE prefix_user_teachers ( enrol varchar (20) NOT NULL default '' ); -CREATE INDEX prefix_user_teachers_courseuserid_idx ON prefix_user_teachers (course,userid); +CREATE UNIQUE INDEX prefix_user_teachers_courseuserid_uk ON prefix_user_teachers (course,userid); CREATE INDEX prefix_user_teachers_userid_idx ON prefix_user_teachers (userid); CREATE INDEX prefix_user_teachers_enrol_idx ON prefix_user_teachers (enrol); diff --git a/version.php b/version.php index 15e517693c9..47286317b13 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2004112300; // YYYYMMDD = date of first major branch release 1.4 + $version = 2004112400; // YYYYMMDD = date of first major branch release 1.4 // XY = increments within a single day $release = '1.5 UNSTABLE DEVELOPMENT'; // Human-friendly version name