Permalink
Browse files

Student and teacher enrolments now unique

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.
The database changes are rolled as part of a version bump that we are *deferring* on MOODLE_14_STABLE.

Merge of patchset  arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-422
  • Loading branch information...
martinlanghoff
martinlanghoff committed Nov 24, 2004
1 parent 1a0c7e4 commit 7a2596f854cf8229a452e448f12e06091987fd77
Showing with 63 additions and 18 deletions.
  1. +0 −13 admin/cron.php
  2. +30 −0 lib/db/mysql.php
  3. +2 −2 lib/db/mysql.sql
  4. +29 −1 lib/db/postgres7.php
  5. +2 −2 lib/db/postgres7.sql
View
@@ -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
@@ -929,6 +929,36 @@ function main_upgrade($oldversion=0) {
}
+ if ($oldversion < 2004083130) {
+ /// 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
@@ -416,7 +416,7 @@ CREATE TABLE `prefix_user_students` (
`timeaccess` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
- KEY `courseuserid` (course,userid),
+ UNIQUE KEY `courseuserid` (course,userid),
KEY `userid` (userid),
KEY `timeaccess` (timeaccess)
) TYPE=MyISAM;
@@ -439,7 +439,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
@@ -666,7 +666,35 @@ function main_upgrade($oldversion=0) {
if ($oldversion < 2004083130) { // update log display to use correct postgres friendly sql
execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='user' AND action='view' AND mtable='user'");
execute_sql("UPDATE {$CFG->prefix}log_display SET field='firstname||\' \'||lastname' WHERE module='course' AND action='user report' AND mtable='user'");
- }
+
+ /// 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('','DROP INDEX prefix_user_students_courseuserid_idx;');
+ 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();
+
+ modify_database('','DROP INDEX prefix_user_teachers_courseuserid_idx;');
+ modify_database('','CREATE UNIQUE INDEX prefix_user_teachers_courseuserid_uk ON prefix_user_teachers (course,userid);');
+ }
return $result;
}
View
@@ -289,7 +289,7 @@ CREATE TABLE prefix_user_students (
timeaccess integer NOT NULL default '0'
);
-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_timeaccess_idx ON prefix_user_students (timeaccess);
@@ -306,7 +306,7 @@ CREATE TABLE prefix_user_teachers (
timeaccess integer NOT NULL default '0'
);
-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 TABLE prefix_user_coursecreators (

0 comments on commit 7a2596f

Please sign in to comment.