diff --git a/lib/enrollib.php b/lib/enrollib.php index c90139d4418fd..b6736c4ece146 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -678,6 +678,7 @@ function enrol_get_course_description_texts($course) { /** * Returns list of courses user is enrolled into. + * (Note: use enrol_get_all_users_courses if you want to use the list wihtout any cap checks ) * * - $fields is an array of fieldnames to ADD * so name the fields you really need, which will @@ -692,15 +693,53 @@ function enrol_get_course_description_texts($course) { function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC') { global $DB; + $courses = enrol_get_all_users_courses($userid, $onlyactive, $fields, $sort); + + // preload contexts and check visibility + if ($onlyactive) { + foreach ($courses as $id=>$course) { + context_instance_preload($course); + if (!$course->visible) { + if (!$context = context_course::instance($id)) { + unset($courses[$id]); + continue; + } + if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) { + unset($courses[$id]); + continue; + } + } + } + } + + return $courses; + +} + +/** + * Returns list of courses user is enrolled into without any capability checks + * - $fields is an array of fieldnames to ADD + * so name the fields you really need, which will + * be added and uniq'd + * + * @param int $userid + * @param bool $onlyactive return only active enrolments in courses user may see + * @param string|array $fields + * @param string $sort + * @return array + */ +function enrol_get_all_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC') { + global $DB; + // Guest account does not have any courses if (isguestuser($userid) or empty($userid)) { return(array()); } $basefields = array('id', 'category', 'sortorder', - 'shortname', 'fullname', 'idnumber', - 'startdate', 'visible', - 'groupmode', 'groupmodeforce'); + 'shortname', 'fullname', 'idnumber', + 'startdate', 'visible', + 'groupmode', 'groupmodeforce'); if (empty($fields)) { $fields = $basefields; @@ -764,30 +803,11 @@ function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $ $courses = $DB->get_records_sql($sql, $params); - // preload contexts and check visibility - foreach ($courses as $id=>$course) { - context_instance_preload($course); - if ($onlyactive) { - if (!$course->visible) { - if (!$context = get_context_instance(CONTEXT_COURSE, $id)) { - unset($courses[$id]); - continue; - } - if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) { - unset($courses[$id]); - continue; - } - } - } - $courses[$id] = $course; - } - - //wow! Is that really all? :-D - return $courses; - } + + /** * Called when user is about to be deleted. * @param object $user diff --git a/user/profile.php b/user/profile.php index 3f619d36375e8..989702e7aaac5 100644 --- a/user/profile.php +++ b/user/profile.php @@ -299,7 +299,7 @@ if (!isset($hiddenfields['mycourses'])) { - if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) { + if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) { $shown=0; $courselisting = ''; foreach ($mycourses as $mycourse) { diff --git a/user/view.php b/user/view.php index 59cbcb32deb20..66728b7a6a4a7 100644 --- a/user/view.php +++ b/user/view.php @@ -283,7 +283,7 @@ // Show other courses they may be in if (!isset($hiddenfields['mycourses'])) { - if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) { + if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) { $shown = 0; $courselisting = ''; foreach ($mycourses as $mycourse) {