Permalink
Browse files

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

  • Loading branch information...
1 parent 6a952ce commit 7bd1aa1d648e801bd21fb5fa3030ef9c902f33dd moodler committed Oct 14, 2002
Showing with 84 additions and 18 deletions.
  1. +9 −8 mod/quiz/edit.php
  2. +75 −10 mod/quiz/lib.php
View
@@ -35,6 +35,11 @@
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
if ($up) { //------------------------------------------------------------
@@ -79,7 +84,7 @@
}
}
$questions[] = $key;
- $newgrade->quiz = $quiz->id;
+ $modform->grades[$key] = 1;
}
}
$modform->questions = implode(",", $questions);
@@ -90,9 +95,7 @@
foreach ($questions as $key => $question) {
if ($question == $delete) {
unset($questions[$key]);
- $db->debug=true;
- execute_sql("DELETE FROM quiz_question_grades WHERE quiz='$quiz->id' and question='$question'");
- $db->debug=false;
+ unset($modform->grades[$question]);
}
}
$modform->questions = implode(",", $questions);
@@ -103,7 +106,7 @@
foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
if (substr($key, 0, 1) == "q") {
$key = substr($key,1);
- set_field("quiz_question_grades", "grade", $value, "id", $key);
+ $modform->grades[$key] = $value;
}
}
}
@@ -120,12 +123,10 @@
}
}
-
$SESSION->modform = $modform;
save_session("SESSION");
-
$strediting = get_string("editingquiz", "quiz");
$strname = get_string("name");
@@ -138,7 +139,7 @@
echo "<TR><TD WIDTH=50%>";
print_simple_box_start("CENTER", "100%", $THEME->body);
print_heading($modform->name);
- quiz_print_question_list($modform->questions);
+ quiz_print_question_list($modform->questions, $modform->grades);
?>
<CENTER>
<P>&nbsp;</P>
View
@@ -32,9 +32,23 @@ function quiz_add_instance($quiz) {
$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->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
+// $questionlist is comma-separated list
+// $grades is an array of corresponding grades
global $THEME;
@@ -295,8 +361,7 @@ function quiz_print_question_list($questionlist) {
$order = explode(",", $questionlist);
- if (!$questions = get_records_sql("SELECT q.*, qg.grade FROM quiz_questions q, quiz_question_grades qg
- WHERE q.id in ($questionlist) and qg.question = q.id")) {
+ if (!$questions = get_records_list("quiz_questions", "id", $questionlist)) {
error("No questions were found!");
}
@@ -310,7 +375,7 @@ function quiz_print_question_list($questionlist) {
$strsavegrades = get_string("savegrades", "quiz");
for ($i=100; $i>=0; $i--) {
- $grades[$i] = $i;
+ $gradesmenu[$i] = $i;
}
$count = 0;
$sumgrade = 0;
@@ -336,15 +401,15 @@ function quiz_print_question_list($questionlist) {
echo "</TD>";
echo "<TD>".$questions[$qnum]->name."</TD>";
echo "<TD>";
- choose_from_menu($grades, "q$qnum", $questions[$qnum]->grade, "");
+ choose_from_menu($gradesmenu, "q$qnum", $grades[$qnum], "");
echo "<TD>";
echo "<A TITLE=\"$strdelete\" HREF=\"edit.php?delete=$qnum\"><IMG
SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$qnum\"><IMG
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
echo "</TD>";
- $sumgrade += $questions[$qnum]->grade;
+ $sumgrade += $grades[$qnum];
}
echo "<TR><TD COLSPAN=3><TD ALIGN=right>";
echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">";

0 comments on commit 7bd1aa1

Please sign in to comment.