public function test_chapter_updated() { // There is no proper API to call to generate chapters for a book, so what we are // doing here is simply making sure that the events returns the right information. $course = $this->getDataGenerator()->create_course(); $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)); $params = array('context' => context_module::instance($book->cmid), 'objectid' => $chapter->id); $event = \mod_book\event\chapter_updated::create($params); // Triggering and capturing the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_book\\event\\chapter_updated', $event); $this->assertEquals(context_module::instance($book->id), $event->get_context()); $this->assertEquals($chapter->id, $event->objectid); $expected = array($course->id, 'book', 'update chapter', 'view.php?id=' . $book->cmid . '&chapterid=' . $chapter->id, $chapter->id, $book->cmid); $this->assertEventLegacyLogData($expected, $event); }
$chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id), '*', MUST_EXIST); // Switch hidden state. $chapter->hidden = $chapter->hidden ? 0 : 1; // Update record. $DB->update_record('book_chapters', $chapter); \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter)->trigger(); // Change visibility of subchapters too. if (!$chapter->subchapter) { $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); \mod_book\event\chapter_updated::create_from_chapter($book, $context, $ch)->trigger(); } else { if ($found) { break; } } } } $chapters->close(); } book_preload_chapters($book); // fix structure $DB->set_field('book', 'revision', $book->revision + 1, array('id' => $book->id)); redirect('view.php?id=' . $cm->id . '&chapterid=' . $chapter->id);
if (empty($chapter->id)) { redirect("view.php?id={$cm->id}"); } else { redirect("view.php?id={$cm->id}&chapterid={$chapter->id}"); } } else { if ($data = $mform->get_data()) { if ($data->id) { // store the files $data->timemodified = time(); $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)); add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id=' . $cm->id, 'book ' . $book->id); $params = array('context' => $context, 'objectid' => $data->id); $event = \mod_book\event\chapter_updated::create($params); $event->add_record_snapshot('book_chapters', $data); $event->trigger(); } else { // adding new chapter $data->bookid = $book->id; $data->hidden = 0; $data->timecreated = time(); $data->timemodified = time(); $data->importsrc = ''; $data->content = ''; // updated later $data->contentformat = FORMAT_HTML; // updated later // make room for new page $sql = "UPDATE {book_chapters}\n SET pagenum = pagenum + 1\n WHERE bookid = ? AND pagenum >= ?";