//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);
}
array('id' => $quiz->instance))) {
print_error('cannotsavequestion', 'quiz');
}
-
-
}
+
$deletequestions=array();
if($quizdeleteselected=optional_param('quizdeleteselected',false)){
$rawgrades = (array) data_submitted();
}
}
-
if ( (($delete = optional_param('delete', false, PARAM_INT)) !== false OR
!empty($deletequestions)) 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
$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)
quiz_delete_attempt($attempt, $quiz);
}
}
+ quiz_update_sumgrades($quiz);
redirect($qcobject->pageurl->out());
}
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){
* 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);
/**
* 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;
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