Merge branch 'MDL-66712-master-selfautounenrol' of git://github.com/mudrd8mz/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2020-01-20 22:27:20 +01:00
commit a05b402c12
2 changed files with 22 additions and 6 deletions

View File

@ -450,8 +450,9 @@ class enrol_self_plugin extends enrol_plugin {
$userid = $instance->userid; $userid = $instance->userid;
unset($instance->userid); unset($instance->userid);
$this->unenrol_user($instance, $userid); $this->unenrol_user($instance, $userid);
$days = $instance->customint2 / 60*60*24; $days = $instance->customint2 / DAYSECS;
$trace->output("unenrolling user $userid from course $instance->courseid as they have did not log in for at least $days days", 1); $trace->output("unenrolling user $userid from course $instance->courseid " .
"as they did not log in for at least $days days", 1);
} }
$rs->close(); $rs->close();
@ -467,8 +468,9 @@ class enrol_self_plugin extends enrol_plugin {
$userid = $instance->userid; $userid = $instance->userid;
unset($instance->userid); unset($instance->userid);
$this->unenrol_user($instance, $userid); $this->unenrol_user($instance, $userid);
$days = $instance->customint2 / 60*60*24; $days = $instance->customint2 / DAYSECS;
$trace->output("unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days", 1); $trace->output("unenrolling user $userid from course $instance->courseid " .
"as they did not access the course for at least $days days", 1);
} }
$rs->close(); $rs->close();

View File

@ -61,7 +61,7 @@ class enrol_self_testcase extends advanced_testcase {
$now = time(); $now = time();
$trace = new null_progress_trace(); $trace = new progress_trace_buffer(new text_progress_trace(), false);
// Prepare some data. // Prepare some data.
@ -133,18 +133,32 @@ class enrol_self_testcase extends advanced_testcase {
// Execute sync - this is the same thing used from cron. // Execute sync - this is the same thing used from cron.
$selfplugin->sync($trace, $course2->id); $selfplugin->sync($trace, $course2->id);
$output = $trace->get_buffer();
$trace->reset_buffer();
$this->assertEquals(10, $DB->count_records('user_enrolments')); $this->assertEquals(10, $DB->count_records('user_enrolments'));
$this->assertStringContainsString('No expired enrol_self enrolments detected', $output);
$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'=>$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'=>$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'=>$user1->id)));
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
$selfplugin->sync($trace, null); $selfplugin->sync($trace, null);
$output = $trace->get_buffer();
$trace->reset_buffer();
$this->assertEquals(6, $DB->count_records('user_enrolments')); $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'=>$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'=>$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'=>$user1->id)));
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id))); $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
$this->assertStringContainsString('unenrolling user ' . $user1->id . ' from course ' . $course1->id .
' as they did not log in for at least 14 days', $output);
$this->assertStringContainsString('unenrolling user ' . $user1->id . ' from course ' . $course3->id .
' as they did not log in for at least 50 days', $output);
$this->assertStringContainsString('unenrolling user ' . $user2->id . ' from course ' . $course1->id .
' as they did not access the course for at least 14 days', $output);
$this->assertStringContainsString('unenrolling user ' . $user3->id . ' from course ' . $course3->id .
' as they did not access the course for at least 50 days', $output);
$this->assertStringNotContainsString('unenrolling user ' . $user4->id, $output);
$this->assertEquals(6, $DB->count_records('role_assignments')); $this->assertEquals(6, $DB->count_records('role_assignments'));
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id))); $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));