Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

More object oriented wrappers around parts of the Moodle question bank. In due course, I expect that the question bank will be converted to a fully object oriented structure, at which point this file can be a starting point.

Copyright: 2009 The Open University
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 729 lines (30 kb)
Included or required: 1 time
Referenced: 9 times
Includes or requires: 1 file
 question/type/questiontypebase.php

Defines 2 classes


Class: question_bank  - X-Ref

This static class provides access to the other question bank.

It provides functions for managing question types and question definitions.

is_qtype_installed($qtypename)   X-Ref

param: string $qtypename a question type name, e.g. 'multichoice'.
return: bool whether that question type is installed in this Moodle.

get_qtype($qtypename, $mustexist = true)   X-Ref
Get the question type class for a particular question type.

param: string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
param: bool $mustexist if false, the missing question type is returned when
return: question_type the corresponding question type class.

get_config()   X-Ref
Load the question configuration data from config_plugins.

return: object get_config('question') with caching.

qtype_enabled($qtypename)   X-Ref

param: string $qtypename the internal name of a question type. For example multichoice.
return: bool whether users are allowed to create questions of this type.

qtype_exists($qtypename)   X-Ref

param: string $qtypename the internal name of a question type. For example multichoice.
return: bool whether this question type exists.

get_qtype_name($qtypename)   X-Ref

param: $qtypename the internal name of a question type, for example multichoice.
return: string the human_readable name of this question type, from the language pack.

get_all_qtypes()   X-Ref

return: array all the installed question types.

sort_qtype_array($qtypes, $config = null)   X-Ref
Sort an array of question types according to the order the admin set up,
and then alphabetically for the rest.

param: array qtype->name() => qtype->local_name().
return: array sorted array.

get_creatable_qtypes()   X-Ref

return: array all the question types that users are allowed to create,

load_question_definition_classes($qtypename)   X-Ref
Load the question definition class(es) belonging to a question type. That is,
include_once('/question/type/' . $qtypename . '/question.php'), with a bit
of checking.

param: string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.

notify_question_edited($questionid)   X-Ref
This method needs to be called whenever a question is edited.


load_question_data($questionid)   X-Ref
Load a question definition data from the database. The data will be
returned as a plain stdClass object.

param: int $questionid the id of the question to load.
return: object question definition loaded from the database.

load_question($questionid, $allowshuffle = true)   X-Ref
Load a question definition from the database. The object returned
will actually be of an appropriate {@link question_definition} subclass.

param: int $questionid the id of the question to load.
param: bool $allowshuffle if false, then any shuffle option on the selected
return: question_definition loaded from the database.

make_question($questiondata)   X-Ref
Convert the question information loaded with {@link get_question_options()}
to a question_definintion object.

param: object $questiondata raw data loaded from the database.
return: question_definition loaded from the database.

get_all_versions_of_question(int $questionid)   X-Ref
Get all the versions of a particular question.

param: int $questionid id of the question
return: array The array keys are version number, and the values are objects with three int fields

get_all_versions_of_questions(array $questionids)   X-Ref
Get all the versions of questions.

param: array $questionids Array of question ids.
return: array two dimensional array question_bank_entries.id => version number => question.id.

get_finder()   X-Ref

return: question_finder a question finder.

start_unit_test()   X-Ref
Only to be called from unit tests. Allows {@link load_test_data()} to be used.


end_unit_test()   X-Ref
Only to be called from unit tests. Allows {@link load_test_data()} to be used.


return_test_question_data($questionid)   X-Ref
No description

load_test_question_data(question_definition $question)   X-Ref
To be used for unit testing only. Will throw an exception if
{@link start_unit_test()} has not been called first.

param: object $questiondata a question data object to put in the test data store.

ensure_fraction_options_initialised()   X-Ref
No description

fraction_options()   X-Ref

return: array string => string The standard set of grade options (fractions)

fraction_options_full()   X-Ref
No description

get_all_question_types_in_categories($categories)   X-Ref
Return a list of the different question types present in the given categories.

param: array $categories a list of category ids
return: array the list of question types in the categories

Class: question_finder  - X-Ref

Class for loading questions according to various criteria.

get_instance()   X-Ref

return: question_finder a question finder.

get_instance_for_cache(cache_definition $definition)   X-Ref
No description

get_data_cache()   X-Ref

return: cache_application the question definition cache we are using.

uncache_question($questionid)   X-Ref
This method needs to be called whenever a question is edited.


load_question_data($questionid)   X-Ref
Load a question definition data from the database. The data will be
returned as a plain stdClass object.

param: int $questionid the id of the question to load.
return: object question definition loaded from the database.

get_questions_from_categories($categoryids, $extraconditions,$extraparams = array()   X-Ref
Get the ids of all the questions in a list of categories.

param: array $categoryids either a category id, or a comma-separated list
param: string $extraconditions extra conditions to AND with the rest of
param: array $extraparams any parameters used by $extraconditions.
return: array questionid => questionid.

get_questions_from_categories_with_usage_counts($categoryids,qubaid_condition $qubaids, $extraconditions = '', $extraparams = array()   X-Ref
Get the ids of all the questions in a list of categories, with the number
of times they have already been used in a given set of usages.

The result array is returned in order of increasing (count previous uses).

param: array $categoryids an array question_category ids.
param: qubaid_condition $qubaids which question_usages to count previous uses from.
param: string $extraconditions extra conditions to AND with the rest of
param: array $extraparams any parameters used by $extraconditions.
return: array questionid => count of number of previous uses.

get_questions_from_categories_and_tags_with_usage_counts($categoryids,qubaid_condition $qubaids, $extraconditions = '', $extraparams = array()   X-Ref
Get the ids of all the questions in a list of categories that have ALL the provided tags,
with the number of times they have already been used in a given set of usages.

The result array is returned in order of increasing (count previous uses).

param: array $categoryids an array of question_category ids.
param: qubaid_condition $qubaids which question_usages to count previous uses from.
param: string $extraconditions extra conditions to AND with the rest of
param: array $extraparams any parameters used by $extraconditions.
param: array $tagids an array of tag ids
return: array questionid => count of number of previous uses.

load_for_cache($questionid)   X-Ref
No description

load_many_for_cache(array $questionids)   X-Ref
No description