Moodle APIs 3.9
Moodle 3.9.13+ (Build: 20220325)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
quiz_access_manager Class Reference

This class keeps track of the various access rules that apply to a particular quiz, with convinient methods for seeing whether access is allowed. More...

Public Member Functions

 __construct ($quizobj, $timenow, $canignoretimelimits)
 Create an instance for a particular quiz. More...
 
 attempt_must_be_in_popup ()
 
 back_to_view_page ($output, $message='')
 Send the user back to the quiz view page. More...
 
 current_attempt_finished ()
 Inform the rules that the current attempt is finished. More...
 
 describe_rules ()
 Provide a description of the rules that apply to this quiz, such as is shown at the top of the quiz view page. More...
 
 get_active_rule_names ()
 
 get_end_time ($attempt)
 Compute when the attempt must be submitted. More...
 
 get_popup_options ()
 
 get_preflight_check_form (moodle_url $url, $attemptid)
 Build the form required to do the pre-flight checks. More...
 
 get_time_left_display ($attempt, $timenow)
 Compute what should be displayed to the user for time remaining in this attempt. More...
 
 is_finished ($numprevattempts, $lastattempt)
 Do any of the rules mean that this student will no be allowed any further attempts at this quiz. More...
 
 is_preflight_check_required ($attemptid)
 
 make_review_link ($attempt, $reviewoptions, $output)
 Make some text into a link to review the quiz, if that is appropriate. More...
 
 notify_preflight_check_passed ($attemptid)
 The pre-flight check has passed. More...
 
 prevent_access ()
 Whether the user should be blocked from starting a new attempt or continuing an attempt now. More...
 
 prevent_new_attempt ($numprevattempts, $lastattempt)
 Whether or not a user should be allowed to start a new attempt at this quiz now. More...
 
 setup_attempt_page ($page)
 Sets up the attempt (review or summary) page with any properties required by the access rules. More...
 
 validate_preflight_check ($data, $files, $attemptid)
 Run the preflight checks using the given data in all the rules supporting them. More...
 

Static Public Member Functions

static add_settings_form_fields (mod_quiz_mod_form $quizform, MoodleQuickForm $mform)
 Add any form fields that the access rules require to the settings form. More...
 
static delete_settings ($quiz)
 Delete any rule-specific settings when the quiz is deleted. More...
 
static get_browser_security_choices ()
 The the options for the Browser security settings menu. More...
 
static load_quiz_and_settings ($quizid)
 Load the quiz settings and any settings required by the access rules. More...
 
static load_settings ($quizid)
 Load any settings required by the access rules. More...
 
static save_settings ($quiz)
 Save any submitted settings when the quiz settings form is submitted. More...
 
static validate_settings_form_fields (array $errors, array $data, $files, mod_quiz_mod_form $quizform)
 Validate the data from any form fields added using add_settings_form_fields(). More...
 

Protected Member Functions

 accumulate_messages ($messages, $new)
 Accumulates an array of messages. More...
 
 make_rules ($quizobj, $timenow, $canignoretimelimits)
 Make all the rules relevant to a particular quiz. More...
 

Static Protected Member Functions

static get_load_sql ($quizid, $rules, $basefields)
 Build the SQL for loading all the access settings in one go. More...
 
static get_rule_classes ()
 

Protected Attributes

quiz $quizobj
 the quiz settings object.
 
array $rules = array()
 of quiz_access_rule_base.
 
int $timenow
 the time to be considered as 'now'.
 

Detailed Description

This class keeps track of the various access rules that apply to a particular quiz, with convinient methods for seeing whether access is allowed.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Since
Moodle 2.2

Constructor & Destructor Documentation

◆ __construct()

quiz_access_manager::__construct (   $quizobj,
  $timenow,
  $canignoretimelimits 
)

Create an instance for a particular quiz.

Parameters
object$quizobjAn instance of the class quiz from attemptlib.php. The quiz we will be controlling access to.
int$timenowThe time to use as 'now'.
bool$canignoretimelimitsWhether this user is exempt from time limits (has_capability('mod/quiz:ignoretimelimits', ...)).

Member Function Documentation

◆ accumulate_messages()

quiz_access_manager::accumulate_messages (   $messages,
  $new 
)
protected

Accumulates an array of messages.

Parameters
array$messagesthe current list of messages.
string | array$newthe new messages or messages.
Return values
arraythe updated array of messages.

◆ add_settings_form_fields()

static quiz_access_manager::add_settings_form_fields ( mod_quiz_mod_form  $quizform,
MoodleQuickForm  $mform 
)
static

Add any form fields that the access rules require to the settings form.

Note that the standard plugins do not use this mechanism, becuase all their settings are stored in the quiz table.

Parameters
mod_quiz_mod_form$quizformthe quiz settings form that is being built.
MoodleQuickForm$mformthe wrapped MoodleQuickForm.

◆ attempt_must_be_in_popup()

quiz_access_manager::attempt_must_be_in_popup ( )
Return values
boleanif this quiz should only be shown to students in a popup window.

◆ back_to_view_page()

quiz_access_manager::back_to_view_page (   $output,
  $message = '' 
)

Send the user back to the quiz view page.

Normally this is just a redirect, but If we were in a secure window, we close this window, and reload the view window we came from.

This method does not return;

Parameters
mod_quiz_renderer$outputthe quiz renderer.
string$messageoptional message to output while redirecting.

◆ current_attempt_finished()

quiz_access_manager::current_attempt_finished ( )

Inform the rules that the current attempt is finished.

This is use, for example by the password rule, to clear the flag in the session.

◆ delete_settings()

static quiz_access_manager::delete_settings (   $quiz)
static

Delete any rule-specific settings when the quiz is deleted.

Note that the standard plugins do not use this mechanism because their settings are stored in the quiz table.

Parameters
object$quizthe data from the database, including $quiz->id which is the id of the quiz being deleted.
Since
Moodle 2.7.1, 2.6.4, 2.5.7

◆ describe_rules()

quiz_access_manager::describe_rules ( )

Provide a description of the rules that apply to this quiz, such as is shown at the top of the quiz view page.

Note that not all rules consider themselves important enough to output a description.

Return values
arrayan array of description messages which may be empty. It would be sensible to output each one surrounded by <p> tags.

◆ get_active_rule_names()

quiz_access_manager::get_active_rule_names ( )
Return values
arraythe class names of all the active rules. Mainly useful for debugging.

◆ get_browser_security_choices()

static quiz_access_manager::get_browser_security_choices ( )
static

The the options for the Browser security settings menu.

Return values
arraykey => lang string.

◆ get_end_time()

quiz_access_manager::get_end_time (   $attempt)

Compute when the attempt must be submitted.

Parameters
object$attemptthe data from the relevant quiz_attempts row.
Return values
int|falsethe attempt close time. False if there is no limit.

◆ get_load_sql()

static quiz_access_manager::get_load_sql (   $quizid,
  $rules,
  $basefields 
)
staticprotected

Build the SQL for loading all the access settings in one go.

Parameters
int$quizidthe quiz id.
string$basefieldsinitial part of the select list.
Return values
arraywith two elements, the sql and the placeholder values. If $basefields is '' then you must allow for the possibility that there is no data to load, in which case this method returns $sql = ''.

◆ get_popup_options()

quiz_access_manager::get_popup_options ( )
Return values
arrayany options that are required for showing the attempt page in a popup window.

◆ get_preflight_check_form()

quiz_access_manager::get_preflight_check_form ( moodle_url  $url,
  $attemptid 
)

Build the form required to do the pre-flight checks.

Parameters
moodle_url$urlthe form action URL.
int | null$attemptidthe id of the current attempt, if there is one, otherwise null.
Return values
mod_quiz_preflight_check_formthe form.

◆ get_rule_classes()

static quiz_access_manager::get_rule_classes ( )
staticprotected
Return values
arrayof all the installed rule class names.

◆ get_time_left_display()

quiz_access_manager::get_time_left_display (   $attempt,
  $timenow 
)

Compute what should be displayed to the user for time remaining in this attempt.

Parameters
object$attemptthe data from the relevant quiz_attempts row.
int$timenowthe time to consider as 'now'.
Return values
int|falsethe number of seconds remaining for this attempt. False if no limit should be displayed.

◆ is_finished()

quiz_access_manager::is_finished (   $numprevattempts,
  $lastattempt 
)

Do any of the rules mean that this student will no be allowed any further attempts at this quiz.

Used, for example, to change the label by the grade displayed on the view page from 'your current grade is' to 'your final grade is'.

Parameters
int$numattemptsthe number of previous attempts this user has made.
object$lastattemptinformation about the user's last completed attempt.
Return values
booltrue if there is no way the user will ever be allowed to attempt this quiz again.

◆ is_preflight_check_required()

quiz_access_manager::is_preflight_check_required (   $attemptid)
Parameters
int | null$attemptidthe id of the current attempt, if there is one, otherwise null.
Return values
boolwhether a check is required before the user starts/continues their attempt.

◆ load_quiz_and_settings()

static quiz_access_manager::load_quiz_and_settings (   $quizid)
static

Load the quiz settings and any settings required by the access rules.

We try to do this with a single DB query.

Note that the standard plugins do not use this mechanism, becuase all their settings are stored in the quiz table.

Parameters
int$quizidthe quiz id.
Return values
objectmdl_quiz row with extra fields.

◆ load_settings()

static quiz_access_manager::load_settings (   $quizid)
static

Load any settings required by the access rules.

We try to do this with a single DB query.

Note that the standard plugins do not use this mechanism, becuase all their settings are stored in the quiz table.

Parameters
int$quizidthe quiz id.
Return values
arraysetting value name => value. The value names should all start with the name of the corresponding plugin to avoid collisions.

◆ make_review_link()

quiz_access_manager::make_review_link (   $attempt,
  $reviewoptions,
  $output 
)

Make some text into a link to review the quiz, if that is appropriate.

Parameters
string$linktextsome text.
object$attemptthe attempt object
Return values
stringsome HTML, the $linktext either unmodified or wrapped in a link to the review page.

◆ make_rules()

quiz_access_manager::make_rules (   $quizobj,
  $timenow,
  $canignoretimelimits 
)
protected

Make all the rules relevant to a particular quiz.

Parameters
quiz$quizobjinformation about the quiz in question.
int$timenowthe time that should be considered as 'now'.
bool$canignoretimelimitswhether the current user is exempt from time limits by the mod/quiz:ignoretimelimits capability.
Return values
arrayof .

◆ notify_preflight_check_passed()

quiz_access_manager::notify_preflight_check_passed (   $attemptid)

The pre-flight check has passed.

This is a chance to record that fact in some way.

Parameters
int | null$attemptidthe id of the current attempt, if there is one, otherwise null.

◆ prevent_access()

quiz_access_manager::prevent_access ( )

Whether the user should be blocked from starting a new attempt or continuing an attempt now.

If there are any restrictions in force now, return an array of reasons why access should be blocked. If access is OK, return false.

Return values
mixedAn array of reason why access is not allowed, or an empty array (== false) if access should be allowed.

◆ prevent_new_attempt()

quiz_access_manager::prevent_new_attempt (   $numprevattempts,
  $lastattempt 
)

Whether or not a user should be allowed to start a new attempt at this quiz now.

If there are any restrictions in force now, return an array of reasons why access should be blocked. If access is OK, return false.

Parameters
int$numattemptsthe number of previous attempts this user has made.
object | false$lastattemptinformation about the user's last completed attempt. if there is not a previous attempt, the false is passed.
Return values
mixedAn array of reason why access is not allowed, or an empty array (== false) if access should be allowed.

◆ save_settings()

static quiz_access_manager::save_settings (   $quiz)
static

Save any submitted settings when the quiz settings form is submitted.

Note that the standard plugins do not use this mechanism because their settings are stored in the quiz table.

Parameters
object$quizthe data from the quiz form, including $quiz->id which is the id of the quiz being saved.

◆ setup_attempt_page()

quiz_access_manager::setup_attempt_page (   $page)

Sets up the attempt (review or summary) page with any properties required by the access rules.

Parameters
moodle_page$pagethe page object to initialise.

◆ validate_preflight_check()

quiz_access_manager::validate_preflight_check (   $data,
  $files,
  $attemptid 
)

Run the preflight checks using the given data in all the rules supporting them.

Parameters
array$datapassed data for validation
array$filesun-used, Moodle seems to not support it anymore
int | null$attemptidthe id of the current attempt, if there is one, otherwise null.
Return values
arrayof errors, empty array means no erros
Since
Moodle 3.1

◆ validate_settings_form_fields()

static quiz_access_manager::validate_settings_form_fields ( array  $errors,
array  $data,
  $files,
mod_quiz_mod_form  $quizform 
)
static

Validate the data from any form fields added using add_settings_form_fields().

Parameters
array$errorsthe errors found so far.
array$datathe submitted form data.
array$filesinformation about any uploaded files.
mod_quiz_mod_form$quizformthe quiz form object.
Return values
array::$errorsthe updated $errors array.

The documentation for this class was generated from the following file: