From 1fff1b8cba1ad93407923b56f45f6a8ce914fd41 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Tue, 26 Mar 2013 14:07:18 +0800 Subject: [PATCH 1/2] MDL-38700 course: add tests for moveto_module() * Test visibility when moving around hidden sections * Test for visiblity changes when moving around in the same sections. --- course/tests/courselib_test.php | 133 ++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 6fc792a24534e..c484f50a58bb4 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1072,4 +1072,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); + } } From bb1592c860a027c7d7aaa4de7d7da5a8261802ec Mon Sep 17 00:00:00 2001 From: Charles Fulton Date: Mon, 25 Mar 2013 12:59:50 -0700 Subject: [PATCH 2/2] MDL-38700 course: only change visibility if a module moves to a different section --- course/lib.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/course/lib.php b/course/lib.php index 347d17cde8f8d..cb709b4d33222 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2467,17 +2467,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