]> git.mjollnir.org Git - moodle.git/commitdiff
quiz edit.php now saves changes to the quiz in the database as soon as the teacher...
authorgustav_delius <gustav_delius>
Sat, 22 Jan 2005 15:42:22 +0000 (15:42 +0000)
committergustav_delius <gustav_delius>
Sat, 22 Jan 2005 15:42:22 +0000 (15:42 +0000)
mod/quiz/edit.php
mod/quiz/lib.php
mod/quiz/locallib.php

index 596f9a5be7a4b19fc176c593a4e701c1381e2cc6..3bee558dc0eafc688418b6afaafd93d4c82cac02 100644 (file)
         if (! $modform = get_record('quiz', 'id', $quizid)) {
             error("The required quiz doesn't exist");
         }
-        
         $modform->instance = $modform->id;
-        
         $SESSION->modform = $modform;    // Save the form in the current session
+            
+        $cm = get_coursemodule_from_instance('quiz', $modform->instance);
+        add_to_log($cm->course, 'quiz', 'editquestions', 
+                           "view.php?id=$modform->instance", 
+                           "$modform->name", $cm->id); 
 
     } else if ($courseid) { // Page retrieve through "Edit Questions" link - no quiz selected
         $modform->course = $courseid;
         unset($modform->instance);
-
         $SESSION->modform = $modform;    // Save the form in the current session
-
+        
+        add_to_log($courseid, 'quiz', 'editquestions', "index.php?id=$courseid");
+        
     } else {
         if (!isset($SESSION->modform)) {
           // We currently also get here after editing a question by
@@ -52,6 +56,8 @@
             error('');
         }
 
+        // The data is obtained from a $SESSION variable. This is mostly for historic reasons. 
+        // With the way things work now it would be just as possible to get the data from the database.
         $modform = $SESSION->modform;
     }
 
             }
             $modform->questions = implode(",", $questions);
         }
+        if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
+            error('Could not save question list');
+        }
     }
 
     if (!empty($down)) { /// Move the given question down a slot
             }
             $modform->questions = implode(",", $questions);
         }
+        if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
+            error('Could not save question list');
+        }
     }
 
     if (!empty($add)) { /// Add a question to the current quiz
         } else {
             $modform->questions = "";
         }
+        if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
+            error('Could not save question list');
+        }
+        quiz_questiongrades_update($modform->grades, $modform->instance);
     }
 
     if (!empty($delete)) { /// Delete a question from the list 
             if ($question == $delete) {
                 unset($questions[$key]);
                 unset($modform->grades[$question]);
+                if (!delete_records('quiz_question_grades', 'quiz', $modform->instance, 'question', $question)) {
+                    error("Could not delete question grade");
+                }
             }
         }
         $modform->questions = implode(",", $questions);
+        if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
+            error('Could not save question list');
+        }
     }
 
     if (!empty($setgrades)) { /// The grades have been updated, so update our internal list
                 $modform->grades[$key] = $value;
             }
         }
-    }
-    
-    if (!empty($save)) {  // Save the list of questions and grades in the database and return
-    
-        //If caller is correct, $SESSION->sesskey must exist and coincide
-        if (empty($SESSION->sesskey) or !confirm_sesskey($SESSION->sesskey)) {
-            error(get_string('confirmsesskeybad', 'error'));
+        if (!set_field('quiz', 'questions', $modform->questions, 'id', $modform->instance)) {
+            error('Could not save question list');
         }
-        //Unset this, check done
-        unset($SESSION->sesskey);
-        
-        quiz_update_instance($modform);
-        $coursemodule = get_coursemodule_from_instance('quiz', $modform->instance);
-        add_to_log($course->id, 'quiz', 'editquestions', 
-                           "view.php?id=$coursemodule", 
-                           "$modform->instance", $coursemodule); 
-        redirect('view.php?q='.$modform->instance);
-        die;
+        quiz_questiongrades_update($modform->grades, $modform->instance);
     }
  
 
 
     $strname    = get_string('name');
     $strquizzes = get_string('modulenameplural', 'quiz');
-    $strediting = get_string(isset($modform->instance) ? "editingquiz" : "editquestions", "quiz");
+    $strediting = get_string('editquestions', "quiz");
     $strheading = empty($modform->name) ? $strediting : $modform->name;
 
     // Print basic page layout.
         print_simple_box_start("center", "100%", $THEME->cellcontent2);        
         print_heading($modform->name);
         quiz_print_question_list($modform->questions, $modform->grades); 
-        ?>
-        <center>
-        <p>&nbsp;</p>
-        <?php
 
         if ($attemptcount = count_records_select("quiz_attempts", "quiz = '$modform->instance' AND timefinish > 0"))  {
             $strviewallanswers  = get_string("viewallanswers","quiz",$attemptcount);
             }
             notify("$strattemptsexist<br /><a href=\"report.php?id=$cm->id\">$strviewallanswers ($usercount $strusers)</a>");
         }
-        
-        $SESSION->sesskey = !empty($USER->id) ? $USER->sesskey : '';
-        ?>
-        <form method="post" action="edit.php">
-        <input type="hidden" name="sesskey" value="save" />
-        <input type="submit" name="save" value="<?php  print_string("savequiz", "quiz") ?>" />
-        <input type="submit" name="cancel" value="<?php  print_string("cancel") ?>" />
-        </form>
-        </center>
-        <?php
-
 
         print_simple_box_end();
+        print_continue('view.php?q='.$modform->instance);
         echo '</td><td valign="top" width="50%">';
     }
     print_simple_box_start("center", "100%", $THEME->cellcontent2);
index ecb53d03c6382cf2cc999a8b5b5def9a4a3cc68b..290f10cdf1436e645bd09dc584c42f6d48db4dac 100644 (file)
@@ -33,23 +33,6 @@ function quiz_add_instance($quiz) {
         set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
     }
 
-    // The grades for every question in this quiz are stored in an array
-    // (because this is currently only called from mod.html there are not
-    // going to be any grades, but we will leave this code here just in case)
-    if (isset($quiz->grades)) {
-        foreach ($quiz->grades as $question => $grade) {
-            if ($question) {
-                unset($questiongrade);
-                $questiongrade->quiz = $quiz->id;
-                $questiongrade->question = $question;
-                $questiongrade->grade = $grade;
-                if (!insert_record("quiz_question_grades", $questiongrade)) {
-                    return false;
-                }
-            }
-        }
-    }
-
     delete_records('event', 'modulename', 'quiz', 'instance', $quiz->id);  // Just in case
 
     $event = NULL;
@@ -111,34 +94,6 @@ function quiz_update_instance($quiz) {
         set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
     }
 
-    // The grades for every 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 (isset($quiz->grades)) { // this will not be set if we come from mod.html
-        foreach ($quiz->grades as $question => $grade) {
-            if ($question) {
-                unset($questiongrade);
-                $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;
-                    }
-                }
-            }
-        }
-    }
-
     // currently this code deletes all existing events and adds new ones
     // this should be improved to update existing events only
     if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
index 64e927cc127cc88a6ac764a2112ca9aaafa73eb4..af0bd671a116e6cc22cbbc4438d3d6961acd78ea 100644 (file)
@@ -352,6 +352,30 @@ function quiz_get_question_grades($quizid, $questionlist) {
                             AND question IN ($questionlist)");
 }
 
+function quiz_questiongrades_update($grades, $quizid) {
+    $existing = get_records("quiz_question_grades", "quiz", $quizid, "", "question,grade,id");
+    foreach ($grades as $question => $grade) {
+        if ($question) {
+            unset($questiongrade);
+            $questiongrade->quiz = $quizid;
+            $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;
+                }
+            }
+        }
+    }
+}
+
 function quiz_get_grade_records($quiz) {
 /// Gets all info required to display the table of quiz results
 /// for report.php