Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * Starred courses block external API
  18   *
  19   * @package    block_starredcourses
  20   * @category   external
  21   * @copyright  2018 Simey Lameze <simey@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  require_once($CFG->dirroot . '/course/lib.php');
  28  require_once($CFG->dirroot . '/course/externallib.php');
  29  
  30  use core_course\external\course_summary_exporter;
  31  use core_external\external_function_parameters;
  32  use core_external\external_multiple_structure;
  33  use core_external\external_value;
  34  
  35  /**
  36   * Starred courses block external functions.
  37   *
  38   * @copyright  2018 Simey Lameze <simey@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class block_starredcourses_external extends core_course_external {
  42  
  43      /**
  44       * Returns description of method parameters
  45       *
  46       * @return external_function_parameters
  47       * @since Moodle 3.6
  48       */
  49      public static function get_starred_courses_parameters() {
  50          return new external_function_parameters([
  51              'limit' => new external_value(PARAM_INT, 'Limit', VALUE_DEFAULT, 0),
  52              'offset' => new external_value(PARAM_INT, 'Offset', VALUE_DEFAULT, 0)
  53          ]);
  54      }
  55  
  56      /**
  57       * Get users starred courses.
  58       *
  59       * @param int $limit Limit
  60       * @param int $offset Offset
  61       *
  62       * @return  array list of courses and warnings
  63       */
  64      public static function get_starred_courses($limit, $offset) {
  65          global $USER, $PAGE;
  66  
  67          $params = self::validate_parameters(self::get_starred_courses_parameters(), [
  68              'limit' => $limit,
  69              'offset' => $offset
  70          ]);
  71  
  72          $limit = $params['limit'];
  73          $offset = $params['offset'];
  74  
  75          $usercontext = context_user::instance($USER->id);
  76  
  77          self::validate_context($usercontext);
  78          $PAGE->set_context($usercontext);
  79          $renderer = $PAGE->get_renderer('core');
  80  
  81          // Get the user favourites service, scoped to a single user (their favourites only).
  82          $userservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
  83  
  84          // Get the favourites, by type, for the user.
  85          $favourites = $userservice->find_favourites_by_type('core_course', 'courses', $offset, $limit);
  86  
  87          $favouritecourseids = [];
  88          if ($favourites) {
  89              $favouritecourseids = array_map(
  90                  function($favourite) {
  91                      return $favourite->itemid;
  92                  }, $favourites);
  93          }
  94  
  95          // Get all courses that the current user is enroled in, restricted down to favourites.
  96          $filteredcourses = [];
  97          if ($favouritecourseids) {
  98              $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, null,
  99                  COURSE_DB_QUERY_LIMIT, $favouritecourseids);
 100              list($filteredcourses, $processedcount) = course_filter_courses_by_favourites(
 101                  $courses,
 102                  $favouritecourseids,
 103                  0
 104              );
 105          }
 106          // Grab the course ids.
 107          $filteredcourseids = array_column($filteredcourses, 'id');
 108  
 109          // Filter out any favourites that are not in the list of enroled courses.
 110          $filteredfavourites = array_filter($favourites, function($favourite) use ($filteredcourseids) {
 111              return in_array($favourite->itemid, $filteredcourseids);
 112          });
 113  
 114          // Sort the favourites getting last added first.
 115          usort($filteredfavourites, function($a, $b) {
 116              if ($a->timemodified == $b->timemodified) return 0;
 117              return ($a->timemodified > $b->timemodified) ? -1 : 1;
 118          });
 119  
 120          $formattedcourses = array();
 121          foreach ($filteredfavourites as $favourite) {
 122              $course = get_course($favourite->itemid);
 123              $context = \context_course::instance($favourite->itemid);
 124              $canviewhiddencourses = has_capability('moodle/course:viewhiddencourses', $context);
 125  
 126              if ($course->visible || $canviewhiddencourses) {
 127                  $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => true]);
 128                  $formattedcourse = $exporter->export($renderer);
 129                  $formattedcourses[] = $formattedcourse;
 130              }
 131          }
 132  
 133          return $formattedcourses;
 134      }
 135  
 136      /**
 137       * Returns description of method result value
 138       *
 139       * @return \core_external\external_description
 140       * @since Moodle 3.6
 141       */
 142      public static function get_starred_courses_returns() {
 143          return new external_multiple_structure(course_summary_exporter::get_read_structure());
 144      }
 145  }