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 Exception;
33
use mod_edusharing\EduSharingService;
34
use stdClass;
35
 
36
/**
37
 * class AddInstance
38
 *
39
 * Service class for the endpoint 'mod_edusharing_add_instance'.
40
 *
41
 * @author Marian Ziegler <ziegler@edu-sharing.net>
42
 * @package mod_edusharing
43
 */
44
class AddInstance extends external_api {
45
    /**
46
     * Function execute_parameters
47
     *
48
     * defines the structure of the parameters to be provided
49
     * The end point expects json as follows:
50
     *
51
     * {"eduStructure": {
52
     *      "name": "testName",
53
     *      "objectUrl": "www.test.de",
54
     *      "courseId": 5,
55
     *      "objectVersion": 1.1
56
     *      }
57
     * }
58
     *
59
     * @return external_function_parameters
60
     */
61
    public static function execute_parameters(): external_function_parameters {
62
        $edustructure = new external_single_structure([
63
            'name'          => new external_value(PARAM_TEXT, 'the name of the object'),
64
            'objectUrl'     => new external_value(PARAM_TEXT, 'the object url of the object'),
65
            'courseId'      => new external_value(PARAM_INT, 'course id'),
66
            'objectVersion' => new external_value(PARAM_TEXT, 'The object version'),
67
        ]);
68
        return new external_function_parameters(['eduStructure' => $edustructure]);
69
    }
70
 
71
    /**
72
     * Function execute_returns
73
     *
74
     * defines the return data
75
     *
76
     * @return external_single_structure
77
     */
78
    public static function execute_returns(): external_single_structure {
79
        return new external_single_structure([
80
            'name'          => new external_value(PARAM_TEXT, 'the name of the object'),
81
            'objectUrl'     => new external_value(PARAM_TEXT, 'the object url of the object'),
82
            'courseId'      => new external_value(PARAM_INT, 'course id'),
83
            'id'            => new external_value(PARAM_INT, 'id'),
84
            'objectVersion' => new external_value(PARAM_TEXT, 'The object version'),
85
        ]);
86
    }
87
 
88
    /**
89
     * Function execute
90
     *
91
     * handles the service call
92
     *
93
     * @param array $edustructure
94
     * @return array
95
     * @throws Exception
96
     */
97
    public static function execute(array $edustructure): array {
98
        $context = context_course::instance($edustructure['courseId']);
99
        require_capability('mod/edusharing:wysiwygvisibility', $context);
100
        $edusharing                 = new stdClass();
101
        $edusharing->name           = $edustructure['name'];
102
        $edusharing->object_url     = $edustructure['objectUrl'];
103
        $edusharing->course         = $edustructure['courseId'];
104
        $edusharing->object_version = $edustructure['objectVersion'];
105
        $edusharing->introformat    = 0;
106
        $service                    = new EduSharingService();
107
        $id                         = $service->add_instance($edusharing);
108
        if ($id === false) {
109
            throw new Exception('Error adding instance');
110
        }
111
        $edustructure['id'] = $id;
112
        return $edustructure;
113
    }
114
}