Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

(no description)

File Size: 444 lines (17 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: grade_calculator  - X-Ref

This class contains all the logic for computing the grade of a quiz.

There are two sorts of calculation which need to be done. For a single
attempt, we need to compute the total attempt score from score for each question.
And for a quiz user, we need to compute the final grade from all the separate attempt grades.

__construct(quiz_settings $quizobj)   X-Ref
Constructor. Recommended way to get an instance is $quizobj->get_grade_calculator();

param: quiz_settings $quizobj

create(quiz_settings $quizobj)   X-Ref
Factory. The recommended way to get an instance is $quizobj->get_grade_calculator();

param: quiz_settings $quizobj settings of a quiz.
return: grade_calculator instance of this class for the given quiz.

recompute_quiz_sumgrades()   X-Ref
Update the sumgrades field of the quiz.

This needs to be called whenever the grading structure of the quiz is changed.
For example if a question is added or removed, or a question weight is changed.

You should call {@see quiz_delete_previews()} before you call this function.

recompute_all_attempt_sumgrades()   X-Ref
Update the sumgrades field of attempts at this quiz.


recompute_final_grade(?int $userid = null, array $attempts = [])   X-Ref
Update the final grade at this quiz for a particular student.

That is, given the quiz settings, and all the attempts this user has made,
compute their final grade for the quiz, as shown in the gradebook.

The $attempts parameter is for efficiency. If you already have the data for
all this user's attempts loaded (for example from {@see quiz_get_user_attempts()}
or because you are looping through a large recordset fetched in one efficient query,
then you can pass that data here to save DB queries.

param: int|null $userid The userid to calculate the grade for. Defaults to the current user.
param: array $attempts if you already have this user's attempt records loaded, pass them here to save queries.

compute_final_grade_from_attempts(array $attempts)   X-Ref
Calculate the overall grade for a quiz given a number of attempts by a particular user.

param: array $attempts an array of all the user's attempts at this quiz in order.
return: float|null the overall grade, or null if the user does not have a grade.

recompute_all_final_grades()   X-Ref
Update the final grade at this quiz for all students.

This function is equivalent to calling {@see recompute_final_grade()} for all
users who have attempted the quiz, but is much more efficient.

update_quiz_maximum_grade(float $newgrade)   X-Ref
Update the quiz setting for the grade the quiz is out of.

This function will update the data in quiz_grades and quiz_feedback, and
pass the new grades on to the gradebook.

param: float $newgrade the new maximum grade for the quiz.