From da82688689b8c0e0ff5771e5389dd812a6a64022 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 29 Jan 2016 16:05:54 +0800 Subject: [PATCH] MDL-45740 mod_choice: fixed remaining issues --- mod/choice/classes/event/answer_deleted.php | 20 +++++++++++----- .../classes/event/report_downloaded.php | 18 ++++++++++---- mod/choice/lib.php | 15 +++++++----- mod/choice/report.php | 2 +- mod/choice/tests/events_test.php | 5 ++-- mod/choice/view.php | 24 ++++--------------- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/mod/choice/classes/event/answer_deleted.php b/mod/choice/classes/event/answer_deleted.php index 44f66fd42f1..7b8cb18bd06 100644 --- a/mod/choice/classes/event/answer_deleted.php +++ b/mod/choice/classes/event/answer_deleted.php @@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die(); * 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 @@ class answer_deleted extends \core\event\base { * @return void */ protected function init() { + $this->data['objecttable'] = 'choice_answers'; $this->data['crud'] = 'd'; $this->data['edulevel'] = self::LEVEL_PARTICIPATING; } @@ -93,14 +94,21 @@ class answer_deleted extends \core\event\base { 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; } } diff --git a/mod/choice/classes/event/report_downloaded.php b/mod/choice/classes/event/report_downloaded.php index 0de92c8abd4..96f64c0c77f 100644 --- a/mod/choice/classes/event/report_downloaded.php +++ b/mod/choice/classes/event/report_downloaded.php @@ -31,7 +31,9 @@ defined('MOODLE_INTERNAL') || die(); * @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 @@ class report_downloaded extends \core\event\base { 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 @@ class report_downloaded extends \core\event\base { } 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; } } diff --git a/mod/choice/lib.php b/mod/choice/lib.php index c980ffb32e3..0deb0a822e7 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -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; } diff --git a/mod/choice/report.php b/mod/choice/report.php index f1215036dfd..59ac1b65770 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -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(); diff --git a/mod/choice/tests/events_test.php b/mod/choice/tests/events_test.php index 9d12b6eb6e1..d383ed47d8a 100644 --- a/mod/choice/tests/events_test.php +++ b/mod/choice/tests/events_test.php @@ -232,6 +232,7 @@ class mod_choice_events_testcase extends advanced_testcase { $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 @@ class mod_choice_events_testcase extends advanced_testcase { $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 @@ class mod_choice_events_testcase extends advanced_testcase { $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(); } diff --git a/mod/choice/view.php b/mod/choice/view.php index 923575abb59..7ced17e3a3f 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -40,26 +40,10 @@ if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 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"); } }