mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 04:30:15 +01:00
MDL-50013 mod_book: Move logging and completion to new API
This commit is contained in:
parent
1dabedeedf
commit
1af52267d3
@ -597,4 +597,32 @@ function book_export_contents($cm, $baseurl) {
|
||||
array_unshift($contents, $structurefile);
|
||||
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
||||
*
|
||||
* @param stdClass $book book object
|
||||
* @param stdClass $chapter chapter object
|
||||
* @param bool $islaschapter is the las chapter of the book?
|
||||
* @param stdClass $course course object
|
||||
* @param stdClass $cm course module object
|
||||
* @param stdClass $context context object
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
function book_view($book, $chapter, $islastchapter, $course, $cm, $context) {
|
||||
|
||||
// First case, we are just opening the book.
|
||||
if (empty($chapter)) {
|
||||
\mod_book\event\course_module_viewed::create_from_book($book, $context)->trigger();
|
||||
|
||||
} else {
|
||||
\mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter)->trigger();
|
||||
|
||||
if ($islastchapter) {
|
||||
// We cheat a bit here in assuming that viewing the last page means the user viewed the whole book.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,4 +83,57 @@ class mod_book_lib_testcase extends advanced_testcase {
|
||||
$this->assertEquals(json_encode(array()), $contents[0]['content']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
*/
|
||||
|
||||
require(dirname(__FILE__).'/../../config.php');
|
||||
require_once(dirname(__FILE__).'/lib.php');
|
||||
require_once(dirname(__FILE__).'/locallib.php');
|
||||
require_once($CFG->libdir.'/completionlib.php');
|
||||
|
||||
@ -75,7 +76,8 @@ if ($allowedit and !$chapters) {
|
||||
}
|
||||
// Check chapterid and read chapter data
|
||||
if ($chapterid == '0') { // Go to first chapter if no given.
|
||||
\mod_book\event\course_module_viewed::create_from_book($book, $context)->trigger();
|
||||
// Trigger course module viewed event.
|
||||
book_view($book, null, false, $course, $cm, $context);
|
||||
|
||||
foreach ($chapters as $ch) {
|
||||
if ($edit) {
|
||||
@ -109,10 +111,6 @@ unset($id);
|
||||
unset($bid);
|
||||
unset($chapterid);
|
||||
|
||||
// Security checks END.
|
||||
|
||||
\mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter)->trigger();
|
||||
|
||||
// Read standard strings.
|
||||
$strbooks = get_string('modulenameplural', 'mod_book');
|
||||
$strbook = get_string('modulename', 'mod_book');
|
||||
@ -147,7 +145,7 @@ foreach ($chapters as $ch) {
|
||||
$last = $ch->id;
|
||||
}
|
||||
|
||||
|
||||
$islastchapter = false;
|
||||
if ($book->navstyle) {
|
||||
$navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
|
||||
$navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
|
||||
@ -195,12 +193,12 @@ if ($book->navstyle) {
|
||||
'<span class="chaptername">' . $navexit . ' ' . $OUTPUT->uarrow() . '</span></a>';
|
||||
}
|
||||
|
||||
// We cheat a bit here in assuming that viewing the last page means the user viewed the whole book.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
$islastchapter = true;
|
||||
}
|
||||
}
|
||||
|
||||
book_view($book, $chapter, $islastchapter, $course, $cm, $context);
|
||||
|
||||
// =====================================================
|
||||
// Book display HTML code
|
||||
// =====================================================
|
||||
|
Loading…
x
Reference in New Issue
Block a user