I've been looking at ways for quizzes to give multiple grades. For example, I want to implement a simplified personality test which tells you how creative, numerical, analytical, etc you are.
As far as I can see, quizzes currently only give a single grade, like 83%. And I would like them to give multiple grades, like 80% creative, 40% numerical, etc.
I found a simplish way of doing this, by computing grades by question category. So the administrator could create Creative and Numerical categories, and put questions from both categories onto the same quiz. When the quiz results are comuputed it will compute separate results on a category-by-category basis. A sample set of results is shown in the attached image.
So my questions are: Is there a built-in way for Moodle to already do this? If not, can anyone see any problems with the method I've adopted?
To test out this method, you'll need to a new field in the quiz_attempts table:
ALTER TABLE `mdl_quiz_attempts` ADD `categorygrades` VARCHAR( 255 ) NOT NULL AFTER `sumgrades`
And some changes to the code between the indicated bold lines in mod/quiz/attempt.php (around like 294):
$questionidarray = explode(',', $questionids);
$catraw = $catmax = $catperc = array(); // blank arrays to store category information
foreach($questionidarray as $i) {
if (!isset($actions[$i])) {
$actions[$i]->responses = array('' => '');
}
$actions[$i]->timestamp = $timestamp;
quiz_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt);
if (!isset ($catraw[$questions[$i]->category])) { // if this category has not been seen before
$catraw[$questions[$i]->category] = 0; // set the raw total to 0
$catmax[$questions[$i]->category] = 0; // set the maximum possible points to 0
}
$catraw[$questions[$i]->category] += $states[$i]->raw_grade; // add to the raw grade achieved for this category
$catmax[$questions[$i]->category] += $questions[$i]->maxgrade; // add to the max grade possible
quiz_save_question_session($questions[$i], $states[$i]);
}
foreach ($catraw as $categoryid=>$categorygrade) { // for each category involved in this quiz
if ($category = get_record('quiz_categories', 'id', $categoryid)) { // look up the category name
array_push ($catperc, $category->name.': '.round ($categorygrade/$catmax[$categoryid]*100).'%');
}
}
sort ($catperc); // sort the categories by key
$attempt->categorygrades = join ("\n", $catperc); // put this information in the new categorygrades field
$attempt->timemodified = $timestamp;
Displaying the results required a change to mod/quiz/review.php around line 178:
$table->data[] = array("$strgrade:", $grade.get_string('outof', 'quiz').$quiz->grade.nl2br("\n".$attempt->categorygrades));
Thank you,
Paul

Just yesterday I created a quiz and was wishing I could show scale scores.
I was hoping to be able to add a title to the scale scores. However, I cannot get the line break to work. Any ideas on that? Here's the line I added to review.php (without the break):
$table->data[] = array(get_string('catgrade', 'quiz'), "\n".$attempt->categorygrades);
and here's a screenshot:
the \n will produce a new line in the source html but you need <br /> to have new lines on the page as displayed in the browser. Hence I would try
$table->data[] = array(get_string('catgrade', 'quiz'), "<br />\n".$attempt->categorygrades);
Ah, I see that Paul is doing this using the function nl2br, which inserts <br /> for you
http://php.net/nl2br
perhaps
$table->data[] = array(get_string('catgrade', 'quiz'), nl2br("\n".$attempt->categorygrades));
might work too?
Hi Chardelle,
Thank you - that's a nice way to begin Monday morning. And I'm glad you were able to resolve the newline problem.
I thought it better to use new lines in the actual data field, and just convert them to <br/>'s when the data was displayed. In case the data ever needs to be displayed in a non-HTML context too.
Hi, I was looking for such a solution and came across this solution. I tried what Paul has mentioned but found few problems.
1. Paul's code has the function quiz_save_question_session and whereas in attempt.php its save_question_session. If i use paul's function, the quiz module breaks when i try to do a test. I also tried to use paul's code and just replace the function name with the original name, in this case the test works fine but i see nothing inserted in the database. I have added the field as mentioned by paul in the table.
2. I have also put the relevanr code in review.php but cannot see any output as data didnt got inserted in db for that new field.
Since, you got it working, i would appreciate what exactly you did so that i can test and make it working.
Thanks in advacne.
just got this feature request on Friday and stumbled on your code.
Awesome!
Thanks,
Tony
Hi again Paul,
There are a couple of things that need some tweaking and I hope to get it all straightened out since I think this is a great option to have for quizzes.
1. Your category grade % code in attempt.php is not handling a "description" question. If a description is added, I get the Warning: division by zero error message.
2. The code is also not compatible with pagination. Which is really a shame since this feature is nice to have with long quizzes. If I paginate the quiz, then only the scores for the categories appearing on the last page of the quiz get shown.
Any ideas on how to fix?
Thanks,
Chardelle
Hi Chardelle,
The first issue should be easy to resolve. The second will take more time, as I'll have to learn how quizzes work better. But I will try and implement both over the holidays.
1) I think the division by zero error comes from when it's computing the percentage. So you can replace 7th line from the bottom with (note that I haven't tested this, but I think it should work):
if (($category=get_record('quiz_categories', 'id', $categoryid)) && $catmax[$categoryid]>0) { // look up the category name
2) I guess that the quiz results are only computed on a page by page basis. So I'll have to either keep a running total of the results-by-category, or loop over the whole quiz when a single page is submitted.
I'll keep you posted,
Kind regards,
Paul
Dear Paul
Great tool
I have implemented. For now it only works if I give a grade to the student. If I select no grade it seems to get lost somewhere. Maybe a problem on my site. All and all I like it.
Frank
After "Submit & Save" or "Submit All" it displays blank page. Looking at the error log, it says "Call to undefined function quiz_process_responses()"
I changed it to question_process_responses()
Also error on "Call to undefined function quiz_save_question_session()"
I changed it to "save_question_session()"
Because both were referrenced in the code prior to this change.
Now it shows results but without grouping by category code. Seems like there is no change in the code. I changed the table as suggested as well as review.php
Kindly guide.
If some one who can do that I am even ready for pay for it.
Regards,
Sunita
What is the same process for moodle 2.0?
I can't find it.
I can not find multiple grades in one quiz in moodle 2.0 .
What is the procedure to add more than one grade types in one quiz?
Can anybody help me?