Skip to content
Browse files

Merge branch 'MDL-38700-24' of git://github.com/danpoltawski/moodle i…

…nto MOODLE_24_STABLE
  • Loading branch information...
2 parents 536780c + 914624d commit f034c23b0a4d1ae2ab0a5adf9ddd132ab6df38d0 @damyon damyon committed Apr 2, 2013
Showing with 146 additions and 11 deletions.
  1. +13 −11 course/lib.php
  2. +133 −0 course/tests/courselib_test.php
View
24 course/lib.php
@@ -3164,17 +3164,19 @@ function moveto_module($mod, $section, $beforemod=NULL) {
}
// if moving to a hidden section then hide module
- if (!$section->visible && $mod->visible) {
- // Set this in the object because it is sent as a response to ajax calls.
- $mod->visible = 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) {
- 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;
+ 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;
+ 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) {
+ 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;
+ }
}
/// Add the module into the new section
View
133 course/tests/courselib_test.php
@@ -535,4 +535,137 @@ public function test_course_page_type_list() {
$pagetypelist = course_page_type_list($pagetype, null, null);
$this->assertEquals($pagetypelist, $testpagetypelist1);
}
+
+ /**
+ * Tests moving a module between hidden/visible sections and
+ * verifies that the course/module visiblity seettings are
+ * retained.
+ */
+ public function test_moveto_module_between_hidden_sections() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
+ $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+ $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
+ $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
+
+ // Set the page as hidden
+ set_coursemodule_visible($page->cmid, 0);
+
+ // Set sections 3 as hidden.
+ set_section_visible($course->id, 3, 0);
+
+ $modinfo = get_fast_modinfo($course);
+
+ $hiddensection = $modinfo->get_section_info(3);
+ // New section is definitely not visible:
+ $this->assertEquals($hiddensection->visible, 0);
+
+ $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);
+
+ // Reset modinfo cache.
+ get_fast_modinfo(0, 0, true);
+
+ $modinfo = get_fast_modinfo($course);
+
+ // Verify that forum and page have been moved to the hidden section and quiz has not.
+ $this->assertContains($forum->cmid, $modinfo->sections[3]);
+ $this->assertContains($page->cmid, $modinfo->sections[3]);
+ $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
+
+ // Verify that forum has been made invisible.
+ $forumcm = $modinfo->cms[$forum->cmid];
+ $this->assertEquals($forumcm->visible, 0);
+ // Verify that old state has been retained.
+ $this->assertEquals($forumcm->visibleold, 1);
+
+ // Verify that page has stayed invisible.
+ $pagecm = $modinfo->cms[$page->cmid];
+ $this->assertEquals($pagecm->visible, 0);
+ // Verify that old state has been retained.
+ $this->assertEquals($pagecm->visibleold, 0);
+
+ // Verify that quiz has been unaffected.
+ $quizcm = $modinfo->cms[$quiz->cmid];
+ $this->assertEquals($quizcm->visible, 1);
+
+ // Move forum and page back to visible section.
+ $visiblesection = $modinfo->get_section_info(2);
+ moveto_module($forumcm, $visiblesection);
+ moveto_module($pagecm, $visiblesection);
+
+ // Reset modinfo cache.
+ get_fast_modinfo(0, 0, true);
+ $modinfo = get_fast_modinfo($course);
+
+ // Verify that forum has been made visible.
+ $forumcm = $modinfo->cms[$forum->cmid];
+ $this->assertEquals($forumcm->visible, 1);
+
+ // Verify 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);
+
+ // Reset modinfo cache.
+ get_fast_modinfo(0, 0, true);
+
+ // Verify that the the page is still hidden
+ $modinfo = get_fast_modinfo($course);
+ $pagecm = $modinfo->cms[$page->cmid];
+ $this->assertEquals($pagecm->visible, 0);
+ }
+
+ /**
+ * Tests moving a module around in the same section. moveto_module()
+ * is called this way in modduplicate.
+ */
+ public function test_moveto_module_in_same_section() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
+ $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
+ $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+ // Simulate inconsistent visible/visibleold values (MDL-38713).
+ $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
+ $cm->visible = 0;
+ $cm->visibleold = 1;
+ $DB->update_record('course_modules', $cm);
+
+ $modinfo = get_fast_modinfo($course);
+ $forumcm = $modinfo->cms[$forum->cmid];
+ $pagecm = $modinfo->cms[$page->cmid];
+
+ // Verify that page is hidden.
+ $this->assertEquals($pagecm->visible, 0);
+
+ // Verify section 0 is where all mods added.
+ $section = $modinfo->get_section_info(0);
+ $this->assertEquals($section->id, $forumcm->section);
+ $this->assertEquals($section->id, $pagecm->section);
+
+
+ // Move the forum and the page to a hidden section.
+ moveto_module($pagecm, $section, $forumcm);
+
+ // Reset modinfo cache.
+ get_fast_modinfo(0, 0, true);
+
+ // Verify that the the page is still hidden
+ $modinfo = get_fast_modinfo($course);
+ $pagecm = $modinfo->cms[$page->cmid];
+ $this->assertEquals($pagecm->visible, 0);
+ }
}

0 comments on commit f034c23

Please sign in to comment.
Something went wrong with that request. Please try again.