|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->libdir . '/formslib.php'); 00030 00031 00038 class quiz_override_form extends moodleform { 00039 00040 protected $cm; // course module object 00041 protected $quiz; // quiz object 00042 protected $context; // context object 00043 protected $groupmode; // editing group override (true) or user override (false) 00044 protected $groupid; // groupid, if provided 00045 protected $userid; // userid, if provided 00046 00047 public function __construct($submiturl, $cm, $quiz, $context, $groupmode, $override) { 00048 00049 $this->cm = $cm; 00050 $this->quiz = $quiz; 00051 $this->context = $context; 00052 $this->groupmode = $groupmode; 00053 $this->groupid = empty($override->groupid) ? 0 : $override->groupid; 00054 $this->userid = empty($override->userid) ? 0 : $override->userid; 00055 00056 parent::__construct($submiturl, null, 'post'); 00057 00058 } 00059 00060 protected function definition() { 00061 global $CFG, $DB; 00062 00063 $cm = $this->cm; 00064 $mform = $this->_form; 00065 00066 $mform->addElement('header', 'override', get_string('override', 'quiz')); 00067 00068 if ($this->groupmode) { 00069 // group override 00070 if ($this->groupid) { 00071 // There is already a groupid, so freeze the selector 00072 $groupchoices = array(); 00073 $groupchoices[$this->groupid] = groups_get_group_name($this->groupid); 00074 $mform->addElement('select', 'groupid', 00075 get_string('overridegroup', 'quiz'), $groupchoices); 00076 $mform->freeze('groupid'); 00077 } else { 00078 // Prepare the list of groups 00079 $groups = groups_get_all_groups($cm->course); 00080 if (empty($groups)) { 00081 // generate an error 00082 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id)); 00083 print_error('groupsnone', 'quiz', $link); 00084 } 00085 00086 $groupchoices = array(); 00087 foreach ($groups as $group) { 00088 $groupchoices[$group->id] = $group->name; 00089 } 00090 unset($groups); 00091 00092 if (count($groupchoices) == 0) { 00093 $groupchoices[0] = get_string('none'); 00094 } 00095 00096 $mform->addElement('select', 'groupid', 00097 get_string('overridegroup', 'quiz'), $groupchoices); 00098 $mform->addRule('groupid', get_string('required'), 'required', null, 'client'); 00099 } 00100 } else { 00101 //user override 00102 if ($this->userid) { 00103 // There is already a userid, so freeze the selector 00104 $user = $DB->get_record('user', array('id'=>$this->userid)); 00105 $userchoices = array(); 00106 $userchoices[$this->userid] = fullname($user); 00107 $mform->addElement('select', 'userid', 00108 get_string('overrideuser', 'quiz'), $userchoices); 00109 $mform->freeze('userid'); 00110 } else { 00111 // Prepare the list of users 00112 $users = array(); 00113 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) { 00114 // only users from the grouping 00115 $groups = groups_get_all_groups($cm->course, 0, $cm->groupingid); 00116 if (!empty($groups)) { 00117 $users = get_users_by_capability($this->context, 'mod/quiz:attempt', 00118 'u.id, u.firstname, u.lastname, u.email', 00119 'firstname ASC, lastname ASC', '', '', array_keys($groups), 00120 '', false, true); 00121 } 00122 } else { 00123 $users = get_users_by_capability($this->context, 'mod/quiz:attempt', 00124 'u.id, u.firstname, u.lastname, u.email' , 00125 'firstname ASC, lastname ASC', '', '', '', '', false, true); 00126 } 00127 if (empty($users)) { 00128 // generate an error 00129 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id)); 00130 print_error('usersnone', 'quiz', $link); 00131 } 00132 00133 $userchoices = array(); 00134 foreach ($users as $id => $user) { 00135 if (empty($invalidusers[$id]) || (!empty($override) && 00136 $id == $override->userid)) { 00137 $userchoices[$id] = fullname($user) . ', ' . $user->email; 00138 } 00139 } 00140 unset($users); 00141 00142 if (count($userchoices) == 0) { 00143 $userchoices[0] = get_string('none'); 00144 } 00145 $mform->addElement('searchableselector', 'userid', 00146 get_string('overrideuser', 'quiz'), $userchoices); 00147 $mform->addRule('userid', get_string('required'), 'required', null, 'client'); 00148 } 00149 } 00150 00151 // Password 00152 // This field has to be above the date and timelimit fields, 00153 // otherwise browsers will clear it when those fields are changed 00154 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz')); 00155 $mform->setType('password', PARAM_TEXT); 00156 $mform->addHelpButton('password', 'requirepassword', 'quiz'); 00157 $mform->setDefault('password', $this->quiz->password); 00158 00159 // Open and close dates. 00160 $mform->addElement('date_time_selector', 'timeopen', 00161 get_string('quizopen', 'quiz'), array('optional' => true)); 00162 $mform->setDefault('timeopen', $this->quiz->timeopen); 00163 00164 $mform->addElement('date_time_selector', 'timeclose', 00165 get_string('quizclose', 'quiz'), array('optional' => true)); 00166 $mform->setDefault('timeclose', $this->quiz->timeclose); 00167 00168 // Time limit. 00169 $mform->addElement('duration', 'timelimit', 00170 get_string('timelimit', 'quiz'), array('optional' => true)); 00171 $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); 00172 $mform->setDefault('timelimit', $this->quiz->timelimit); 00173 00174 // Number of attempts. 00175 $attemptoptions = array('0' => get_string('unlimited')); 00176 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) { 00177 $attemptoptions[$i] = $i; 00178 } 00179 $mform->addElement('select', 'attempts', 00180 get_string('attemptsallowed', 'quiz'), $attemptoptions); 00181 $mform->setDefault('attempts', $this->quiz->attempts); 00182 00183 // Submit buttons 00184 $mform->addElement('submit', 'resetbutton', 00185 get_string('reverttodefaults', 'quiz')); 00186 00187 $buttonarray = array(); 00188 $buttonarray[] = $mform->createElement('submit', 'submitbutton', 00189 get_string('save', 'quiz')); 00190 $buttonarray[] = $mform->createElement('submit', 'againbutton', 00191 get_string('saveoverrideandstay', 'quiz')); 00192 $buttonarray[] = $mform->createElement('cancel'); 00193 00194 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false); 00195 $mform->closeHeaderBefore('buttonbar'); 00196 00197 } 00198 00199 // form verification 00200 public function validation($data, $files) { 00201 global $COURSE, $DB; 00202 $errors = parent::validation($data, $files); 00203 00204 $mform =& $this->_form; 00205 $quiz = $this->quiz; 00206 00207 if ($mform->elementExists('userid')) { 00208 if (empty($data['userid'])) { 00209 $errors['userid'] = get_string('required'); 00210 } 00211 } 00212 00213 if ($mform->elementExists('groupid')) { 00214 if (empty($data['groupid'])) { 00215 $errors['groupid'] = get_string('required'); 00216 } 00217 } 00218 00219 // Ensure that the dates make sense 00220 if (!empty($data['timeopen']) && !empty($data['timeclose'])) { 00221 if ($data['timeclose'] < $data['timeopen'] ) { 00222 $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); 00223 } 00224 } 00225 00226 // Ensure that at least one quiz setting was changed 00227 $changed = false; 00228 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password'); 00229 foreach ($keys as $key) { 00230 if ($data[$key] != $quiz->{$key}) { 00231 $changed = true; 00232 break; 00233 } 00234 } 00235 if (!$changed) { 00236 $errors['timeopen'] = get_string('nooverridedata', 'quiz'); 00237 } 00238 00239 return $errors; 00240 } 00241 }