Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…nto MOODLE_23_STABLE
  • Loading branch information...
commit 2dc4d19f7906169455a84ed4deeabfeef5dd2a71 2 parents 482823f + 47b17f9
@damyon damyon authored
Showing with 137 additions and 6 deletions.
  1. +13 −6 course/lib.php
  2. +124 −0 course/tests/courselib_test.php
View
19 course/lib.php
@@ -2917,16 +2917,23 @@ function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
}
}
+ $cminfo = new stdClass();
+ $cminfo->id = $id;
+ $cminfo->visible = $visible;
+
if ($prevstateoverrides) {
- if ($visible == '0') {
- // Remember the current visible state so we can toggle this back.
- $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id'=>$id));
+ // If we making whole section visiblility change..
+ if ($visible == 0) {
+ // Retain previous visibility state.
+ $cminfo->visibleold = $cm->visible;
} else {
- // Get the previous saved visible states.
- return $DB->set_field('course_modules', 'visible', $cm->visibleold, array('id'=>$id));
+ // Restore previous visibility state.
+ $cminfo->visible = $cm->visibleold;
}
+ } else {
+ $cminfo->visibleold = $visible;
}
- return $DB->set_field("course_modules", "visible", $visible, array("id"=>$id));
+ return $DB->update_record('course_modules', $cminfo);
}
/**
View
124 course/tests/courselib_test.php
@@ -291,6 +291,130 @@ public function test_move_module_in_course() {
$this->assertEquals($newsection->id, $cm->section);
}
+ public function test_module_visibility() {
+ $this->setAdminUser();
+ $this->resetAfterTest(true);
+
+ // Create course and modules.
+ $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
+ $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+ $page = $this->getDataGenerator()->create_module('page', array('duedate' => time(), 'course' => $course->id));
+ $modules = compact('forum', 'page');
+
+ // Hiding the modules.
+ foreach ($modules as $mod) {
+ set_coursemodule_visible($mod->cmid, 0);
+ $this->check_module_visibility($mod, 0, 0);
+ }
+
+ // Showing the modules.
+ foreach ($modules as $mod) {
+ set_coursemodule_visible($mod->cmid, 1);
+ $this->check_module_visibility($mod, 1, 1);
+ }
+ }
+
+ public function test_section_visibility() {
+ $this->setAdminUser();
+ $this->resetAfterTest(true);
+
+ // Create course.
+ $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
+
+ // Testing an empty section.
+ $sectionnumber = 1;
+ set_section_visible($course->id, $sectionnumber, 0);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 0);
+ set_section_visible($course->id, $sectionnumber, 1);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 1);
+
+ // Testing a section with visible modules.
+ $sectionnumber = 2;
+ $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
+ array('section' => $sectionnumber));
+ $page= $this->getDataGenerator()->create_module('page', array('duedate' => time(),
+ 'course' => $course->id), array('section' => $sectionnumber));
+ $modules = compact('forum', 'page');
+ set_section_visible($course->id, $sectionnumber, 0);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 0);
+ foreach ($modules as $mod) {
+ $this->check_module_visibility($mod, 0, 1);
+ }
+ set_section_visible($course->id, $sectionnumber, 1);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 1);
+ foreach ($modules as $mod) {
+ $this->check_module_visibility($mod, 1, 1);
+ }
+
+ // Testing a section with hidden modules, which should stay hidden.
+ $sectionnumber = 3;
+ $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
+ array('section' => $sectionnumber));
+ $page = $this->getDataGenerator()->create_module('page', array('duedate' => time(),
+ 'course' => $course->id), array('section' => $sectionnumber));
+ $modules = compact('forum', 'page');
+ foreach ($modules as $mod) {
+ set_coursemodule_visible($mod->cmid, 0);
+ $this->check_module_visibility($mod, 0, 0);
+ }
+ set_section_visible($course->id, $sectionnumber, 0);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 0);
+ foreach ($modules as $mod) {
+ $this->check_module_visibility($mod, 0, 0);
+ }
+ set_section_visible($course->id, $sectionnumber, 1);
+ $section_info = get_fast_modinfo($course)->get_section_info($sectionnumber);
+ $this->assertEquals($section_info->visible, 1);
+ foreach ($modules as $mod) {
+ $this->check_module_visibility($mod, 0, 0);
+ }
+ }
+
+ /**
+ * Helper function to assert that a module has correctly been made visible, or hidden.
+ *
+ * @param stdClass $mod module information
+ * @param int $visibility the current state of the module
+ * @param int $visibleold the current state of the visibleold property
+ * @return void
+ */
+ public function check_module_visibility($mod, $visibility, $visibleold) {
+ global $DB;
+
+ rebuild_course_cache($mod->course);
+ $course = $DB->get_record('course', array('id' => $mod->course));
+ $cm = get_fast_modinfo($course)->get_cm($mod->cmid);
+
+ $this->assertEquals($visibility, $cm->visible);
+ $this->assertEquals($visibleold, $cm->visibleold);
+
+ // Check the module grade items.
+ $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
+ 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
+ if ($grade_items) {
+ foreach ($grade_items as $grade_item) {
+ if ($visibility) {
+ $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
+ } else {
+ $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
+ }
+ }
+ }
+
+ // Check the events visibility.
+ if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
+ foreach ($events as $event) {
+ $calevent = new calendar_event($event);
+ $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
+ }
+ }
+ }
+
/**
* Tests moving a module between hidden/visible sections and
* verifies that the course/module visiblity seettings are
Please sign in to comment.
Something went wrong with that request. Please try again.