Merge from MOODLE_14_STABLE.

Student and teacher enrolments now unique + version bump

Made student and teacher enrolments unique, enforced at the database level. This patch also removes a cleanup job in moodle's cron that removed duplicate enrolments.

Merge of patchset  arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-422
This commit is contained in:
martinlanghoff 2004-11-24 02:25:36 +00:00
parent a56f0d607b
commit b110348b71
6 changed files with 71 additions and 18 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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';

View File

@ -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;
}

View File

@ -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);

View File

@ -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