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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

Calculated question definition class.

Copyright: 2011 The Open University
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 497 lines (19 kb)
Included or required:0 times
Referenced: 2 times
Includes or requires: 0 files

Defines 5 classes

qtype_calculated_question:: (6 methods):
  start_attempt()
  apply_attempt_state()
  calculate_all_expressions()
  get_num_variants()
  get_variants_selection_seed()
  get_correct_response()

qtype_calculated_question_with_expressions:: (1 method):
  calculate_all_expressions()

qtype_calculated_question_helper:: (2 methods):
  start_attempt()
  apply_attempt_state()

qtype_calculated_dataset_loader:: (5 methods):
  __construct()
  get_number_of_items()
  load_values()
  get_values()
  datasets_are_synchronised()

qtype_calculated_variable_substituter:: (8 methods):
  __construct()
  format_float()
  get_values()
  calculate()
  calculate_raw()
  substitute_values_for_eval()
  substitute_values_pretty()
  replace_expressions_in_text()


Class: qtype_calculated_question  - X-Ref

Represents a calculated question.

start_attempt(question_attempt_step $step, $variant)   X-Ref


apply_attempt_state(question_attempt_step $step)   X-Ref
No description

calculate_all_expressions()   X-Ref
No description

get_num_variants()   X-Ref
No description

get_variants_selection_seed()   X-Ref
No description

get_correct_response()   X-Ref
No description

Interface: qtype_calculated_question_with_expressions  - X-Ref

This interface defines the method that a quetsion type must implement if it
is to work with {@link qtype_calculated_question_helper}.

As well as this method, the class that implements this interface must have
fields
public $datasetloader; // of type qtype_calculated_dataset_loader
public $vs; // of type qtype_calculated_variable_substituter

calculate_all_expressions()   X-Ref
Replace all the expression in the question definition with the values
computed from the selected dataset by calling $this->vs->calculate() and
$this->vs->replace_expressions_in_text() on the parts of the question
that require it.


Class: qtype_calculated_question_helper  - X-Ref

Helper class for questions that use datasets. Works with the interface
{@link qtype_calculated_question_with_expressions} and the class
{@link qtype_calculated_dataset_loader} to set up the value of each variable
in start_attempt, and restore that in apply_attempt_state.

start_attempt(qtype_calculated_question_with_expressions $question,question_attempt_step $step, $variant)   X-Ref
No description

apply_attempt_state(qtype_calculated_question_with_expressions $question, question_attempt_step $step)   X-Ref
No description

Class: qtype_calculated_dataset_loader  - X-Ref

This class is responsible for loading the dataset that a question needs from
the database.

__construct($questionid)   X-Ref
Constructor

param: int $questionid the question to load datasets for.

get_number_of_items()   X-Ref
Get the number of items (different values) in each dataset used by this
question. This is the minimum number of items in any dataset used by this
question.

return: int the number of items available.

load_values($itemnumber)   X-Ref
Actually query the database for the values.

param: int $itemnumber which set of values to load.
return: array name => value;

get_values($itemnumber)   X-Ref
Load a particular set of values for each dataset used by this question.

param: int $itemnumber which set of values to load.
return: array name => value.

datasets_are_synchronised($category)   X-Ref
No description

Class: qtype_calculated_variable_substituter  - X-Ref

This class holds the current values of all the variables used by a calculated
question.

It can compute formulae using those values, and can substitute equations
embedded in text.

__construct(array $values, $decimalpoint)   X-Ref
Constructor

param: array $values variable name => value.

format_float($x, $length = null, $format = null)   X-Ref
Display a float properly formatted with a certain number of decimal places.

param: number $x the number to format
param: int $length restrict to this many decimal places or significant
param: int format 1 => decimalformat, 2 => significantfigures.
return: string formtted number.

get_values()   X-Ref
Return an array of the variables and their values.

return: array name => value.

calculate($expression)   X-Ref
Evaluate an expression using the variable values.

param: string $expression the expression. A PHP expression with placeholders
return: float the computed result.

calculate_raw($expression)   X-Ref
Evaluate an expression after the variable values have been substituted.

param: string $expression the expression. A PHP expression with placeholders
return: float the computed result.

substitute_values_for_eval($expression)   X-Ref
Substitute variable placehodlers like {a} with their value wrapped in ().

param: string $expression the expression. A PHP expression with placeholders
return: string the expression with each placeholder replaced by the

substitute_values_pretty($text)   X-Ref
Substitute variable placehodlers like {a} with their value without wrapping
the value in anything.

param: string $text some content with placeholders
return: string the expression with each placeholder replaced by the

replace_expressions_in_text($text, $length = null, $format = null)   X-Ref
Replace any embedded variables (like {a}) or formulae (like {={a} + {b}})
in some text with the corresponding values.

param: string $text the text to process.
return: string the text with values substituted.