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.

(no description)

File Size: 356 lines (14 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: access_rule_base  - X-Ref

Base class for rules that restrict the ability to attempt a quiz.

Quiz access rule plugins must sublclass this one to form their main 'rule' class.
Most of the methods are defined in a slightly unnatural way because we either
want to say that access is allowed, or explain the reason why it is block.
Therefore instead of is_access_allowed(...) we have prevent_access(...) that
return false if access is permitted, or a string explanation (which is treated
as true) if access should be blocked. Slighly unnatural, but actually the easiest
way to implement this.

__construct($quizobj, $timenow)   X-Ref
Create an instance of this rule for a particular quiz.

param: quiz_settings $quizobj information about the quiz in question.
param: int $timenow the time that should be considered as 'now'.

make(quiz_settings $quizobj, $timenow, $canignoretimelimits)   X-Ref
Return an appropriately configured instance of this rule, if it is applicable
to the given quiz, otherwise return null.

param: quiz_settings $quizobj information about the quiz in question.
param: int $timenow the time that should be considered as 'now'.
param: bool $canignoretimelimits whether the current user is exempt from
return: self|null the rule, if applicable, else null.

prevent_new_attempt($numprevattempts, $lastattempt)   X-Ref
Whether a user should be allowed to start a new attempt at this quiz now.

param: int $numprevattempts the number of previous attempts this user has made.
param: stdClass $lastattempt information about the user's last completed attempt.
return: string false if access should be allowed, a message explaining the

prevent_access()   X-Ref
Whether the user should be blocked from starting a new attempt or continuing
an attempt now.

return: string false if access should be allowed, a message explaining the

is_preflight_check_required($attemptid)   X-Ref
Does this rule require a UI check with the user before an attempt is started?

param: int|null $attemptid the id of the current attempt, if there is one,
return: bool whether a check is required before the user starts/continues

add_preflight_check_form_fields(preflight_check_form $quizform,MoodleQuickForm $mform, $attemptid)   X-Ref
Add any field you want to pre-flight check form. You should only do
something here if {@see is_preflight_check_required()} returned true.

param: preflight_check_form $quizform the form being built.
param: MoodleQuickForm $mform The wrapped MoodleQuickForm.
param: int|null $attemptid the id of the current attempt, if there is one,

validate_preflight_check($data, $files, $errors, $attemptid)   X-Ref
Validate the pre-flight check form submission. You should only do
something here if {@see is_preflight_check_required()} returned true.

If the form validates, the user will be allowed to continue.

param: array $data the submitted form data.
param: array $files any files in the submission.
param: array $errors the list of validation errors that is being built up.
param: int|null $attemptid the id of the current attempt, if there is one,
return: array the update $errors array;

notify_preflight_check_passed($attemptid)   X-Ref
The pre-flight check has passed. This is a chance to record that fact in
some way.

param: int|null $attemptid the id of the current attempt, if there is one,

current_attempt_finished()   X-Ref
This is called when the current attempt at the quiz is finished. This is
used, for example by the password rule, to clear the flag in the session.


description()   X-Ref
Return a brief summary of this rule, to show to users, if required.

This information is show shown, for example, on the quiz view page, to explain this
restriction. There is no obligation to return anything. If it is not appropriate to
tell students about this rule, then just return ''.

return: string a message, or array of messages, explaining the restriction

is_finished($numprevattempts, $lastattempt)   X-Ref
Is the current user unable to start any more attempts in future, because of this rule?

If this rule can determine that this user will never be allowed another attempt at
this quiz, for example because the last possible start time is past, or all attempts
have been used up, then return true. This is used to know whether to display a
final grade on the view page. This will only be called if there is not a currently
active attempt for this user.

param: int $numprevattempts the number of previous attempts this user has made.
param: stdClass $lastattempt information about the user's last completed attempt.
return: bool true if this rule means that this user will never be allowed another

end_time($attempt)   X-Ref
Time by which, according to this rule, the user has to finish their attempt.

param: stdClass $attempt the current attempt
return: int|false the attempt close time, or false if there is no close time.

time_left_display($attempt, $timenow)   X-Ref
If the user should be shown a different amount of time than $timenow - $this->end_time(), then
override this method.  This is useful if the time remaining is large enough to be omitted.

param: stdClass $attempt the current attempt
param: int $timenow the time now. We don't use $this->timenow, so we can
return: mixed the time left in seconds (can be negative) or false if there is no limit.

attempt_must_be_in_popup()   X-Ref
Does this rule requires the attempt (and review) to be displayed in a pop-up window?

return: bool true if it does.

get_popup_options()   X-Ref
Any options required when showing the attempt in a pop-up.

return: array any options that are required for showing the attempt page

setup_attempt_page($page)   X-Ref
Sets up the attempt (review or summary) page with any special extra
properties required by this rule. securewindow rule is an example of where
this is used.

param: moodle_page $page the page object to initialise.

get_superceded_rules()   X-Ref
It is possible for one rule to override other rules.

The aim is that third-party rules should be able to replace sandard rules
if they want. See, for example MDL-13592.

return: array plugin names of other rules that this one replaces.

add_settings_form_fields(mod_quiz_mod_form $quizform, MoodleQuickForm $mform)   X-Ref
Add any fields that this rule requires to the quiz settings form. This
method is called from {@see mod_quiz_mod_form::definition()}, while the
security seciton is being built.

param: mod_quiz_mod_form $quizform the quiz settings form that is being built.
param: MoodleQuickForm $mform the wrapped MoodleQuickForm.

validate_settings_form_fields(array $errors,array $data, $files, mod_quiz_mod_form $quizform)   X-Ref
Validate the data from any form fields added using {@see add_settings_form_fields()}.

param: array $errors the errors found so far.
param: array $data the submitted form data.
param: array $files information about any uploaded files.
param: mod_quiz_mod_form $quizform the quiz form object.
return: array $errors the updated $errors array.

get_browser_security_choices()   X-Ref
Get any options this rule adds to the 'Browser security' quiz setting.

return: array key => lang string any choices to add to the quiz Browser

save_settings($quiz)   X-Ref
Save any submitted settings when the quiz settings form is submitted. This
is called from {@see quiz_after_add_or_update()} in lib.php.

param: stdClass $quiz the data from the quiz form, including $quiz->id

delete_settings($quiz)   X-Ref
Delete any rule-specific settings when the quiz is deleted. This is called
from {@see quiz_delete_instance()} in lib.php.

param: stdClass $quiz the data from the database, including $quiz->id

get_settings_sql($quizid)   X-Ref
Return the bits of SQL needed to load all the settings from all the access
plugins in one DB query. The easiest way to understand what you need to do
here is probably to read the code of {@see access_manager::load_settings()}.

If you have some settings that cannot be loaded in this way, then you can
use the {@see get_extra_settings()} method instead, but that has
performance implications.

param: int $quizid the id of the quiz we are loading settings for. This
return: array with three elements:

get_extra_settings($quizid)   X-Ref
You can use this method to load any extra settings your plugin has that
cannot be loaded efficiently with get_settings_sql().

param: int $quizid the quiz id.
return: array setting value name => value. The value names should all