Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  defined('MOODLE_INTERNAL') || die;
  25  
  26  require_once($CFG->libdir . '/externallib.php');
  27  
  28  require_once($CFG->dirroot . '/course/lib.php');
  29  require_once($CFG->dirroot . '/course/externallib.php');
  30  
  31  use \core_course\external\course_summary_exporter;
  32  
  33  /**
  34   * Starred courses block external functions.
  35   *
  36   * @copyright  2018 Simey Lameze <simey@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class block_starredcourses_external extends core_course_external {
  40  
  41      /**
  42       * Returns description of method parameters
  43       *
  44       * @return external_function_parameters
  45       * @since Moodle 3.6
  46       */
  47      public static function get_starred_courses_parameters() {
  48          return new external_function_parameters([
  49              'limit' => new external_value(PARAM_INT, 'Limit', VALUE_DEFAULT, 0),
  50              'offset' => new external_value(PARAM_INT, 'Offset', VALUE_DEFAULT, 0)
  51          ]);
  52      }
  53  
  54      /**
  55       * Get users starred courses.
  56       *
  57       * @param int $limit Limit
  58       * @param int $offset Offset
  59       *
  60       * @return  array list of courses and warnings
  61       */
  62      public static function get_starred_courses($limit, $offset) {
  63          global $USER, $PAGE;
  64  
  65          $params = self::validate_parameters(self::get_starred_courses_parameters(), [
  66              'limit' => $limit,
  67              'offset' => $offset
  68          ]);
  69  
  70          $limit = $params['limit'];
  71          $offset = $params['offset'];
  72  
  73          $usercontext = context_user::instance($USER->id);
  74  
  75          self::validate_context($usercontext);
  76          $PAGE->set_context($usercontext);
  77          $renderer = $PAGE->get_renderer('core');
  78  
  79          // Get the user favourites service, scoped to a single user (their favourites only).
  80          $userservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
  81  
  82          // Get the favourites, by type, for the user.
  83          $favourites = $userservice->find_favourites_by_type('core_course', 'courses', $offset, $limit);
  84  
  85          $favouritecourseids = [];
  86          if ($favourites) {
  87              $favouritecourseids = array_map(
  88                  function($favourite) {
  89                      return $favourite->itemid;
  90                  }, $favourites);
  91          }
  92  
  93          // Get all courses that the current user is enroled in, restricted down to favourites.
  94          $filteredcourses = [];
  95          if ($favouritecourseids) {
  96              $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, null,
  97                  COURSE_DB_QUERY_LIMIT, $favouritecourseids);
  98              list($filteredcourses, $processedcount) = course_filter_courses_by_favourites(
  99                  $courses,
 100                  $favouritecourseids,
 101                  0
 102              );
 103          }
 104          // Grab the course ids.
 105          $filteredcourseids = array_column($filteredcourses, 'id');
 106  
 107          // Filter out any favourites that are not in the list of enroled courses.
 108          $filteredfavourites = array_filter($favourites, function($favourite) use ($filteredcourseids) {
 109              return in_array($favourite->itemid, $filteredcourseids);
 110          });
 111  
 112          // Sort the favourites getting last added first.
 113          usort($filteredfavourites, function($a, $b) {
 114              if ($a->timemodified == $b->timemodified) return 0;
 115              return ($a->timemodified > $b->timemodified) ? -1 : 1;
 116          });
 117  
 118          $formattedcourses = array();
 119          foreach ($filteredfavourites as $favourite) {
 120              $course = get_course($favourite->itemid);
 121              $context = \context_course::instance($favourite->itemid);
 122              $canviewhiddencourses = has_capability('moodle/course:viewhiddencourses', $context);
 123  
 124              if ($course->visible || $canviewhiddencourses) {
 125                  $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => true]);
 126                  $formattedcourse = $exporter->export($renderer);
 127                  $formattedcourses[] = $formattedcourse;
 128              }
 129          }
 130  
 131          return $formattedcourses;
 132      }
 133  
 134      /**
 135       * Returns description of method result value
 136       *
 137       * @return external_description
 138       * @since Moodle 3.6
 139       */
 140      public static function get_starred_courses_returns() {
 141          return new external_multiple_structure(course_summary_exporter::get_read_structure());
 142      }
 143  }