Commit ef2a9ece authored by Petr Skoda's avatar Petr Skoda
Browse files

MDL-45144 convert book events to create_from_xxx() helpers

parent 98cb244d
......@@ -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.
......
......@@ -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;
}
}
......@@ -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.
......
......@@ -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.
......
......@@ -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;
}}
......@@ -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.
......
......@@ -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));
......
......@@ -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
......
......@@ -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)) {
......
......@@ -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++;
}
......
......@@ -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
......
......@@ -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();
......
......@@ -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.
......
......@@ -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);
......
......@@ -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();
......
......@@ -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();
}
}
}
......
......@@ -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.
......
......@@ -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.
......
......@@ -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 '</body> </html>';
} 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);
......
......@@ -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
);