]> git.mjollnir.org Git - moodle.git/commitdiff
quiz editing: MDL-15903 fix the code that updates $quiz->sumgrades,
authortjhunt <tjhunt>
Fri, 5 Dec 2008 05:08:37 +0000 (05:08 +0000)
committertjhunt <tjhunt>
Fri, 5 Dec 2008 05:08:37 +0000 (05:08 +0000)
so we only update the database when something changes, not on every page view.

mod/quiz/edit.php
mod/quiz/editlib.php
mod/quiz/locallib.php

index cedbc45ebb9e0b3a17ccd00e972e79622c655110..77fe928119f51204de68a01dcede31d83b786f97 100644 (file)
@@ -168,8 +168,7 @@ add_to_log($cm->course, 'quiz', 'editquestions',
 //this page.
 require_capability('mod/quiz:manage', $contexts->lowest());
 
-if (isset($quiz->instance)
-&& empty($quiz->grades)){  // Construct an array to hold all the grades.
+if (isset($quiz->instance) && empty($quiz->grades)){  // Construct an array to hold all the grades.
     $quiz->grades = quiz_get_all_question_grades($quiz);
 }
 
@@ -387,9 +386,8 @@ if ((($deleteemptypage = optional_param('deleteemptypage', false, PARAM_INT))
             array('id' => $quiz->instance))) {
         print_error('cannotsavequestion', 'quiz');
     }
-
-
 }
+
 $deletequestions=array();
 if($quizdeleteselected=optional_param('quizdeleteselected',false)){
     $rawgrades = (array) data_submitted();
@@ -401,7 +399,6 @@ if($quizdeleteselected=optional_param('quizdeleteselected',false)){
     }
 }
 
-
 if ( (($delete = optional_param('delete', false, PARAM_INT)) !== false OR
         !empty($deletequestions)) and confirm_sesskey() ) {
 
@@ -447,8 +444,7 @@ if (optional_param('savechanges', false, PARAM_BOOL) and confirm_sesskey()) {
             /// Parse input for question -> grades
             $key = $matches[1];
             $quiz->grades[$key] = $value;
-            quiz_update_question_instance($quiz->grades[$key], $key,
-            $quiz->instance);
+            quiz_update_question_instance($quiz->grades[$key], $key, $quiz->instance);
 
         } elseif (preg_match('!^o([0-9]+)$!', $key, $matches)) {
             /// Parse input for ordering info
@@ -461,6 +457,7 @@ if (optional_param('savechanges', false, PARAM_BOOL) and confirm_sesskey()) {
                 $value++;
             }
             $questions[$value] = $oldquestions[$key];
+
         } elseif (preg_match('!^s([0-9]+)$!', $key, $matches)){
             // Parse input for selected questions
             // (add new pages after questions in quiz)
@@ -540,6 +537,7 @@ if ($significantchangemade) {
             quiz_delete_attempt($attempt, $quiz);
         }
     }
+    quiz_update_sumgrades($quiz);
     redirect($qcobject->pageurl->out());
 }
 
@@ -696,28 +694,20 @@ if($quiz_reordertool){
     echo ' <input type="submit" name="repaginate" value="'. $gostring .'" '.$repaginatingdisabledhtml.' />';
     echo '</div></fieldset></form></div></div>';
 }
-ob_start();
-$sumgrades = quiz_print_question_list($quiz, $thispageurl, true,
-        $quiz_reordertool, $quiz_qbanktool, $quiz_has_attempts);
-
-if (!$DB->set_field('quiz', 'sumgrades', $sumgrades, array('id' => $quiz->instance))) {
-    print_error('cannotsetsumgrades', 'quiz');
-}
-
-$question_list=ob_get_contents();
-ob_end_clean();
 $tabindex=0;
 
 if(!$quiz_reordertool){
-    quiz_print_grading_form($quiz,$thispageurl,$tabindex);
+    quiz_print_grading_form($quiz, $thispageurl, $tabindex);
 }
-quiz_print_status_bar($quiz,$sumgrades);
+quiz_print_status_bar($quiz);
 ?>
 <div class="<?php echo $currenttab; ?>">
-<?php echo $question_list; ?>
+<?php quiz_print_question_list($quiz, $thispageurl, true,
+        $quiz_reordertool, $quiz_qbanktool, $quiz_has_attempts); ?>
 </div>
 <?php
-//close <div class="quizcontents">:
+
+// Close <div class="quizcontents">:
 echo '</div>';
 
 if(!$quiz_reordertool){
index 1d32be1869ad76afaabd6bf6818634c5ccd742c1..56e38544112359384c23ca1d4f8feb5fbf6bf5b1 100644 (file)
@@ -1515,14 +1515,13 @@ function quiz_print_grading_form($quiz, $pageurl, $tabindex){
  * Print the status bar
  *
  * @param object $quiz The quiz object of the quiz in question
- * @param integer $sumgrades The sum of the grades of the quiz to display
  */
 
-function quiz_print_status_bar($quiz,$sumgrades){
+function quiz_print_status_bar($quiz){
     global $CFG;
     $numberofquestions=quiz_number_of_questions_in_quiz($quiz->questions);
     ?><div class="statusdisplay"><span class="totalpoints">
-    <?php echo get_string("totalpointsx","quiz",$sumgrades) ?></span>
+    <?php echo get_string('totalpointsx', 'quiz', quiz_format_grade($quiz, $quiz->sumgrades)) ?></span>
     | <span class="numberofquestions">
     <?php
     echo get_string("numquestionsx","quiz",$numberofquestions);
index 32cca7f5298485c7008109252ece4420bfb21feb..4121f2e25ac091b7958c9548e91aea9d9a7812af 100644 (file)
@@ -359,12 +359,11 @@ function quiz_print_navigation_panel($page, $pages) {
 
 /**
  * Creates an array of maximum grades for a quiz
- *
  * The grades are extracted from the quiz_question_instances table.
- * @return array        Array of grades indexed by question id
- *                      These are the maximum possible grades that
- *                      students can achieve for each of the questions
+ * 
  * @param integer $quiz The quiz object
+ * @return array Array of grades indexed by question id. These are the maximum
+ *      possible grades that students can achieve for each of the questions.
  */
 function quiz_get_all_question_grades($quiz) {
     global $CFG, $DB;
@@ -399,7 +398,25 @@ function quiz_get_all_question_grades($quiz) {
     return $grades;
 }
 
-
+/**
+ * Update the sumgrades field of the quiz. This needs to be called whenever
+ * the grading structure of the quiz is changed. For example if a question is
+ * added or removed, or a question weight is changed.
+ *
+ * @param object $quiz a quiz.
+ */
+function quiz_update_sumgrades($quiz) {
+    global $DB;
+    $grades = quiz_get_all_question_grades($quiz);
+    $sumgrades = 0;
+    foreach ($grades as $grade) {
+        $sumgrades += $grade;
+    }
+    if (!isset($quiz->sumgrades) || $quiz->sumgrades != $sumgrades) {
+        $DB->set_field('quiz', 'sumgrades', $sumgrades, array('id' => $quiz->id));
+        $quiz->sumgrades = $sumgrades;
+    }
+}
 
 /**
  * Convert the raw grade stored in $attempt into a grade out of the maximum