From c04c41c7d2de34109edc01d24025d9e1963a3aaf Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 30 Dec 2002 05:10:01 +0000 Subject: [PATCH] Many little fixes and cleanups for robustness --- lang/en/quiz.php | 2 + mod/quiz/attempt.php | 10 +- mod/quiz/category.php | 2 +- mod/quiz/edit.php | 30 ++-- mod/quiz/lib.php | 8 +- mod/quiz/multichoice.html | 8 +- mod/quiz/question.php | 368 ++++++++++++++++++++++---------------- mod/quiz/shortanswer.html | 6 + mod/quiz/truefalse.html | 6 + 9 files changed, 264 insertions(+), 176 deletions(-) diff --git a/lang/en/quiz.php b/lang/en/quiz.php index 220250fa102..e5eb1ace21a 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -61,6 +61,8 @@ $string['guestsno'] = "Sorry, guests can not see or attempt quizzes"; $string['imagedisplay'] = "Image to display"; $string['introduction'] = "Introduction"; $string['marks'] = "Marks"; +$string['missingname'] = "Missing question name"; +$string['missingquestiontext'] = "Missing question text"; $string['multichoice'] = "Multiple Choice"; $string['noanswers'] = "No answers were selected!"; $string['noattempts'] = "No attempts have been made on this quiz"; diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 3e92c67d10a..a3831756545 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -69,14 +69,8 @@ } } - if ($course->format == "weeks" and $quiz->days) { - $timenow = time(); - $timestart = $course->startdate + (($cw->section - 1) * 608400); - $timefinish = $timestart + (3600 * 24 * $quiz->days); - $available = ($timestart < $timenow and $timenow < $timefinish); - } else { - $available = true; - } + $timenow = time(); + $available = ($quiz->timeopen < $timenow and $timenow < $quiz->timeclose); /// Check to see if they are submitting answers if (match_referer() && isset($HTTP_POST_VARS)) { diff --git a/mod/quiz/category.php b/mod/quiz/category.php index 8186c16e758..eb55e6521b3 100644 --- a/mod/quiz/category.php +++ b/mod/quiz/category.php @@ -83,7 +83,7 @@ echo "

"; echo $streditcategories; - helpbutton("categories", $strcategories, "quiz"); + helpbutton("categories", $streditcategories, "quiz"); echo "

"; /// If data submitted, then process and store. diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index dd186ed3fa9..511055b1183 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -5,10 +5,14 @@ require_login(); + if (empty($destination)) { + $destination = ""; + } + if (match_referer($destination) && isset($course) && isset($HTTP_POST_VARS)) { // form submitted from mod.html $modform = (object)$HTTP_POST_VARS; - if (!$modform->name or !$modform->intro) { + if (empty($modform->name) or empty($modform->intro)) { error(get_string("filloutallfields"), $HTTP_REFERER); } @@ -34,14 +38,14 @@ error("You can't modify this course!"); } - if (! $modform->grades) { // Construct an array to hold all the grades. + if (empty($modform->grades)) { // Construct an array to hold all the grades. $modform->grades = quiz_get_all_question_grades($modform->questions, $modform->instance); } // Now, check for commands on this page and modify variables as necessary - if ($up) { /// Move the given question up a slot + if (!empty($up)) { /// Move the given question up a slot $questions = explode(",", $modform->questions); if ($questions[0] <> $up) { foreach ($questions as $key => $question) { @@ -56,7 +60,7 @@ } } - if ($down) { /// Move the given question down a slot + if (!empty($down)) { /// Move the given question down a slot $questions = explode(",", $modform->questions); if ($questions[count($questions)-1] <> $down) { foreach ($questions as $key => $question) { @@ -71,9 +75,9 @@ } } - if ($add) { /// Add a question to the current quiz + if (!empty($add)) { /// Add a question to the current quiz $rawquestions = $HTTP_POST_VARS; - if ($modform->questions) { + if (!empty($modform->questions)) { $questions = explode(",", $modform->questions); } foreach ($rawquestions as $key => $value) { // Parse input for question ids @@ -90,14 +94,14 @@ $modform->grades[$key] = 1; // default score } } - if ($questions) { + if (!empty($questions)) { $modform->questions = implode(",", $questions); } else { $modform->questions = ""; } } - if ($delete) { /// Delete a question from the list + if (!empty($delete)) { /// Delete a question from the list $questions = explode(",", $modform->questions); foreach ($questions as $key => $question) { if ($question == $delete) { @@ -108,7 +112,7 @@ $modform->questions = implode(",", $questions); } - if ($setgrades) { /// The grades have been updated, so update our internal list + if (!empty($setgrades)) { /// The grades have been updated, so update our internal list $rawgrades = $HTTP_POST_VARS; unset($modform->grades); foreach ($rawgrades as $key => $value) { // Parse input for question -> grades @@ -119,12 +123,16 @@ } } - if ($cat) { //----------------------------------------------------------- + if (!empty($cat)) { //----------------------------------------------------------- $modform->category = $cat; } + if (empty($modform->category)) { + $modform->category = ""; + } + $modform->sumgrades = 0; - if ($modform->grades) { + if (!empty($modform->grades)) { foreach ($modform->grades as $grade) { $modform->sumgrades += $grade; } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 853ecb3018b..241d31b54e4 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -34,9 +34,9 @@ function quiz_add_instance($quiz) { $quiz->created = time(); $quiz->timemodified = time(); $quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday, - $quiz->openhour, $quiz->openminute, $quiz->opensecond); + $quiz->openhour, $quiz->openminute, 0); $quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday, - $quiz->closehour, $quiz->closeminute, $quiz->closesecond); + $quiz->closehour, $quiz->closeminute, 0); if (!$quiz->id = insert_record("quiz", $quiz)) { return false; // some error occurred @@ -68,9 +68,9 @@ function quiz_update_instance($quiz) { $quiz->timemodified = time(); $quiz->timeopen = make_timestamp($quiz->openyear, $quiz->openmonth, $quiz->openday, - $quiz->openhour, $quiz->openminute, $quiz->opensecond); + $quiz->openhour, $quiz->openminute, 0); $quiz->timeclose = make_timestamp($quiz->closeyear, $quiz->closemonth, $quiz->closeday, - $quiz->closehour, $quiz->closeminute, $quiz->closesecond); + $quiz->closehour, $quiz->closeminute, 0); $quiz->id = $quiz->instance; if (!update_record("quiz", $quiz)) { diff --git a/mod/quiz/multichoice.html b/mod/quiz/multichoice.html index 01794f7cac1..942da9df349 100644 --- a/mod/quiz/multichoice.html +++ b/mod/quiz/multichoice.html @@ -11,11 +11,17 @@

:

+

:

+ "; + } + ?> @@ -35,10 +41,10 @@

:

single", ""); + unset($menu); ?> diff --git a/mod/quiz/question.php b/mod/quiz/question.php index 75e5379c34d..1806adfa970 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -51,7 +51,7 @@ print_header("$course->shortname: $streditingquestion", "$course->shortname: $streditingquestion", "wwwroot/course/view.php?id=$course->id\">$course->shortname - -> $streditingquiz -> $streditingquestion"); + -> $streditingquiz -> $streditingquestion"); if (isset($delete)) { if (isset($confirm)) { @@ -104,156 +104,158 @@ $question->image = $form->image; $question->category = $form->category; - if ($question->id) { // Question already exists - if (!update_record("quiz_questions", $question)) { - error("Could not update question!"); + if (!$err = formcheck($question)) { + + if (!empty($question->id)) { // Question already exists + if (!update_record("quiz_questions", $question)) { + error("Could not update question!"); + } + } else { // Question is a new one + if (!$question->id = insert_record("quiz_questions", $question)) { + error("Could not insert new question!"); + } } - } else { // Question is a new one - if (!$question->id = insert_record("quiz_questions", $question)) { - error("Could not insert new question!"); - } - } - - // Now to save all the answers and type-specific options - - switch ($question->type) { - case SHORTANSWER: - // Delete all the old answers - delete_records("quiz_answers", "question", $question->id); - delete_records("quiz_shortanswer", "question", $question->id); - - $answers = array(); - $maxfraction = -1; - - // Insert all the new answers - foreach ($form->answer as $key => $formanswer) { - if ($formanswer) { - unset($answer); - $answer->answer = $formanswer; - $answer->question = $question->id; - $answer->fraction = $fraction[$key]; - $answer->feedback = $feedback[$key]; - if (!$answer->id = insert_record("quiz_answers", $answer)) { - error("Could not insert quiz answer!"); - } - $answers[] = $answer->id; - if ($fraction[$key] > $maxfraction) { - $maxfraction = $fraction[$key]; + + // Now to save all the answers and type-specific options + + switch ($question->type) { + case SHORTANSWER: + // Delete all the old answers + delete_records("quiz_answers", "question", $question->id); + delete_records("quiz_shortanswer", "question", $question->id); + + $answers = array(); + $maxfraction = -1; + + // Insert all the new answers + foreach ($form->answer as $key => $formanswer) { + if ($formanswer) { + unset($answer); + $answer->answer = $formanswer; + $answer->question = $question->id; + $answer->fraction = $fraction[$key]; + $answer->feedback = $feedback[$key]; + if (!$answer->id = insert_record("quiz_answers", $answer)) { + error("Could not insert quiz answer!"); + } + $answers[] = $answer->id; + if ($fraction[$key] > $maxfraction) { + $maxfraction = $fraction[$key]; + } } } - } - - unset($options); - $options->question = $question->id; - $options->answers = implode(",",$answers); - $options->usecase = $form->usecase; - if (!insert_record("quiz_shortanswer", $options)) { - error("Could not insert quiz shortanswer options!"); - } - - /// Perform sanity checks on fractional grades - if ($maxfraction != 1) { - $maxfraction = $maxfraction * 100; - notice_yesno(get_string("fractionsnomax", "quiz", $maxfraction), "question.php?id=$question->id", "edit.php"); - print_footer($course); - exit; - } - break; - case TRUEFALSE: - delete_records("quiz_answers", "question", $question->id); - delete_records("quiz_truefalse", "question", $question->id); - - $true->answer = get_string("true", "quiz"); - $true->question = $question->id; - $true->fraction = $form->answer; - $true->feedback = $form->feedbacktrue; - if (!$true->id = insert_record("quiz_answers", $true)) { - error("Could not insert quiz answer \"true\")!"); - } - - $false->answer = get_string("false", "quiz"); - $false->question = $question->id; - $false->fraction = 1 - (int)$form->answer; - $false->feedback = $form->feedbackfalse; - if (!$false->id = insert_record("quiz_answers", $false)) { - error("Could not insert quiz answer \"false\")!"); - } - - unset($options); - $options->question = $question->id; - $options->true = $true->id; - $options->false = $false->id; - if (!insert_record("quiz_truefalse", $options)) { - error("Could not insert quiz truefalse options!"); - } - break; - case MULTICHOICE: - delete_records("quiz_answers", "question", $question->id); - delete_records("quiz_multichoice", "question", $question->id); - - $totalfraction = 0; - $maxfraction = -1; - - $answers = array(); - - // Insert all the new answers - foreach ($form->answer as $key => $formanswer) { - if ($formanswer) { - unset($answer); - $answer->answer = $formanswer; - $answer->question = $question->id; - $answer->fraction = $fraction[$key]; - $answer->feedback = $feedback[$key]; - if (!$answer->id = insert_record("quiz_answers", $answer)) { - error("Could not insert quiz answer!"); - } - $answers[] = $answer->id; - - if ($fraction[$key] > 0) { // Sanity checks - $totalfraction += $fraction[$key]; - } - if ($fraction[$key] > $maxfraction) { - $maxfraction = $fraction[$key]; - } + + unset($options); + $options->question = $question->id; + $options->answers = implode(",",$answers); + $options->usecase = $form->usecase; + if (!insert_record("quiz_shortanswer", $options)) { + error("Could not insert quiz shortanswer options!"); } - } - - unset($options); - $options->question = $question->id; - $options->answers = implode(",",$answers); - $options->single = $form->single; - if (!insert_record("quiz_multichoice", $options)) { - error("Could not insert quiz multichoice options!"); - } - - /// Perform sanity checks on fractional grades - if ($options->single) { + + /// Perform sanity checks on fractional grades if ($maxfraction != 1) { $maxfraction = $maxfraction * 100; notice_yesno(get_string("fractionsnomax", "quiz", $maxfraction), "question.php?id=$question->id", "edit.php"); print_footer($course); exit; } - } else { - $totalfraction = round($totalfraction,2); - if ($totalfraction != 1) { - $totalfraction = $totalfraction * 100; - notice_yesno(get_string("fractionsaddwrong", "quiz", $totalfraction), "question.php?id=$question->id", "edit.php"); - print_footer($course); - exit; + break; + case TRUEFALSE: + delete_records("quiz_answers", "question", $question->id); + delete_records("quiz_truefalse", "question", $question->id); + + $true->answer = get_string("true", "quiz"); + $true->question = $question->id; + $true->fraction = $form->answer; + $true->feedback = $form->feedbacktrue; + if (!$true->id = insert_record("quiz_answers", $true)) { + error("Could not insert quiz answer \"true\")!"); } - } - break; - case RANDOM: - echo "

Not supported yet

"; - break; - default: - error("Non-existent question type!"); - break; + + $false->answer = get_string("false", "quiz"); + $false->question = $question->id; + $false->fraction = 1 - (int)$form->answer; + $false->feedback = $form->feedbackfalse; + if (!$false->id = insert_record("quiz_answers", $false)) { + error("Could not insert quiz answer \"false\")!"); + } + + unset($options); + $options->question = $question->id; + $options->true = $true->id; + $options->false = $false->id; + if (!insert_record("quiz_truefalse", $options)) { + error("Could not insert quiz truefalse options!"); + } + break; + case MULTICHOICE: + delete_records("quiz_answers", "question", $question->id); + delete_records("quiz_multichoice", "question", $question->id); + + $totalfraction = 0; + $maxfraction = -1; + + $answers = array(); + + // Insert all the new answers + foreach ($form->answer as $key => $formanswer) { + if ($formanswer) { + unset($answer); + $answer->answer = $formanswer; + $answer->question = $question->id; + $answer->fraction = $fraction[$key]; + $answer->feedback = $feedback[$key]; + if (!$answer->id = insert_record("quiz_answers", $answer)) { + error("Could not insert quiz answer!"); + } + $answers[] = $answer->id; + + if ($fraction[$key] > 0) { // Sanity checks + $totalfraction += $fraction[$key]; + } + if ($fraction[$key] > $maxfraction) { + $maxfraction = $fraction[$key]; + } + } + } + + unset($options); + $options->question = $question->id; + $options->answers = implode(",",$answers); + $options->single = $form->single; + if (!insert_record("quiz_multichoice", $options)) { + error("Could not insert quiz multichoice options!"); + } + + /// Perform sanity checks on fractional grades + if ($options->single) { + if ($maxfraction != 1) { + $maxfraction = $maxfraction * 100; + notice_yesno(get_string("fractionsnomax", "quiz", $maxfraction), "question.php?id=$question->id", "edit.php"); + print_footer($course); + exit; + } + } else { + $totalfraction = round($totalfraction,2); + if ($totalfraction != 1) { + $totalfraction = $totalfraction * 100; + notice_yesno(get_string("fractionsaddwrong", "quiz", $totalfraction), "question.php?id=$question->id", "edit.php"); + print_footer($course); + exit; + } + } + break; + case RANDOM: + echo "

Not supported yet

"; + break; + default: + error("Non-existent question type!"); + break; + } + + redirect("edit.php"); } - - redirect("edit.php"); - } $grades = array(1,0.9,0.8,0.75,0.70,0.66666,0.60,0.50,0.40,0.33333,0.30,0.25,0.20,0.10,0.05,0); @@ -284,38 +286,88 @@ // Print the question editing form + if (empty($question->id)) { + $question->id = ""; + } + if (empty($question->name)) { + $question->name = ""; + } + if (empty($question->questiontext)) { + $question->questiontext = ""; + } + if (empty($question->image)) { + $question->image = ""; + } + + switch ($type) { case SHORTANSWER: - $options = get_record("quiz_shortanswer", "question", "$question->id");// OK to fail - $answersraw = get_records_list("quiz_answers", "id", "$options->answers");// OK to fail - print_heading_with_help(get_string("editingshortanswer", "quiz"), "shortanswer", "quiz"); - if ($answersraw) { + if (!empty($question->id)) { + $options = get_record("quiz_shortanswer", "question", $question->id); + } else { + $options->usecase = 0; + } + if (!empty($options->answers)) { + $answersraw = get_records_list("quiz_answers", "id", $options->answers); + } + for ($i=0; $i<6; $i++) { + $answers[] = ""; // Make answer slots, default as blank + } + if (!empty($answersraw)) { + $i=0; foreach ($answersraw as $answer) { - $answers[] = $answer; // to renumber index 0,1,2... + $answers[$i] = $answer; // insert answers into slots + $i++; } } + print_heading_with_help(get_string("editingshortanswer", "quiz"), "shortanswer", "quiz"); require("shortanswer.html"); break; case TRUEFALSE: - $options = get_record("quiz_truefalse", "question", "$question->id"); // OK to fail - $true = get_record("quiz_answers", "id", "$options->true"); // OK to fail - $false = get_record("quiz_answers", "id", "$options->false"); // OK to fail + if (!empty($question->id)) { + $options = get_record("quiz_truefalse", "question", "$question->id"); + } + if (!empty($options->true)) { + $true = get_record("quiz_answers", "id", "$options->true"); + } else { + $true->fraction = 1; + $true->feedback = ""; + } + if (!empty($options->false)) { + $false = get_record("quiz_answers", "id", "$options->false"); + } else { + $false->fraction = 0; + $false->feedback = ""; + } + if ($true->fraction > $false->fraction) { $question->answer = 1; } else { $question->answer = 0; } + print_heading_with_help(get_string("editingtruefalse", "quiz"), "truefalse", "quiz"); require("truefalse.html"); break; case MULTICHOICE: - $options = get_record("quiz_multichoice", "question", "$question->id");// OK to fail - $answersraw = get_records_list("quiz_answers", "id", "$options->answers");// OK to fail - if ($answersraw) { + if (!empty($question->id)) { + $options = get_record("quiz_multichoice", "question", $question->id); + } else { + $options->single = ""; + } + if (!empty($options->answers)) { + $answersraw = get_records_list("quiz_answers", "id", $options->answers); + } + for ($i=0; $i<6; $i++) { + $answers[] = ""; // Make answer slots, default as blank + } + if (!empty($answersraw)) { + $i=0; foreach ($answersraw as $answer) { - $answers[] = $answer; // to renumber index 0,1,2... + $answers[$i] = $answer; // insert answers into slots + $i++; } } print_heading_with_help(get_string("editingmultichoice", "quiz"), "multichoice", "quiz"); @@ -333,4 +385,18 @@ } print_footer($course); + + +function formcheck($question) { + $err = array(); + + if (empty($question->name)) { + $err["name"] = get_string("missingname", "quiz"); + } + if (empty($question->questiontext)) { + $err["questiontext"] = get_string("missingquestiontext", "quiz"); + } + return $err; +} + ?> diff --git a/mod/quiz/shortanswer.html b/mod/quiz/shortanswer.html index 733dfe3ed14..0f1f92da094 100644 --- a/mod/quiz/shortanswer.html +++ b/mod/quiz/shortanswer.html @@ -11,11 +11,17 @@

:

+

:

+ "; + } + ?> diff --git a/mod/quiz/truefalse.html b/mod/quiz/truefalse.html index 86616b5ee6c..308f66b246c 100644 --- a/mod/quiz/truefalse.html +++ b/mod/quiz/truefalse.html @@ -11,11 +11,17 @@

:

+

:

+ "; + } + ?>