From a25f9cc3570935aacfbae247827ee42279d069a1 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Wed, 16 Oct 2019 15:56:19 +0800 Subject: [PATCH] MDL-66447 forumreport_summary: Added event logging --- .../classes/event/report_downloaded.php | 91 ++++++++++++ .../summary/classes/event/report_viewed.php | 93 ++++++++++++ mod/forum/report/summary/index.php | 11 ++ .../summary/lang/en/forumreport_summary.php | 2 + .../report/summary/tests/events_test.php | 134 ++++++++++++++++++ 5 files changed, 331 insertions(+) create mode 100644 mod/forum/report/summary/classes/event/report_downloaded.php create mode 100644 mod/forum/report/summary/classes/event/report_viewed.php create mode 100644 mod/forum/report/summary/tests/events_test.php diff --git a/mod/forum/report/summary/classes/event/report_downloaded.php b/mod/forum/report/summary/classes/event/report_downloaded.php new file mode 100644 index 00000000000..cc9533686c8 --- /dev/null +++ b/mod/forum/report/summary/classes/event/report_downloaded.php @@ -0,0 +1,91 @@ +. + +/** + * The forum summary report downloaded event. + * + * @package forumreport_summary + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace forumreport_summary\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * The forum summary report downloaded event class. + * + * @package forumreport_summary + * @since Moodle 3.8 + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class report_downloaded extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventreportdownloaded', 'forumreport_summary'); + } + + /** + * Returns non-localised event description with ids for admin use only. + * + * @return string + */ + public function get_description() { + if ($this->other['hasviewall']) { + return "The user with id '{$this->userid}' downloaded the summary report for the forum with " . + "course module id '{$this->contextinstanceid}'."; + } else { + return "The user with id '{$this->userid}' downloaded their own summary report for the forum with " . + "course module id '{$this->contextinstanceid}'."; + } + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/forum/report/summary/index.php', + ['courseid' => $this->courseid, 'forumid' => $this->other['forumid']]); + } + + /** + * Custom validation. + * + * @throws \coding_exception + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['hasviewall'])) { + throw new \coding_exception('The \'hasviewall\' value must be set'); + } + } +} diff --git a/mod/forum/report/summary/classes/event/report_viewed.php b/mod/forum/report/summary/classes/event/report_viewed.php new file mode 100644 index 00000000000..5f313d18a5b --- /dev/null +++ b/mod/forum/report/summary/classes/event/report_viewed.php @@ -0,0 +1,93 @@ +. + +/** + * The forum summary report viewed event. + * + * @package forumreport_summary + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace forumreport_summary\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * The forum summary report viewed event class. + * + * @package forumreport_summary + * @since Moodle 3.8 + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class report_viewed extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventreportviewed', 'forumreport_summary'); + } + + /** + * Returns non-localised event description with ids for admin use only. + * + * @return string + */ + public function get_description() { + if ($this->other['hasviewall']) { + return "The user with id '{$this->userid}' viewed the summary report for the forum with " . + "course module id '{$this->contextinstanceid}'."; + + } else { + return "The user with id '{$this->userid}' viewed their own summary report for the forum with " . + "course module id '{$this->contextinstanceid}'."; + } + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/forum/report/summary/index.php', + ['courseid' => $this->courseid, 'forumid' => $this->other['forumid']]); + } + + /** + * Custom validation. + * + * @throws \coding_exception + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['hasviewall'])) { + throw new \coding_exception('The \'hasviewall\' value must be set'); + } + } +} diff --git a/mod/forum/report/summary/index.php b/mod/forum/report/summary/index.php index 8ef6e2330b7..274adcbe83d 100644 --- a/mod/forum/report/summary/index.php +++ b/mod/forum/report/summary/index.php @@ -86,9 +86,20 @@ $table = new \forumreport_summary\summary_table($courseid, $filters, $allowbulko $canseeprivatereplies, $perpage, $canexport); $table->baseurl = $url; +$eventparams = [ + 'context' => $context, + 'other' => [ + 'forumid' => $forumid, + 'hasviewall' => has_capability('forumreport/summary:viewall', $context), + ], +]; + if ($download) { + \forumreport_summary\event\report_downloaded::create($eventparams)->trigger(); $table->download($download); } else { + \forumreport_summary\event\report_viewed::create($eventparams)->trigger(); + echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('summarytitle', 'forumreport_summary', $forumname), 2, 'p-b-2'); diff --git a/mod/forum/report/summary/lang/en/forumreport_summary.php b/mod/forum/report/summary/lang/en/forumreport_summary.php index 4d1813d24b8..71d51be0fa7 100644 --- a/mod/forum/report/summary/lang/en/forumreport_summary.php +++ b/mod/forum/report/summary/lang/en/forumreport_summary.php @@ -26,6 +26,8 @@ $string['attachmentcount'] = 'Number of attachments'; $string['charcount'] = 'Character count'; $string['viewcount'] = 'Number of views'; $string['earliestpost'] = 'Earliest post'; +$string['eventreportdownloaded'] = 'Forum summary report downloaded'; +$string['eventreportviewed'] = 'Forum summary report viewed'; $string['filter:datesbuttonlabel'] = 'Open the dates filter'; $string['filter:datesname'] = 'Dates'; $string['filter:datesfrom'] = 'From {$a}'; diff --git a/mod/forum/report/summary/tests/events_test.php b/mod/forum/report/summary/tests/events_test.php new file mode 100644 index 00000000000..4ee3ab99083 --- /dev/null +++ b/mod/forum/report/summary/tests/events_test.php @@ -0,0 +1,134 @@ +. + +/** + * Tests for forum report summary events. + * + * @package forumreport_summary + * @category test + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tests for forum report summary events. + * + * @package forumreport_summary + * @category test + * @copyright 2019 Michael Hawkins + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class forumreport_summary_events_testcase extends advanced_testcase { + /** + * Test report_downloaded event. + */ + public function test_report_downloaded() { + global $DB; + + $this->resetAfterTest(); + + // Create course and teacher user. + $course = $this->getDataGenerator()->create_course(); + $teacher = $this->getDataGenerator()->create_user(); + $roleteacher = $DB->get_record('role', ['shortname' => 'teacher']); + $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id); + + // Create forum. + $this->setUser($teacher); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + $context = context_module::instance($forum->cmid); + + // Trigger and capture event. + $eventparams = [ + 'context' => $context, + 'other' => [ + 'forumid' => $forum->id, + 'hasviewall' => true, + ], + ]; + $event = \forumreport_summary\event\report_downloaded::create($eventparams); + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = reset($events); + $sink->close(); + + // Check the event contains the expected data. + $this->assertInstanceOf('\forumreport_summary\event\report_downloaded', $event); + $this->assertEquals($context, $event->get_context()); + $this->assertEquals(CONTEXT_MODULE, $event->contextlevel); + $this->assertEquals($forum->cmid, $event->contextinstanceid); + $this->assertEquals($teacher->id, $event->userid); + $url = new moodle_url('/mod/forum/report/summary/index.php', + ['courseid' => $course->id, 'forumid' => $forum->id]); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + $this->assertNotEmpty($event->get_description()); + } + + /** + * Test report_viewed event. + */ + public function test_report_viewed() { + global $DB; + + $this->resetAfterTest(); + + // Create course and teacher user. + $course = $this->getDataGenerator()->create_course(); + $teacher = $this->getDataGenerator()->create_user(); + $roleteacher = $DB->get_record('role', ['shortname' => 'teacher']); + $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id); + + // Create forum. + $this->setUser($teacher); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + $context = context_module::instance($forum->cmid); + + // Trigger and capture event. + $eventparams = [ + 'context' => $context, + 'other' => [ + 'forumid' => $forum->id, + 'hasviewall' => true, + ], + ]; + $event = \forumreport_summary\event\report_viewed::create($eventparams); + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = reset($events); + $sink->close(); + + // Check the event contains the expected data. + $this->assertInstanceOf('\forumreport_summary\event\report_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $this->assertEquals(CONTEXT_MODULE, $event->contextlevel); + $this->assertEquals($forum->cmid, $event->contextinstanceid); + $this->assertEquals($teacher->id, $event->userid); + $url = new moodle_url('/mod/forum/report/summary/index.php', + ['courseid' => $course->id, 'forumid' => $forum->id]); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + $this->assertNotEmpty($event->get_description()); + } +}