Browse Source

rewrite groupcheck and remove permissions class

MOODLE_29_STABLE
Dan Marsden 10 years ago
parent
commit
493780eed0
  1. 65
      locallib.php
  2. 13
      renderables.php
  3. 23
      renderer.php
  4. 6
      take.php

65
locallib.php

@ -38,66 +38,6 @@ define('ATT_VIEW_NOTPRESENT', 6);
define('ATT_SORT_LASTNAME', 1); define('ATT_SORT_LASTNAME', 1);
define('ATT_SORT_FIRSTNAME', 2); 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 { class att_page_with_filter_controls {
const SELECTOR_NONE = 1; const SELECTOR_NONE = 1;
@ -492,9 +432,6 @@ class attendance {
/** current page parameters */ /** current page parameters */
public $pageparams; public $pageparams;
/** @var attendance_permissions permission of current user for attendance instance*/
public $perm;
private $groupmode; private $groupmode;
private $statuses; private $statuses;
@ -535,8 +472,6 @@ class attendance {
} }
$this->pageparams = $pageparams; $this->pageparams = $pageparams;
$this->perm = new attendance_permissions($this->cm, $this->context);
} }
public function get_group_mode() { public function get_group_mode() {

13
renderables.php

@ -214,22 +214,18 @@ class attendance_manage_data implements renderable {
/** @var int number of hidden sessions (sessions before $course->startdate)*/ /** @var int number of hidden sessions (sessions before $course->startdate)*/
public $hiddensessionscount; public $hiddensessionscount;
/** @var attendance_permissions permission of current user for attendance instance*/
public $perm;
public $groups; public $groups;
public $hiddensesscount; public $hiddensesscount;
/** @var attendance */ /** @var attendance */
private $att; public $att;
/** /**
* Prepare info about attendance sessions taking into account view parameters. * Prepare info about attendance sessions taking into account view parameters.
* *
* @param attendance $att instance * @param attendance $att instance
*/ */
public function __construct(attendance $att) { public function __construct(attendance $att) {
$this->perm = $att->perm;
$this->sessions = $att->get_filtered_sessions(); $this->sessions = $att->get_filtered_sessions();
@ -256,7 +252,6 @@ class attendance_take_data implements renderable {
public $users; public $users;
public $pageparams; public $pageparams;
public $perm;
public $groupmode; public $groupmode;
public $cm; public $cm;
@ -283,7 +278,6 @@ class attendance_take_data implements renderable {
} }
$this->pageparams = $att->pageparams; $this->pageparams = $att->pageparams;
$this->perm = $att->perm;
$this->groupmode = $att->get_group_mode(); $this->groupmode = $att->get_group_mode();
$this->cm = $att->cm; $this->cm = $att->cm;
@ -438,7 +432,6 @@ class attendance_user_data implements renderable {
} }
class attendance_report_data implements renderable { class attendance_report_data implements renderable {
public $perm;
public $pageparams; public $pageparams;
public $users; public $users;
@ -465,13 +458,11 @@ class attendance_report_data implements renderable {
public $maxgrades = array(); public $maxgrades = array();
private $att; public $att;
public function __construct(attendance $att) { public function __construct(attendance $att) {
global $CFG; global $CFG;
$this->perm = $att->perm;
$currenttime = time(); $currenttime = time();
if ($att->pageparams->view == ATT_VIEW_NOTPRESENT) { if ($att->pageparams->view == ATT_VIEW_NOTPRESENT) {
$att->pageparams->enddate = $currenttime; $att->pageparams->enddate = $currenttime;

23
renderer.php

@ -249,7 +249,7 @@ class mod_attendance_renderer extends plugin_renderer_base {
$date = userdate($sess->sessdate, get_string('strftimedmyw', 'attendance')); $date = userdate($sess->sessdate, get_string('strftimedmyw', 'attendance'));
$time = $this->construct_time($sess->sessdate, $sess->duration); $time = $this->construct_time($sess->sessdate, $sess->duration);
if ($sess->lasttaken > 0) { 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); $url = $sessdata->url_take($sess->id, $sess->groupid);
$title = get_string('changeattendance', 'attendance'); $title = get_string('changeattendance', 'attendance');
@ -262,13 +262,14 @@ class mod_attendance_renderer extends plugin_renderer_base {
$time = '<i>' . $time . '</i>'; $time = '<i>' . $time . '</i>';
} }
} else { } else {
if ($sessdata->perm->can_take()) { if (has_capability('mod/attendance:takeattendances', $sessdata->att->context)) {
$url = $sessdata->url_take($sess->id, $sess->groupid); $url = $sessdata->url_take($sess->id, $sess->groupid);
$title = get_string('takeattendance', 'attendance'); $title = get_string('takeattendance', 'attendance');
$actions = $this->output->action_icon($url, new pix_icon('t/go', $title)); $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); $url = $sessdata->url_sessions($sess->id, att_sessions_page_params::ACTION_UPDATE);
$title = get_string('editsession', 'attendance'); $title = get_string('editsession', 'attendance');
$actions .= $this->output->action_icon($url, new pix_icon('t/edit', $title)); $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', $table->data[0][] = $this->output->help_icon('hiddensessions', 'attendance',
get_string('hiddensessions', 'attendance').': '.$sessdata->hiddensessionscount); get_string('hiddensessions', 'attendance').': '.$sessdata->hiddensessionscount);
if ($sessdata->perm->can_manage()) { if (has_capability('mod/attendance:manageattendances', $sessdata->att->context)) {
$options = array( $options = array(
att_sessions_page_params::ACTION_DELETE_SELECTED => get_string('delete'), att_sessions_page_params::ACTION_DELETE_SELECTED => get_string('delete'),
att_sessions_page_params::ACTION_CHANGE_DURATION => get_string('changeduration', 'attendance')); 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; GLOBAL $CFG;
$controls = ''; $controls = '';
$context = context_module::instance($takedata->cm->id);
$group = 0; $group = 0;
if ($takedata->pageparams->grouptype != attendance::SESSION_COMMON) { if ($takedata->pageparams->grouptype != attendance::SESSION_COMMON) {
$group = $takedata->pageparams->grouptype; $group = $takedata->pageparams->grouptype;
@ -387,13 +388,13 @@ class mod_attendance_renderer extends plugin_renderer_base {
} else { } else {
$groups = $group; $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', 'u.id, u.firstname, u.lastname, u.email',
'', '', '', $groups, '', '', '', $groups,
'', false, true); '', false, true);
$totalusers = count($users); $totalusers = count($users);
} else { } 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; $usersperpage = $takedata->pageparams->perpage;
if (!empty($takedata->pageparams->page) && $takedata->pageparams->page && $totalusers && $usersperpage) { 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 if ($takedata->pageparams->grouptype == attendance::SESSION_COMMON and
($takedata->groupmode == VISIBLEGROUPS or ($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); $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 = userdate($sess->sessdate, get_string('strftimedm', 'attendance'));
$sesstext .= html_writer::empty_tag('br'); $sesstext .= html_writer::empty_tag('br');
$sesstext .= userdate($sess->sessdate, '('.get_string('strftimehm', 'attendance').')'); $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::link($reportdata->url_take($sess->id, $sess->groupid), $sesstext);
} }
$sesstext .= html_writer::empty_tag('br'); $sesstext .= html_writer::empty_tag('br');

6
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); $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
require_login($course, true, $cm); 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->group = groups_get_activity_group($cm, true);
$pageparams->init($course->id); $pageparams->init($course->id);
$att = new attendance($att, $cm, $course, $PAGE->context, $pageparams); $att = new attendance($att, $cm, $course, $PAGE->context, $pageparams);
if (!$att->perm->can_take_session($pageparams->grouptype)) { $allowedgroups = groups_get_activity_allowed_groups($this->cm);
if (!empty($pageparams->grouptype) && !array_key_exists($groupid, $allowedgroups)) {
$group = groups_get_group($pageparams->grouptype); $group = groups_get_group($pageparams->grouptype);
throw new moodle_exception('cannottakeforgroup', 'attendance', '', $group->name); throw new moodle_exception('cannottakeforgroup', 'attendance', '', $group->name);
} }
if (($formdata = data_submitted()) && confirm_sesskey()) { if (($formdata = data_submitted()) && confirm_sesskey()) {
$att->take_from_form_data($formdata); $att->take_from_form_data($formdata);
} }

Loading…
Cancel
Save