MDL-45740 mod_choice: fixed remaining issues

This commit is contained in:
Mark Nelson 2016-01-29 16:05:54 +08:00
parent e62f6a01e4
commit da82688689
6 changed files with 44 additions and 40 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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();

View File

@ -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();
}

View File

@ -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");
}
}