Permalink
Browse files

MDL-45144 convert book events to create_from_xxx() helpers

  • Loading branch information...
1 parent 98cb244 commit ef2a9ece71664782586a11d28cec01f1b5f1f043 Petr Skoda committed Apr 17, 2014
@@ -34,6 +34,27 @@
* @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 @@
* @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 @@ public function get_description() {
* @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 @@ protected function init() {
$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 @@
* @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 @@
* @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 @@
* @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 @@
* @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.
View
@@ -60,14 +60,7 @@
} 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 @@
$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));
View
@@ -74,17 +74,9 @@
$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 @@
$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
View
@@ -47,12 +47,7 @@
$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)) {
View
@@ -173,13 +173,9 @@
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++;
}
View
@@ -47,36 +47,26 @@
// 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
Oops, something went wrong.

0 comments on commit ef2a9ec

Please sign in to comment.