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 402 and 403]

A class for efficiently finds questions at random from the question bank.

Copyright: 2015 The Open University
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 328 lines (15 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: random_question_loader  - X-Ref

This class efficiently finds questions at random from the question bank.

You can ask for questions at random one at a time. Each time you ask, you
pass a category id, and whether to pick from that category and all subcategories
or just that category.

The number of teams each question has been used is tracked, and we will always
return a question from among those elegible that has been used the fewest times.
So, if there are questions that have not been used yet in the category asked for,
one of those will be returned. However, within one instantiation of this class,
we will never return a given question more than once, and we will never return
questions passed into the constructor as $usedquestions.

__construct(\qubaid_condition $qubaids, array $usedquestions = [])   X-Ref
Constructor.

param: \qubaid_condition $qubaids the usages to consider when counting previous uses of each question.
param: array $usedquestions questionid => number of times used count. If we should allow for

get_next_question_id($categoryid, $includesubcategories, $tagids = [])   X-Ref
Pick a question at random from the given category, from among those with the fewest uses.
If an array of tag ids are specified, then only the questions that are tagged with ALL those tags will be selected.

It is up the the caller to verify that the cateogry exists. An unknown category
behaves like an empty one.

param: int $categoryid the id of a category in the question bank.
param: bool $includesubcategories wether to pick a question from exactly
param: array $tagids An array of tag ids. A question has to be tagged with all the provided tagids (if any)
return: int|null the id of the question picked, or null if there aren't any.

get_category_key($categoryid, $includesubcategories, $tagids = [])   X-Ref
Get the key into {@see $availablequestionscache} for this combination of options.

param: int $categoryid the id of a category in the question bank.
param: bool $includesubcategories wether to pick a question from exactly
param: array $tagids an array of tag ids.
return: string the cache key.

ensure_questions_for_category_loaded($categoryid, $includesubcategories, $tagids = [])   X-Ref
Populate {@see $availablequestionscache} for this combination of options.

param: int $categoryid The id of a category in the question bank.
param: bool $includesubcategories Whether to pick a question from exactly
param: array $tagids An array of tag ids. If an array is provided, then

use_question($questionid)   X-Ref
Update the internal data structures to indicate that a given question has
been used one more time.

param: int $questionid the question that is being used.

get_question_ids($categoryid, $includesubcategories, $tagids = [])   X-Ref
Get the list of available question ids for the given criteria.

param: int $categoryid The id of a category in the question bank.
param: bool $includesubcategories Whether to pick a question from exactly
param: array $tagids An array of tag ids. If an array is provided, then
return: int[] The list of question ids

is_question_available($categoryid, $includesubcategories, $questionid, $tagids = [])   X-Ref
Check whether a given question is available in a given category. If so, mark it used.
If an optional list of tag ids are provided, then the question must be tagged with
ALL of the provided tags to be considered as available.

param: int $categoryid the id of a category in the question bank.
param: bool $includesubcategories wether to pick a question from exactly
param: int $questionid the question that is being used.
param: array $tagids An array of tag ids. Only the questions that are tagged with all the provided tagids can be available.
return: bool whether the question is available in the requested category.

get_questions($categoryid, $includesubcategories, $tagids = [], $limit = 100, $offset = 0, $fields = [])   X-Ref
Get the list of available questions for the given criteria.

param: int $categoryid The id of a category in the question bank.
param: bool $includesubcategories Whether to pick a question from exactly
param: array $tagids An array of tag ids. If an array is provided, then
param: int $limit Maximum number of results to return.
param: int $offset Number of items to skip from the begging of the result set.
param: string[] $fields The fields to return for each question.
return: \stdClass[] The list of question records

count_questions($categoryid, $includesubcategories, $tagids = [])   X-Ref
Count the number of available questions for the given criteria.

param: int $categoryid The id of a category in the question bank.
param: bool $includesubcategories Whether to pick a question from exactly
param: array $tagids An array of tag ids. If an array is provided, then
return: int The number of questions matching the criteria.