Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-31548 enrollments: Introducing new api enrol_get_all_users_course…

…s() that returns all user courses without any checks
  • Loading branch information...
commit 084c07f5999568fd67707f0c6bd2b18bb5c1594e 1 parent bd44a3b
Ankit Agarwal ankitagarwal authored

Showing 3 changed files with 46 additions and 26 deletions. Show diff stats Hide diff stats

  1. +44 24 lib/enrollib.php
  2. +1 1  user/profile.php
  3. +1 1  user/view.php
68 lib/enrollib.php
@@ -674,6 +674,7 @@ function enrol_get_course_description_texts($course) {
674 674
675 675 /**
676 676 * Returns list of courses user is enrolled into.
  677 + * (Note: use enrol_get_all_users_courses if you want to use the list wihtout any cap checks )
677 678 *
678 679 * - $fields is an array of fieldnames to ADD
679 680 * so name the fields you really need, which will
@@ -688,15 +689,53 @@ function enrol_get_course_description_texts($course) {
688 689 function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC') {
689 690 global $DB;
690 691
  692 + $courses = enrol_get_all_users_courses($userid, $onlyactive, $fields, $sort);
  693 +
  694 + // preload contexts and check visibility
  695 + if ($onlyactive) {
  696 + foreach ($courses as $id=>$course) {
  697 + context_instance_preload($course);
  698 + if (!$course->visible) {
  699 + if (!$context = get_context_instance(CONTEXT_COURSE, $id)) {
  700 + unset($courses[$id]);
  701 + continue;
  702 + }
  703 + if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) {
  704 + unset($courses[$id]);
  705 + continue;
  706 + }
  707 + }
  708 + }
  709 + }
  710 +
  711 + return $courses;
  712 +
  713 +}
  714 +
  715 +/**
  716 + * Returns list of courses user is enrolled into without any capability checks
  717 + * - $fields is an array of fieldnames to ADD
  718 + * so name the fields you really need, which will
  719 + * be added and uniq'd
  720 + *
  721 + * @param int $userid
  722 + * @param bool $onlyactive return only active enrolments in courses user may see
  723 + * @param string|array $fields
  724 + * @param string $sort
  725 + * @return array
  726 + */
  727 +function enrol_get_all_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC') {
  728 + global $DB;
  729 +
691 730 // Guest account does not have any courses
692 731 if (isguestuser($userid) or empty($userid)) {
693 732 return(array());
694 733 }
695 734
696 735 $basefields = array('id', 'category', 'sortorder',
697   - 'shortname', 'fullname', 'idnumber',
698   - 'startdate', 'visible',
699   - 'groupmode', 'groupmodeforce');
  736 + 'shortname', 'fullname', 'idnumber',
  737 + 'startdate', 'visible',
  738 + 'groupmode', 'groupmodeforce');
700 739
701 740 if (empty($fields)) {
702 741 $fields = $basefields;
@@ -760,30 +799,11 @@ function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $
760 799
761 800 $courses = $DB->get_records_sql($sql, $params);
762 801
763   - // preload contexts and check visibility
764   - foreach ($courses as $id=>$course) {
765   - context_instance_preload($course);
766   - if ($onlyactive) {
767   - if (!$course->visible) {
768   - if (!$context = get_context_instance(CONTEXT_COURSE, $id)) {
769   - unset($courses[$id]);
770   - continue;
771   - }
772   - if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) {
773   - unset($courses[$id]);
774   - continue;
775   - }
776   - }
777   - }
778   - $courses[$id] = $course;
779   - }
780   -
781   - //wow! Is that really all? :-D
782   -
783 802 return $courses;
784   -
785 803 }
786 804
  805 +
  806 +
787 807 /**
788 808 * Called when user is about to be deleted.
789 809 * @param object $user
2  user/profile.php
@@ -290,7 +290,7 @@
290 290
291 291
292 292 if (!isset($hiddenfields['mycourses'])) {
293   - if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
  293 + if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
294 294 $shown=0;
295 295 $courselisting = '';
296 296 foreach ($mycourses as $mycourse) {
2  user/view.php
@@ -283,7 +283,7 @@
283 283
284 284 // Show other courses they may be in
285 285 if (!isset($hiddenfields['mycourses'])) {
286   - if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
  286 + if ($mycourses = enrol_get_all_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
287 287 $shown = 0;
288 288 $courselisting = '';
289 289 foreach ($mycourses as $mycourse) {

0 comments on commit 084c07f

Please sign in to comment.
Something went wrong with that request. Please try again.