Merge branch 'w38_MDL-35062_m24_selfaction' of git://github.com/skodak/moodle
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* CLI update for self enrolments, use for debugging or immediate update
|
||||
* of all courses.
|
||||
*
|
||||
* Notes:
|
||||
* - it is required to use the web server account when executing PHP CLI scripts
|
||||
* - you need to change the "www-data" to match the apache user account
|
||||
* - use "su" if "sudo" not available
|
||||
*
|
||||
* @package enrol_self
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
define('CLI_SCRIPT', true);
|
||||
|
||||
require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
|
||||
// Now get cli options.
|
||||
list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
|
||||
|
||||
if ($unrecognized) {
|
||||
$unrecognized = implode("\n ", $unrecognized);
|
||||
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
||||
}
|
||||
|
||||
if ($options['help']) {
|
||||
$help =
|
||||
"Execute self course enrol updates.
|
||||
|
||||
Options:
|
||||
-v, --verbose Print verbose progress information
|
||||
-h, --help Print out this help
|
||||
|
||||
Example:
|
||||
\$sudo -u www-data /usr/bin/php enrol/self/cli/sync.php
|
||||
";
|
||||
|
||||
echo $help;
|
||||
die;
|
||||
}
|
||||
|
||||
$verbose = !empty($options['verbose']);
|
||||
|
||||
$plugin = enrol_get_plugin('self');
|
||||
|
||||
$result = $plugin->sync(null, $verbose);
|
||||
|
||||
exit($result);
|
||||
@@ -44,6 +44,8 @@ $string['enrolperiod_desc'] = 'Default length of time that the enrolment is vali
|
||||
$string['enrolperiod_help'] = 'Length of time that the enrolment is valid, starting with the moment the user enrols themselves. If disabled, the enrolment duration will be unlimited.';
|
||||
$string['enrolstartdate'] = 'Start date';
|
||||
$string['enrolstartdate_help'] = 'If enabled, users can enrol themselves from this date onward only.';
|
||||
$string['expiredaction'] = 'Enrolment expiration action';
|
||||
$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.';
|
||||
$string['groupkey'] = 'Use group enrolment keys';
|
||||
$string['groupkey_desc'] = 'Use group enrolment keys by default.';
|
||||
$string['groupkey_help'] = 'In addition to restricting access to the course to only those who know the key, use of a group enrolment key means users are automatically added to the group when they enrol in the course.
|
||||
|
||||
+107
-13
@@ -334,31 +334,57 @@ class enrol_self_plugin extends enrol_plugin {
|
||||
* @return void
|
||||
*/
|
||||
public function cron() {
|
||||
$this->sync(null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync all meta course links.
|
||||
*
|
||||
* @param int $courseid one course, empty mean all
|
||||
* @param bool $verbose verbose CLI output
|
||||
* @return int 0 means ok, 1 means error, 2 means plugin disabled
|
||||
*/
|
||||
public function sync($courseid = null, $verbose = false) {
|
||||
global $DB;
|
||||
|
||||
if (!enrol_is_enabled('self')) {
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
|
||||
$plugin = enrol_get_plugin('self');
|
||||
// Unfortunately this may take a long time, execution can be interrupted safely here.
|
||||
@set_time_limit(0);
|
||||
raise_memory_limit(MEMORY_HUGE);
|
||||
|
||||
$now = time();
|
||||
if ($verbose) {
|
||||
mtrace('Verifying self-enrolments...');
|
||||
}
|
||||
|
||||
$params = array('now'=>time(), 'useractive'=>ENROL_USER_ACTIVE, 'courselevel'=>CONTEXT_COURSE);
|
||||
$coursesql = "";
|
||||
if ($courseid) {
|
||||
$coursesql = "AND e.courseid = :courseid";
|
||||
$params['courseid'] = $courseid;
|
||||
}
|
||||
|
||||
// Note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set)
|
||||
// and that user accessed course at least once too (=== user_lastaccess record exists).
|
||||
|
||||
// First deal with users that did not log in for a really long time.
|
||||
// First deal with users that did not log in for a really long time - they do not have user_lastaccess records.
|
||||
$sql = "SELECT e.*, ue.userid
|
||||
FROM {user_enrolments} ue
|
||||
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
|
||||
JOIN {user} u ON u.id = ue.userid
|
||||
WHERE :now - u.lastaccess > e.customint2";
|
||||
$rs = $DB->get_recordset_sql($sql, array('now'=>$now));
|
||||
WHERE :now - u.lastaccess > e.customint2
|
||||
$coursesql";
|
||||
$rs = $DB->get_recordset_sql($sql, $params);
|
||||
foreach ($rs as $instance) {
|
||||
$userid = $instance->userid;
|
||||
unset($instance->userid);
|
||||
$plugin->unenrol_user($instance, $userid);
|
||||
mtrace("unenrolling user $userid from course $instance->courseid as they have did not log in for $instance->customint2 days");
|
||||
$this->unenrol_user($instance, $userid);
|
||||
if ($verbose) {
|
||||
$days = $instance->customint2 / 60*60*24;
|
||||
mtrace(" unenrolling user $userid from course $instance->courseid as they have did not log in for at least $days days");
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
@@ -367,17 +393,85 @@ class enrol_self_plugin extends enrol_plugin {
|
||||
FROM {user_enrolments} ue
|
||||
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0)
|
||||
JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid)
|
||||
WHERE :now - ul.timeaccess > e.customint2";
|
||||
$rs = $DB->get_recordset_sql($sql, array('now'=>$now));
|
||||
WHERE :now - ul.timeaccess > e.customint2
|
||||
$coursesql";
|
||||
$rs = $DB->get_recordset_sql($sql, $params);
|
||||
foreach ($rs as $instance) {
|
||||
$userid = $instance->userid;
|
||||
unset($instance->userid);
|
||||
$plugin->unenrol_user($instance, $userid);
|
||||
mtrace("unenrolling user $userid from course $instance->courseid as they have did not access course for $instance->customint2 days");
|
||||
$this->unenrol_user($instance, $userid);
|
||||
if ($verbose) {
|
||||
$days = $instance->customint2 / 60*60*24;
|
||||
mtrace(" unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days");
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
flush();
|
||||
// Deal with expired accounts.
|
||||
$action = $this->get_config('expiredaction', ENROL_EXT_REMOVED_KEEP);
|
||||
|
||||
if ($action == ENROL_EXT_REMOVED_UNENROL) {
|
||||
$instances = array();
|
||||
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
|
||||
FROM {user_enrolments} ue
|
||||
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self')
|
||||
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
|
||||
WHERE ue.timeend > 0 AND ue.timeend < :now
|
||||
$coursesql";
|
||||
$rs = $DB->get_recordset_sql($sql, $params);
|
||||
foreach ($rs as $ue) {
|
||||
if (empty($instances[$ue->enrolid])) {
|
||||
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
|
||||
}
|
||||
$instance = $instances[$ue->enrolid];
|
||||
if ($instance->roleid) {
|
||||
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
|
||||
}
|
||||
$this->unenrol_user($instance, $ue->userid);
|
||||
if ($verbose) {
|
||||
mtrace(" unenrolling expired user $ue->userid from course $instance->courseid");
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
||||
} else if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
|
||||
$instances = array();
|
||||
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
|
||||
FROM {user_enrolments} ue
|
||||
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self')
|
||||
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
|
||||
WHERE ue.timeend > 0 AND ue.timeend < :now
|
||||
AND ue.status = :useractive
|
||||
$coursesql";
|
||||
$rs = $DB->get_recordset_sql($sql, $params);
|
||||
foreach ($rs as $ue) {
|
||||
if (empty($instances[$ue->enrolid])) {
|
||||
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
|
||||
}
|
||||
$instance = $instances[$ue->enrolid];
|
||||
if (1 == $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid))) {
|
||||
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
|
||||
} else if ($instance->roleid) {
|
||||
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
|
||||
}
|
||||
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
|
||||
if ($verbose) {
|
||||
mtrace(" suspending expired user $ue->userid in course $instance->courseid");
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
unset($instances);
|
||||
|
||||
} else {
|
||||
// ENROL_EXT_REMOVED_KEEP means no changes.
|
||||
}
|
||||
|
||||
if ($verbose) {
|
||||
mtrace('...user self-enrolment updates finished.');
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,6 +38,15 @@ if ($ADMIN->fulltree) {
|
||||
$settings->add(new admin_setting_configcheckbox('enrol_self/showhint',
|
||||
get_string('showhint', 'enrol_self'), get_string('showhint_desc', 'enrol_self'), 0));
|
||||
|
||||
// Note: let's reuse the ext sync constants and strings here, internally it is very similar,
|
||||
// it describes what should happend when users are not supposed to be enerolled any more.
|
||||
$options = array(
|
||||
ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'),
|
||||
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'),
|
||||
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
|
||||
);
|
||||
$settings->add(new admin_setting_configselect('enrol_self/expiredaction', get_string('expiredaction', 'enrol_self'), get_string('expiredaction_help', 'enrol_self'), ENROL_EXT_REMOVED_KEEP, $options));
|
||||
|
||||
//--- enrol instance defaults ----------------------------------------------------------------------------
|
||||
$settings->add(new admin_setting_heading('enrol_self_defaults',
|
||||
get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin')));
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Self enrolment plugin tests.
|
||||
*
|
||||
* @package enrol_self
|
||||
* @category phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot.'/enrol/self/lib.php');
|
||||
require_once($CFG->dirroot.'/enrol/self/locallib.php');
|
||||
|
||||
class enrol_self_testcase extends advanced_testcase {
|
||||
|
||||
public function test_basics() {
|
||||
$this->assertTrue(enrol_is_enabled('self'));
|
||||
$plugin = enrol_get_plugin('self');
|
||||
$this->assertInstanceOf('enrol_self_plugin', $plugin);
|
||||
$this->assertEquals(1, get_config('enrol_self', 'defaultenrol'));
|
||||
$this->assertEquals(ENROL_EXT_REMOVED_KEEP, get_config('enrol_self', 'expiredaction'));
|
||||
}
|
||||
|
||||
public function test_sync_nothing() {
|
||||
global $SITE;
|
||||
|
||||
$selfplugin = enrol_get_plugin('self');
|
||||
|
||||
// Just make sure the sync does not throw any errors when nothing to do.
|
||||
$selfplugin->sync(NULL, false);
|
||||
$selfplugin->sync($SITE->id, false);
|
||||
}
|
||||
|
||||
public function test_longtimnosee() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$selfplugin = enrol_get_plugin('self');
|
||||
$manualplugin = enrol_get_plugin('manual');
|
||||
$this->assertNotEmpty($manualplugin);
|
||||
|
||||
$now = time();
|
||||
|
||||
// Prepare some data.
|
||||
|
||||
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
||||
$this->assertNotEmpty($studentrole);
|
||||
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
|
||||
$this->assertNotEmpty($teacherrole);
|
||||
|
||||
$record = array('firstaccess'=>$now-60*60*24*800);
|
||||
$record['lastaccess'] = $now-60*60*24*100;
|
||||
$user1 = $this->getDataGenerator()->create_user($record);
|
||||
$record['lastaccess'] = $now-60*60*24*10;
|
||||
$user2 = $this->getDataGenerator()->create_user($record);
|
||||
$record['lastaccess'] = $now-60*60*24*1;
|
||||
$user3 = $this->getDataGenerator()->create_user($record);
|
||||
$record['lastaccess'] = $now-10;
|
||||
$user4 = $this->getDataGenerator()->create_user($record);
|
||||
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$course3 = $this->getDataGenerator()->create_course();
|
||||
$context1 = context_course::instance($course1->id);
|
||||
$context2 = context_course::instance($course2->id);
|
||||
$context3 = context_course::instance($course3->id);
|
||||
|
||||
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
|
||||
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
|
||||
$instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST);
|
||||
unset($id);
|
||||
|
||||
$this->assertEquals($studentrole->id, $instance1->roleid);
|
||||
$instance1->customint2 = 60*60*24*14;
|
||||
$DB->update_record('enrol', $instance1);
|
||||
$selfplugin->enrol_user($instance1, $user1->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance1, $user2->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance1, $user3->id, $studentrole->id);
|
||||
$this->assertEquals(3, $DB->count_records('user_enrolments'));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*20));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*2));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60));
|
||||
|
||||
$this->assertEquals($studentrole->id, $instance3->roleid);
|
||||
$instance3->customint2 = 60*60*24*50;
|
||||
$DB->update_record('enrol', $instance3);
|
||||
$selfplugin->enrol_user($instance3, $user1->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance3, $user2->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance3, $user3->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id);
|
||||
$selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id);
|
||||
$this->assertEquals(8, $DB->count_records('user_enrolments'));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*11));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200));
|
||||
$DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200));
|
||||
|
||||
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
||||
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
||||
|
||||
$manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id);
|
||||
$manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id);
|
||||
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(9, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
|
||||
// Execute sync - this is the same thing used from cron.
|
||||
|
||||
$selfplugin->sync($course2->id, false);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
|
||||
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
||||
$selfplugin->sync(null, false);
|
||||
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
||||
|
||||
$this->assertEquals(6, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
}
|
||||
|
||||
public function test_expired() {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$selfplugin = enrol_get_plugin('self');
|
||||
$manualplugin = enrol_get_plugin('manual');
|
||||
$this->assertNotEmpty($manualplugin);
|
||||
|
||||
$now = time();
|
||||
|
||||
// Prepare some data.
|
||||
|
||||
$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);
|
||||
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
$user3 = $this->getDataGenerator()->create_user();
|
||||
$user4 = $this->getDataGenerator()->create_user();
|
||||
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$course3 = $this->getDataGenerator()->create_course();
|
||||
$context1 = context_course::instance($course1->id);
|
||||
$context2 = context_course::instance($course2->id);
|
||||
$context3 = context_course::instance($course3->id);
|
||||
|
||||
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
|
||||
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$this->assertEquals($studentrole->id, $instance1->roleid);
|
||||
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$this->assertEquals($studentrole->id, $instance2->roleid);
|
||||
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
|
||||
$this->assertEquals($studentrole->id, $instance3->roleid);
|
||||
$id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
|
||||
$instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST);
|
||||
$this->assertEquals($teacherrole->id, $instance3b->roleid);
|
||||
unset($id);
|
||||
|
||||
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
||||
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
||||
|
||||
$manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id);
|
||||
$manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id);
|
||||
|
||||
$this->assertEquals(2, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
|
||||
$selfplugin->enrol_user($instance1, $user1->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance1, $user2->id, $studentrole->id);
|
||||
$selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
|
||||
|
||||
$selfplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0);
|
||||
$selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now-60*60);
|
||||
$selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now+60*60);
|
||||
$selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60);
|
||||
$selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id);
|
||||
|
||||
role_assign($managerrole->id, $user3->id, $context1->id);
|
||||
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
|
||||
// Execute tests.
|
||||
|
||||
$this->assertEquals(ENROL_EXT_REMOVED_KEEP, $selfplugin->get_config('expiredaction'));
|
||||
$selfplugin->sync(null, false);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
|
||||
|
||||
$selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
|
||||
$selfplugin->sync($course2->id, false);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
|
||||
$selfplugin->sync(null, false);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
|
||||
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
|
||||
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
|
||||
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
|
||||
|
||||
|
||||
$selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
|
||||
|
||||
role_assign($studentrole->id, $user3->id, $context1->id);
|
||||
role_assign($studentrole->id, $user2->id, $context3->id);
|
||||
role_assign($teacherrole->id, $user1->id, $context3->id);
|
||||
$this->assertEquals(10, $DB->count_records('user_enrolments'));
|
||||
$this->assertEquals(10, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
|
||||
$selfplugin->sync(null, false);
|
||||
$this->assertEquals(7, $DB->count_records('user_enrolments'));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user2->id)));
|
||||
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3b->id, 'userid'=>$user1->id)));
|
||||
$this->assertEquals(6, $DB->count_records('role_assignments'));
|
||||
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
||||
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2012082300; // The current plugin version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2012082300; // Requires this Moodle version
|
||||
$plugin->version = 2012091500; // The current plugin version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2012091400; // Requires this Moodle version
|
||||
$plugin->component = 'enrol_self'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 180;
|
||||
$plugin->cron = 600;
|
||||
|
||||
Reference in New Issue
Block a user