Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
declare(strict_types=1);
18
 
19
namespace mod_edusharing\external;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
// Once Moodle versions < 4.2 are out of LTS, we need to revert this to the proper namespaces.
24
global $CFG;
25
require_once($CFG->dirroot . '/lib/externallib.php');
26
 
27
use context_course;
28
use external_api;
29
use external_function_parameters;
30
use external_single_structure;
31
use external_value;
32
use mod_edusharing\EduSharingService;
33
use required_capability_exception;
34
 
35
/**
36
 * Class GetTicket
37
 *
38
 * Service class for the endpoint 'mod_edusharing_get_ticket'.
39
 *
40
 * @author Marian Ziegler <ziegler@edu-sharing.net>
41
 * @package mod_edusharing
42
 */
43
class GetTicket extends external_api {
44
    /**
45
     * Function execute_parameters
46
     *
47
     * defines the structure of the parameters to be provided
48
     * The end point expects json as follows:
49
     *
50
     * {"eduTicketStructure": {
51
     *      "courseId": 5
52
     *      }
53
     * }
54
     *
55
     * @return external_function_parameters
56
     */
57
    public static function execute_parameters(): external_function_parameters {
58
        $eduticketstructure = new external_single_structure([
59
            'courseId' => new external_value(PARAM_INT, 'course id'),
60
        ]);
61
        return new external_function_parameters(['eduTicketStructure' => $eduticketstructure]);
62
    }
63
 
64
    /**
65
     * Function execute_returns
66
     *
67
     * defines the return data
68
     *
69
     * @return external_single_structure
70
     */
71
    public static function execute_returns(): external_single_structure {
72
        return new external_single_structure([
73
            'ticket' => new external_value(PARAM_TEXT, 'the ticket'),
74
        ]);
75
    }
76
 
77
    /**
78
     * Function execute
79
     *
80
     * handles the service call
81
     *
82
     * @param array $input
83
     * @return array
84
     * @throws required_capability_exception
85
     */
86
    public static function execute(array $input): array {
87
        if ($input['courseId'] !== 0) {
88
            $context = context_course::instance($input['courseId']);
89
            require_capability('moodle/course:update', $context);
90
        }
91
        $service = new EduSharingService();
92
        $ticket  = $service->get_ticket();
93
        return ['ticket' => $ticket];
94
    }
95
}