Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 15... Línea 15...
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea -... Línea 17...
-
 
17
namespace core_courseformat\local;
17
namespace core_courseformat\local;
18
 
18
 
19
 
19
 
20
use core_courseformat\sectiondelegatemodule;
20
use course_modinfo;
21
use course_modinfo;
21
/**
22
/**
22
 * Course module course format actions.
23
 * Course module course format actions.
23
 *
24
 *
24
 * @package    core_courseformat
25
 * @package    core_courseformat
25
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
28
 */
-
 
29
class cmactions extends baseactions {
-
 
30
    /**
-
 
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;
27
 */
63
    }
-
 
64
 
28
class cmactions extends baseactions {
65
    /**
29
    /**
66
     * Rename a course module.
30
     * Rename a course module.
67
     *
31
     * @param int $cmid the course module id.
68
     * @param int $cmid the course module id.
32
     * @param string $name the new name.
69
     * @param string $name the new name.
Línea 62... Línea 99...
62
                'name' => $name,
99
                'name' => $name,
63
                'timemodified' => time(),
100
                'timemodified' => time(),
64
            ]
101
            ]
65
        );
102
        );
66
        $cm->name = $name;
103
        $cm->name = $name;
-
 
104
        $fields = new \stdClass();
-
 
105
        $fields->name = $name;
Línea 67... Línea 106...
67
 
106
 
Línea 68... Línea 107...
68
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
107
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
69
 
108
 
Línea -... Línea 109...
-
 
109
        course_modinfo::purge_course_module_cache($cm->course, $cm->id);
-
 
110
        rebuild_course_cache($cm->course, false, true);
70
        course_modinfo::purge_course_module_cache($cm->course, $cm->id);
111
 
71
        rebuild_course_cache($cm->course, false, true);
112
        $this->update_delegated($cm, ['name' => $name]);
72
 
113
 
73
        // Modules may add some logic to renaming.
114
        // Modules may add some logic to renaming.
74
        $modinfo = get_fast_modinfo($cm->course);
115
        $modinfo = get_fast_modinfo($cm->course);
Línea 85... Línea 126...
85
        // Update calendar events with the new name.
126
        // Update calendar events with the new name.
86
        course_module_update_calendar_events($cm->modname, $grademodule, $cm);
127
        course_module_update_calendar_events($cm->modname, $grademodule, $cm);
Línea 87... Línea 128...
87
 
128
 
88
        return true;
129
        return true;
-
 
130
    }
-
 
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;
89
    }
226
    }