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]

Question type class for the numerical question type.

Copyright: 1999 onwards Martin Dougiamas {@link http://moodle.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 759 lines (28 kb)
Included or required:0 times
Referenced: 2 times
Includes or requires: 0 files

Defines 2 classes


Class: qtype_numerical  - X-Ref

The numerical question type class.

This class contains some special features in order to make the
question type embeddable within a multianswer (cloze) question

is_valid_number(string $x)   X-Ref
Validate that a string is a number formatted correctly for the current locale.

param: string $x a string
return: bool whether $x is a number that the numerical question type can interpret.

get_question_options($question)   X-Ref
No description

get_numerical_units(&$question)   X-Ref
No description

get_default_numerical_unit($question)   X-Ref
No description

get_numerical_options($question)   X-Ref
No description

save_defaults_for_new_questions(stdClass $fromform)   X-Ref
No description

save_question_options($question)   X-Ref
Save the units and the answers associated with this question.


save_unit_options($question)   X-Ref
The numerical options control the display and the grading of the unit
part of the numerical question and related types (calculateds)
Questions previous to 2.0 do not have this table as multianswer questions
in all versions including 2.0. The default values are set to give the same grade
as old question.


save_units($question)   X-Ref
No description

initialise_question_instance(question_definition $question, $questiondata)   X-Ref
No description

initialise_numerical_answers(question_definition $question, $questiondata)   X-Ref
No description

make_answer_processor($units, $unitsleft)   X-Ref
No description

delete_question($questionid, $contextid)   X-Ref
No description

get_random_guess_score($questiondata)   X-Ref
No description

add_unit($questiondata, $answer, $unit = null)   X-Ref
Add a unit to a response for display.

param: object $questiondata the data defining the quetsion.
param: string $answer a response.
param: object $unit a unit. If null, {@link get_default_numerical_unit()}

get_possible_responses($questiondata)   X-Ref
No description

apply_unit($rawresponse, $units, $unitsleft)   X-Ref
Checks if the $rawresponse has a unit and applys it if appropriate.

param: string $rawresponse  The response string to be converted to a float.
param: array $units         An array with the defined units, where the
return: float               The rawresponse with the unit taken into

move_files($questionid, $oldcontextid, $newcontextid)   X-Ref
No description

delete_files($questionid, $contextid)   X-Ref
No description

Class: qtype_numerical_answer_processor  - X-Ref

This class processes numbers with units.

__construct($units, $unitsbefore = false, $decsep = null,$thousandssep = null)   X-Ref
No description

set_characters($decsep, $thousandssep)   X-Ref
Set the decimal point and thousands separator character that should be used.

param: string $decsep
param: string $thousandssep

get_point()   X-Ref
No description

get_separator()   X-Ref
No description

contains_thousands_seaparator($value)   X-Ref

return: bool If the student's response contains a '.' or a ',' that

build_regex()   X-Ref
Create the regular expression that {@link parse_response()} requires.

return: string

parse_response($response)   X-Ref
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.
param: string $response a value, optionally with a unit.
return: array four strings (some of which may be blank) the digits before

apply_units($response, $separateunit = null)   X-Ref
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.

param: string $response a value, optionally with a unit.
return: array(numeric, string, multiplier) the value with the unit stripped, and normalised

get_default_unit()   X-Ref

return: string the default unit.

add_unit($answer, $unit = null)   X-Ref

param: string $answer a response.
param: string $unit a unit.

is_known_unit($unit)   X-Ref
Is this unit recognised.

param: string $unit the unit
return: bool whether this is a unit we recognise.

are_units_before()   X-Ref
Whether the units go before or after the number.

return: true = before, false = after.

get_unit_options()   X-Ref
Get the units as an array suitably for passing to html_writer::select.

return: array of unit choices.