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;
18
 
19
use action_menu;
20
use cm_info;
21
use core_courseformat\base as course_format;
22
use core_courseformat\formatactions;
23
use core_courseformat\output\local\content\section\controlmenu;
24
use core_courseformat\stateupdates;
25
use renderer_base;
26
use section_info;
27
use stdClass;
28
 
29
/**
30
 * Class sectiondelegatemodule
31
 *
32
 * @package    core_courseformat
33
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
abstract class sectiondelegatemodule extends sectiondelegate {
37
    /** @var section_info $sectioninfo The section_info object of the delegated section module */
38
 
39
    /** @var cm_info|null $cm The cm_info object of the delegated section module */
40
    private $cm = null;
41
 
42
    /** @var stdClass|null $course The course object of the delegated section module */
43
    private $course = null;
44
 
45
    /**
46
     * Constructor.
47
     * @param section_info $sectioninfo
48
     */
49
    public function __construct(
50
        protected section_info $sectioninfo
51
    ) {
52
        parent::__construct($sectioninfo);
53
        /** @var \course_modinfo $modinfo */
54
        $modinfo = $sectioninfo->modinfo;
55
        try {
56
            // Disabled or missing plugins can throw exceptions.
57
            $this->cm = $modinfo->get_instance_of(
58
                $this->get_module_name(),
59
                $this->sectioninfo->itemid,
60
            );
61
            $this->course = $modinfo->get_course();
62
        } catch (\Exception $e) {
63
            $this->cm = null;
64
            $this->course = null;
65
        }
66
    }
67
 
68
    /**
69
     * Check if the delegated component is enabled.
70
     *
71
     * @return bool
72
     */
73
    public function is_enabled(): bool {
74
        return $this->cm !== null;
75
    }
76
 
77
    /**
78
     * Get the delegated section id controlled by a specific cm.
79
     *
80
     * This method is used when reverse search is needed bu we cannot access the database.
81
     * This happens mostly on backup and restore. Do NOT use for normal operations.
82
     *
83
     * @param stdClass|cm_info $cm a course module compatible data structure.
84
     * @return int the section id.
85
     */
86
    public static function delegated_section_id(stdClass|cm_info $cm): int {
87
        global $DB;
88
        return $DB->get_field(
89
            'course_sections',
90
            'id',
91
            [
92
                'course' => $cm->course,
93
                'component' => explode('\\', static::class)[0],
94
                'itemid' => $cm->instance,
95
            ],
96
            MUST_EXIST
97
        );
98
    }
99
 
100
    /**
101
     * Get the parent section of the current delegated section.
102
     *
103
     * @return section_info|null
104
     */
105
    public function get_parent_section(): ?section_info {
106
        return $this->cm->get_section_info();
107
    }
108
 
109
    /**
110
     * Get the course object.
111
     *
112
     * @return cm_info
113
     */
114
    public function get_cm(): cm_info {
115
        return $this->cm;
116
    }
117
 
118
    /**
119
     * Get the course object.
120
     *
121
     * @return stdClass
122
     */
123
    public function get_course(): stdClass {
124
        return $this->course;
125
    }
126
 
127
    /**
128
     * Get the module name from the section component frankenstyle name.
129
     *
130
     * @return string
131
     */
132
    private function get_module_name(): string {
133
        return \core_component::normalize_component($this->sectioninfo->component)[1];
134
    }
135
 
136
    /**
137
     * Sync the section renaming with the activity name.
138
     *
139
     * @param section_info $section
140
     * @param string|null $newname
141
     * @return string|null
142
     */
143
    public function preprocess_section_name(section_info $section, ?string $newname): ?string {
144
        $cm = get_coursemodule_from_instance($this->get_module_name(), $section->itemid);
145
        if (!$cm) {
146
            return $newname;
147
        }
148
        if (empty($newname) || $newname === $cm->name) {
149
            return $cm->name;
150
        }
151
        formatactions::cm($section->course)->rename($cm->id, $newname);
152
        return $newname;
153
    }
154
 
155
    /**
156
     * Allow delegate plugin to modify the available section menu.
157
     *
158
     * @param course_format $format The course format instance.
159
     * @param controlmenu $controlmenu The control menu instance.
160
     * @param renderer_base $output The renderer instance.
161
     * @return action_menu|null The new action menu with the list of edit control items or null if no action menu is available.
162
     */
163
    public function get_section_action_menu(
164
        course_format $format,
165
        controlmenu $controlmenu,
166
        renderer_base $output,
167
    ): ?action_menu {
168
        $controlmenuclass = $format->get_output_classname('content\\cm\\delegatedcontrolmenu');
169
        $controlmenu = new $controlmenuclass(
170
            $format,
171
            $this->sectioninfo,
172
            $this->cm,
173
        );
174
        return $controlmenu->get_action_menu($output);
175
    }
176
 
177
    /**
178
     * Add extra state updates when put or create a section.
179
     *
180
     * @param section_info $section the affected section.
181
     * @param stateupdates $updates the state updates object to notify the UI.
182
     */
183
    public function put_section_state_extra_updates(section_info $section, stateupdates $updates): void {
184
        $cm = get_coursemodule_from_instance($this->get_module_name(), $section->itemid);
185
        $updates->add_cm_put($cm->id);
186
    }
187
 
188
    public function section_updated(stdClass $sectionrecord): void {
189
        global $DB;
190
 
191
        $cmrecord = [];
192
        if (isset($sectionrecord->availability) && $sectionrecord->availability !== $this->cm->availability) {
193
            $cmrecord['availability'] = $sectionrecord->availability;
194
        }
195
 
196
        if (isset($sectionrecord->visible) && $sectionrecord->visible !== $this->cm->visible) {
197
            $cmrecord['visible'] = $sectionrecord->visible;
198
            $cmrecord['visibleold'] = $sectionrecord->visible;
199
        }
200
 
201
        if (empty($cmrecord)) {
202
            return;
203
        }
204
 
205
        $cmrecord['id'] = $this->cm->id;
206
        $DB->update_record('course_modules', (object)$cmrecord);
207
    }
208
}