diff --git a/report/questioninstances/classes/event/report_viewed.php b/report/questioninstances/classes/event/report_viewed.php new file mode 100644 index 00000000000..2d4e40aaceb --- /dev/null +++ b/report/questioninstances/classes/event/report_viewed.php @@ -0,0 +1,100 @@ +. + +/** + * Event for when capability report is viewed. + * + * @package report_questioninstances + * @copyright 2014 Petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace report_questioninstances\event; + +/** + * Event triggered, when capability report is viewed. + * + * @property-read array $other Extra information about the event. + * -string requestedqtype: Requested report type. + * + * @package report_questioninstances + * @since Moodle 2.7 + * @copyright 2014 Petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class report_viewed extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->context = \context_system::instance(); + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventreportviewed', 'report_questioninstances'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id " . $this->userid . " viewed question instances report"; + } + + /** + * Return the legacy event log data. + * + * @return array + */ + protected function get_legacy_logdata() { + $requestedqtype = $this->other['requestedqtype']; + return array(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype); + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/report/questioninstances/index.php', array('qtype' => $this->other['requestedqtype'])); + } + + /** + * Custom validation. + * + * @throws \coding_exception + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->data['other']['requestedqtype'])) { + throw new \coding_exception('requestedqtype must be set in other array.'); + } + } +} + diff --git a/report/questioninstances/index.php b/report/questioninstances/index.php index 904cdb9ea42..dbcea346b41 100644 --- a/report/questioninstances/index.php +++ b/report/questioninstances/index.php @@ -35,7 +35,7 @@ admin_externalpage_setup('reportquestioninstances', '', null, '', array('pagelay echo $OUTPUT->header(); // Log. -add_to_log(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype); +\report_questioninstances\event\report_viewed::create(array('other' => array('requestedqtype' => $requestedqtype)))->trigger(); // Prepare the list of capabilities to choose from $qtypes = question_bank::get_all_qtypes(); diff --git a/report/questioninstances/lang/en/report_questioninstances.php b/report/questioninstances/lang/en/report_questioninstances.php index 2f823df23b2..d66beb92c08 100644 --- a/report/questioninstances/lang/en/report_questioninstances.php +++ b/report/questioninstances/lang/en/report_questioninstances.php @@ -24,6 +24,7 @@ */ $string['editquestionshere'] = 'Edit questions in this context'; +$string['eventreportviewed'] = 'Report viewed'; $string['getreport'] = 'Get the report'; $string['hiddenquestions'] = 'Hidden'; $string['intro'] = 'This report lists all the contexts in the system where there are questions of a particular type.'; diff --git a/report/questioninstances/tests/events_test.php b/report/questioninstances/tests/events_test.php new file mode 100644 index 00000000000..f002176c5b0 --- /dev/null +++ b/report/questioninstances/tests/events_test.php @@ -0,0 +1,66 @@ +. + +/** + * Tests for question instances events. + * + * @package report_questioninstances + * @copyright 2014 Petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class for question instances events. + * + * @package report_questioninstances + * @copyright 2014 Petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class report_questioninstances_events_testcase extends advanced_testcase { + + /** + * Setup testcase. + */ + public function setUp() { + $this->setAdminUser(); + $this->resetAfterTest(); + } + + /** + * Test the report viewed event. + */ + public function test_report_viewed() { + $requestedqtype = 'all'; + $event = \report_questioninstances\event\report_viewed::create(array('other' => array('requestedqtype' => $requestedqtype))); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + $this->assertInstanceOf('\report_questioninstances\event\report_viewed', $event); + $this->assertEquals(context_system::instance(), $event->get_context()); + $expected = array(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype); + $this->assertEventLegacyLogData($expected, $event); + $this->assertEventContextNotUsed($event); + $url = new moodle_url('/report/questioninstances/index.php', array('qtype' => $requestedqtype)); + $this->assertEquals($url, $event->get_url()); + $event->get_name(); + } +}