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
namespace core_courseformat;
18
 
19
use coding_exception;
20
use core_courseformat\base as course_format;
21
use renderer_base;
22
use stdClass;
23
use course_modinfo;
24
use JsonSerializable;
25
 
26
/**
27
 * Class to track state actions.
28
 *
29
 * The methods from this class should be executed via "stateactions" methods.
30
 *
31
 * Each format plugin could extend this class to provide new updates to the frontend
32
 * mutation module.
33
 * Extended classes should be located in "format_XXX\course" namespace and
34
 * extends {@see \core_courseformat\stateupdates}.
35
 *
36
 * @package    core_course
37
 * @copyright  2021 Ferran Recio <ferran@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class stateupdates implements JsonSerializable {
41
 
42
    /** @var course_format format the course format */
43
    protected $format;
44
 
45
    /** @var renderer_base renderer format renderer */
46
    protected $output;
47
 
48
    /** @var array the tracked updates */
49
    protected $updates;
50
 
51
    /**
52
     * State update class constructor.
53
     *
54
     * @param course_format $format Course format.
55
     */
56
    public function __construct(course_format $format) {
57
        global $PAGE;
58
 
59
        $this->format = $format;
60
        $this->output = $this->format->get_renderer($PAGE);
61
        $this->updates = [];
62
    }
63
 
64
    /**
65
     * Return the data to serialize the current track in JSON.
66
     *
67
     * @return stdClass the statement data structure
68
     */
69
    public function jsonSerialize(): array {
70
        return $this->updates;
71
    }
72
 
73
    /**
74
     * Add track about a general course state change.
75
     */
76
    public function add_course_put(): void {
77
        $courseclass = $this->format->get_output_classname('state\\course');
78
        $currentstate = new $courseclass($this->format);
79
        $this->add_update('course', 'put', $currentstate->export_for_template($this->output));
80
    }
81
 
82
    /**
83
     * Add track about a section state put.
84
     *
85
     * @param int $sectionid The affected section id.
86
     */
87
    public function add_section_put(int $sectionid): void {
88
        $this->create_or_put_section($sectionid, 'put');
89
    }
90
 
91
    /**
92
     * Add track about a new section created.
93
     *
94
     * @param int $sectionid The affected section id.
95
     */
96
    public function add_section_create(int $sectionid): void {
97
        $this->create_or_put_section($sectionid, 'create');
98
    }
99
 
100
    /**
101
     * Add track about section created or put.
102
     *
103
     * @param int $sectionid The affected section id.
104
     * @param string $action The action to track for the section ('create' or 'put').
105
     */
106
    protected function create_or_put_section(int $sectionid, string $action): void {
107
        if ($action != 'create' && $action != 'put') {
108
            throw new coding_exception(
109
                "Invalid action passed ($action) to create_or_put_section. Only 'create' and 'put' are valid."
110
            );
111
        }
112
        $course = $this->format->get_course();
113
        $modinfo = course_modinfo::instance($course);
114
        $format = $this->format;
115
 
116
        $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST);
117
 
118
        if (!$format->is_section_visible($section)) {
119
            return;
120
        }
121
 
122
        $sectionclass = $format->get_output_classname('state\\section');
123
        $currentstate = new $sectionclass($this->format, $section);
124
 
125
        $this->add_update('section', $action, $currentstate->export_for_template($this->output));
126
 
127
        // If the section is delegated to a component, give the component oportunity to add updates.
128
        $delegated = $section->get_component_instance();
129
        if ($delegated) {
130
            $delegated->put_section_state_extra_updates($section, $this);
131
        }
132
    }
133
 
134
    /**
135
     * Add track about a section deleted.
136
     *
137
     * @deprecated since Moodle 4.1 MDL-74925 - please call add_section_remove() instead.
138
     * @param int $sectionid The affected section id.
139
     */
140
    public function add_section_delete(int $sectionid): void {
141
        debugging('add_section_delete() is deprecated. Please use add_section_remove() instead.', DEBUG_DEVELOPER);
142
 
143
        $this->add_update('section', 'remove', (object)['id' => $sectionid]);
144
    }
145
 
146
    /**
147
     * Add track about a section removed.
148
     *
149
     * @param int $sectionid The affected section id.
150
     */
151
    public function add_section_remove(int $sectionid): void {
152
        $this->add_update('section', 'remove', (object)['id' => $sectionid]);
153
    }
154
 
155
    /**
156
     * Add track about a course module state update.
157
     *
158
     * @param int $cmid the affected course module id
159
     */
160
    public function add_cm_put(int $cmid): void {
161
        $this->create_or_put_cm($cmid, 'put');
162
    }
163
 
164
    /**
165
     * Add track about a course module created.
166
     *
167
     * @param int $cmid the affected course module id
168
     */
169
    public function add_cm_create(int $cmid): void {
170
        $this->create_or_put_cm($cmid, 'create', true);
171
    }
172
 
173
    /**
174
     * Add track about section created or put.
175
     *
176
     * @param int $cmid The affected course module id.
177
     * @param string $action The action to track for the section ('create' or 'put').
178
     */
179
    protected function create_or_put_cm(int $cmid, string $action): void {
180
        $modinfo = course_modinfo::instance($this->format->get_course());
181
 
182
        $cm = $modinfo->get_cm($cmid);
183
        $section = $modinfo->get_section_info_by_id($cm->section);
184
        $format = $this->format;
185
 
186
        if (!$section->uservisible || !$cm->is_visible_on_course_page()) {
187
            return;
188
        }
189
 
190
        $cmclass = $format->get_output_classname('state\\cm');
191
        $currentstate = new $cmclass($this->format, $section, $cm);
192
 
193
        $this->add_update('cm', $action, $currentstate->export_for_template($this->output));
194
    }
195
 
196
    /**
197
     * Add track about a course module deleted.
198
     *
199
     * @deprecated since Moodle 4.1 MDL-74925 - please call add_cm_remove() instead.
200
     * @param int $cmid the affected course module id
201
     */
202
    public function add_cm_delete(int $cmid): void {
203
        debugging('add_cm_delete() is deprecated. Please use add_cm_remove() instead.', DEBUG_DEVELOPER);
204
 
205
        $this->add_update('cm', 'remove', (object)['id' => $cmid]);
206
    }
207
 
208
    /**
209
     * Add track about a course module removed.
210
     *
211
     * @param int $cmid the affected course module id
212
     */
213
    public function add_cm_remove(int $cmid): void {
214
        $this->add_update('cm', 'remove', (object)['id' => $cmid]);
215
    }
216
 
217
    /**
218
     * Add a valid update message to the update list.
219
     *
220
     * @param string $name the update name
221
     * @param string $action the update action (usually update, create, remove)
222
     * @param stdClass $fields the object fields
223
     */
224
    protected function add_update(string $name, string $action, stdClass $fields): void {
225
        $this->updates[] = (object)[
226
            'name' => $name,
227
            'action' => $action,
228
            'fields' => $fields,
229
        ];
230
    }
231
 
232
}