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\local;
18
 
19
 
20
use course_modinfo;
21
/**
22
 * Course module course format actions.
23
 *
24
 * @package    core_courseformat
25
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class cmactions extends baseactions {
29
    /**
30
     * Rename a course module.
31
     * @param int $cmid the course module id.
32
     * @param string $name the new name.
33
     * @return bool true if the course module was renamed, false otherwise.
34
     */
35
    public function rename(int $cmid, string $name): bool {
36
        global $CFG, $DB;
37
        require_once($CFG->libdir . '/gradelib.php');
38
 
39
        $paramcleaning = empty($CFG->formatstringstriptags) ? PARAM_CLEANHTML : PARAM_TEXT;
40
        $name = clean_param($name, $paramcleaning);
41
 
42
        if (empty($name)) {
43
            return false;
44
        }
45
        if (\core_text::strlen($name) > 255) {
46
            throw new \moodle_exception('maximumchars', 'moodle', '', 255);
47
        }
48
 
49
        // The name is stored in the activity instance record.
50
        // However, events, gradebook and calendar API uses a legacy
51
        // course module data extraction from the DB instead of a section_info.
52
        $cm = get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
53
 
54
        if ($name === $cm->name) {
55
            return false;
56
        }
57
 
58
        $DB->update_record(
59
            $cm->modname,
60
            (object)[
61
                'id' => $cm->instance,
62
                'name' => $name,
63
                'timemodified' => time(),
64
            ]
65
        );
66
        $cm->name = $name;
67
 
68
        \core\event\course_module_updated::create_from_cm($cm)->trigger();
69
 
70
        course_modinfo::purge_course_module_cache($cm->course, $cm->id);
71
        rebuild_course_cache($cm->course, false, true);
72
 
73
        // Modules may add some logic to renaming.
74
        $modinfo = get_fast_modinfo($cm->course);
75
        \core\di::get(\core\hook\manager::class)->dispatch(
76
            new \core_courseformat\hook\after_cm_name_edited($modinfo->get_cm($cm->id), $name),
77
        );
78
 
79
        // Attempt to update the grade item if relevant.
80
        $grademodule = $DB->get_record($cm->modname, ['id' => $cm->instance]);
81
        $grademodule->cmidnumber = $cm->idnumber;
82
        $grademodule->modname = $cm->modname;
83
        grade_update_mod_grades($grademodule);
84
 
85
        // Update calendar events with the new name.
86
        course_module_update_calendar_events($cm->modname, $grademodule, $cm);
87
 
88
        return true;
89
    }
90
}