From 52fa982eba83cde74f0f3552df363f1bbb246161 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sun, 5 Jun 2005 11:49:32 +0000 Subject: [PATCH] Change to when newly created states get saved Changes to what is printed during regrade --- lang/en/quiz.php | 6 ++-- mod/quiz/attempt.php | 19 +++++----- mod/quiz/locallib.php | 57 +++++++++++++++++++----------- mod/quiz/report/regrade/report.php | 27 ++++++++------ mod/quiz/reviewquestion.php | 3 +- 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/lang/en/quiz.php b/lang/en/quiz.php index c8dd80d26fc..146b000f0a5 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -311,11 +311,11 @@ $string['reattemptquiz'] = 'Re-attempt quiz'; $string['recentlyaddedquestion'] = 'Recently added question!'; $string['recurse'] = 'Display questions from sub-categories too'; $string['regrade'] = 'Regrade all attempts'; -$string['regradeattempt'] = 'Regrading attempt #{$a->attempt}, $a->statecount states are being regraded'; $string['regradecomplete'] = 'All attempts have been regraded'; $string['regradecount'] = '$a->changed out of $a->attempt grades were changed'; -$string['regradedone'] = 'done. Changed grades for $a->changed states out of $a->statecount.'; -$string['regradequiz'] = 'Regrading Quiz $a->name'; +$string['regradedisplayexplanation'] = 'Attempts that change during regrading are displayed as hyperlinks to the question review window'; +$string['regradingquestion'] = 'Regrading \"$a\".'; +$string['regradingquiz'] = 'Regrading Quiz \"$a\"'; $string['relative'] = 'Relative'; $string['remove'] = 'Remove'; $string['rename'] = 'Rename'; diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 820960fe182..1f577263c0f 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -216,13 +216,13 @@ // list of questions needed by page $pagelist = quiz_questions_on_page($attempt->layout, $page); - // add all questions that are on the submitted form if ($newattempt) { - $questionlist = $attempt->layout; + $questionlist = quiz_questions_in_quiz($attempt->layout); } else { $questionlist = $pagelist; } + // add all questions that are on the submitted form if ($questionids) { $questionlist .= ','.$questionids; } @@ -253,6 +253,13 @@ error('Could not restore question sessions'); } + // Save all the newly created states + if ($newattempt) { + foreach ($questions as $i => $question) { + quiz_save_question_session($questions[$i], $states[$i]); + } + } + /// Process form data ///////////////////////////////////////////////// @@ -437,14 +444,6 @@ quiz_save_question_session($questions[$i], $states[$i]); $number += $questions[$i]->length; } - if ($newattempt) { - $questionlist = explode(',', $attempt->layout); - foreach ($questionlist as $i) { - if ($i != 0) { - quiz_save_question_session($questions[$i], $states[$i]); - } - } - } /// Print the submit buttons diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index b72e775fe47..8c6fe554340 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -1492,6 +1492,8 @@ function quiz_regrade_question_in_attempt($question, $attempt, $quiz=false, $ver "AND question = '{$question->id}'", 'seq_number ASC')) { $states = array_values($states); + + // what is this for? $attempt->sumgrades -= $states[count($states)-1]->grade; // Initialise the replaystate @@ -1499,14 +1501,8 @@ function quiz_regrade_question_in_attempt($question, $attempt, $quiz=false, $ver $states[0]->sumpenalty = 0.0; $replaystate = clone($states[0]); $replaystate->last_graded = clone($states[0]); - if ($verbose) { - $a = new stdClass; - $a->attempt = $attempt->id; - $a->statecount = count($states); - print_string('regradeattempt', 'quiz', $a); - echo " . "; - } - $a->changed = 0; + + $changed = 0; $oldgrade = 0; for($j = 1; $j < count($states); $j++) { quiz_restore_state($question, $states[$j]); @@ -1534,17 +1530,23 @@ function quiz_regrade_question_in_attempt($question, $attempt, $quiz=false, $ver } if ((float)$replaystate->raw_grade != (float)$states[$j]->raw_grade) { - $a->changed++; - } + $changed++; - $verbose && print(". "); // Progress indicator + } $replaystate->id = $states[$j]->id; update_record('quiz_states', $replaystate); } - update_record('quiz_attempts', $attempt); + if ($verbose) { + if ($changed) { + link_to_popup_window ('/mod/quiz/reviewquestion.php?attempt='.$attempt->id.'&question='.$question->id, + 'reviewquestion', ' #'.$attempt->id, 450, 550, get_string('reviewresponse', 'quiz')); + } else { + echo ' #'.$attempt->id; + } + echo "\n"; flush(); ob_flush(); + } + quiz_save_best_grade($quiz, $attempt->userid); - $verbose && print_string('regradedone', 'quiz', $a); - $verbose && print("
"); return true; } return true; @@ -2235,7 +2237,7 @@ if (!$grade = get_record('quiz_grades', 'quiz', $quiz->id, 'userid', $userid)) { } /** -* TODO: document this +* Save the overall grade for a user at a quiz in the quiz_grades table * * @return boolean Indicates success or failure. * @param object $quiz The quiz for which the best grade is to be calculated @@ -2260,11 +2262,12 @@ function quiz_save_best_grade($quiz, $userid=null) { // Calculate the best grade $bestgrade = quiz_calculate_best_grade($quiz, $attempts); $bestgrade = (($bestgrade / $quiz->sumgrades) * $quiz->grade); + $bestgrade = round($bestgrade, $quiz->decimalpoints); // Save the best grade in the database if ($grade = get_record('quiz_grades', 'quiz', $quiz->id, 'userid', $userid)) { - $grade->grade = round($bestgrade, $quiz->decimalpoints); + $grade->grade = $bestgrade; $grade->timemodified = time(); if (!update_record('quiz_grades', $grade)) { notify('Could not update best grade'); @@ -2273,7 +2276,7 @@ function quiz_save_best_grade($quiz, $userid=null) { } else { $grade->quiz = $quiz->id; $grade->userid = $userid; - $grade->grade = round($bestgrade, $quiz->decimalpoints); + $grade->grade = $bestgrade; $grade->timemodified = time(); if (!insert_record('quiz_grades', $grade)) { notify('Could not insert new best grade'); @@ -2283,9 +2286,14 @@ function quiz_save_best_grade($quiz, $userid=null) { return true; } - +/** +* Calculate the overall grade for a quiz given a number of attempts by a particular user. +* +* @return float The overall grade +* @param object $quiz The quiz for which the best grade is to be calculated +* @param array $attempts An array of all the attempts of the user at the quiz +*/ function quiz_calculate_best_grade($quiz, $attempts) { -/// Calculate the best grade for a quiz given a number of attempts by a particular user. switch ($quiz->grademethod) { @@ -2322,9 +2330,16 @@ function quiz_calculate_best_grade($quiz, $attempts) { } } - +/** +* Return the attempt with the best grade for a quiz +* +* Which attempt is the best depends on $quiz->grademethod. If the grade +* method is GRADEAVERAGE then this function simply returns the last attempt. +* @return object The attempt with the best grade +* @param object $quiz The quiz for which the best grade is to be calculated +* @param array $attempts An array of all the attempts of the user at the quiz +*/ function quiz_calculate_best_attempt($quiz, $attempts) { -/// Return the attempt with the best grade for a quiz switch ($quiz->grademethod) { diff --git a/mod/quiz/report/regrade/report.php b/mod/quiz/report/regrade/report.php index 7e5477138bb..f7696db91eb 100644 --- a/mod/quiz/report/regrade/report.php +++ b/mod/quiz/report/regrade/report.php @@ -9,18 +9,16 @@ class quiz_report extends quiz_default_report { function display($quiz, $cm, $course) { /// This function just displays the report global $CFG, $SESSION, $db, $QUIZ_QTYPES; - $strheading = get_string('regradequiz', 'quiz', $quiz); - $strnoattempts = get_string('noattempts'); - // 'There are no attempts for this quiz that could be regraded.'; - /// Print header $this->print_header_and_tabs($cm, $course, $quiz, $reportmode="regrade"); - if (!$attempts = get_records('quiz_attempts', 'quiz', $quiz->id)) { - notify($strnoattempts); + /// Fetch all attempts + if (!$attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = 0")) { + print_heading(get_string('noattempts', 'quiz')); return true; } + /// Fetch all questions $sql = "SELECT q.*, i.grade AS maxgrade FROM {$CFG->prefix}quiz_questions q, {$CFG->prefix}quiz_question_instances i WHERE i.quiz = $quiz->id @@ -31,15 +29,22 @@ class quiz_report extends quiz_default_report { } quiz_get_question_options($questions); + /// Print heading + print_heading(get_string('regradingquiz', 'quiz', $quiz)); + echo '
'; + print_string('regradedisplayexplanation', 'quiz'); + echo '
'; - print_heading($strheading); - print_simple_box_start('center', '70%'); - foreach ($attempts as $attempt) { - foreach ($questions as $question) { + /// Loop through all questions and all attempts and regrade while printing progress info + foreach ($questions as $question) { + echo ''.get_string('regradingquestion', 'quiz', $question->name).' '.get_string('attempts', 'quiz').": \n"; + foreach ($attempts as $attempt) { quiz_regrade_question_in_attempt($question, $attempt, $quiz, true); } + echo '
'; + // the following makes sure that the output is sent immediately. + flush();ob_flush(); } - print_simple_box_end(); return true; } diff --git a/mod/quiz/reviewquestion.php b/mod/quiz/reviewquestion.php index 4484582b61f..584d34af904 100644 --- a/mod/quiz/reviewquestion.php +++ b/mod/quiz/reviewquestion.php @@ -18,8 +18,7 @@ $stateid = optional_param('state', 0, PARAM_INT); // state id $attemptid = optional_param('attempt', 0, PARAM_INT); // attempt id $questionid = optional_param('question', 0, PARAM_INT); // attempt id - - $number = required_param('number', PARAM_INT); // question number + $number = optional_param('number', 0, PARAM_INT); // question number if ($stateid) { if (! $state = get_record('quiz_states', 'id', $stateid)) {