diff --git a/course/editsection.php b/course/editsection.php index c75e3dc0ba4..932a230bfa2 100644 --- a/course/editsection.php +++ b/course/editsection.php @@ -40,7 +40,7 @@ require_login($course); $context = context_course::instance($course->id); require_capability('moodle/course:update', $context); -// get section_info object with all availability options +// Get section_info object with all availability options. $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum); $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); @@ -51,31 +51,44 @@ $mform = course_get_format($course->id)->editsection_form($PAGE->url, $mform->set_data(convert_to_array($sectioninfo)); if ($mform->is_cancelled()){ - // form cancelled, return to course + // Form cancelled, return to course. redirect(course_get_url($course, $section, array('sr' => $sectionreturn))); } else if ($data = $mform->get_data()) { - // data submitted and validated, update and return to course + // Data submitted and validated, update and return to course. $DB->update_record('course_sections', $data); rebuild_course_cache($course->id, true); if (isset($data->section)) { - // usually edit form does not change relative section number but just in case + // Usually edit form does not change relative section number but just in case. $sectionnum = $data->section; } if (!empty($CFG->enableavailability)) { - // Update grade and completion conditions + // Update grade and completion conditions. $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum); condition_info_section::update_section_from_form($sectioninfo, $data); rebuild_course_cache($course->id, true); } course_get_format($course->id)->update_section_format_options($data); - add_to_log($course->id, "course", "editsection", "editsection.php?id=$id", "$sectionnum"); + // Set section info, as this might not be present in form_data. + if (!isset($data->section)) { + $data->section = $sectionnum; + } + // Trigger an event for course section update. + $event = \core\event\course_section_updated::create( + array( + 'objectid' => $data->id, + 'courseid' => $course->id, + 'context' => $context, + 'other' => array('sectionnum' => $data->section) + ) + ); + $event->trigger(); + $PAGE->navigation->clear_cache(); redirect(course_get_url($course, $section, array('sr' => $sectionreturn))); } -// the edit form is displayed for the first time or there was a validation -// error on the previous step. Display the edit form: +// The edit form is displayed for the first time or if there was validation error on the previous step. $sectionname = get_section_name($course, $sectionnum); $stredit = get_string('edita', '', " $sectionname"); $strsummaryof = get_string('summaryof', '', " $sectionname"); diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index b805289ca06..73866f2dee0 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1334,4 +1334,54 @@ class core_course_courselib_testcase extends advanced_testcase { $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign')); $this->assertEmpty($eventcount); } + + /** + * Test that triggering a course_section_updated event works as expected. + */ + public function test_course_section_updated_event() { + global $DB; + + $this->resetAfterTest(); + + // Create the course with sections. + $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true)); + $sections = $DB->get_records('course_sections', array('course' => $course->id)); + + $coursecontext = context_course::instance($course->id); + + $section = array_pop($sections); + $section->name = 'Test section'; + $section->summary = 'Test section summary'; + $DB->update_record('course_sections', $section); + + // Trigger an event for course section update. + $event = \core\event\course_section_updated::create( + array( + 'objectid' => $section->id, + 'courseid' => $course->id, + 'context' => context_course::instance($course->id) + ) + ); + $event->add_record_snapshot('course_sections', $section); + // Trigger and catch event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_section_updated', $event); + $this->assertEquals('course_sections', $event->objecttable); + $this->assertEquals($section->id, $event->objectid); + $this->assertEquals($course->id, $event->courseid); + $this->assertEquals($coursecontext->id, $event->contextid); + $expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid; + $this->assertEquals($expecteddesc, $event->get_description()); + $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid)); + $id = $section->id; + $sectionnum = $section->section; + $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum); + $this->assertEventLegacyLogData($expectedlegacydata, $event); + } } diff --git a/lang/en/moodle.php b/lang/en/moodle.php index fdbabdbea90..f625c432e1a 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -659,6 +659,7 @@ $string['errorcreatingactivity'] = 'Unable to create an instance of activity \'{ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; +$string['eventcoursesectionupdated'] = ' Course section updated'; $string['everybody'] = 'Everybody'; $string['executeat'] = 'Execute at'; $string['existing'] = 'Existing'; diff --git a/lib/classes/event/course_section_updated.php b/lib/classes/event/course_section_updated.php new file mode 100644 index 00000000000..832c9603029 --- /dev/null +++ b/lib/classes/event/course_section_updated.php @@ -0,0 +1,86 @@ +. + +/** + * Course section updated. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Course section updated. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_section_updated extends base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['objecttable'] = 'course_sections'; + $this->data['crud'] = 'u'; + // TODO MDL-41040 set level. + $this->data['level'] = 50; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursesectionupdated'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return 'Course ' . $this->courseid . ' section ' . $this->other['sectionnum'] . ' updated by user ' . $this->userid; + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/course/editsection.php', array('id' => $this->objectid)); + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + 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); + } +}