Moodle APIs 3.9
Moodle 3.9.13+ (Build: 20220325)
Static Public Member Functions | Public Attributes | Static Protected Member Functions | List of all members
question_utils Class Reference

Useful functions for writing question types and behaviours. More...

Static Public Member Functions

static arrays_have_same_keys_and_values (array $array1, array $array2)
 Tests to see whether two arrays have the same keys, with the same values (as compared by ===) for each key. More...
 
static arrays_same_at_key (array $array1, array $array2, $key)
 Tests to see whether two arrays have the same value at a particular key. More...
 
static arrays_same_at_key_integer (array $array1, array $array2, $key)
 Tests to see whether two arrays have the same value at a particular key. More...
 
static arrays_same_at_key_missing_is_blank (array $array1, array $array2, $key)
 Tests to see whether two arrays have the same value at a particular key. More...
 
static clean_param_mark ($mark)
 Typically, $mark will have come from optional_param($name, null, PARAM_RAW_TRIMMED). More...
 
static get_editor_options ($context)
 Get editor options for question related text areas. More...
 
static get_filepicker_options ($context, $draftitemid)
 Get filepicker options for question related text areas. More...
 
static int_to_letter ($number)
 Convert an integer to a letter of alphabet. More...
 
static int_to_roman ($number)
 Convert an integer to roman numerals. More...
 
static optional_param_mark ($parname)
 Get a sumitted variable (from the GET or POST data) that is a mark. More...
 
static to_plain_text ($text, $format, $options=array('noclean'=> 'true'))
 Convert part of some question content to plain text. More...
 

Public Attributes

float const MARK_TOLERANCE = 0.00000005
 tolerance to use when comparing question mark/fraction values. More...
 

Static Protected Member Functions

static specific_filepicker_options ($acceptedtypes, $draftitemid, $context)
 Get the options required to configure the filepicker for one of the editor toolbar buttons. More...
 

Detailed Description

Useful functions for writing question types and behaviours.

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

Member Function Documentation

◆ arrays_have_same_keys_and_values()

static question_utils::arrays_have_same_keys_and_values ( array  $array1,
array  $array2 
)
static

Tests to see whether two arrays have the same keys, with the same values (as compared by ===) for each key.

However, the order of the arrays does not have to be the same.

Parameters
array$array1the first array.
array$array2the second array.
Return values
boolwhether the two arrays have the same keys with the same corresponding values.

◆ arrays_same_at_key()

static question_utils::arrays_same_at_key ( array  $array1,
array  $array2,
  $key 
)
static

Tests to see whether two arrays have the same value at a particular key.

This method will return true if:

  1. Neither array contains the key; or
  2. Both arrays contain the key, and the corresponding values compare identical when cast to strings and compared with ===.
    Parameters
    array$array1the first array.
    array$array2the second array.
    string$keyan array key.
    Return values
    boolwhether the two arrays have the same value (or lack of one) for a given key.

◆ arrays_same_at_key_integer()

static question_utils::arrays_same_at_key_integer ( array  $array1,
array  $array2,
  $key 
)
static

Tests to see whether two arrays have the same value at a particular key.

Missing values are replaced by 0, and then the values are cast to integers and compared with ===.

Parameters
array$array1the first array.
array$array2the second array.
string$keyan array key.
Return values
boolwhether the two arrays have the same value (or lack of one) for a given key.

◆ arrays_same_at_key_missing_is_blank()

static question_utils::arrays_same_at_key_missing_is_blank ( array  $array1,
array  $array2,
  $key 
)
static

Tests to see whether two arrays have the same value at a particular key.

Missing values are replaced by '', and then the values are cast to strings and compared with ===.

Parameters
array$array1the first array.
array$array2the second array.
string$keyan array key.
Return values
boolwhether the two arrays have the same value (or lack of one) for a given key.

◆ clean_param_mark()

static question_utils::clean_param_mark (   $mark)
static

Typically, $mark will have come from optional_param($name, null, PARAM_RAW_TRIMMED).

This method copes with:

  • keeping null or '' input unchanged - important to let teaches set a question back to requries grading.
  • numbers that were typed as either 1.00 or 1,00 form.
  • invalid things, which get turned into null.
Parameters
string | null$markraw use input of a mark.
Return values
float|string|nullcleaned mark as a float if possible. Otherwise '' or null.

◆ get_editor_options()

static question_utils::get_editor_options (   $context)
static

Get editor options for question related text areas.

Parameters
object$contextthe context.
Return values
arrayAn array of options

◆ get_filepicker_options()

static question_utils::get_filepicker_options (   $context,
  $draftitemid 
)
static

Get filepicker options for question related text areas.

Parameters
object$contextthe context.
int$draftitemidthe draft area item id.
Return values
arrayAn array of options

◆ int_to_letter()

static question_utils::int_to_letter (   $number)
static

Convert an integer to a letter of alphabet.

Parameters
int$numberan integer between 1 and 26 inclusive. Anything else will throw an exception.
Return values
stringthe number converted to upper case letter of alphabet.

◆ int_to_roman()

static question_utils::int_to_roman (   $number)
static

Convert an integer to roman numerals.

Parameters
int$numberan integer between 1 and 3999 inclusive. Anything else will throw an exception.
Return values
stringthe number converted to lower case roman numerals.

◆ optional_param_mark()

static question_utils::optional_param_mark (   $parname)
static

Get a sumitted variable (from the GET or POST data) that is a mark.

Parameters
string$parnamethe submitted variable name.
Return values
float|string|nullcleaned mark as a float if possible. Otherwise '' or null.

◆ specific_filepicker_options()

static question_utils::specific_filepicker_options (   $acceptedtypes,
  $draftitemid,
  $context 
)
staticprotected

Get the options required to configure the filepicker for one of the editor toolbar buttons.

Parameters
mixed$acceptedtypesarray of types of '*'.
int$draftitemidthe draft area item id.
object$contextthe context.
Return values
objectthe required options.

◆ to_plain_text()

static question_utils::to_plain_text (   $text,
  $format,
  $options = array('noclean' => 'true') 
)
static

Convert part of some question content to plain text.

Parameters
string$textthe text.
int$formatthe text format.
array$optionsformatting options. Passed to format_text.
Return values
float|string|nullcleaned mark as a float if possible. Otherwise '' or null.

Member Data Documentation

◆ MARK_TOLERANCE

float const question_utils::MARK_TOLERANCE = 0.00000005

tolerance to use when comparing question mark/fraction values.

When comparing floating point numbers in a computer, the representation is not necessarily exact. Therefore, we need to allow a tolerance. Question marks are stored in the database as decimal numbers with 7 decimal places. Therefore, this is the appropriate tolerance to use.


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