From cbaf375f62a07cd34299edc3fd6eacef51517d04 Mon Sep 17 00:00:00 2001 From: Artem Andreev Date: Sat, 18 Jun 2011 17:02:48 +0400 Subject: [PATCH] Implemented view.php --- locallib.php | 153 +++++++++++++++++++++++++++++------------------- renderables.php | 41 +++++++++++-- renderer.php | 63 +++++++++++++------- view.php | 6 +- 4 files changed, 173 insertions(+), 90 deletions(-) diff --git a/locallib.php b/locallib.php index c7054a7..ed99fb3 100644 --- a/locallib.php +++ b/locallib.php @@ -194,7 +194,7 @@ class att_view_page_params extends att_page_with_filter_controls { const MODE_THIS_COURSE = 0; const MODE_ALL_COURSES = 1; - public $student; + public $studentid; public $mode; @@ -205,7 +205,7 @@ class att_view_page_params extends att_page_with_filter_controls { public function get_significant_params() { $params = array(); - if (isset($this->student)) $params['student'] = $this->student; + if (isset($this->studentid)) $params['studentid'] = $this->studentid; if ($this->mode != self::MODE_THIS_COURSE) $params['mode'] = $this->mode; return $params; @@ -605,13 +605,13 @@ class attforblock { } $this->sessgroupslist = array(); - if ($allowedgroups or $this->groupmode == VISIBLEGROUPS or $this->perm->can_access_all_groups()) { - $this->sessgroupslist[self::SELECTOR_ALL] = get_string('all', 'attforblock'); - } - if ($this->groupmode == VISIBLEGROUPS) { - $this->sessgroupslist[self::SELECTOR_COMMON] = get_string('commonsessions', 'attforblock'); - } if ($allowedgroups) { + if ($this->groupmode == VISIBLEGROUPS or $this->perm->can_access_all_groups()) { + $this->sessgroupslist[self::SELECTOR_ALL] = get_string('all', 'attforblock'); + } + if ($this->groupmode == VISIBLEGROUPS) { + $this->sessgroupslist[self::SELECTOR_COMMON] = get_string('commonsessions', 'attforblock'); + } foreach ($allowedgroups as $group) { $this->sessgroupslist[$group->id] = format_string($group->name); } @@ -802,11 +802,7 @@ class attforblock { global $DB; if (!isset($this->statuses)) { - if ($onlyvisible) { - $this->statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND visible = 1 AND deleted = 0", array('aid' => $this->id), 'grade DESC'); - } else { - $this->statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND deleted = 0", array('aid' => $this->id), 'grade DESC'); - } + $this->statuses = get_statuses($this->id, $onlyvisible); } return $this->statuses; @@ -839,24 +835,11 @@ class attforblock { return $ret; } - public function get_user_taken_sessions_count($userid, $courseid=NULL) { + public function get_user_taken_sessions_count($userid) { global $DB; - if (!isset($this->usertakensesscount)) { - $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' => $this->id, - 'cstartdate' => $this->course->startdate, - 'uid' => $userid); - - $this->usertakensesscount = $DB->count_records_sql($qry, $params); - } + if (!isset($this->usertakensesscount)) + $this->usertakensesscount = get_user_taken_sessions_count($this->id, $this->course->startdate, $userid); return $this->usertakensesscount; } @@ -884,16 +867,8 @@ class attforblock { return $this->userstatusesstat; } - public function get_user_grade($userid, $courseid=NULL) { - $statistics = $this->get_user_statuses_stat($userid, $courseid); - $statuses = $this->get_statuses($courseid); - - $sum = 0; - foreach ($statistics as $stat) { - $sum += $stat->stcnt * $statuses[$stat->statusid]->grade; - } - - return $sum; + public function get_user_grade($userid) { + return get_user_grade($this->get_user_statuses_stat($userid), $this->get_statuses()); } // For getting sessions count implemented simplest method - taken sessions. @@ -901,11 +876,8 @@ class attforblock { // In the future we can implement another methods: // * all sessions between user start enrolment date and now; // * all sessions between user start and end enrolment date. - public function get_user_max_grade($userid, $courseid=NULL) { - $takensessions = $this->get_user_taken_sessions_count($userid, $courseid); - $statuses = $this->get_statuses($courseid); - - return current($statuses)->grade * $takensessions; + public function get_user_max_grade($userid) { + return get_user_max_grade($this->get_user_taken_sessions_count($userid), $this->get_statuses()); } public function get_user_filtered_sessions_log($userid) { @@ -938,31 +910,90 @@ class attforblock { return $sessions; } +} - public function get_user_courses_with_attendance($userid) { - global $DB; - // we can't get user courses based only on attendance_log information - // because of then users will see only courses with taked attendance - $usercourses = enrol_get_users_courses($userid); +function get_statuses($attid, $onlyvisible=true) { + global $DB; - list($usql, $uparams) = $DB->get_in_or_equal(array_keys($usercourses), SQL_PARAMS_NAMED, 'cid0'); + if ($onlyvisible) { + $statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND visible = 1 AND deleted = 0", array('aid' => $attid), 'grade DESC'); + } else { + $statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND deleted = 0", array('aid' => $attid), 'grade DESC'); + } - $sql = "SELECT ats.courseid, course.fullname - FROM {attendance_sessions} ats - JOIN {attendance_log} al - ON ats.id = al.sessionid AND al.studentid = :uid - JOIN {course} course - ON ats.courseid = course.id - WHERE ats.courseid $usql - GROUP BY ats.courseid - ORDER BY course.fullname ASC"; + return $statuses; +} - $params = array_merge($uparams, array('uid' => $userid)); +function get_user_taken_sessions_count($attid, $coursestartdate, $userid) { + global $DB; + + $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, + 'uid' => $userid); + + return $DB->count_records_sql($qry, $params); +} - return $DB->get_records_sql($sql, $params); +function get_user_statuses_stat($attid, $coursestartdate, $userid) { + global $DB; + + $qry = "SELECT al.statusid, count(al.statusid) AS stcnt + 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 + GROUP BY al.statusid"; + $params = array( + 'aid' => $attid, + 'cstartdate' => $coursestartdate, + 'uid' => $userid); + + return $DB->get_records_sql($qry, $params); +} + +function get_user_grade($userstatusesstat, $statuses) { + $sum = 0; + foreach ($userstatusesstat as $stat) { + $sum += $stat->stcnt * $statuses[$stat->statusid]->grade; } + + return $sum; +} + +function get_user_max_grade($sesscount, $statuses) { + reset($statuses); + return current($statuses)->grade * $sesscount; } +function get_user_courses_attendances($userid) { + global $DB; + + $usercourses = enrol_get_users_courses($userid); + + list($usql, $uparams) = $DB->get_in_or_equal(array_keys($usercourses), SQL_PARAMS_NAMED, 'cid0'); + + $sql = "SELECT att.id as attid, att.course as courseid, course.fullname as coursefullname, + course.startdate as coursestartdate, att.name as attname, att.grade as attgrade + FROM {attforblock} att + JOIN {course} course + ON att.course = course.id + WHERE att.course $usql + ORDER BY coursefullname ASC, attname ASC"; + + $params = array_merge($uparams, array('uid' => $userid)); + + return $DB->get_records_sql($sql, $params); +} ?> diff --git a/renderables.php b/renderables.php index d16a3d1..a96b64c 100644 --- a/renderables.php +++ b/renderables.php @@ -324,7 +324,7 @@ class attforblock_user_data implements renderable { public $sessionslog; - public $courses; + public $coursesatts; private $urlpath; private $urlparams; @@ -336,13 +336,13 @@ class attforblock_user_data implements renderable { $this->pageparams = $att->pageparams; - $this->statuses = $att->get_statuses(); - if (!$this->decimalpoints = grade_get_setting($att->course->id, 'decimalpoints')) { $this->decimalpoints = $CFG->grade_decimalpoints; } if ($this->pageparams->mode == att_view_page_params::MODE_THIS_COURSE) { + $this->statuses = $att->get_statuses(); + $this->stat = $att->get_user_stat($userid); $this->gradable = $att->grade > 0; @@ -357,7 +357,40 @@ class attforblock_user_data implements renderable { $this->sessionslog = $att->get_user_filtered_sessions_log($userid); } else { - $this->courses = $att->get_user_courses_with_attendance($userid); + $this->coursesatts = get_user_courses_attendances($userid); + + $this->statuses = array(); + $this->stat = array(); + $this->gradable = array(); + $this->grade = array(); + $this->maxgrade = array(); + foreach ($this->coursesatts as $ca) { + $statuses = get_statuses($ca->attid); + $user_taken_sessions_count = get_user_taken_sessions_count($ca->attid, $ca->coursestartdate, $userid); + $user_statuses_stat = get_user_statuses_stat($ca->attid, $ca->coursestartdate, $userid); + + $this->statuses[$ca->attid] = $statuses; + + $this->stat[$ca->attid]['completed'] = $user_taken_sessions_count; + $this->stat[$ca->attid]['statuses'] = $user_statuses_stat; + + $this->gradable[$ca->attid] = $ca->attgrade > 0; + + if ($this->gradable[$ca->attid]) { + $this->grade[$ca->attid] = get_user_grade($user_statuses_stat, $statuses); + // For getting sessions count implemented simplest method - taken sessions. + // It can have error if users don't have attendance info for some sessions. + // In the future we can implement another methods: + // * all sessions between user start enrolment date and now; + // * all sessions between user start and end enrolment date. + $this->maxgrade[$ca->attid] = get_user_max_grade($user_taken_sessions_count, $statuses); + } + else { + //for more comfortable and universal work with arrays + $this->grade[$ca->attid] = NULL; + $this->maxgrade[$ca->attid] = NULL; + } + } } $this->urlpath = $att->url_view()->out_omit_querystring(); diff --git a/renderer.php b/renderer.php index 457748e..f9b55fa 100644 --- a/renderer.php +++ b/renderer.php @@ -61,17 +61,18 @@ class mod_attforblock_renderer extends plugin_renderer_base { protected function render_sess_group_selector(attforblock_filter_controls $fcontrols) { switch ($fcontrols->pageparams->selectortype) { case att_page_with_filter_controls::SELECTOR_SESS_TYPE: - $select = new single_select($fcontrols->url(), 'group', $fcontrols->get_sess_groups_list(), - $fcontrols->get_current_group(), null, 'selectgroup'); - $select->label = get_string('sessions', 'attforblock'); - $output = $this->output->render($select); - - return html_writer::tag('div', $output, array('class' => 'groupselector')); - - default: - return ''; + $sessgroups = $fcontrols->get_sess_groups_list(); + if ($sessgroups) { + $select = new single_select($fcontrols->url(), 'group', $sessgroups, + $fcontrols->get_current_group(), null, 'selectgroup'); + $select->label = get_string('sessions', 'attforblock'); + $output = $this->output->render($select); + + return html_writer::tag('div', $output, array('class' => 'groupselector')); + } } + return ''; } protected function render_curdate_controls(attforblock_filter_controls $fcontrols) { @@ -368,7 +369,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { $i++; $row = new html_table_row(); $row->cells[] = $i; - $fullname = html_writer::link($takedata->url_view(array('student' => $user->id)), fullname($user)); + $fullname = html_writer::link($takedata->url_view(array('studentid' => $user->id)), fullname($user)); $row->cells[] = $this->output->user_picture($user).$fullname; $celldata = $this->construct_take_user_controls($takedata, $user); @@ -407,7 +408,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { foreach($takedata->users as $user) { $celltext = $this->output->user_picture($user, array('size' => 100)); $celltext .= html_writer::empty_tag('br'); - $fullname = html_writer::link($takedata->url_view(array('student' => $user->id)), fullname($user)); + $fullname = html_writer::link($takedata->url_view(array('studentid' => $user->id)), fullname($user)); $celltext .= html_writer::tag('span', $fullname, array('class' => 'fullname')); $celltext .= html_writer::empty_tag('br'); $celldata = $this->construct_take_user_controls($takedata, $user); @@ -525,49 +526,67 @@ class mod_attforblock_renderer extends plugin_renderer_base { private function construct_user_data(attforblock_user_data $userdata) { $o = html_writer::tag('h2', fullname($userdata->user)); - $o .= html_writer::empty_tag('hr'); if ($userdata->pageparams->mode == att_view_page_params::MODE_THIS_COURSE) { - $o .= $this->construct_user_data_stat($userdata); + $o .= html_writer::empty_tag('hr'); + + $o .= $this->construct_user_data_stat($userdata->stat, $userdata->statuses, + $userdata->gradable, $userdata->grade, $userdata->maxgrade, $userdata->decimalpoints); $o .= $this->render_attforblock_filter_controls($userdata->filtercontrols); $o .= $this->construct_user_sessions_log($userdata); } + else { + $prevcid = 0; + foreach ($userdata->coursesatts as $ca) { + if ($prevcid != $ca->courseid) { + $o .= html_writer::empty_tag('hr'); + $prevcid = $ca->courseid; + + $o .= html_writer::tag('h3', $ca->coursefullname); + } + $o .= html_writer::tag('h4', $ca->attname); + + $o .= $this->construct_user_data_stat($userdata->stat[$ca->attid], $userdata->statuses[$ca->attid], + $userdata->gradable[$ca->attid], $userdata->grade[$ca->attid], + $userdata->maxgrade[$ca->attid], $userdata->decimalpoints); + } + } return $o; } - private function construct_user_data_stat(attforblock_user_data $userdata) { + private function construct_user_data_stat($stat, $statuses, $gradable, $grade, $maxgrade, $decimalpoints) { $stattable = new html_table(); $stattable->attributes['class'] = 'list'; $row = new html_table_row(); $row->cells[] = get_string('sessionscompleted','attforblock').':'; - $row->cells[] = $userdata->stat['completed']; + $row->cells[] = $stat['completed']; $stattable->data[] = $row; - foreach ($userdata->statuses as $st) { + foreach ($statuses as $st) { $row = new html_table_row(); $row->cells[] = $st->description . ':'; - $row->cells[] = array_key_exists($st->id, $userdata->stat['statuses']) ? $userdata->stat['statuses'][$st->id]->stcnt : 0; + $row->cells[] = array_key_exists($st->id, $stat['statuses']) ? $stat['statuses'][$st->id]->stcnt : 0; $stattable->data[] = $row; } - if ($userdata->gradable) { + if ($gradable) { $row = new html_table_row(); $row->cells[] = get_string('attendancegrade','attforblock') . ':'; - $row->cells[] = $userdata->grade . ' / ' . $userdata->maxgrade; + $row->cells[] = $grade . ' / ' . $maxgrade; $stattable->data[] = $row; $row = new html_table_row(); $row->cells[] = get_string('attendancepercent','attforblock') . ':'; - if ($userdata->maxgrade == 0) { + if ($maxgrade == 0) { $percent = 0; } else { - $percent = $userdata->grade / $userdata->maxgrade * 100; + $percent = $grade / $maxgrade * 100; } - $row->cells[] = sprintf("%0.{$userdata->decimalpoints}f", $percent); + $row->cells[] = sprintf("%0.{$decimalpoints}f", $percent); $stattable->data[] = $row; } diff --git a/view.php b/view.php index 50b704f..f0b8b73 100644 --- a/view.php +++ b/view.php @@ -15,7 +15,7 @@ require_once(dirname(__FILE__).'/locallib.php'); $pageparams = new att_view_page_params(); $id = required_param('id', PARAM_INT); -$pageparams->student = optional_param('student', NULL, PARAM_INT); +$pageparams->studentid = optional_param('studentid', NULL, PARAM_INT); $pageparams->mode = optional_param('mode', att_view_page_params::MODE_THIS_COURSE, PARAM_INT); $pageparams->view = optional_param('view', NULL, PARAM_INT); $pageparams->curdate = optional_param('curdate', NULL, PARAM_INT); @@ -31,7 +31,7 @@ $att = new attforblock($attforblock, $cm, $course, $PAGE->context, $pageparams); // Not specified studentid for displaying attendance? // Redirect to appropriate page if can -if (!$pageparams->student) { +if (!$pageparams->studentid) { if ($att->perm->can_manage() || $att->perm->can_take() || $att->perm->can_change()) { redirect($att->url_manage()); } @@ -50,7 +50,7 @@ $PAGE->navbar->add(get_string('attendancereport', 'attforblock')); $output = $PAGE->get_renderer('mod_attforblock'); -$userid = isset($pageparams->student) ? $pageparams->student : $USER->id; +$userid = isset($pageparams->studentid) ? $pageparams->studentid : $USER->id; $userdata = new attforblock_user_data($att, $userid); echo $output->header();