mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-66447 forumreport_summary: Added event logging
- Loading branch information
1 parent
f9db589
commit a25f9cc
Showing
5 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
mod/forum/report/summary/classes/event/report_downloaded.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |