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 * Generate a new draft itemid for the current user.
19 *
20 * @package    core_files
21 * @since      Moodle 3.11
22 * @copyright  2020 Juan Leyva <juan@moodle.com>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_files\external\get;
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->libdir . '/externallib.php');
32require_once($CFG->libdir . '/filelib.php');
33
34use external_api;
35use external_function_parameters;
36use external_multiple_structure;
37use external_single_structure;
38use external_value;
39use external_warnings;
40use context_user;
41
42/**
43 * Generate a new draft itemid for the current user.
44 *
45 * @copyright  2020 Juan Leyva <juan@moodle.com>
46 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 */
48class unused_draft extends external_api {
49
50    /**
51     * Describes the parameters for execute.
52     *
53     * @return external_function_parameters
54     * @since Moodle 3.11
55     */
56    public static function execute_parameters() : external_function_parameters {
57        return new external_function_parameters ([]);
58    }
59
60    /**
61     * Generate a new draft itemid for the current user.
62     *
63     * @return array of information containing the draft item area and possible warnings.
64     * @since Moodle 3.11
65     */
66    public static function execute() : array {
67        global $USER;
68
69        $usercontext = context_user::instance($USER->id);
70        self::validate_context($usercontext);
71
72        return [
73            'component' => 'user',
74            'contextid' => $usercontext->id,
75            'userid' => $USER->id,
76            'filearea' => 'draft',
77            'itemid' => file_get_unused_draft_itemid(),
78            'warnings' => [],
79        ];
80    }
81
82    /**
83     * Describes the execute return value.
84     *
85     * @return external_single_structure
86     * @since Moodle 3.11
87     */
88    public static function execute_returns() : external_single_structure {
89        return new external_single_structure(
90            [
91                'component' => new external_value(PARAM_COMPONENT, 'File area component.'),
92                'contextid' => new external_value(PARAM_INT, 'File area context.'),
93                'userid' => new external_value(PARAM_INT, 'File area user id.'),
94                'filearea' => new external_value(PARAM_ALPHA, 'File area name.'),
95                'itemid' => new external_value(PARAM_INT, 'File are item id.'),
96                'warnings' => new external_warnings(),
97            ]
98        );
99    }
100}
101