MDL-86854 core_course: refactor moveto_module other usages

This commit is contained in:
Laurent David
2025-12-22 11:56:55 +01:00
committed by Sara Arjona
parent d5f97d42d2
commit cefc1f0166
9 changed files with 86 additions and 43 deletions
@@ -0,0 +1,8 @@
issueNumber: MDL-86854
notes:
core_course:
- message: >-
Deprecates moveto_module (core_course) in favor of
cmactions::move_before or cmactions::move_end_section
(core_courseformat\local\cmactions).
type: deprecated
+6 -2
View File
@@ -16,6 +16,8 @@
namespace core_availability;
use core_courseformat\formatactions;
/**
* Unit tests for info and subclasses.
*
@@ -170,7 +172,8 @@ final class info_test extends \advanced_testcase {
$modinfo = get_fast_modinfo($course);
$section = $modinfo->get_section_info(1);
$cm = $modinfo->get_cm($pages[2]->cmid);
moveto_module($cm, $section);
$cmactions = formatactions::cm($course);
$cmactions->move_end_section($cm->id, $section->id);
// Set the availability restrictions in database. The enableavailability
// setting is off so these do not take effect yet.
@@ -417,7 +420,8 @@ final class info_test extends \advanced_testcase {
$DB->set_field('course_sections', 'availability',
'{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id .']}]}',
array('id' => $section2->id));
moveto_module($modinfo->get_cm($page2->cmid), $section2);
$cmactions = formatactions::cm($course);
$cmactions->move_end_section($page2->cmid, $section2->id);
// With no restrictions, returns full list.
$info = new info_module($modinfo->get_cm($page->cmid));
@@ -16,6 +16,7 @@
namespace core_courseformat\local;
use core_courseformat\formatactions;
use section_info;
use stdClass;
use core\event\course_module_updated;
@@ -335,9 +336,11 @@ class sectionactions extends baseactions {
// Move all modules to section 0.
$modinfo = get_fast_modinfo($this->course->id);
$action = formatactions::cm($this->course);
$section0 = $modinfo->get_section_info(0);
foreach ($modinfo->get_cms() as $cm) {
if ($cm->sectionnum == $sectioninfo->section) {
moveto_module($cm, $modinfo->get_section_info(0));
$action->move_end_section($cm->id, $section0->id);
}
}
+10 -1
View File
@@ -594,10 +594,19 @@ class stateactions {
// Duplicate course modules.
$affectedcmids = [];
$action = formatactions::cm($course);
foreach ($cms as $cm) {
if ($newcm = duplicate_module($course, $cm)) {
if ($targetsection) {
moveto_module($newcm, $targetsection, $beforecm);
if ($beforecm) {
$action->move_before($newcm->id, $beforecm->id);
} else {
// We retrieve the target section directly from the cache to avoid stale information in the section info.
$action->move_end_section(
$newcm->id,
$targetsection->id,
);
}
} else {
$affectedcmids[] = $newcm->id;
}
@@ -15,6 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_courseformat\local;
use core_courseformat\formatactions;
use ReflectionMethod;
use section_info;
use cm_info;
@@ -136,7 +137,8 @@ final class baseactions_test extends \advanced_testcase {
$this->assertEquals($originalcm->name, $cm->name);
// CM info should be always the most updated one.
moveto_module($originalcm, $destinationsection);
$formatactions = formatactions::cm($course);
$formatactions->move_end_section($originalcm->id, $destinationsection->id);
$cm = $method->invoke($baseactions, $originalcm->id);
$this->assertInstanceOf(cm_info::class, $cm);
+33 -32
View File
@@ -1176,42 +1176,41 @@ function reorder_sections($sections, $origin_position, $target_position) {
* before which the module needs to be included. Null for inserting in the
* end of the section
* @return int new value for module visibility (0 or 1)
* @todo Remove this method in Moodle 6.0 (MDL-87465).
*/
#[\core\attribute\deprecated(
replacement: 'core_courseformat\local\cmactions',
since: '5.2',
mdl: 'MDL-86854',
reason: 'Replaced by an cmactions::move_before or cmactions::move_end_section.',
)]
function moveto_module($mod, $section, $beforemod=NULL) {
global $OUTPUT, $DB;
\core\deprecation::emit_deprecation(__FUNCTION__);
if ($section->section != 0 && !course_modinfo::is_mod_type_visible_on_course($mod->modname)) {
throw new coding_exception("Modules with FEATURE_CAN_DISPLAY set to false can not be moved from section 0");
}
// Current module visibility state - return value of this function.
$modvisible = $mod->visible;
// Remove original module from original section.
if (! delete_mod_from_section($mod->id, $mod->section)) {
echo $OUTPUT->notification("Could not delete module from existing section");
[$course, $cm] = get_course_and_cm_from_cmid($mod->id);
$action = \core_courseformat\formatactions::cm($course);
if ($beforemod) {
$action->move_before($cm->id, $beforemod->id);
} else {
// We retrieve the target section directly from the cache to avoid stale information in the section info.
$action->move_end_section(
$cm->id,
$section->id,
);
}
// Add the module into the new section.
course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod, $mod->modname);
// If moving to a hidden section then hide module.
if ($mod->section != $section->id) {
if (!$section->visible && $mod->visible) {
// Module was visible but must become hidden after moving to hidden section.
$modvisible = 0;
set_coursemodule_visible($mod->id, 0);
// Set visibleold to 1 so module will be visible when section is made visible.
$DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
}
if ($section->visible && !$mod->visible) {
// Hidden module was moved to the visible section, restore the module visibility from visibleold.
set_coursemodule_visible($mod->id, $mod->visibleold);
$modvisible = $mod->visibleold;
}
}
return $modvisible;
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($mod->id);
$modvisibility = $cm->visible;
// Purge the cm cache to ensure visibility changes are reflected.
// This was done last in the original method so we need to keep this here for backward compatibility.
// The explanation is that get_fast_modinfo was sometimes called with the last parameter to true in order to purge the cache.
// But this is not working well, so removing the following line will lead to a unit test failure for
// info_test::test_is_user_visible as the course module visibility is not refreshed properly.
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
return $modvisibility;
}
/**
@@ -2657,14 +2656,16 @@ function duplicate_module($course, $cm, ?int $sectionid = null, bool $changename
set_coursemodule_name($newcm->id, $newname);
}
$section = $DB->get_record('course_sections', ['id' => $sectionid ?? $cm->section, 'course' => $cm->course]);
$section = get_fast_modinfo($course)->get_section_info_by_id($sectionid ?? $cm->section);
$action = formatactions::cm($course);
if (isset($sectionid)) {
moveto_module($newcm, $section);
$action->move_end_section($newcm->id, $section->id);
} else {
$modarray = explode(",", trim($section->sequence));
$cmindex = array_search($cm->id, $modarray);
if ($cmindex !== false && $cmindex < count($modarray) - 1) {
moveto_module($newcm, $section, $modarray[$cmindex + 1]);
$beforecmid = $modarray[$cmindex + 1];
$action->move_before($newcm->id, $beforecmid);
}
}
+8 -1
View File
@@ -232,7 +232,14 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
throw new \moodle_exception('needcopy', '', "view.php?id=$section->course");
}
moveto_module($cm, $section, $beforecm);
$formatactions = formatactions::cm($course->id);
if (!empty($section)) {
$formatactions->move_end_section($cm->id, $section->id);
} else if (!empty($beforecm)) {
$formatactions->move_before($cm->id, $beforecm->id);
} else {
throw new \moodle_exception('invalidmovetarget');
}
$sectionreturn = $USER->activitycopysectionreturn;
unset($USER->activitycopy);
+8 -2
View File
@@ -81,7 +81,13 @@ if ($class === 'section' && $field === 'move') {
} else {
$beforemod = null;
}
$isvisible = moveto_module($cm, $section, $beforemod);
$action = \core_courseformat\formatactions::cm($course);
if (!$beforemod) {
$action->move_end_section($cm, $section->id);
} else {
$action->move_before($cm->id, $beforemod->id);
}
$modinfo = get_fast_modinfo($course);
$isvisible = $modinfo->get_cm($cm->id)->is_visible();
echo json_encode(array('visible' => (bool) $isvisible));
}
+6 -3
View File
@@ -31,6 +31,7 @@ use context_system;
use context_coursecat;
use core\event\section_viewed;
use core_completion_external;
use core_courseformat\formatactions;
use core_external;
use core_tag_index_builder;
use core_tag_tag;
@@ -1303,7 +1304,8 @@ final class courselib_test extends advanced_testcase {
$oldsectionid = $cm->section;
// Perform the move
moveto_module($cm, $newsection);
$cmactions = formatactions::cm($course);
$cmactions->move_end_section($cm->id, $newsection->id);
$cms = get_fast_modinfo($course)->get_cms();
$cm = reset($cms);
@@ -1330,7 +1332,7 @@ final class courselib_test extends advanced_testcase {
// Perform a second move as some issues were only seen on the second move
$newsection = get_fast_modinfo($course)->get_section_info(2);
$oldsectionid = $cm->section;
moveto_module($cm, $newsection);
$cmactions->move_end_section($cm->id, $newsection->id);
$cms = get_fast_modinfo($course)->get_cms();
$cm = reset($cms);
@@ -1372,8 +1374,9 @@ final class courselib_test extends advanced_testcase {
// Try to perform the move.
$this->expectExceptionMessageMatches($codingerror);
$cmactions = formatactions::cm($course);
try {
moveto_module($qbankcm, $newsection);
$cmactions->move_end_section($qbankcm->id, $newsection->id);
} finally {
$qbankcms = get_fast_modinfo($course)->get_instances_of('qbank');
$qbankcm = reset($qbankcms);