Permalink
Browse files

Merge branch 'MDL-31548-m22' of git://github.com/ankitagarwal/moodle …

…into MOODLE_22_STABLE
  • Loading branch information...
2 parents 352476d + 62c325e commit 46c485a63cb8872812e757527299601bed1e3db3 @stronk7 stronk7 committed Apr 24, 2012
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
@@ -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 = get_context_instance(CONTEXT_COURSE, $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
@@ -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
@@ -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) {

0 comments on commit 46c485a

Please sign in to comment.