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'); $context = context_module::instance($book->cmid); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter); // 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->cmid), $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); $this->assertEventContextNotUsed($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);