print_error('invalidcourseid'); } if (!($context = get_context_instance(CONTEXT_COURSE, $course->id))) { print_error('invalidcontext'); } require_login($course->id); if ($course->metacourse) { print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot . '/course/view.php?id=' . $course->id); } if ($userid) { // Unenrolling someone else require_capability('moodle/role:assign', $context, NULL, false); $roles = get_user_roles($context, $userid, false); // verify user may unassign all roles at course context foreach ($roles as $role) { if (!user_can_assign($context, $role->roleid)) { print_error('cannotunassignrolefrom', '', '', $role->roleid); } } } else { // Unenrol yourself require_capability('moodle/role:unassignself', $context, NULL, false); } if (!empty($USER->access['rsw'][$context->path])) { print_error('cantunenrollinthisrole', '', $CFG->wwwroot . '/course/view.php?id=' . $course->id); } if ($confirm and confirm_sesskey()) { if ($userid) { if (!role_unassign(0, $userid, 0, $context->id)) { print_error("unenrolerror"); }
} if ($roleid > 0) { $a = new stdClass(); $a->number = $totalcount; $a->role = $rolenames[$roleid]; $heading = format_string(get_string('xuserswiththerole', 'role', $a)); if ($currentgroup and $group) { $a->group = $group->name; $heading .= ' ' . format_string(get_string('ingroup', 'role', $a)); } if ($accesssince) { $a->timeperiod = $timeoptions[$accesssince]; $heading .= ' ' . format_string(get_string('inactiveformorethan', 'role', $a)); } $heading .= ": {$a->number}"; if (user_can_assign($context, $roleid)) { $headingurl = new moodle_url($CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php', array('roleid' => $roleid, 'contextid' => $context->id)); $heading .= $OUTPUT->action_icon($headingurl, new pix_icon('t/edit', get_string('edit'))); } echo $OUTPUT->heading($heading, 3); } else { if ($course->id != SITEID && has_capability('moodle/course:enrolreview', $context)) { $editlink = $OUTPUT->action_icon(new moodle_url('/enrol/users.php', array('id' => $course->id)), new pix_icon('t/edit', get_string('edit'))); } else { $editlink = ''; } if ($course->id == SITEID and $roleid < 0) { $strallparticipants = get_string('allsiteusers', 'role'); } else { $strallparticipants = get_string('allparticipants'); }
/** * Test returning of assignable roles in context. */ public function test_get_assignable_roles() { global $DB; $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $coursecontext = context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST); $teacher = $this->getDataGenerator()->create_user(); role_assign($teacherrole->id, $teacher->id, $coursecontext); $teacherename = (object) array('roleid' => $teacherrole->id, 'name' => 'Učitel', 'contextid' => $coursecontext->id); $DB->insert_record('role_names', $teacherename); $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); $student = $this->getDataGenerator()->create_user(); role_assign($studentrole->id, $student->id, $coursecontext); $contexts = $DB->get_records('context'); $users = $DB->get_records('user'); $allroles = $DB->get_records('role'); // Evaluate all results for all users in all contexts. foreach ($users as $user) { $this->setUser($user); foreach ($contexts as $contextid => $unused) { $context = context_helper::instance_by_id($contextid); $roles = get_assignable_roles($context, ROLENAME_SHORT); foreach ($allroles as $roleid => $role) { if (isset($roles[$roleid])) { if (is_siteadmin()) { $this->assertTrue($DB->record_exists('role_context_levels', array('contextlevel' => $context->contextlevel, 'roleid' => $roleid))); } else { $this->assertTrue(user_can_assign($context, $roleid), "u:{$user->id} r:{$roleid}"); } $this->assertEquals($role->shortname, $roles[$roleid]); } else { $allowed = $DB->record_exists('role_context_levels', array('contextlevel' => $context->contextlevel, 'roleid' => $roleid)); if (is_siteadmin()) { $this->assertFalse($allowed); } else { $this->assertFalse($allowed and user_can_assign($context, $roleid), "u:{$user->id}, r:{$allroles[$roleid]->name}, c:{$context->contextlevel}"); } } } } } // Not-logged-in user. $this->setUser(0); foreach ($contexts as $contextid => $unused) { $context = context_helper::instance_by_id($contextid); $roles = get_assignable_roles($context, ROLENAME_SHORT); $this->assertSame(array(), $roles); } // Test current user. $this->setUser(0); $admin = $DB->get_record('user', array('username' => 'admin'), '*', MUST_EXIST); $roles1 = get_assignable_roles($coursecontext, ROLENAME_SHORT, false, $admin); $roles2 = get_assignable_roles($coursecontext, ROLENAME_SHORT, false, $admin->id); $this->setAdminUser(); $roles3 = get_assignable_roles($coursecontext, ROLENAME_SHORT); $this->assertSame($roles1, $roles3); $this->assertSame($roles2, $roles3); // Test parameter defaults. $this->setAdminUser(); $roles1 = get_assignable_roles($coursecontext); $roles2 = get_assignable_roles($coursecontext, ROLENAME_ALIAS, false, $admin); $this->assertEquals($roles2, $roles1); // Verify returned names - let's allow all roles everywhere to simplify this a bit. $alllevels = context_helper::get_all_levels(); $alllevels = array_keys($alllevels); foreach ($allroles as $roleid => $role) { set_role_contextlevels($roleid, $alllevels); } $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT); foreach ($alltypes as $type) { $rolenames = role_fix_names($allroles, $coursecontext, $type); $roles = get_assignable_roles($coursecontext, $type, false, $admin); foreach ($roles as $roleid => $rolename) { $this->assertSame($rolenames[$roleid]->localname, $rolename); } } // Verify counts. $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT); foreach ($alltypes as $type) { $roles = get_assignable_roles($coursecontext, $type, false, $admin); list($rolenames, $rolecounts, $nameswithcounts) = get_assignable_roles($coursecontext, $type, true, $admin); $this->assertEquals($roles, $rolenames); foreach ($rolenames as $roleid => $name) { if ($roleid == $teacherrole->id or $roleid == $studentrole->id) { $this->assertEquals(1, $rolecounts[$roleid]); } else { $this->assertEquals(0, $rolecounts[$roleid]); } $this->assertSame("{$name} ({$rolecounts[$roleid]})", $nameswithcounts[$roleid]); } } }
// to extend enrolments current user needs to be able to do role assignments require_capability('moodle/role:assign', $context); $today = time(); $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); if (count($users) > 0 and $form = data_submitted() and confirm_sesskey()) { if (count($form->userid) != count($form->extendperiod) || count($form->userid) != count($form->extendbase)) { print_error('invalidformdata', '', $CFG->wwwroot . '/user/index.php?id=' . $id); } foreach ($form->userid as $k => $v) { // find all roles this student have in this course if ($students = $DB->get_records_sql("SELECT ra.id, ra.roleid, ra.timestart, ra.timeend\n FROM {role_assignments} ra\n WHERE userid = ?\n AND contextid = ?", array($v, $context->id))) { // enrol these users again, with time extension // not that this is not necessarily a student role foreach ($students as $student) { // only extend if the user can make role assignments on this role if (user_can_assign($context, $student->roleid)) { switch ($form->extendperiod[$k]) { case 0: // No change break; case -1: // unlimited $student->timeend = 0; break; default: // extend switch ($form->extendbase[$k]) { case 0: // course start date $student->timeend = $course->startdate + $form->extendperiod[$k]; break;
protected function process_unassignment($data) { $context = $this->get_context(); if (!user_can_assign($context, $data->role)) { print_error('nopermissions', 'error'); } foreach ($data->_selection as $user) { role_unassign($data->role, cm_get_moodleuserid($user), 0, $context->id); } $tmppage = $this->get_basepage(); redirect($tmppage->get_url(), get_string('users_removed_from_role', 'block_curr_admin', count($data->_selection))); }
/** * Gets a list of roles that this user can assign in this context * @param object $context * @return array */ function get_assignable_roles($context, $field = "name") { $options = array(); if ($roles = get_all_roles()) { foreach ($roles as $role) { if (user_can_assign($context, $role->id)) { $options[$role->id] = strip_tags(format_string($role->{$field}, true)); } } } return $options; }
notify(get_string('coursegroupunknown', 'error', $addgroup[$i])); } else { if ($gid = groups_get_group_by_name($course[$i]->id, $addgroup[$i])) { $groupid[$i] = $gid; } else { notify(get_string('groupunknown', 'error', $addgroup[$i])); } } } } for ($i = 0; $i < 5; $i++) { /// Enrol into courses if necessary if ($course[$i]) { if (isset($addrole[$i])) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course[$i]->id); if (!user_can_assign($coursecontext, $addrole[$i])) { notify('--> Can not assign role in course'); //TODO: localize } $ret = role_assign($addrole[$i], $user->id, 0, $coursecontext->id); } else { if (isset($addtype[$i])) { switch ($addtype[$i]) { case 2: // teacher $ret = add_teacher($user->id, $course[$i]->id, 1); break; case 3: // non-editing teacher $ret = add_teacher($user->id, $course[$i]->id, 0); break;