Skip to content

Commit

Permalink
MDL-45740 choice: add new events
Browse files Browse the repository at this point in the history
  • Loading branch information
sbourget authored and mdjnelson committed Jan 29, 2016
1 parent eddec36 commit e62f6a0
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 2 deletions.
106 changes: 106 additions & 0 deletions mod/choice/classes/event/answer_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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 mod_choice answer deleted event.
*
* @package mod_choice
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_choice\event;

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

/**
* The mod_choice answer updated event class.
*
* @property-read array $other {
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: (optional) id of option.
* }
*
* @package mod_choice
* @since Moodle 3.1
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_deleted extends \core\event\base {

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has deleted responses from
the choice activity with course module id '$this->contextinstanceid'.";
}

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

/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
}

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

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
}

public static function get_objectid_mapping() {
return false;
}

public static function get_other_mapping() {
// No need to map the 'content' value.
return false;
}
}
106 changes: 106 additions & 0 deletions mod/choice/classes/event/report_downloaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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 mod_choice report viewed event.
*
* @package mod_choice
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();

/**
* The mod_choice report viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - string content: (optional) The content we are viewing.
* }
*
* @package mod_choice
* @since Moodle 3.1
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_downloaded extends \core\event\base {

/**
* Init method.
*/
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', 'mod_choice');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has downloaded the report in the '".$this->other['format']."' format for
the choice activity with course module id '$this->contextinstanceid'";
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/report.php', array('id' => $this->contextinstanceid));
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();

// Report format downloaded.
if (!isset($this->other['format'])) {
throw new \coding_exception('The \'format\' value must be set in other.');
}
// ID of the choice activity.
if (!isset($this->other['choice'])) {
throw new \coding_exception('The \'choice\' value must be set in other.');
}
}

public static function get_objectid_mapping() {
return false;
}

public static function get_other_mapping() {
// No need to map the 'content' value.
return false;
}
}
2 changes: 2 additions & 0 deletions mod/choice/lang/en/choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
$string['displaymode'] = 'Display mode for the options';
$string['displayvertical'] = 'Display vertically';
$string['eventanswercreated'] = 'Choice made';
$string['eventanswerdeleted'] = 'Choice answer deleted';
$string['eventanswerupdated'] = 'Choice updated';
$string['eventreportdownloaded'] = 'Choice report downloaded';
$string['eventreportviewed'] = 'Choice report viewed';
$string['expired'] = 'Sorry, this activity closed on {$a} and is no longer available';
$string['atleastoneoption'] = 'You need to provide at least one possible answer.';
Expand Down
19 changes: 18 additions & 1 deletion mod/choice/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
* @return bool
*/
function choice_delete_responses($attemptids, $choice, $cm, $course) {
global $DB, $CFG;
global $DB, $CFG, $USER;
require_once($CFG->libdir.'/completionlib.php');

if(!is_array($attemptids) || empty($attemptids)) {
Expand All @@ -526,10 +526,27 @@ function choice_delete_responses($attemptids, $choice, $cm, $course) {
}
}

$context = context_module::instance($cm->id);
$completion = new completion_info($course);
foreach($attemptids as $attemptid) {
if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) {

// Trigger the event answer deleted.
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['userid'] = $USER->id;
$eventdata['courseid'] = $course->id;
$eventdata['relateduserid'] = $todelete->userid;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$event = \mod_choice\event\answer_deleted::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
$event->trigger();

$DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid));

// Update completion state
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
Expand Down
11 changes: 11 additions & 0 deletions mod/choice/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@
}
} else {
$groupmode = groups_get_activity_groupmode($cm);

// Trigger the report downloaded event.
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
$eventdata['other']['format'] = $download;
$eventdata['other']['choice'] = $choice->id;
$event = \mod_choice\event\report_downloaded::create($eventdata);
$event->trigger();

}

// Check if we want to include responses from inactive users.
Expand Down
72 changes: 72 additions & 0 deletions mod/choice/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@ public function test_answer_updated_other_exception() {
$this->assertEventContextNotUsed($event);
}

/**
* Test to ensure that event data is being stored correctly.
*/
public function test_answer_deleted() {
global $DB, $USER;
// Generate user data.
$user = $this->getDataGenerator()->create_user();

$optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));

// Create the first answer.
choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
// Get the users response.
$answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id),
'*', $strictness = IGNORE_MULTIPLE);

// Redirect event.
$sink = $this->redirectEvents();
// Now delete the answer.
choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course);

// Get our event event.
$events = $sink->get_events();
$event = reset($events);

// Data checking.
$this->assertInstanceOf('\mod_choice\event\answer_deleted', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context());
$this->assertEquals($this->choice->id, $event->other['choiceid']);
$this->assertEventContextNotUsed($event);
$sink->close();
}

/**
* Test to ensure that event data is being stored correctly.
*/
Expand Down Expand Up @@ -238,6 +273,43 @@ public function test_report_viewed() {
$sink->close();
}

/**
* Test to ensure that event data is being stored correctly.
*/
public function test_report_downloaded() {
global $USER;

$this->resetAfterTest();

// Generate user data.
$this->setAdminUser();

$eventdata = array();
$eventdata['context'] = $this->context;
$eventdata['courseid'] = $this->course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
$eventdata['other']['format'] = 'csv';
$eventdata['other']['choice'] = $this->choice->id;

// This is fired in a page view so we can't run this through a function.
$event = \mod_choice\event\report_downloaded::create($eventdata);

// Redirect event.
$sink = $this->redirectEvents();
$event->trigger();
$event = $sink->get_events();

// Data checking.
$this->assertCount(1, $event);
$this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]);
$this->assertEquals($USER->id, $event[0]->userid);
$this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
$this->assertEquals('csv', $event[0]->other['format']);
$this->assertEquals($this->choice->id, $event[0]->other['choice']);
$this->assertEventContextNotUsed($event[0]);
$sink->close();
}

/**
* Test to ensure that event data is being stored correctly.
*/
Expand Down
2 changes: 1 addition & 1 deletion mod/choice/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

$plugin->version = 2015111600; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016010300; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2015111000; // Requires this Moodle version
$plugin->component = 'mod_choice'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;
13 changes: 13 additions & 0 deletions mod/choice/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
and $choiceavailable) {
$answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
if ($answercount > 0) {

// Trigger the answer_deleted event.
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['userid'] = $USER->id;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$event = \mod_choice\event\answer_deleted::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
$event->trigger();
$DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));

// Update completion state
Expand Down

0 comments on commit e62f6a0

Please sign in to comment.