Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
qtype_calculated_variable_substituter Class Reference

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

Public Member Functions

 __construct (array $values, $decimalpoint)
 Constructor. More...
 
 calculate ($expression)
 Evaluate an expression using the variable values. More...
 
 format_float ($x, $length=null, $format=null)
 Display a float properly formatted with a certain number of decimal places. More...
 
 get_values ()
 Return an array of the variables and their values. More...
 
 replace_expressions_in_text ($text, $length=null, $format=null)
 Replace any embedded variables (like {a}) or formulae (like {={a} + {b}}) in some text with the corresponding values. More...
 

Protected Member Functions

 calculate_raw ($expression)
 Evaluate an expression after the variable values have been substituted. More...
 
 substitute_values_for_eval ($expression)
 Substitute variable placehodlers like {a} with their value wrapped in (). More...
 
 substitute_values_pretty ($text)
 Substitute variable placehodlers like {a} with their value without wrapping the value in anything. More...
 

Protected Attributes

string $decimalpoint
 character to use for the decimal point in displayed numbers.
 
array $prettyvalue
 variable values, with negative numbers wrapped in (...). More...
 
array $safevalue
 variable values, with negative numbers wrapped in (...). More...
 
array $search
 variable names wrapped in {...}. More...
 
array $values
 variable name => value
 

Detailed Description

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.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

qtype_calculated_variable_substituter::__construct ( array  $values,
  $decimalpoint 
)

Constructor.

Parameters
array$valuesvariable name => value.

Member Function Documentation

◆ calculate()

qtype_calculated_variable_substituter::calculate (   $expression)

Evaluate an expression using the variable values.

Parameters
string$expressionthe expression. A PHP expression with placeholders like {a} for where the variables need to go.
Return values
floatthe computed result.

◆ calculate_raw()

qtype_calculated_variable_substituter::calculate_raw (   $expression)
protected

Evaluate an expression after the variable values have been substituted.

Parameters
string$expressionthe expression. A PHP expression with placeholders like {a} for where the variables need to go.
Return values
floatthe computed result.

◆ format_float()

qtype_calculated_variable_substituter::format_float (   $x,
  $length = null,
  $format = null 
)

Display a float properly formatted with a certain number of decimal places.

Parameters
number$xthe number to format
int$lengthrestrict to this many decimal places or significant figures. If null, the number is not rounded.
intformat 1 => decimalformat, 2 => significantfigures.
Return values
stringformtted number.

◆ get_values()

qtype_calculated_variable_substituter::get_values ( )

Return an array of the variables and their values.

Return values
arrayname => value.

◆ replace_expressions_in_text()

qtype_calculated_variable_substituter::replace_expressions_in_text (   $text,
  $length = null,
  $format = null 
)

Replace any embedded variables (like {a}) or formulae (like {={a} + {b}}) in some text with the corresponding values.

Parameters
string$textthe text to process.
Return values
stringthe text with values substituted.

◆ substitute_values_for_eval()

qtype_calculated_variable_substituter::substitute_values_for_eval (   $expression)
protected

Substitute variable placehodlers like {a} with their value wrapped in ().

Parameters
string$expressionthe expression. A PHP expression with placeholders like {a} for where the variables need to go.
Return values
stringthe expression with each placeholder replaced by the corresponding value.

◆ substitute_values_pretty()

qtype_calculated_variable_substituter::substitute_values_pretty (   $text)
protected

Substitute variable placehodlers like {a} with their value without wrapping the value in anything.

Parameters
string$textsome content with placeholders like {a} for where the variables need to go.
Return values
stringthe expression with each placeholder replaced by the corresponding value.

Member Data Documentation

◆ $prettyvalue

array qtype_calculated_variable_substituter::$prettyvalue
protected

variable values, with negative numbers wrapped in (...).

Used by substitute_values().

◆ $safevalue

array qtype_calculated_variable_substituter::$safevalue
protected

variable values, with negative numbers wrapped in (...).

Used by substitute_values().

◆ $search

array qtype_calculated_variable_substituter::$search
protected

variable names wrapped in {...}.

Used by substitute_values().


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