Skip to content
Permalink
Browse files

MDL-45740 mod_choice: fixed remaining issues

  • Loading branch information...
markn86 committed Jan 29, 2016
1 parent e62f6a0 commit da82688689b8c0e0ff5771e5389dd812a6a64022
@@ -33,7 +33,7 @@
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: (optional) id of option.
* - int optionid: id of the option.
* }
*
* @package mod_choice
@@ -49,8 +49,8 @@ class answer_deleted extends \core\event\base {
* @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 "The user with id '$this->userid' has deleted the option with id '" . $this->other['optionid'] . "' for the
user with id '$this->relateduserid' from the choice activity with course module id '$this->contextinstanceid'.";
}
/**
@@ -77,6 +77,7 @@ public function get_url() {
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'choice_answers';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
@@ -93,14 +94,21 @@ protected function validate_data() {
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
if (!isset($this->other['optionid'])) {
throw new \coding_exception('The \'optionid\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return false;
return array('db' => 'choice_answers', 'restore' => \core\event\base::NOT_MAPPED);
}
public static function get_other_mapping() {
// No need to map the 'content' value.
return false;
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
$othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
return $othermapped;
}
}
@@ -31,7 +31,9 @@
* @property-read array $other {
* Extra information about the event.
*
* - string content: (optional) The content we are viewing.
* - string content: The content we are viewing.
* - string format: The report format
* - int choiced: The id of the choice
* }
*
* @package mod_choice
@@ -85,13 +87,17 @@ public function get_url() {
protected function validate_data() {
parent::validate_data();
// Report format downloaded.
if (!isset($this->other['content'])) {
throw new \coding_exception('The \'content\' value must be set in other.');
}
// 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.');
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
}
@@ -100,7 +106,9 @@ public static function get_objectid_mapping() {
}
public static function get_other_mapping() {
// No need to map the 'content' value.
return false;
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
return $othermapped;
}
}
@@ -530,29 +530,32 @@ function choice_delete_responses($attemptids, $choice, $cm, $course) {
$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['objectid'] = $todelete->id;
$eventdata['context'] = $context;
$eventdata['userid'] = $USER->id;
$eventdata['courseid'] = $course->id;
$eventdata['relateduserid'] = $todelete->userid;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $todelete->optionid;
$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->add_record_snapshot('choice_answers', $todelete);
$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);
}
}
}
// Update completion state.
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE);
}
return true;
}
@@ -78,7 +78,7 @@
$eventdata['courseid'] = $course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
$eventdata['other']['format'] = $download;
$eventdata['other']['choice'] = $choice->id;
$eventdata['other']['choiceid'] = $choice->id;
$event = \mod_choice\event\report_downloaded::create($eventdata);
$event->trigger();
@@ -232,6 +232,7 @@ public function test_answer_deleted() {
$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->assertEquals($answer->optionid, $event->other['optionid']);
$this->assertEventContextNotUsed($event);
$sink->close();
}
@@ -289,7 +290,7 @@ public function test_report_downloaded() {
$eventdata['courseid'] = $this->course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
$eventdata['other']['format'] = 'csv';
$eventdata['other']['choice'] = $this->choice->id;
$eventdata['other']['choiceid'] = $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);
@@ -305,7 +306,7 @@ public function test_report_downloaded() {
$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->assertEquals($this->choice->id, $event[0]->other['choiceid']);
$this->assertEventContextNotUsed($event[0]);
$sink->close();
}
@@ -40,26 +40,10 @@
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
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE);
}
$choiceanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id),
'', 'id');
$todelete = array_keys($choiceanswers);
choice_delete_responses($todelete, $choice, $cm, $course);
redirect("view.php?id=$cm->id");
}
}

0 comments on commit da82688

Please sign in to comment.
You can’t perform that action at this time.