Skip to content

Commit

Permalink
Merge branch 'MDL-45229-master' of git://github.com/FMCorz/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Apr 28, 2014
2 parents cd360f6 + 1f6988b commit 48dba53
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
11 changes: 11 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,17 @@ function set_section_visible($courseid, $sectionnumber, $visibility) {
$resourcestotoggle = array();
if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
$DB->set_field("course_sections", "visible", "$visibility", array("id"=>$section->id));

$event = \core\event\course_section_updated::create(array(
'context' => context_course::instance($courseid),
'objectid' => $section->id,
'other' => array(
'sectionnum' => $sectionnumber
)
));
$event->add_record_snapshot('course_sections', $section);
$event->trigger();

if (!empty($section->sequence)) {
$modules = explode(",", $section->sequence);
foreach ($modules as $moduleid) {
Expand Down
13 changes: 12 additions & 1 deletion course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ public function test_section_visibility() {
// Create course.
$course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));

$sink = $this->redirectEvents();

// Testing an empty section.
$sectionnumber = 1;
set_section_visible($course->id, $sectionnumber, 0);
Expand All @@ -997,6 +999,11 @@ public function test_section_visibility() {
$section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
$this->assertEquals($section_info->visible, 1);

// Checking that an event was fired.
$events = $sink->get_events();
$this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
$sink->close();

// Testing a section with visible modules.
$sectionnumber = 2;
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
Expand Down Expand Up @@ -1785,7 +1792,10 @@ public function test_course_section_updated_event() {
array(
'objectid' => $section->id,
'courseid' => $course->id,
'context' => context_course::instance($course->id)
'context' => context_course::instance($course->id),
'other' => array(
'sectionnum' => $section->section
)
)
);
$event->add_record_snapshot('course_sections', $section);
Expand All @@ -1802,6 +1812,7 @@ public function test_course_section_updated_event() {
$this->assertEquals($section->id, $event->objectid);
$this->assertEquals($course->id, $event->courseid);
$this->assertEquals($coursecontext->id, $event->contextid);
$this->assertEquals($section->section, $event->other['sectionnum']);
$expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid;
$this->assertEquals($expecteddesc, $event->get_description());
$url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
Expand Down
13 changes: 13 additions & 0 deletions lib/classes/event/course_section_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ protected function get_legacy_logdata() {
$sectiondata = $this->get_record_snapshot('course_sections', $this->objectid);
return array($this->courseid, 'course', 'editsection', 'editsection.php?id=' . $this->objectid, $sectiondata->section);
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['sectionnum'])) {
throw new \coding_exception('The sectionnum must be set in $other');
}
}
}

0 comments on commit 48dba53

Please sign in to comment.