Merge branch 'MDL-40054_master' of https://github.com/markn86/moodle
authorSam Hemelryk <sam@moodle.com>
Sun, 5 Jan 2014 23:01:54 +0000 (12:01 +1300)
committerSam Hemelryk <sam@moodle.com>
Sun, 5 Jan 2014 23:01:54 +0000 (12:01 +1300)
14 files changed:
mod/lesson/classes/event/course_module_instance_list_viewed.php [new file with mode: 0644]
mod/lesson/classes/event/course_module_viewed.php [new file with mode: 0644]
mod/lesson/classes/event/essay_attempt_viewed.php [new file with mode: 0644]
mod/lesson/classes/event/highscore_added.php [new file with mode: 0644]
mod/lesson/classes/event/highscores_viewed.php [new file with mode: 0644]
mod/lesson/classes/event/lesson_ended.php [new file with mode: 0644]
mod/lesson/classes/event/lesson_started.php [new file with mode: 0644]
mod/lesson/essay.php
mod/lesson/highscores.php
mod/lesson/index.php
mod/lesson/lang/en/lesson.php
mod/lesson/locallib.php
mod/lesson/tests/events_test.php [new file with mode: 0644]
mod/lesson/view.php

diff --git a/mod/lesson/classes/event/course_module_instance_list_viewed.php b/mod/lesson/classes/event/course_module_instance_list_viewed.php
new file mode 100644 (file)
index 0000000..50a89dd
--- /dev/null
@@ -0,0 +1,31 @@
+<?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/>.
+
+/**
+ * Event for when all lesson instances for a course are viewed.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
+    // No code required here as the parent class handles it all.
+}
diff --git a/mod/lesson/classes/event/course_module_viewed.php b/mod/lesson/classes/event/course_module_viewed.php
new file mode 100644 (file)
index 0000000..3625533
--- /dev/null
@@ -0,0 +1,39 @@
+<?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/>.
+
+/**
+ * Course module viewed event.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class course_module_viewed extends \core\event\course_module_viewed {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson';
+        $this->data['crud'] = 'r';
+        $this->data['level'] = self::LEVEL_PARTICIPATING;
+    }
+}
diff --git a/mod/lesson/classes/event/essay_attempt_viewed.php b/mod/lesson/classes/event/essay_attempt_viewed.php
new file mode 100644 (file)
index 0000000..7e066c7
--- /dev/null
@@ -0,0 +1,78 @@
+<?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/>.
+
+/**
+ * Event to be triggered when the an essay attempt is viewed
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class essay_attempt_viewed extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson_attempts';
+        $this->data['crud'] = 'r';
+        $this->data['level'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventessayattemptviewed', 'mod_lesson');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/lesson/essay.php', array('id' => $this->context->instanceid,
+            'mode' => 'grade', 'attemptid' =>  $this->objectid));
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return 'The essay grade for the user with the id ' . $this->relateduserid . ' for the attempt with the id ' .
+            $this->objectid . ' was viewed by the user with the id ' . $this->userid;
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'lesson', 'view grade', 'essay.php?id=' . $this->context->instanceid . '&mode=grade&attemptid='
+            . $this->objectid, get_string('manualgrading', 'lesson'), $this->context->instanceid);
+    }
+}
diff --git a/mod/lesson/classes/event/highscore_added.php b/mod/lesson/classes/event/highscore_added.php
new file mode 100644 (file)
index 0000000..b2204db
--- /dev/null
@@ -0,0 +1,81 @@
+<?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/>.
+
+/**
+ * Event to be triggered when a highscore is added.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class highscore_added extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson_high_scores';
+        $this->data['crud'] = 'c';
+        $this->data['level'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventhighscoreadded', 'mod_lesson');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/lesson/highscores.php', array('id' => $this->context->instanceid));
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid);
+
+        return 'A new highscore was added to the lesson with the id ' . $highscore->lessonid .
+            ' for user with the id ' . $this->userid;
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid);
+
+        return array($this->courseid, 'lesson', 'update highscores', 'highscores.php?id=' . $this->context->instanceid,
+            $highscore->nickname, $this->context->instanceid);
+    }
+}
diff --git a/mod/lesson/classes/event/highscores_viewed.php b/mod/lesson/classes/event/highscores_viewed.php
new file mode 100644 (file)
index 0000000..549e451
--- /dev/null
@@ -0,0 +1,78 @@
+<?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/>.
+
+/**
+ * Event to be triggered when the highscores are viewed.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class highscores_viewed extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson';
+        $this->data['crud'] = 'r';
+        $this->data['level'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventhighscoresviewed', 'mod_lesson');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/lesson/highscores.php', array('id' => $this->context->instanceid));
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return 'The highscores for lesson ' . $this->objectid . ' were viewed by ' . $this->userid;
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        $lesson = $this->get_record_snapshot('lesson', $this->objectid);
+
+        return array($this->courseid, 'lesson', 'view highscores', 'highscores.php?id=' . $this->context->instanceid,
+            $lesson->name, $this->context->instanceid);
+    }
+}
diff --git a/mod/lesson/classes/event/lesson_ended.php b/mod/lesson/classes/event/lesson_ended.php
new file mode 100644 (file)
index 0000000..76963d2
--- /dev/null
@@ -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/>.
+
+/**
+ * Event to be triggered when a lesson is ended.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class lesson_ended extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson';
+        $this->data['crud'] = 'r';
+        $this->data['level'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventlessonended', '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->context->instanceid));
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return 'The lesson ' . $this->objectid . ' was ended by the user ' . $this->userid;
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'lesson', 'end', 'view.php?id=' . $this->context->instanceid, $this->objectid,
+            $this->context->instanceid);
+    }
+}
diff --git a/mod/lesson/classes/event/lesson_started.php b/mod/lesson/classes/event/lesson_started.php
new file mode 100644 (file)
index 0000000..de37727
--- /dev/null
@@ -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/>.
+
+/**
+ * Event to be triggered when a lesson is started.
+ *
+ * @package    mod_lesson
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
+namespace mod_lesson\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class lesson_started extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'lesson';
+        $this->data['crud'] = 'r';
+        $this->data['level'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventlessonstarted', '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->context->instanceid));
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return 'The lesson ' . $this->objectid . ' was started by the user ' . $this->userid;
+    }
+
+    /**
+     * Replace add_to_log() statement.
+     *
+     * @return array of parameters to be passed to legacy add_to_log() function.
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'lesson', 'start', 'view.php?id=' . $this->context->instanceid,
+            $this->objectid, $this->context->instanceid);
+    }
+}
index c824a63..123a166 100644 (file)
@@ -243,8 +243,6 @@ switch ($mode) {
                 $essayinfo->sent = 1;
                 $attempt->useranswer = serialize($essayinfo);
                 $DB->update_record('lesson_attempts', $attempt);
-                // Log it
-                add_to_log($course->id, 'lesson', 'update email essay grade', "essay.php?id=$cm->id", format_string($pages[$attempt->pageid]->title,true).': '.fullname($users[$attempt->userid]), $cm->id);
             }
         }
         $lesson->add_message(get_string('emailsuccess', 'lesson'), 'notifysuccess');
@@ -299,8 +297,6 @@ switch ($mode) {
         }
         break;
 }
-// Log it
-add_to_log($course->id, 'lesson', 'view grade', "essay.php?id=$cm->id", get_string('manualgrading', 'lesson'), $cm->id);
 
 $lessonoutput = $PAGE->get_renderer('mod_lesson');
 echo $lessonoutput->header($lesson, $cm, 'essay', false, null, get_string('manualgrading', 'lesson'));
@@ -384,6 +380,16 @@ switch ($mode) {
         echo html_writer::table($table);
         break;
     case 'grade':
+        // Trigger the essay grade viewed event.
+        $event = \mod_lesson\event\essay_attempt_viewed::create(array(
+            'objectid' => $attempt->id,
+            'relateduserid' => $attempt->userid,
+            'context' => $context,
+            'courseid' => $course->id,
+        ));
+        $event->add_record_snapshot('lesson_attempts', $attempt);
+        $event->trigger();
+
         // Grading form
         // Expects the following to be set: $attemptid, $answer, $user, $page, $attempt
         $essayinfo = unserialize($attempt->useranswer);
index c75dbbf..0493cd7 100644 (file)
@@ -143,10 +143,15 @@ switch ($mode) {
             $newhighscore->gradeid = $newgrade->id;
             $newhighscore->nickname = $name;
 
-            $DB->insert_record('lesson_high_scores', $newhighscore);
+            $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore);
 
-            // Log it
-            add_to_log($course->id, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id);
+            // Trigger highscore updated event.
+            $event = \mod_lesson\event\highscore_added::create(array(
+                'objectid' => $newhighscore->id,
+                'context' => $context,
+                'courseid' => $course->id,
+            ));
+            $event->trigger();
 
             $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess');
             redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1");
@@ -156,8 +161,13 @@ switch ($mode) {
         break;
 }
 
-// Log it
-add_to_log($course->id, 'lesson', 'view highscores', "highscores.php?id=$cm->id", $lesson->name, $cm->id);
+// Trigger highscore viewed event.
+$event = \mod_lesson\event\highscores_viewed::create(array(
+    'objectid' => $lesson->properties()->id,
+    'context' => $context,
+    'courseid' => $course->id
+));
+$event->trigger();
 
 $lessonoutput = $PAGE->get_renderer('mod_lesson');
 echo $lessonoutput->header($lesson, $cm, 'highscores', false, null, get_string('viewhighscores', 'lesson'));
index 1f95c53..ad13f61 100644 (file)
@@ -39,8 +39,12 @@ if (!$course = $DB->get_record("course", array("id" => $id))) {
 require_login($course);
 $PAGE->set_pagelayout('incourse');
 
-add_to_log($course->id, "lesson", "view all", "index.php?id=$course->id", "");
-
+// Trigger instances list viewed event.
+$params = array(
+    'context' => context_course::instance($course->id)
+);
+$event = \mod_lesson\event\course_module_instance_list_viewed::create($params);
+$event->trigger();
 
 /// Get all required strings
 
index 6dc34e7..a0ff52d 100644 (file)
@@ -169,6 +169,11 @@ $string['essayemailmessage2'] = '<p>Essay prompt:<blockquote>{$a->question}</blo
 $string['essayemailsubject'] = 'Your grade for {$a} question';
 $string['essays'] = 'Essays';
 $string['essayscore'] = 'Essay score';
+$string['eventessayattemptviewed'] = 'Essay attempt viewed';
+$string['eventhighscoreadded'] = 'Highscore added';
+$string['eventhighscoresviewed'] = 'Highscores viewed';
+$string['eventlessonended'] = 'Lesson ended';
+$string['eventlessonstarted'] = 'Lesson started';
 $string['fileformat'] = 'File format';
 $string['finish'] = 'Finish';
 $string['firstanswershould'] = 'First answer should jump to the "Correct" page';
index 7011be6..35c1382 100644 (file)
@@ -1191,6 +1191,18 @@ class lesson extends lesson_base {
      */
     public function start_timer() {
         global $USER, $DB;
+
+        $cm = get_coursemodule_from_instance('lesson', $this->properties()->id, $this->properties()->course,
+            false, MUST_EXIST);
+
+        // Trigger lesson started event.
+        $event = \mod_lesson\event\lesson_started::create(array(
+            'objectid' => $this->properties()->id,
+            'context' => context_module::instance($cm->id),
+            'courseid' => $this->properties()->course
+        ));
+        $event->trigger();
+
         $USER->startlesson[$this->properties->id] = true;
         $startlesson = new stdClass;
         $startlesson->lessonid = $this->properties->id;
@@ -1243,6 +1255,18 @@ class lesson extends lesson_base {
     public function stop_timer() {
         global $USER, $DB;
         unset($USER->startlesson[$this->properties->id]);
+
+        $cm = get_coursemodule_from_instance('lesson', $this->properties()->id, $this->properties()->course,
+            false, MUST_EXIST);
+
+        // Trigger lesson ended event.
+        $event = \mod_lesson\event\lesson_ended::create(array(
+            'objectid' => $this->properties()->id,
+            'context' => context_module::instance($cm->id),
+            'courseid' => $this->properties()->course
+        ));
+        $event->trigger();
+
         return $this->update_timer(false, false);
     }
 
diff --git a/mod/lesson/tests/events_test.php b/mod/lesson/tests/events_test.php
new file mode 100644 (file)
index 0000000..55ff921
--- /dev/null
@@ -0,0 +1,197 @@
+<?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_lesson
+ * @category   test
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+
+require_once($CFG->dirroot.'/mod/lesson/locallib.php');
+
+class mod_lesson_events_testcase extends advanced_testcase {
+
+    /** @var stdClass the course used for testing */
+    private $course;
+
+    /** @var lesson the lesson used for testing */
+    private $lesson;
+
+    /**
+     * Test set up.
+     *
+     * This is executed before running any test in this file.
+     */
+    public function setUp() {
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+        $this->course = $this->getDataGenerator()->create_course();
+        $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $this->course->id));
+
+        // Convert to a lesson object.
+        $this->lesson = new lesson($lesson);
+    }
+
+    /**
+     * Test the essay attempt viewed event.
+     *
+     * There is no external API for viewing an essay attempt, so the unit test will simply
+     * create and trigger the event and ensure the legacy log data is returned as expected.
+     */
+    public function test_essay_attempt_viewed() {
+        // Create a essays list viewed event
+        $event = \mod_lesson\event\essay_attempt_viewed::create(array(
+            'objectid' => $this->lesson->id,
+            'relateduserid' => 3,
+            'context' => context_module::instance($this->lesson->properties()->cmid),
+            'courseid' => $this->course->id
+        ));
+
+        // 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\essay_attempt_viewed', $event);
+        $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
+        $expected = array($this->course->id, 'lesson', 'view grade', 'essay.php?id=' . $this->lesson->properties()->cmid .
+            '&mode=grade&attemptid=1', get_string('manualgrading', 'lesson'), $this->lesson->properties()->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+    }
+
+    /**
+     * Test the highscore added event.
+     *
+     * There is no external API for adding a highscore, so the unit test will simply create
+     * and trigger the event and ensure the legacy log data is returned as expected.
+     */
+    public function test_highscore_added() {
+        global $DB;
+
+        // Create a highscore.
+        $newhighscore = new stdClass;
+        $newhighscore->lessonid = $this->lesson->id;
+        $newhighscore->userid = 3;
+        $newhighscore->gradeid = 70;
+        $newhighscore->nickname = 'noob';
+
+        $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore);
+
+        // Create a highscore added event.
+        $event = \mod_lesson\event\highscore_added::create(array(
+            'objectid' => $newhighscore->id,
+            'context' => context_module::instance($this->lesson->properties()->cmid),
+            'courseid' => $this->course->id
+        ));
+
+        // 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\highscore_added', $event);
+        $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
+        $expected = array($this->course->id, 'lesson', 'update highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
+            'noob', $this->lesson->properties()->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+    }
+
+    /**
+     * Test the highscores viewed event.
+     *
+     * There is no external API for viewing highscores, so the unit test will simply create
+     * and trigger the event and ensure the legacy log data is returned as expected.
+     */
+    public function test_highscores_viewed() {
+        // Create a highscore viewed event.
+        $event = \mod_lesson\event\highscores_viewed::create(array(
+            'objectid' => $this->lesson->id,
+            'context' => context_module::instance($this->lesson->properties()->cmid),
+            'courseid' => $this->course->id
+        ));
+
+        // 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\highscores_viewed', $event);
+        $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
+        $expected = array($this->course->id, 'lesson', 'view highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
+            $this->lesson->properties()->name, $this->lesson->properties()->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+    }
+
+    /**
+     * Test the lesson started event.
+     */
+    public function test_lesson_started() {
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $this->lesson->start_timer();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\mod_lesson\event\lesson_started', $event);
+        $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
+        $expected = array($this->course->id, 'lesson', 'start', 'view.php?id=' . $this->lesson->properties()->cmid,
+            $this->lesson->properties()->id, $this->lesson->properties()->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+    }
+
+    /**
+     * Test the lesson ended event.
+     */
+    public function test_lesson_ended() {
+        global $DB, $USER;
+
+        // Add a lesson timer so that stop_timer() does not complain.
+        $lessontimer = new stdClass();
+        $lessontimer->lessonid = $this->lesson->properties()->id;
+        $lessontimer->userid = $USER->id;
+        $lessontimer->startime = time();
+        $lessontimer->lessontime = time();
+        $DB->insert_record('lesson_timer', $lessontimer);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $this->lesson->stop_timer();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\mod_lesson\event\lesson_ended', $event);
+        $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
+        $expected = array($this->course->id, 'lesson', 'end', 'view.php?id=' . $this->lesson->properties()->cmid,
+            $this->lesson->properties()->id, $this->lesson->properties()->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+    }
+}
index e73435f..383e9f9 100644 (file)
@@ -179,8 +179,6 @@ if (empty($pageid)) {
         }
     }
 
-    add_to_log($course->id, 'lesson', 'start', 'view.php?id='. $cm->id, $lesson->id, $cm->id);
-
     // if no pageid given see if the lesson has been started
     $retries = $DB->count_records('lesson_grades', array("lessonid" => $lesson->id, "userid" => $USER->id));
     if ($retries > 0) {
@@ -287,7 +285,13 @@ if ($pageid != LESSON_EOL) {
         $page = $lesson->load_page($newpageid);
     }
 
-    add_to_log($PAGE->course->id, 'lesson', 'view', 'view.php?id='. $PAGE->cm->id, $page->id, $PAGE->cm->id);
+    // Trigger module viewed event.
+    $event = \mod_lesson\event\course_module_viewed::create(array(
+        'objectid' => $lesson->id,
+        'context' => $context,
+        'courseid' => $course->id
+    ));
+    $event->trigger();
 
     // This is where several messages (usually warnings) are displayed
     // all of this is displayed above the actual page
@@ -417,9 +421,6 @@ if ($pageid != LESSON_EOL) {
     // Used to check to see if the student ran out of time
     $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
 
-    // Update the clock / get time information for this user
-    add_to_log($course->id, "lesson", "end", "view.php?id=".$PAGE->cm->id, "$lesson->id", $PAGE->cm->id);
-
     // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
     $lessoncontent .= $OUTPUT->heading(get_string("congratulations", "lesson"), 3);
     $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
@@ -428,6 +429,7 @@ if ($pageid != LESSON_EOL) {
         $ntries--;  // need to look at the old attempts :)
     }
     if (!$canmanage) {
+        // Update the clock / get time information for this user.
         $lesson->stop_timer();
         $gradeinfo = lesson_grade($lesson, $ntries);