MDL-58116 core_enrol: fix handling of users on different methods

This commit is contained in:
barrasroger 2017-04-18 13:28:08 +02:00
parent 0714bcb2a2
commit 499d37755c
2 changed files with 67 additions and 7 deletions

View file

@ -280,6 +280,62 @@ class core_enrollib_testcase extends advanced_testcase {
$this->assertEquals($sharedcourse->id, $course1->id); $this->assertEquals($sharedcourse->id, $course1->id);
} }
public function test_enrol_get_shared_courses_different_methods() {
global $DB, $CFG;
require_once($CFG->dirroot . '/enrol/self/externallib.php');
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
// Enrol user1 and user2 in course1 with a different enrolment methode.
// Add self enrolment method for course1.
$selfplugin = enrol_get_plugin('self');
$this->assertNotEmpty($selfplugin);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
$instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance 1',
'customint6' => 1,
'roleid' => $studentrole->id));
$instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
self::setUser($user2);
// Self enrol me (user2).
$result = enrol_self_external::enrol_user($course1->id);
// Enrol user1 manually.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
$course2 = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user1->id, $course2->id);
$course3 = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user2->id, $course3->id);
// Test that user1 and user2 have courses in common.
$this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
// Test that user1 and user3 have no courses in common.
$this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
// Test retrieving the courses in common.
$sharedcourses = enrol_get_shared_courses($user1, $user2, true);
// Only should be one shared course.
$this->assertCount(1, $sharedcourses);
$sharedcourse = array_shift($sharedcourses);
// It should be course 1.
$this->assertEquals($sharedcourse->id, $course1->id);
}
/** /**
* Test user enrolment created event. * Test user enrolment created event.
*/ */

View file

@ -290,8 +290,11 @@ function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $che
return false; return false;
} }
list($plugins, $params) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee'); list($plugins1, $params1) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee1');
$params['enabled'] = ENROL_INSTANCE_ENABLED; list($plugins2, $params2) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee2');
$params = array_merge($params1, $params2);
$params['enabled1'] = ENROL_INSTANCE_ENABLED;
$params['enabled2'] = ENROL_INSTANCE_ENABLED;
$params['active1'] = ENROL_USER_ACTIVE; $params['active1'] = ENROL_USER_ACTIVE;
$params['active2'] = ENROL_USER_ACTIVE; $params['active2'] = ENROL_USER_ACTIVE;
$params['user1'] = $user1; $params['user1'] = $user1;
@ -309,11 +312,12 @@ function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $che
FROM {course} c FROM {course} c
JOIN ( JOIN (
SELECT DISTINCT c.id SELECT DISTINCT c.id
FROM {enrol} e FROM {course} c
JOIN {user_enrolments} ue1 ON (ue1.enrolid = e.id AND ue1.status = :active1 AND ue1.userid = :user1) JOIN {enrol} e1 ON (c.id = e1.courseid AND e1.status = :enabled1 AND e1.enrol $plugins1)
JOIN {user_enrolments} ue2 ON (ue2.enrolid = e.id AND ue2.status = :active2 AND ue2.userid = :user2) JOIN {user_enrolments} ue1 ON (ue1.enrolid = e1.id AND ue1.status = :active1 AND ue1.userid = :user1)
JOIN {course} c ON (c.id = e.courseid AND c.visible = 1) JOIN {enrol} e2 ON (c.id = e2.courseid AND e2.status = :enabled2 AND e2.enrol $plugins2)
WHERE e.status = :enabled AND e.enrol $plugins JOIN {user_enrolments} ue2 ON (ue2.enrolid = e2.id AND ue2.status = :active2 AND ue2.userid = :user2)
WHERE c.visible = 1
) ec ON ec.id = c.id ) ec ON ec.id = c.id
$ctxjoin"; $ctxjoin";