From 350d6f076abc2edad8db68a478d8d05464328f19 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Mon, 16 Dec 2013 12:35:24 +1300 Subject: [PATCH] Attendance - only show groups in reports that user is a member of. --- lib.php | 7 +++---- locallib.php | 52 ++++++++++++++++++++++++++++++++++++----------- preferences.php | 2 +- renderables.php | 4 ++-- renderhelpers.php | 8 ++++---- sessions.php | 4 ++-- 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/lib.php b/lib.php index a13f2fe..737d5e7 100644 --- a/lib.php +++ b/lib.php @@ -213,7 +213,6 @@ function attendance_reset_userdata($data) { */ function attendance_user_outline($course, $user, $mod, $attendance) { global $CFG; - require_once(dirname(__FILE__).'/locallib.php'); require_once($CFG->libdir.'/gradelib.php'); @@ -229,9 +228,9 @@ function attendance_user_outline($course, $user, $mod, $attendance) { if (has_capability('mod/attendance:canbelisted', $mod->context, $user->id)) { $statuses = att_get_statuses($attendance->id); $grade = att_get_user_grade(att_get_user_statuses_stat($attendance->id, $course->startdate, - $user->id), $statuses); + $user->id, $mod), $statuses); $maxgrade = att_get_user_max_grade(att_get_user_taken_sessions_count($attendance->id, $course->startdate, - $user->id), $statuses); + $user->id, $mod), $statuses); $result->info = $grade.' / '.$maxgrade; } @@ -250,7 +249,7 @@ function attendance_user_complete($course, $user, $mod, $attendance) { require_once($CFG->libdir.'/gradelib.php'); if (has_capability('mod/attendance:canbelisted', $mod->context, $user->id)) { - echo construct_full_user_stat_html_table($attendance, $course, $user); + echo construct_full_user_stat_html_table($attendance, $course, $user, $mod); } } function attendance_print_recent_activity($course, $isteacher, $timestart) { diff --git a/locallib.php b/locallib.php index de63376..40c7a85 100644 --- a/locallib.php +++ b/locallib.php @@ -1061,7 +1061,7 @@ class attendance { public function get_user_taken_sessions_count($userid) { if (!array_key_exists($userid, $this->usertakensesscount)) { - $this->usertakensesscount[$userid] = att_get_user_taken_sessions_count($this->id, $this->course->startdate, $userid); + $this->usertakensesscount[$userid] = att_get_user_taken_sessions_count($this->id, $this->course->startdate, $userid, $this->cm); } return $this->usertakensesscount[$userid]; } @@ -1179,7 +1179,6 @@ class attendance { public function get_user_filtered_sessions_log_extended($userid) { global $DB; - // All taked sessions (including previous groups). if ($this->pageparams->startdate && $this->pageparams->enddate) { @@ -1193,13 +1192,22 @@ class attendance { // 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 + if (!empty($this->cm->groupmode)) { + $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 + JOIN {groups_members} gm ON gm.userid = al.studentid AND gm.groupid = ats.groupid WHERE $where ORDER BY ats.sessdate ASC"; + } else { + $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 + WHERE $where + ORDER BY ats.sessdate ASC"; + } $params = array( 'uid' => $userid, @@ -1339,16 +1347,25 @@ function att_get_statuses($attid, $onlyvisible=true) { return $statuses; } -function att_get_user_taken_sessions_count($attid, $coursestartdate, $userid) { +function att_get_user_taken_sessions_count($attid, $coursestartdate, $userid, $coursemodule) { global $DB; - - $qry = "SELECT count(*) as cnt + if (!empty($coursemodule->groupmode)) { + $qry = "SELECT count(*) as cnt + FROM {attendance_log} al + JOIN {attendance_sessions} ats ON al.sessionid = ats.id + JOIN {groups_members} gm ON gm.userid = al.studentid AND gm.groupid = ats.groupid + WHERE ats.attendanceid = :aid AND + ats.sessdate >= :cstartdate AND + al.studentid = :uid"; + } else { + $qry = "SELECT count(*) as cnt FROM {attendance_log} al JOIN {attendance_sessions} ats ON al.sessionid = ats.id WHERE ats.attendanceid = :aid AND ats.sessdate >= :cstartdate AND al.studentid = :uid"; + } $params = array( 'aid' => $attid, 'cstartdate' => $coursestartdate, @@ -1357,10 +1374,20 @@ function att_get_user_taken_sessions_count($attid, $coursestartdate, $userid) { return $DB->count_records_sql($qry, $params); } -function att_get_user_statuses_stat($attid, $coursestartdate, $userid) { +function att_get_user_statuses_stat($attid, $coursestartdate, $userid, $coursemodule) { global $DB; - $qry = "SELECT al.statusid, count(al.statusid) AS stcnt + if (!empty($coursemodule->groupmode)) { + $qry = "SELECT al.statusid, count(al.statusid) AS stcnt + FROM {attendance_log} al + JOIN {attendance_sessions} ats ON al.sessionid = ats.id + JOIN {groups_members} gm ON gm.userid = al.studentid AND gm.groupid = ats.groupid + WHERE ats.attendanceid = :aid AND + ats.sessdate >= :cstartdate AND + al.studentid = :uid + GROUP BY al.statusid"; + } else { + $qry = "SELECT al.statusid, count(al.statusid) AS stcnt FROM {attendance_log} al JOIN {attendance_sessions} ats ON al.sessionid = ats.id @@ -1368,6 +1395,7 @@ function att_get_user_statuses_stat($attid, $coursestartdate, $userid) { ats.sessdate >= :cstartdate AND al.studentid = :uid GROUP BY al.statusid"; + } $params = array( 'aid' => $attid, 'cstartdate' => $coursestartdate, @@ -1424,7 +1452,7 @@ function att_get_gradebook_maxgrade($attid) { return $DB->get_field('attendance', 'grade', array('id' => $attid)); } -function att_update_all_users_grades($attid, $course, $context) { +function att_update_all_users_grades($attid, $course, $context, $coursemodule) { $grades = array(); $userids = array_keys(get_enrolled_users($context, 'mod/attendance:canbelisted', 0, 'u.id')); @@ -1434,8 +1462,8 @@ function att_update_all_users_grades($attid, $course, $context) { foreach ($userids as $userid) { $grade = new stdClass; $grade->userid = $userid; - $userstatusesstat = att_get_user_statuses_stat($attid, $course->startdate, $userid); - $usertakensesscount = att_get_user_taken_sessions_count($attid, $course->startdate, $userid); + $userstatusesstat = att_get_user_statuses_stat($attid, $course->startdate, $userid, $coursemodule); + $usertakensesscount = att_get_user_taken_sessions_count($attid, $course->startdate, $userid, $coursemodule); $usergrade = att_get_user_grade($userstatusesstat, $statuses); $usermaxgrade = att_get_user_max_grade($usertakensesscount, $statuses); $grade->rawgrade = att_calc_user_grade_fraction($usergrade, $usermaxgrade) * $gradebook_maxgrade; diff --git a/preferences.php b/preferences.php index b597958..37b8128 100644 --- a/preferences.php +++ b/preferences.php @@ -93,7 +93,7 @@ switch ($att->pageparams->action) { foreach ($acronym as $id => $v) { $att->update_status($id, $acronym[$id], $description[$id], $grade[$id], null); } - att_update_all_users_grades($att->id, $att->course, $att->context); + att_update_all_users_grades($att->id, $att->course, $att->context, $cm); break; } diff --git a/renderables.php b/renderables.php index af38c0f..373bdc0 100644 --- a/renderables.php +++ b/renderables.php @@ -385,8 +385,8 @@ class attendance_user_data implements renderable { $this->maxgrade = array(); foreach ($this->coursesatts as $ca) { $statuses = att_get_statuses($ca->attid); - $user_taken_sessions_count = att_get_user_taken_sessions_count($ca->attid, $ca->coursestartdate, $userid); - $user_statuses_stat = att_get_user_statuses_stat($ca->attid, $ca->coursestartdate, $userid); + $user_taken_sessions_count = att_get_user_taken_sessions_count($ca->attid, $ca->coursestartdate, $userid, $att->cm); + $user_statuses_stat = att_get_user_statuses_stat($ca->attid, $ca->coursestartdate, $userid, $att->cm); $this->statuses[$ca->attid] = $statuses; diff --git a/renderhelpers.php b/renderhelpers.php index a501537..15ab178 100644 --- a/renderhelpers.php +++ b/renderhelpers.php @@ -254,17 +254,17 @@ function construct_user_data_stat($stat, $statuses, $gradable, $grade, $maxgrade return html_writer::table($stattable); } -function construct_full_user_stat_html_table($attendance, $course, $user) { +function construct_full_user_stat_html_table($attendance, $course, $user, $coursemodule) { global $CFG; $gradeable = $attendance->grade > 0; $statuses = att_get_statuses($attendance->id); - $userstatusesstat = att_get_user_statuses_stat($attendance->id, $course->startdate, $user->id); - $stat['completed'] = att_get_user_taken_sessions_count($attendance->id, $course->startdate, $user->id); + $userstatusesstat = att_get_user_statuses_stat($attendance->id, $course->startdate, $user->id, $coursemodule); + $stat['completed'] = att_get_user_taken_sessions_count($attendance->id, $course->startdate, $user->id, $coursemodule); $stat['statuses'] = $userstatusesstat; if ($gradeable) { $grade = att_get_user_grade($userstatusesstat, $statuses); $maxgrade = att_get_user_max_grade(att_get_user_taken_sessions_count($attendance->id, $course->startdate, - $user->id), $statuses); + $user->id, $coursemodule), $statuses); if (!$decimalpoints = grade_get_setting($course->id, 'decimalpoints')) { $decimalpoints = $CFG->grade_decimalpoints; } diff --git a/sessions.php b/sessions.php index 046a596..b32e087 100644 --- a/sessions.php +++ b/sessions.php @@ -85,7 +85,7 @@ switch ($att->pageparams->action) { if (isset($confirm) && confirm_sesskey()) { $att->delete_sessions(array($sessionid)); - att_update_all_users_grades($att->id, $att->course, $att->context); + att_update_all_users_grades($att->id, $att->course, $att->context, $cm); redirect($att->url_manage(), get_string('sessiondeleted', 'attendance')); } @@ -112,7 +112,7 @@ switch ($att->pageparams->action) { $sessionsids = explode('_', $sessionsids); $att->delete_sessions($sessionsids); - att_update_all_users_grades($att->id, $att->course, $att->context); + att_update_all_users_grades($att->id, $att->course, $att->context, $cm); redirect($att->url_manage(), get_string('sessiondeleted', 'attendance')); } $sessid = required_param('sessid', PARAM_SEQUENCE);