MDL-76614 quiz: quiz_access_rule_base => mod_quiz\local\access_rule_base

This commit is contained in:
Tim Hunt 2022-12-16 18:07:38 +00:00
parent dda9ba07f3
commit 2ff112c688
18 changed files with 413 additions and 471 deletions

View File

@ -15,323 +15,13 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for rules that restrict the ability to attempt a quiz.
* File only retained to prevent fatal errors in code that tries to require/include this.
*
* @package mod_quiz
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @todo MDL-76612 delete this file as part of Moodle 4.6 development.
* @deprecated This file is no longer required in Moodle 4.2+.
*/
use mod_quiz\form\preflight_check_form;
defined('MOODLE_INTERNAL') || die();
debugging('This file is no longer required in Moodle 4.2+. Please do not include/require it.', DEBUG_DEVELOPER);
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/**
* A base class that defines the interface for the various quiz access rules.
* 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.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
*/
abstract class quiz_access_rule_base {
/** @var stdClass the quiz settings. */
protected $quiz;
/** @var quiz the quiz object. */
protected $quizobj;
/** @var int the time to use as 'now'. */
protected $timenow;
/**
* Create an instance of this rule for a particular quiz.
* @param quiz $quizobj information about the quiz in question.
* @param int $timenow the time that should be considered as 'now'.
*/
public function __construct($quizobj, $timenow) {
$this->quizobj = $quizobj;
$this->quiz = $quizobj->get_quiz();
$this->timenow = $timenow;
}
/**
* Return an appropriately configured instance of this rule, if it is applicable
* to the given quiz, otherwise return null.
* @param quiz $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
* time limits by the mod/quiz:ignoretimelimits capability.
* @return quiz_access_rule_base|null the rule, if applicable, else null.
*/
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
return null;
}
/**
* Whether or not a user should be allowed to start a new attempt at this quiz now.
* @param int $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return string false if access should be allowed, a message explaining the
* reason if access should be prevented.
*/
public function prevent_new_attempt($numprevattempts, $lastattempt) {
return false;
}
/**
* 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
* reason if access should be prevented.
*/
public function prevent_access() {
return false;
}
/**
* @param int|null $attemptid the id of the current attempt, if there is one,
* otherwise null.
* @return bool whether a check is required before the user starts/continues
* their attempt.
*/
public function is_preflight_check_required($attemptid) {
return false;
}
/**
* Add any field you want to pre-flight check form. You should only do
* something here if {@link 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,
* otherwise null.
*/
public function add_preflight_check_form_fields(preflight_check_form $quizform,
MoodleQuickForm $mform, $attemptid) {
// Do nothing by default.
}
/**
* Validate the pre-flight check form submission. You should only do
* something here if {@link 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,
* otherwise null.
* @return array the update $errors array;
*/
public function validate_preflight_check($data, $files, $errors, $attemptid) {
return $errors;
}
/**
* 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,
* otherwise null.
*/
public function notify_preflight_check_passed($attemptid) {
// Do nothing by default.
}
/**
* 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.
*/
public function current_attempt_finished() {
// Do nothing by default.
}
/**
* Information, such as might be shown on the quiz view page, relating to this restriction.
* There is no obligation to return anything. If it is not appropriate to tell students
* about this rule, then just return ''.
* @return mixed a message, or array of messages, explaining the restriction
* (may be '' if no message is appropriate).
*/
public function description() {
return '';
}
/**
* If this rule can determine that this user will never be allowed another attempt at
* this quiz, then return true. This is used so we can 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 $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return bool true if this rule means that this user will never be allowed another
* attempt at this quiz.
*/
public function is_finished($numprevattempts, $lastattempt) {
return false;
}
/**
* If, because of this rule, the user has to finish their attempt by a certain time,
* you should override this method to return the attempt end time.
* @param object $attempt the current attempt
* @return mixed the attempt close time, or false if there is no close time.
*/
public function end_time($attempt) {
return false;
}
/**
* 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 object $attempt the current attempt
* @param int $timenow the time now. We don't use $this->timenow, so we can
* give the user a more accurate indication of how much time is left.
* @return mixed the time left in seconds (can be negative) or false if there is no limit.
*/
public function time_left_display($attempt, $timenow) {
$endtime = $this->end_time($attempt);
if ($endtime === false) {
return false;
}
return $endtime - $timenow;
}
/**
* @return boolean whether this rule requires that the attemp (and review)
* pages must be displayed in a pop-up window.
*/
public function attempt_must_be_in_popup() {
return false;
}
/**
* @return array any options that are required for showing the attempt page
* in a popup window.
*/
public function get_popup_options() {
return array();
}
/**
* 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.
*/
public function setup_attempt_page($page) {
// Do nothing by default.
}
/**
* 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.
* For example array('ipaddress', 'password').
*/
public function get_superceded_rules() {
return array();
}
/**
* Add any fields that this rule requires to the quiz settings form. This
* method is called from {@link 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.
*/
public static function add_settings_form_fields(
mod_quiz_mod_form $quizform, MoodleQuickForm $mform) {
// By default do nothing.
}
/**
* Validate the data from any form fields added using {@link 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.
*/
public static function validate_settings_form_fields(array $errors,
array $data, $files, mod_quiz_mod_form $quizform) {
return $errors;
}
/**
* @return array key => lang string any choices to add to the quiz Browser
* security settings menu.
*/
public static function get_browser_security_choices() {
return array();
}
/**
* Save any submitted settings when the quiz settings form is submitted. This
* is called from {@link quiz_after_add_or_update()} in lib.php.
* @param object $quiz the data from the quiz form, including $quiz->id
* which is the id of the quiz being saved.
*/
public static function save_settings($quiz) {
// By default do nothing.
}
/**
* Delete any rule-specific settings when the quiz is deleted. This is called
* from {@link quiz_delete_instance()} in lib.php.
* @param object $quiz the 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
*/
public static function delete_settings($quiz) {
// By default do nothing.
}
/**
* 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 {@link get_extra_settings()} method instead, but that has
* performance implications.
*
* @param int $quizid the id of the quiz we are loading settings for. This
* can also be accessed as quiz.id in the SQL. (quiz is a table alisas for {quiz}.)
* @return array with three elements:
* 1. fields: any fields to add to the select list. These should be alised
* if neccessary so that the field name starts the name of the plugin.
* 2. joins: any joins (should probably be LEFT JOINS) with other tables that
* are needed.
* 3. params: array of placeholder values that are needed by the SQL. You must
* used named placeholders, and the placeholder names should start with the
* plugin name, to avoid collisions.
*/
public static function get_settings_sql($quizid) {
return array('', '', array());
}
/**
* 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
* start with the name of your plugin to avoid collisions.
*/
public static function get_extra_settings($quizid) {
return array();
}
}

View File

@ -14,28 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_delaybetweenattempts plugin.
*
* @package quizaccess
* @subpackage delaybetweenattempts
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule imposing the delay between attempts settings.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_delaybetweenattempts
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_delaybetweenattempts extends quiz_access_rule_base {
class quizaccess_delaybetweenattempts extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
if (empty($quizobj->get_quiz()->delay1) && empty($quizobj->get_quiz()->delay2)) {

View File

@ -14,28 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_ipaddress plugin.
*
* @package quizaccess
* @subpackage ipaddress
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule implementing the ipaddress check against the ->subnet setting.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_ipaddress
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_ipaddress extends quiz_access_rule_base {
class quizaccess_ipaddress extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
if (empty($quizobj->get_quiz()->subnet)) {

View File

@ -14,28 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_numattempts plugin.
*
* @package quizaccess
* @subpackage numattempts
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule controlling the number of attempts allowed.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_numattempts
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_numattempts extends quiz_access_rule_base {
class quizaccess_numattempts extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {

View File

@ -14,28 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_offlineattempts plugin.
*
* @package quizaccess_offlineattempts
* @copyright 2016 Juan Leyva
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_quiz\form\preflight_check_form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule implementing the offlineattempts check.
*
* @package quizaccess_offlineattempts
* @copyright 2016 Juan Leyva
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.2
*/
class quizaccess_offlineattempts extends quiz_access_rule_base {
class quizaccess_offlineattempts extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
global $CFG;

View File

@ -14,28 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_openclosedate plugin.
*
* @package quizaccess
* @subpackage openclosedate
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule enforcing open and close dates.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_openclosedate
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_openclosedate extends quiz_access_rule_base {
class quizaccess_openclosedate extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
// This rule is always used, even if the quiz has no open or close date.

View File

@ -14,29 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_password plugin.
*
* @package quizaccess
* @subpackage password
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_quiz\form\preflight_check_form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule implementing the password check.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_password
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_password extends quiz_access_rule_base {
class quizaccess_password extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
if (empty($quizobj->get_quiz()->password)) {

View File

@ -14,6 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use mod_quiz\local\access_rule_base;
use quizaccess_seb\access_manager;
use quizaccess_seb\quiz_settings;
use quizaccess_seb\settings_provider;
use quizaccess_seb\event\access_prevented;
/**
* Implementation of the quizaccess_seb plugin.
*
@ -23,24 +29,7 @@
* @copyright 2019 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use quizaccess_seb\access_manager;
use quizaccess_seb\quiz_settings;
use quizaccess_seb\settings_provider;
use \quizaccess_seb\event\access_prevented;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
/**
* Implementation of the quizaccess_seb plugin.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb extends quiz_access_rule_base {
class quizaccess_seb extends access_rule_base {
/** @var access_manager $accessmanager Instance to manage the access to the quiz for this plugin. */
private $accessmanager;
@ -65,9 +54,9 @@ class quizaccess_seb extends quiz_access_rule_base {
* @param int $timenow the time that should be considered as 'now'.
* @param bool $canignoretimelimits whether the current user is exempt from
* time limits by the mod/quiz:ignoretimelimits capability.
* @return quiz_access_rule_base|null the rule, if applicable, else null.
* @return access_rule_base|null the rule, if applicable, else null.
*/
public static function make (quiz $quizobj, $timenow, $canignoretimelimits) {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
$accessmanager = new access_manager($quizobj);
// If Safe Exam Browser is not required, this access rule is not applicable.
if (!$accessmanager->seb_required()) {

View File

@ -23,6 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_quiz\local\access_rule_base;
use quizaccess_seb\access_manager;
use quizaccess_seb\settings_provider;
@ -247,7 +248,7 @@ trait quizaccess_seb_test_helper_trait {
/**
* A helper method to make the rule form the currently created quiz and course.
*
* @return \quiz_access_rule_base|null
* @return access_rule_base|null
*/
protected function make_rule() {
return \quizaccess_seb::make(

View File

@ -14,29 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_securewindow plugin.
*
* @package quizaccess
* @subpackage securewindow
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule for ensuring that the quiz is opened in a popup, with some JavaScript
* to prevent copying and pasting, etc.
*
* @package quizaccess_securewindow
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_securewindow extends quiz_access_rule_base {
class quizaccess_securewindow extends access_rule_base {
/** @var array options that should be used for opening the secure popup. */
protected static $popupoptions = array(
'left' => 0,

View File

@ -32,7 +32,7 @@ require_once($CFG->dirroot . '/mod/quiz/accessrule/securewindow/rule.php');
* @copyright 2008 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \quiz_access_rule_base
* @covers \mod_quiz\local\access_rule_base
* @covers \quizaccess_securewindow
*/
class rule_test extends \basic_testcase {

View File

@ -14,30 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton of the quizaccess_timelimit plugin.
*
* @package quizaccess
* @subpackage timelimit
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_quiz\form\preflight_check_form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
use mod_quiz\local\access_rule_base;
/**
* A rule representing the time limit. It does not actually restrict access, but we use this
* A rule representing the time limit.
*
* It does not actually restrict access, but we use this
* class to encapsulate some of the relevant code.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package quizaccess_timelimit
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_timelimit extends quiz_access_rule_base {
class quizaccess_timelimit extends access_rule_base {
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {

View File

@ -6,6 +6,11 @@ Overview of this plugin type at http://docs.moodle.org/dev/Quiz_access_rules
* Note that class mod_quiz_preflight_check_form has been renamed to
mod_quiz\form\preflight_check_form.
* The base class quiz_access_rule_base has been moved to mod_quiz\local\access_rule_base.
Please:
1. update your class declaration to ... extends access_rule_base {
2. Add use mod_quiz\local\access_rule_base;
3. Remove require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
=== 2.8, 2.7.1, 2.6.4 and 2.5.7 ===

View File

@ -18,6 +18,7 @@ namespace mod_quiz;
use core_component;
use mod_quiz\form\preflight_check_form;
use mod_quiz\local\access_rule_base;
use mod_quiz\question\display_options;
use mod_quiz_mod_form;
use mod_quiz_renderer;
@ -46,7 +47,7 @@ class access_manager {
/** @var int the time to be considered as 'now'. */
protected $timenow;
/** @var \quiz_access_rule_base instances of the active rules for this quiz. */
/** @var access_rule_base instances of the active rules for this quiz. */
protected $rules = [];
/**
@ -71,7 +72,7 @@ class access_manager {
* @param int $timenow the time that should be considered as 'now'.
* @param bool $canignoretimelimits whether the current user is exempt from
* time limits by the mod/quiz:ignoretimelimits capability.
* @return array of {@see quiz_access_rule_base}s.
* @return access_rule_base[] rules that apply to this quiz.
*/
protected function make_rules(quiz $quizobj, int $timenow, bool $canignoretimelimits): array {

View File

@ -822,7 +822,7 @@ class mod_quiz_external extends external_api {
// Check that this attempt belongs to this user.
if ($attemptobj->get_userid() != $USER->id) {
throw new moodle_exception('notyourattempt', 'quiz', $quizobj->view_url());
throw new moodle_exception('notyourattempt', 'quiz', $attemptobj->view_url());
}
// General capabilities check.
@ -840,7 +840,7 @@ class mod_quiz_external extends external_api {
$messages = $accessmanager->prevent_access();
if (!$ispreviewuser && $messages) {
throw new moodle_exception('attempterror', 'quiz', $quizobj->view_url());
throw new moodle_exception('attempterror', 'quiz', $attemptobj->view_url());
}
}

View File

@ -0,0 +1,344 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for rules that restrict the ability to attempt a quiz.
*
* @package mod_quiz
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\local;
use mod_quiz\form\preflight_check_form;
use mod_quiz_mod_form;
use moodle_page;
use MoodleQuickForm;
use quiz;
use stdClass;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
/**
* A base class that defines the interface for the various quiz access rules.
* 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.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.2
*/
abstract class access_rule_base {
/** @var stdClass the quiz settings. */
protected $quiz;
/** @var quiz the quiz object. */
protected $quizobj;
/** @var int the time to use as 'now'. */
protected $timenow;
/**
* Create an instance of this rule for a particular quiz.
* @param quiz $quizobj information about the quiz in question.
* @param int $timenow the time that should be considered as 'now'.
*/
public function __construct($quizobj, $timenow) {
$this->quizobj = $quizobj;
$this->quiz = $quizobj->get_quiz();
$this->timenow = $timenow;
}
/**
* Return an appropriately configured instance of this rule, if it is applicable
* to the given quiz, otherwise return null.
* @param quiz $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
* time limits by the mod/quiz:ignoretimelimits capability.
* @return self|null the rule, if applicable, else null.
*/
public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
return null;
}
/**
* Whether or not a user should be allowed to start a new attempt at this quiz now.
* @param int $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return string false if access should be allowed, a message explaining the
* reason if access should be prevented.
*/
public function prevent_new_attempt($numprevattempts, $lastattempt) {
return false;
}
/**
* 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
* reason if access should be prevented.
*/
public function prevent_access() {
return false;
}
/**
* @param int|null $attemptid the id of the current attempt, if there is one,
* otherwise null.
* @return bool whether a check is required before the user starts/continues
* their attempt.
*/
public function is_preflight_check_required($attemptid) {
return false;
}
/**
* Add any field you want to pre-flight check form. You should only do
* something here if {@link 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,
* otherwise null.
*/
public function add_preflight_check_form_fields(preflight_check_form $quizform,
MoodleQuickForm $mform, $attemptid) {
// Do nothing by default.
}
/**
* Validate the pre-flight check form submission. You should only do
* something here if {@link 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,
* otherwise null.
* @return array the update $errors array;
*/
public function validate_preflight_check($data, $files, $errors, $attemptid) {
return $errors;
}
/**
* 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,
* otherwise null.
*/
public function notify_preflight_check_passed($attemptid) {
// Do nothing by default.
}
/**
* 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.
*/
public function current_attempt_finished() {
// Do nothing by default.
}
/**
* Information, such as might be shown on the quiz view page, relating to this restriction.
* There is no obligation to return anything. If it is not appropriate to tell students
* about this rule, then just return ''.
* @return mixed a message, or array of messages, explaining the restriction
* (may be '' if no message is appropriate).
*/
public function description() {
return '';
}
/**
* If this rule can determine that this user will never be allowed another attempt at
* this quiz, then return true. This is used so we can 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 $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return bool true if this rule means that this user will never be allowed another
* attempt at this quiz.
*/
public function is_finished($numprevattempts, $lastattempt) {
return false;
}
/**
* If, because of this rule, the user has to finish their attempt by a certain time,
* you should override this method to return the attempt end time.
* @param object $attempt the current attempt
* @return mixed the attempt close time, or false if there is no close time.
*/
public function end_time($attempt) {
return false;
}
/**
* 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 object $attempt the current attempt
* @param int $timenow the time now. We don't use $this->timenow, so we can
* give the user a more accurate indication of how much time is left.
* @return mixed the time left in seconds (can be negative) or false if there is no limit.
*/
public function time_left_display($attempt, $timenow) {
$endtime = $this->end_time($attempt);
if ($endtime === false) {
return false;
}
return $endtime - $timenow;
}
/**
* @return boolean whether this rule requires that the attemp (and review)
* pages must be displayed in a pop-up window.
*/
public function attempt_must_be_in_popup() {
return false;
}
/**
* @return array any options that are required for showing the attempt page
* in a popup window.
*/
public function get_popup_options() {
return array();
}
/**
* 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.
*/
public function setup_attempt_page($page) {
// Do nothing by default.
}
/**
* 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.
* For example array('ipaddress', 'password').
*/
public function get_superceded_rules() {
return array();
}
/**
* Add any fields that this rule requires to the quiz settings form. This
* method is called from {@link 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.
*/
public static function add_settings_form_fields(
mod_quiz_mod_form $quizform, MoodleQuickForm $mform) {
// By default do nothing.
}
/**
* Validate the data from any form fields added using {@link 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.
*/
public static function validate_settings_form_fields(array $errors,
array $data, $files, mod_quiz_mod_form $quizform) {
return $errors;
}
/**
* @return array key => lang string any choices to add to the quiz Browser
* security settings menu.
*/
public static function get_browser_security_choices() {
return array();
}
/**
* Save any submitted settings when the quiz settings form is submitted. This
* is called from {@link quiz_after_add_or_update()} in lib.php.
* @param object $quiz the data from the quiz form, including $quiz->id
* which is the id of the quiz being saved.
*/
public static function save_settings($quiz) {
// By default do nothing.
}
/**
* Delete any rule-specific settings when the quiz is deleted. This is called
* from {@link quiz_delete_instance()} in lib.php.
* @param object $quiz the 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
*/
public static function delete_settings($quiz) {
// By default do nothing.
}
/**
* 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 {@link get_extra_settings()} method instead, but that has
* performance implications.
*
* @param int $quizid the id of the quiz we are loading settings for. This
* can also be accessed as quiz.id in the SQL. (quiz is a table alisas for {quiz}.)
* @return array with three elements:
* 1. fields: any fields to add to the select list. These should be alised
* if neccessary so that the field name starts the name of the plugin.
* 2. joins: any joins (should probably be LEFT JOINS) with other tables that
* are needed.
* 3. params: array of placeholder values that are needed by the SQL. You must
* used named placeholders, and the placeholder names should start with the
* plugin name, to avoid collisions.
*/
public static function get_settings_sql($quizid) {
return array('', '', array());
}
/**
* 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
* start with the name of your plugin to avoid collisions.
*/
public static function get_extra_settings($quizid) {
return array();
}
}

View File

@ -54,4 +54,5 @@ $renamedclasses = [
'quiz_access_manager' => 'mod_quiz\access_manager',
'mod_quiz_preflight_check_form' => 'mod_quiz\form\preflight_check_form',
'quiz_override_form' => 'mod_quiz\form\edit_override_form',
'quiz_access_rule_base' => 'mod_quiz\local\access_rule_base',
];

View File

@ -38,6 +38,7 @@ This files describes API changes in the quiz code.
- quiz_access_manager => mod_quiz\access_manager
- mod_quiz_preflight_check_form => mod_quiz\form\preflight_check_form
- quiz_override_form => mod_quiz\form\edit_override_form
- quiz_access_rule_base => mod_quiz\local\access_rule_base
* The following classes have been deprecated:
- mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts
@ -54,6 +55,7 @@ This files describes API changes in the quiz code.
- mod/quiz/accessmanager_form.php
- mod/quiz/cronlib.php
- mod/quiz/override_form.php
- mod/quiz/accessrule/accessrulebase.php
=== 4.1 ===