* Move the module object $mod to the specified $section
* If $beforemod exists then that is the module
* before which $modid should be inserted
- * All parameters are objects
+ *
+ * @param stdClass|cm_info $mod
+ * @param stdClass|section_info $section
+ * @param int|stdClass $beforemod id or object with field id corresponding to the module
+ * 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)
*/
function moveto_module($mod, $section, $beforemod=NULL) {
global $OUTPUT, $DB;
-/// Remove original module from original section
+ // 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");
}
- // if moving to a hidden section then hide module
+ // If moving to a hidden section then hide module.
if ($mod->section != $section->id) {
if (!$section->visible && $mod->visible) {
- // Set this in the object because it is sent as a response to ajax calls.
- $mod->visible = 0;
+ // 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);
- // Set this in the object because it is sent as a response to ajax calls.
- $mod->visible = $mod->visibleold;
+ $modvisible = $mod->visibleold;
}
}
-/// Add the module into the new section
+ // Add the module into the new section.
course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
- return true;
+ return $modvisible;
}
/**
// 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;
- $result = moveto_module($cm, $newsection);
- $this->assertTrue($result);
+ moveto_module($cm, $newsection);
$cms = get_fast_modinfo($course)->get_cms();
$cm = reset($cms);
$forumcm = $modinfo->cms[$forum->cmid];
$pagecm = $modinfo->cms[$page->cmid];
- // Move the forum and the page to a hidden section.
- moveto_module($forumcm, $hiddensection);
- moveto_module($pagecm, $hiddensection);
+ // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
+ $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
+ $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
$modinfo = get_fast_modinfo($course);
$this->assertEquals($quizcm->visible, 1);
// Move forum and page back to visible section.
+ // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
$visiblesection = $modinfo->get_section_info(2);
- moveto_module($forumcm, $visiblesection);
- moveto_module($pagecm, $visiblesection);
+ $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
+ $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
$modinfo = get_fast_modinfo($course);
- // Verify that forum has been made visible.
+ // Double check that forum has been made visible.
$forumcm = $modinfo->cms[$forum->cmid];
$this->assertEquals($forumcm->visible, 1);
- // Verify that page has stayed invisible.
+ // Double check that page has stayed invisible.
$pagecm = $modinfo->cms[$page->cmid];
$this->assertEquals($pagecm->visible, 0);
- // Move the page in the same section (this is what mod duplicate does_
- moveto_module($pagecm, $visiblesection, $forumcm);
+ // Move the page in the same section (this is what mod duplicate does).
+ // Visibility of page remains 0.
+ $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
- // Verify that the the page is still hidden
+ // Double check that the the page is still hidden.
$modinfo = get_fast_modinfo($course);
$pagecm = $modinfo->cms[$page->cmid];
$this->assertEquals($pagecm->visible, 0);
$this->assertEquals($section->id, $pagecm->section);
- // Move the forum and the page to a hidden section.
- moveto_module($pagecm, $section, $forumcm);
+ // Move the page inside the hidden section. Make sure it is hidden.
+ $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
- // Verify that the the page is still hidden
+ // Double check that the the page is still hidden.
$modinfo = get_fast_modinfo($course);
$pagecm = $modinfo->cms[$page->cmid];
$this->assertEquals($pagecm->visible, 0);