Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 
19
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_value;
22
use moodle_exception;
23
use coding_exception;
24
use context_course;
25
use core_courseformat\base as course_format;
26
 
27
/**
28
 * External service to create a new module instance in the course.
29
 *
30
 * @package    core_courseformat
31
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class new_module extends external_api {
35
 
36
    /**
37
     * Webservice parameters.
38
     *
39
     * @return external_function_parameters
40
     */
41
    public static function execute_parameters(): external_function_parameters {
42
        return new external_function_parameters(
43
            [
44
                'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
45
                'modname' => new external_value(PARAM_ALPHANUMEXT, 'module name', VALUE_REQUIRED),
46
                'targetsectionid' => new external_value(PARAM_INT, 'target section id', VALUE_REQUIRED, null),
47
                'targetcmid' => new external_value(PARAM_INT, 'Optional target cm id', VALUE_DEFAULT, null),
48
            ]
49
        );
50
    }
51
 
52
    /**
53
     * This webservice will execute the create_module action from the course editor.
54
     *
55
     * The action will register in a {@see \core_courseformat\stateupdates} all the affected
56
     * sections, cms and course attribute. This object (in JSON) will be sent back to the
57
     * frontend editor to refresh the updated state elements.
58
     *
59
     * By default, {@see \core_courseformat\stateupdates} will register only create, delete and update events
60
     * on cms, sections and the general course data. However, if some plugin needs adhoc messages for
61
     * its own mutation module, extend this class in format_XXX\course.
62
     *
63
     * @param int $courseid the course id
64
     * @param string $modname the module name
65
     * @param int $targetsectionid the target section id
66
     * @param int|null $targetcmid optional target cm id
67
     * @return string Course state in JSON
68
     */
69
    public static function execute(
70
        int $courseid,
71
        string $modname,
72
        int $targetsectionid,
73
        ?int $targetcmid = null
74
    ): string {
75
        global $CFG;
76
 
77
        require_once($CFG->dirroot . '/course/lib.php');
78
 
79
        [
80
            'courseid' => $courseid,
81
            'modname' => $modname,
82
            'targetsectionid' => $targetsectionid,
83
            'targetcmid' => $targetcmid,
84
        ] = self::validate_parameters(self::execute_parameters(), [
85
            'courseid' => $courseid,
86
            'modname' => $modname,
87
            'targetsectionid' => $targetsectionid,
88
            'targetcmid' => $targetcmid,
89
        ]);
90
 
91
        self::validate_context(context_course::instance($courseid));
92
 
93
        // Plugin needs to support quick creation and the course format needs to support components.
94
        // Formats using YUI modules should not be able to quick-create because the front end cannot react to the change.
95
        if (!plugin_supports('mod', $modname, FEATURE_QUICKCREATE) || !course_get_format($courseid)->supports_components()) {
96
            throw new moodle_exception("Module $modname does not support quick creation");
97
        }
98
 
99
        $courseformat = course_get_format($courseid);
100
 
101
        // Create a course changes tracker object.
102
        $defaultupdatesclass = 'core_courseformat\\stateupdates';
103
        $updatesclass = 'format_' . $courseformat->get_format() . '\\courseformat\\stateupdates';
104
        if (!class_exists($updatesclass)) {
105
            $updatesclass = $defaultupdatesclass;
106
        }
107
        $updates = new $updatesclass($courseformat);
108
 
109
        if (!is_a($updates, $defaultupdatesclass)) {
110
            throw new coding_exception("The \"$updatesclass\" class must extend \"$defaultupdatesclass\"");
111
        }
112
 
113
        // Get the actions class from the course format.
114
        $actionsclass = 'format_'. $courseformat->get_format().'\\courseformat\\stateactions';
115
        if (!class_exists($actionsclass)) {
116
            $actionsclass = 'core_courseformat\\stateactions';
117
        }
118
        /** @var \core_courseformat\stateactions $actions */
119
        $actions = new $actionsclass();
120
 
121
        $action = 'new_module';
122
        if (!is_callable([$actions, $action])) {
123
            throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
124
        }
125
 
126
        $course = $courseformat->get_course();
127
 
128
        // Execute the action.
129
        $actions->$action($updates, $course, $modname, $targetsectionid, $targetcmid);
130
 
131
        // Dispatch the hook for post course content update.
132
        $hook = new \core_courseformat\hook\after_course_content_updated(
133
            course: $course
134
        );
135
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
136
 
137
        return json_encode($updates);
138
    }
139
 
140
    /**
141
     * Webservice returns.
142
     *
143
     * @return external_value
144
     */
145
    public static function execute_returns(): external_value {
146
        return new external_value(PARAM_RAW, 'Encoded course update JSON');
147
    }
148
}