/** * Test book_view * @return void */ public function test_book_view() { global $CFG, $DB; $CFG->enablecompletion = 1; $this->resetAfterTest(); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id), array('completion' => 2, 'completionview' => 1)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); $context = context_module::instance($book->cmid); $cm = get_coursemodule_from_instance('book', $book->id); // Trigger and capture the event. $sink = $this->redirectEvents(); // Check just opening the book. book_view($book, 0, false, $course, $cm, $context); $events = $sink->get_events(); $this->assertCount(1, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_book\\event\\course_module_viewed', $event); $this->assertEquals($context, $event->get_context()); $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id)); $this->assertEquals($moodleurl, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); // Check viewing one book chapter (the only one so it will be the first and last). book_view($book, $chapter, true, $course, $cm, $context); $events = $sink->get_events(); // We expect a total of 4 events. One for module viewed, one for chapter viewed and two belonging to completion. $this->assertCount(4, $events); // Check completion status. $completion = new completion_info($course); $completiondata = $completion->get_data($cm); $this->assertEquals(1, $completiondata->completionstate); }
/** * Simulate the book/view.php web interface page: trigger events, completion, etc... * * @param int $bookid the book instance id * @param int $chapterid the book chapter id * @return array of warnings and status result * @since Moodle 3.0 * @throws moodle_exception */ public static function view_book($bookid, $chapterid = 0) { global $DB, $CFG; require_once($CFG->dirroot . "/mod/book/lib.php"); require_once($CFG->dirroot . "/mod/book/locallib.php"); $params = self::validate_parameters(self::view_book_parameters(), array( 'bookid' => $bookid, 'chapterid' => $chapterid )); $bookid = $params['bookid']; $chapterid = $params['chapterid']; $warnings = array(); // Request and permission validation. $book = $DB->get_record('book', array('id' => $bookid), '*', MUST_EXIST); list($course, $cm) = get_course_and_cm_from_instance($book, 'book'); $context = context_module::instance($cm->id); self::validate_context($context); require_capability('mod/book:read', $context); $chapters = book_preload_chapters($book); $firstchapterid = 0; $lastchapterid = 0; foreach ($chapters as $ch) { if ($ch->hidden) { continue; } if (!$firstchapterid) { $firstchapterid = $ch->id; } $lastchapterid = $ch->id; } if (!$chapterid) { // Trigger the module viewed events since we are displaying the book. book_view($book, null, false, $course, $cm, $context); $chapterid = $firstchapterid; } // Check if book is empty (warning). if (!$chapterid) { $warnings[] = array( 'item' => 'book', 'itemid' => $book->id, 'warningcode' => '1', 'message' => get_string('nocontent', 'mod_book') ); } else { $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id)); $viewhidden = has_capability('mod/book:viewhiddenchapters', $context); if (!$chapter or ($chapter->hidden and !$viewhidden)) { throw new moodle_exception('errorchapter', 'mod_book'); } // Trigger the chapter viewed event. $islastchapter = ($chapter->id == $lastchapterid) ? true : false; book_view($book, $chapter, $islastchapter, $course, $cm, $context); } $result = array(); $result['status'] = true; $result['warnings'] = $warnings; return $result; }
} else { $chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' . $cm->id . '&chapterid=' . $nextid . '">' . $navnext . ':<span class="chaptername"> ' . $nexttitle . '<span class="arrow"> ' . $OUTPUT->rarrow() . '</span></span></a>'; } } else { $navexit = get_string('navexit', 'book'); $sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section)); $returnurl = course_get_url($course, $sec); if ($book->navstyle == 1) { $chnavigation .= '<a title="' . $navexit . '" class="bookexit" href="' . $returnurl . '">' . '<img src="' . $OUTPUT->pix_url('nav_exit', 'mod_book') . '" class="icon" alt="' . $navexit . '" /></a>'; } else { $chnavigation .= ' <a title="' . $navexit . '" class="bookexit" href="' . $returnurl . '">' . '<span class="chaptername">' . $navexit . ' ' . $OUTPUT->uarrow() . '</span></a>'; } $islastchapter = true; } } book_view($book, $chapter, $islastchapter, $course, $cm, $context); // ===================================================== // Book display HTML code // ===================================================== echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($book->name)); $navclasses = book_get_nav_classes(); if ($book->navstyle) { // Upper navigation. echo '<div class="navtop clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>'; } // The chapter itself. $hidden = $chapter->hidden ? ' dimmed_text' : null; echo $OUTPUT->box_start('generalbox book_content' . $hidden); if (!$book->customtitles) { if (!$chapter->subchapter) {