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 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  /**
  18   * External API.
  19   *
  20   * @package    block_recentlyaccesseditems
  21   * @copyright  2018 Victor Deniz <victor@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace block_recentlyaccesseditems;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/externallib.php");
  28  
  29  use block_recentlyaccesseditems\external\recentlyaccesseditems_item_exporter;
  30  use external_api;
  31  use external_function_parameters;
  32  use external_value;
  33  use external_multiple_structure;
  34  use context_user;
  35  use context_module;
  36  
  37  /**
  38   * External API class.
  39   *
  40   * @package    block_recentlyaccesseditems
  41   * @copyright  2018 Victor Deniz <victor@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class external extends external_api {
  45  
  46      /**
  47       * Returns description of method parameters
  48       * @return external_function_parameters
  49       */
  50      public static function get_recent_items_parameters() {
  51          return new external_function_parameters(
  52                  array(
  53                          'limit' => new external_value(PARAM_INT, 'result set limit', VALUE_DEFAULT, 0)
  54                  )
  55          );
  56      }
  57  
  58      /**
  59       * Get last accessed items by the logged user (activities or resources).
  60       *
  61       * @param  int $limit Max num of items to return
  62       * @return array List of items
  63       * @since Moodle 3.6
  64       */
  65      public static function get_recent_items(int $limit = 0) {
  66          global $USER, $PAGE;
  67  
  68          $userid = $USER->id;
  69  
  70          $params = self::validate_parameters(self::get_recent_items_parameters(),
  71              array(
  72                  'limit' => $limit,
  73              )
  74          );
  75  
  76          $limit = $params['limit'];
  77  
  78          self::validate_context(context_user::instance($userid));
  79  
  80          $items = helper::get_recent_items($limit);
  81  
  82          $renderer = $PAGE->get_renderer('core');
  83          $recentitems = array_map(function($item) use ($renderer) {
  84              $context = context_module::instance($item->cmid);
  85              $exporter = new recentlyaccesseditems_item_exporter($item, ['context' => $context]);
  86              return $exporter->export($renderer);
  87          }, $items);
  88  
  89          return $recentitems;
  90      }
  91  
  92      /**
  93       * Returns description of method result value
  94       *
  95       * @return external_description
  96       * @since Moodle 3.6
  97       */
  98      public static function get_recent_items_returns() {
  99          return new external_multiple_structure(recentlyaccesseditems_item_exporter::get_read_structure(),
 100                  'The most recently accessed activities/resources by the logged user');
 101      }
 102  }