Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
Some refactoring: sesstype and sessgroupslist moved to att_page_with_…
Browse files Browse the repository at this point in the history
…filter_controls + reimplemented theirs calculations.
  • Loading branch information
andreev-artem committed Jul 15, 2011
1 parent edb3bc1 commit 3515e99
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 140 deletions.
246 changes: 118 additions & 128 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -149,10 +157,10 @@ private function init_view() {
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;
Expand All @@ -163,17 +171,17 @@ private function init_curdate() {
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'];
Expand Down Expand Up @@ -203,6 +211,91 @@ private function init_start_end_date() {
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 {
Expand Down Expand Up @@ -311,10 +404,10 @@ public function __construct() {
$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;
}

Expand Down Expand Up @@ -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;

Expand All @@ -386,11 +475,6 @@ class attforblock {

private $groupmode;

private $sessgroupslist;

private $currentgroup;


private $statuses;

// Cache
Expand All @@ -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;
Expand All @@ -430,11 +514,18 @@ public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course,
$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
*
Expand Down Expand Up @@ -535,15 +626,15 @@ public function get_filtered_sessions() {
} 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(
'aid' => $this->id,
'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)) {
Expand Down Expand Up @@ -611,107 +702,6 @@ public function url_view($params=array()) {
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;

Expand Down Expand Up @@ -804,7 +794,7 @@ public function get_users($groupid = 0) {
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";
Expand Down
12 changes: 6 additions & 6 deletions manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@

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
Expand Down
Loading

0 comments on commit 3515e99

Please sign in to comment.