diff --git a/mod/lesson/classes/event/question_answered.php b/mod/lesson/classes/event/question_answered.php new file mode 100644 index 00000000000..5f1b5fef95b --- /dev/null +++ b/mod/lesson/classes/event/question_answered.php @@ -0,0 +1,98 @@ +. + +/** + * The mod_lesson true / false question answered 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 question answered 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 question_answered 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_PARTICIPATING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventquestionanswered', '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 answered the ".$this->other['pagetype'] . + " question with 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.'); + } + } +} diff --git a/mod/lesson/classes/event/question_viewed.php b/mod/lesson/classes/event/question_viewed.php new file mode 100644 index 00000000000..a6d82a699ba --- /dev/null +++ b/mod/lesson/classes/event/question_viewed.php @@ -0,0 +1,98 @@ +. + +/** + * The mod_lesson true / false question viewed 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 question viewed 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 question_viewed extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['objecttable'] = 'lesson_pages'; + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_PARTICIPATING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventquestionviewed', '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 viewed the ".$this->other['pagetype'] . + " question with 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.'); + } + } +} diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index 57d05edbfe9..ef8da1fa946 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -181,6 +181,8 @@ $string['eventhighscoreadded'] = 'Highscore added'; $string['eventhighscoresviewed'] = 'Highscores viewed'; $string['eventlessonended'] = 'Lesson ended'; $string['eventlessonstarted'] = 'Lesson started'; +$string['eventquestionanswered'] = 'Question answered'; +$string['eventquestionviewed'] = 'Question viewed'; $string['false'] = 'False'; $string['fileformat'] = 'File format'; $string['finish'] = 'Finish'; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 8db02b16b1c..69e119c1c9b 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -1993,7 +1993,7 @@ abstract class lesson_page extends lesson_base { * @return stdClass Returns the result of the attempt */ final public function record_attempt($context) { - global $DB, $USER, $OUTPUT; + global $DB, $USER, $OUTPUT, $PAGE; /** * This should be overridden by each page type to actually check the response @@ -2033,7 +2033,19 @@ abstract class lesson_page extends lesson_base { // Only insert a record if we are not reviewing the lesson. if (!$userisreviewing) { if ($this->lesson->retake || (!$this->lesson->retake && $nretakes == 0)) { - $DB->insert_record("lesson_attempts", $attempt); + $attempt->id = $DB->insert_record("lesson_attempts", $attempt); + // Trigger an event: question answered. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + $event = \mod_lesson\event\question_answered::create($eventparams); + $event->add_record_snapshot('lesson_attempts', $attempt); + $event->trigger(); + } } // "number of attempts remaining" message if $this->lesson->maxattempts > 1 diff --git a/mod/lesson/pagetypes/essay.php b/mod/lesson/pagetypes/essay.php index be2edfa73ba..28d0d31aca8 100644 --- a/mod/lesson/pagetypes/essay.php +++ b/mod/lesson/pagetypes/essay.php @@ -77,6 +77,18 @@ class lesson_page_type_essay extends lesson_page { $data->answer = $essayinfo->answer; } $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } public function create_answers($properties) { diff --git a/mod/lesson/pagetypes/matching.php b/mod/lesson/pagetypes/matching.php index c6b1230bb55..6688c0ce774 100644 --- a/mod/lesson/pagetypes/matching.php +++ b/mod/lesson/pagetypes/matching.php @@ -54,6 +54,18 @@ class lesson_page_type_matching extends lesson_page { $data->id = $PAGE->cm->id; $data->pageid = $this->properties->id; $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } diff --git a/mod/lesson/pagetypes/multichoice.php b/mod/lesson/pagetypes/multichoice.php index 5dc763af77a..41ca4103d12 100644 --- a/mod/lesson/pagetypes/multichoice.php +++ b/mod/lesson/pagetypes/multichoice.php @@ -99,6 +99,18 @@ class lesson_page_type_multichoice extends lesson_page { $data->id = $PAGE->cm->id; $data->pageid = $this->properties->id; $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } diff --git a/mod/lesson/pagetypes/numerical.php b/mod/lesson/pagetypes/numerical.php index 6a1b00b9708..9e5414a0e0f 100644 --- a/mod/lesson/pagetypes/numerical.php +++ b/mod/lesson/pagetypes/numerical.php @@ -57,6 +57,18 @@ class lesson_page_type_numerical extends lesson_page { $data->answer = s($attempt->useranswer); } $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } public function check_answer() { diff --git a/mod/lesson/pagetypes/shortanswer.php b/mod/lesson/pagetypes/shortanswer.php index e62dca3be02..798b1800441 100644 --- a/mod/lesson/pagetypes/shortanswer.php +++ b/mod/lesson/pagetypes/shortanswer.php @@ -57,6 +57,18 @@ class lesson_page_type_shortanswer extends lesson_page { $data->answer = s($attempt->useranswer); } $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } public function check_answer() { diff --git a/mod/lesson/pagetypes/truefalse.php b/mod/lesson/pagetypes/truefalse.php index d9896801983..88aae11fb73 100644 --- a/mod/lesson/pagetypes/truefalse.php +++ b/mod/lesson/pagetypes/truefalse.php @@ -61,6 +61,18 @@ class lesson_page_type_truefalse extends lesson_page { $data->id = $PAGE->cm->id; $data->pageid = $this->properties->id; $mform->set_data($data); + + // Trigger an event question viewed. + $eventparams = array( + 'context' => context_module::instance($PAGE->cm->id), + 'objectid' => $this->properties->id, + 'other' => array( + 'pagetype' => $this->get_typestring() + ) + ); + + $event = \mod_lesson\event\question_viewed::create($eventparams); + $event->trigger(); return $mform->display(); } public function check_answer() { diff --git a/mod/lesson/version.php b/mod/lesson/version.php index 84556650829..a8c88c25555 100644 --- a/mod/lesson/version.php +++ b/mod/lesson/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015012300; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015012301; // 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;