Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
mod_quiz\grade_calculator Class Reference

Public Member Functions

 compute_grade_item_totals (question_usage_by_activity $quba)
 Compute the grade and maximum for each item, for an attempt where the question_usage_by_activity is available.
 
 compute_grade_item_totals_for_attempts (array $qubaids)
 Compute the grade and maximum for each item, for some attempts where we only have the usage ids.
 
 get_grade_items ()
 Get the extra grade items for this quiz.
 
 load_grade_item_totals (qubaid_condition $qubaids)
 Query the database return the total mark for each grade item for a set of attempts.
 
 recompute_all_attempt_sumgrades ()
 Update the sumgrades field of attempts at this quiz.
 
 recompute_all_final_grades ()
 Update the final grade at this quiz for all students.
 
 recompute_final_grade (?int $userid=null, array $attempts=[])
 Update the final grade at this quiz for a particular student.
 
 recompute_quiz_sumgrades ()
 Update the sumgrades field of the quiz.
 
 set_slots (array $slots)
 Lets other code pass in the slot information, so it does note have to be re-loaded from the DB.
 
 update_quiz_maximum_grade (float $newgrade)
 Update the quiz setting for the grade the quiz is out of.
 

Static Public Member Functions

static create (quiz_settings $quizobj)
 Factory.
 

Public Attributes

float const ALMOST_ZERO = 0.000005
 a number that is effectively zero.
 

Protected Member Functions

 __construct (quiz_settings $quizobj)
 Constructor.
 
 compute_final_grade_from_attempts (array $attempts)
 Calculate the overall grade for a quiz given a number of attempts by a particular user.
 
 ensure_grade_items_loaded ()
 Ensure the {.
 
 ensure_slots_loaded ()
 Ensure the {.
 

Protected Attributes

stdClass[] null array $gradeitems = null
 quiz_grade_items for this quiz indexed by id, sorted by sortorder, with a maxmark field added.
 
quiz_settings $quizobj
 the quiz for which this instance computes grades.
 
stdClass[] null array $slots = null
 quiz_slot for this quiz.
 

Constructor & Destructor Documentation

◆ __construct()

mod_quiz\grade_calculator::__construct ( quiz_settings $quizobj)
protected

Constructor.

Recommended way to get an instance is $quizobj->get_grade_calculator();

Parameters
quiz_settings$quizobj

Member Function Documentation

◆ compute_final_grade_from_attempts()

mod_quiz\grade_calculator::compute_final_grade_from_attempts ( array $attempts)
protected

Calculate the overall grade for a quiz given a number of attempts by a particular user.

Parameters
array$attemptsan array of all the user's attempts at this quiz in order.
Return values
float|nullthe overall grade, or null if the user does not have a grade.

◆ compute_grade_item_totals()

mod_quiz\grade_calculator::compute_grade_item_totals ( question_usage_by_activity $quba)

Compute the grade and maximum for each item, for an attempt where the question_usage_by_activity is available.

Parameters
question_usage_by_activity$qubausage for the quiz attempt we want to calculate the grades of.
Return values
grade_out_of[]the grade for each item where the total grade is not zero. ->name will be set to the grade item name. Must be output through {
See also
format_string()}.

◆ compute_grade_item_totals_for_attempts()

mod_quiz\grade_calculator::compute_grade_item_totals_for_attempts ( array $qubaids)

Compute the grade and maximum for each item, for some attempts where we only have the usage ids.

Parameters
int[]$qubaidsarray of usage ids.
Return values
grade_out_of[][]question_usage.id => array of grade_out_of. ->name will be set to the grade item name. Must be output through {
See also
format_string()}..

◆ create()

static mod_quiz\grade_calculator::create ( quiz_settings $quizobj)
static

Factory.

The recommended way to get an instance is $quizobj->get_grade_calculator();

Parameters
quiz_settings$quizobjsettings of a quiz.
Return values
grade_calculatorinstance of this class for the given quiz.

◆ ensure_grade_items_loaded()

mod_quiz\grade_calculator::ensure_grade_items_loaded ( )
protected

Ensure the {.

See also
grade_calculator\$gradeitems} field is ready to use.

◆ ensure_slots_loaded()

mod_quiz\grade_calculator::ensure_slots_loaded ( )
protected

Ensure the {.

See also
$gradeitems} field is ready to use.

◆ get_grade_items()

mod_quiz\grade_calculator::get_grade_items ( )

Get the extra grade items for this quiz.

Returned objects have fields ->id, ->quizid, ->sortorder, ->name and maxmark.

Return values
stdClass[]the grade items for this quiz.

◆ load_grade_item_totals()

mod_quiz\grade_calculator::load_grade_item_totals ( qubaid_condition $qubaids)

Query the database return the total mark for each grade item for a set of attempts.

Parameters
qubaid_condition$qubaidswhich question_usages to computer the total marks for.
Return values
float[][]Array question_usage.id => quiz_grade_item.id => mark.

◆ recompute_all_final_grades()

mod_quiz\grade_calculator::recompute_all_final_grades ( )

Update the final grade at this quiz for all students.

This function is equivalent to calling {

See also
recompute_final_grade()} for all users who have attempted the quiz, but is much more efficient.

◆ recompute_final_grade()

mod_quiz\grade_calculator::recompute_final_grade ( ?int $userid = null,
array $attempts = [] )

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 also
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.
Parameters
int | null$useridThe userid to calculate the grade for. Defaults to the current user.
array$attemptsif you already have this user's attempt records loaded, pass them here to save queries.

◆ recompute_quiz_sumgrades()

mod_quiz\grade_calculator::recompute_quiz_sumgrades ( )

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 also
quiz_delete_previews()} before you call this function.

◆ set_slots()

mod_quiz\grade_calculator::set_slots ( array $slots)

Lets other code pass in the slot information, so it does note have to be re-loaded from the DB.

Parameters
stdClass[]$slotsthe data from quiz_slots. The only required fields are ->slot and ->quizgradeitemid.

◆ update_quiz_maximum_grade()

mod_quiz\grade_calculator::update_quiz_maximum_grade ( float $newgrade)

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.

Parameters
float$newgradethe new maximum grade for the quiz.

Member Data Documentation

◆ $gradeitems

stdClass [] null array mod_quiz\grade_calculator::$gradeitems = null
protected

quiz_grade_items for this quiz indexed by id, sorted by sortorder, with a maxmark field added.

Lazy-loaded when needed. See {

See also
ensure_grade_items_loaded()}.

◆ $slots

stdClass [] null array mod_quiz\grade_calculator::$slots = null
protected

quiz_slot for this quiz.

Only ->slot and ->quizgradeitemid fields are used.

This is either set by another class that already has the data, using {

See also
set_slots()} or it is lazy-loaded when needed. See {
ensure_slots_loaded()}.

◆ ALMOST_ZERO

float const mod_quiz\grade_calculator::ALMOST_ZERO = 0.000005

a number that is effectively zero.

Used to avoid division-by-zero or underflow problems.


The documentation for this class was generated from the following file: