Skip to content

Commit

Permalink
MDL-58116 core_enrol: fix handling of users on different methods
Browse files Browse the repository at this point in the history
  • Loading branch information
barrasroger committed Apr 18, 2017
1 parent 0714bcb commit 499d377
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
56 changes: 56 additions & 0 deletions enrol/tests/enrollib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,62 @@ public function test_enrol_get_shared_courses() {
$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.
*/
Expand Down
18 changes: 11 additions & 7 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $che
return false;
}

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

Expand Down

0 comments on commit 499d377

Please sign in to comment.