Skip to content

Commit

Permalink
Merge branch 'MDL-41123-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Apr 9, 2014
2 parents 43968c9 + e5f4487 commit 3c72b71
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 2 deletions.
88 changes: 88 additions & 0 deletions report/completion/classes/event/report_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 for when completion report is viewed.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal<ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace report_completion\event;

defined('MOODLE_INTERNAL') || die();

/**
* Event triggered, when completion report is viewed.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal<ankit.agrr@gmail.com>
* @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;
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreportviewed', 'report_completion');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id " . $this->userid . " viewed completion report for course with id " . $this->courseid;
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/completion/index.php', array('course' => $this->courseid));
}

/**
* custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
parent::validate_data();
if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context passed must be course context.');
}
}
}

98 changes: 98 additions & 0 deletions report/completion/classes/event/user_report_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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 for when user log report is viewed.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal<ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace report_completion\event;

defined('MOODLE_INTERNAL') || die();

/**
* Event triggered, when user log report is viewed.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_report_viewed extends \core\event\base {

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventuserreportviewed', 'report_completion');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return 'The user with id ' . $this->userid . ' viewed user log report for user with id ' . $this->relateduserid;
}

/**
* Return the legacy event log data.
*
* @return array
*/
protected function get_legacy_logdata() {
$url = 'report/completion/user.php?id=' . $this->relateduserid . '&course=' . $this->courseid;
return array($this->courseid, 'course', 'report completion', $url, $this->courseid);
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/completion/user.php', array('course' => $this->courseid, 'id' => $this->relateduserid));
}

/**
* custom validations.
*
* @throws \coding_exception when validation fails.
* @return void
*/
protected function validate_data() {
parent::validate_data();
if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context passed must be course context.');
}
}
}

4 changes: 4 additions & 0 deletions report/completion/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,7 @@
print '</ul>';

echo $OUTPUT->footer($course);

// Trigger a report viewed event.
$event = \report_completion\event\report_viewed::create(array('context' => $context));
$event->trigger();
2 changes: 2 additions & 0 deletions report/completion/lang/en/report_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
$string['page-report-completion-index'] = 'Course completion report';
$string['page-report-completion-user'] = 'User course completion report';
$string['pluginname'] = 'Course completion';
$string['eventreportviewed'] = 'Completion report viewed';
$string['eventuserreportviewed'] = 'Completion user report viewed';
99 changes: 99 additions & 0 deletions report/completion/tests/events_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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 report completion events.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal<ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/

defined('MOODLE_INTERNAL') || die();

/**
* Class report_completion_events_testcase
*
* Class for tests related to completion report events.
*
* @package report_completion
* @copyright 2014 onwards Ankit Agarwal<ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class report_completion_events_testcase extends advanced_testcase {

/**
* Setup testcase.
*/
public function setUp() {
$this->setAdminUser();
$this->resetAfterTest();
}

/**
* Test the report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of log report, so here we
* simply create the event and trigger it.
*/
public function test_report_viewed() {
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
// Trigger event for completion report viewed.
$event = \report_completion\event\report_viewed::create(array('context' => $context));

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);

$this->assertInstanceOf('\report_completion\event\report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/report/completion/index.php', array('course' => $course->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}

/**
* Test the user report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of log report, so here we
* simply create the event and trigger it.
*/
public function test_user_report_viewed() {
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);
// Trigger event for completion report viewed.
$event = \report_completion\event\user_report_viewed::create(array('context' => $context, 'relateduserid' => 3));

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);

$this->assertInstanceOf('\report_completion\event\user_report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEquals(3, $event->relateduserid);
$this->assertEquals(new moodle_url('/report/completion/user.php', array('id' => 3, 'course' => $course->id)),
$event->get_url());
$expected = array($course->id, 'course', 'report completion', "report/completion/user.php?id=3&course=$course->id",
$course->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
5 changes: 3 additions & 2 deletions report/completion/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
error('Can not access user completion report');
}

add_to_log($course->id, 'course', 'report completion', "report/completion/user.php?id=$user->id&course=$course->id", $course->id);

$stractivityreport = get_string('activityreport');

$PAGE->set_pagelayout('admin');
Expand Down Expand Up @@ -301,3 +299,6 @@


echo $OUTPUT->footer();
// Trigger a user report viewed event.
$event = \report_completion\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $userid));
$event->trigger();

0 comments on commit 3c72b71

Please sign in to comment.