Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-31548 enrollments: Introducing new api enrol_get_all_users_course…

…s() that returns all user courses without any checks
  • Loading branch information...
commit 9ffd29ceb330bb02ad769060712f431187a545b6 1 parent ead4f18
@ankitagarwal ankitagarwal authored
Showing with 46 additions and 26 deletions.
  1. +44 −24 lib/enrollib.php
  2. +1 −1  user/profile.php
  3. +1 −1  user/view.php
View
68 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
View
2  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) {
View
2  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) {
Please sign in to comment.
Something went wrong with that request. Please try again.