Can now create and re-edit all three types of questions.

Still working on grading ...
This commit is contained in:
moodler 2002-10-15 10:04:28 +00:00
parent 2834cce0d9
commit 2a2c9725bb
6 changed files with 225 additions and 61 deletions

View File

@ -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

View File

@ -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 "<P>$question->question</P>";
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 "<P>$question->question</P>";
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 "<P>$question->question</P>";
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 "<INPUT TYPE=hidden NAME=q VALUE=\"$quiz->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 "<BR>";
}
@ -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);
}

View File

@ -16,23 +16,24 @@
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="intro" rows=5 cols=50 wrap="virtual"><? p($question->question) ?></textarea>
<textarea name="questiontext" rows=5 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("imageaddress", "quiz") ?>:</B></P></TD>
<TD align=right><P><B><? print_string("imagedisplay", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="image" size=80 value="<? p($question->image) ?>">
<? choose_from_menu($images, "image", "$question->image", get_string("none"),"",""); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("answerhowmany", "quiz") ?>:</B></P></TD>
<TD>
<?
$options[0] = get_string("answersingleno", "quiz");
$options[1] = get_string("answersingleyes", "quiz");
choose_from_menu($options, "single", "$options->single", "");
unset($menu);
$menu[0] = get_string("answersingleno", "quiz");
$menu[1] = get_string("answersingleyes", "quiz");
choose_from_menu($menu, "single", "$options->single", "");
?>
</TD>
</TR>
@ -50,14 +51,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[0]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[0]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[0]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[0]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[0]->feedback) ?></textarea>
</TD>
</TR>
@ -71,14 +72,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[1]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[1]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[1]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[1]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[1]->feedback) ?></textarea>
</TD>
</TR>
@ -92,14 +93,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[2]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[2]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[2]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[2]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[2]->feedback) ?></textarea>
</TD>
</TR>
@ -113,14 +114,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[3]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[3]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[3]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[3]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[3]->feedback) ?></textarea>
</TD>
</TR>
@ -134,14 +135,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[4]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[4]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[4]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[4]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[4]->feedback) ?></textarea>
</TD>
</TR>
@ -155,14 +156,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[5]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[5]->fraction", ""); ?>
choose_from_menu($gradeoptionsfull, "fraction[]", $answers[5]->fraction, ""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[5]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[5]->feedback) ?></textarea>
</TD>
</TR>
</TABLE>

View File

@ -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) {

View File

@ -16,23 +16,24 @@
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="intro" rows=6 cols=50 wrap="virtual"><? p($question->question) ?></textarea>
<textarea name="questiontext" rows=6 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("imageaddress", "quiz") ?>:</B></P></TD>
<TD align=right><P><B><? print_string("imagedisplay", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="image" size=80 value="<? p($question->image) ?>">
<? choose_from_menu($images, "image", "$question->image", get_string("none"),"",""); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("casesensitive", "quiz") ?>:</B></P></TD>
<TD>
<?
$options[0] = get_string("caseno", "quiz");
$options[1] = get_string("caseyes", "quiz");
choose_from_menu($options, "case", "$options->case", "");
unset($menu);
$menu[0] = get_string("caseno", "quiz");
$menu[1] = get_string("caseyes", "quiz");
choose_from_menu($menu, "usecase", "$options->usecase", "");
?>
</TD>
</TR>
@ -49,14 +50,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[0]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[0]->fraction", ""); ?>
choose_from_menu($gradeoptions, "fraction[]", $answers[0]->fraction,""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[0]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[0]->feedback) ?></textarea>
</TD>
</TR>
@ -70,14 +71,14 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[1]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[1]->fraction", ""); ?>
choose_from_menu($gradeoptions, "fraction[]", $answers[1]->fraction,""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[1]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[1]->feedback) ?></textarea>
</TD>
</TR>
@ -91,16 +92,59 @@
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[2]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", "$answers[2]->fraction", ""); ?>
choose_from_menu($gradeoptions, "fraction[]", $answers[2]->fraction,""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback" rows=2 cols=50 wrap="virtual"><? p($answers[2]->feedback) ?></textarea>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[2]->feedback) ?></textarea>
</TD>
</TR>
<TR valign=top>
<TD colspan=2>&nbsp;</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("answer", "quiz") ?> 4:</B></P></TD>
<TD>
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[3]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", $answers[3]->fraction,""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[3]->feedback) ?></textarea>
</TD>
</TR>
<TR valign=top>
<TD colspan=2>&nbsp;</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("answer", "quiz") ?> 5:</B></P></TD>
<TD>
<INPUT type="text" name="answer[]" size=50 value="<? p($answers[4]->answer) ?>">&nbsp;&nbsp;
<? print_string("grade", "quiz");
echo ":&nbsp;";
choose_from_menu($gradeoptions, "fraction[]", $answers[4]->fraction,""); ?>
<BR>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("feedback", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="feedback[]" rows=2 cols=50 wrap="virtual"><? p($answers[4]->feedback) ?></textarea>
</TD>
</TR>
</TABLE>
<INPUT type="hidden" name=id value="<? p($question->id) ?>">

View File

@ -16,14 +16,14 @@
<TR valign=top>
<TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>
<TD>
<textarea name="intro" rows=6 cols=50 wrap="virtual"><? p($question->question) ?></textarea>
<textarea name="questiontext" rows=6 cols=50 wrap="virtual"><? p($question->questiontext)?></textarea>
<? helpbutton("text", get_string("helptext")); ?>
</TD>
</TR>
<TR valign=top>
<TD align=right><P><B><? print_string("imageaddress", "quiz") ?>:</B></P></TD>
<TD align=right><P><B><? print_string("imagedisplay", "quiz") ?>:</B></P></TD>
<TD>
<INPUT type="text" name="image" size=80 value="<? p($question->image) ?>">
<? choose_from_menu($images, "image", "$question->image", get_string("none"),"",""); ?>
</TD>
</TR>