This commit is contained in:
Damyon Wiese 2014-02-25 12:44:35 +08:00
commit 03b8d9e3c3
12 changed files with 552 additions and 8 deletions

View file

@ -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.
}

View file

@ -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.');
}
}
}

View file

@ -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.');
}
}
}

View file

@ -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);
}
}

View file

@ -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.');
}
}
}

View file

@ -51,7 +51,14 @@ if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
print_error('invalidsurveyid', 'survey'); 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 /// Check to see if groups are being used in this survey

View file

@ -14,7 +14,12 @@
require_course_login($course); require_course_login($course);
$PAGE->set_pagelayout('incourse'); $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"); $strsurveys = get_string("modulenameplural", "survey");
$strname = get_string("name"); $strname = get_string("name");

View file

@ -197,6 +197,9 @@ $string['downloadinfo'] = 'You can download the complete raw data for this surve
$string['downloadresults'] = 'Download results'; $string['downloadresults'] = 'Download results';
$string['downloadtext'] = 'Download data as a plain text file'; $string['downloadtext'] = 'Download data as a plain text file';
$string['editingasurvey'] = 'Editing a survey'; $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['guestsnotallowed'] = 'Guests are not allowed to submit surveys';
$string['howlong'] = 'How long did this survey take you to complete?'; $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'; $string['howlongoptions'] = 'under 1 min,1-2 min,2-3 min,3-4 min,4-5-min,5-10 min,more than 10';

View file

@ -92,8 +92,6 @@
$strseemoredetail = get_string("seemoredetail", "survey"); $strseemoredetail = get_string("seemoredetail", "survey");
$strnotes = get_string("notes", "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) { switch ($action) {
case 'download': case 'download':
$PAGE->navbar->add(get_string('downloadresults', 'survey')); $PAGE->navbar->add(get_string('downloadresults', 'survey'));
@ -131,6 +129,16 @@
$currentgroup = 0; $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) { if ($currentgroup) {
$users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false); $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
} else if (!empty($cm->groupingid)) { } else if (!empty($cm->groupingid)) {

View file

@ -54,7 +54,13 @@
print_error('invalidsurveyid', 'survey'); 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'); $strsurveysaved = get_string('surveysaved', 'survey');

View file

@ -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);
}
}

View file

@ -96,8 +96,14 @@ $completion->set_module_viewed($cm);
// Check the survey hasn't already been filled out. // Check the survey hasn't already been filled out.
if (survey_already_done($survey->id, $USER->id)) { if (survey_already_done($survey->id, $USER->id)) {
$params = array(
add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id); '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); $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
if ($showscales) { if ($showscales) {
@ -134,7 +140,14 @@ $completion->set_module_viewed($cm);
} }
// Start the survey form // 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 "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
echo '<div>'; echo '<div>';