2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
24 namespace block_recentlyaccesseditems;
25 defined('MOODLE_INTERNAL') || die();
27 require_once("$CFG->libdir/externallib.php");
29 use block_recentlyaccesseditems\external\recentlyaccesseditems_item_exporter;
31 use external_function_parameters;
33 use external_multiple_structure;
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
44 class external extends external_api {
47 * Returns description of method parameters
48 * @return external_function_parameters
50 public static function get_recent_items_parameters() {
51 return new external_function_parameters(
53 'limit' => new external_value(PARAM_INT, 'result set limit', VALUE_DEFAULT, 0)
59 * Get last accessed items by the logged user (activities or resources).
61 * @param int $limit Max num of items to return
62 * @return array List of items
65 public static function get_recent_items(int $limit = 0) {
70 if ($userid != $USER->id) {
74 $params = self::validate_parameters(self::get_recent_items_parameters(),
80 $limit = $params['limit'];
82 self::validate_context(context_user::instance($userid));
84 $items = helper::get_recent_items($limit);
86 $renderer = $PAGE->get_renderer('core');
87 $recentitems = array_map(function($item) use ($renderer) {
88 $context = context_module::instance($item->cmid);
89 $exporter = new recentlyaccesseditems_item_exporter($item, ['context' => $context]);
90 return $exporter->export($renderer);
97 * Returns description of method result value
99 * @return external_description
102 public static function get_recent_items_returns() {
103 return new external_multiple_structure(recentlyaccesseditems_item_exporter::get_read_structure(),
104 'The most recently accessed activities/resources by the logged user');