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 index 00000000000..23959f04abb --- /dev/null +++ b/mod/survey/classes/event/course_module_instance_list_viewed.php @@ -0,0 +1,31 @@ +. + +/** + * Event triggered when all survey instances for a course are viewed. + * + * @package mod_survey + * @copyright 2014 Rajesh Taneja + * @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 index 00000000000..7289eea3273 --- /dev/null +++ b/mod/survey/classes/event/course_module_viewed.php @@ -0,0 +1,61 @@ +. + +/** + * Course module viewed event. + * + * @package mod_survey + * @copyright 2014 Rajesh Taneja + * @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 index 00000000000..2704862f39a --- /dev/null +++ b/mod/survey/classes/event/report_downloaded.php @@ -0,0 +1,95 @@ +. + +/** + * 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 + * @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 index 00000000000..a5d081a0251 --- /dev/null +++ b/mod/survey/classes/event/report_viewed.php @@ -0,0 +1,81 @@ +. + +/** + * 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 + * @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 index 00000000000..ccba07ee67b --- /dev/null +++ b/mod/survey/classes/event/response_submitted.php @@ -0,0 +1,91 @@ +. + +/** + *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 + * @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.'); + } + } +} diff --git a/mod/survey/download.php b/mod/survey/download.php index 8d2a23613ff..adec941b8b2 100644 --- a/mod/survey/download.php +++ b/mod/survey/download.php @@ -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 diff --git a/mod/survey/index.php b/mod/survey/index.php index b7536598023..6c17cdf2cf9 100644 --- a/mod/survey/index.php +++ b/mod/survey/index.php @@ -14,7 +14,12 @@ 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"); diff --git a/mod/survey/lang/en/survey.php b/mod/survey/lang/en/survey.php index 505d72514b3..40beefa6fab 100644 --- a/mod/survey/lang/en/survey.php +++ b/mod/survey/lang/en/survey.php @@ -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'; diff --git a/mod/survey/report.php b/mod/survey/report.php index f52f8a1aa14..c58a719433e 100644 --- a/mod/survey/report.php +++ b/mod/survey/report.php @@ -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')); @@ -131,6 +129,16 @@ $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)) { diff --git a/mod/survey/save.php b/mod/survey/save.php index 736509bee63..aa511d3336a 100644 --- a/mod/survey/save.php +++ b/mod/survey/save.php @@ -54,7 +54,13 @@ 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 index 00000000000..433b4d3f715 --- /dev/null +++ b/mod/survey/tests/events_test.php @@ -0,0 +1,143 @@ +. + +/** + * Events tests. + * + * @package mod_survey + * @copyright 2014 Rajesh Taneja + * @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 + * @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); + } +} diff --git a/mod/survey/view.php b/mod/survey/view.php index 7e24047d1fa..cc7b4bf57db 100644 --- a/mod/survey/view.php +++ b/mod/survey/view.php @@ -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 "
"; echo '
';