Skip to content

Commit

Permalink
MDL-66447 forumreport_summary: Added event logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhawkins committed Nov 11, 2019
1 parent f9db589 commit a25f9cc
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 0 deletions.
91 changes: 91 additions & 0 deletions mod/forum/report/summary/classes/event/report_downloaded.php
@@ -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/>.

/**
* The forum summary report downloaded event.
*
* @package forumreport_summary
* @copyright 2019 Michael Hawkins <michaelh@moodle.com>
* @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 <michaelh@moodle.com>
* @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');
}
}
}
93 changes: 93 additions & 0 deletions mod/forum/report/summary/classes/event/report_viewed.php
@@ -0,0 +1,93 @@
<?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/>.

/**
* The forum summary report viewed event.
*
* @package forumreport_summary
* @copyright 2019 Michael Hawkins <michaelh@moodle.com>
* @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 <michaelh@moodle.com>
* @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');
}
}
}
11 changes: 11 additions & 0 deletions mod/forum/report/summary/index.php
Expand Up @@ -86,9 +86,20 @@
$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');

Expand Down
2 changes: 2 additions & 0 deletions mod/forum/report/summary/lang/en/forumreport_summary.php
Expand Up @@ -26,6 +26,8 @@
$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}';
Expand Down
134 changes: 134 additions & 0 deletions mod/forum/report/summary/tests/events_test.php
@@ -0,0 +1,134 @@
<?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/>.

/**
* Tests for forum report summary events.
*
* @package forumreport_summary
* @category test
* @copyright 2019 Michael Hawkins <michaelh@moodle.com>
* @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 <michaelh@moodle.com>
* @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());
}
}

0 comments on commit a25f9cc

Please sign in to comment.