コード例 #1
0
ファイル: cohortlib_test.php プロジェクト: evltuma/moodle
 public function test_cohort_delete_cohort_event()
 {
     $this->resetAfterTest();
     $cohort = $this->getDataGenerator()->create_cohort();
     // Capture the events.
     $sink = $this->redirectEvents();
     // Perform the delete.
     cohort_delete_cohort($cohort);
     $events = $sink->get_events();
     $sink->close();
     // Validate the event structure.
     $this->assertCount(1, $events);
     $event = $events[0];
     $this->assertInstanceOf('\\core\\event\\cohort_deleted', $event);
     $this->assertEquals('cohort', $event->objecttable);
     $this->assertEquals($cohort->id, $event->objectid);
     $url = new moodle_url('/cohort/index.php', array('contextid' => $event->contextid));
     $this->assertEquals($url, $event->get_url());
     $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id));
     $this->assertEventLegacyData($cohort, $event);
     $this->assertEventContextNotUsed($event);
 }
コード例 #2
0
ファイル: convert.php プロジェクト: parksandwildlife/learning
        redirect($returnurl);
        break;
    case 'restore':
        if ($clist) {
            list($usql, $params) = $DB->get_in_or_equal($clist);
            $DB->set_field_select('cohort', 'component', '', 'id ' . $usql, $params);
        }
        redirect($returnurl);
        break;
    case 'delete':
        if ($clist) {
            set_time_limit(0);
            echo $OUTPUT->header();
            echo $OUTPUT->heading(get_string('auth_cohorttoolmcae', 'auth_mcae'));
            $progress = new progress_bar('delcohort');
            $progress->create();
            $delcount = count($clist);
            $delcurrent = 1;
            foreach ($clist as $cid) {
                $cohort = $DB->get_record('cohort', array('contextid' => $context->id, 'id' => $cid));
                cohort_delete_cohort($cohort);
                $progress->update($delcurrent, $delcount, "{$delcurrent} / {$delcount}");
                $delcurrent++;
            }
        }
        echo $OUTPUT->continue_button($returnurl);
        echo $OUTPUT->footer();
        die;
        break;
}
echo $OUTPUT->footer();
コード例 #3
0
 /**
  * cron synchronization script
  *
  * @param int $do_updates true to update existing accounts
  *
  * @return int       
  */
 function sync_users($do_updates = false)
 {
     global $CFG, $DB;
     // process users in Moodle that no longer exist in Drupal
     $remote_user = $this->config->remote_user;
     $remote_pw = $this->config->remote_pw;
     $base_url = $this->config->host_uri;
     $apiObj = new RemoteAPI($base_url);
     // Required for authentication, and all other operations:
     $ret = $apiObj->Login($remote_user, $remote_pw, true);
     if ($ret->info['http_code'] == 404) {
         die("ERROR: Login service unreachable!\n");
     }
     if ($ret->info['http_code'] == 401) {
         die("ERROR: Login failed - check username and password!\n");
     } elseif ($ret->info['http_code'] !== 200) {
         $error = "ERROR: Login to drupal failed with http code " . $ret->info['http_code'];
         if (!empty($ret->error)) {
             $error .= PHP_EOL . $ret->error . PHP_EOL;
         }
         die($error);
     }
     // list external users since last update
     $vid = isset($this->config->last_vid) ? $this->config->last_vid : 0;
     $pagesize = $this->config->pagesize;
     $page = 0;
     $drupal_users = $apiObj->Index('user', "?vid={$vid},page={$page},pagesize={$pagesize}");
     if (is_null($drupal_users) || empty($drupal_users)) {
         die("ERROR: Problems trying to get index of users!\n");
     }
     // sync users in Drupal with users in Moodle (adding users if needed)
     print_string('auth_drupalservicesuserstoupdate', 'auth_drupalservices', count($drupal_users));
     foreach ($drupal_users as $drupal_user_info) {
         // get the full user object rather than the prototype from the index service
         // merge the listing and the full value because if the user is blocked, a full user will not be retrieved
         $drupal_user = (array) $drupal_user_info + (array) $apiObj->Index("user/{$drupal_user_info->uid}");
         // recast drupaluser as an object
         $drupal_user = (object) $drupal_user;
         // the drupal services module strips off the mail attribute if the user requested is not
         // either the user requesting, or a user with administer users permission.
         // luckily the updates service has the value, so we have to copy it over.
         $drupal_user->mail = $drupal_user_info->mail;
         if ($drupal_user_info->uid < 1) {
             //No anon
             print "Skipping anon user - uid {$drupal_user->uid}\n";
             continue;
         }
         print_string('auth_drupalservicesupdateuser', 'auth_drupalservices', array($drupal_user->name . '(' . $drupal_user->uid . ')'));
         $user = $this->create_update_user($drupal_user);
         if (empty($user)) {
             // Something went wrong while creating the user
             print_error('auth_drupalservicescreateaccount', 'auth_drupalservices', array($drupal_user->name));
             continue;
             //Next user
         }
     }
     // now that all the latest updates have been imported, store the revision point we are at.
     set_config('last_vid', $drupal_user->vid, 'auth_drupalservices');
     // Now do cohorts
     if ($this->config->cohorts != 0) {
         $cohort_view = $this->config->cohort_view;
         print "Updating cohorts using services view - {$cohort_view}\n";
         $context = context_system::instance();
         //$processed_cohorts_list = array();
         $drupal_cohorts = $apiObj->Index($cohort_view);
         if (is_null($drupal_cohorts)) {
             print "ERROR: Error retreiving cohorts!\n";
         } else {
             // OK First lets create any Moodle cohorts that are in drupal.
             foreach ($drupal_cohorts as $drupal_cohort) {
                 if ($drupal_cohort->cohort_name == '') {
                     continue;
                     // We don't want an empty cohort name
                 }
                 $drupal_cohort_list[] = $drupal_cohort->cohort_name;
                 if (!$this->cohort_exists($drupal_cohort->cohort_name)) {
                     $newcohort = new stdClass();
                     $newcohort->name = $drupal_cohort->cohort_name;
                     $newcohort->idnumber = $drupal_cohort->cohort_id;
                     $newcohort->description = $drupal_cohort->cohort_description;
                     $newcohort->contextid = $context->id;
                     $newcohort->component = 'auth_drupalservices';
                     $cid = cohort_add_cohort($newcohort);
                     print "Cohort {$drupal_cohort->cohort_name} ({$cid}) created!\n";
                 }
             }
             // Next lets delete any Moodle cohorts that are not in drupal.
             // Now create a unique array
             $drupal_cohort_list = array_unique($drupal_cohort_list);
             //print_r($drupal_cohort_list);
             $moodle_cohorts = $this->moodle_cohorts();
             //print_r($moodle_cohorts);
             foreach ($moodle_cohorts as $moodle_cohort) {
                 if (array_search($moodle_cohort->name, $drupal_cohort_list) === false) {
                     print "{$moodle_cohort->name} not in drupal - deleteing\n";
                     cohort_delete_cohort($moodle_cohort);
                 }
                 $moodle_cohorts_list[$moodle_cohort->id] = $moodle_cohort->name;
             }
             // Cool. Now lets go through each user and add them to cohorts.
             // arrays to use? $userlist - list of uids.
             // $drupal_cohorts - view. $drupal_cohorts_list. Moodle lists.
             foreach ($userlist as $uid) {
                 $drupal_user_cohort_list = array();
                 //print "$uid\n";
                 $user = $DB->get_record('user', array('idnumber' => $uid, 'mnethostid' => $CFG->mnet_localhost_id));
                 // Get array of cohort names this user belongs to.
                 $drupal_user_cohorts = $this->drupal_user_cohorts($uid, $drupal_cohorts);
                 foreach ($drupal_user_cohorts as $drupal_user_cohort) {
                     //get the cohort id frm the moodle list.
                     $cid = array_search($drupal_user_cohort->cohort_name, $moodle_cohorts_list);
                     //print "$cid\n";
                     if (!$DB->record_exists('cohort_members', array('cohortid' => $cid, 'userid' => $user->id))) {
                         cohort_add_member($cid, $user->id);
                         print "Added {$user->username} ({$user->id}) to cohort {$drupal_user_cohort->cohort_name}\n";
                     }
                     // Create a list of enrolled cohorts to use later.
                     $drupal_user_cohort_list[] = $cid;
                 }
                 // Cool. now get this users list of moodle cohorts and compare
                 // with drupal. remove from moodle if needed.
                 $moodle_user_cohorts = $this->moodle_user_cohorts($user);
                 //print_r($moodle_user_cohorts);
                 foreach ($moodle_user_cohorts as $moodle_user_cohort) {
                     if (array_search($moodle_user_cohort->cid, $drupal_user_cohort_list) === false) {
                         cohort_remove_member($moodle_user_cohort->cid, $user->id);
                         print "Removed {$user->username} ({$user->id}) from cohort {$moodle_user_cohort->name}\n";
                     }
                 }
             }
         }
     }
     // End of cohorts
     //LOGOUT
     if (get_config('auth_drupalservices', 'call_logout_service')) {
         $ret = $apiObj->Logout();
         if (is_null($ret)) {
             print "ERROR logging out!\n";
         } else {
             print "Logged out from drupal services\n";
         }
     }
 }
コード例 #4
0
ファイル: hooks_test.php プロジェクト: evltuma/moodle
 public function test_hook_cohort_deleted()
 {
     $this->resetAfterTest();
     $this->setAdminUser();
     $datagen = $this->getDataGenerator();
     $corecompgen = $datagen->get_plugin_generator('core_competency');
     $c1 = $datagen->create_cohort();
     $c2 = $datagen->create_cohort();
     $t1 = $corecompgen->create_template();
     $t2 = $corecompgen->create_template();
     // Create the template cohorts.
     core_competency\api::create_template_cohort($t1->get_id(), $c1->id);
     core_competency\api::create_template_cohort($t1->get_id(), $c2->id);
     core_competency\api::create_template_cohort($t2->get_id(), $c1->id);
     // Check that the association was made.
     $this->assertEquals(2, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id())));
     $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id())));
     // Delete the first cohort.
     cohort_delete_cohort($c1);
     // Check that the association was removed.
     $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id())));
     $this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id())));
 }
コード例 #5
0
ファイル: sync_test.php プロジェクト: stronk7/moodle
 public function test_handler_sync()
 {
     global $DB;
     $this->resetAfterTest();
     $trace = new null_progress_trace();
     // Setup a few courses and categories.
     $cohortplugin = enrol_get_plugin('cohort');
     $manualplugin = enrol_get_plugin('manual');
     $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     $this->assertNotEmpty($studentrole);
     $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
     $this->assertNotEmpty($teacherrole);
     $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
     $this->assertNotEmpty($managerrole);
     $cat1 = $this->getDataGenerator()->create_category();
     $cat2 = $this->getDataGenerator()->create_category();
     $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
     $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
     $course3 = $this->getDataGenerator()->create_course(array('category' => $cat2->id));
     $course4 = $this->getDataGenerator()->create_course(array('category' => $cat2->id));
     $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     $user1 = $this->getDataGenerator()->create_user();
     $user2 = $this->getDataGenerator()->create_user();
     $user3 = $this->getDataGenerator()->create_user();
     $user4 = $this->getDataGenerator()->create_user();
     $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat1->id)->id));
     $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat2->id)->id));
     $cohort3 = $this->getDataGenerator()->create_cohort();
     $this->enable_plugin();
     $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
     $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
     $this->assertEquals(2, $DB->count_records('role_assignments', array()));
     $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
     $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort1->id, 'roleid' => $studentrole->id));
     $cohortinstance1 = $DB->get_record('enrol', array('id' => $id));
     $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort2->id, 'roleid' => $teacherrole->id));
     $cohortinstance2 = $DB->get_record('enrol', array('id' => $id));
     $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id));
     $cohortinstance3 = $DB->get_record('enrol', array('id' => $id));
     $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
     $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
     // Test cohort member add event.
     cohort_add_member($cohort1->id, $user1->id);
     cohort_add_member($cohort1->id, $user2->id);
     cohort_add_member($cohort1->id, $user4->id);
     $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
     $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user1->id)));
     $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user2->id)));
     $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user4->id)));
     $this->assertEquals(5, $DB->count_records('role_assignments', array()));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user1->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user2->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user4->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     cohort_add_member($cohort2->id, $user3->id);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance2->id, 'userid' => $user3->id)));
     $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance3->id, 'userid' => $user3->id)));
     $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->id, 'userid' => $user3->id)));
     $this->assertEquals(7, $DB->count_records('role_assignments', array()));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user3->id, 'roleid' => $teacherrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance2->id)));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course2->id)->id, 'userid' => $user3->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance3->id)));
     cohort_add_member($cohort3->id, $user3->id);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(7, $DB->count_records('role_assignments', array()));
     // Test cohort remove action.
     $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
     cohort_remove_member($cohort1->id, $user2->id);
     cohort_remove_member($cohort1->id, $user4->id);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(5, $DB->count_records('role_assignments', array()));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user2->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user4->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     cohort_add_member($cohort1->id, $user2->id);
     cohort_add_member($cohort1->id, $user4->id);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(7, $DB->count_records('role_assignments', array()));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user2->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertTrue($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user4->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
     cohort_remove_member($cohort1->id, $user2->id);
     cohort_remove_member($cohort1->id, $user4->id);
     $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
     $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user2->id)));
     $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance1->id, 'userid' => $user4->id)));
     $this->assertEquals(5, $DB->count_records('role_assignments', array()));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user2->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user4->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     cohort_remove_member($cohort2->id, $user3->id);
     $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
     $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance2->id, 'userid' => $user3->id)));
     $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance3->id, 'userid' => $user3->id)));
     $this->assertEquals(3, $DB->count_records('role_assignments', array()));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course1->id)->id, 'userid' => $user3->id, 'roleid' => $teacherrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance2->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('contextid' => context_course::instance($course2->id)->id, 'userid' => $user3->id, 'roleid' => $studentrole->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance3->id)));
     // Test cohort deleting.
     cohort_add_member($cohort1->id, $user2->id);
     cohort_add_member($cohort1->id, $user4->id);
     cohort_add_member($cohort2->id, $user3->id);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(7, $DB->count_records('role_assignments', array()));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
     cohort_delete_cohort($cohort2);
     $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(5, $DB->count_records('role_assignments', array()));
     $cohortinstance2 = $DB->get_record('enrol', array('id' => $cohortinstance2->id), '*', MUST_EXIST);
     $cohortinstance3 = $DB->get_record('enrol', array('id' => $cohortinstance3->id), '*', MUST_EXIST);
     $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
     $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
     $this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance2->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance3->id)));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
     cohort_delete_cohort($cohort1);
     $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(2, $DB->count_records('role_assignments', array()));
     $this->assertFalse($DB->record_exists('enrol', array('id' => $cohortinstance1->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     // Cleanup after previous test (remove the extra user_enrolment).
     enrol_cohort_sync($trace, $course1->id);
     $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
     // Test group sync.
     $id = groups_create_group((object) array('name' => 'Group 1', 'courseid' => $course1->id));
     $group1 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
     $id = groups_create_group((object) array('name' => 'Group 2', 'courseid' => $course1->id));
     $group2 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
     $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($cat1->id)->id));
     $id = $cohortplugin->add_instance($course1, array('customint1' => $cohort1->id, 'roleid' => $studentrole->id, 'customint2' => $group1->id));
     $cohortinstance1 = $DB->get_record('enrol', array('id' => $id));
     $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(2, $DB->count_records('role_assignments', array()));
     $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
     $this->assertTrue(groups_add_member($group1, $user4));
     $this->assertTrue(groups_add_member($group2, $user4));
     $this->assertFalse(groups_is_member($group1->id, $user1->id));
     cohort_add_member($cohort1->id, $user1->id);
     $this->assertTrue(groups_is_member($group1->id, $user1->id));
     $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user1->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     cohort_add_member($cohort1->id, $user4->id);
     $this->assertTrue(groups_is_member($group1->id, $user4->id));
     $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $group1->id, 'userid' => $user4->id, 'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
     cohort_remove_member($cohort1->id, $user1->id);
     $this->assertFalse(groups_is_member($group1->id, $user1->id));
     cohort_remove_member($cohort1->id, $user4->id);
     $this->assertTrue(groups_is_member($group1->id, $user4->id));
     $this->assertTrue(groups_is_member($group2->id, $user4->id));
     $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
     cohort_add_member($cohort1->id, $user1->id);
     cohort_remove_member($cohort1->id, $user1->id);
     $this->assertTrue(groups_is_member($group1->id, $user1->id));
     // Test deleting of instances.
     cohort_add_member($cohort1->id, $user1->id);
     cohort_add_member($cohort1->id, $user2->id);
     cohort_add_member($cohort1->id, $user3->id);
     $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(5, $DB->count_records('role_assignments', array()));
     $this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertEquals(5, $DB->count_records('groups_members', array()));
     $this->assertEquals(3, $DB->count_records('groups_members', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $cohortplugin->delete_instance($cohortinstance1);
     $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
     $this->assertEquals(2, $DB->count_records('role_assignments', array()));
     $this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
     $this->assertEquals(2, $DB->count_records('groups_members', array()));
     $this->assertEquals(0, $DB->count_records('groups_members', array('component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 }
コード例 #6
0
ファイル: externallib.php プロジェクト: alanaipe2015/moodle
 /**
  * Delete cohorts
  *
  * @param array $cohortids
  * @return null
  * @since Moodle 2.5
  */
 public static function delete_cohorts($cohortids)
 {
     global $CFG, $DB;
     require_once "{$CFG->dirroot}/cohort/lib.php";
     $params = self::validate_parameters(self::delete_cohorts_parameters(), array('cohortids' => $cohortids));
     $transaction = $DB->start_delegated_transaction();
     foreach ($params['cohortids'] as $cohortid) {
         // Validate params.
         $cohortid = validate_param($cohortid, PARAM_INT);
         $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
         // Now security checks.
         $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
         if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
             throw new invalid_parameter_exception('Invalid context');
         }
         self::validate_context($context);
         require_capability('moodle/cohort:manage', $context);
         cohort_delete_cohort($cohort);
     }
     $transaction->allow_commit();
     return null;
 }
コード例 #7
0
ファイル: api_test.php プロジェクト: evltuma/moodle
 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);
 }
コード例 #8
0
ファイル: prcimport.php プロジェクト: xhippy01/prcimport
function remove_cohort($id, $delete)
{
    global $DB;
    if ($id) {
        $cohort = $DB->get_record('cohort', array('id' => $id));
        if (!empty($cohort)) {
            $context = context::instance_by_id($cohort->contextid);
        }
        if ($delete == 1) {
            if (!empty($cohort)) {
                cohort_delete_cohort($cohort);
                echo "Deleted Cohort:" . $cohort->id . PHP_EOL;
            } else {
                echo "No such cohort:" . $id . PHP_EOL;
            }
        }
    } else {
        $context = context::instance_by_id($contextid);
        if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
            //print_error('invalidcontext');
            echo "Invalid Context" . PHP_EOL;
        }
        $cohort = new stdClass();
        $cohort->id = 0;
        $cohort->contextid = $context->id;
        $cohort->name = '';
        $cohort->description = '';
    }
}
コード例 #9
0
 public function test_cohort_delete_cohort()
 {
     global $DB;
     $this->resetAfterTest();
     $cohort = $this->getDataGenerator()->create_cohort();
     cohort_delete_cohort($cohort);
     $this->assertFalse($DB->record_exists('cohort', array('id' => $cohort->id)));
 }