From 3515e99655d446ede317315cca52028de7811bb8 Mon Sep 17 00:00:00 2001 From: Artem Andreev Date: Fri, 15 Jul 2011 15:40:43 +0400 Subject: [PATCH] Some refactoring: sesstype and sessgroupslist moved to att_page_with_filter_controls + reimplemented theirs calculations. --- locallib.php | 246 +++++++++++++++++++++++------------------------- manage.php | 12 +-- renderables.php | 6 +- renderer.php | 2 +- report.php | 2 +- view.php | 2 +- 6 files changed, 130 insertions(+), 140 deletions(-) diff --git a/locallib.php b/locallib.php index 378051e..5a3cd20 100644 --- a/locallib.php +++ b/locallib.php @@ -120,6 +120,10 @@ class att_page_with_filter_controls { const SELECTOR_GROUP = 2; const SELECTOR_SESS_TYPE = 3; + const SESSTYPE_COMMON = 0; + const SESSTYPE_ALL = -1; + const SESSTYPE_NO_VALUE = -2; + /** @var int current view mode */ public $view; @@ -136,10 +140,14 @@ class att_page_with_filter_controls { protected $defaultview = VIEW_WEEKS; - private $courseid; + private $cm; + + private $sessgroupslist; + + private $sesstype; - public function init($courseid) { - $this->courseid = $courseid; + public function init($cm) { + $this->cm = $cm; $this->init_view(); $this->init_curdate(); $this->init_start_end_date(); @@ -149,10 +157,10 @@ class att_page_with_filter_controls { global $SESSION; if (isset($this->view)) { - $SESSION->attcurrentattview[$this->courseid] = $this->view; + $SESSION->attcurrentattview[$this->cm->course] = $this->view; } - elseif (isset($SESSION->attcurrentattview[$this->courseid])) { - $this->view = $SESSION->attcurrentattview[$this->courseid]; + elseif (isset($SESSION->attcurrentattview[$this->cm->course])) { + $this->view = $SESSION->attcurrentattview[$this->cm->course]; } else { $this->view = $this->defaultview; @@ -163,17 +171,17 @@ class att_page_with_filter_controls { global $SESSION; if (isset($this->curdate)) { - $SESSION->attcurrentattdate[$this->courseid] = $this->curdate; + $SESSION->attcurrentattdate[$this->cm->course] = $this->curdate; } - elseif (isset($SESSION->attcurrentattdate[$this->courseid])) { - $this->curdate = $SESSION->attcurrentattdate[$this->courseid]; + elseif (isset($SESSION->attcurrentattdate[$this->cm->course])) { + $this->curdate = $SESSION->attcurrentattdate[$this->cm->course]; } else { $this->curdate = time(); } } - private function init_start_end_date() { + public function init_start_end_date() { $date = usergetdate($this->curdate); $mday = $date['mday']; $wday = $date['wday']; @@ -203,6 +211,91 @@ class att_page_with_filter_controls { break; } } + + private function calc_sessgroupslist_sesstype() { + global $USER, $SESSION; + + if (!array_key_exists('attsessiontype', $SESSION)) { + $SESSION->attsessiontype = array(); + } + elseif (!array_key_exists($this->cm->course, $SESSION->attsessiontype)) { + $SESSION->attsessiontype[$this->cm->course] = self::SESSTYPE_ALL; + } + + $group = optional_param('group', self::SESSTYPE_NO_VALUE, PARAM_INT); + if ($this->selectortype == self::SELECTOR_SESS_TYPE) { + if ($group > self::SESSTYPE_NO_VALUE) { + $SESSION->attsessiontype[$this->cm->course] = $group; + if ($group > self::SESSTYPE_ALL) { + // set activegroup in $SESSION + groups_get_activity_group($this->cm, true); + } else { + // reset activegroup in $SESSION + unset($SESSION->activegroup[$this->cm->course][VISIBLEGROUPS][$this->cm->groupingid]); + unset($SESSION->activegroup[$this->cm->course]['aag'][$this->cm->groupingid]); + unset($SESSION->activegroup[$this->cm->course][SEPARATEGROUPS][$this->cm->groupingid]); + } + $this->sesstype = $group; + } else { + $this->sesstype = $SESSION->attsessiontype[$this->cm->course]; + } + + $this->calc_sessgroupslist(); + } elseif ($this->selectortype == self::SELECTOR_GROUP) { + if ($group == 0) { + $SESSION->attsessiontype[$this->cm->course] = self::SESSTYPE_ALL; + $this->sesstype = self::SESSTYPE_ALL; + } + elseif ($group > 0) { + $SESSION->attsessiontype[$this->cm->course] = $group; + $this->sesstype = $group; + } + else { + $this->sesstype = $SESSION->attsessiontype[$this->cm->course]; + } + } + } + + private function calc_sessgroupslist() { + global $PAGE; + + $this->sessgroupslist = array(); + $groupmode = groups_get_activity_groupmode($this->cm); + if ($groupmode == NOGROUPS) + return; + + if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $PAGE->context)) { + $allowedgroups = groups_get_all_groups($this->cm->course, 0, $this->cm->groupingid); + } else { + $allowedgroups = groups_get_all_groups($this->cm->course, $USER->id, $this->cm->groupingid); + } + + if ($allowedgroups) { + if ($groupmode == VISIBLEGROUPS or $this->perm->can_access_all_groups()) { + $this->sessgroupslist[self::SESSTYPE_ALL] = get_string('all', 'attforblock'); + } + if ($groupmode == VISIBLEGROUPS) { + $this->sessgroupslist[self::SESSTYPE_COMMON] = get_string('commonsessions', 'attforblock'); + } + foreach ($allowedgroups as $group) { + $this->sessgroupslist[$group->id] = format_string($group->name); + } + } + } + + public function get_sess_groups_list() { + if (is_null($this->sessgroupslist)) + $this->calc_sessgroupslist_sesstype(); + + return $this->sessgroupslist; + } + + public function get_current_sesstype() { + if (is_null($this->sesstype)) + $this->calc_sessgroupslist_sesstype(); + + return $this->sesstype; + } } class att_view_page_params extends att_page_with_filter_controls { @@ -311,10 +404,10 @@ class att_report_page_params extends att_page_with_filter_controls { $this->selectortype = self::SELECTOR_GROUP; } - public function init($courseid) { - parent::init($courseid); + public function init($cm) { + parent::init($cm); - if (!isset($this->group)) $this->group = 0; + if (!isset($this->group)) $this->group = $this->get_current_sesstype() > 0 ? $this->get_current_sesstype() : 0; if (!isset($this->sort)) $this->sort = SORT_LASTNAME; } @@ -356,10 +449,6 @@ class attforblock { const SESSION_COMMON = 0; const SESSION_GROUP = 1; - const SELECTOR_COMMON = 0; - const SELECTOR_ALL = -1; - const SELECTOR_NOT_EXISTS = -2; - /** @var stdclass course module record */ public $cm; @@ -386,11 +475,6 @@ class attforblock { private $groupmode; - private $sessgroupslist; - - private $currentgroup; - - private $statuses; // Cache @@ -413,7 +497,7 @@ class attforblock { * @param stdClass $course Course record from {course} table * @param stdClass $context The context of the workshop instance */ - public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course, stdclass $context=NULL, $view_params=NULL) { + public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course, stdclass $context=NULL, $pageparams=NULL) { foreach ($dbrecord as $field => $value) { if (property_exists('attforblock', $field)) { $this->{$field} = $value; @@ -430,11 +514,18 @@ class attforblock { $this->context = $context; } - $this->pageparams = $view_params; + $this->pageparams = $pageparams; $this->perm = new attforblock_permissions($this->context); } + public function get_group_mode() { + if (is_null($this->groupmode)) + $this->groupmode = groups_get_activity_groupmode($this->cm); + + return $this->groupmode; + } + /** * Returns current sessions for this attendance * @@ -535,7 +626,7 @@ class attforblock { } else { $where = "attendanceid = :aid AND sessdate >= :csdate"; } - if ($this->get_current_group() > attforblock::SELECTOR_ALL) { + if ($this->pageparams->get_current_sesstype() > att_page_with_filter_controls::SESSTYPE_ALL) { $where .= " AND groupid=:cgroup"; } $params = array( @@ -543,7 +634,7 @@ class attforblock { 'csdate' => $this->course->startdate, 'sdate' => $this->pageparams->startdate, 'edate' => $this->pageparams->enddate, - 'cgroup' => $this->get_current_group()); + 'cgroup' => $this->pageparams->get_current_sesstype()); $sessions = $DB->get_records_select('attendance_sessions', $where, $params, 'sessdate asc'); foreach ($sessions as $sess) { if (empty($sess->description)) { @@ -611,107 +702,6 @@ class attforblock { return new moodle_url('/mod/attforblock/view.php', $params); } - private function calc_groupmode_sessgroupslist_currentgroup(){ - global $USER, $SESSION; - - $cm = $this->cm; - - $this->get_group_mode(); - - if ($this->groupmode == NOGROUPS) - return; - - if ($this->groupmode == VISIBLEGROUPS or $this->perm->can_access_all_groups()) { - $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) - // detect changes related to groups and fix active group - if (!empty($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid])) { - if (!array_key_exists($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid], $allowedgroups)) { - // active group does not exist anymore - unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]); - } - } - if (!empty($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid])) { - if (!array_key_exists($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid], $allowedgroups)) { - // active group does not exist anymore - unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]); - } - } - - } else { - $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups - // detect changes related to groups and fix active group - if (isset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid])) { - if ($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid] == 0) { - if ($allowedgroups) { - // somebody must have assigned at least one group, we can select it now - yay! - unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]); - } - } else { - if (!array_key_exists($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid], $allowedgroups)) { - // active group not allowed or does not exist anymore - unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]); - } - } - } - } - - $group = optional_param('group', self::SELECTOR_NOT_EXISTS, PARAM_INT); - if (!array_key_exists('attsessiontype', $SESSION)) { - $SESSION->attsessiontype = array(); - } - if ($group > self::SELECTOR_NOT_EXISTS) { - $SESSION->attsessiontype[$cm->course] = $group; - } elseif (!array_key_exists($cm->course, $SESSION->attsessiontype)) { - $SESSION->attsessiontype[$cm->course] = self::SELECTOR_ALL; - } - - if ($group == self::SELECTOR_ALL) { - $this->currentgroup = $group; - unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]); - unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]); - unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]); - } else { - $this->currentgroup = groups_get_activity_group($cm, true); - if ($this->currentgroup == 0 and $SESSION->attsessiontype[$cm->course] == self::SELECTOR_ALL) { - $this->currentgroup = self::SELECTOR_ALL; - } - } - - $this->sessgroupslist = array(); - 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); - } - } - } - - public function get_group_mode() { - if (is_null($this->groupmode)) - $this->groupmode = groups_get_activity_groupmode($this->cm); - - return $this->groupmode; - } - - public function get_sess_groups_list() { - if (is_null($this->sessgroupslist)) - $this->calc_groupmode_sessgroupslist_currentgroup(); - - return $this->sessgroupslist; - } - - public function get_current_group() { - if (is_null($this->currentgroup)) - $this->calc_groupmode_sessgroupslist_currentgroup(); - - return $this->currentgroup; - } - public function add_sessions($sessions) { global $DB; @@ -804,7 +794,7 @@ class attforblock { global $DB; //fields we need from the user table - $userfields = user_picture::fields('u'); + $userfields = user_picture::fields('u').',u.username'; if (isset($this->pageparams->sort) and ($this->pageparams->sort == SORT_FIRSTNAME)) { $orderby = "u.firstname ASC, u.lastname ASC"; diff --git a/manage.php b/manage.php index 48ea9af..532842e 100644 --- a/manage.php +++ b/manage.php @@ -25,21 +25,21 @@ $att = $DB->get_record('attforblock', array('id' => $cm->instance), ' require_login($course, true, $cm); -$pageparams->init($course->id); +$pageparams->init($cm); $att = new attforblock($att, $cm, $course, $PAGE->context, $pageparams); if (!$att->perm->can_manage() && !$att->perm->can_take() && !$att->perm->can_change()) - redirect("view.php?id=$cm->id"); + redirect($att->url_view()); // if teacher is coming from block, then check for a session exists for today if ($from === 'block') { - $atts = $att->get_today_sessions(); - $size = count($atts); + $sessions = $att->get_today_sessions(); + $size = count($sessions); if ($size == 1) { - $att = reset($atts); + $sess = reset($sessions); $nottaken = !$att->lasttaken && has_capability('mod/attforblock:takeattendances', $context); $canchange = $att->lasttaken && has_capability('mod/attforblock:changeattendances', $context); if ($nottaken || $canchange) - redirect('attendances.php?id='.$id.'&sessionid='.$att->id.'&grouptype='.$att->groupid); + redirect($att->url_take(array('sessionid' => $sess->id, 'grouptype' => $sess->groupid))); } elseif ($size > 1) { $att->curdate = $today; //temporally set $view for single access to page from block diff --git a/renderables.php b/renderables.php index 7e34cf0..a546ec2 100644 --- a/renderables.php +++ b/renderables.php @@ -162,11 +162,11 @@ class attforblock_filter_controls implements renderable { } public function get_sess_groups_list() { - return $this->att->get_sess_groups_list(); + return $this->att->pageparams->get_sess_groups_list(); } - public function get_current_group() { - return $this->att->get_current_group(); + public function get_current_sesstype() { + return $this->att->pageparams->get_current_sesstype(); } } diff --git a/renderer.php b/renderer.php index cef7f6a..83e5063 100644 --- a/renderer.php +++ b/renderer.php @@ -64,7 +64,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { $sessgroups = $fcontrols->get_sess_groups_list(); if ($sessgroups) { $select = new single_select($fcontrols->url(), 'group', $sessgroups, - $fcontrols->get_current_group(), null, 'selectgroup'); + $fcontrols->get_current_sesstype(), null, 'selectgroup'); $select->label = get_string('sessions', 'attforblock'); $output = $this->output->render($select); diff --git a/report.php b/report.php index f7e56a7..29e19f4 100644 --- a/report.php +++ b/report.php @@ -27,7 +27,7 @@ $att = $DB->get_record('attforblock', array('id' => $cm->instance), ' require_login($course, true, $cm); -$pageparams->init($course->id); +$pageparams->init($cm); $att = new attforblock($att, $cm, $course, $PAGE->context, $pageparams); $att->perm->require_view_reports_capability(); diff --git a/view.php b/view.php index f0b8b73..9862ac1 100644 --- a/view.php +++ b/view.php @@ -26,7 +26,7 @@ $attforblock = $DB->get_record('attforblock', array('id' => $cm->instance), ' require_login($course, true, $cm); -$pageparams->init($course->id); +$pageparams->init($cm); $att = new attforblock($attforblock, $cm, $course, $PAGE->context, $pageparams); // Not specified studentid for displaying attendance?