2010-03-08 16:01:38 +00:00
|
|
|
<?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.
|
|
|
|
*
|
2011-02-21 16:13:25 +00:00
|
|
|
* @package mod
|
|
|
|
* @subpackage quiz
|
|
|
|
* @copyright 2010 Matt Petro
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2010-03-08 16:01:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-21 16:13:25 +00:00
|
|
|
|
2011-02-23 15:03:35 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2010-05-13 02:02:05 +00:00
|
|
|
|
2011-05-12 00:30:25 +01:00
|
|
|
require_once($CFG->libdir . '/formslib.php');
|
2010-03-08 16:01:38 +00:00
|
|
|
|
2011-02-21 16:13:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form for editing settings overrides.
|
|
|
|
*
|
|
|
|
* @copyright 2010 Matt Petro
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2010-03-08 16:01:38 +00:00
|
|
|
class quiz_override_form extends moodleform {
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
/** @var object course module object. */
|
|
|
|
protected $cm;
|
2010-03-08 16:01:38 +00:00
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
/** @var object the quiz settings object. */
|
|
|
|
protected $quiz;
|
|
|
|
|
|
|
|
/** @var context the quiz context. */
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
/** @var bool editing group override (true) or user override (false). */
|
|
|
|
protected $groupmode;
|
|
|
|
|
|
|
|
/** @var int groupid, if provided. */
|
|
|
|
protected $groupid;
|
|
|
|
|
|
|
|
/** @var int userid, if provided. */
|
|
|
|
protected $userid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param moodle_url $submiturl the form action URL.
|
|
|
|
* @param object course module object.
|
|
|
|
* @param object the quiz settings object.
|
|
|
|
* @param context the quiz context.
|
|
|
|
* @param bool editing group override (true) or user override (false).
|
|
|
|
* @param object $override the override being edited, if it already exists.
|
|
|
|
*/
|
2011-05-12 00:30:25 +01:00
|
|
|
public function __construct($submiturl, $cm, $quiz, $context, $groupmode, $override) {
|
2010-03-08 16:01:38 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2011-05-12 00:30:25 +01:00
|
|
|
parent::__construct($submiturl, null, 'post');
|
2010-03-08 16:01:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-23 16:22:25 +00:00
|
|
|
protected function definition() {
|
2012-01-30 17:47:11 +00:00
|
|
|
global $CFG, $DB;
|
2010-03-08 16:01:38 +00:00
|
|
|
|
|
|
|
$cm = $this->cm;
|
|
|
|
$mform = $this->_form;
|
|
|
|
|
|
|
|
$mform->addElement('header', 'override', get_string('override', 'quiz'));
|
|
|
|
|
|
|
|
if ($this->groupmode) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Group override.
|
2010-03-08 16:01:38 +00:00
|
|
|
if ($this->groupid) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// There is already a groupid, so freeze the selector.
|
2010-03-08 16:01:38 +00:00
|
|
|
$groupchoices = array();
|
|
|
|
$groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('select', 'groupid',
|
|
|
|
get_string('overridegroup', 'quiz'), $groupchoices);
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->freeze('groupid');
|
|
|
|
} else {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Prepare the list of groups.
|
2010-05-10 22:15:17 +00:00
|
|
|
$groups = groups_get_all_groups($cm->course);
|
2010-03-08 16:01:38 +00:00
|
|
|
if (empty($groups)) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Generate an error.
|
2010-05-10 22:15:17 +00:00
|
|
|
$link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
|
|
|
|
print_error('groupsnone', 'quiz', $link);
|
2010-03-08 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$groupchoices = array();
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
$groupchoices[$group->id] = $group->name;
|
|
|
|
}
|
|
|
|
unset($groups);
|
|
|
|
|
|
|
|
if (count($groupchoices) == 0) {
|
|
|
|
$groupchoices[0] = get_string('none');
|
|
|
|
}
|
|
|
|
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('select', 'groupid',
|
|
|
|
get_string('overridegroup', 'quiz'), $groupchoices);
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->addRule('groupid', get_string('required'), 'required', null, 'client');
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-04 12:40:21 +01:00
|
|
|
// User override.
|
2010-03-08 16:01:38 +00:00
|
|
|
if ($this->userid) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// There is already a userid, so freeze the selector.
|
2010-03-08 16:01:38 +00:00
|
|
|
$user = $DB->get_record('user', array('id'=>$this->userid));
|
|
|
|
$userchoices = array();
|
|
|
|
$userchoices[$this->userid] = fullname($user);
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('select', 'userid',
|
|
|
|
get_string('overrideuser', 'quiz'), $userchoices);
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->freeze('userid');
|
|
|
|
} else {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Prepare the list of users.
|
2010-05-10 22:15:17 +00:00
|
|
|
$users = array();
|
2012-08-08 13:56:12 +01:00
|
|
|
list($sort, $sortparams) = users_order_by_sql('u');
|
|
|
|
if (!empty($sortparams)) {
|
|
|
|
throw new coding_exception('users_order_by_sql returned some query parameters. ' .
|
|
|
|
'This is unexpected, and a problem because there is no way to pass these ' .
|
|
|
|
'parameters to get_users_by_capability. See MDL-34657.');
|
|
|
|
}
|
2010-05-10 22:15:17 +00:00
|
|
|
if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Only users from the grouping.
|
2010-03-08 16:01:38 +00:00
|
|
|
$groups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
|
2011-05-12 00:30:25 +01:00
|
|
|
if (!empty($groups)) {
|
|
|
|
$users = get_users_by_capability($this->context, 'mod/quiz:attempt',
|
2013-11-23 15:06:58 -08:00
|
|
|
'u.id, u.email, ' . get_all_user_name_fields(true, 'u'),
|
2012-08-08 13:56:12 +01:00
|
|
|
$sort, '', '', array_keys($groups),
|
2011-05-12 00:30:25 +01:00
|
|
|
'', false, true);
|
2010-05-10 22:15:17 +00:00
|
|
|
}
|
2010-03-08 16:01:38 +00:00
|
|
|
} else {
|
2011-05-12 00:30:25 +01:00
|
|
|
$users = get_users_by_capability($this->context, 'mod/quiz:attempt',
|
2013-11-23 15:06:58 -08:00
|
|
|
'u.id, u.email, ' . get_all_user_name_fields(true, 'u'),
|
2012-08-08 13:56:12 +01:00
|
|
|
$sort, '', '', '', '', false, true);
|
2010-03-08 16:01:38 +00:00
|
|
|
}
|
|
|
|
if (empty($users)) {
|
2012-05-04 12:40:21 +01:00
|
|
|
// Generate an error.
|
2010-05-10 22:15:17 +00:00
|
|
|
$link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
|
|
|
|
print_error('usersnone', 'quiz', $link);
|
2010-03-08 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$userchoices = array();
|
2011-05-12 00:30:25 +01:00
|
|
|
foreach ($users as $id => $user) {
|
|
|
|
if (empty($invalidusers[$id]) || (!empty($override) &&
|
|
|
|
$id == $override->userid)) {
|
2010-03-08 16:01:38 +00:00
|
|
|
$userchoices[$id] = fullname($user) . ', ' . $user->email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($users);
|
|
|
|
|
|
|
|
if (count($userchoices) == 0) {
|
|
|
|
$userchoices[0] = get_string('none');
|
|
|
|
}
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('searchableselector', 'userid',
|
|
|
|
get_string('overrideuser', 'quiz'), $userchoices);
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->addRule('userid', get_string('required'), 'required', null, 'client');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Password.
|
2010-03-08 16:01:38 +00:00
|
|
|
// This field has to be above the date and timelimit fields,
|
2012-05-04 12:40:21 +01:00
|
|
|
// otherwise browsers will clear it when those fields are changed.
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
|
|
|
|
$mform->setType('password', PARAM_TEXT);
|
MDL-21695 quiz: deprecated setHelpButton() and old_help_icon() replaced with new calls
Some help icons dropped without replacement as suggested by Helen, the
Help Writer. Some strings moved to core_question.
AMOS BEGIN
HLP quiz/timelimit.html,[timelimit_help,mod_quiz]
HLP quiz/grademethod.html,[grademethod_help,mod_quiz]
HLP quiz/questionsperpage.html,[newpage_help,mod_quiz]
HLP quiz/shufflewithin.html,[shufflewithin_help,mod_quiz]
HLP quiz/penaltyscheme.html,[penaltyscheme_help,mod_quiz]
HLP quiz/repeatattempts.html,[eachattemptbuildsonthelast_help,mod_quiz]
HLP quiz/reviewoptions.html,[reviewoptionsheading_help,mod_quiz]
HLP quiz/showuserpicture.html,[showuserpicture_help,mod_quiz]
HLP quiz/decimalpoints.html,[decimalplaces_help,mod_quiz]
HLP quiz/decimalplacesquestion.html,[decimalplacesquestion_help,mod_quiz]
HLP quiz/requirepassword.html,[requirepassword_help,mod_quiz]
HLP quiz/requiresubnet.html,[requiresubnet_help,mod_quiz]
HLP quiz/timedelay1.html,[delay1st2nd_help,mod_quiz]
HLP quiz/timedelay2.html,[delaylater_help,mod_quiz]
HLP quiz/overallfeedback.html,[overallfeedback_help,mod_quiz]
HLP quiz_statistics/negcovar.html,[negcovar_help,quiz_statistics]
MOV [penaltyfactor,mod_quiz],[penaltyfactor,core_question]
MOV [penaltyfactor_help,mod_quiz],[penaltyfactor_help,core_question]
HLP quiz/penalty.html,[penaltyfactor_help,core_question]
AMOS END
2010-08-02 20:56:15 +00:00
|
|
|
$mform->addHelpButton('password', 'requirepassword', 'quiz');
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->setDefault('password', $this->quiz->password);
|
|
|
|
|
|
|
|
// Open and close dates.
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('date_time_selector', 'timeopen',
|
|
|
|
get_string('quizopen', 'quiz'), array('optional' => true));
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->setDefault('timeopen', $this->quiz->timeopen);
|
|
|
|
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('date_time_selector', 'timeclose',
|
|
|
|
get_string('quizclose', 'quiz'), array('optional' => true));
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->setDefault('timeclose', $this->quiz->timeclose);
|
|
|
|
|
|
|
|
// Time limit.
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('duration', 'timelimit',
|
|
|
|
get_string('timelimit', 'quiz'), array('optional' => true));
|
MDL-21695 quiz: deprecated setHelpButton() and old_help_icon() replaced with new calls
Some help icons dropped without replacement as suggested by Helen, the
Help Writer. Some strings moved to core_question.
AMOS BEGIN
HLP quiz/timelimit.html,[timelimit_help,mod_quiz]
HLP quiz/grademethod.html,[grademethod_help,mod_quiz]
HLP quiz/questionsperpage.html,[newpage_help,mod_quiz]
HLP quiz/shufflewithin.html,[shufflewithin_help,mod_quiz]
HLP quiz/penaltyscheme.html,[penaltyscheme_help,mod_quiz]
HLP quiz/repeatattempts.html,[eachattemptbuildsonthelast_help,mod_quiz]
HLP quiz/reviewoptions.html,[reviewoptionsheading_help,mod_quiz]
HLP quiz/showuserpicture.html,[showuserpicture_help,mod_quiz]
HLP quiz/decimalpoints.html,[decimalplaces_help,mod_quiz]
HLP quiz/decimalplacesquestion.html,[decimalplacesquestion_help,mod_quiz]
HLP quiz/requirepassword.html,[requirepassword_help,mod_quiz]
HLP quiz/requiresubnet.html,[requiresubnet_help,mod_quiz]
HLP quiz/timedelay1.html,[delay1st2nd_help,mod_quiz]
HLP quiz/timedelay2.html,[delaylater_help,mod_quiz]
HLP quiz/overallfeedback.html,[overallfeedback_help,mod_quiz]
HLP quiz_statistics/negcovar.html,[negcovar_help,quiz_statistics]
MOV [penaltyfactor,mod_quiz],[penaltyfactor,core_question]
MOV [penaltyfactor_help,mod_quiz],[penaltyfactor_help,core_question]
HLP quiz/penalty.html,[penaltyfactor_help,core_question]
AMOS END
2010-08-02 20:56:15 +00:00
|
|
|
$mform->addHelpButton('timelimit', 'timelimit', 'quiz');
|
2010-03-08 16:01:38 +00:00
|
|
|
$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;
|
|
|
|
}
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('select', 'attempts',
|
|
|
|
get_string('attemptsallowed', 'quiz'), $attemptoptions);
|
2010-03-08 16:01:38 +00:00
|
|
|
$mform->setDefault('attempts', $this->quiz->attempts);
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Submit buttons.
|
2011-05-12 00:30:25 +01:00
|
|
|
$mform->addElement('submit', 'resetbutton',
|
|
|
|
get_string('reverttodefaults', 'quiz'));
|
2010-03-08 16:01:38 +00:00
|
|
|
|
|
|
|
$buttonarray = array();
|
2011-05-12 00:30:25 +01:00
|
|
|
$buttonarray[] = $mform->createElement('submit', 'submitbutton',
|
|
|
|
get_string('save', 'quiz'));
|
|
|
|
$buttonarray[] = $mform->createElement('submit', 'againbutton',
|
|
|
|
get_string('saveoverrideandstay', 'quiz'));
|
2010-03-08 16:01:38 +00:00
|
|
|
$buttonarray[] = $mform->createElement('cancel');
|
|
|
|
|
|
|
|
$mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
|
|
|
|
$mform->closeHeaderBefore('buttonbar');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Ensure that the dates make sense.
|
2010-03-08 16:01:38 +00:00
|
|
|
if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
|
|
|
|
if ($data['timeclose'] < $data['timeopen'] ) {
|
|
|
|
$errors['timeclose'] = get_string('closebeforeopen', 'quiz');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 12:40:21 +01:00
|
|
|
// Ensure that at least one quiz setting was changed.
|
2010-03-08 16:01:38 +00:00
|
|
|
$changed = false;
|
2011-05-12 00:30:25 +01:00
|
|
|
$keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
|
2010-03-08 16:01:38 +00:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
if ($data[$key] != $quiz->{$key}) {
|
|
|
|
$changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$changed) {
|
|
|
|
$errors['timeopen'] = get_string('nooverridedata', 'quiz');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
}
|