Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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          // Sort the favourites getting last added first.
  86          usort($favourites, function($a, $b) {
  87              if ($a->timemodified == $b->timemodified) return 0;
  88              return ($a->timemodified > $b->timemodified) ? -1 : 1;
  89          });
  90  
  91          $formattedcourses = array();
  92          foreach ($favourites as $favourite) {
  93              $course = get_course($favourite->itemid);
  94              $context = \context_course::instance($favourite->itemid);
  95              $canviewhiddencourses = has_capability('moodle/course:viewhiddencourses', $context);
  96  
  97              if ($course->visible || $canviewhiddencourses) {
  98                  $exporter = new course_summary_exporter($course, ['context' => $context, 'isfavourite' => true]);
  99                  $formattedcourse = $exporter->export($renderer);
 100                  $formattedcourses[] = $formattedcourse;
 101              }
 102          }
 103  
 104          return $formattedcourses;
 105      }
 106  
 107      /**
 108       * Returns description of method result value
 109       *
 110       * @return external_description
 111       * @since Moodle 3.6
 112       */
 113      public static function get_starred_courses_returns() {
 114          return new external_multiple_structure(course_summary_exporter::get_read_structure());
 115      }
 116  }