Esempio n. 1
0
 /**
  * Execute.
  */
 public function execute()
 {
     mtrace('Sync cohort roles...');
     $result = api::sync_all_cohort_roles();
     mtrace('Added ' . count($result['rolesadded']));
     mtrace('Removed ' . count($result['rolesremoved']));
 }
Esempio n. 2
0
    } else {
        $notification = get_string('cohortroleassignmentnotremoved', 'tool_cohortroles');
        echo $output->notify_problem($notification);
    }
    echo $output->continue_button(new moodle_url($pageurl));
} else {
    if ($data = $form->get_data()) {
        require_sesskey();
        // We must create them all or none.
        $saved = 0;
        // Loop through userids and cohortids only if both of them are not empty.
        if (!empty($data->userids) && !empty($data->cohortids)) {
            foreach ($data->userids as $userid) {
                foreach ($data->cohortids as $cohortid) {
                    $params = (object) array('userid' => $userid, 'cohortid' => $cohortid, 'roleid' => $data->roleid);
                    $result = \tool_cohortroles\api::create_cohort_role_assignment($params);
                    if ($result) {
                        $saved++;
                    }
                }
            }
        }
        if ($saved == 0) {
            $notification = get_string('nocohortroleassignmentssaved', 'tool_cohortroles');
            echo $output->notify_problem($notification);
        } else {
            if ($saved == 1) {
                $notification = get_string('onecohortroleassignmentsaved', 'tool_cohortroles');
                echo $output->notify_success($notification);
            } else {
                $notification = get_string('acohortroleassignmentssaved', 'tool_cohortroles', $saved);
Esempio n. 3
0
 public function test_sync_all_cohort_roles()
 {
     $this->setAdminUser();
     $params = (object) array('userid' => $this->userassignto->id, 'roleid' => $this->roleid, 'cohortid' => $this->cohort->id);
     $result = api::create_cohort_role_assignment($params);
     // Verify roles are assigned when users enter the cohort.
     $sync = api::sync_all_cohort_roles();
     $rolesadded = array(array('useridassignedto' => $this->userassignto->id, 'useridassignedover' => $this->userassignover->id, 'roleid' => $this->roleid));
     $rolesremoved = array();
     $expected = array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
     $this->assertEquals($sync, $expected);
     // Verify roles are removed when users leave the cohort.
     cohort_remove_member($this->cohort->id, $this->userassignover->id);
     $sync = api::sync_all_cohort_roles();
     $rolesadded = array();
     $rolesremoved = array(array('useridassignedto' => $this->userassignto->id, 'useridassignedover' => $this->userassignover->id, 'roleid' => $this->roleid));
     $expected = array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
     $this->assertEquals($sync, $expected);
     // Verify roles assigned by any other component are not removed.
     $usercontext = context_user::instance($this->userassignover->id);
     role_assign($this->roleid, $this->userassignto->id, $usercontext->id);
     $sync = api::sync_all_cohort_roles();
     $rolesadded = array();
     $rolesremoved = array();
     $expected = array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
     $this->assertEquals($sync, $expected);
     // Remove manual role assignment.
     role_unassign($this->roleid, $this->userassignto->id, $usercontext->id);
     // Add someone to the cohort again...
     cohort_add_member($this->cohort->id, $this->userassignover->id);
     $sync = api::sync_all_cohort_roles();
     $rolesadded = array(array('useridassignedto' => $this->userassignto->id, 'useridassignedover' => $this->userassignover->id, 'roleid' => $this->roleid));
     $rolesremoved = array();
     $expected = array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
     $this->assertEquals($sync, $expected);
     // Verify no fatal errors when a cohort is deleted.
     cohort_delete_cohort($this->cohort);
     $sync = api::sync_all_cohort_roles();
     $rolesadded = array();
     $rolesremoved = array(array('useridassignedto' => $this->userassignto->id, 'useridassignedover' => $this->userassignover->id, 'roleid' => $this->roleid));
     $expected = array('rolesadded' => $rolesadded, 'rolesremoved' => $rolesremoved);
     $this->assertEquals($sync, $expected);
 }