Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
qtype_numerical_answer_processor Class Reference

This class processes numbers with units. More...

Public Member Functions

 __construct ($units, $unitsbefore=false, $decsep=null, $thousandssep=null)
 
 add_unit ($answer, $unit=null)
 
 apply_units ($response, $separateunit=null)
 Takes a number in almost any localised form, and possibly with a unit after it.
 
 are_units_before ()
 Whether the units go before or after the number.
 
 contains_thousands_seaparator ($value)
 
 get_default_unit ()
 
 get_point ()
 
 get_separator ()
 
 get_unit_options ()
 Get the units as an array suitably for passing to html_writer\select.
 
 is_known_unit ($unit)
 Is this unit recognised.
 
 set_characters ($decsep, $thousandssep)
 Set the decimal point and thousands separator character that should be used.
 

Protected Member Functions

 build_regex ()
 Create the regular expression that parse_response() requires.
 
 parse_response ($response)
 This method can be used for more locale-strict parsing of repsonses.
 

Protected Attributes

string $decsep
 character used as decimal point.
 
 $regex = null
 
string $thousandssep
 character used as thousands separator.
 
array $units
 unit name => multiplier.
 
boolean $unitsbefore
 whether the units come before or after the number.
 

Detailed Description

This class processes numbers with units.

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

Member Function Documentation

◆ add_unit()

qtype_numerical_answer_processor::add_unit ( $answer,
$unit = null )
Parameters
string$answera response.
string$unita unit.

◆ apply_units()

qtype_numerical_answer_processor::apply_units ( $response,
$separateunit = null )

Takes a number in almost any localised form, and possibly with a unit after it.

It separates off the unit, if present, and converts to the default unit, by using the given unit multiplier.

Parameters
string$responsea value, optionally with a unit.
Return values
array(numeric,string,multiplier)the value with the unit stripped, and normalised by the unit multiplier, if any, and the unit string, for reference.

◆ are_units_before()

qtype_numerical_answer_processor::are_units_before ( )

Whether the units go before or after the number.

Return values
true= before, false = after.

◆ build_regex()

qtype_numerical_answer_processor::build_regex ( )
protected

Create the regular expression that parse_response() requires.

Return values
string

◆ contains_thousands_seaparator()

qtype_numerical_answer_processor::contains_thousands_seaparator ( $value)
Return values
boolIf the student's response contains a '.' or a ',' that matches the thousands separator in the current locale. In this case, the parsing in apply_unit can give a result that the student did not expect.

◆ get_default_unit()

qtype_numerical_answer_processor::get_default_unit ( )
Return values
stringthe default unit.

◆ get_point()

qtype_numerical_answer_processor::get_point ( )
Return values
stringthe decimal point character used.

◆ get_separator()

qtype_numerical_answer_processor::get_separator ( )
Return values
stringthe thousands separator character used.

◆ get_unit_options()

qtype_numerical_answer_processor::get_unit_options ( )

Get the units as an array suitably for passing to html_writer\select.

Return values
arrayof unit choices.

◆ is_known_unit()

qtype_numerical_answer_processor::is_known_unit ( $unit)

Is this unit recognised.

Parameters
string$unitthe unit
Return values
boolwhether this is a unit we recognise.

◆ parse_response()

qtype_numerical_answer_processor::parse_response ( $response)
protected

This method can be used for more locale-strict parsing of repsonses.

At the moment we don't use it, and instead use the more lax parsing in apply_units. This is just a note that this funciton was used in the past, so if you are intersted, look through version control history.

Take a string which is a number with or without a decimal point and exponent, and possibly followed by one of the units, and split it into bits.

Parameters
string$responsea value, optionally with a unit.
Return values
arrayfour strings (some of which may be blank) the digits before and after the decimal point, the exponent, and the unit. All four will be null if the response cannot be parsed.

◆ set_characters()

qtype_numerical_answer_processor::set_characters ( $decsep,
$thousandssep )

Set the decimal point and thousands separator character that should be used.

Parameters
string$decsep
string$thousandssep

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