MDL-40919 mod_survey: replaced add_to_log with events
authorRajesh Taneja <rajesh@moodle.com>
Fri, 21 Feb 2014 02:43:34 +0000 (10:43 +0800)
committerRajesh Taneja <rajesh@moodle.com>
Mon, 24 Feb 2014 07:00:00 +0000 (15:00 +0800)
12 files changed:
mod/survey/classes/event/course_module_instance_list_viewed.php [new file with mode: 0644]
mod/survey/classes/event/course_module_viewed.php [new file with mode: 0644]
mod/survey/classes/event/report_downloaded.php [new file with mode: 0644]
mod/survey/classes/event/report_viewed.php [new file with mode: 0644]
mod/survey/classes/event/response_submitted.php [new file with mode: 0644]
mod/survey/download.php
mod/survey/index.php
mod/survey/lang/en/survey.php
mod/survey/report.php
mod/survey/save.php
mod/survey/tests/events_test.php [new file with mode: 0644]
mod/survey/view.php

diff --git a/mod/survey/classes/event/course_module_instance_list_viewed.php b/mod/survey/classes/event/course_module_instance_list_viewed.php
new file mode 100644 (file)
index 0000000..23959f0
--- /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 triggered when all survey instances for a course are viewed.
+ *
+ * @package    mod_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_survey\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/survey/classes/event/course_module_viewed.php b/mod/survey/classes/event/course_module_viewed.php
new file mode 100644 (file)
index 0000000..7289eea
--- /dev/null
@@ -0,0 +1,61 @@
+<?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_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_survey\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'] = 'survey';
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, $this->objecttable, 'view '. $this->other['viewed'], 'view.php?id=' .
+            $this->contextinstanceid, $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        if (empty($this->other['viewed'])) {
+            throw new \coding_exception('Other must contain the key viewed.');
+        }
+    }
+}
diff --git a/mod/survey/classes/event/report_downloaded.php b/mod/survey/classes/event/report_downloaded.php
new file mode 100644 (file)
index 0000000..2704862
--- /dev/null
@@ -0,0 +1,95 @@
+<?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 triggered, when survey report is downloaded.
+ *
+ * @property-read array $other Extra information about the event.
+ *     -string type: Type of report format downloaded.
+ *     -int groupid: (optional) report for groupid.
+ *
+ * @package    mod_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_survey\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class report_downloaded extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'survey';
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventreportdownloaded', 'mod_survey');
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User with id '$this->userid' downloaded survey report for survey with instance id '$this->objectid'";
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        $params = array('id' => $this->contextinstanceid, 'type' => $this->other['type']);
+        if (isset($this->other['groupid'])) {
+            $params['group'] = $this->other['groupid'];
+        }
+        return new \moodle_url("/mod/survey/download.php", $params);
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, "survey", "download", $this->get_url(), $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        if (empty($this->other['type'])) {
+            throw new \coding_exception('Other must contain the key type.');
+        }
+    }
+}
diff --git a/mod/survey/classes/event/report_viewed.php b/mod/survey/classes/event/report_viewed.php
new file mode 100644 (file)
index 0000000..a5d081a
--- /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 triggered, when survey report is viewed.
+ *
+ * @property-read array $other Extra information about the event.
+ *     -string action: (optional) report view.
+ *     -int groupid: (optional) report for groupid.
+ *
+ * @package    mod_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_survey\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class report_viewed extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'survey';
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventreportviewed', 'mod_survey');
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User with id '$this->userid' viewed survey report for survey with instance id '$this->objectid'";
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url("/mod/survey/report.php", array('id' => $this->contextinstanceid,
+            'action' => $this->other['action']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, "survey", "view report", "report.php?id=" . $this->contextinstanceid, $this->objectid,
+                     $this->contextinstanceid);
+    }
+}
diff --git a/mod/survey/classes/event/response_submitted.php b/mod/survey/classes/event/response_submitted.php
new file mode 100644 (file)
index 0000000..ccba07e
--- /dev/null
@@ -0,0 +1,91 @@
+<?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 triggered, when survey response is submitted.
+ *
+ * @property-read array $other Extra information about the event.
+ *     -int surveyid: ID of survey for which response was submitted.
+ *
+ * @package    mod_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_survey\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+class response_submitted extends \core\event\base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventresponsesubmitted', 'mod_survey');
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "User with id '". $this->userid . "' submitted response for for survey with instance id '" .
+                $this->other['surveyid'] . "'";
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url("/mod/survey/view.php", array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, "survey", "submit", "view.php?id=" . $this->contextinstanceid, $this->other['surveyid'],
+                     $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        if (empty($this->other['surveyid'])) {
+            throw new \coding_exception('Other must contain the key surveyid.');
+        }
+    }
+}
index 8d2a236..adec941 100644 (file)
@@ -51,7 +51,14 @@ if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
     print_error('invalidsurveyid', 'survey');
 }
 
-add_to_log($course->id, "survey", "download", $PAGE->url->out(), "$survey->id", $cm->id);
+$params = array(
+    'objectid' => $survey->id,
+    'context' => $context,
+    'courseid' => $course->id,
+    'other' => array('type' => $type, 'groupid' => $group)
+);
+$event = \mod_survey\event\report_downloaded::create($params);
+$event->trigger();
 
 /// Check to see if groups are being used in this survey
 
index b753659..6c17cdf 100644 (file)
     require_course_login($course);
     $PAGE->set_pagelayout('incourse');
 
-    add_to_log($course->id, "survey", "view all", "index.php?id=$course->id", "");
+    $params = array(
+        'context' => context_course::instance($course->id),
+        'courseid' => $course->id
+    );
+    $event = \mod_survey\event\course_module_instance_list_viewed::create($params);
+    $event->trigger();
 
     $strsurveys = get_string("modulenameplural", "survey");
     $strname = get_string("name");
index 505d725..40beefa 100644 (file)
@@ -197,6 +197,9 @@ $string['downloadinfo'] = 'You can download the complete raw data for this surve
 $string['downloadresults'] = 'Download results';
 $string['downloadtext'] = 'Download data as a plain text file';
 $string['editingasurvey'] = 'Editing a survey';
+$string['eventreportdownloaded'] = 'Survey report downloaded';
+$string['eventreportviewed'] = 'Survey report viewed';
+$string['eventresponsesubmitted'] = 'Survey response submitted';
 $string['guestsnotallowed'] = 'Guests are not allowed to submit surveys';
 $string['howlong'] = 'How long did this survey take you to complete?';
 $string['howlongoptions'] = 'under 1 min,1-2 min,2-3 min,3-4 min,4-5-min,5-10 min,more than 10';
index f52f8a1..c58a719 100644 (file)
@@ -92,8 +92,6 @@
     $strseemoredetail = get_string("seemoredetail", "survey");
     $strnotes = get_string("notes", "survey");
 
-    add_to_log($course->id, "survey", "view report", "report.php?id=$cm->id", "$survey->id", $cm->id);
-
     switch ($action) {
         case 'download':
             $PAGE->navbar->add(get_string('downloadresults', 'survey'));
         $currentgroup = 0;
     }
 
+    $params = array(
+        'objectid' => $survey->id,
+        'context' => $context,
+        'courseid' => $course->id,
+        'relateduserid' => $student,
+        'other' => array('action' => $action, 'groupid' => $currentgroup)
+    );
+    $event = \mod_survey\event\report_viewed::create($params);
+    $event->trigger();
+
     if ($currentgroup) {
         $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
     } else if (!empty($cm->groupingid)) {
index 736509b..aa511d3 100644 (file)
         print_error('invalidsurveyid', 'survey');
     }
 
-    add_to_log($course->id, "survey", "submit", "view.php?id=$cm->id", "$survey->id", "$cm->id");
+    $params = array(
+        'context' => $context,
+        'courseid' => $course->id,
+        'other' => array('surveyid' => $survey->id)
+    );
+    $event = \mod_survey\event\response_submitted::create($params);
+    $event->trigger();
 
     $strsurveysaved = get_string('surveysaved', 'survey');
 
diff --git a/mod/survey/tests/events_test.php b/mod/survey/tests/events_test.php
new file mode 100644 (file)
index 0000000..433b4d3
--- /dev/null
@@ -0,0 +1,143 @@
+<?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_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Events tests class.
+ *
+ * @package    mod_survey
+ * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_survey_events_testcase extends advanced_testcase {
+
+    /**
+     * Setup.
+     */
+    public function setUp() {
+        $this->resetAfterTest();
+    }
+
+    /**
+     * Test report downloaded event.
+     */
+    public function test_report_downloaded() {
+        // 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();
+        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
+
+        $params = array(
+            'objectid' => $survey->id,
+            'context' => context_module::instance($survey->cmid),
+            'courseid' => $course->id,
+            'other' => array('type' => 'xls')
+        );
+        $event = \mod_survey\event\report_downloaded::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_survey\event\report_downloaded', $event);
+        $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
+        $this->assertEquals($survey->id, $event->objectid);
+        $url = new moodle_url('/mod/survey/download.php', array('id' => $survey->cmid, 'type' => 'xls'));
+        $expected = array($course->id, "survey", "download", $url->out(), $survey->id, $survey->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Test report viewed event.
+     */
+    public function test_report_viewed() {
+        // 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();
+        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
+
+        $params = array(
+            'objectid' => $survey->id,
+            'context' => context_module::instance($survey->cmid),
+            'courseid' => $course->id
+        );
+        $event = \mod_survey\event\report_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_survey\event\report_viewed', $event);
+        $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
+        $this->assertEquals($survey->id, $event->objectid);
+        $expected = array($course->id, "survey", "view report", 'report.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Test response submitted event.
+     */
+    public function test_response_submitted() {
+        // 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();
+        $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($survey->cmid),
+            'courseid' => $course->id,
+            'other' => array('surveyid' => $survey->id)
+        );
+        $event = \mod_survey\event\response_submitted::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_survey\event\response_submitted', $event);
+        $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
+        $this->assertEquals($survey->id, $event->other['surveyid']);
+        $expected = array($course->id, "survey", "submit", 'view.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+}
index 7e24047..cc7b4bf 100644 (file)
@@ -96,8 +96,14 @@ $completion->set_module_viewed($cm);
 //  Check the survey hasn't already been filled out.
 
     if (survey_already_done($survey->id, $USER->id)) {
-
-        add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
+        $params = array(
+            'objectid' => $survey->id,
+            'context' => $context,
+            'courseid' => $course->id,
+            'other' => array('viewed' => 'graph')
+        );
+        $event = \mod_survey\event\course_module_viewed::create($params);
+        $event->trigger();
         $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 
         if ($showscales) {
@@ -134,7 +140,14 @@ $completion->set_module_viewed($cm);
     }
 
 //  Start the survey form
-    add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
+    $params = array(
+        'objectid' => $survey->id,
+        'context' => $context,
+        'courseid' => $course->id,
+        'other' => array('viewed' => 'form')
+    );
+    $event = \mod_survey\event\course_module_viewed::create($params);
+    $event->trigger();
 
     echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
     echo '<div>';