MDL-51569 mod_choice: Check choice availability prior to do any action

This commit is contained in:
Juan Leyva
2015-11-04 11:08:42 +00:00
committed by Dan Poltawski
parent 77e072ebec
commit 7ca8c34045
2 changed files with 35 additions and 1 deletions
+26
View File
@@ -921,3 +921,29 @@ function choice_print_overview($courses, &$htmlarray) {
}
return;
}
/**
* Check if a choice is available for the current user.
*
* @param stdClass $choice choice record
* @return array status (available or not and possible warnings)
*/
function choice_get_availability_status($choice) {
$available = true;
$warnings = array();
if ($choice->timeclose != 0) {
$timenow = time();
if ($choice->timeopen > $timenow) {
$available = false;
$warnings['notopenyet'] = userdate($choice->timeopen);
} else if ($timenow > $choice->timeclose) {
$available = false;
$warnings['expired'] = userdate($choice->timeclose);
}
}
// Choice is available.
return array($available, $warnings);
}
+9 -1
View File
@@ -34,7 +34,10 @@ $strchoices = get_string('modulenameplural', 'choice');
$context = context_module::instance($cm->id);
if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
list($choiceavailable, $warnings) = choice_get_availability_status($choice);
if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
and $choiceavailable) {
$answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
if ($answercount > 0) {
$DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
@@ -71,6 +74,11 @@ if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && conf
$answer = optional_param('answer', '', PARAM_INT);
}
if (!$choiceavailable) {
$reason = current(array_keys($warnings));
throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
}
if ($answer) {
choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
redirect(new moodle_url('/mod/choice/view.php',