Skip to content

Commit

Permalink
MDL-51569 mod_choice: Validate the submitted optionid
Browse files Browse the repository at this point in the history
Make sure any submitted choice options actually belong to the current choice
module.
  • Loading branch information
Damyon Wiese authored and danpoltawski committed Nov 5, 2015
1 parent 2759c12 commit 0bca129
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mod/choice/lib.php
Expand Up @@ -269,6 +269,12 @@ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm
$formanswers = array($formanswer);
}

$options = $DB->get_records('choice_options', array('choiceid' => $choice->id), '', 'id');
foreach ($formanswers as $key => $val) {
if (!isset($options[$val])) {
print_error('cannotsubmit', 'choice', $continueurl);
}
}
// Start lock to prevent synchronous access to the same data
// before it's updated, if using limits.
if ($choice->limitanswers) {
Expand Down
22 changes: 22 additions & 0 deletions mod/choice/tests/lib_test.php
Expand Up @@ -131,6 +131,28 @@ public function test_choice_can_view_results() {

}

public function test_choice_user_submit_response_validation() {
global $USER;

$this->resetAfterTest();

$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$choice1 = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
$choice2 = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
$cm = get_coursemodule_from_instance('choice', $choice1->id);

$choicewithoptions1 = choice_get_choice($choice1->id);
$choicewithoptions2 = choice_get_choice($choice2->id);
$optionids1 = array_keys($choicewithoptions1->option);
$optionids2 = array_keys($choicewithoptions2->option);

// Make sure we cannot submit options from a different choice instance.
$this->setExpectedException('moodle_exception');
choice_user_submit_response($optionids2[0], $choice1, $USER->id, $course, $cm);
}

/**
* Test choice_get_my_response
* @return void
Expand Down

0 comments on commit 0bca129

Please sign in to comment.