Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Random question type

From MoodleDocs

The random question is not a real question but only a device to randomly choose a question from a category. The $question->questiontext field is abused as a flag: 1 means choose question from the category and its subcategories, 0 means only use questions in the category itself.

When a new session is started for a random question then a question is chosen from the category. We need to make sure that no question is used more than once in the quiz. Therefore the following need to be excluded:

  1. All questions that are explicitly assigned to the quiz
  2. All questions that are already chosen by an other random question
  3. Random questions
  4. Other explicitly excluded question types
  5. Wrapped questions

To do the first the question type uses an additional property $quiz->questionsinuse that holds a comma separated list of all questions used in the current quiz. To do the second the questiontype class has a property 'catrandoms' which is an array indexed by category id and by $question->questiontext. Each entry is a randomized array of questions in that category which can be used.

Explicitly excluded question types

See

question/type/random/questiontype.php

for the list of explicitly excluded types:

var $excludedtypes = array("'random'", "'randomsamatch'", "'essay'", "'description'");

Please note that because of problems with grading, the essay question is not supported at the moment. See more information at the thread Random Question Test.

Response storage

The answer field for random questions come in two flavours:

  • For responses stored by Moodle version 1.5 and later the answer field has the pattern random#-* where the # part is the numeric question id of the actual question shown in the quiz attempt and * represents the student response to that actual question.
  • For responses stored by older Moodle versions - the answer field is simply the question id of the actual question. The student response to the actual question is stored in a separate record.

This means that prior to Moodle version 1.5, random questions needed two records for storing the response to a single question. From version 1.5 and later the question type random works like all the other question types in that it now only needs one record per question.

$question->options

The random question type does not have any options for $question->options.

$state->options

This has one property 'question' which is set to the fully instantiated question object for the randomly chosen question.