Skip to content

Commit

Permalink
Merge branch 'MDL-31548-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Apr 24, 2012
2 parents 2ea2549 + 9ffd29c commit 795558d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
68 changes: 44 additions & 24 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion user/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion user/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 795558d

Please sign in to comment.