Quiz generation is working now. Just need question and category editors.

This commit is contained in:
moodler 2002-10-14 12:21:18 +00:00
parent 6a952ce7a0
commit 7bd1aa1d64
2 changed files with 84 additions and 18 deletions

View file

@ -35,6 +35,11 @@
error("You can't modify this course!"); error("You can't modify this course!");
} }
if (! $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 // Now, check for commands on this page and modify variables as necessary
if ($up) { //------------------------------------------------------------ if ($up) { //------------------------------------------------------------
@ -79,7 +84,7 @@
} }
} }
$questions[] = $key; $questions[] = $key;
$newgrade->quiz = $quiz->id; $modform->grades[$key] = 1;
} }
} }
$modform->questions = implode(",", $questions); $modform->questions = implode(",", $questions);
@ -90,9 +95,7 @@
foreach ($questions as $key => $question) { foreach ($questions as $key => $question) {
if ($question == $delete) { if ($question == $delete) {
unset($questions[$key]); unset($questions[$key]);
$db->debug=true; unset($modform->grades[$question]);
execute_sql("DELETE FROM quiz_question_grades WHERE quiz='$quiz->id' and question='$question'");
$db->debug=false;
} }
} }
$modform->questions = implode(",", $questions); $modform->questions = implode(",", $questions);
@ -103,7 +106,7 @@
foreach ($rawgrades as $key => $value) { // Parse input for question -> grades foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
if (substr($key, 0, 1) == "q") { if (substr($key, 0, 1) == "q") {
$key = substr($key,1); $key = substr($key,1);
set_field("quiz_question_grades", "grade", $value, "id", $key); $modform->grades[$key] = $value;
} }
} }
} }
@ -120,12 +123,10 @@
} }
} }
$SESSION->modform = $modform; $SESSION->modform = $modform;
save_session("SESSION"); save_session("SESSION");
$strediting = get_string("editingquiz", "quiz"); $strediting = get_string("editingquiz", "quiz");
$strname = get_string("name"); $strname = get_string("name");
@ -138,7 +139,7 @@
echo "<TR><TD WIDTH=50%>"; echo "<TR><TD WIDTH=50%>";
print_simple_box_start("CENTER", "100%", $THEME->body); print_simple_box_start("CENTER", "100%", $THEME->body);
print_heading($modform->name); print_heading($modform->name);
quiz_print_question_list($modform->questions); quiz_print_question_list($modform->questions, $modform->grades);
?> ?>
<CENTER> <CENTER>
<P>&nbsp;</P> <P>&nbsp;</P>

View file

@ -32,9 +32,23 @@ function quiz_add_instance($quiz) {
$quiz->timemodified = time(); $quiz->timemodified = time();
# May have to add extra stuff in here # if (!$quiz->id = insert_record("quiz", $quiz)) {
return false; // some error occurred
}
// The grades for each question in this quiz are stored in an array
if ($quiz->grades) {
foreach ($quiz->grades as $question => $grade) {
$questiongrade->quiz = $quiz->id;
$questiongrade->question = $question;
$questiongrade->grade = $grade;
if (!insert_record("quiz_question_grades", $questiongrade)) {
return false;
}
}
}
return insert_record("quiz", $quiz); return $quiz->id;
} }
@ -46,9 +60,37 @@ function quiz_update_instance($quiz) {
$quiz->timemodified = time(); $quiz->timemodified = time();
$quiz->id = $quiz->instance; $quiz->id = $quiz->instance;
# May have to add extra stuff in here # if (!update_record("quiz", $quiz)) {
return false; // some error occurred
}
return update_record("quiz", $quiz);
// The grades for each question in this quiz are stored in an array
// Insert or update records as appropriate
$existing = get_records("quiz_question_grades", "quiz", $quiz->id, "", "question,grade,id");
if ($quiz->grades) {
foreach ($quiz->grades as $question => $grade) {
$questiongrade->quiz = $quiz->id;
$questiongrade->question = $question;
$questiongrade->grade = $grade;
if (isset($existing[$question])) {
if ($existing[$question]->grade != $grade) {
$questiongrade->id = $existing[$question]->id;
if (!update_record("quiz_question_grades", $questiongrade)) {
return false;
}
}
} else {
if (!insert_record("quiz_question_grades", $questiongrade)) {
return false;
}
}
}
}
return true;
} }
@ -281,8 +323,32 @@ function quiz_print_category_form($course, $current) {
} }
function quiz_print_question_list($questionlist) { function quiz_get_all_question_grades($questionlist, $quizid) {
// Given a list of question IDs, finds grades or invents them to
// create an array of matching grades
$questions = get_records_sql("SELECT * FROM quiz_question_grades
WHERE quiz = '$quizid'
AND question IN ($questionlist)");
$list = explode(",", $questionlist);
$grades = array();
foreach ($list as $qid) {
if (isset($questions[$qid])) {
$grades[$qid] = $questions[$qid]->grade;
} else {
$grades[$qid] = 1;
}
}
return $grades;
}
function quiz_print_question_list($questionlist, $grades) {
// Prints a list of quiz questions in a small layout form with knobs // Prints a list of quiz questions in a small layout form with knobs
// $questionlist is comma-separated list
// $grades is an array of corresponding grades
global $THEME; global $THEME;
@ -295,8 +361,7 @@ function quiz_print_question_list($questionlist) {
$order = explode(",", $questionlist); $order = explode(",", $questionlist);
if (!$questions = get_records_sql("SELECT q.*, qg.grade FROM quiz_questions q, quiz_question_grades qg if (!$questions = get_records_list("quiz_questions", "id", $questionlist)) {
WHERE q.id in ($questionlist) and qg.question = q.id")) {
error("No questions were found!"); error("No questions were found!");
} }
@ -310,7 +375,7 @@ function quiz_print_question_list($questionlist) {
$strsavegrades = get_string("savegrades", "quiz"); $strsavegrades = get_string("savegrades", "quiz");
for ($i=100; $i>=0; $i--) { for ($i=100; $i>=0; $i--) {
$grades[$i] = $i; $gradesmenu[$i] = $i;
} }
$count = 0; $count = 0;
$sumgrade = 0; $sumgrade = 0;
@ -336,7 +401,7 @@ function quiz_print_question_list($questionlist) {
echo "</TD>"; echo "</TD>";
echo "<TD>".$questions[$qnum]->name."</TD>"; echo "<TD>".$questions[$qnum]->name."</TD>";
echo "<TD>"; echo "<TD>";
choose_from_menu($grades, "q$qnum", $questions[$qnum]->grade, ""); choose_from_menu($gradesmenu, "q$qnum", $grades[$qnum], "");
echo "<TD>"; echo "<TD>";
echo "<A TITLE=\"$strdelete\" HREF=\"edit.php?delete=$qnum\"><IMG echo "<A TITLE=\"$strdelete\" HREF=\"edit.php?delete=$qnum\"><IMG
SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;"; SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
@ -344,7 +409,7 @@ function quiz_print_question_list($questionlist) {
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>"; SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
echo "</TD>"; echo "</TD>";
$sumgrade += $questions[$qnum]->grade; $sumgrade += $grades[$qnum];
} }
echo "<TR><TD COLSPAN=3><TD ALIGN=right>"; echo "<TR><TD COLSPAN=3><TD ALIGN=right>";
echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">"; echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">";