Skip to content

Commit

Permalink
MDL-10682 - Ensure quiz and question grades are formatted consisently…
Browse files Browse the repository at this point in the history
… and correctly. While testing, I also fixed a few XHTML Strrrict errors.
  • Loading branch information
tjhunt committed Aug 15, 2008
1 parent 7caa6bb commit f88fb62
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 56 deletions.
27 changes: 10 additions & 17 deletions blocks/quiz_results/block_quiz_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,8 @@ function get_content() {
}

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) {
Expand Down Expand Up @@ -216,10 +209,10 @@ function get_content() {
$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:
Expand Down Expand Up @@ -251,10 +244,10 @@ function get_content() {
$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:
Expand Down Expand Up @@ -350,10 +343,10 @@ function get_content() {
$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:
Expand Down Expand Up @@ -391,10 +384,10 @@ function get_content() {
$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:
Expand Down
9 changes: 9 additions & 0 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 //////////////////////////////////
/**
Expand Down
11 changes: 7 additions & 4 deletions mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public function get_question_score($questionid) {
$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 '';
}
Expand Down Expand Up @@ -648,10 +648,13 @@ public function print_navigation_panel($panelclass, $page) {
$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) {
Expand Down Expand Up @@ -705,9 +708,9 @@ private function page_and_question_fragment($questionid, $page, $showall = false
}
$param = '';
if ($showall) {
$param = '&showall=1';
$param = '&amp;showall=1';
} else if ($page > 0) {
$param = '&page=' . $page;
$param = '&amp;page=' . $page;
}
return $param . $fragment;
}
Expand Down
26 changes: 16 additions & 10 deletions mod/quiz/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down Expand Up @@ -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
Expand All @@ -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 />';
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
*
Expand Down
7 changes: 4 additions & 3 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,16 @@ 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;
$grade = $DB->get_field('quiz_grades', 'grade', array('quiz' => $quiz->id, 'userid' => $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;
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/report/overview/overview_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function build_table(){
$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);
Expand All @@ -59,7 +59,7 @@ function build_table(){
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);
Expand Down
6 changes: 3 additions & 3 deletions mod/quiz/report/overview/overviewgraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ function graph_get_new_colour(){
$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;
}
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/report/overview/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ function display($quiz, $cm, $course) {
}
if ($showgrades) {
$columns[] = 'sumgrades';
$headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
$headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
}

if ($hasfeedback) {
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/report/reportlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/report/responses/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function display($quiz, $cm, $course) {

if ($showgrades) {
$columns[] = 'sumgrades';
$headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
$headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
}

if ($hasfeedback) {
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/report/statistics/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function output_quiz_stats_table($course, $cm, $quiz, $quizstats, $usingattempts
$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;
Expand Down
7 changes: 3 additions & 4 deletions mod/quiz/review.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@
/// 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>';
Expand Down Expand Up @@ -223,7 +223,6 @@
print_container_end();
echo '</div>';

echo '</div>';
echo '<div class="clearer"></div>';

// Finish the page
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = '';
Expand Down
15 changes: 9 additions & 6 deletions question/type/questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,11 @@ function print_question(&$question, &$state, $number, $cmoptions, $options) {
$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;
}
Expand Down Expand Up @@ -937,9 +941,8 @@ function history($question, $state, $number, $cmoptions, $options) {
$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 (
Expand Down Expand Up @@ -989,9 +992,9 @@ function print_question_grading_details(&$question, &$state, $cmoptions, $option
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 /
Expand Down
Loading

0 comments on commit f88fb62

Please sign in to comment.