Moodle PHP Documentation 4.4
Moodle 4.4.1 (Build: 20240610) (db07c09afc5)
|
Represents a calculated question. More...
Public Member Functions | |
__get ($name) | |
When a pending definition tries to read its latest version, fill in the latest version for all pending definitions. | |
apply_attempt_state (question_attempt_step $step) | |
When an in-progress question_attempt is re-loaded from the database, this method is called so that the question can re-initialise its internal state as needed by this attempt. | |
apply_unit_penalty ($fraction, $unitisright) | |
Adjust the fraction based on whether the unit was correct. | |
calculate_all_expressions () | |
Replace all the expression in the question definition with the values computed from the selected dataset by calling $this->vs->calculate() and $this->vs->replace_expressions_in_text() on the parts of the question that require it. | |
check_file_access ($qa, $options, $component, $filearea, $args, $forcedownload) | |
Checks whether the users is allow to be served a particular file. | |
classify_response (array $response) | |
Categorise the student's response according to the categories defined by get_possible_responses. | |
clear_wrong_from_response (array $response) | |
Given a response, rest the parts that are wrong. | |
format_generalfeedback ($qa) | |
format_hint (question_hint $hint, question_attempt $qa) | |
format_questiontext ($qa) | |
format_text ($text, $format, $qa, $component, $filearea, $itemid, $clean=false) | |
Apply format_text() to some content with appropriate settings for this question. | |
get_correct_answer () | |
get_correct_response () | |
What data would need to be submitted to get this question correct. | |
get_expected_data () | |
What data may be included in the form submission when a student submits this question in its current state? | |
get_hint ($hintnumber, question_attempt $qa) | |
Get one of the question hints. | |
get_matching_answer ($value, $multiplier) | |
Get an answer that contains the feedback and fraction that should be awarded for this response. | |
get_max_fraction () | |
Some questions can return a mark greater than the maximum. | |
get_min_fraction () | |
Some questions can return a negative mark if the student gets it wrong. | |
get_num_parts_right (array $response) | |
Return the number of subparts of this response that are right. | |
get_num_variants () | |
get_question_definition_for_external_rendering (question_attempt $qa, question_display_options $options) | |
Return the question settings that define this question as structured data. | |
get_question_summary () | |
Generate a brief, plain-text, summary of this question. | |
get_renderer (moodle_page $page) | |
get_right_answer_summary () | |
Generate a brief, plain-text, summary of the correct answer to this question. | |
get_student_response_values_for_simulation ($realresponse) | |
Does the opposite of prepare_simulated_post_data. | |
get_type_name () | |
get_validation_error (array $response) | |
In situations where is_gradable_response() returns false, this method should generate a description of what the problem is. | |
get_variants_selection_seed () | |
grade_response (array $response) | |
Grade a response to the question, returning a fraction between get_min_fraction() and get_max_fraction(), and the corresponding question_state right, partial or wrong. | |
has_separate_unit_field () | |
html_to_text ($text, $format) | |
Convert some part of the question text to plain text. | |
is_complete_response (array $response) | |
Used by many of the behaviours, to work out whether the student's response to the question is complete. | |
is_gradable_response (array $response) | |
Use by many of the behaviours to determine whether the student has provided enough of an answer for the question to be graded automatically, or whether it must be considered aborted. | |
is_same_response (array $prevresponse, array $newresponse) | |
Use by many of the behaviours to determine whether the student's response has changed. | |
is_unit_right (qtype_numerical_answer $answer, float $value, ?float $multiplier) | |
Checks if the provided $multiplier is appropriate for the unit of the given $value, ensuring that multiplying $value by the $multiplier yields the expected $answer. | |
make_behaviour (question_attempt $qa, $preferredbehaviour) | |
Creat the appropriate behaviour for an attempt at this quetsion, given the desired (archetypal) behaviour. | |
make_html_inline ($html) | |
Take some HTML that should probably already be a single line, like a multiple choice choice, or the corresponding feedback, and make it so that it is suitable to go in a place where the HTML must be inline, like inside a. | |
prepare_simulated_post_data ($simulatedresponse) | |
Takes an array of values representing a student response represented in a way that is understandable by a human and transforms that to the response as the POST values returned from the HTML form that takes the student response during a student attempt. | |
set_latest_version (int $latestversion) | |
Set the latest version. | |
start_attempt (question_attempt_step $step, $variant) | |
Start a new attempt at this question, storing any information that will be needed later in the step. | |
summarise_response (array $response) | |
Produce a plain text summary of a response. | |
un_summarise_response (string $summary) | |
If possible, construct a response that could have lead to the given response summary. | |
update_attempt_state_data_for_new_version (question_attempt_step $oldstep, question_definition $oldquestion) | |
Update the data representing the initial state of an attempt another version of this question, to allow for the changes. | |
validate_can_regrade_with_other_version (question_definition $otherversion) | |
Verify if an attempt at this question can be re-graded using the other question version. | |
Public Attributes | |
array | $answers = array() |
of question_answer. | |
qtype_numerical_answer_processor | $ap |
integer | $category |
question category id. | |
integer | $contextid |
question context id. | |
integer | $createdby |
userid of the use who created this question. | |
array | $customfields = array() |
of array of core_customfield\data_controller objects indexed by fieldid for the questions custom fields. | |
qtype_calculated_dataset_loader | $datasetloader |
helper for loading the dataset. | |
float | $defaultmark = 1 |
what this quetsion is marked out of, by default. | |
string | $generalfeedback |
question general feedback. | |
integer | $generalfeedbackformat |
question test format. | |
array | $hints = array() |
of question_hints. | |
integer | $id |
id of the question in the datase, or null if this question is not in the database. | |
string | $idnumber |
question idnumber. | |
integer | $length = 1 |
How many question numbers this question consumes. | |
integer | $modifiedby |
userid of the use who modified this question. | |
string | $name |
question name. | |
integer | $parent = 0 |
parent question id. | |
number | $penalty = 0 |
penalty factor of this question. | |
question_type | $qtype |
the question type this question is. | |
int | $questionbankentryid |
Bank entry id for the question. | |
string | $questiontext |
question text. | |
integer | $questiontextformat |
question test format. | |
Some | $shownumcorrect = false |
question types have the option to show the number of sub-parts correct. | |
string | $stamp |
unique identifier of this question. | |
boolean | $status = core_question\local\bank\question_version_status::QUESTION_STATUS_READY |
question status hidden/ready/draft in the question bank. | |
bool | $synchronised |
wheter the dataset item to use should be chose based on attempt start time, rather than randomly. | |
integer | $timecreated |
timestamp when this question was created. | |
integer | $timemodified |
timestamp when this question was modified. | |
int | $unitdisplay |
one of the constants UNITNONE, UNITRADIO, UNITSELECT or UNITINPUT. | |
int | $unitgradingtype |
one of the constants UNITGRADEDOUTOFMARK or UNITGRADEDOUTOFMAX. | |
number | $unitpenalty |
the penalty for a missing or unrecognised unit. | |
boolean | $unitsleft |
whether the units come before or after the number | |
int | $version |
Version number of the question in a question bank. | |
int | $versionid |
Version id of the question in a question bank. | |
qtype_calculated_variable_substituter | $vs |
stores the dataset we are using. | |
Protected Member Functions | |
check_combined_feedback_file_access ($qa, $options, $filearea, $args=null) | |
Check a request for access to a file belonging to a combined feedback field. | |
check_hint_file_access ($qa, $options, $args) | |
Check a request for access to a file belonging to a hint. | |
Protected Attributes | |
int | $latestversion = null |
The latest version of the question. | |
Represents a calculated question.
|
inherited |
When a pending definition tries to read its latest version, fill in the latest version for all pending definitions.
string | $name |
mixed |
qtype_calculated_question::apply_attempt_state | ( | question_attempt_step | $step | ) |
When an in-progress question_attempt is re-loaded from the database, this method is called so that the question can re-initialise its internal state as needed by this attempt.
For example, the multiple choice question type needs to set the order of the choices to the order that was set up when start_attempt was called originally. All the information required to do this should be in the $step object, which is the first step of the question_attempt being loaded.
question_attempt_step | The first step of the question_attempt being loaded. |
Reimplemented from qtype_numerical_question.
|
inherited |
Adjust the fraction based on whether the unit was correct.
number | $fraction | |
bool | $unitisright |
number |
qtype_calculated_question::calculate_all_expressions | ( | ) |
Replace all the expression in the question definition with the values computed from the selected dataset by calling $this->vs->calculate() and $this->vs->replace_expressions_in_text() on the parts of the question that require it.
Implements qtype_calculated_question_with_expressions.
|
protectedinherited |
Check a request for access to a file belonging to a combined feedback field.
question_attempt | $qa | the question attempt being displayed. |
question_display_options | $options | the options that control display of the question. |
string | $filearea | the name of the file area. |
array | $args | the remaining bits of the file path. |
bool | whether access to the file should be allowed. |
Reimplemented in qtype_ordering_question.
|
inherited |
Checks whether the users is allow to be served a particular file.
question_attempt | $qa | the question attempt being displayed. |
question_display_options | $options | the options that control display of the question. |
string | $component | the name of the component we are serving files for. |
string | $filearea | the name of the file area. |
array | $args | the remaining bits of the file path. |
bool | $forcedownload | whether the user must be forced to download the file. |
bool | true if the user can access this file. |
Reimplemented from question_definition.
|
protectedinherited |
Check a request for access to a file belonging to a hint.
question_attempt | $qa | the question attempt being displayed. |
question_display_options | $options | the options that control display of the question. |
array | $args | the remaining bits of the file path. |
bool | whether access to the file should be allowed. |
|
inherited |
Categorise the student's response according to the categories defined by get_possible_responses.
$response | a response, as might be passed to grade_response(). |
array | subpartid => question_classified_response objects. returns an empty array if no analysis is possible. |
Reimplemented from question_with_responses.
|
inherited |
Given a response, rest the parts that are wrong.
array | $response | a response |
array | a cleaned up response with the wrong bits reset. |
Reimplemented in qtype_ddmarker_question, qtype_ddtoimage_question_base, qtype_gapselect_question_base, qtype_match_question, qtype_multianswer_question, and qtype_multichoice_multi_question.
|
inherited |
the | result of applying format_text() to the general feedback. |
|
inherited |
the | result of applying format_text() to the question text. |
|
inherited |
Apply format_text() to some content with appropriate settings for this question.
string | $text | some content that needs to be output. |
int | $format | the FORMAT_... constant. |
question_attempt | $qa | the question attempt. |
string | $component | used for rewriting file area URLs. |
string | $filearea | used for rewriting file area URLs. |
bool | $clean | Whether the HTML needs to be cleaned. Generally, parts of the question do not need to be cleaned, and student input does. |
string | the text formatted for output by format_text. |
qtype_calculated_question::get_correct_response | ( | ) |
What data would need to be submitted to get this question correct.
If there is more than one correct answer, this method should just return one possibility. If it is not possible to compute a correct response, this method should return null.
array|null | parameter name => value. |
Reimplemented from qtype_numerical_question.
|
inherited |
What data may be included in the form submission when a student submits this question in its current state?
This information is used in calls to optional_param. The parameter name has question_attempt::get_field_prefix() automatically prepended.
array|string | variable name => PARAM_... constant, or, as a special case that should only be used in unavoidable, the constant question_attempt\USE_RAW_DATA meaning take all the raw submitted data belonging to this question. |
Reimplemented from question_definition.
|
inherited |
Get one of the question hints.
The question_attempt is passed in case the question type wants to do something complex. For example, the multiple choice with multiple responses question type will turn off most of the hint options if the student has selected too many opitions.
int | $hintnumber | Which hint to display. Indexed starting from 0 |
question_attempt | $qa | The question_attempt. |
Implements question_automatically_gradable.
Reimplemented in qtype_multichoice_multi_question.
|
inherited |
Get an answer that contains the feedback and fraction that should be awarded for this response.
number | $value | the numerical value of a response. |
number | $multiplier | for the unit the student gave, if any. When no unit was given, or an unrecognised unit was given, $multiplier will be null. |
question_answer | the matching answer. |
|
inherited |
Some questions can return a mark greater than the maximum.
This method returns the lowest highest the question can return, on the fraction scale. that is, where the nominal maximum mark is 1.0.
float | maximum fraction this question will ever return. |
Reimplemented in qtype_multianswer_question.
|
inherited |
Some questions can return a negative mark if the student gets it wrong.
This method returns the lowest mark the question can return, on the fraction scale. that is, where the maximum possible mark is 1.0.
float | minimum fraction this question will ever return. |
Reimplemented in qtype_multianswer_question, qtype_multichoice_multi_question, and qtype_multichoice_single_question.
|
inherited |
Return the number of subparts of this response that are right.
array | $response | a response |
array | with two elements, the number of correct subparts, and the total number of subparts. |
Reimplemented in qtype_ddmarker_question, qtype_gapselect_question_base, qtype_match_question, qtype_multianswer_question, qtype_multichoice_multi_question, and qtype_ordering_question.
qtype_calculated_question::get_num_variants | ( | ) |
int | the number of vaiants that this question has. |
Reimplemented from question_definition.
|
inherited |
Return the question settings that define this question as structured data.
question_attempt | $qa | the current attempt for which we are exporting the settings. |
question_display_options | $options | the question display options which say which aspects of the question should be visible. |
mixed | structure representing the question settings. In web services, this will be JSON-encoded. |
Reimplemented from question_definition.
|
inherited |
Generate a brief, plain-text, summary of this question.
This is used by various reports. This should show the particular variant of the question as presented to students. For example, the calculated quetsion type would fill in the particular numbers that were presented to the student. This method will return null if such a summary is not possible, or inappropriate.
string|null | a plain text summary of this question. |
Reimplemented in qtype_ddtoimage_question_base, qtype_gapselect_question_base, qtype_match_question, qtype_multianswer_question, qtype_multichoice_base, and question_information_item.
|
inherited |
moodle_page | the page we are outputting to. |
qtype_renderer | the renderer to use for outputting this question. |
Reimplemented in qtype_multichoice_multi_question, and qtype_multichoice_single_question.
|
inherited |
Generate a brief, plain-text, summary of the correct answer to this question.
This is used by various reports, and can also be useful when testing. This method will return null if such a summary is not possible, or inappropriate.
string|null | a plain text summary of the right answer to this question. |
Implements question_automatically_gradable.
Reimplemented in qtype_ddmarker_question.
|
inherited |
Does the opposite of prepare_simulated_post_data.
This takes a student response (the POST values returned from the HTML form that takes the student response during a student attempt) it then represents it in a way that is understandable by a human.
Primarily this is used when creating a file of csv from real student responses in order later to be able to simulate the same student interaction with a quiz later.
string[] | $realresponse | the response array as was returned from the form during a student attempt (without prefixes). |
string[] | an array of data representing a student response. |
Reimplemented in qtype_match_question, qtype_multianswer_question, qtype_multichoice_multi_question, and qtype_multichoice_single_question.
|
inherited |
string | the name of the question type (for example multichoice) that this question is. |
|
inherited |
In situations where is_gradable_response() returns false, this method should generate a description of what the problem is.
string | the message. |
Implements question_automatically_gradable.
qtype_calculated_question::get_variants_selection_seed | ( | ) |
string | that can be used to seed the pseudo-random selection of a variant. |
Reimplemented from question_definition.
|
inherited |
Grade a response to the question, returning a fraction between get_min_fraction() and get_max_fraction(), and the corresponding question_state right, partial or wrong.
array | $response | responses, as returned by question_attempt_step::get_qt_data(). |
array | (float, integer) the fraction, and the state. |
Implements question_automatically_gradable.
|
inherited |
Convert some part of the question text to plain text.
This might be used, for example, by get_response_summary().
string | $text | The HTML to reduce to plain text. |
int | $format | the FORMAT_... constant. |
string | the equivalent plain text. |
|
inherited |
Used by many of the behaviours, to work out whether the student's response to the question is complete.
That is, whether the question attempt should move to the COMPLETE or INCOMPLETE state.
array | $response | responses, as returned by question_attempt_step::get_qt_data(). |
bool | whether this response is a complete answer to this question. |
Implements question_manually_gradable.
|
inherited |
Use by many of the behaviours to determine whether the student has provided enough of an answer for the question to be graded automatically, or whether it must be considered aborted.
array | $response | responses, as returned by question_attempt_step::get_qt_data(). |
bool | whether this response can be graded. |
Reimplemented from question_with_responses.
|
inherited |
Use by many of the behaviours to determine whether the student's response has changed.
This is normally used to determine that a new set of responses can safely be discarded.
array | $prevresponse | the responses previously recorded for this question, as returned by question_attempt_step::get_qt_data() |
array | $newresponse | the new responses, in the same format. |
bool | whether the two sets of responses are the same - that is whether the new set of responses can safely be discarded. |
Implements question_manually_gradable.
|
inherited |
Checks if the provided $multiplier is appropriate for the unit of the given $value, ensuring that multiplying $value by the $multiplier yields the expected $answer.
qtype_numerical_answer | $answer | The expected result when multiplying $value by the appropriate $multiplier. |
float | $value | The provided value |
float | null | $multiplier | The multiplier value for the unit of $value. |
bool | Returns true if the $multiplier is correct for the unit of $value, false otherwise. |
|
inherited |
Creat the appropriate behaviour for an attempt at this quetsion, given the desired (archetypal) behaviour.
This default implementation will suit most normal graded questions.
If your question is of a patricular type, then it may need to do something different. For example, if your question can only be graded manually, then it should probably return a manualgraded behaviour, irrespective of what is asked for.
If your question wants to do somthing especially complicated is some situations, then you may wish to return a particular behaviour related to the one asked for. For example, you migth want to return a qbehaviour_interactive_adapted_for_myqtype.
question_attempt | $qa | the attempt we are creating a behaviour for. |
string | $preferredbehaviour | the requested type of behaviour. |
question_behaviour | the new behaviour object. |
Reimplemented in qtype_essay_question, question_graded_automatically_with_countback, and question_information_item.
|
inherited |
Take some HTML that should probably already be a single line, like a multiple choice choice, or the corresponding feedback, and make it so that it is suitable to go in a place where the HTML must be inline, like inside a.
tag.
string | $html | to HTML to fix up. |
string | the fixed HTML. |
|
inherited |
Takes an array of values representing a student response represented in a way that is understandable by a human and transforms that to the response as the POST values returned from the HTML form that takes the student response during a student attempt.
Primarily this is used when reading csv values from a file of student responses in order to be able to simulate the student interaction with a quiz.
In most cases the array will just be returned as is. Some question types will need to transform the keys of the array, as the meaning of the keys in the html form is deliberately obfuscated so that someone looking at the html does not get an advantage. The values that represent the response might also be changed in order to more meaningful to a human.
See the examples of question types that have overridden this in core and also see the csv files of simulated student responses used in unit tests in :
Also see , a quiz report plug in for uploading and downloading student responses as csv files.
array | $simulatedresponse | an array of data representing a student response |
array | a response array as would be returned from the html form (but without prefixes) |
Reimplemented in qtype_match_question, qtype_multianswer_question, qtype_multichoice_multi_question, and qtype_multichoice_single_question.
|
inherited |
Set the latest version.
Making $this->latestversion public would break the magic __get() behaviour above, so allow it to be set externally.
int | $latestversion |
void |
qtype_calculated_question::start_attempt | ( | question_attempt_step | $step, |
$variant ) |
Start a new attempt at this question, storing any information that will be needed later in the step.
This is where the question can do any initialisation required on a per-attempt basis. For example, this is where the multiple choice question type randomly shuffles the choices (if that option is set).
Any information about how the question has been set up for this attempt should be stored in the $step, by calling $step->set_qt_var(...).
question_attempt_step | The first step of the question_attempt being started. Can be used to store state. | |
int | $varant | which variant of this question to start. Will be between 1 and get_num_variants() inclusive. |
Reimplemented from qtype_numerical_question.
|
inherited |
Produce a plain text summary of a response.
array | $response | a response, as might be passed to grade_response(). |
string | a plain text summary of that response, that could be used in reports. |
Implements question_manually_gradable.
|
inherited |
If possible, construct a response that could have lead to the given response summary.
This is basically the opposite of summarise_response() but it is intended only to be used for testing.
string | $summary | a string, which might have come from summarise_response |
array | a response that could have lead to that. |
Reimplemented from question_with_responses.
|
inherited |
Update the data representing the initial state of an attempt another version of this question, to allow for the changes.
What is required is probably most easily understood using an example. Think about multiple choice questions. The first step has a variable '_order' which is a comma-separated list of question_answer ids. A different version of the question will have different question_answers with different ids. However, the list of choices should be similar, and so we need to shuffle the new list of ids in the same way that the old one was.
Note: be sure to return all the data that was originally in $oldstep, while updating the fields that require it. Otherwise you might break features like 'Each attempt builds on last' in the quiz.
This method should only be called if {
question_attempt_step | $oldstep | the first step of a { |
question_definition | $oldquestion | the previous version of the question, which $oldstate comes from. |
array | the submit data which can be passed to { |
coding_exception | if this can't be done. |
Reimplemented in qtype_match_question, qtype_multianswer_question, qtype_multichoice_base, and qtype_ordering_question.
|
inherited |
Verify if an attempt at this question can be re-graded using the other question version.
To put it another way, will {
It is expected that this relationship is symmetrical, so if you can regrade from V1 to V3, then you can change back from V3 to V1.
question_definition | $otherversion | a different version of the question to use in the regrade. |
string|null | null if the regrade can proceed, else a reason why not. |
Reimplemented in qtype_gapselect_question_base, qtype_match_question, qtype_multianswer_question, qtype_multichoice_base, and qtype_ordering_question.
|
protectedinherited |
The latest version of the question.
null if we haven't checked yet.