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\local;
18
 
19
 
1441 ariadna 20
use core_courseformat\sectiondelegatemodule;
1 efrain 21
use course_modinfo;
22
/**
23
 * Course module course format actions.
24
 *
25
 * @package    core_courseformat
26
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class cmactions extends baseactions {
30
    /**
1441 ariadna 31
     * Update a course delegated section linked to the given module.
32
     *
33
     * @param \stdClass $cm
34
     * @param array $sectionfields to change in section database record.
35
     * @param bool $rebuildcache If true (default), perform a partial cache purge and rebuild.
36
     * @return bool true if any delegated section has been updated, false otherwise.
37
     */
38
    protected function update_delegated(
39
            \stdClass $cm,
40
            array $sectionfields,
41
            bool $rebuildcache = true
42
    ): bool {
43
 
44
        if (!sectiondelegatemodule::has_delegate_class('mod_' . $cm->modname)) {
45
            return false;
46
        }
47
 
48
        // Propagate the changes to delegated section.
49
        $cminfo = \cm_info::create($cm);
50
        if (!$delegatedsection = $cminfo->get_delegated_section_info()) {
51
            return false;
52
        }
53
 
54
        $sectionactions = new sectionactions($this->course);
55
        $sectionactions->update($delegatedsection, $sectionfields);
56
 
57
        if ($rebuildcache) {
58
            course_modinfo::purge_course_section_cache_by_id($cm->course, $delegatedsection->id);
59
            rebuild_course_cache($cm->course, false, true);
60
        }
61
 
62
        return true;
63
    }
64
 
65
    /**
1 efrain 66
     * Rename a course module.
1441 ariadna 67
     *
1 efrain 68
     * @param int $cmid the course module id.
69
     * @param string $name the new name.
70
     * @return bool true if the course module was renamed, false otherwise.
71
     */
72
    public function rename(int $cmid, string $name): bool {
73
        global $CFG, $DB;
74
        require_once($CFG->libdir . '/gradelib.php');
75
 
76
        $paramcleaning = empty($CFG->formatstringstriptags) ? PARAM_CLEANHTML : PARAM_TEXT;
77
        $name = clean_param($name, $paramcleaning);
78
 
79
        if (empty($name)) {
80
            return false;
81
        }
82
        if (\core_text::strlen($name) > 255) {
83
            throw new \moodle_exception('maximumchars', 'moodle', '', 255);
84
        }
85
 
86
        // The name is stored in the activity instance record.
87
        // However, events, gradebook and calendar API uses a legacy
88
        // course module data extraction from the DB instead of a section_info.
89
        $cm = get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
90
 
91
        if ($name === $cm->name) {
92
            return false;
93
        }
94
 
95
        $DB->update_record(
96
            $cm->modname,
97
            (object)[
98
                'id' => $cm->instance,
99
                'name' => $name,
100
                'timemodified' => time(),
101
            ]
102
        );
103
        $cm->name = $name;
1441 ariadna 104
        $fields = new \stdClass();
105
        $fields->name = $name;
1 efrain 106
 
107
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
108
 
109
        course_modinfo::purge_course_module_cache($cm->course, $cm->id);
110
        rebuild_course_cache($cm->course, false, true);
111
 
1441 ariadna 112
        $this->update_delegated($cm, ['name' => $name]);
113
 
1 efrain 114
        // Modules may add some logic to renaming.
115
        $modinfo = get_fast_modinfo($cm->course);
116
        \core\di::get(\core\hook\manager::class)->dispatch(
117
            new \core_courseformat\hook\after_cm_name_edited($modinfo->get_cm($cm->id), $name),
118
        );
119
 
120
        // Attempt to update the grade item if relevant.
121
        $grademodule = $DB->get_record($cm->modname, ['id' => $cm->instance]);
122
        $grademodule->cmidnumber = $cm->idnumber;
123
        $grademodule->modname = $cm->modname;
124
        grade_update_mod_grades($grademodule);
125
 
126
        // Update calendar events with the new name.
127
        course_module_update_calendar_events($cm->modname, $grademodule, $cm);
128
 
129
        return true;
130
    }
1441 ariadna 131
 
132
    /**
133
     * Update a course module.
134
     *
135
     * @param int $cmid the course module id.
136
     * @param int $visible state of the module
137
     * @param int $visibleoncoursepage state of the module on the course page
138
     * @param bool $rebuildcache If true (default), perform a partial cache purge and rebuild.
139
     * @return bool whether course module was updated
140
     */
141
    public function set_visibility(int $cmid, int $visible, int $visibleoncoursepage = 1, bool $rebuildcache = true): bool {
142
        global $DB, $CFG;
143
        require_once($CFG->libdir.'/gradelib.php');
144
        require_once($CFG->dirroot.'/calendar/lib.php');
145
 
146
        if (!$cm = get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST)) {
147
            return false;
148
        }
149
 
150
        // Create events and propagate visibility to associated grade items if the value has changed.
151
        // Only do this if it's changed to avoid accidently overwriting manual showing/hiding of student grades.
152
        if ($cm->visible == $visible && $cm->visibleoncoursepage == $visibleoncoursepage) {
153
            return true;
154
        }
155
 
156
        if (!$modulename = $DB->get_field('modules', 'name', ['id' => $cm->module])) {
157
            return false;
158
        }
159
 
160
        // Updating visible and visibleold to keep them in sync. Only changing a section visibility will
161
        // affect visibleold to allow for an original visibility restore. See set_section_visible().
162
        $cminfo = (object)[
163
                'id' => $cmid,
164
                'visible' => $visible,
165
                'visibleoncoursepage' => $visibleoncoursepage,
166
                'visibleold' => $visible,
167
        ];
168
 
169
        $DB->update_record('course_modules', $cminfo);
170
        $DB->update_record(
171
            $cm->modname,
172
            (object)[
173
                'id' => $cm->instance,
174
                'timemodified' => time(),
175
            ]
176
        );
177
 
178
        $fields = ['visible' => $visible, 'visibleold' => $visible];
179
        $this->update_delegated($cm, $fields, false);
180
 
181
        if ($rebuildcache) {
182
            \course_modinfo::purge_course_module_cache($cm->course, $cm->id);
183
            rebuild_course_cache($cm->course, false, true);
184
        }
185
 
186
        if ($cm->visible == $visible) {
187
            // There is nothing else to change.
188
            return true;
189
        }
190
 
191
        if ($events = $DB->get_records('event', ['instance' => $cm->instance, 'modulename' => $modulename])) {
192
            foreach ($events as $event) {
193
                if ($visible) {
194
                    $event = new \calendar_event($event);
195
                    $event->toggle_visibility(true);
196
                } else {
197
                    $event = new \calendar_event($event);
198
                    $event->toggle_visibility(false);
199
                }
200
            }
201
        }
202
 
203
        // Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there.
204
        // Note that this must be done after updating the row in course_modules, in case
205
        // the modules grade_item_update function needs to access $cm->visible.
206
        $supportsgrade = plugin_supports('mod', $modulename, FEATURE_CONTROLS_GRADE_VISIBILITY) &&
207
                component_callback_exists('mod_' . $modulename, 'grade_item_update');
208
        if ($supportsgrade) {
209
            $instance = $DB->get_record($modulename, ['id' => $cm->instance], '*', MUST_EXIST);
210
            component_callback('mod_' . $modulename, 'grade_item_update', [$instance]);
211
        } else {
212
            $gradeitems = \grade_item::fetch_all([
213
                    'itemtype' => 'mod',
214
                    'itemmodule' => $modulename,
215
                    'iteminstance' => $cm->instance,
216
                    'courseid' => $cm->course,
217
            ]);
218
            if ($gradeitems) {
219
                foreach ($gradeitems as $gradeitem) {
220
                    $gradeitem->set_hidden(!$visible);
221
                }
222
            }
223
        }
224
 
225
        return true;
226
    }
1 efrain 227
}