From 7ef4a5218835fb7584677d67ccfe8cda4fcaf816 Mon Sep 17 00:00:00 2001 From: NeillM Date: Mon, 16 Sep 2013 09:25:50 +0100 Subject: [PATCH 1/2] Revert "Fix for sessions with taken attendance show twice in student report." This reverts commit 5bddb944c0b912ef3f0bc74ff3d99dfae3be1300. --- locallib.php | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/locallib.php b/locallib.php index a772da0..5d50ff6 100644 --- a/locallib.php +++ b/locallib.php @@ -1128,47 +1128,51 @@ class attendance { // All taked sessions (including previous groups). - $groups = array_keys(groups_get_all_groups($this->course->id, $userid)); - $groups[] = 0; - list($gsql, $gparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'gid0'); - if ($this->pageparams->startdate && $this->pageparams->enddate) { $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND ats.sessdate >= :sdate AND ats.sessdate < :edate"; - $where2 = "ats.attendanceid = :aid2 AND ats.sessdate >= :csdate2 AND - ats.sessdate >= :sdate2 AND ats.sessdate < :edate2 AND ats.groupid $gsql"; } else { $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate"; - $where2 = "ats.attendanceid = :aid2 AND ats.sessdate >= :csdate2 AND ats.groupid $gsql"; } $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks FROM {attendance_sessions} ats - RIGHT JOIN {attendance_log} al + RIGHT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid WHERE $where - UNION - SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks - FROM {attendance_sessions} ats - LEFT JOIN {attendance_log} al - ON ats.id = al.sessionid AND al.studentid = :uid2 - WHERE $where2 - ORDER BY sessdate ASC"; + ORDER BY ats.sessdate ASC"; $params = array( 'uid' => $userid, 'aid' => $this->id, 'csdate' => $this->course->startdate, 'sdate' => $this->pageparams->startdate, - 'edate' => $this->pageparams->enddate, - 'uid2' => $userid, - 'aid2' => $this->id, - 'csdate2' => $this->course->startdate, - 'sdate2' => $this->pageparams->startdate, - 'edate2' => $this->pageparams->enddate); - $params = array_merge($params, $gparams); + 'edate' => $this->pageparams->enddate); $sessions = $DB->get_records_sql($sql, $params); + // All sessions for current groups. + + $groups = array_keys(groups_get_all_groups($this->course->id, $userid)); + $groups[] = 0; + list($gsql, $gparams) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED, 'gid0'); + + if ($this->pageparams->startdate && $this->pageparams->enddate) { + $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND + ats.sessdate >= :sdate AND ats.sessdate < :edate AND ats.groupid $gsql"; + } else { + $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND ats.groupid $gsql"; + } + + $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks + FROM {attendance_sessions} ats + LEFT JOIN {attendance_log} al + ON ats.id = al.sessionid AND al.studentid = :uid + WHERE $where + ORDER BY ats.sessdate ASC"; + + $params = array_merge($params, $gparams); + $sessions = array_merge($sessions, $DB->get_records_sql($sql, $params)); + foreach ($sessions as $sess) { if (empty($sess->description)) { $sess->description = get_string('nodescription', 'attendance'); From 47d04704c6470e3883bc6ffa835c343c78b0f6b8 Mon Sep 17 00:00:00 2001 From: NeillM Date: Mon, 16 Sep 2013 09:51:34 +0100 Subject: [PATCH 2/2] Duplicate fix that will work on MSSQL --- locallib.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index 5d50ff6..011c56c 100644 --- a/locallib.php +++ b/locallib.php @@ -1135,7 +1135,12 @@ class attendance { $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate"; } - $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks + // We need to add this concatination so that moodle will use it as the array index that is a string. + // If the array's index is a number it will not merge entries. + // It would be better as a UNION query butunfortunatly MS SQL does not seem to support doing a DISTINCT on a the description field. + $id = $DB->sql_concat(':value', 'ats.id'); + + $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks FROM {attendance_sessions} ats RIGHT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid @@ -1147,7 +1152,8 @@ class attendance { 'aid' => $this->id, 'csdate' => $this->course->startdate, 'sdate' => $this->pageparams->startdate, - 'edate' => $this->pageparams->enddate); + 'edate' => $this->pageparams->enddate, + 'value' => 'c'); $sessions = $DB->get_records_sql($sql, $params); // All sessions for current groups. @@ -1163,7 +1169,7 @@ class attendance { $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND ats.groupid $gsql"; } - $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks + $sql = "SELECT $id, ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks FROM {attendance_sessions} ats LEFT JOIN {attendance_log} al ON ats.id = al.sessionid AND al.studentid = :uid