From 1cf8ca343fbf760668e695dbfb93291289e2808b Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Thu, 19 Feb 2015 21:16:26 -0500 Subject: [PATCH] MDL-49183 Lesson: Add additional events --- mod/lesson/classes/event/page_created.php | 98 ++++++++++++++++++ mod/lesson/classes/event/page_deleted.php | 98 ++++++++++++++++++ mod/lesson/classes/event/page_updated.php | 116 ++++++++++++++++++++++ mod/lesson/lang/en/lesson.php | 3 + mod/lesson/locallib.php | 30 ++++++ mod/lesson/pagetypes/essay.php | 3 + mod/lesson/pagetypes/matching.php | 3 + mod/lesson/pagetypes/truefalse.php | 3 + mod/lesson/tests/events_test.php | 84 ++++++++++++++++ mod/lesson/version.php | 2 +- 10 files changed, 439 insertions(+), 1 deletion(-) create mode 100644 mod/lesson/classes/event/page_created.php create mode 100644 mod/lesson/classes/event/page_deleted.php create mode 100644 mod/lesson/classes/event/page_updated.php diff --git a/mod/lesson/classes/event/page_created.php b/mod/lesson/classes/event/page_created.php new file mode 100644 index 00000000000..de3c1f94cd9 --- /dev/null +++ b/mod/lesson/classes/event/page_created.php @@ -0,0 +1,98 @@ +. + +/** + * The mod_lesson page_added event class. + * + * @package mod_lesson + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +namespace mod_lesson\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The mod_lesson page_created event class. + * + * @property-read array $other { + * Extra information about event. + * + * - string pagetype: the name of the pagetype as defined in the individual page class + * } + * + * @package mod_lesson + * @since Moodle 2.9 + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class page_created extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['objecttable'] = 'lesson_pages'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventpagecreated', 'mod_lesson'); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid, 'pageid' => $this->objectid)); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' has created a ".$this->other['pagetype']." page with the ". + "id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'."; + } + + /** + * Custom validations. + * + * @throws \coding_exception when validation fails. + * @return void + */ + protected function validate_data() { + parent::validate_data(); + // Make sure this class is never used without proper object details. + if (!$this->contextlevel === CONTEXT_MODULE) { + throw new \coding_exception('Context level must be CONTEXT_MODULE.'); + } + if (!isset($this->other['pagetype'])) { + throw new \coding_exception('The \'pagetype\' value must be set in other.'); + } + } +} \ No newline at end of file diff --git a/mod/lesson/classes/event/page_deleted.php b/mod/lesson/classes/event/page_deleted.php new file mode 100644 index 00000000000..a4342a30b02 --- /dev/null +++ b/mod/lesson/classes/event/page_deleted.php @@ -0,0 +1,98 @@ +. + +/** + * The mod_lesson page_added event class. + * + * @package mod_lesson + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +namespace mod_lesson\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The mod_lesson page_deleted event class. + * + * @property-read array $other { + * Extra information about event. + * + * - string pagetype: the name of the pagetype as defined in the individual page class + * } + * + * @package mod_lesson + * @since Moodle 2.9 + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class page_deleted extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['objecttable'] = 'lesson_pages'; + $this->data['crud'] = 'd'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventpagedeleted', 'mod_lesson'); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid)); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' has deleted the ".$this->other['pagetype']." page with the ". + "id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'."; + } + + /** + * Custom validations. + * + * @throws \coding_exception when validation fails. + * @return void + */ + protected function validate_data() { + parent::validate_data(); + // Make sure this class is never used without proper object details. + if (!$this->contextlevel === CONTEXT_MODULE) { + throw new \coding_exception('Context level must be CONTEXT_MODULE.'); + } + if (!isset($this->other['pagetype'])) { + throw new \coding_exception('The \'pagetype\' value must be set in other.'); + } + } +} \ No newline at end of file diff --git a/mod/lesson/classes/event/page_updated.php b/mod/lesson/classes/event/page_updated.php new file mode 100644 index 00000000000..55065f18fa0 --- /dev/null +++ b/mod/lesson/classes/event/page_updated.php @@ -0,0 +1,116 @@ +. + +/** + * The mod_lesson page_added event class. + * + * @package mod_lesson + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +namespace mod_lesson\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The mod_lesson page_updated event class. + * @property-read array $other { + * Extra information about event. + * + * - string pagetype: the name of the pagetype as defined in the individual page class + * } + * + * @package mod_lesson + * @since Moodle 2.9 + * @copyright 2015 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class page_updated extends \core\event\base { + + /** + * Create instance of event. + * + * @param \lesson_page $lessonpage + * @param \context_module $context + * @return page_updated + */ + public static function create_from_lesson_page(\lesson_page $lessonpage, \context_module $context) { + $data = array( + 'context' => $context, + 'objectid' => $lessonpage->properties()->id, + 'other' => array( + 'pagetype' => $lessonpage->get_typestring() + ) + ); + return self::create($data); + } + + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['objecttable'] = 'lesson_pages'; + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventpageupdated', 'mod_lesson'); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid, 'pageid' => $this->objectid)); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' has updated the ".$this->other['pagetype']." page with the ". + "id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'."; + } + + /** + * Custom validations. + * + * @throws \coding_exception when validation fails. + * @return void + */ + protected function validate_data() { + parent::validate_data(); + // Make sure this class is never used without proper object details. + if (!$this->contextlevel === CONTEXT_MODULE) { + throw new \coding_exception('Context level must be CONTEXT_MODULE.'); + } + if (!isset($this->other['pagetype'])) { + throw new \coding_exception('The \'pagetype\' value must be set in other.'); + } + } +} \ No newline at end of file diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index b9714142982..ac7f3d75c3c 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -181,6 +181,9 @@ $string['eventhighscoreadded'] = 'Highscore added'; $string['eventhighscoresviewed'] = 'Highscores viewed'; $string['eventlessonended'] = 'Lesson ended'; $string['eventlessonstarted'] = 'Lesson started'; +$string['eventpagecreated'] = 'Page created'; +$string['eventpageupdated'] = 'Page updated'; +$string['eventpagedeleted'] = 'Page deleted'; $string['eventquestionanswered'] = 'Question answered'; $string['eventquestionviewed'] = 'Question viewed'; $string['false'] = 'False'; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index fd4118a3855..1e9ae0a68c1 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -1846,6 +1846,20 @@ abstract class lesson_page extends lesson_base { $page = lesson_page::load($newpage, $lesson); $page->create_answers($properties); + // Trigger an event: page created. + $eventparams = array( + 'context' => $context, + 'objectid' => $newpage->id, + 'other' => array( + 'pagetype' => $page->get_typestring() + ) + ); + $event = \mod_lesson\event\page_created::create($eventparams); + $snapshot = clone($newpage); + $snapshot->timemodified = 0; + $event->add_record_snapshot('lesson_pages', $snapshot); + $event->trigger(); + $lesson->add_message(get_string('insertedpage', 'lesson').': '.format_string($newpage->title, true), 'notifysuccess'); return $page; @@ -1908,6 +1922,18 @@ abstract class lesson_page extends lesson_base { // ..and the page itself $DB->delete_records("lesson_pages", array("id" => $this->properties->id)); + // Trigger an event: page deleted. + $eventparams = array( + 'context' => $context, + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + $event = \mod_lesson\event\page_deleted::create($eventparams); + $event->add_record_snapshot('lesson_pages', $this->properties); + $event->trigger(); + // Delete files associated with this page. $fs->delete_area_files($context->id, 'mod_lesson', 'page_contents', $this->properties->id); $fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $this->properties->id); @@ -2297,6 +2323,10 @@ abstract class lesson_page extends lesson_base { $properties->timemodified = time(); $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$maxbytes), $context, 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); + + // Trigger an event: page updated. + \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger(); + if ($this->type == self::TYPE_STRUCTURE && $this->get_typeid() != LESSON_PAGE_BRANCHTABLE) { if (count($answers) > 1) { $answer = array_shift($answers); diff --git a/mod/lesson/pagetypes/essay.php b/mod/lesson/pagetypes/essay.php index 28d0d31aca8..546a7db568a 100644 --- a/mod/lesson/pagetypes/essay.php +++ b/mod/lesson/pagetypes/essay.php @@ -164,6 +164,9 @@ class lesson_page_type_essay extends lesson_page { $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); + // Trigger an event: page updated. + \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger(); + if (!array_key_exists(0, $this->answers)) { $this->answers[0] = new stdClass; $this->answers[0]->lessonid = $this->lesson->id; diff --git a/mod/lesson/pagetypes/matching.php b/mod/lesson/pagetypes/matching.php index e5e60006a2c..29775df2127 100644 --- a/mod/lesson/pagetypes/matching.php +++ b/mod/lesson/pagetypes/matching.php @@ -321,6 +321,9 @@ class lesson_page_type_matching extends lesson_page { $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); + // Trigger an event: page updated. + \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger(); + // need to add two to offset correct response and wrong response $this->lesson->maxanswers += 2; for ($i = 0; $i < $this->lesson->maxanswers; $i++) { diff --git a/mod/lesson/pagetypes/truefalse.php b/mod/lesson/pagetypes/truefalse.php index 88aae11fb73..8c8056a48b3 100644 --- a/mod/lesson/pagetypes/truefalse.php +++ b/mod/lesson/pagetypes/truefalse.php @@ -177,6 +177,9 @@ class lesson_page_type_truefalse extends lesson_page { $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id); $DB->update_record("lesson_pages", $properties); + // Trigger an event: page updated. + \mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger(); + // need to reset offset for correct and wrong responses $this->lesson->maxanswers = 2; for ($i = 0; $i < $this->lesson->maxanswers; $i++) { diff --git a/mod/lesson/tests/events_test.php b/mod/lesson/tests/events_test.php index ea0e94f10d2..85d776eb3ef 100644 --- a/mod/lesson/tests/events_test.php +++ b/mod/lesson/tests/events_test.php @@ -53,6 +53,90 @@ class mod_lesson_events_testcase extends advanced_testcase { $this->lesson = new lesson($lesson); } + /** + * Test the page created event. + * + */ + public function test_page_created() { + + // Set up a generator to create content. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $pagerecord = $generator->create_content($this->lesson); + $page = $this->lesson->load_page($pagerecord->id); + + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_lesson\event\page_created', $event); + $this->assertEquals($page->id, $event->objectid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + + /** + * Test the page deleted event. + * + */ + public function test_page_deleted() { + + // Set up a generator to create content. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + // Create a content page. + $pagerecord = $generator->create_content($this->lesson); + // Get the lesson page information. + $page = $this->lesson->load_page($pagerecord->id); + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $page->delete(); + + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_lesson\event\page_deleted', $event); + $this->assertEquals($page->id, $event->objectid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + + /** + * Test the page updated event. + * + * There is no external API for updateing a page, so the unit test will simply + * create and trigger the event and ensure data is returned as expected. + */ + public function test_page_updated() { + + // Trigger an event: page updated. + $eventparams = array( + 'context' => context_module::instance($this->lesson->properties()->cmid), + 'objectid' => 25, + 'other' => array( + 'pagetype' => 'True/false' + ) + ); + + $event = \mod_lesson\event\page_updated::create($eventparams); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_lesson\event\page_updated', $event); + $this->assertEquals(25, $event->objectid); + $this->assertEquals('True/false', $event->other['pagetype']); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + /** * Test the essay attempt viewed event. * diff --git a/mod/lesson/version.php b/mod/lesson/version.php index 8c3f6233801..000270d39ad 100644 --- a/mod/lesson/version.php +++ b/mod/lesson/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015021800; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015021900; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2014110400; // Requires this Moodle version $plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0;