diff --git a/locallib.php b/locallib.php index a528e42..44291b8 100644 --- a/locallib.php +++ b/locallib.php @@ -38,66 +38,6 @@ define('ATT_VIEW_NOTPRESENT', 6); define('ATT_SORT_LASTNAME', 1); define('ATT_SORT_FIRSTNAME', 2); -class attendance_permissions { - private $cantake; - private $canchange; - private $canmanage; - private $canaccessallgroups; - - private $cm; - private $context; - - public function __construct($cm, $context) { - $this->cm = $cm; - $this->context = $context; - } - - public function can_take() { - if (is_null($this->cantake)) { - $this->cantake = has_capability('mod/attendance:takeattendances', $this->context); - } - - return $this->cantake; - } - - public function can_take_session($groupid) { - if (!$this->can_take()) { - return false; - } - - if ($groupid == attendance::SESSION_COMMON - || $this->can_access_all_groups() - || array_key_exists($groupid, groups_get_activity_allowed_groups($this->cm))) { - return true; - } - - return false; - } - - public function can_change() { - if (is_null($this->canchange)) { - $this->canchange = has_capability('mod/attendance:changeattendances', $this->context); - } - - return $this->canchange; - } - - public function can_manage() { - if (is_null($this->canmanage)) { - $this->canmanage = has_capability('mod/attendance:manageattendances', $this->context); - } - - return $this->canmanage; - } - - public function can_access_all_groups() { - if (is_null($this->canaccessallgroups)) { - $this->canaccessallgroups = has_capability('moodle/site:accessallgroups', $this->context); - } - - return $this->canaccessallgroups; - } -} class att_page_with_filter_controls { const SELECTOR_NONE = 1; @@ -492,9 +432,6 @@ class attendance { /** current page parameters */ public $pageparams; - /** @var attendance_permissions permission of current user for attendance instance*/ - public $perm; - private $groupmode; private $statuses; @@ -535,8 +472,6 @@ class attendance { } $this->pageparams = $pageparams; - - $this->perm = new attendance_permissions($this->cm, $this->context); } public function get_group_mode() { diff --git a/renderables.php b/renderables.php index 56e6d20..053ca18 100644 --- a/renderables.php +++ b/renderables.php @@ -214,22 +214,18 @@ class attendance_manage_data implements renderable { /** @var int number of hidden sessions (sessions before $course->startdate)*/ public $hiddensessionscount; - /** @var attendance_permissions permission of current user for attendance instance*/ - public $perm; - public $groups; public $hiddensesscount; /** @var attendance */ - private $att; + public $att; /** * Prepare info about attendance sessions taking into account view parameters. * * @param attendance $att instance */ public function __construct(attendance $att) { - $this->perm = $att->perm; $this->sessions = $att->get_filtered_sessions(); @@ -256,7 +252,6 @@ class attendance_take_data implements renderable { public $users; public $pageparams; - public $perm; public $groupmode; public $cm; @@ -283,7 +278,6 @@ class attendance_take_data implements renderable { } $this->pageparams = $att->pageparams; - $this->perm = $att->perm; $this->groupmode = $att->get_group_mode(); $this->cm = $att->cm; @@ -438,7 +432,6 @@ class attendance_user_data implements renderable { } class attendance_report_data implements renderable { - public $perm; public $pageparams; public $users; @@ -465,13 +458,11 @@ class attendance_report_data implements renderable { public $maxgrades = array(); - private $att; + public $att; public function __construct(attendance $att) { global $CFG; - $this->perm = $att->perm; - $currenttime = time(); if ($att->pageparams->view == ATT_VIEW_NOTPRESENT) { $att->pageparams->enddate = $currenttime; diff --git a/renderer.php b/renderer.php index d358aef..36134cf 100644 --- a/renderer.php +++ b/renderer.php @@ -249,7 +249,7 @@ class mod_attendance_renderer extends plugin_renderer_base { $date = userdate($sess->sessdate, get_string('strftimedmyw', 'attendance')); $time = $this->construct_time($sess->sessdate, $sess->duration); if ($sess->lasttaken > 0) { - if ($sessdata->perm->can_change()) { + if (has_capability('mod/attendance:changeattendances', $sessdata->att->context)) { $url = $sessdata->url_take($sess->id, $sess->groupid); $title = get_string('changeattendance', 'attendance'); @@ -262,13 +262,14 @@ class mod_attendance_renderer extends plugin_renderer_base { $time = '' . $time . ''; } } else { - if ($sessdata->perm->can_take()) { + if (has_capability('mod/attendance:takeattendances', $sessdata->att->context)) { $url = $sessdata->url_take($sess->id, $sess->groupid); $title = get_string('takeattendance', 'attendance'); $actions = $this->output->action_icon($url, new pix_icon('t/go', $title)); } } - if ($sessdata->perm->can_manage()) { + + if (has_capability('mod/attendance:manageattendances', $sessdata->att->context)) { $url = $sessdata->url_sessions($sess->id, att_sessions_page_params::ACTION_UPDATE); $title = get_string('editsession', 'attendance'); $actions .= $this->output->action_icon($url, new pix_icon('t/edit', $title)); @@ -290,7 +291,7 @@ class mod_attendance_renderer extends plugin_renderer_base { $table->data[0][] = $this->output->help_icon('hiddensessions', 'attendance', get_string('hiddensessions', 'attendance').': '.$sessdata->hiddensessionscount); - if ($sessdata->perm->can_manage()) { + if (has_capability('mod/attendance:manageattendances', $sessdata->att->context)) { $options = array( att_sessions_page_params::ACTION_DELETE_SELECTED => get_string('delete'), att_sessions_page_params::ACTION_CHANGE_DURATION => get_string('changeduration', 'attendance')); @@ -371,7 +372,7 @@ class mod_attendance_renderer extends plugin_renderer_base { GLOBAL $CFG; $controls = ''; - + $context = context_module::instance($takedata->cm->id); $group = 0; if ($takedata->pageparams->grouptype != attendance::SESSION_COMMON) { $group = $takedata->pageparams->grouptype; @@ -387,13 +388,13 @@ class mod_attendance_renderer extends plugin_renderer_base { } else { $groups = $group; } - $users = get_users_by_capability(context_module::instance($takedata->cm->id), 'mod/attendance:canbelisted', + $users = get_users_by_capability($context, 'mod/attendance:canbelisted', 'u.id, u.firstname, u.lastname, u.email', '', '', '', $groups, '', false, true); $totalusers = count($users); } else { - $totalusers = count_enrolled_users(context_module::instance($takedata->cm->id), 'mod/attendance:canbelisted', $group); + $totalusers = count_enrolled_users($context, 'mod/attendance:canbelisted', $group); } $usersperpage = $takedata->pageparams->perpage; if (!empty($takedata->pageparams->page) && $takedata->pageparams->page && $totalusers && $usersperpage) { @@ -412,7 +413,7 @@ class mod_attendance_renderer extends plugin_renderer_base { if ($takedata->pageparams->grouptype == attendance::SESSION_COMMON and ($takedata->groupmode == VISIBLEGROUPS or - ($takedata->groupmode and $takedata->perm->can_access_all_groups()))) { + ($takedata->groupmode and has_capability('moodle/site:accessallgroups', $context)))) { $controls .= groups_print_activity_menu($takedata->cm, $takedata->url(), true); } @@ -820,7 +821,11 @@ class mod_attendance_renderer extends plugin_renderer_base { $sesstext = userdate($sess->sessdate, get_string('strftimedm', 'attendance')); $sesstext .= html_writer::empty_tag('br'); $sesstext .= userdate($sess->sessdate, '('.get_string('strftimehm', 'attendance').')'); - if (is_null($sess->lasttaken) and $reportdata->perm->can_take() or $reportdata->perm->can_change()) { + $capabilities = array( + 'mod/attendance:takeattendances', + 'mod/attendance:changeattendances' + ); + if (is_null($sess->lasttaken) and has_any_capability($capabilities, $reportdata->att->context)) { $sesstext = html_writer::link($reportdata->url_take($sess->id, $sess->groupid), $sesstext); } $sesstext .= html_writer::empty_tag('br'); diff --git a/take.php b/take.php index 055dd27..5a710f2 100644 --- a/take.php +++ b/take.php @@ -42,16 +42,20 @@ $course = $DB->get_record('course', array('id' => $cm->course), '*', MUS $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST); require_login($course, true, $cm); +$context = context_module::instance($cm->id); +require_capability('mod/attendance:takeattendances', $context); $pageparams->group = groups_get_activity_group($cm, true); $pageparams->init($course->id); $att = new attendance($att, $cm, $course, $PAGE->context, $pageparams); -if (!$att->perm->can_take_session($pageparams->grouptype)) { - $group = groups_get_group($pageparams->grouptype); - throw new moodle_exception('cannottakeforgroup', 'attendance', '', $group->name); +$allowedgroups = groups_get_activity_allowed_groups($this->cm); +if (!empty($pageparams->grouptype) && !array_key_exists($groupid, $allowedgroups)) { + $group = groups_get_group($pageparams->grouptype); + throw new moodle_exception('cannottakeforgroup', 'attendance', '', $group->name); } + if (($formdata = data_submitted()) && confirm_sesskey()) { $att->take_from_form_data($formdata); }