MDL-59227 course: Use low level early renaming to avoid dupes

Instead of using set_coursemodule_name() to proceed with the name
change we perform the change earlier in the process, both to avoid
duplicate events caused by the renaming function and also to get the new
name used by all proper generated events.
This commit is contained in:
Eloy Lafuente (stronk7) 2018-09-19 16:55:20 +02:00
parent 4453613767
commit dd3508df5b
2 changed files with 12 additions and 4 deletions

View File

@ -3453,11 +3453,18 @@ function duplicate_module($course, $cm) {
// right below the original one. otherwise it will stay at the
// end of the section.
if ($newcmid) {
// Proceed with activity renaming before everything else. We don't use APIs here to avoid
// triggering a lot of create/update duplicated events.
$newcm = get_coursemodule_from_id($cm->modname, $newcmid, $cm->course);
// Add ' (copy)' to duplicates. Note we don't cleanup or validate lengths here. It comes
// from original name that was valid, so the copy should be too.
$newname = get_string('duplicatedmodule', 'moodle', $newcm->name);
$DB->set_field($cm->modname, 'name', $newname, ['id' => $newcm->instance]);
$section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course));
$modarray = explode(",", trim($section->sequence));
$cmindex = array_search($cm->id, $modarray);
if ($cmindex !== false && $cmindex < count($modarray) - 1) {
$newcm = get_coursemodule_from_id($cm->modname, $newcmid, $cm->course);
moveto_module($newcm, $section, $modarray[$cmindex + 1]);
}
@ -3469,9 +3476,6 @@ function duplicate_module($course, $cm) {
$newcm = get_fast_modinfo($cm->course)->get_cm($newcmid);
$event = \core\event\course_module_created::create_from_cm($newcm);
$event->trigger();
// Add ' (copy)' to duplicates.
set_coursemodule_name($newcm->id, get_string('duplicatedmodule', 'moodle', $newcm->name));
}
return isset($newcm) ? $newcm : null;

View File

@ -2853,6 +2853,10 @@ class core_course_courselib_testcase extends advanced_testcase {
// Ignore obviously different properties.
continue;
}
if ($prop == 'name') {
// We expect ' (copy)' to be added to the original name since MDL-59227.
$value = get_string('duplicatedmodule', 'moodle', $value);
}
$this->assertEquals($value, $newcm->$prop);
}
}