}
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) {
$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:
$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:
$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:
$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:
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 //////////////////////////////////
/**
$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 '';
}
$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) {
}
$param = '';
if ($showall) {
- $param = '&showall=1';
+ $param = '&showall=1';
} else if ($page > 0) {
- $param = '&page=' . $page;
+ $param = '&page=' . $page;
}
return $param . $fragment;
}
'quiz_attempts' => 'quiz',
'quiz_grades' => 'quiz',
'quiz_question_instances' => 'quiz',
- 'quiz_grades' => 'quiz',
'quiz_feedback' => 'quizid',
'quiz' => 'id'
);
return $result;
}
-
function quiz_user_outline($course, $user, $mod, $quiz) {
global $DB;
/// Return a small object with summary information about what a
/// $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.'<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 />';
}
*
* @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;
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
*
*
* @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;
// 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;
}
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;
$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);
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);
$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;
}
}
if ($showgrades) {
$columns[] = 'sumgrades';
- $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
+ $headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
}
if ($hasfeedback) {
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;
if ($showgrades) {
$columns[] = 'sumgrades';
- $headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
+ $headers[] = get_string('grade', 'quiz').'/'.quiz_format_grade($quiz, $quiz->grade);
}
if ($hasfeedback) {
$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;
/// 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
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[] = '';
$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;
}
$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 (
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 /
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 /