Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
namespace core_courseformat\external;
18
 
1441 ariadna 19
use core\exception\moodle_exception;
1 efrain 20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_multiple_structure;
23
use core_external\external_value;
24
use context_course;
25
use core_courseformat\base as course_format;
26
 
27
/**
28
 * External secrvie to update the course from the course editor components.
29
 *
30
 * @package    core_course
31
 * @copyright  2021 Ferran Recio <moodle@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @since      Moodle 4.0
34
 */
35
class update_course extends external_api {
36
 
37
    /**
38
     * Webservice parameters.
39
     *
40
     * @return external_function_parameters
41
     */
42
    public static function execute_parameters(): external_function_parameters {
43
        return new external_function_parameters(
44
            [
45
                'action' => new external_value(
46
                    PARAM_ALPHANUMEXT,
47
                    'action: cm_hide, cm_show, section_hide, section_show, cm_moveleft...',
48
                    VALUE_REQUIRED
49
                ),
50
                'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
51
                'ids' => new external_multiple_structure(
52
                    new external_value(PARAM_INT, 'Target id'),
53
                    'Affected ids',
54
                    VALUE_DEFAULT,
55
                    []
56
                ),
57
                'targetsectionid' => new external_value(
58
                    PARAM_INT, 'Optional target section id', VALUE_DEFAULT, null
59
                ),
60
                'targetcmid' => new external_value(
61
                    PARAM_INT, 'Optional target cm id', VALUE_DEFAULT, null
62
                ),
63
            ]
64
        );
65
    }
66
 
67
    /**
68
     * This webservice will execute any action from the course editor. The default actions
69
     * are located in {@see \core_courseformat\stateactions} but the format plugin can extend that class
70
     * in format_XXX\course.
71
     *
72
     * The specific action methods will register in a {@see \core_courseformat\stateupdates} all the affected
73
     * sections, cms and course attribute. This object (in JSON) will be sent back to the
74
     * frontend editor to refresh the updated state elements.
75
     *
76
     * By default, {@see \core_courseformat\stateupdates} will register only create, delete and update events
77
     * on cms, sections and the general course data. However, if some plugin needs adhoc messages for
78
     * its own mutation module, extend this class in format_XXX\course.
79
     *
80
     * @param string $action the action name to execute
81
     * @param int $courseid the course id
82
     * @param int[] $ids the affected ids (section or cm depending on the action)
83
     * @param int|null $targetsectionid optional target section id (for move action)
84
     * @param int|null $targetcmid optional target cm id (for move action)
85
     * @return string Course state in JSON
86
     */
87
    public static function execute(string $action, int $courseid, array $ids = [],
88
            ?int $targetsectionid = null, ?int $targetcmid = null): string {
89
        global $CFG;
90
 
91
        require_once($CFG->dirroot . '/course/lib.php');
92
 
93
        $params = external_api::validate_parameters(self::execute_parameters(), [
94
            'action' => $action,
95
            'courseid' => $courseid,
96
            'ids' => $ids,
97
            'targetsectionid' => $targetsectionid,
98
            'targetcmid' => $targetcmid,
99
        ]);
100
        $action = $params['action'];
101
        $courseid = $params['courseid'];
102
        $ids = $params['ids'];
103
        $targetsectionid = $params['targetsectionid'];
104
        $targetcmid = $params['targetcmid'];
105
 
106
        self::validate_context(context_course::instance($courseid));
107
 
1441 ariadna 108
        $format = course_get_format($courseid);
1 efrain 109
 
1441 ariadna 110
        $updates = $format->get_stateupdates_instance();
111
        $actions = $format->get_stateactions_instance();
1 efrain 112
 
113
        if (!is_callable([$actions, $action])) {
114
            throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
115
        }
116
 
1441 ariadna 117
        $course = $format->get_course();
1 efrain 118
 
119
        // Execute the action.
120
        $actions->$action($updates, $course, $ids, $targetsectionid, $targetcmid);
121
 
1441 ariadna 122
        // Dispatch the hook for post course content update.
123
        $hook = new \core_courseformat\hook\after_course_content_updated(
124
            course: $course,
125
        );
126
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
1 efrain 127
 
128
        return json_encode($updates);
129
    }
130
 
131
    /**
132
     * Webservice returns.
133
     *
134
     * @return external_value
135
     */
136
    public static function execute_returns(): external_value {
137
        return new external_value(PARAM_RAW, 'Encoded course update JSON');
138
    }
139
}