Skip to content

Commit

Permalink
Merge branch 'MDL-38700-master' of git://github.com/danpoltawski/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	course/tests/courselib_test.php
  • Loading branch information
Damyon Wiese committed Apr 2, 2013
2 parents 08e5aed + bb1592c commit 9ab0aec
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 11 deletions.
24 changes: 13 additions & 11 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2340,17 +2340,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
Expand Down
133 changes: 133 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,137 @@ public function test_compare_activities_by_time_asc() {
$this->assertGreaterThanOrEqual($last, $activity->timestamp);
}
}

/**
* 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 9ab0aec

Please sign in to comment.