]> git.mjollnir.org Git - moodle.git/commitdiff
Quiz generation is working now. Just need question and category editors.
authormoodler <moodler>
Mon, 14 Oct 2002 12:21:18 +0000 (12:21 +0000)
committermoodler <moodler>
Mon, 14 Oct 2002 12:21:18 +0000 (12:21 +0000)
mod/quiz/edit.php
mod/quiz/lib.php

index d66424e944444e7f4f6ef3c457d27704e9f14b46..8cc5c179de3c5695e097fadb5081cc04c2fbc4e1 100644 (file)
         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);
         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;
             }
         }
     }
         }
     }
 
-
     $SESSION->modform = $modform;
     save_session("SESSION");
 
 
-
     $strediting = get_string("editingquiz", "quiz");
     $strname    = get_string("name");
 
     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>
index 6c5fec8684c55e89a77ae31fd14798e826e5c112..0523ef5aff5678e884e4d2df9dc83427ca8cb144 100644 (file)
@@ -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,7 +401,7 @@ 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;";
@@ -344,7 +409,7 @@ function quiz_print_question_list($questionlist) {
                  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:\">";