Search moodle.org's
Developer Documentation

See Release Notes

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

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