diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 3219641a941..9ccfa50ad6d 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -90,7 +90,11 @@ $modform->grades[$key] = 1; // default score } } - $modform->questions = implode(",", $questions); + if ($questions) { + $modform->questions = implode(",", $questions); + } else { + $modform->questions = ""; + } } if ($delete) { /// Delete a question from the list diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 7643bba22d9..09c304bdb19 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -195,7 +195,7 @@ function quiz_print_question($number, $questionid, $grade, $courseid) { if (!$options = get_record("quiz_shortanswer", "question", $question->id)) { notify("Error: Missing question options!"); } - echo "

$question->question

"; + echo text_to_html($question->questiontext); if ($question->image) { print_file_picture($question->image, $courseid, 200); } @@ -218,7 +218,7 @@ function quiz_print_question($number, $questionid, $grade, $courseid) { if (!$false->answer) { $false->answer = get_string("false", "quiz"); } - echo "

$question->question

"; + echo text_to_html($question->questiontext); if ($question->image) { print_file_picture($question->image, $courseid, 200); } @@ -235,7 +235,7 @@ function quiz_print_question($number, $questionid, $grade, $courseid) { if (!$answers = get_records_list("quiz_answers", "id", $options->answers)) { notify("Error: Missing question answers!"); } - echo "

$question->question

"; + echo text_to_html($question->questiontext); if ($question->image) { print_file_picture($question->image, $courseid, 200); } @@ -286,7 +286,7 @@ function quiz_print_quiz_questions($quiz, $results=NULL) { echo "id\">"; foreach ($questions as $key => $questionid) { print_simple_box_start("CENTER", "90%"); - quiz_print_question($key+1, $questionid, $grades[$questionid]->grade, $course->id); + quiz_print_question($key+1, $questionid, $grades[$questionid]->grade, $quiz->course); print_simple_box_end(); echo "
"; } @@ -577,11 +577,11 @@ function quiz_save_best_grade($quiz, $user) { } -function quiz_get_answer($question) { +function quiz_get_answers($question) { // Given a question, returns the correct answers and grades switch ($question->type) { case SHORTANSWER; // Could be multiple answers - return get_records_sql("SELECT a.*, sa.case, g.grade + return get_records_sql("SELECT a.*, sa.usecase, g.grade FROM quiz_shortanswer sa, quiz_answers a, quiz_question_grades g WHERE sa.question = '$question->id' AND sa.question = a.question @@ -702,9 +702,12 @@ function quiz_grade_attempt_results($quiz, $questions) { $result->sumgrades = 0; + global $db; + $db->debug=true; + foreach ($questions as $question) { - if (!$answers = quiz_get_answer($question)) { - error("No answer defined for question id $question->id!"); + if (!$answers = quiz_get_answers($question)) { + error("No answers defined for question id $question->id!"); } $grade = 0; // default @@ -719,7 +722,7 @@ function quiz_grade_attempt_results($quiz, $questions) { } foreach($answers as $answer) { // There might be multiple right answers $feedback[$answer->id] = $answer->feedback; - if (!$answer->case) { // Don't compare case + if (!$answer->usecase) { // Don't compare case $answer->answer = strtolower($answer->answer); $question->answer = strtolower($question->answer); } diff --git a/mod/quiz/multichoice.html b/mod/quiz/multichoice.html index 21dc044348b..f0ff1885fa1 100644 --- a/mod/quiz/multichoice.html +++ b/mod/quiz/multichoice.html @@ -16,23 +16,24 @@

:

- + -

:

+

:

- + image", get_string("none"),"",""); ?>

:

single", ""); + unset($menu); + $menu[0] = get_string("answersingleno", "quiz"); + $menu[1] = get_string("answersingleyes", "quiz"); + choose_from_menu($menu, "single", "$options->single", ""); ?> @@ -50,14 +51,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[0]->fraction, ""); ?>

:

- + @@ -71,14 +72,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[1]->fraction, ""); ?>

:

- + @@ -92,14 +93,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[2]->fraction, ""); ?>

:

- + @@ -113,14 +114,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[3]->fraction, ""); ?>

:

- + @@ -134,14 +135,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[4]->fraction, ""); ?>

:

- + @@ -155,14 +156,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptionsfull, "fraction[]", $answers[5]->fraction, ""); ?>

:

- + diff --git a/mod/quiz/question.php b/mod/quiz/question.php index 92ca6dc19f2..2448dd37bd9 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -3,6 +3,7 @@ require("../../config.php"); require("lib.php"); + require("../../files/mimetypes.php"); optional_variable($id); @@ -23,20 +24,6 @@ $type = $question->type; - switch ($type) { - case SHORTANSWER: - print_heading(get_string("editingshortanswer", "quiz")); - require("shortanswer.html"); - break; - case TRUEFALSE: - print_heading(get_string("editingtruefalse", "quiz")); - require("truefalse.html"); - break; - case MULTICHOICE: - print_heading(get_string("editingmultichoice", "quiz")); - require("multichoice.html"); - break; - } } else if ($category) { if (! $category = get_record("quiz_categories", "id", $category)) { @@ -68,16 +55,133 @@ if (match_referer() and isset($HTTP_POST_VARS)) { // question submitted + $form = (object)$HTTP_POST_VARS; + + // First, save the basic question itself + $question->name = $form->name; + $question->questiontext = $form->questiontext; + $question->image = $form->image; + + if ($question->id) { // Question already exists + if (!update_record("quiz_questions", $question)) { + error("Could not update question!"); + } + } else { // Question is a new one + $db->debug =true; + 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(); + + // 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; + } + } + + 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!"); + } + 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 = "1.0"; + $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 = "0"; + $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); + + $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; + } + } + + 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!"); + } + break; + default: + error("Non-existent question type!"); + break; + } + redirect("edit.php"); } - $grades = array(100,90,80,75,70,66.66,60,50,40,33.33,30,25,20,10,5); + $grades = array(1,0.9,0.8,0.75,0.70,0.6666,0.60,0.50,0.40,0.3333,0.30,0.25,0.20,0.10,0.05,0); foreach ($grades as $grade) { - $gradeoptions[$grade] = "$grade %"; - $gradeoptionsfull[$grade] = "$grade %"; - $gradeoptionsfull[-$grade] = -$grade." %"; + $percentage = 100 * $grade; + $neggrade = -$grade; + $gradeoptions["$grade"] = "$percentage %"; + $gradeoptionsfull["$grade"] = "$percentage %"; + $gradeoptionsfull["$neggrade"] = -$percentage." %"; } + $gradeoptionsfull["0"] = $gradeoptions["0"] = get_string("none"); + arsort($gradeoptions, SORT_NUMERIC); arsort($gradeoptionsfull, SORT_NUMERIC); @@ -87,6 +191,14 @@ error("No categories!"); } + + $coursefiles = get_directory_list("$CFG->dataroot/$course->id", $CFG->moddata); + foreach ($coursefiles as $filename) { + if (mimeinfo("icon", $filename) == "image.gif") { + $images["$filename"] = $filename; + } + } + // Print the question editing form switch ($type) { diff --git a/mod/quiz/shortanswer.html b/mod/quiz/shortanswer.html index e604dba67e5..8b93a20a38b 100644 --- a/mod/quiz/shortanswer.html +++ b/mod/quiz/shortanswer.html @@ -16,23 +16,24 @@

:

- + -

:

+

:

- + image", get_string("none"),"",""); ?>

:

case", ""); + unset($menu); + $menu[0] = get_string("caseno", "quiz"); + $menu[1] = get_string("caseyes", "quiz"); + choose_from_menu($menu, "usecase", "$options->usecase", ""); ?> @@ -49,14 +50,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptions, "fraction[]", $answers[0]->fraction,""); ?>

:

- + @@ -70,14 +71,14 @@    fraction", ""); ?> + choose_from_menu($gradeoptions, "fraction[]", $answers[1]->fraction,""); ?>

:

- + @@ -91,16 +92,59 @@    fraction", ""); ?> + choose_from_menu($gradeoptions, "fraction[]", $answers[2]->fraction,""); ?>

:

- + + + +   + + + +

4:

+ +    + fraction,""); ?> +
+ + + +

:

+ + + + + + +   + + + +

5:

+ +    + fraction,""); ?> +
+ + + +

:

+ + + + + diff --git a/mod/quiz/truefalse.html b/mod/quiz/truefalse.html index ebf30c4be41..7ec480a697e 100644 --- a/mod/quiz/truefalse.html +++ b/mod/quiz/truefalse.html @@ -16,14 +16,14 @@

:

- + -

:

+

:

- + image", get_string("none"),"",""); ?>