]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10682 - Ensure quiz and question grades are formatted consisently and correctly...
authortjhunt <tjhunt>
Fri, 15 Aug 2008 06:42:38 +0000 (06:42 +0000)
committertjhunt <tjhunt>
Fri, 15 Aug 2008 06:42:38 +0000 (06:42 +0000)
15 files changed:
blocks/quiz_results/block_quiz_results.php
lib/questionlib.php
mod/quiz/attemptlib.php
mod/quiz/lib.php
mod/quiz/locallib.php
mod/quiz/report/overview/overview_table.php
mod/quiz/report/overview/overviewgraph.php
mod/quiz/report/overview/report.php
mod/quiz/report/reportlib.php
mod/quiz/report/responses/report.php
mod/quiz/report/statistics/report.php
mod/quiz/review.php
mod/quiz/view.php
question/type/questiontype.php
question/type/shortanswer/questiontype.php

index 4dc9da970fa961cd4b6d378cb1be7e7a2e13122d..cc64cbcce35a7258e7bf7c372d2e76034f4485fa 100644 (file)
@@ -93,15 +93,8 @@ class block_quiz_results extends block_base {
         }
 
         if(!empty($this->config->usegroups)) {
-            // The block was configured to operate in group mode
-            if($course->groupmodeforce) {
-                $groupmode = $course->groupmode;
-            }
-            else {
-                $module = $DB->get_record_sql('SELECT cm.groupmode FROM {modules} m LEFT JOIN {course_modules} cm ON m.id = cm.module WHERE m.name = \'quiz\' AND cm.instance = ?', array($quizid));
-                $groupmode = $module->groupmode;
-            }
-            // The actual groupmode for the quiz is now known to be $groupmode
+            $groupmode = groups_get_activity_groupmode(
+                    get_coursemodule_from_instance('quiz', $quizid, $course->id), $course);
         }
 
         if (has_capability('moodle/site:accessallgroups', $context) && $groupmode == SEPARATEGROUPS) {
@@ -216,10 +209,10 @@ class block_quiz_results extends block_base {
                     $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
                     switch($gradeformat) {
                         case B_QUIZRESULTS_GRADE_FORMAT_FRA:
-                            $this->content->text .= (format_float($averagegrade,$quiz->decimalpoints).'/'.$quiz->grade);
+                            $this->content->text .= quiz_format_grade($quiz, $averagegrade).'/'.$quiz->grade;
                         break;
                         case B_QUIZRESULTS_GRADE_FORMAT_ABS:
-                            $this->content->text .= format_float($averagegrade,$quiz->decimalpoints);
+                            $this->content->text .= quiz_format_grade($quiz, $averagegrade);
                         break;
                         default:
                         case B_QUIZRESULTS_GRADE_FORMAT_PCT:
@@ -251,10 +244,10 @@ class block_quiz_results extends block_base {
                     $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
                     switch($gradeformat) {
                         case B_QUIZRESULTS_GRADE_FORMAT_FRA:
-                            $this->content->text .= (format_float($averagegrade,$quiz->decimalpoints).'/'.$quiz->grade);
+                            $this->content->text .= quiz_format_grade($quiz, $averagegrade).'/'.$quiz->grade;
                         break;
                         case B_QUIZRESULTS_GRADE_FORMAT_ABS:
-                            $this->content->text .= format_float($averagegrade,$quiz->decimalpoints);
+                            $this->content->text .= quiz_format_grade($quiz, $averagegrade);
                         break;
                         default:
                         case B_QUIZRESULTS_GRADE_FORMAT_PCT:
@@ -350,10 +343,10 @@ class block_quiz_results extends block_base {
                     $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
                     switch($gradeformat) {
                         case B_QUIZRESULTS_GRADE_FORMAT_FRA:
-                            $this->content->text .= (format_float($grades[$gradeid]->grade,$quiz->decimalpoints).'/'.$quiz->grade);
+                            $this->content->text .=  quiz_format_grade($quiz, $grades[$gradeid]->grade).'/'.$quiz->grade;
                         break;
                         case B_QUIZRESULTS_GRADE_FORMAT_ABS:
-                            $this->content->text .= format_float($grades[$gradeid]->grade,$quiz->decimalpoints);
+                            $this->content->text .= quiz_format_grade($quiz, $grades[$gradeid]->grade);
                         break;
                         default:
                         case B_QUIZRESULTS_GRADE_FORMAT_PCT:
@@ -391,10 +384,10 @@ class block_quiz_results extends block_base {
                     $this->content->text .= '<tr><td>'.(++$rank).'.</td><td>'.$thisname.'</td><td>';
                     switch($gradeformat) {
                         case B_QUIZRESULTS_GRADE_FORMAT_FRA:
-                            $this->content->text .= (format_float($grades[$gradeid]->grade,$quiz->decimalpoints).'/'.$quiz->grade);
+                            $this->content->text .= quiz_format_grade($quiz, $grades[$gradeid]->grade).'/'.$quiz->grade;
                         break;
                         case B_QUIZRESULTS_GRADE_FORMAT_ABS:
-                            $this->content->text .= format_float($grades[$gradeid]->grade,$quiz->decimalpoints);
+                            $this->content->text .= quiz_format_grade($quiz, $grades[$gradeid]->grade);
                         break;
                         default:
                         case B_QUIZRESULTS_GRADE_FORMAT_PCT:
index ad9bbf87c24403b7bb35ba85cf63ea650a4112ae..feac0fefc2752c82fc4864fe9a3baf933f663e1e 100644 (file)
@@ -1806,6 +1806,15 @@ function question_hash($question) {
     return make_unique_id_code();
 }
 
+/**
+ * Round a grade to to the correct number of decimal places, and format it for display.
+ *
+ * @param object $cmoptions The modules settings, only ->decimalpoints is used.
+ * @param float $grade The grade to round.
+ */
+function question_format_grade($cmoptions, $grade) {
+    return format_float($grade, $cmoptions->decimalpoints);
+}
 
 /// FUNCTIONS THAT SIMPLY WRAP QUESTIONTYPE METHODS //////////////////////////////////
 /**
index ea054359848ffb61a3b8b2998af7b144574adbe2..7725b803d06f03288d3d3198e3c8d6344d6826ce 100644 (file)
@@ -562,7 +562,7 @@ class quiz_attempt extends quiz {
         $this->ensure_state_loaded($questionid);
         $options = quiz_get_renderoptions($this->quiz->review, $this->states[$questionid]);
         if ($options->scores) {
-            return round($this->states[$questionid]->last_graded->grade, $this->quiz->decimalpoints);
+            return quiz_format_grade($this->quiz, $this->states[$questionid]->last_graded->grade);
         } else {
             return '';
         }
@@ -648,10 +648,13 @@ class quiz_attempt extends quiz {
         $panel->display();
     }
 
-        /// List of all this user's attempts for people who can see reports.
+    /// List of all this user's attempts for people who can see reports.
     public function links_to_other_attempts($url) {
         $search = '/\battempt=' . $this->attempt->id . '\b/';
         $attempts = quiz_get_user_attempts($this->quiz->id, $this->attempt->userid, 'all');
+        if (count($attempts) <= 1) {
+            return false;
+        }
         $attemptlist = array();
         foreach ($attempts as $at) {
             if ($at->id == $this->attempt->id) {
@@ -705,9 +708,9 @@ class quiz_attempt extends quiz {
         }
         $param = '';
         if ($showall) {
-            $param = '&showall=1';
+            $param = '&amp;showall=1';
         } else if ($page > 0) {
-            $param = '&page=' . $page;
+            $param = '&amp;page=' . $page;
         }
         return $param . $fragment;
     }
index b5cffc27316bc1c99da04f023acea19b2ed8884b..2b0d456ccd417dfe3f00d8b19e5d784e0d8718a9 100644 (file)
@@ -158,7 +158,6 @@ function quiz_delete_instance($id) {
         'quiz_attempts' => 'quiz',
         'quiz_grades' => 'quiz',
         'quiz_question_instances' => 'quiz',
-        'quiz_grades' => 'quiz',
         'quiz_feedback' => 'quizid',
         'quiz' => 'id'
     );
@@ -186,7 +185,6 @@ function quiz_delete_instance($id) {
     return $result;
 }
 
-
 function quiz_user_outline($course, $user, $mod, $quiz) {
     global $DB;
 /// Return a small object with summary information about what a
@@ -195,34 +193,31 @@ function quiz_user_outline($course, $user, $mod, $quiz) {
 /// $return->time = the time they did it
 /// $return->info = a short text description
     if ($grade = $DB->get_record('quiz_grades', array('userid' => $user->id, 'quiz' => $quiz->id))) {
-
         $result = new stdClass;
         if ((float)$grade->grade) {
-            $result->info = get_string('grade').':&nbsp;'.round($grade->grade, $quiz->decimalpoints);
+            $result->info = get_string('grade').':&nbsp;'.quiz_format_grade($quiz, $grade->grade);
         }
         $result->time = $grade->timemodified;
         return $result;
     }
     return NULL;
-
 }
 
-
 function quiz_user_complete($course, $user, $mod, $quiz) {
     global $DB;
 /// Print a detailed representation of what a  user has done with
 /// a given particular instance of this module, for user activity reports.
 
     if ($attempts = $DB->get_records_select('quiz_attempts', "userid=? AND quiz=?", 'attempt ASC', array($user->id, $quiz->id))) {
-        if ($quiz->grade  and $quiz->sumgrades && $grade = $DB->get_record('quiz_grades', array('userid' => $user->id, 'quiz' => $quiz->id))) {
-            echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'<br />';
+        if ($quiz->grade && $quiz->sumgrades && $grade = quiz_get_best_grade($quiz, $user->id)) {
+            echo get_string('grade') . ': ' . $grade . '/' . $quiz->grade . '<br />';
         }
         foreach ($attempts as $attempt) {
             echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
             if ($attempt->timefinish == 0) {
                 print_string('unfinished');
             } else {
-                echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
+                echo quiz_format_grade($quiz, $attempt->sumgrades).'/'.$quiz->sumgrades;
             }
             echo ' - '.userdate($attempt->timemodified).'<br />';
         }
@@ -282,7 +277,8 @@ function quiz_get_user_attempts($quizid, $userid=0, $status = 'finished', $inclu
  *
  * @param int $quizid id of quiz
  * @param int $userid optional user id, 0 means all users
- * @return array array of grades, false if none
+ * @return array array of grades, false if none. These are raw grades. They should
+ * be processed with quiz_format_grade for display.
  */
 function quiz_get_user_grades($quiz, $userid=0) {
     global $CFG, $DB;
@@ -301,6 +297,16 @@ function quiz_get_user_grades($quiz, $userid=0) {
     return $DB->get_records_sql($sql, $params);
 }
 
+/**
+ * Round a grade to to the correct number of decimal places, and format it for display.
+ *
+ * @param object $quiz The quiz table row, only $quiz->decimalpoints is used.
+ * @param float $grade The grade to round.
+ */
+function quiz_format_grade($quiz, $grade) {
+    return format_float($grade, $quiz->decimalpoints);
+}
+
 /**
  * Update grades in central gradebook
  *
index d14519f4a6a60609be01ec31ed3313bda50b2a84..457e6f9532fca935574eaca5007d377e914c1410 100644 (file)
@@ -381,7 +381,8 @@ function quiz_get_all_question_grades($quiz) {
  *
  * @param object $quiz the quiz object.
  * @param integer $userid the id of the user.
- * @return float the user's current grade for this quiz.
+ * @return float the user's current grade for this quiz, or NULL if this user does
+ * not have a grade on this quiz.
  */
 function quiz_get_best_grade($quiz, $userid) {
     global $DB;
@@ -389,7 +390,7 @@ function quiz_get_best_grade($quiz, $userid) {
 
     // Need to detect errors/no result, without catching 0 scores.
     if (is_numeric($grade)) {
-        return round($grade, $quiz->decimalpoints);
+        return quiz_format_grade($quiz, $grade);
     } else {
         return NULL;
     }
@@ -407,7 +408,7 @@ function quiz_rescale_grade($rawgrade, $quiz, $round = true) {
     if ($quiz->sumgrades) {
         $grade = $rawgrade * $quiz->grade / $quiz->sumgrades;
         if ($round) {
-            $grade = round($grade, $quiz->decimalpoints);
+            $grade = quiz_format_grade($quiz, $grade);
         }
     } else {
         $grade = 0;
index dfa4486fefc330f20c810e0e0186af824daea78c..575d3a57cff203b1e6911804a5bfd928df0df506 100644 (file)
@@ -47,7 +47,7 @@ class quiz_report_overview_table extends table_sql {
                 $groupaveragesql = $averagesql." AND qg.userid $g_usql";
                 $groupaverage = $DB->get_record_sql($groupaveragesql, array_merge($params, $g_params));
                 $groupaveragerow = array($namekey => get_string('groupavg', 'grades'),
-                        'sumgrades' => round($groupaverage->grade, $this->quiz->decimalpoints),
+                        'sumgrades' => quiz_format_grade($this->quiz, $groupaverage->grade),
                         'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($groupaverage->grade, $this->quiz->id)));
                 if($this->detailedmarks && $this->qmsubselect) {
                     $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->groupstudents);
@@ -59,7 +59,7 @@ class quiz_report_overview_table extends table_sql {
             list($s_usql, $s_params) = $DB->get_in_or_equal($this->students);
             $overallaverage = $DB->get_record_sql($averagesql." AND qg.userid $s_usql", array_merge($params, $s_params));
             $overallaveragerow = array($namekey => get_string('overallaverage', 'grades'),
-                        'sumgrades' => round($overallaverage->grade, $this->quiz->decimalpoints),
+                        'sumgrades' => quiz_format_grade($this->quiz, $overallaverage->grade),
                         'feedbacktext'=> strip_tags(quiz_report_feedback_for_grade($overallaverage->grade, $this->quiz->id)));
             if($this->detailedmarks && $this->qmsubselect) {
                 $avggradebyq = quiz_get_average_grade_for_questions($this->quiz, $this->students);
index 687272c5e36dc26764a9cfc56497d50a251418d5..1dd60bba7b21121e2e6aa0bb9c78adcdd359a076 100644 (file)
@@ -61,11 +61,11 @@ $bandwidth = $quiz->grade / $bands;
 $bands = ceil($bands);
 $bandlabels = array();
 for ($i=0;$i < $quiz->grade;$i += $bandwidth){
-    $label = number_format($i, $quiz->decimalpoints).' - ';
+    $label = quiz_format_grade($quiz, $i).' - ';
     if ($quiz->grade > $i+$bandwidth){
-        $label .= number_format($i+$bandwidth, $quiz->decimalpoints);
+        $label .= quiz_format_grade($quiz, $i+$bandwidth);
     } else {
-        $label .= number_format($quiz->grade, $quiz->decimalpoints);
+        $label .= quiz_format_grade($quiz, $quiz->grade);
     }
     $bandlabels[] = $label;
 }
index c058677386a9d926048d4b2842f0e22e1dc83bed..2959ba9dec3751e1c8dfa9be86c34753407994ad 100644 (file)
@@ -369,7 +369,7 @@ class quiz_overview_report extends quiz_default_report {
             }
             if ($showgrades) {
                 $columns[] = 'sumgrades';
-                $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
+                $headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
              }
     
             if ($hasfeedback) {
index 417b71dd2f89bcdb3dd86cea0bf939cb0fbf1e69..415d6dc1282851b0ed31695e50d255457c456e9e 100644 (file)
@@ -307,7 +307,7 @@ function quiz_report_scale_sumgrades_as_percentage($rawgrade, $quiz, $round = tr
     if ($quiz->sumgrades) {
         $grade = $rawgrade * 100 / $quiz->sumgrades;
         if ($round) {
-            $grade = number_format($grade, $quiz->decimalpoints);
+            $grade = quiz_format_grade($quiz, $grade);
         }
     } else {
         $grade = 0;
index 9dfc554212b02f0b4418a9463dceeb925d4b2710..76ee1c598ab9b5bfb32f6302b20398813b1d7169 100644 (file)
@@ -278,7 +278,7 @@ class quiz_responses_report extends quiz_default_report {
             
             if ($showgrades) {
                 $columns[] = 'sumgrades';
-                $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
+                $headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
             }
 
             if ($hasfeedback) {
index e2812a56fce9e06bd30fc7df6016ad040b3d2a29..294c095698a7f2325d21bde5cca59be2ed7b8c80 100644 (file)
@@ -255,7 +255,7 @@ class quiz_statistics_report extends quiz_default_report {
                         $formattedvalue = quiz_report_scale_sumgrades_as_percentage($value, $quiz);
                         break;
                     case 'number_format' :
-                        $formattedvalue = number_format($value, $quiz->decimalpoints).'%';
+                        $formattedvalue = quiz_format_grade($quiz, $value).'%';
                         break;
                     default :
                         $formattedvalue = $value;
index be4a2ae184ab80e161ab1984e4c4ef7fa38657ae..29a8308550f832035de5ae6c8ff627562e0d8182 100644 (file)
         /// Show raw marks only if they are different from the grade (like on the view page.
             if ($quiz->grade != $quiz->sumgrades) {
                 $a = new stdClass;
-                $a->grade = round($attempt->sumgrades, $CFG->quiz_decimalpoints);
-                $a->maxgrade = $quiz->sumgrades;
+                $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
+                $a->maxgrade = quiz_format_grade($quiz, $attempt->sumgrades);
                 $rows[] = '<tr><th scope="row" class="cell">' . get_string('marks', 'quiz') . '</th><td class="cell">' .
                         get_string('outofshort', 'quiz', $a) . '</td></tr>';
             }
         /// Now the scaled grade.
             $a = new stdClass;
             $a->grade = '<b>' . $grade . '</b>';
-            $a->maxgrade = $quiz->grade;
+            $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
             $a->percent = '<b>' . round(($attempt->sumgrades/$quiz->sumgrades)*100, 0) . '</b>';
             $rows[] = '<tr><th scope="row" class="cell">' . get_string('grade') . '</th><td class="cell">' .
                     get_string('outofpercent', 'quiz', $a) . '</td></tr>';
     print_container_end();
     echo '</div>';
 
-    echo '</div>';
     echo '<div class="clearer"></div>';
 
     // Finish the page
index 20152658c91fa85cd9598a658153c80c7acb128e..5a6aeb7ceb65b0816855392879ad309ecf8f8f0b 100644 (file)
 
             if ($markcolumn && $attempt->timefinish > 0) {
                 if ($attemptoptions->scores) {
-                    $row[] = $accessmanager->make_review_link(round($attempt->sumgrades, $quiz->decimalpoints),
+                    $row[] = $accessmanager->make_review_link(quiz_format_grade($quiz, $attempt->sumgrades),
                             $attempt, $canpreview, $attemptoptions);
                 } else {
                     $row[] = '';
index 9efc545172e5df237cfcdb473f8700a908d3cfdb..7e68c0e0e90f8854df12550500331b6414aee748 100644 (file)
@@ -855,7 +855,11 @@ class default_questiontype {
         $grade = '';
         if ($question->maxgrade and $options->scores) {
             if ($cmoptions->optionflags & QUESTION_ADAPTIVE) {
-                $grade = !$isgraded ? '--/' : round($state->last_graded->grade, $cmoptions->decimalpoints).'/';
+                if ($isgraded) {
+                    $grade = question_format_grade($cmoptions, $state->last_graded->grade).'/';
+                } else {
+                    $grade = '--/'; 
+                }
             }
             $grade .= $question->maxgrade;
         }
@@ -937,9 +941,8 @@ class default_questiontype {
                                                 $b.get_string('event'.$st->event, 'quiz').$be,
                                                 $b.s($this->response_summary($question, $st)).$be,
                                                 $b.userdate($st->timestamp, get_string('timestr', 'quiz')).$be,
-                                                $b.round($st->raw_grade, $cmoptions->decimalpoints).$be,
-                                                //$b.round($st->penalty, $cmoptions->decimalpoints).$be,
-                                                $b.round($st->grade, $cmoptions->decimalpoints).$be
+                                                $b.question_format_grade($cmoptions, $st->raw_grade).$be,
+                                                $b.question_format_grade($cmoptions, $st->grade).$be
                                                 );
                     } else {
                         $table->data[] = array (
@@ -989,9 +992,9 @@ class default_questiontype {
             if (question_state_is_graded($state->last_graded)) {
                 // Display the grading details from the last graded state
                 $grade = new stdClass;
-                $grade->cur = round($state->last_graded->grade, $cmoptions->decimalpoints);
+                $grade->cur = question_format_grade($cmoptions, $state->last_graded->grade);
                 $grade->max = $question->maxgrade;
-                $grade->raw = round($state->last_graded->raw_grade, $cmoptions->decimalpoints);
+                $grade->raw = question_format_grade($cmoptions, $state->last_graded->raw_grade);
 
                 // let student know wether the answer was correct
                 $class = question_get_feedback_class($state->last_graded->raw_grade / 
index 5bbffcd543a19a24f58235e324387856971584ee..5e700df4c9c3f821242e7fa283df78f0d8981497 100644 (file)
@@ -400,9 +400,9 @@ class question_shortanswer_qtype extends default_questiontype {
             if (question_state_is_graded($state->last_graded)) {
                 // Display the grading details from the last graded state
                 $grade = new stdClass;
-                $grade->cur = round($state->last_graded->grade, $cmoptions->decimalpoints);
+                $grade->cur = question_format_grade($cmoptions, $state->last_graded->grade);
                 $grade->max = $question->maxgrade;
-                $grade->raw = round($state->last_graded->raw_grade, $cmoptions->decimalpoints);
+                $grade->raw = question_format_grade($cmoptions, $state->last_graded->raw_grade);
 
                 // let student know wether the answer was correct
                 $class = question_get_feedback_class($state->last_graded->raw_grade /