From ef2a9ece71664782586a11d28cec01f1b5f1f043 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Thu, 17 Apr 2014 14:15:27 +0800 Subject: [PATCH] MDL-45144 convert book events to create_from_xxx() helpers --- mod/book/classes/event/chapter_created.php | 21 +++++++++++++ mod/book/classes/event/chapter_deleted.php | 35 ++++++++++++---------- mod/book/classes/event/chapter_updated.php | 21 +++++++++++++ mod/book/classes/event/chapter_viewed.php | 21 +++++++++++++ .../event/course_module_instance_list_viewed.php | 15 +++++++++- mod/book/classes/event/course_module_viewed.php | 19 ++++++++++++ mod/book/delete.php | 18 ++--------- mod/book/edit.php | 21 +++---------- mod/book/index.php | 7 +---- mod/book/move.php | 8 ++--- mod/book/show.php | 20 ++++--------- mod/book/tests/events_test.php | 33 +++++++------------- .../exportimscp/classes/event/book_exported.php | 19 ++++++++++++ mod/book/tool/exportimscp/index.php | 8 +---- mod/book/tool/exportimscp/tests/events_test.php | 7 ++--- mod/book/tool/importhtml/locallib.php | 9 ++---- mod/book/tool/print/classes/event/book_printed.php | 19 ++++++++++++ .../tool/print/classes/event/chapter_printed.php | 21 +++++++++++++ mod/book/tool/print/index.php | 17 ++--------- mod/book/tool/print/tests/events_test.php | 14 +++------ mod/book/view.php | 18 ++--------- 21 files changed, 212 insertions(+), 159 deletions(-) diff --git a/mod/book/classes/event/chapter_created.php b/mod/book/classes/event/chapter_created.php index 5377973d391..71e6491b5da 100644 --- a/mod/book/classes/event/chapter_created.php +++ b/mod/book/classes/event/chapter_created.php @@ -34,6 +34,27 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class chapter_created extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @param \stdClass $chapter + * @return chapter_created + */ + public static function create_from_chapter(\stdClass $book, \context_module $context, \stdClass $chapter) { + $data = array( + 'context' => $context, + 'objectid' => $chapter->id, + ); + /** @var chapter_created $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + $event->add_record_snapshot('book_chapters', $chapter); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/classes/event/chapter_deleted.php b/mod/book/classes/event/chapter_deleted.php index d882a6b21ad..80ddf7136cf 100644 --- a/mod/book/classes/event/chapter_deleted.php +++ b/mod/book/classes/event/chapter_deleted.php @@ -34,13 +34,27 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class chapter_deleted extends \core\event\base { - /** - * Legacy log data. + * Create instance of event. + * + * @since Moodle 2.7 * - * @var array + * @param \stdClass $book + * @param \context_module $context + * @param \stdClass $chapter + * @return chapter_deleted */ - protected $legacylogdata; + public static function create_from_chapter(\stdClass $book, \context_module $context, \stdClass $chapter) { + $data = array( + 'context' => $context, + 'objectid' => $chapter->id, + ); + /** @var chapter_deleted $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + $event->add_record_snapshot('book_chapters', $chapter); + return $event; + } /** * Returns description of what happened. @@ -57,7 +71,8 @@ class chapter_deleted extends \core\event\base { * @return array|null */ protected function get_legacy_logdata() { - return $this->legacylogdata; + $chapter = $this->get_record_snapshot('book_chapters', $this->objectid); + return array($this->courseid, 'book', 'update', 'view.php?id='.$this->contextinstanceid, $chapter->bookid, $this->contextinstanceid); } /** @@ -88,14 +103,4 @@ class chapter_deleted extends \core\event\base { $this->data['edulevel'] = self::LEVEL_TEACHING; $this->data['objecttable'] = 'book_chapters'; } - - /** - * Set the legacy event log data. - * - * @return array|null - */ - public function set_legacy_logdata($legacydata) { - $this->legacylogdata = $legacydata; - } - } diff --git a/mod/book/classes/event/chapter_updated.php b/mod/book/classes/event/chapter_updated.php index 7293eb4e346..586d5aee2f0 100644 --- a/mod/book/classes/event/chapter_updated.php +++ b/mod/book/classes/event/chapter_updated.php @@ -34,6 +34,27 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class chapter_updated extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @param \stdClass $chapter + * @return chapter_updated + */ + public static function create_from_chapter(\stdClass $book, \context_module $context, \stdClass $chapter) { + $data = array( + 'context' => $context, + 'objectid' => $chapter->id, + ); + /** @var chapter_updated $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + $event->add_record_snapshot('book_chapters', $chapter); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/classes/event/chapter_viewed.php b/mod/book/classes/event/chapter_viewed.php index d6d0f8bd1ba..8a8dee07b98 100644 --- a/mod/book/classes/event/chapter_viewed.php +++ b/mod/book/classes/event/chapter_viewed.php @@ -34,6 +34,27 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class chapter_viewed extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @param \stdClass $chapter + * @return chapter_viewed + */ + public static function create_from_chapter(\stdClass $book, \context_module $context, \stdClass $chapter) { + $data = array( + 'context' => $context, + 'objectid' => $chapter->id, + ); + /** @var chapter_viewed $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + $event->add_record_snapshot('book_chapters', $chapter); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/classes/event/course_module_instance_list_viewed.php b/mod/book/classes/event/course_module_instance_list_viewed.php index a1ad6d75715..2216d31806d 100644 --- a/mod/book/classes/event/course_module_instance_list_viewed.php +++ b/mod/book/classes/event/course_module_instance_list_viewed.php @@ -34,5 +34,18 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed { -} + /** + * Create the event from course record. + * + * @param \stdClass $course + * @return course_module_instance_list_viewed + */ + public static function create_from_course(\stdClass $course) { + $params = array( + 'context' => \context_course::instance($course->id) + ); + $event = \mod_book\event\course_module_instance_list_viewed::create($params); + $event->add_record_snapshot('course', $course); + return $event; + }} diff --git a/mod/book/classes/event/course_module_viewed.php b/mod/book/classes/event/course_module_viewed.php index 19dd67be818..af02c7c6dd0 100644 --- a/mod/book/classes/event/course_module_viewed.php +++ b/mod/book/classes/event/course_module_viewed.php @@ -34,6 +34,25 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class course_module_viewed extends \core\event\course_module_viewed { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @return course_module_viewed + */ + public static function create_from_book(\stdClass $book, \context_module $context) { + $data = array( + 'context' => $context, + 'objectid' => $book->id + ); + /** @var course_module_viewed $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + return $event; + } /** * Init method. diff --git a/mod/book/delete.php b/mod/book/delete.php index 78c7fc1f0b5..1706147c78a 100644 --- a/mod/book/delete.php +++ b/mod/book/delete.php @@ -60,14 +60,7 @@ if ($confirm) { // the operation was confirmed. } else if ($found and $ch->subchapter) { $fs->delete_area_files($context->id, 'mod_book', 'chapter', $ch->id); $DB->delete_records('book_chapters', array('id'=>$ch->id)); - - $params = array( - 'context' => $context, - 'objectid' => $ch->id - ); - $event = \mod_book\event\chapter_deleted::create($params); - $event->add_record_snapshot('book_chapters', $ch); - $event->trigger(); + \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $ch)->trigger(); } else if ($found) { break; } @@ -77,14 +70,7 @@ if ($confirm) { // the operation was confirmed. $fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id); $DB->delete_records('book_chapters', array('id'=>$chapter->id)); - $params = array( - 'context' => $context, - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_deleted::create($params); - $event->add_record_snapshot('book_chapters', $chapter); - $event->set_legacy_logdata(array($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id)); - $event->trigger(); + \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter)->trigger(); book_preload_chapters($book); // Fix structure. $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id)); diff --git a/mod/book/edit.php b/mod/book/edit.php index 9727c90bde4..0d0a9af080d 100644 --- a/mod/book/edit.php +++ b/mod/book/edit.php @@ -74,17 +74,9 @@ if ($mform->is_cancelled()) { $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id); $DB->update_record('book_chapters', $data); $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id)); + $chapter = $DB->get_record('book_chapters', array('id' => $data->id)); - $params = array( - 'context' => $context, - 'objectid' => $data->id - ); - $event = \mod_book\event\chapter_updated::create($params); - foreach ($data as $key => $value) { - $chapter->$key = $value; - } - $event->add_record_snapshot('book_chapters', $chapter); - $event->trigger(); + \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger(); } else { // adding new chapter @@ -108,14 +100,9 @@ if ($mform->is_cancelled()) { $data = file_postupdate_standard_editor($data, 'content', $options, $context, 'mod_book', 'chapter', $data->id); $DB->update_record('book_chapters', $data); $DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id)); + $chapter = $DB->get_record('book_chapters', array('id' => $data->id)); - $params = array( - 'context' => $context, - 'objectid' => $data->id - ); - $event = \mod_book\event\chapter_created::create($params); - $event->add_record_snapshot('book_chapters', $data); - $event->trigger(); + \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger(); } book_preload_chapters($book); // fix structure diff --git a/mod/book/index.php b/mod/book/index.php index f58f358a65c..359ecbd133d 100644 --- a/mod/book/index.php +++ b/mod/book/index.php @@ -47,12 +47,7 @@ $PAGE->set_heading($course->fullname); $PAGE->navbar->add($strbooks); echo $OUTPUT->header(); -$params = array( - 'context' => context_course::instance($course->id) -); -$event = \mod_book\event\course_module_instance_list_viewed::create($params); -$event->add_record_snapshot('course', $course); -$event->trigger(); +\mod_book\event\course_module_instance_list_viewed::create_from_course($course)->trigger(); // Get all the appropriate data if (!$books = get_all_instances_in_course('book', $course)) { diff --git a/mod/book/move.php b/mod/book/move.php index 06499d26673..1da5ef7ff2a 100644 --- a/mod/book/move.php +++ b/mod/book/move.php @@ -173,13 +173,9 @@ if (!$nothing) { foreach ($newchapters as $ch) { $ch->pagenum = $i; $DB->update_record('book_chapters', $ch); + $ch = $DB->get_record('book_chapters', array('id' => $ch->id)); - $params = array( - 'context' => $context, - 'objectid' => $ch->id - ); - $event = \mod_book\event\chapter_updated::create($params); - $event->trigger(); + \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger(); $i++; } diff --git a/mod/book/show.php b/mod/book/show.php index 34b54371c1d..d842a4654d0 100644 --- a/mod/book/show.php +++ b/mod/book/show.php @@ -47,36 +47,26 @@ $chapter->hidden = $chapter->hidden ? 0 : 1; // Update record. $DB->update_record('book_chapters', $chapter); -$params = array( - 'context' => $context, - 'objectid' => $chapter->id -); -$event = \mod_book\event\chapter_updated::create($params); -$event->add_record_snapshot('book_chapters', $chapter); -$event->trigger(); +\mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger(); // Change visibility of subchapters too. if (!$chapter->subchapter) { - $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, subchapter, hidden'); + $chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum ASC'); $found = 0; foreach ($chapters as $ch) { if ($ch->id == $chapter->id) { $found = 1; + } else if ($found and $ch->subchapter) { $ch->hidden = $chapter->hidden; $DB->update_record('book_chapters', $ch); - - $params = array( - 'context' => $context, - 'objectid' => $ch->id - ); - $event = \mod_book\event\chapter_updated::create($params); - $event->trigger(); + \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger(); } else if ($found) { break; } } + $chapters->close(); } book_preload_chapters($book); // fix structure diff --git a/mod/book/tests/events_test.php b/mod/book/tests/events_test.php index 2992884323e..cd95aea24e3 100644 --- a/mod/book/tests/events_test.php +++ b/mod/book/tests/events_test.php @@ -47,14 +47,11 @@ class mod_book_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); + $context = context_module::instance($book->cmid); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_created::create($params); + $event = \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -80,14 +77,11 @@ class mod_book_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); + $context = context_module::instance($book->cmid); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_updated::create($params); + $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -113,16 +107,12 @@ class mod_book_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); + $context = context_module::instance($book->cmid); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_deleted::create($params); - $event->add_record_snapshot('book_chapters', $chapter); - $event->set_legacy_logdata(array('1', 2, false)); + $event = \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter); + $legacy = array($course->id, 'book', 'update', 'view.php?id='.$book->cmid, $book->id, $book->cmid); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -136,7 +126,7 @@ class mod_book_events_testcase extends advanced_testcase { $this->assertEquals(context_module::instance($book->cmid), $event->get_context()); $this->assertEquals($chapter->id, $event->objectid); $this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id)); - $this->assertEventLegacyLogData(array('1', 2, false), $event); + $this->assertEventLegacyLogData($legacy, $event); $this->assertEventContextNotUsed($event); } @@ -201,14 +191,11 @@ class mod_book_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); + $context = context_module::instance($book->cmid); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_viewed::create($params); + $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter); // Triggering and capturing the event. $sink = $this->redirectEvents(); diff --git a/mod/book/tool/exportimscp/classes/event/book_exported.php b/mod/book/tool/exportimscp/classes/event/book_exported.php index 6f7dd63e01f..310857614b3 100644 --- a/mod/book/tool/exportimscp/classes/event/book_exported.php +++ b/mod/book/tool/exportimscp/classes/event/book_exported.php @@ -34,6 +34,25 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class book_exported extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @return book_exported + */ + public static function create_from_book(\stdClass $book, \context_module $context) { + $data = array( + 'context' => $context, + 'objectid' => $book->id + ); + /** @var book_exported $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/tool/exportimscp/index.php b/mod/book/tool/exportimscp/index.php index d12aa42465d..f3c4b0daf22 100644 --- a/mod/book/tool/exportimscp/index.php +++ b/mod/book/tool/exportimscp/index.php @@ -43,13 +43,7 @@ $context = context_module::instance($cm->id); require_capability('mod/book:read', $context); require_capability('booktool/exportimscp:export', $context); -$params = array( - 'context' => $context, - 'objectid' => $book->id -); -$event = \booktool_exportimscp\event\book_exported::create($params); -$event->add_record_snapshot('book', $book); -$event->trigger(); +\booktool_exportimscp\event\book_exported::create_from_book($book, $context)->trigger(); $file = booktool_exportimscp_build_package($book, $context); diff --git a/mod/book/tool/exportimscp/tests/events_test.php b/mod/book/tool/exportimscp/tests/events_test.php index e83d2f159ec..3132a41fb0e 100644 --- a/mod/book/tool/exportimscp/tests/events_test.php +++ b/mod/book/tool/exportimscp/tests/events_test.php @@ -46,12 +46,9 @@ class booktool_exportimscp_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); + $context = context_module::instance($book->cmid); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $book->id - ); - $event = \booktool_exportimscp\event\book_exported::create($params); + $event = \booktool_exportimscp\event\book_exported::create_from_book($book, $context); // Triggering and capturing the event. $sink = $this->redirectEvents(); diff --git a/mod/book/tool/importhtml/locallib.php b/mod/book/tool/importhtml/locallib.php index 286b6e62eb6..5ca3e4743a3 100644 --- a/mod/book/tool/importhtml/locallib.php +++ b/mod/book/tool/importhtml/locallib.php @@ -82,15 +82,10 @@ function toolbook_importhtml_import_chapters($package, $type, $book, $context, $ } $chapter->id = $DB->insert_record('book_chapters', $chapter); + $chapter = $DB->get_record('book_chapters', array('id' => $chapter->id)); $chapters[$chapter->id] = $chapter; - $params = array( - 'context' => $context, - 'objectid' => $chapter->id - ); - $event = \mod_book\event\chapter_created::create($params); - $event->add_record_snapshot('book_chapters', $chapter); - $event->trigger(); + \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter)->trigger(); } } } diff --git a/mod/book/tool/print/classes/event/book_printed.php b/mod/book/tool/print/classes/event/book_printed.php index ca77d3af56b..f397d75fccc 100644 --- a/mod/book/tool/print/classes/event/book_printed.php +++ b/mod/book/tool/print/classes/event/book_printed.php @@ -34,6 +34,25 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class book_printed extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @return book_printed + */ + public static function create_from_book(\stdClass $book, \context_module $context) { + $data = array( + 'context' => $context, + 'objectid' => $book->id + ); + /** @var book_printed $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/tool/print/classes/event/chapter_printed.php b/mod/book/tool/print/classes/event/chapter_printed.php index ed1c79f9640..22cd9889d11 100644 --- a/mod/book/tool/print/classes/event/chapter_printed.php +++ b/mod/book/tool/print/classes/event/chapter_printed.php @@ -34,6 +34,27 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class chapter_printed extends \core\event\base { + /** + * Create instance of event. + * + * @since Moodle 2.7 + * + * @param \stdClass $book + * @param \context_module $context + * @param \stdClass $chapter + * @return chapter_printed + */ + public static function create_from_chapter(\stdClass $book, \context_module $context, \stdClass $chapter) { + $data = array( + 'context' => $context, + 'objectid' => $chapter->id, + ); + /** @var chapter_printed $event */ + $event = self::create($data); + $event->add_record_snapshot('book', $book); + $event->add_record_snapshot('book_chapters', $chapter); + return $event; + } /** * Returns description of what happened. diff --git a/mod/book/tool/print/index.php b/mod/book/tool/print/index.php index 9997117cdef..fb83d719352 100644 --- a/mod/book/tool/print/index.php +++ b/mod/book/tool/print/index.php @@ -76,14 +76,7 @@ if ($chapter) { if ($chapter->hidden) { require_capability('mod/book:viewhiddenchapters', $context); } - - $params = array( - 'context' => $context, - 'objectid' => $chapter->id - ); - $event = \booktool_print\event\chapter_printed::create($params); - $event->add_record_snapshot('book_chapters', $chapter); - $event->trigger(); + \booktool_print\event\chapter_printed::create_from_chapter($book, $context, $chapter)->trigger(); // page header ?> @@ -129,13 +122,7 @@ if ($chapter) { echo ' '; } else { - $params = array( - 'context' => $context, - 'objectid' => $book->id - ); - $event = \booktool_print\event\book_printed::create($params); - $event->add_record_snapshot('book', $book); - $event->trigger(); + \booktool_print\event\book_printed::create_from_book($book, $context)->trigger(); $allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum'); $book->intro = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null); diff --git a/mod/book/tool/print/tests/events_test.php b/mod/book/tool/print/tests/events_test.php index bd3fd7d2b54..da071e58bb1 100644 --- a/mod/book/tool/print/tests/events_test.php +++ b/mod/book/tool/print/tests/events_test.php @@ -46,12 +46,9 @@ class booktool_print_events_testcase extends advanced_testcase { $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); + $context = context_module::instance($book->cmid); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $book->id - ); - $event = \booktool_print\event\book_printed::create($params); + $event = \booktool_print\event\book_printed::create_from_book($book, $context); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -78,12 +75,9 @@ class booktool_print_events_testcase extends advanced_testcase { $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); + $context = context_module::instance($book->cmid); - $params = array( - 'context' => context_module::instance($book->cmid), - 'objectid' => $chapter->id - ); - $event = \booktool_print\event\chapter_printed::create($params); + $event = \booktool_print\event\chapter_printed::create_from_chapter($book, $context, $chapter); // Triggering and capturing the event. $sink = $this->redirectEvents(); diff --git a/mod/book/view.php b/mod/book/view.php index acb7f004a27..3fd9b45f25c 100644 --- a/mod/book/view.php +++ b/mod/book/view.php @@ -75,15 +75,7 @@ if ($allowedit and !$chapters) { } // Check chapterid and read chapter data if ($chapterid == '0') { // Go to first chapter if no given. - $params = array( - 'context' => $context, - 'objectid' => $book->id - ); - $event = \mod_book\event\course_module_viewed::create($params); - $event->add_record_snapshot('course_modules', $cm); - $event->add_record_snapshot('course', $course); - $event->add_record_snapshot('book', $book); - $event->trigger(); + \mod_book\event\course_module_viewed::create_from_book($book, $context)->trigger(); foreach ($chapters as $ch) { if ($edit) { @@ -119,13 +111,7 @@ unset($chapterid); // Security checks END. -$params = array( - 'context' => $context, - 'objectid' => $chapter->id -); -$event = \mod_book\event\chapter_viewed::create($params); -$event->add_record_snapshot('book_chapters', $chapter); -$event->trigger(); +\mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter)->trigger(); // Read standard strings. $strbooks = get_string('modulenameplural', 'mod_book'); -- 2.11.4.GIT