Merge branch 'MDL-40060-master' of git://github.com/FMCorz/moodle

This commit is contained in:
Dan Poltawski
2013-10-08 14:33:27 +08:00
28 changed files with 1607 additions and 12 deletions
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book chapter created event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book chapter created event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chapter_created extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The chapter $this->objectid of the book $this->context->instanceid has been created.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'add chapter', 'view.php?id=' . $this->context->instanceid . '&chapterid=' .
$this->objectid, $this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_chapter_created', 'mod_book');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array(
'id' => $this->context->instanceid,
'chapterid' => $this->objectid
));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'book_chapters';
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book chapter deleted event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book chapter deleted event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chapter_deleted extends \core\event\base {
/**
* Legacy log data.
*
* @var array
*/
protected $legacylogdata;
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The chapter $this->objectid of the book $this->context->instanceid has been deleted.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return $this->legacylogdata;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_chapter_deleted', 'mod_book');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'd';
$this->data['level'] = 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;
}
}
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book chapter updated event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book chapter updated event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chapter_updated extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The chapter $this->objectid of the book $this->context->instanceid has been updated.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'update chapter', 'view.php?id=' . $this->context->instanceid . '&chapterid=' .
$this->objectid, $this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_chapter_updated', 'mod_book');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array(
'id' => $this->context->instanceid,
'chapterid' => $this->objectid
));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['level'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'book_chapters';
}
}
+97
View File
@@ -0,0 +1,97 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book chapter viewed event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book chapter viewed event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chapter_viewed extends \core\event\content_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->userid has viewed the chapter $this->objectid of book module $this->context->instanceid";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'view chapter', 'view.php?id=' . $this->context->instanceid .
'&amp;chapterid=' . $this->objectid, $this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_chapter_viewed', 'mod_book');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array('id' => $this->context->instanceid, 'chapterid' => $this->objectid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'book_chapters';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
// Hack to please the parent class. 'view chapter' was the key used in old add_to_log().
$this->data['other']['content'] = 'view chapter';
parent::validate_data();
}
}
@@ -0,0 +1,97 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book course module viewed event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book course module viewed event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\content_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->userid has viewed the book $this->objectid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'view', 'view.php?id=' . $this->context->instanceid, $this->objectid,
$this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_course_module_viewed', 'mod_book');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'book';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
// Hack to please the parent class. 'view' was the key used in old add_to_log().
$this->data['other']['content'] = 'view';
parent::validate_data();
}
}
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book instances list viewed event.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_book\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_book instances list viewed event class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class instances_list_viewed extends \core\event\course_module_instances_list_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User $this->userid viewed the list of book activities in the course $this->courseid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'view all', 'index.php?id=' . $this->courseid, '');
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_instances_list_viewed', 'mod_book');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/index.php', array('id' => $this->courseid));
}
}
+18 -2
View File
@@ -52,7 +52,7 @@ $PAGE->set_heading($course->fullname);
if ($confirm) { // the operation was confirmed.
$fs = get_file_storage();
if (!$chapter->subchapter) { // Delete all its sub-chapters if any
$chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, subchapter');
$chapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id), 'pagenum');
$found = false;
foreach ($chapters as $ch) {
if ($ch->id == $chapter->id) {
@@ -60,16 +60,32 @@ if ($confirm) { // the operation was confirmed.
} 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();
} else if ($found) {
break;
}
}
$chapters->close();
}
$fs->delete_area_files($context->id, 'mod_book', 'chapter', $chapter->id);
$DB->delete_records('book_chapters', array('id'=>$chapter->id));
add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->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();
book_preload_chapters($book); // Fix structure.
$DB->set_field('book', 'revision', $book->revision+1, array('id'=>$book->id));
+14 -2
View File
@@ -76,7 +76,13 @@ if ($mform->is_cancelled()) {
$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);
add_to_log($course->id, 'book', 'update chapter', 'view.php?id='.$cm->id.'&chapterid='.$data->id, $data->id, $cm->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
@@ -102,7 +108,13 @@ if ($mform->is_cancelled()) {
$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);
add_to_log($course->id, 'book', 'add chapter', 'view.php?id='.$cm->id.'&chapterid='.$data->id, $data->id, $cm->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();
}
book_preload_chapters($book); // fix structure
+5 -1
View File
@@ -48,7 +48,11 @@ $PAGE->set_heading($course->fullname);
$PAGE->navbar->add($strbooks);
echo $OUTPUT->header();
add_to_log($course->id, 'book', 'view all', 'index.php?id='.$course->id, '');
$params = array(
'context' => context_course::instance($course->id)
);
$event = \mod_book\event\instances_list_viewed::create($params);
$event->trigger();
// Get all the appropriate data
if (!$books = get_all_instances_in_course('book', $course)) {
+7 -1
View File
@@ -43,9 +43,15 @@ $string['customtitles_help'] = 'Normally the chapter title is displayed in the t
If the custom titles checkbox is ticked, the chapter title is NOT displayed as a heading above the content. A different title (perhaps longer than the chapter title) may be entered as part of the content.';
$string['chapters'] = 'Chapters';
$string['editingchapter'] = 'Editing chapter';
$string['chaptertitle'] = 'Chapter title';
$string['content'] = 'Content';
$string['editingchapter'] = 'Editing chapter';
$string['event_chapter_created'] = 'Chapter created';
$string['event_chapter_deleted'] = 'Chapter deleted';
$string['event_chapter_updated'] = 'Chapter updated';
$string['event_chapter_viewed'] = 'Chapter viewed';
$string['event_instances_list_viewed'] = 'Instances list viewed';
$string['event_course_module_viewed'] = 'Course module viewed';
$string['subchapter'] = 'Subchapter';
$string['nocontent'] = 'No content has been added to this book yet.';
$string['numbering'] = 'Chapter formatting';
+9
View File
@@ -173,10 +173,19 @@ if (!$nothing) {
foreach ($newchapters as $ch) {
$ch->pagenum = $i;
$DB->update_record('book_chapters', $ch);
$params = array(
'context' => $context,
'objectid' => $ch->id
);
$event = \mod_book\event\chapter_updated::create($params);
$event->trigger();
$i++;
}
}
// MDL-39963 Decide what to do with those logs.
add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
+16
View File
@@ -47,6 +47,13 @@ $chapter->hidden = $chapter->hidden ? 0 : 1;
// 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();
// Change visibility of subchapters too.
if (!$chapter->subchapter) {
@@ -58,12 +65,21 @@ if (!$chapter->subchapter) {
} 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();
} else if ($found) {
break;
}
}
}
// MDL-39963 Decide what to do with those logs.
add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id, $book->id, $cm->id);
+224
View File
@@ -0,0 +1,224 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package mod_book
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Events tests class.
*
* @package mod_book
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_book_events_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
public function test_chapter_created() {
// 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_created::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_created', $event);
$this->assertEquals(context_module::instance($book->id), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$expected = array($course->id, 'book', 'add chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
$chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
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);
}
public function test_chapter_deleted() {
// There is no proper API to call to delete 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_deleted::create($params);
$event->add_record_snapshot('book_chapters', $chapter);
$event->set_legacy_logdata(array('1', 2, false));
// 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_deleted', $event);
$this->assertEquals(context_module::instance($book->id), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
$this->assertEventLegacyLogData(array('1', 2, false), $event);
}
public function test_instances_list_viewed() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$course = $this->getDataGenerator()->create_course();
$params = array(
'context' => context_course::instance($course->id)
);
$event = \mod_book\event\instances_list_viewed::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\instances_list_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'book', 'view all', 'index.php?id='.$course->id, '');
$this->assertEventLegacyLogData($expected, $event);
}
public function test_course_module_viewed() {
// There is no proper API to call to trigger this event, 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));
$params = array(
'context' => context_module::instance($book->cmid),
'objectid' => $book->id
);
$event = \mod_book\event\course_module_viewed::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\course_module_viewed', $event);
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($book->id, $event->objectid);
$expected = array($course->id, 'book', 'view', 'view.php?id=' . $book->cmid, $book->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
public function test_chapter_viewed() {
// There is no proper API to call to trigger this event, 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_viewed::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_viewed', $event);
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$expected = array($course->id, 'book', 'view chapter', 'view.php?id=' . $book->cmid . '&amp;chapterid=' .
$chapter->id, $chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
}
+147
View File
@@ -0,0 +1,147 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* mod_book data generator.
*
* @package mod_book
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_book data generator class.
*
* @package mod_book
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_book_generator extends testing_module_generator {
/**
* @var int keep track of how many chapters have been created.
*/
protected $chaptercount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->chaptercount = 0;
parent::reset();
}
/**
* Create new book module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/book/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('Module generator requires $record->course.');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'book') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test book ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->numbering)) {
$record->numbering = BOOK_NUM_NUMBERS;
}
if (!isset($record->customtitles)) {
$record->customtitles = 0;
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = book_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
public function create_chapter($record = null, array $options = null) {
global $DB;
$record = (object) (array) $record;
$options = (array) $options;
$this->chaptercount++;
if (empty($record->bookid)) {
throw new coding_exception('Chapter generator requires $record->bookid');
}
if (empty($record->title)) {
$record->title = "Chapter {$this->chaptercount}";
}
if (empty($record->pagenum)) {
$record->pagenum = 1;
}
if (!isset($record->subchapter)) {
$record->subchapter = 0;
}
if (!isset($record->hidden)) {
$record->hidden = 0;
}
if (!isset($record->importsrc)) {
$record->importsrc = '';
}
if (!isset($record->content)) {
$record->content = "Chapter {$this->chaptercount} content";
}
if (!isset($record->contentformat)) {
$record->contentformat = FORMAT_MOODLE;
}
if (!isset($record->timecreated)) {
$record->timecreated = time();
}
if (!isset($record->timemodified)) {
$record->timemodified = time();
}
// Make room for new page.
$sql = "UPDATE {book_chapters}
SET pagenum = pagenum + 1
WHERE bookid = ? AND pagenum >= ?";
$DB->execute($sql, array($record->bookid, $record->pagenum));
$record->id = $DB->insert_record('book_chapters', $record);
$sql = "UPDATE {book}
SET revision = revision + 1
WHERE id = ?";
$DB->execute($sql, array($record->bookid));
return $record;
}
}
+71
View File
@@ -0,0 +1,71 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Genarator tests.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class.
*
* @package mod_book
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_book_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('book', array('course' => $course->id)));
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$this->assertEquals(1, $DB->count_records('book', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('book', array('course' => $course->id, 'id' => $book->id)));
$params = array('course' => $course->id, 'name' => 'One more book');
$book = $this->getDataGenerator()->create_module('book', $params);
$this->assertEquals(2, $DB->count_records('book', array('course' => $course->id)));
$this->assertEquals('One more book', $DB->get_field_select('book', 'name', 'id = :id', array('id' => $book->id)));
}
public function test_create_chapter() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
$this->assertFalse($DB->record_exists('book_chapters', array('bookid' => $book->id)));
$bookgenerator->create_chapter(array('bookid' => $book->id));
$this->assertTrue($DB->record_exists('book_chapters', array('bookid' => $book->id)));
$chapter = $bookgenerator->create_chapter(array('bookid' => $book->id, 'content' => 'Yay!', 'title' => 'Oops'));
$this->assertEquals(2, $DB->count_records('book_chapters', array('bookid' => $book->id)));
$this->assertEquals('Oops', $DB->get_field_select('book_chapters', 'title', 'id = :id', array('id' => $chapter->id)));
$this->assertEquals('Yay!', $DB->get_field_select('book_chapters', 'content', 'id = :id', array('id' => $chapter->id)));
}
}
@@ -0,0 +1,85 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* booktool_exportimscp book exported event.
*
* @package booktool_exportimscp
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace booktool_exportimscp\event;
defined('MOODLE_INTERNAL') || die();
/**
* booktool_exportimscp book exported event class.
*
* @package booktool_exportimscp
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class book_exported extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->userid has exported the book $this->objectid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'exportimscp', 'tool/exportimscp/index.php?id=' . $this->context->instanceid,
$this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_book_exported', 'booktool_exportimscp');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'book';
}
}
+7 -1
View File
@@ -43,7 +43,13 @@ $context = context_module::instance($cm->id);
require_capability('mod/book:read', $context);
require_capability('booktool/exportimscp:export', $context);
add_to_log($course->id, 'book', 'exportimscp', 'tool/exportimscp/index.php?id='.$cm->id, $book->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $book->id
);
$event = \booktool_exportimscp\event\book_exported::create($params);
$event->add_record_snapshot('book', $book);
$event->trigger();
$file = booktool_exportimscp_build_package($book, $context);
@@ -24,6 +24,7 @@
defined('MOODLE_INTERNAL') || die;
$string['event_book_exported'] = 'Book exported';
$string['exportimscp:export'] = 'Export book as IMS content package';
$string['generateimscp'] = 'Generate IMS CP';
$string['nochapters'] = 'No book chapters found, so unable to export to IMS CP.';
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package booktool_exportimscp
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Events tests class.
*
* @package booktool_exportimscp
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booktool_exportimscp_events_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
public function test_book_exported() {
// There is no proper API to call to test the event, 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));
$params = array(
'context' => context_module::instance($book->cmid),
'objectid' => $book->id
);
$event = \booktool_exportimscp\event\book_exported::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('\booktool_exportimscp\event\book_exported', $event);
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($book->id, $event->objectid);
$expected = array($course->id, 'book', 'exportimscp', 'tool/exportimscp/index.php?id=' . $book->cmid,
$book->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
}
+7 -1
View File
@@ -84,7 +84,13 @@ function toolbook_importhtml_import_chapters($package, $type, $book, $context, $
$chapter->id = $DB->insert_record('book_chapters', $chapter);
$chapters[$chapter->id] = $chapter;
add_to_log($book->course, 'book', 'add chapter', 'view.php?id='.$context->instanceid.'&chapterid='.$chapter->id, $chapter->id, $context->instanceid);
$params = array(
'context' => $context,
'objectid' => $chapter->id
);
$event = \mod_book\event\chapter_created::create($params);
$event->add_record_snapshot('book_chapters', $chapter);
$event->trigger();
}
}
}
Binary file not shown.
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* booktool_importhtml tests.
*
* @package booktool_importhtml
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/mod/book/tool/importhtml/locallib.php');
/**
* booktool_importhtml tests class.
*
* @package booktool_importhtml
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booktool_importhtml_locallib_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
public function test_import_chapters_events() {
$course = $this->getDataGenerator()->create_course();
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
$context = context_module::instance($book->cmid);
$record = new stdClass();
$record->contextid = $context->id;
$record->component = 'phpunit';
$record->filearea = 'test';
$record->itemid = 0;
$record->filepath = '/';
$record->filename = 'chapters.zip';
$fs = get_file_storage();
$file = $fs->create_file_from_pathname($record, __DIR__ . '/fixtures/chapters.zip');
// Importing the chapters.
$sink = $this->redirectEvents();
toolbook_importhtml_import_chapters($file, 2, $book, $context, false);
$events = $sink->get_events();
// Checking the results.
$this->assertCount(5, $events);
foreach ($events as $event) {
$this->assertInstanceOf('\mod_book\event\chapter_created', $event);
$this->assertEquals($context, $event->get_context());
$chapter = $event->get_record_snapshot('book_chapters', $event->objectid);
$this->assertNotEmpty($chapter);
}
}
}
@@ -0,0 +1,85 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* booktool_print book printed event.
*
* @package booktool_print
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace booktool_print\event;
defined('MOODLE_INTERNAL') || die();
/**
* booktool_print book printed event class.
*
* @package booktool_print
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class book_printed extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->userid has printed the book $this->objectid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'print', 'tool/print/index.php?id=' . $this->context->instanceid,
$this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_book_printed', 'booktool_print');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/tool/print/index.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'book';
}
}
@@ -0,0 +1,85 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* booktool_print chapter printed event.
*
* @package booktool_print
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace booktool_print\event;
defined('MOODLE_INTERNAL') || die();
/**
* booktool_print chapter printed event class.
*
* @package booktool_print
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chapter_printed extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user $this->userid has printed the chapter $this->objectid of the book module $this->context->instanceid.";
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'book', 'print chapter', 'tool/print/index.php?id=' . $this->context->instanceid .
'&chapterid=' . $this->objectid, $this->objectid, $this->context->instanceid);
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_chapter_printed', 'booktool_print');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/book/tool/print/index.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'book';
}
}
+15 -2
View File
@@ -77,7 +77,13 @@ if ($chapter) {
require_capability('mod/book:viewhiddenchapters', $context);
}
add_to_log($course->id, 'book', 'print chapter', 'tool/print/index.php?id='.$cm->id.'&chapterid='.$chapter->id, $chapter->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $chapter->id
);
$event = \booktool_print\event\chapter_printed::create($params);
$event->add_record_snapshot('book_chapters', $chapter);
$event->trigger();
// page header
?>
@@ -123,7 +129,14 @@ if ($chapter) {
echo '</body> </html>';
} else {
add_to_log($course->id, 'book', 'print', 'tool/print/index.php?id='.$cm->id, $book->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $book->id
);
$event = \booktool_print\event\book_printed::create($params);
$event->add_record_snapshot('book', $book);
$event->trigger();
$allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
$book->intro = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null);
@@ -24,6 +24,8 @@
defined('MOODLE_INTERNAL') || die;
$string['event_book_printed'] = 'Book printed';
$string['event_chapter_printed'] = 'Chapter printed';
$string['pluginname'] = 'Book printing';
$string['printbook'] = 'Print book';
$string['printchapter'] = 'Print this chapter';
+103
View File
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package booktool_print
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Events tests class.
*
* @package booktool_print
* @category phpunit
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class booktool_print_events_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
public function test_book_printed() {
// There is no proper API to call to test the event, 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));
$params = array(
'context' => context_module::instance($book->cmid),
'objectid' => $book->id
);
$event = \booktool_print\event\book_printed::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('\booktool_print\event\book_printed', $event);
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($book->id, $event->objectid);
$expected = array($course->id, 'book', 'print', 'tool/print/index.php?id=' . $book->cmid, $book->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
public function test_chapter_printed() {
// There is no proper API to call to test the event, 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 = \booktool_print\event\chapter_printed::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('\booktool_print\event\chapter_printed', $event);
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$expected = array($course->id, 'book', 'print chapter', 'tool/print/index.php?id=' . $book->cmid .
'&chapterid=' . $chapter->id, $chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
}
}
+15 -2
View File
@@ -75,7 +75,14 @@ if ($allowedit and !$chapters) {
}
// Check chapterid and read chapter data
if ($chapterid == '0') { // Go to first chapter if no given.
add_to_log($course->id, 'book', 'view', 'view.php?id='.$cm->id, $book->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $book->id
);
$event = \mod_book\event\course_module_viewed::create($params);
$event->add_record_snapshot('book', $book);
$event->trigger();
foreach ($chapters as $ch) {
if ($edit) {
$chapterid = $ch->id;
@@ -110,7 +117,13 @@ unset($chapterid);
// Security checks END.
add_to_log($course->id, 'book', 'view chapter', 'view.php?id='.$cm->id.'&amp;chapterid='.$chapter->id, $chapter->id, $cm->id);
$params = array(
'context' => $context,
'objectid' => $chapter->id
);
$event = \mod_book\event\chapter_viewed::create($params);
$event->add_record_snapshot('book_chapters', $chapter);
$event->trigger();
// Read standard strings.
$strbooks = get_string('modulenameplural', 'mod_book');