From d01030946a04a55fc7cffddb7bc838cd5ad79d97 Mon Sep 17 00:00:00 2001 From: Gordon Bateson Date: Sun, 9 Feb 2014 15:05:20 +0900 Subject: [PATCH 1/2] CONTRIB-4868 fix duplicate records found warning in courses with meta enrolments --- locallib.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/locallib.php b/locallib.php index 8d4ff15..288b979 100644 --- a/locallib.php +++ b/locallib.php @@ -916,7 +916,7 @@ class attendance { global $DB, $CFG; // Fields we need from the user table. - $userfields = user_picture::fields('u').',u.username'; + $userfields = user_picture::fields('u', array('username')); if (isset($this->pageparams->sort) and ($this->pageparams->sort == ATT_SORT_FIRSTNAME)) { $orderby = "u.firstname ASC, u.lastname ASC"; @@ -959,23 +959,29 @@ class attendance { // Add a flag to each user indicating whether their enrolment is active. if (!empty($users)) { - list($usql, $uparams) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED, 'usid0'); + list($sql, $params) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED, 'usid0'); - // CONTRIB-3549. - $sql = "SELECT ue.userid, ue.status, ue.timestart, ue.timeend + // CONTRIB-4868 + $mintime = 'MIN(CASE WHEN (ue.timestart > :mintime) THEN ue.timestart ELSE ue.timecreated END)'; + $maxtime = 'MAX(CASE WHEN (ue.timeend > :maxtime) THEN ue.timeend ELSE ue.timemodified END)'; + + // CONTRIB-3549 + $sql = "SELECT ue.userid, ue.status, + $mintime AS mintime, + $maxtime AS maxtime FROM {user_enrolments} ue JOIN {enrol} e ON e.id = ue.enrolid - WHERE ue.userid $usql + WHERE ue.userid $sql AND e.status = :estatus AND e.courseid = :courseid - GROUP BY ue.userid, ue.status, ue.timestart, ue.timeend"; - $params = array_merge($uparams, array('estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id)); - $enrolmentsparams = $DB->get_records_sql($sql, $params); + GROUP BY ue.userid, ue.status"; + $params += array('mintime'=>0, 'maxtime'=>0, 'estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id); + $enrolments = $DB->get_records_sql($sql, $params); foreach ($users as $user) { - $users[$user->id]->enrolmentstatus = $enrolmentsparams[$user->id]->status; - $users[$user->id]->enrolmentstart = $enrolmentsparams[$user->id]->timestart; - $users[$user->id]->enrolmentend = $enrolmentsparams[$user->id]->timeend; + $users[$user->id]->enrolmentstatus = $enrolments[$user->id]->status; + $users[$user->id]->enrolmentstart = $enrolments[$user->id]->mintime; + $users[$user->id]->enrolmentend = $enrolments[$user->id]->maxtime; } } From 8681bb2133bf014b99681bc87a2cfe0dcb1c8ba1 Mon Sep 17 00:00:00 2001 From: Gordon Bateson Date: Mon, 10 Feb 2014 08:45:02 +0900 Subject: [PATCH 2/2] CONTRIB-4868 (1) do not set maxtime to timemodified; (2) use mintime and maxtime in get_user() function too --- locallib.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/locallib.php b/locallib.php index 288b979..11b73ff 100644 --- a/locallib.php +++ b/locallib.php @@ -936,10 +936,10 @@ class attendance { $users = get_users_by_capability($this->context, 'mod/attendance:canbelisted', $userfields.',u.id, u.firstname, u.lastname, u.email', $orderby, $startusers, $usersperpage, $groups, - '', false, true); + '', false, true); } else { $startusers = ($page - 1) * $usersperpage; - $users = get_enrolled_users($this->context, 'mod/attendance:canbelisted', $groupid, $userfields, $orderby, $startusers, $usersperpage); + $users = get_enrolled_users($this->context, 'mod/attendance:canbelisted', $groupid, $userfields, $orderby, $startusers, $usersperpage); } } else { if (!empty($CFG->enablegroupmembersonly) and $this->cm->groupmembersonly) { @@ -962,8 +962,8 @@ class attendance { list($sql, $params) = $DB->get_in_or_equal(array_keys($users), SQL_PARAMS_NAMED, 'usid0'); // CONTRIB-4868 - $mintime = 'MIN(CASE WHEN (ue.timestart > :mintime) THEN ue.timestart ELSE ue.timecreated END)'; - $maxtime = 'MAX(CASE WHEN (ue.timeend > :maxtime) THEN ue.timeend ELSE ue.timemodified END)'; + $mintime = 'MIN(CASE WHEN (ue.timestart > :zerotime) THEN ue.timestart ELSE ue.timecreated END)'; + $maxtime = 'MAX(ue.timeend)'; // CONTRIB-3549 $sql = "SELECT ue.userid, ue.status, @@ -975,7 +975,7 @@ class attendance { AND e.status = :estatus AND e.courseid = :courseid GROUP BY ue.userid, ue.status"; - $params += array('mintime'=>0, 'maxtime'=>0, 'estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id); + $params += array('zerotime'=>0, 'estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id); $enrolments = $DB->get_records_sql($sql, $params); foreach ($users as $user) { @@ -993,19 +993,25 @@ class attendance { $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); - $sql = "SELECT ue.userid, ue.status, ue.timestart, ue.timeend + // CONTRIB-4868 + $mintime = 'MIN(CASE WHEN (ue.timestart > :zerotime) THEN ue.timestart ELSE ue.timecreated END)'; + $maxtime = 'MAX(ue.timeend)'; + + $sql = "SELECT ue.userid, ue.status, + $mintime AS mintime, + $maxtime AS maxtime FROM {user_enrolments} ue JOIN {enrol} e ON e.id = ue.enrolid WHERE ue.userid = :uid AND e.status = :estatus AND e.courseid = :courseid - GROUP BY ue.userid, ue.status, ue.timestart, ue.timeend"; - $params = array('uid' => $userid, 'estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id); - $enrolmentsparams = $DB->get_record_sql($sql, $params); + GROUP BY ue.userid, ue.status"; + $params = array('zerotime'=>0, 'uid'=>$userid, 'estatus'=>ENROL_INSTANCE_ENABLED, 'courseid'=>$this->course->id); + $enrolments = $DB->get_record_sql($sql, $params); - $user->enrolmentstatus = $enrolmentsparams->status; - $user->enrolmentstart = $enrolmentsparams->timestart; - $user->enrolmentend = $enrolmentsparams->timeend; + $user->enrolmentstatus = $enrolments->status; + $user->enrolmentstart = $enrolments->mintime; + $user->enrolmentend = $enrolments->maxtime; return $user; }