Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

Question type class for the calculated 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: 2045 lines (89 kb)
Included or required:0 times
Referenced: 3 times
Includes or requires: 0 files

Defines 1 class

qtype_calculated:: (54 methods):
  get_question_options()
  get_datasets_for_export()
  save_question_options()
  import_datasets()
  initialise_question_instance()
  finished_edit_wizard()
  wizardpagesnumber()
  print_next_wizard_page()
  next_wizard_form()
  display_question_editing_page()
  validate_text()
  validate_answer()
  validate_question_data()
  clean_technical_prefix_from_question_name()
  preparedatasets()
  addnamecategory()
  save_question()
  delete_question()
  get_random_guess_score()
  supports_dataset_item_generation()
  custom_generator_tools_part()
  custom_generator_set_data()
  custom_generator_tools()
  update_dataset_options()
  save_question_calculated()
  get_database_dataset_items()
  save_dataset_items()
  generate_dataset_item()
  comment_header()
  comment_on_datasetitems()
  tolerance_types()
  dataset_options()
  construct_dataset_menus()
  substitute_variables()
  evaluate_equations()
  substitute_variables_and_eval()
  get_dataset_definitions()
  save_dataset_definitions()
  save_as_new_dataset_definitions()
  pick_question_dataset()
  dataset_options_from_database()
  find_dataset_names()
  find_formulas()
  get_dataset_definitions_category()
  print_dataset_definitions_category()
  get_short_question_name()
  print_dataset_definitions_category_shared()
  get_virtual_qtype()
  get_possible_responses()
  move_files()
  delete_files()
  qtype_calculated_calculate_answer()
  qtype_calculated_find_formula_errors()
  qtype_calculated_find_formula_errors_in_text()


Class: qtype_calculated  - X-Ref

The calculated question type.

get_question_options($question)   X-Ref


get_datasets_for_export($question)   X-Ref
No description

save_question_options($question)   X-Ref
No description

import_datasets($question)   X-Ref
No description

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

finished_edit_wizard($form)   X-Ref
No description

wizardpagesnumber()   X-Ref
No description

print_next_wizard_page($question, $form, $course)   X-Ref
No description

next_wizard_form($submiturl, $question, $wizardnow)   X-Ref
No description

display_question_editing_page($mform, $question, $wizardnow)   X-Ref
This method should be overriden if you want to include a special heading or some other
html on a question editing page besides the question editing form.

param: question_edit_form $mform a child of question_edit_form
param: object $question
param: string $wizardnow is '' for first page.

validate_text($text)   X-Ref
Verify that the equations in part of the question are OK.
We throw an exception here because this should have already been validated
by the form. This is just a last line of defence to prevent a question
being stored in the database if it has bad formulas. This saves us from,
for example, malicious imports.

param: string $text containing equations.

validate_answer($answer)   X-Ref
Verify that an answer is OK.
We throw an exception here because this should have already been validated
by the form. This is just a last line of defence to prevent a question
being stored in the database if it has bad formulas. This saves us from,
for example, malicious imports.

param: string $text containing equations.

validate_question_data($question)   X-Ref
Validate data before save.

param: stdClass $question data from the form / import file.

clean_technical_prefix_from_question_name($name)   X-Ref
Remove prefix #{..}# if exists.

param: $name a question name,
return: string the cleaned up question name.

preparedatasets($form, $questionfromid = '0')   X-Ref
This method prepare the $datasets in a format similar to dadatesetdefinitions_form.php
so that they can be saved
using the function save_dataset_definitions($form)
when creating a new calculated question or
when editing an already existing calculated question
or by  function save_as_new_dataset_definitions($form, $initialid)
when saving as new an already existing calculated question.

param: object $form
param: int $questionfromid default = '0'

addnamecategory(&$question)   X-Ref
No description

save_question($question, $form)   X-Ref
this version save the available data at the different steps of the question editing process
without using global $SESSION as storage between steps
at the first step $wizardnow = 'question'
when creating a new question
when modifying a question
when copying as a new question
the general parameters and answers are saved using parent::save_question
then the datasets are prepared and saved
at the second step $wizardnow = 'datasetdefinitions'
the datadefs final type are defined as private, category or not a datadef
at the third step $wizardnow = 'datasetitems'
the datadefs parameters and the data items are created or defined

param: object question
param: object $form
param: int $course
param: PARAM_ALPHA $wizardnow should be added as we are coming from question2.php

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

get_random_guess_score($questiondata)   X-Ref
No description

supports_dataset_item_generation()   X-Ref
No description

custom_generator_tools_part($mform, $idx, $j)   X-Ref
No description

custom_generator_set_data($datasetdefs, $formdata)   X-Ref
No description

custom_generator_tools($datasetdef)   X-Ref
No description

update_dataset_options($datasetdefs, $form)   X-Ref
No description

save_question_calculated($question, $fromform)   X-Ref
No description

get_database_dataset_items($definition)   X-Ref
This function get the dataset items using id as unique parameter and return an
array with itemnumber as index sorted ascendant
If the multiple records with the same itemnumber exist, only the newest one
i.e with the greatest id is used, the others are ignored but not deleted.
MDL-19210


save_dataset_items($question, $fromform)   X-Ref
No description

generate_dataset_item($options)   X-Ref
No description

comment_header($question)   X-Ref
No description

comment_on_datasetitems($qtypeobj, $questionid, $questiontext,$answers, $data, $number)   X-Ref
No description

tolerance_types()   X-Ref
No description

dataset_options($form, $name, $mandatory = true,$renameabledatasets = false)   X-Ref
No description

construct_dataset_menus($form, $mandatorydatasets,$optionaldatasets)   X-Ref
No description

substitute_variables($str, $dataset)   X-Ref
No description

evaluate_equations($str, $dataset)   X-Ref
No description

substitute_variables_and_eval($str, $dataset)   X-Ref
No description

get_dataset_definitions($questionid, $newdatasets)   X-Ref
No description

save_dataset_definitions($form)   X-Ref
No description

save_as_new_dataset_definitions($form, $initialid)   X-Ref
No description

pick_question_dataset($question, $datasetitem)   X-Ref
No description

dataset_options_from_database($form, $name, $prefix = '',$langfile = 'qtype_calculated')   X-Ref
No description

find_dataset_names($text)   X-Ref
Find the names of all datasets mentioned in a piece of question content like the question text.

param: $text the text to analyse.
return: array with dataset name for both key and value.

find_formulas($text)   X-Ref
Find all the formulas in a bit of text.

For example, called with "What is {a} plus {b}? (Hint, it is not {={a}*{b}}.)" this
returns ['{a}*{b}'].

param: $text text to analyse.
return: array where they keys an values are the formulas.

get_dataset_definitions_category($form)   X-Ref
This function retrieve the item count of the available category shareable
wild cards that is added as a comment displayed when a wild card with
the same name is displayed in datasetdefinitions_form.php


print_dataset_definitions_category($form)   X-Ref
This function build a table showing the available category shareable
wild cards, their name, their definition (Min, Max, Decimal) , the item count
and the name of the question where they are used.
This table is intended to be add before the question text to help the user use
these wild cards


get_short_question_name($stringtoshorten, $characterlimit)   X-Ref
This function shortens a question name if it exceeds the character limit.

param: string $stringtoshorten the string to be shortened.
param: int $characterlimit the character limit.
return: string

print_dataset_definitions_category_shared($question, $datasetdefsq)   X-Ref
This function build a table showing the available category shareable
wild cards, their name, their definition (Min, Max, Decimal) , the item count
and the name of the question where they are used.
This table is intended to be add before the question text to help the user use
these wild cards


get_virtual_qtype()   X-Ref
No description

get_possible_responses($questiondata)   X-Ref
No description

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

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

qtype_calculated_calculate_answer($formula, $individualdata,$tolerance, $tolerancetype, $answerlength, $answerformat = '1', $unit = '')   X-Ref
No description

qtype_calculated_find_formula_errors($formula)   X-Ref
Validate a forumula.

param: string $formula the formula to validate.
return: string|boolean false if there are no problems. Otherwise a string error message.

qtype_calculated_find_formula_errors_in_text($text)   X-Ref
Validate all the forumulas in a bit of text.

param: string $text the text in which to validate the formulas.
return: string|boolean false if there are no problems. Otherwise a string error message.