From: tjhunt Date: Fri, 15 Aug 2008 06:42:38 +0000 (+0000) Subject: MDL-10682 - Ensure quiz and question grades are formatted consisently and correctly... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f88fb62c40249c74475de64eedeae2c3a549441c;p=moodle.git MDL-10682 - Ensure quiz and question grades are formatted consisently and correctly. While testing, I also fixed a few XHTML Strrrict errors. --- diff --git a/blocks/quiz_results/block_quiz_results.php b/blocks/quiz_results/block_quiz_results.php index 4dc9da970f..cc64cbcce3 100644 --- a/blocks/quiz_results/block_quiz_results.php +++ b/blocks/quiz_results/block_quiz_results.php @@ -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 .= ''.(++$rank).'.'.$thisname.''; 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 .= ''.(++$rank).'.'.$thisname.''; 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 .= ''.(++$rank).'.'.$thisname.''; 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 .= ''.(++$rank).'.'.$thisname.''; 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: diff --git a/lib/questionlib.php b/lib/questionlib.php index ad9bbf87c2..feac0fefc2 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -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 ////////////////////////////////// /** diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index ea05435984..7725b803d0 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -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 = '&showall=1'; } else if ($page > 0) { - $param = '&page=' . $page; + $param = '&page=' . $page; } return $param . $fragment; } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index b5cffc2731..2b0d456ccd 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -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').': '.round($grade->grade, $quiz->decimalpoints); + $result->info = get_string('grade').': '.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.'
'; + if ($quiz->grade && $quiz->sumgrades && $grade = quiz_get_best_grade($quiz, $user->id)) { + echo get_string('grade') . ': ' . $grade . '/' . $quiz->grade . '
'; } 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).'
'; } @@ -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 * diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index d14519f4a6..457e6f9532 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -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; diff --git a/mod/quiz/report/overview/overview_table.php b/mod/quiz/report/overview/overview_table.php index dfa4486fef..575d3a57cf 100644 --- a/mod/quiz/report/overview/overview_table.php +++ b/mod/quiz/report/overview/overview_table.php @@ -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); diff --git a/mod/quiz/report/overview/overviewgraph.php b/mod/quiz/report/overview/overviewgraph.php index 687272c5e3..1dd60bba7b 100644 --- a/mod/quiz/report/overview/overviewgraph.php +++ b/mod/quiz/report/overview/overviewgraph.php @@ -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; } diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index c058677386..2959ba9dec 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -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) { diff --git a/mod/quiz/report/reportlib.php b/mod/quiz/report/reportlib.php index 417b71dd2f..415d6dc128 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -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; diff --git a/mod/quiz/report/responses/report.php b/mod/quiz/report/responses/report.php index 9dfc554212..76ee1c598a 100644 --- a/mod/quiz/report/responses/report.php +++ b/mod/quiz/report/responses/report.php @@ -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) { diff --git a/mod/quiz/report/statistics/report.php b/mod/quiz/report/statistics/report.php index e2812a56fc..294c095698 100644 --- a/mod/quiz/report/statistics/report.php +++ b/mod/quiz/report/statistics/report.php @@ -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; diff --git a/mod/quiz/review.php b/mod/quiz/review.php index be4a2ae184..29a8308550 100644 --- a/mod/quiz/review.php +++ b/mod/quiz/review.php @@ -166,8 +166,8 @@ /// 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[] = '' . get_string('marks', 'quiz') . '' . get_string('outofshort', 'quiz', $a) . ''; } @@ -175,7 +175,7 @@ /// Now the scaled grade. $a = new stdClass; $a->grade = '' . $grade . ''; - $a->maxgrade = $quiz->grade; + $a->maxgrade = quiz_format_grade($quiz, $quiz->grade); $a->percent = '' . round(($attempt->sumgrades/$quiz->sumgrades)*100, 0) . ''; $rows[] = '' . get_string('grade') . '' . get_string('outofpercent', 'quiz', $a) . ''; @@ -223,7 +223,6 @@ print_container_end(); echo ''; - echo ''; echo '
'; // Finish the page diff --git a/mod/quiz/view.php b/mod/quiz/view.php index 20152658c9..5a6aeb7ceb 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -239,7 +239,7 @@ 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[] = ''; diff --git a/question/type/questiontype.php b/question/type/questiontype.php index 9efc545172..7e68c0e0e9 100644 --- a/question/type/questiontype.php +++ b/question/type/questiontype.php @@ -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 / diff --git a/question/type/shortanswer/questiontype.php b/question/type/shortanswer/questiontype.php index 5bbffcd543..5e700df4c9 100644 --- a/question/type/shortanswer/questiontype.php +++ b/question/type/shortanswer/questiontype.php @@ -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 /