diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 8d620763081..17c2505437e 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -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); +} diff --git a/mod/choice/view.php b/mod/choice/view.php index 500db553788..bbfef618e4d 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -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',