From: moodler Date: Wed, 4 Feb 2004 13:40:20 +0000 (+0000) Subject: Caters for the case when nothing was selected X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=bd115b3f8219dab177511965718e2ad0e7b37cd4;p=moodle.git Caters for the case when nothing was selected --- diff --git a/lang/en/choice.php b/lang/en/choice.php index 9937c5ca23..662a22815c 100644 --- a/lang/en/choice.php +++ b/lang/en/choice.php @@ -8,6 +8,7 @@ $string['choicename'] = 'Choice name'; $string['choicetext'] = 'Choice text'; $string['modulename'] = 'Choice'; $string['modulenameplural'] = 'Choices'; +$string['mustchooseone'] = 'You must choose an answer before saving. Nothing was saved.'; $string['notanswered'] = 'Not answered yet'; $string['publish'] = 'Publish results'; $string['publishanonymous'] = 'Publish anonymous results, do not show student names'; diff --git a/mod/choice/view.php b/mod/choice/view.php index 9bc612b923..d6ec67249e 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -20,31 +20,37 @@ } for ($i=1; $i <= $CHOICE_MAX_NUMBER; $i++) { - $answerchecked[$i] = ""; + $answerchecked[$i] = ''; } if ($current = get_record("choice_answers", "choice", $choice->id, "userid", $USER->id)) { - $answerchecked[$current->answer] = "CHECKED"; + $answerchecked[$current->answer] = 'CHECKED'; } if ($form = data_submitted()) { $timenow = time(); - if ($current) { - $newanswer = $current; - $newanswer->answer = $form->answer; - $newanswer->timemodified = $timenow; - if (! update_record("choice_answers", $newanswer)) { - error("Could not update your choice"); - } - add_to_log($course->id, "choice", "update", "view.php?id=$cm->id", $choice->id, $cm->id); + + if (empty($form->answer)) { + redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice')); + } else { - $newanswer->choice = $choice->id; - $newanswer->userid = $USER->id; - $newanswer->answer = $form->answer; - $newanswer->timemodified = $timenow; - if (! insert_record("choice_answers", $newanswer)) { - error("Could not save your choice"); + if ($current) { + $newanswer = $current; + $newanswer->answer = $form->answer; + $newanswer->timemodified = $timenow; + if (! update_record("choice_answers", $newanswer)) { + error("Could not update your choice"); + } + add_to_log($course->id, "choice", "update", "view.php?id=$cm->id", $choice->id, $cm->id); + } else { + $newanswer->choice = $choice->id; + $newanswer->userid = $USER->id; + $newanswer->answer = $form->answer; + $newanswer->timemodified = $timenow; + if (! insert_record("choice_answers", $newanswer)) { + error("Could not save your choice"); + } + add_to_log($course->id, "choice", "add", "view.php?id=$cm->id", $choice->id, $cm->id); } - add_to_log($course->id, "choice", "add", "view.php?id=$cm->id", $choice->id, $cm->id); } redirect("$CFG->wwwroot/course/view.php?id=$course->id"); exit;