mirror of
https://github.com/moodle/moodle.git
synced 2025-08-11 20:06:46 +02:00
MDL-80715 core_courseformat: new cm rename action
This commit is contained in:
parent
9d3bbe6c02
commit
c74a5439f6
3 changed files with 255 additions and 41 deletions
|
@ -16,6 +16,8 @@
|
|||
|
||||
namespace core_courseformat\local;
|
||||
|
||||
|
||||
use course_modinfo;
|
||||
/**
|
||||
* Course module course format actions.
|
||||
*
|
||||
|
@ -24,5 +26,59 @@ namespace core_courseformat\local;
|
|||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class cmactions extends baseactions {
|
||||
// All course module actions will go here.
|
||||
/**
|
||||
* Rename a course module.
|
||||
* @param int $cmid the course module id.
|
||||
* @param string $name the new name.
|
||||
* @return bool true if the course module was renamed, false otherwise.
|
||||
*/
|
||||
public function rename(int $cmid, string $name): bool {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->libdir . '/gradelib.php');
|
||||
|
||||
$paramcleaning = empty($CFG->formatstringstriptags) ? PARAM_CLEANHTML : PARAM_TEXT;
|
||||
$name = clean_param($name, $paramcleaning);
|
||||
|
||||
if (empty($name)) {
|
||||
return false;
|
||||
}
|
||||
if (\core_text::strlen($name) > 255) {
|
||||
throw new \moodle_exception('maximumchars', 'moodle', '', 255);
|
||||
}
|
||||
|
||||
// The name is stored in the activity instance record.
|
||||
// However, events, gradebook and calendar API uses a legacy
|
||||
// course module data extraction from the DB instead of a section_info.
|
||||
$cm = get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
|
||||
|
||||
if ($name === $cm->name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$DB->update_record(
|
||||
$cm->modname,
|
||||
(object)[
|
||||
'id' => $cm->instance,
|
||||
'name' => $name,
|
||||
'timemodified' => time(),
|
||||
]
|
||||
);
|
||||
$cm->name = $name;
|
||||
|
||||
\core\event\course_module_updated::create_from_cm($cm)->trigger();
|
||||
|
||||
course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
||||
rebuild_course_cache($cm->course, false, true);
|
||||
|
||||
// Attempt to update the grade item if relevant.
|
||||
$grademodule = $DB->get_record($cm->modname, ['id' => $cm->instance]);
|
||||
$grademodule->cmidnumber = $cm->idnumber;
|
||||
$grademodule->modname = $cm->modname;
|
||||
grade_update_mod_grades($grademodule);
|
||||
|
||||
// Update calendar events with the new name.
|
||||
course_module_update_calendar_events($cm->modname, $grademodule, $cm);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
194
course/format/tests/local/cmactions_test.php
Normal file
194
course/format/tests/local/cmactions_test.php
Normal file
|
@ -0,0 +1,194 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core_courseformat\local;
|
||||
|
||||
/**
|
||||
* Course module format actions class tests.
|
||||
*
|
||||
* @package core_courseformat
|
||||
* @copyright 2024 Ferran Recio <ferran@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @coversDefaultClass \core_courseformat\cmactions
|
||||
*/
|
||||
final class cmactions_test extends \advanced_testcase {
|
||||
/**
|
||||
* Setup to ensure that fixtures are loaded.
|
||||
*/
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test renaming a course module.
|
||||
*
|
||||
* @dataProvider provider_test_rename
|
||||
* @covers ::rename
|
||||
* @param string $newname The new name for the course module.
|
||||
* @param bool $expected Whether the course module was renamed.
|
||||
* @param bool $expectexception Whether an exception is expected.
|
||||
*/
|
||||
public function test_rename(string $newname, bool $expected, bool $expectexception): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['format' => 'topics']);
|
||||
$activity = $this->getDataGenerator()->create_module(
|
||||
'assign',
|
||||
['course' => $course->id, 'name' => 'Old name']
|
||||
);
|
||||
|
||||
$cmactions = new cmactions($course);
|
||||
|
||||
if ($expectexception) {
|
||||
$this->expectException(\moodle_exception::class);
|
||||
}
|
||||
$result = $cmactions->rename($activity->cmid, $newname);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$cminfo = get_fast_modinfo($course)->get_cm($activity->cmid);
|
||||
if ($result) {
|
||||
$this->assertEquals('New name', $cminfo->name);
|
||||
} else {
|
||||
$this->assertEquals('Old name', $cminfo->name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_rename.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function provider_test_rename(): array {
|
||||
return [
|
||||
'Empty name' => [
|
||||
'newname' => '',
|
||||
'expected' => false,
|
||||
'expectexception' => false,
|
||||
],
|
||||
'Maximum length' => [
|
||||
'newname' => str_repeat('a', 256),
|
||||
'expected' => false,
|
||||
'expectexception' => true,
|
||||
],
|
||||
'Valid name' => [
|
||||
'newname' => 'New name',
|
||||
'expected' => true,
|
||||
'expectexception' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rename an activity also rename the calendar events.
|
||||
*
|
||||
* @covers ::rename
|
||||
*/
|
||||
public function test_rename_calendar_events(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
set_config('enablecompletion', 1);
|
||||
|
||||
$course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
|
||||
$activity = $this->getDataGenerator()->create_module(
|
||||
'assign',
|
||||
[
|
||||
'name' => 'Old name',
|
||||
'course' => $course,
|
||||
'completionexpected' => time(),
|
||||
'duedate' => time(),
|
||||
]
|
||||
);
|
||||
$cm = get_coursemodule_from_instance('assign', $activity->id, $course->id);
|
||||
|
||||
// Validate course events naming.
|
||||
$this->assertEquals(2, $DB->count_records('event'));
|
||||
|
||||
$event = $DB->get_record(
|
||||
'event',
|
||||
['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'due']
|
||||
);
|
||||
$this->assertEquals(
|
||||
get_string('calendardue', 'assign', 'Old name'),
|
||||
$event->name
|
||||
);
|
||||
|
||||
$event = $DB->get_record(
|
||||
'event',
|
||||
['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'expectcompletionon']
|
||||
);
|
||||
$this->assertEquals(
|
||||
get_string('completionexpectedfor', 'completion', (object) ['instancename' => 'Old name']),
|
||||
$event->name
|
||||
);
|
||||
|
||||
// Rename activity.
|
||||
$cmactions = new cmactions($course);
|
||||
$result = $cmactions->rename($activity->cmid, 'New name');
|
||||
$this->assertTrue($result);
|
||||
|
||||
// Validate event renaming.
|
||||
$event = $DB->get_record(
|
||||
'event',
|
||||
['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'due']
|
||||
);
|
||||
$this->assertEquals(
|
||||
get_string('calendardue', 'assign', 'New name'),
|
||||
$event->name
|
||||
);
|
||||
|
||||
$event = $DB->get_record(
|
||||
'event',
|
||||
['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'expectcompletionon']
|
||||
);
|
||||
$this->assertEquals(
|
||||
get_string('completionexpectedfor', 'completion', (object) ['instancename' => 'New name']),
|
||||
$event->name
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test renaming an activity trigger a course update log event.
|
||||
*
|
||||
* @covers ::rename
|
||||
*/
|
||||
public function test_rename_course_module_updated_event(): void {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$activity = $this->getDataGenerator()->create_module(
|
||||
'assign',
|
||||
['course' => $course->id, 'name' => 'Old name']
|
||||
);
|
||||
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
$cmactions = new cmactions($course);
|
||||
$result = $cmactions->rename($activity->cmid, 'New name');
|
||||
$this->assertTrue($result);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$event = reset($events);
|
||||
|
||||
// Check that the event data is valid.
|
||||
$this->assertInstanceOf('\core\event\course_module_updated', $event);
|
||||
$this->assertEquals(\context_module::instance($activity->cmid), $event->get_context());
|
||||
}
|
||||
}
|
|
@ -749,49 +749,13 @@ function set_coursemodule_visible($id, $visible, $visibleoncoursepage = 1, bool
|
|||
/**
|
||||
* Changes the course module name
|
||||
*
|
||||
* @param int $id course module id
|
||||
* @param int $cmid course module id
|
||||
* @param string $name new value for a name
|
||||
* @return bool whether a change was made
|
||||
*/
|
||||
function set_coursemodule_name($id, $name) {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->libdir . '/gradelib.php');
|
||||
|
||||
$cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST);
|
||||
|
||||
$module = new \stdClass();
|
||||
$module->id = $cm->instance;
|
||||
|
||||
// Escape strings as they would be by mform.
|
||||
if (!empty($CFG->formatstringstriptags)) {
|
||||
$module->name = clean_param($name, PARAM_TEXT);
|
||||
} else {
|
||||
$module->name = clean_param($name, PARAM_CLEANHTML);
|
||||
}
|
||||
if ($module->name === $cm->name || strval($module->name) === '') {
|
||||
return false;
|
||||
}
|
||||
if (\core_text::strlen($module->name) > 255) {
|
||||
throw new \moodle_exception('maximumchars', 'moodle', '', 255);
|
||||
}
|
||||
|
||||
$module->timemodified = time();
|
||||
$DB->update_record($cm->modname, $module);
|
||||
$cm->name = $module->name;
|
||||
\core\event\course_module_updated::create_from_cm($cm)->trigger();
|
||||
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
||||
rebuild_course_cache($cm->course, false, true);
|
||||
|
||||
// Attempt to update the grade item if relevant.
|
||||
$grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance));
|
||||
$grademodule->cmidnumber = $cm->idnumber;
|
||||
$grademodule->modname = $cm->modname;
|
||||
grade_update_mod_grades($grademodule);
|
||||
|
||||
// Update calendar events with the new name.
|
||||
course_module_update_calendar_events($cm->modname, $grademodule, $cm);
|
||||
|
||||
return true;
|
||||
function set_coursemodule_name($cmid, $name) {
|
||||
$coursecontext = context_module::instance($cmid)->get_course_context();
|
||||
return formatactions::cm($coursecontext->instanceid)->rename($cmid, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue