MDL-57503 enrol: allow course ids for enrol_get_my_courses

Allow enrol_get_my_courses to be filtered by a set of known course ids
that the user may or may not be enrolled on.

The result will be a subset of the given course ids that the user is
enrolled in.

Part of MDL-55611 epic.
This commit is contained in:
Ryan Wyllie
2017-04-03 11:36:34 +08:00
committed by Damyon Wiese
parent 7c6f961bf9
commit 1ef06b4335
2 changed files with 54 additions and 1 deletions
+45
View File
@@ -476,4 +476,49 @@ class core_enrollib_testcase extends advanced_testcase {
$this->assertGreaterThan($userenrolorig, $userenrolpost);
}
/**
* Test to confirm that enrol_get_my_courses only return the courses that
* the logged in user is enrolled in.
*/
public function test_enrol_get_my_courses_only_enrolled_courses() {
$user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
$course4 = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user->id, $course1->id);
$this->getDataGenerator()->enrol_user($user->id, $course2->id);
$this->getDataGenerator()->enrol_user($user->id, $course3->id);
$this->resetAfterTest(true);
$this->setUser($user);
// By default this function should return all of the courses the user
// is enrolled in.
$courses = enrol_get_my_courses();
$this->assertCount(3, $courses);
$this->assertEquals($course1->id, $courses[$course1->id]->id);
$this->assertEquals($course2->id, $courses[$course2->id]->id);
$this->assertEquals($course3->id, $courses[$course3->id]->id);
// If a set of course ids are provided then the result set will only contain
// these courses.
$courseids = [$course1->id, $course2->id];
$courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
$this->assertCount(2, $courses);
$this->assertEquals($course1->id, $courses[$course1->id]->id);
$this->assertEquals($course2->id, $courses[$course2->id]->id);
// If the course ids list contains any ids for courses the user isn't enrolled in
// then they will be ignored (in this case $course4).
$courseids = [$course1->id, $course2->id, $course4->id];
$courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
$this->assertCount(2, $courses);
$this->assertEquals($course1->id, $courses[$course1->id]->id);
$this->assertEquals($course2->id, $courses[$course2->id]->id);
}
}
+9 -1
View File
@@ -541,9 +541,11 @@ function enrol_add_course_navigation(navigation_node $coursenode, $course) {
* @param string|array $fields
* @param string $sort
* @param int $limit max number of courses
* @param array $courseids the list of course ids to filter by
* @return array
*/
function enrol_get_my_courses($fields = NULL, $sort = 'visible DESC,sortorder ASC', $limit = 0) {
function enrol_get_my_courses($fields = NULL, $sort = 'visible DESC,sortorder ASC',
$limit = 0, $courseids = []) {
global $DB, $USER;
// Guest account does not have any courses
@@ -603,6 +605,12 @@ function enrol_get_my_courses($fields = NULL, $sort = 'visible DESC,sortorder AS
$params['contextlevel'] = CONTEXT_COURSE;
$wheres = implode(" AND ", $wheres);
if (!empty($courseids)) {
list($courseidssql, $courseidsparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
$wheres = sprintf("%s AND c.id %s", $wheres, $courseidssql);
$params = array_merge($params, $courseidsparams);
}
//note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
$sql = "SELECT $coursefields $ccselect
FROM {course} c