Browse Source

Fix for sessions with taken attendance show twice in student report.

The two queries have been refactored into a single query using UNION
MOODLE_26_STABLE
NeillM 12 years ago
parent
commit
5bddb944c0
  1. 48
      locallib.php

48
locallib.php

@ -1101,50 +1101,46 @@ class attendance {
// All taked sessions (including previous groups). // 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) { if ($this->pageparams->startdate && $this->pageparams->enddate) {
$where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND $where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate AND
ats.sessdate >= :sdate AND ats.sessdate < :edate"; 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 { } else {
$where = "ats.attendanceid = :aid AND ats.sessdate >= :csdate"; $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 $sql = "SELECT ats.id, ats.groupid, ats.sessdate, ats.duration, ats.description, al.statusid, al.remarks
FROM {attendance_sessions} ats FROM {attendance_sessions} ats
RIGHT JOIN {attendance_log} al RIGHT JOIN {attendance_log} al
ON ats.id = al.sessionid AND al.studentid = :uid ON ats.id = al.sessionid AND al.studentid = :uid
WHERE $where WHERE $where
ORDER BY ats.sessdate ASC"; 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";
$params = array( $params = array(
'uid' => $userid, 'uid' => $userid,
'aid' => $this->id, 'aid' => $this->id,
'csdate' => $this->course->startdate, 'csdate' => $this->course->startdate,
'sdate' => $this->pageparams->startdate, 'sdate' => $this->pageparams->startdate,
'edate' => $this->pageparams->enddate); 'edate' => $this->pageparams->enddate,
$sessions = $DB->get_records_sql($sql, $params); 'uid2' => $userid,
'aid2' => $this->id,
// All sessions for current groups. 'csdate2' => $this->course->startdate,
'sdate2' => $this->pageparams->startdate,
$groups = array_keys(groups_get_all_groups($this->course->id, $userid)); 'edate2' => $this->pageparams->enddate);
$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); $params = array_merge($params, $gparams);
$sessions = array_merge($sessions, $DB->get_records_sql($sql, $params)); $sessions = $DB->get_records_sql($sql, $params);
foreach ($sessions as $sess) { foreach ($sessions as $sess) {
if (empty($sess->description)) { if (empty($sess->description)) {

Loading…
Cancel
Save