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
 * @deprecated since Moodle 5.0
34
 * @todo Remove this class in Moodle 6.0 MDL-83851
35
 */
36
class create_module extends external_api {
37
 
38
    /**
39
     * Webservice parameters.
40
     *
41
     * @deprecated since Moodle 5.0
42
     * @return external_function_parameters
43
     */
44
    public static function execute_parameters(): external_function_parameters {
45
        return new external_function_parameters(
46
            [
47
                'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
48
                'modname' => new external_value(PARAM_ALPHANUMEXT, 'module name', VALUE_REQUIRED),
49
                'targetsectionnum' => new external_value(PARAM_INT, 'target section number', VALUE_REQUIRED, null),
50
                'targetcmid' => new external_value(PARAM_INT, 'Optional target cm id', VALUE_DEFAULT, null),
51
            ]
52
        );
53
    }
54
 
55
    /**
56
     * This webservice will execute the create_module action from the course editor.
57
     *
58
     * The action will register in a {@see \core_courseformat\stateupdates} all the affected
59
     * sections, cms and course attribute. This object (in JSON) will be sent back to the
60
     * frontend editor to refresh the updated state elements.
61
     *
62
     * By default, {@see \core_courseformat\stateupdates} will register only create, delete and update events
63
     * on cms, sections and the general course data. However, if some plugin needs adhoc messages for
64
     * its own mutation module, extend this class in format_XXX\course.
65
     *
66
     * @deprecated since Moodle 5.0
67
     * @param int $courseid the course id
68
     * @param string $modname the module name
69
     * @param int $targetsectionnum the target section number
70
     * @param int|null $targetcmid optional target cm id
71
     * @return string Course state in JSON
72
     */
73
    public static function execute(
74
        int $courseid,
75
        string $modname,
76
        int $targetsectionnum,
77
        ?int $targetcmid = null
78
    ): string {
79
        global $CFG;
80
 
81
        require_once($CFG->dirroot . '/course/lib.php');
82
 
83
        [
84
            'courseid' => $courseid,
85
            'modname' => $modname,
86
            'targetsectionnum' => $targetsectionnum,
87
            'targetcmid' => $targetcmid,
88
        ] = self::validate_parameters(self::execute_parameters(), [
89
            'courseid' => $courseid,
90
            'modname' => $modname,
91
            'targetsectionnum' => $targetsectionnum,
92
            'targetcmid' => $targetcmid,
93
        ]);
94
 
95
        self::validate_context(context_course::instance($courseid));
96
 
97
        // Plugin needs to support quick creation and the course format needs to support components.
98
        // Formats using YUI modules should not be able to quick-create because the front end cannot react to the change.
99
        if (!plugin_supports('mod', $modname, FEATURE_QUICKCREATE) || !course_get_format($courseid)->supports_components()) {
100
            throw new moodle_exception("Module $modname does not support quick creation");
101
        }
102
 
103
        $courseformat = course_get_format($courseid);
104
 
105
        // Create a course changes tracker object.
106
        $defaultupdatesclass = 'core_courseformat\\stateupdates';
107
        $updatesclass = 'format_' . $courseformat->get_format() . '\\courseformat\\stateupdates';
108
        if (!class_exists($updatesclass)) {
109
            $updatesclass = $defaultupdatesclass;
110
        }
111
        $updates = new $updatesclass($courseformat);
112
 
113
        if (!is_a($updates, $defaultupdatesclass)) {
114
            throw new coding_exception("The \"$updatesclass\" class must extend \"$defaultupdatesclass\"");
115
        }
116
 
117
        // Get the actions class from the course format.
118
        $actionsclass = 'format_'. $courseformat->get_format().'\\courseformat\\stateactions';
119
        if (!class_exists($actionsclass)) {
120
            $actionsclass = 'core_courseformat\\stateactions';
121
        }
122
        $actions = new $actionsclass();
123
 
124
        $action = 'create_module';
125
        if (!is_callable([$actions, $action])) {
126
            throw new moodle_exception("Invalid course state action $action in ".get_class($actions));
127
        }
128
 
129
        $course = $courseformat->get_course();
130
 
131
        // Execute the action.
132
        $actions->$action($updates, $course, $modname, $targetsectionnum, $targetcmid);
133
 
134
        // Dispatch the hook for post course content update.
135
        $hook = new \core_courseformat\hook\after_course_content_updated(
136
            course: $course
137
        );
138
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
139
 
140
        return json_encode($updates);
141
    }
142
 
143
    /**
144
     * Webservice returns.
145
     *
146
     * @deprecated since Moodle 5.0
147
     * @return external_value
148
     */
149
    public static function execute_returns(): external_value {
150
        return new external_value(PARAM_RAW, 'Encoded course update JSON');
151
    }
152
 
153
    /**
154
     * Mark the function as deprecated.
155
     * @return bool
156
     */
157
    public static function execute_is_deprecated() {
158
        return true;
159
    }
160
}