1
0
mirror of https://github.com/moodle/moodle.git synced 2025-03-20 07:30:01 +01:00

MDL-49183 Lesson: Add additional events

This commit is contained in:
Stephen Bourget 2015-02-19 21:16:26 -05:00
parent 95751e81ac
commit 1cf8ca343f
10 changed files with 439 additions and 1 deletions

@ -0,0 +1,98 @@
<?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/>.
/**
* The mod_lesson page_added event class.
*
* @package mod_lesson
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace mod_lesson\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lesson page_created event class.
*
* @property-read array $other {
* Extra information about event.
*
* - string pagetype: the name of the pagetype as defined in the individual page class
* }
*
* @package mod_lesson
* @since Moodle 2.9
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class page_created extends \core\event\base {
/**
* Set basic properties for the event.
*/
protected function init() {
$this->data['objecttable'] = 'lesson_pages';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventpagecreated', 'mod_lesson');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid, 'pageid' => $this->objectid));
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has created a ".$this->other['pagetype']." page with the ".
"id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'.";
}
/**
* Custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
parent::validate_data();
// Make sure this class is never used without proper object details.
if (!$this->contextlevel === CONTEXT_MODULE) {
throw new \coding_exception('Context level must be CONTEXT_MODULE.');
}
if (!isset($this->other['pagetype'])) {
throw new \coding_exception('The \'pagetype\' value must be set in other.');
}
}
}

@ -0,0 +1,98 @@
<?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/>.
/**
* The mod_lesson page_added event class.
*
* @package mod_lesson
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace mod_lesson\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lesson page_deleted event class.
*
* @property-read array $other {
* Extra information about event.
*
* - string pagetype: the name of the pagetype as defined in the individual page class
* }
*
* @package mod_lesson
* @since Moodle 2.9
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class page_deleted extends \core\event\base {
/**
* Set basic properties for the event.
*/
protected function init() {
$this->data['objecttable'] = 'lesson_pages';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventpagedeleted', 'mod_lesson');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid));
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has deleted the ".$this->other['pagetype']." page with the ".
"id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'.";
}
/**
* Custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
parent::validate_data();
// Make sure this class is never used without proper object details.
if (!$this->contextlevel === CONTEXT_MODULE) {
throw new \coding_exception('Context level must be CONTEXT_MODULE.');
}
if (!isset($this->other['pagetype'])) {
throw new \coding_exception('The \'pagetype\' value must be set in other.');
}
}
}

@ -0,0 +1,116 @@
<?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/>.
/**
* The mod_lesson page_added event class.
*
* @package mod_lesson
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace mod_lesson\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lesson page_updated event class.
* @property-read array $other {
* Extra information about event.
*
* - string pagetype: the name of the pagetype as defined in the individual page class
* }
*
* @package mod_lesson
* @since Moodle 2.9
* @copyright 2015 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class page_updated extends \core\event\base {
/**
* Create instance of event.
*
* @param \lesson_page $lessonpage
* @param \context_module $context
* @return page_updated
*/
public static function create_from_lesson_page(\lesson_page $lessonpage, \context_module $context) {
$data = array(
'context' => $context,
'objectid' => $lessonpage->properties()->id,
'other' => array(
'pagetype' => $lessonpage->get_typestring()
)
);
return self::create($data);
}
/**
* Set basic properties for the event.
*/
protected function init() {
$this->data['objecttable'] = 'lesson_pages';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventpageupdated', 'mod_lesson');
}
/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/lesson/view.php', array('id' => $this->contextinstanceid, 'pageid' => $this->objectid));
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has updated the ".$this->other['pagetype']." page with the ".
"id '$this->objectid' in the lesson activity with course module id '$this->contextinstanceid'.";
}
/**
* Custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
parent::validate_data();
// Make sure this class is never used without proper object details.
if (!$this->contextlevel === CONTEXT_MODULE) {
throw new \coding_exception('Context level must be CONTEXT_MODULE.');
}
if (!isset($this->other['pagetype'])) {
throw new \coding_exception('The \'pagetype\' value must be set in other.');
}
}
}

@ -181,6 +181,9 @@ $string['eventhighscoreadded'] = 'Highscore added';
$string['eventhighscoresviewed'] = 'Highscores viewed';
$string['eventlessonended'] = 'Lesson ended';
$string['eventlessonstarted'] = 'Lesson started';
$string['eventpagecreated'] = 'Page created';
$string['eventpageupdated'] = 'Page updated';
$string['eventpagedeleted'] = 'Page deleted';
$string['eventquestionanswered'] = 'Question answered';
$string['eventquestionviewed'] = 'Question viewed';
$string['false'] = 'False';

@ -1846,6 +1846,20 @@ abstract class lesson_page extends lesson_base {
$page = lesson_page::load($newpage, $lesson);
$page->create_answers($properties);
// Trigger an event: page created.
$eventparams = array(
'context' => $context,
'objectid' => $newpage->id,
'other' => array(
'pagetype' => $page->get_typestring()
)
);
$event = \mod_lesson\event\page_created::create($eventparams);
$snapshot = clone($newpage);
$snapshot->timemodified = 0;
$event->add_record_snapshot('lesson_pages', $snapshot);
$event->trigger();
$lesson->add_message(get_string('insertedpage', 'lesson').': '.format_string($newpage->title, true), 'notifysuccess');
return $page;
@ -1908,6 +1922,18 @@ abstract class lesson_page extends lesson_base {
// ..and the page itself
$DB->delete_records("lesson_pages", array("id" => $this->properties->id));
// Trigger an event: page deleted.
$eventparams = array(
'context' => $context,
'objectid' => $this->properties->id,
'other' => array(
'pagetype' => $this->get_typestring()
)
);
$event = \mod_lesson\event\page_deleted::create($eventparams);
$event->add_record_snapshot('lesson_pages', $this->properties);
$event->trigger();
// Delete files associated with this page.
$fs->delete_area_files($context->id, 'mod_lesson', 'page_contents', $this->properties->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'page_answers', $this->properties->id);
@ -2297,6 +2323,10 @@ abstract class lesson_page extends lesson_base {
$properties->timemodified = time();
$properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$maxbytes), $context, 'mod_lesson', 'page_contents', $properties->id);
$DB->update_record("lesson_pages", $properties);
// Trigger an event: page updated.
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
if ($this->type == self::TYPE_STRUCTURE && $this->get_typeid() != LESSON_PAGE_BRANCHTABLE) {
if (count($answers) > 1) {
$answer = array_shift($answers);

@ -164,6 +164,9 @@ class lesson_page_type_essay extends lesson_page {
$properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
$DB->update_record("lesson_pages", $properties);
// Trigger an event: page updated.
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
if (!array_key_exists(0, $this->answers)) {
$this->answers[0] = new stdClass;
$this->answers[0]->lessonid = $this->lesson->id;

@ -321,6 +321,9 @@ class lesson_page_type_matching extends lesson_page {
$properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
$DB->update_record("lesson_pages", $properties);
// Trigger an event: page updated.
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
// need to add two to offset correct response and wrong response
$this->lesson->maxanswers += 2;
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {

@ -177,6 +177,9 @@ class lesson_page_type_truefalse extends lesson_page {
$properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
$DB->update_record("lesson_pages", $properties);
// Trigger an event: page updated.
\mod_lesson\event\page_updated::create_from_lesson_page($this, $context)->trigger();
// need to reset offset for correct and wrong responses
$this->lesson->maxanswers = 2;
for ($i = 0; $i < $this->lesson->maxanswers; $i++) {

@ -53,6 +53,90 @@ class mod_lesson_events_testcase extends advanced_testcase {
$this->lesson = new lesson($lesson);
}
/**
* Test the page created event.
*
*/
public function test_page_created() {
// Set up a generator to create content.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
// Trigger and capture the event.
$sink = $this->redirectEvents();
$pagerecord = $generator->create_content($this->lesson);
$page = $this->lesson->load_page($pagerecord->id);
// Get our event event.
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\mod_lesson\event\page_created', $event);
$this->assertEquals($page->id, $event->objectid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the page deleted event.
*
*/
public function test_page_deleted() {
// Set up a generator to create content.
$generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
// Create a content page.
$pagerecord = $generator->create_content($this->lesson);
// Get the lesson page information.
$page = $this->lesson->load_page($pagerecord->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$page->delete();
// Get our event event.
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\mod_lesson\event\page_deleted', $event);
$this->assertEquals($page->id, $event->objectid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the page updated event.
*
* There is no external API for updateing a page, so the unit test will simply
* create and trigger the event and ensure data is returned as expected.
*/
public function test_page_updated() {
// Trigger an event: page updated.
$eventparams = array(
'context' => context_module::instance($this->lesson->properties()->cmid),
'objectid' => 25,
'other' => array(
'pagetype' => 'True/false'
)
);
$event = \mod_lesson\event\page_updated::create($eventparams);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\mod_lesson\event\page_updated', $event);
$this->assertEquals(25, $event->objectid);
$this->assertEquals('True/false', $event->other['pagetype']);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the essay attempt viewed event.
*

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015021800; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015021900; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;