- gives you the option to show the score details to the user for each category in the quiz in the order the category appears in the quiz
- works with multiple-page quizzes
- can easily be tweaked in format and content
Adding Question Category Grade Breakdown for Quizzes in Moodle 1.6.2
- Add a new column showcategorygrades to the quiz table. This will be used to make the category grade breakdown optional.
ALTER TABLE mdl_quiz ADD COLUMN showcategorygrades int2;
ALTER TABLE mdl_quiz ALTER COLUMN showcategorygrades SET STORAGE PLAIN;
ALTER TABLE mdl_quiz ALTER COLUMN showcategorygrades SET DEFAULT 0;
- Add the following lines to quiz/config.html html to show the control for toggling the showcategorygrades property when configuring quizzes in your moodle.
// SRC: Set the category grade control if it is not set
if (!isset($form->showcategorygrades)) {
$form->showcategorygrades = $CFG->quiz_showcategorygrades;
}
…
<!-- SRC: Show the category grade edit controls -->
<tr valign="top">
<td align="right"><b><?php print_string("showcategorygrades", "quiz") ?>:</b></td>
<td>
<?php
choose_from_menu ($yesnooptions, "showcategorygrades", $form->showcategorygrades, "");
helpbutton("showcategorygrades", get_string("showcategorygrades", "quiz"), "quiz");
?>
</td>
<td align="center">
<input type="hidden" name="fix_showcategorygrades" value="0" />
<input type="checkbox" name="fix_showcategorygrades" value="1" <?php p($CFG->quiz_fix_showcategorygrades ? “checked" : "") ?> />
</td>
</tr>
- Add the following lines to quiz/mod.html to show the control for toggling the showcategorygrades property when editing a quiz.
// SRC: Set the category grade control if it is not set
if (!isset($form->showcategorygrades)) {
$form->showcategorygrades = $CFG->quiz_showcategorygrades;
}
…
<!-- SRC: Show the category grade edit controls -->
<?php if (!$CFG->quiz_fix_decimalpoints) { ?>
<tr valign="top">
<td align="right"><b><?php print_string("showcategorygrades", "quiz") ?>:</b></td>
<td>
<?php
choose_from_menu ($yesnooptions, "showcategorygrades", $form->showcategorygrades, "");
helpbutton("showcategorygrades", get_string("showcategorygrades", "quiz"), "quiz");
?>
</td>
</tr>
<?php } else $fix=1 ?>
- Add the following line to lang/en-UTF8/quiz.php. This is the English translation for the control label in the forms you edited in steps 2 and 3.
$string['showcategorygrades'] = 'Show category grades';
- Add the following lines to quiz/review.php. This is what will actually show the category grade breakdown in the quiz report.
// SRC: Add categories string for breakdown section label
$strcategories = get_string("categories", "quiz");
// SRC: Load all the questions for the category breakdown if report is paginated
if (!$showall && $quiz->showcategorygrades == 1) {
$allquizquestionids = quiz_questions_in_quiz($attempt->layout);
$sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance".
" FROM {$CFG->prefix}question q,".
" {$CFG->prefix}quiz_question_instances i".
" WHERE i.quiz = '$quiz->id' AND q.id = i.question".
" AND q.id IN ($allquizquestionids)";
if (!$allquestions = get_records_sql($sql)) {
error('No questions found');
}
// Load the question type specific information
if (!get_question_options($allquestions)) {
error('Could not load question options');
}
// Restore the question sessions to their most recent states
// creating new sessions where required
if (!$allstates = get_question_states($allquestions, $quiz, $attempt)) {
error('Could not restore question sessions');
}
}
if ($showall && $quiz->showcategorygrades == 1) {
$allquestions = $questions;
$allstates = $states;
}
…
// SRC: Category breakdown if it is enabled.
if ($quiz->showcategorygrades == 1) {
$categorybreakdown = array();
$gradescale = $quiz->grade/$quiz->sumgrades;
foreach ($allquestions as $question) {
if (!isset ($categorybreakdown[$question->category])) {
$categorybreakdown[$question->category] = array();
$questioncategory = get_record('question_categories', 'id', $question->category);
$categorybreakdown[$question->category]["categoryname"] = $questioncategory->name;
$categorybreakdown[$question->category]["maxgrade"] = 0;
$categorybreakdown[$question->category]["scaledmaxgrade"] = 0;
$categorybreakdown[$question->category]["rawgrade"] = 0;
$categorybreakdown[$question->category]["scaledrawgrade"] = 0;
$categorybreakdown[$question->category]["grade"] = 0;
$categorybreakdown[$question->category]["scaledgrade"] = 0;
}
$categorybreakdown[$question->category]["maxgrade"] += $question->maxgrade;
$categorybreakdown[$question->category]["scaledmaxgrade"] += $question->maxgrade*$gradescale;
$categorybreakdown[$question->category]["rawgrade"] += $allstates[$question->id]->raw_grade;
$categorybreakdown[$question->category]["scaledrawgrade"] += $allstates[$question->id]->raw_grade*$gradescale;
$categorybreakdown[$question->category]["grade"] += $allstates[$question->id]->grade;
$categorybreakdown[$question->category]["scaledgrade"] += $allstates[$question->id]->grade*$gradescale;
}
$cattable->align = array("right", "left");
foreach ($categorybreakdown as $catinfo) {
$categorypercentage = round(($catinfo["rawgrade"]/$catinfo["maxgrade"])*100,0);
$cattable->data[] = array($catinfo["categoryname"], round($catinfo["scaledgrade"],$CFG->quiz_decimalpoints)."/".round($catinfo["scaledmaxgrade"],$CFG->quiz_decimalpoints)." (".$categorypercentage."%)");
}
}
…
// SRC: Print category breakdown
if ($cattable) {
echo '<br/><center>'.$strcategories.'</center>';
print_table($cattable);
}
- Create a new file in \lang\en_utf8\help\quiz named showcategorygrades.html. This is the help file for this setting.
<p align="center"><b>Show Category Grades</b></p>
<p>If you enable this option, a question category grade breakdown will be displayed on the quiz results page.</p>
<p>Categories will be listed in the order they appear in the quiz.</p>
<p>The intention is to provide students with more detailed feedback regarding their quiz performance.</p>