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\Constants;
34
 
35
/**
36
 * Class UpdateInstance
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 UpdateInstance 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
     * {"eduStructure": {
51
     *      "id": 1234,
52
     *      "courseId": 123
53
     *      }
54
     * }
55
     *
56
     * @return external_function_parameters
57
     */
58
    public static function execute_parameters(): external_function_parameters {
59
        $edustructure = new external_single_structure([
60
            'id'            => new external_value(PARAM_INT, 'the primary id of the pbject'),
61
            'name'          => new external_value(PARAM_TEXT, 'the name of the object', VALUE_OPTIONAL),
62
            'objectUrl'     => new external_value(PARAM_TEXT, 'the object url of the object', VALUE_OPTIONAL),
63
            'courseId'      => new external_value(PARAM_INT, 'course id'),
64
            'objectVersion' => new external_value(PARAM_TEXT, 'The object version', VALUE_OPTIONAL),
65
        ]);
66
        return new external_function_parameters(['eduStructure' => $edustructure]);
67
    }
68
 
69
    /**
70
     * Function execute_returns
71
     *
72
     * defines the return data
73
     *
74
     * @return external_single_structure
75
     */
76
    public static function execute_returns(): external_single_structure {
77
        return new external_single_structure([
78
            'id'            => new external_value(PARAM_INT, 'id'),
79
            'name'          => new external_value(PARAM_TEXT, 'the name of the object'),
80
            'objectUrl'     => new external_value(PARAM_TEXT, 'the object url of the object'),
81
            'courseId'      => new external_value(PARAM_INT, 'course id'),
82
            'objectVersion' => new external_value(PARAM_TEXT, 'The object version'),
83
        ]);
84
    }
85
 
86
    /**
87
     * Function execute
88
     *
89
     * handles the service call
90
     *
91
     * @param array $edustructure
92
     * @return array
93
     * @throws Exception
94
     */
95
    public static function execute(array $edustructure): array {
96
        global $DB;
97
        $context = context_course::instance($edustructure['courseId']);
98
        require_capability('mod/edusharing:wysiwygvisibility', $context);
99
        $where          = [
100
            'id'     => $edustructure['id'],
101
            'course' => $edustructure['courseId'],
102
        ];
103
        $eduinstance    = $DB->get_record(Constants::EDUSHARING_TABLE, $where, '*', MUST_EXIST);
104
        $isupdateneeded = false;
105
        if (isset($edustructure['name']) && $edustructure['name'] !== $eduinstance->name) {
106
            $eduinstance->name = $edustructure['name'];
107
            $isupdateneeded    = true;
108
        }
109
        if (isset($edustructure['objectUrl']) && $edustructure['objectUrl'] !== $eduinstance->object_url) {
110
            $eduinstance->object_url = $edustructure['objectUrl'];
111
            $isupdateneeded          = true;
112
        }
113
        if (isset($edustructure['objectVersion']) && $edustructure['objectVersion'] !== $eduinstance->object_version) {
114
            $eduinstance->object_version = $edustructure['objectVersion'];
115
            $isupdateneeded              = true;
116
        }
117
        if ($isupdateneeded) {
118
            $eduinstance->timemodified = time();
119
            $DB->update_record(Constants::EDUSHARING_TABLE, $eduinstance);
120
        }
121
        return [
122
            'name'          => $eduinstance->name,
123
            'objectUrl'     => $eduinstance->object_url,
124
            'courseId'      => $eduinstance->course,
125
            'id'            => $eduinstance->id,
126
            'objectVersion' => $eduinstance->object_version,
127
        ];
128
    }
129
}