Skip to content

Commit

Permalink
Merge branch 'MDL-23273-28' of git://github.com/zbdd/moodle into MOOD…
Browse files Browse the repository at this point in the history
…LE_28_STABLE
  • Loading branch information
danpoltawski committed Mar 2, 2015
2 parents 2abfecb + c9e9862 commit 952bc6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 46 deletions.
1 change: 1 addition & 0 deletions mod/choice/lang/en/choice.php
Expand Up @@ -27,6 +27,7 @@
$string['allowupdate'] = 'Allow choice to be updated';
$string['allowmultiple'] = 'Allow more than one choice to be selected';
$string['answered'] = 'Answered';
$string['cannotsubmit'] = 'Sorry, there was a problem submitting your choice. Please try again.';
$string['completionsubmit'] = 'Show as complete when user makes a choice';
$string['displayhorizontal'] = 'Display horizontally';
$string['displaymode'] = 'Display mode for the options';
Expand Down
117 changes: 71 additions & 46 deletions mod/choice/lib.php
Expand Up @@ -236,30 +236,51 @@ function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
}

/**
* @global object
* @param int $formanswer
* @param object $choice
* @param int $userid
* @param object $course Course object
* @param object $cm
* Process user submitted answers for a choice,
* and either updating them or saving new answers.
*
* @param int $formanswer users submitted answers.
* @param object $choice the selected choice.
* @param int $userid user identifier.
* @param object $course current course.
* @param object $cm course context.
* @return void
*/
function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
global $DB, $CFG;
require_once($CFG->libdir.'/completionlib.php');

$continueurl = new moodle_url('/mod/choice/view.php', array('id' => $cm->id));

if (empty($formanswer)) {
print_error('atleastoneoption', 'choice');
print_error('atleastoneoption', 'choice', $continueurl);
}

if (is_array($formanswer)) {
if (!$choice->allowmultiple) {
print_error('multiplenotallowederror', 'choice');
print_error('multiplenotallowederror', 'choice', $continueurl);
}
$formanswers = $formanswer;
} else {
$formanswers = array($formanswer);
}

// Start lock to prevent synchronous access to the same data
// before it's updated, if using limits.
if ($choice->limitanswers) {
$timeout = 10;
$locktype = 'mod_choice_choice_user_submit_response';
// Limiting access to this choice.
$resouce = 'choiceid:' . $choice->id;
$lockfactory = \core\lock\lock_config::get_lock_factory($locktype);

// Opening the lock.
$choicelock = $lockfactory->get_lock($resouce, $timeout);
if (!$choicelock) {
print_error('cannotsubmit', 'choice', $continueurl);
}
}

$current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
$context = context_module::instance($cm->id);

Expand Down Expand Up @@ -305,7 +326,7 @@ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm
}
}
foreach ($countanswers as $opt => $count) {
if ($count > $choice->maxanswers[$opt]) {
if ($count >= $choice->maxanswers[$opt]) {
$choicesexceeded = true;
break;
}
Expand All @@ -316,7 +337,7 @@ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm
if (!($choice->limitanswers && $choicesexceeded)) {
$answersnapshots = array();
if ($current) {

// Update an existing answer.
$existingchoices = array();
foreach ($current as $c) {
if (in_array($c->optionid, $formanswers)) {
Expand All @@ -341,25 +362,10 @@ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm
}
}

$eventdata = array();
$eventdata['context'] = $context;
$eventdata['objectid'] = $choice->id;
$eventdata['userid'] = $userid;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $formanswer;

$event = \mod_choice\event\answer_updated::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
foreach ($answersnapshots as $record) {
$event->add_record_snapshot('choice_answers', $record);
}
$event->trigger();
// Initialised as true, meaning we updated the answer.
$answerupdated = true;
} else {

// Add new answer.
foreach ($formanswers as $answer) {
$newanswer = new stdClass();
$newanswer->choiceid = $choice->id;
Expand All @@ -376,30 +382,49 @@ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm
$completion->update_state($cm, COMPLETION_COMPLETE);
}

$eventdata = array();
$eventdata['context'] = $context;
$eventdata['objectid'] = $choice->id;
$eventdata['userid'] = $userid;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $formanswers;

$event = \mod_choice\event\answer_submitted::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
foreach ($answersnapshots as $record) {
$event->add_record_snapshot('choice_answers', $record);
}
$event->trigger();
// Initalised as false, meaning we submitted a new answer.
$answerupdated = false;
}
} else {
// Check to see if current choice already selected - if not display error.
$currentids = array_keys($current);

if (array_diff($currentids, $formanswers) || array_diff($formanswers, $currentids) ) {
print_error('choicefull', 'choice');
// Release lock before error.
$choicelock->release();
print_error('choicefull', 'choice', $continueurl);
}
}

// Release lock.
if (isset($choicelock)) {
$choicelock->release();
}

// Now record completed event.
if (isset($answerupdated)) {
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['objectid'] = $choice->id;
$eventdata['userid'] = $userid;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;

if ($answerupdated) {
$eventdata['other']['optionid'] = $formanswer;
$event = \mod_choice\event\answer_updated::create($eventdata);
} else {
$eventdata['other']['optionid'] = $formanswers;
$event = \mod_choice\event\answer_submitted::create($eventdata);
}
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
foreach ($answersnapshots as $record) {
$event->add_record_snapshot('choice_answers', $record);
}
$event->trigger();
}
}

Expand Down

0 comments on commit 952bc6c

Please sign in to comment.