/** * Determine whether the current user can assign users to this userset. * * @return bool Whether the user can assign users to this userset or not. */ public function can_do_add() { $id = $this->required_param('id', PARAM_INT); return usersetpage::can_enrol_into_cluster($id); }
/** * Test whether a user can enrol users into a sub-userset if they have the required capability on the * parent userset. */ public function test_canenrolintoclusterwithparentpermission() { global $DB; $this->load_csv_data(); // Create role with cap: 'local/elisprogram:class_view'. $testrole = new stdClass(); $testrole->name = 'ELIS Sub-Userset Manager'; $testrole->shortname = '_test_ELIS_3848'; $testrole->description = 'ELIS userset enrol into sub-userser'; $testrole->archetype = ''; $testrole->id = create_role($testrole->name, $testrole->shortname, $testrole->description, $testrole->archetype); // Ensure our new role is assignable to ELIS class contexts. set_role_contextlevels($testrole->id, array(CONTEXT_ELIS_USERSET)); // Ensure the role has our required capability assigned. $syscontext = context_system::instance(); assign_capability('local/elisprogram:userset', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_view', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_create', CAP_ALLOW, $testrole->id, $syscontext->id, true); assign_capability('local/elisprogram:userset_enrol_userset_user', CAP_ALLOW, $testrole->id, $syscontext->id, true); $syscontext->mark_dirty(); // Assign a test user a role within the parent userset. $context = \local_elisprogram\context\userset::instance(1); role_assign($testrole->id, 100, $context->id); $context->mark_dirty(); // Switch to testuser. $USER = $DB->get_record('user', array('id' => 100)); $USER->access = get_user_accessdata($USER->id); load_role_access_by_context($testrole->id, $context, $USER->access); // We need to force the accesslib cache to refresh. $GLOBALS['USER'] = $USER; // Check if the user can enrol users into the sub-userset. $this->assertTrue(usersetpage::can_enrol_into_cluster(2)); }
/** * Formats various attributes for human consumption. * @param array $row An array for a single result. * @return array The transformed result. */ protected function results_row_transform(array $row) { static $canenrolintocluster = null; if ($canenrolintocluster === null) { $canenrolintocluster = usersetpage::can_enrol_into_cluster($this->usersetid); } $row = parent::results_row_transform($row); $row['canunassign'] = $canenrolintocluster === true ? '1' : '0'; if (clusteruserpage::can_manage_assoc($row['element_id'], $this->usersetid) !== true) { $row['canunassign'] = '0'; } if (isset($row['clstass_plugin'])) { if ($row['clstass_plugin'] !== 'manual') { $row['canunassign'] = '0'; } $row['clstass_plugin'] = $row['clstass_plugin'] === 'manual' ? 'no' : 'yes'; $row['clstass_plugin'] = get_string($row['clstass_plugin'], 'moodle'); } return $row; }
/** * Unassign the user from the userset. * * @param array $elements An array of elements to perform the action on. * @param bool $bulkaction Whether this is a bulk-action or not. * @return array An array to format as JSON and return to the Javascript. */ protected function _respond_to_js(array $elements, $bulkaction) { global $DB; $usersetid = required_param('id', PARAM_INT); $userset = new userset($usersetid); // Permissions. if (usersetpage::can_enrol_into_cluster($userset->id) !== true) { return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram')); } $unassignnotpermitted = false; foreach ($elements as $userid => $label) { if ($this->can_unassign($usersetid, $userid)) { $assignrec = $DB->get_record(clusterassignment::TABLE, array('userid' => $userid, 'clusterid' => $usersetid)); if (!empty($assignrec) && $assignrec->plugin === 'manual') { $curstu = new clusterassignment($assignrec); $curstu->delete(); } } else { $unassignnotpermitted = true; } } if ($unassignnotpermitted) { return array('result' => 'fail', 'msg' => get_string('not_permitted', 'local_elisprogram')); } else { return array('result' => 'success', 'msg' => 'Success'); } }