mirror of
https://github.com/moodle/moodle.git
synced 2025-03-06 08:49:53 +01:00
This was implemented by Matt Petro of the University of Wisconsin - Madison Engineering School and Math Department. Many thanks. Reviewed by and committed by Tim Hunt. This adds a new Overrides tab to the UI, with sub-tabs Group overrides and User overrides. Each of those lists all the overrides that currently exist, and lets you manage them and create more. When a quiz is being attempted, the override that applies to the current user is combined with the current quiz settings loaded from the quiz table (normally called $quiz). If there are both user and group overrides, then just the specific user override is used (more specific). If the user is in several groups, then the overrides are combined to give the most permissive set of options. There is one new database table quiz_overrides, to store the overrides.
213 lines
8.6 KiB
PHP
213 lines
8.6 KiB
PHP
<?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/>.
|
|
|
|
|
|
/**
|
|
* Settings form for overrides in the quiz module.
|
|
*
|
|
* @package mod_quiz
|
|
* @copyright 2010 Matt Petro
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
require_once $CFG->libdir.'/formslib.php';
|
|
|
|
class quiz_override_form extends moodleform {
|
|
|
|
protected $cm; // course module object
|
|
protected $quiz; // quiz object
|
|
protected $context; // context object
|
|
protected $groupmode; // editing group override (true) or user override (false)
|
|
protected $groupid; // groupid, if provided
|
|
protected $userid; // userid, if provided
|
|
|
|
public function quiz_override_form($submiturl, $cm, $quiz, $context, $groupmode, $override) {
|
|
|
|
$this->cm = $cm;
|
|
$this->quiz = $quiz;
|
|
$this->context = $context;
|
|
$this->groupmode = $groupmode;
|
|
$this->groupid = empty($override->groupid) ? 0 : $override->groupid;
|
|
$this->userid = empty($override->userid) ? 0 : $override->userid;
|
|
|
|
parent::moodleform($submiturl, null, 'post');
|
|
|
|
}
|
|
|
|
public function definition() {
|
|
global $CFG, $USER, $DB;
|
|
|
|
$cm = $this->cm;
|
|
$mform = $this->_form;
|
|
|
|
$mform->addElement('header', 'override', get_string('override', 'quiz'));
|
|
|
|
if ($this->groupmode) {
|
|
// group override
|
|
if ($this->groupid) {
|
|
// There is already a groupid, so freeze the selector
|
|
$groupchoices = array();
|
|
$groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
|
|
$mform->addElement('select', 'groupid', get_string('overridegroup', 'quiz'), $groupchoices);
|
|
$mform->freeze('groupid');
|
|
} else {
|
|
// Prepare the list of groups
|
|
$groups = groups_get_all_groups($cm->course, null, $cm->groupingid);
|
|
if (empty($groups)) {
|
|
$groups = array();
|
|
}
|
|
|
|
$groupchoices = array();
|
|
foreach ($groups as $group) {
|
|
$groupchoices[$group->id] = $group->name;
|
|
}
|
|
unset($groups);
|
|
|
|
if (count($groupchoices) == 0) {
|
|
$groupchoices[0] = get_string('none');
|
|
}
|
|
|
|
$mform->addElement('select', 'groupid', get_string('overridegroup', 'quiz'), $groupchoices);
|
|
$mform->addRule('groupid', get_string('required'), 'required', null, 'client');
|
|
}
|
|
} else {
|
|
//user override
|
|
if ($this->userid) {
|
|
// There is already a userid, so freeze the selector
|
|
$user = $DB->get_record('user', array('id'=>$this->userid));
|
|
$userchoices = array();
|
|
$userchoices[$this->userid] = fullname($user);
|
|
$mform->addElement('select', 'userid', get_string('overrideuser', 'quiz'), $userchoices);
|
|
$mform->freeze('userid');
|
|
} else {
|
|
// Prepare the list of users
|
|
if (!empty($cm->groupingid)) {
|
|
$groups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
|
|
$groups = array_keys($groups);
|
|
} else {
|
|
$groups = null;
|
|
}
|
|
$users = get_users_by_capability($this->context, 'mod/quiz:attempt', 'u.id,u.firstname,u.lastname,u.email' ,
|
|
'firstname ASC, lastname ASC', '', '', $groups, '', false, true);
|
|
if (empty($users)) {
|
|
$users = array();
|
|
}
|
|
|
|
$userchoices = array();
|
|
foreach ($users as $id=>$user) {
|
|
if (empty($invalidusers[$id]) || (!empty($override) && $id == $override->userid)) {
|
|
$userchoices[$id] = fullname($user) . ', ' . $user->email;
|
|
}
|
|
}
|
|
unset($users);
|
|
|
|
if (count($userchoices) == 0) {
|
|
$userchoices[0] = get_string('none');
|
|
}
|
|
$mform->addElement('searchableselector', 'userid', get_string('overrideuser', 'quiz'), $userchoices);
|
|
$mform->addRule('userid', get_string('required'), 'required', null, 'client');
|
|
}
|
|
}
|
|
|
|
// Password
|
|
// This field has to be above the date and timelimit fields,
|
|
// otherwise browsers will clear it when those fields are changed
|
|
$mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
|
|
$mform->setType('password', PARAM_TEXT);
|
|
$mform->setHelpButton('password', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
|
|
$mform->setDefault('password', $this->quiz->password);
|
|
|
|
// Open and close dates.
|
|
$mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional' => true));
|
|
$mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
|
|
$mform->setDefault('timeopen', $this->quiz->timeopen);
|
|
|
|
$mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional' => true));
|
|
$mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
|
|
$mform->setDefault('timeclose', $this->quiz->timeclose);
|
|
|
|
// Time limit.
|
|
$mform->addElement('duration', 'timelimit', get_string('quiztimer', 'quiz'), array('optional' => true));
|
|
$mform->setHelpButton('timelimit', array('timelimit', get_string('quiztimer','quiz'), 'quiz'));
|
|
$mform->setDefault('timelimit', $this->quiz->timelimit);
|
|
|
|
// Number of attempts.
|
|
$attemptoptions = array('0' => get_string('unlimited'));
|
|
for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
|
|
$attemptoptions[$i] = $i;
|
|
}
|
|
$mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
|
|
$mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed','quiz'), 'quiz'));
|
|
$mform->setDefault('attempts', $this->quiz->attempts);
|
|
|
|
// Submit buttons
|
|
$mform->addElement('submit', 'resetbutton', get_string('reverttodefaults','quiz'));
|
|
|
|
$buttonarray = array();
|
|
$buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('save', 'quiz'));
|
|
$buttonarray[] = $mform->createElement('submit', 'againbutton', get_string('saveoverrideandstay', 'quiz'));
|
|
$buttonarray[] = $mform->createElement('cancel');
|
|
|
|
$mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
|
|
$mform->closeHeaderBefore('buttonbar');
|
|
|
|
}
|
|
|
|
// form verification
|
|
public function validation($data, $files) {
|
|
global $COURSE, $DB;
|
|
$errors = parent::validation($data, $files);
|
|
|
|
$mform =& $this->_form;
|
|
$quiz = $this->quiz;
|
|
|
|
if ($mform->elementExists('userid')) {
|
|
if (empty($data['userid'])) {
|
|
$errors['userid'] = get_string('required');
|
|
}
|
|
}
|
|
|
|
if ($mform->elementExists('groupid')) {
|
|
if (empty($data['groupid'])) {
|
|
$errors['groupid'] = get_string('required');
|
|
}
|
|
}
|
|
|
|
// Ensure that the dates make sense
|
|
if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
|
|
if ($data['timeclose'] < $data['timeopen'] ) {
|
|
$errors['timeclose'] = get_string('closebeforeopen', 'quiz');
|
|
}
|
|
}
|
|
|
|
// Ensure that at least one quiz setting was changed
|
|
$changed = false;
|
|
$keys = array('timeopen','timeclose', 'timelimit', 'attempts', 'password');
|
|
foreach ($keys as $key) {
|
|
if ($data[$key] != $quiz->{$key}) {
|
|
$changed = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$changed) {
|
|
$errors['timeopen'] = get_string('nooverridedata', 'quiz');
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|