本文整理汇总了PHP中role_assign函数的典型用法代码示例。如果您正苦于以下问题:PHP role_assign函数的具体用法?PHP role_assign怎么用?PHP role_assign使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了role_assign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_user_get_user_details_courses
/**
* Test user_get_user_details_courses
*/
public function test_user_get_user_details_courses()
{
global $DB;
$this->resetAfterTest();
// Create user and modify user profile.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course1->id);
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
role_assign($teacherrole->id, $user1->id, $coursecontext->id);
role_assign($teacherrole->id, $user2->id, $coursecontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Get user2 details as a user with super system capabilities.
$result = user_get_user_details_courses($user2);
$this->assertEquals($user2->id, $result['id']);
$this->assertEquals(fullname($user2), $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
$this->setUser($user1);
// Get user2 details as a user who can only see this user in a course.
$result = user_get_user_details_courses($user2);
$this->assertEquals($user2->id, $result['id']);
$this->assertEquals(fullname($user2), $result['fullname']);
$this->assertEquals($course1->id, $result['enrolledcourses'][0]['id']);
}
示例2: test_coursepermissions
/**
* Test for correct assignment of course permissions
*/
public function test_coursepermissions()
{
global $DB, $CFG, $USER;
accesslib_clear_all_caches(true);
set_config('siteguest', '');
set_config('siteadmins', '');
$this->assertFalse($this->has_program_create_capability());
$this->assertFalse($this->has_associate_and_manage_capability());
$this->assertFalse($this->has_program_view_capability());
$syscontext = context_system::instance();
$assigninguser = new user(array('idnumber' => 'testuserid', 'username' => 'testuser', 'firstname' => 'testuser', 'lastname' => 'testuser', 'email' => 'testuser@testuserdomain.com', 'country' => 'CA'));
$assigninguser->save();
$roleid = create_role('userrole', 'userrole', 'userrole');
$usr = $DB->get_record('user', array('username' => 'testuser'));
$this->setUser($usr);
$USER = $usr;
assign_capability('local/elisprogram:associate', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:manage', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_create', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_enrol', CAP_ALLOW, $roleid, $syscontext->id);
assign_capability('local/elisprogram:program_view', CAP_ALLOW, $roleid, $syscontext->id);
role_assign($roleid, $usr->id, $syscontext->id);
$this->assertTrue($this->has_program_create_capability());
$this->assertTrue($this->has_associate_and_manage_capability());
$this->assertTrue($this->has_program_view_capability());
$this->setUser(null);
}
示例3: setUpDB
/**
* Set up the database for tests
*
* This function is needed so that daily_log_provider has the before-test set up from setUp()
*/
public function setUpDB() {
global $DB;
if ($DB->record_exists('user', array('username' => 'user1'))) {
return;
}
$datagen = self::getDataGenerator();
$user1 = $datagen->create_user(array('username'=>'user1'));
$user2 = $datagen->create_user(array('username'=>'user2'));
$course1 = $datagen->create_course(array('shortname'=>'course1'));
$success = enrol_try_internal_enrol($course1->id, $user1->id, 5);
if (! $success) {
trigger_error('User enrollment failed', E_USER_ERROR);
}
$context = context_system::instance();
role_assign(self::STID, $user2->id, $context->id);
$this->generate_replacement_list();
}
示例4: test_mymoodleredirectreturnsfalseforadmin
/**
* Validate that redirection from My Moodle does not happen for admins
*/
public function test_mymoodleredirectreturnsfalseforadmin()
{
global $CFG, $USER, $DB;
require_once $CFG->dirroot . '/user/lib.php';
// Make sure we're not a guest.
set_config('siteguest', '');
// Obtain the system context.
$syscontext = context_system::instance();
// Set up the current user global.
$user = new stdClass();
$user->username = "testuser";
$userid = user_create_user($user);
$USER = $DB->get_record('user', array('id' => $userid));
// Enable functionaltiy.
pm_set_config('mymoodle_redirect', 1);
elis::$config = new elis_config();
// Give the admin sufficient permissions.
$roleid = create_role('adminrole', 'adminrole', 'adminrole');
assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext->id);
role_assign($roleid, $USER->id, $syscontext->id);
// Validate that redirection does not happen for admins.
$result = pm_mymoodle_redirect();
// Clear out cached permissions data so we don't affect other tests.
accesslib_clear_all_caches(true);
$this->assertFalse($result);
}
示例5: test_enrol_users
/**
* Test get_enrolled_users
*/
public function test_enrol_users()
{
global $USER, $CFG;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Set the required capabilities by the external function.
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('enrol/manual:enrol', $context->id);
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
// Add manager role to $USER.
// So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
role_assign(1, $USER->id, context_system::instance()->id);
// Call the external function.
enrol_manual_external::enrol_users(array(array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course->id), array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course->id)));
// Check we retrieve the good total number of enrolled users.
require_once $CFG->dirroot . '/enrol/externallib.php';
$enrolledusers = core_enrol_external::get_enrolled_users($course->id);
$this->assertEquals(2, count($enrolledusers));
// Call without required capability.
$this->unassignUserCapability('enrol/manual:enrol', $context->id, $roleid);
$this->setExpectedException('moodle_exception');
$categories = enrol_manual_external::enrol_users($course->id);
}
示例6: build
/**
* Create the fixture
*
* This method must be safe to call multiple times.
*
* @return void
* @throws moodle_exception
*/
public function build()
{
global $DB;
if (!$this->exists()) {
// Dependents
$this->get_role()->build();
$this->get_user()->build();
$raid = role_assign($this->get_role()->get('id'), $this->get_user()->get('id'), $this->get_contextid(), $this->get_option('component', ''), $this->get_option('itemid', 0), $this->get_option('timemodified', ''));
$this->set_results($DB->get_record('role_assignments', array('id' => $raid), '*', MUST_EXIST));
}
}
示例7: test_message_get_providers_for_user_more
public function test_message_get_providers_for_user_more()
{
global $DB;
$this->resetAfterTest();
// Create a course.
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
// It would probably be better to use a quiz instance as it has capability controlled messages
// however mod_quiz doesn't have a data generator.
// Instead we're going to use backup notifications and give and take away the capability at various levels.
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$modulecontext = context_module::instance($assign->cmid);
// Create and enrol a teacher.
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
$teacher = $this->getDataGenerator()->create_user();
role_assign($teacherrole->id, $teacher->id, $coursecontext);
$enrolplugin = enrol_get_plugin('manual');
$enrolplugin->add_instance($course);
$enrolinstances = enrol_get_instances($course->id, false);
foreach ($enrolinstances as $enrolinstance) {
if ($enrolinstance->enrol === 'manual') {
break;
}
}
$enrolplugin->enrol_user($enrolinstance, $teacher->id);
// Make the teacher the current user.
$this->setUser($teacher);
// Teacher shouldn't have the required capability so they shouldn't be able to see the backup message.
$this->assertFalse(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
$this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
// Give the user the required capability in an activity module.
// They should now be able to see the backup message.
assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->cmid);
$this->assertTrue(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
$this->assertTrue($this->message_type_present('moodle', 'backup', $providers));
// Prohibit the capability for the user at the course level.
// This overrules the CAP_ALLOW at the module level.
// They should not be able to see the backup message.
assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->cmid);
$this->assertFalse(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
// Actually, handling PROHIBITs would be too expensive. We do not
// care if users with PROHIBITs see a few more preferences than they should.
// $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
}
示例8: assignUserCapability
/**
* Assign a capability to $USER
* The function creates a student $USER if $USER->id is empty
*
* @param string $capability capability name
* @param int $contextid
* @param int $roleid
* @return int the role id - mainly returned for creation, so calling function can reuse it
*/
public static function assignUserCapability($capability, $contextid, $roleid = null)
{
global $USER;
// Create a new student $USER if $USER doesn't exist
if (empty($USER->id)) {
$user = self::getDataGenerator()->create_user();
self::setUser($user);
}
if (empty($roleid)) {
$roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
}
assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
role_assign($roleid, $USER->id, $contextid);
accesslib_clear_all_caches_for_unit_testing();
return $roleid;
}
示例9: get_user_objects
/**
* Returns a user object and its assigned new role.
*
* @param testing_data_generator $generator
* @param $contextid
* @return array The user object and the role ID
*/
protected function get_user_objects(testing_data_generator $generator, $contextid)
{
global $USER;
if (empty($USER->id)) {
$user = $generator->create_user();
$this->setUser($user);
}
$roleid = create_role('Test role', 'testrole', 'Test role description');
if (!is_array($contextid)) {
$contextid = array($contextid);
}
foreach ($contextid as $cid) {
$assignid = role_assign($roleid, $user->id, $cid);
}
return array($user, $roleid);
}
示例10: perform_add
function perform_add($userid)
{
global $CFG;
// Try to add necessary role
$context = get_context_instance(CONTEXT_SYSTEM);
$roleid = $this->check_role();
// If they were able to obtain the correct Moodle role, then
// we will assign them the correct filter role
if (role_assign($roleid, $userid, 0, $context->id)) {
$assign = new stdClass();
$assign->type = $this->type;
$assign->path = $this->path;
$assign->usersid = $userid;
$this->insert_assignment($assign);
}
}
示例11: setUp
public function setUp() {
global $DB;
$this->setAdminUser();
$gen = $this->getDataGenerator();
$this->eventuser = $gen->create_user(); // Create a user.
$course = $gen->create_course(); // Create a course.
// Assign manager role, so user can see reports.
role_assign(1, $this->eventuser->id, context_course::instance($course->id));
// Add a feedback activity to the created course.
$record = new stdClass();
$record->course = $course->id;
$feedback = $gen->create_module('feedback', $record);
$this->eventfeedback = $DB->get_record('feedback', array('id' => $feedback->id), '*', MUST_EXIST); // Get exact copy.
$this->eventcm = get_coursemodule_from_instance('feedback', $this->eventfeedback->id, false, MUST_EXIST);
// Create a feedback item.
$item = new stdClass();
$item->feedback = $this->eventfeedback->id;
$item->type = 'numeric';
$item->presentation = '0|0';
$itemid = $DB->insert_record('feedback_item', $item);
$this->eventfeedbackitem = $DB->get_record('feedback_item', array('id' => $itemid), '*', MUST_EXIST);
// Create a response from a user.
$response = new stdClass();
$response->feedback = $this->eventfeedback->id;
$response->userid = $this->eventuser->id;
$response->anonymous_response = FEEDBACK_ANONYMOUS_YES;
$completedid = $DB->insert_record('feedback_completed', $response);
$this->eventfeedbackcompleted = $DB->get_record('feedback_completed', array('id' => $completedid), '*', MUST_EXIST);
$value = new stdClass();
$value->course_id = $course->id;
$value->item = $this->eventfeedbackitem->id;
$value->completed = $this->eventfeedbackcompleted->id;
$value->value = 25; // User response value.
$valueid = $DB->insert_record('feedback_value', $value);
$this->eventfeedbackvalue = $DB->get_record('feedback_value', array('id' => $valueid), '*', MUST_EXIST);
// Do this in the end to get correct sortorder and cacherev values.
$this->eventcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
}
示例12: role_assigned
public function role_assigned($ra) {
global $DB;
if (!enrol_is_enabled('meta')) {
return true;
}
// prevent circular dependencies - we can not sync meta roles recursively
if ($ra->component === 'enrol_meta') {
return true;
}
//only course level roles are interesting
$parentcontext = get_context_instance_by_id($ra->contextid);
if ($parentcontext->contextlevel != CONTEXT_COURSE) {
return true;
}
// does anything want to sync with this parent?
if (!$enrols = $DB->get_records('enrol', array('customint1'=>$parentcontext->instanceid, 'enrol'=>'meta'), 'id ASC')) {
return true;
}
// make sure the role sync is not prevented
$plugin = enrol_get_plugin('meta');
if ($disabled = $plugin->get_config('nosyncroleids')) {
if (in_array($ra->roleid, explode(',', $disabled))) {
return true;
}
}
foreach ($enrols as $enrol) {
// Is the user enrolled? We want to sync only really enrolled users
if (!$DB->record_exists('user_enrolments', array('userid'=>$ra->userid, 'enrolid'=>$enrol->id))) {
continue;
}
$context = get_context_instance(CONTEXT_COURSE, $enrol->courseid);
// just try to assign role, no problem if role assignment already exists
role_assign($ra->roleid, $ra->userid, $context->id, 'enrol_meta', $enrol->id);
}
return true;
}
示例13: set_up_users
/**
* Set up some base user data
*/
protected function set_up_users()
{
global $DB, $USER;
// Set up a cluster administrator as a PM user.
$clusteradmin = new user(array('idnumber' => 'clusteradmin', 'username' => 'clusteradmin', 'firstname' => 'Cluster', 'lastname' => 'Admin', 'email' => 'cluster@admin.com', 'country' => 'CA'));
$clusteradmin->save();
$USER = $DB->get_record('user', array('username' => 'clusteradmin'));
// Set up a user set member as a PM user.
$clusteruser = new user(array('idnumber' => 'clusteruser', 'username' => 'clusteruser', 'firstname' => 'Cluster', 'lastname' => 'User', 'email' => 'cluster@user.com', 'country' => 'CA'));
$clusteruser->save();
// Set up our test role.
$syscontext = context_system::instance();
$roleid = create_role('clusteradmin', 'clusteradmin', 'clusteradmin');
assign_capability('local/elisprogram:user_edit', CAP_ALLOW, $roleid, $syscontext->id);
// Assign the userset administrator an appropriate role on the userset.
$instance = \local_elisprogram\context\userset::instance(1);
role_assign($roleid, $USER->id, $instance->id);
// Assign the user to the user set.
$clusterassignment = new clusterassignment(array('clusterid' => 1, 'userid' => $clusteruser->id));
$clusterassignment->save();
}
示例14: test_enrolled_course_user_syncstoclass
/**
* Validate that the sync from course role assignment to class instance enrolment works
*/
public function test_enrolled_course_user_syncstoclass()
{
global $CFG, $DB;
require_once elispm::lib('lib.php');
// Set up import data.
$this->load_csv_data();
// Make sure the context is set up.
$crsctx = context_course::instance(100);
// Set up our test role.
$roleid = create_role('gradedrole', 'gradedrole', 'gradedrole');
set_config('gradebookroles', $roleid);
// Create role assignments.
role_assign($roleid, 100, $crsctx->id);
// Attempt the sync.
$sync = new \local_elisprogram\moodle\synchronize();
$sync->synchronize_moodle_class_grades();
// Make sure the student record was created.
$student = student::find();
$this->assertTrue($student->valid());
// Make sure the student has the right class id.
$student = $student->current();
$this->assertEquals(100, $student->classid);
}
示例15: define_execution
protected function define_execution()
{
global $CFG, $DB;
if (!($userid = $this->task->get_userid())) {
return;
}
if (empty($CFG->restorernewroleid)) {
// Bad luck, no fallback role for restorers specified
return;
}
$courseid = $this->get_courseid();
$context = context_course::instance($courseid);
if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
// Current user may access the course (admin, category manager or restored teacher enrolment usually)
return;
}
// Try to add role only - we do not need enrolment if user has moodle/course:view or is already enrolled
role_assign($CFG->restorernewroleid, $userid, $context);
if (is_enrolled($context, $userid, 'moodle/course:update', true) or is_viewing($context, $userid, 'moodle/course:update')) {
// Extra role is enough, yay!
return;
}
// The last chance is to create manual enrol if it does not exist and and try to enrol the current user,
// hopefully admin selected suitable $CFG->restorernewroleid ...
if (!enrol_is_enabled('manual')) {
return;
}
if (!($enrol = enrol_get_plugin('manual'))) {
return;
}
if (!$DB->record_exists('enrol', array('enrol' => 'manual', 'courseid' => $courseid))) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$fields = array('status' => ENROL_INSTANCE_ENABLED, 'enrolperiod' => $enrol->get_config('enrolperiod', 0), 'roleid' => $enrol->get_config('roleid', 0));
$enrol->add_instance($course, $fields);
}
enrol_try_internal_enrol($courseid, $userid);
}