[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Settings form for overrides in the lesson module. 19 * 20 * @package mod_lesson 21 * @copyright 2015 Jean-Michel Vedrine 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->libdir . '/formslib.php'); 29 require_once($CFG->dirroot . '/mod/lesson/mod_form.php'); 30 31 32 /** 33 * Form for editing settings overrides. 34 * 35 * @copyright 2015 Jean-Michel Vedrine 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class lesson_override_form extends moodleform { 39 40 /** @var object course module object. */ 41 protected $cm; 42 43 /** @var object the lesson settings object. */ 44 protected $lesson; 45 46 /** @var context the lesson context. */ 47 protected $context; 48 49 /** @var bool editing group override (true) or user override (false). */ 50 protected $groupmode; 51 52 /** @var int groupid, if provided. */ 53 protected $groupid; 54 55 /** @var int userid, if provided. */ 56 protected $userid; 57 58 /** 59 * Constructor. 60 * @param moodle_url $submiturl the form action URL. 61 * @param object $cm course module object. 62 * @param object $lesson the lesson settings object. 63 * @param object $context the lesson context. 64 * @param bool $groupmode editing group override (true) or user override (false). 65 * @param object $override the override being edited, if it already exists. 66 */ 67 public function __construct($submiturl, $cm, $lesson, $context, $groupmode, $override) { 68 69 $this->cm = $cm; 70 $this->lesson = $lesson; 71 $this->context = $context; 72 $this->groupmode = $groupmode; 73 $this->groupid = empty($override->groupid) ? 0 : $override->groupid; 74 $this->userid = empty($override->userid) ? 0 : $override->userid; 75 76 parent::__construct($submiturl, null, 'post'); 77 78 } 79 80 /** 81 * Define this form - called by the parent constructor 82 */ 83 protected function definition() { 84 global $CFG, $DB; 85 86 $cm = $this->cm; 87 $mform = $this->_form; 88 89 $mform->addElement('header', 'override', get_string('override', 'lesson')); 90 91 if ($this->groupmode) { 92 // Group override. 93 if ($this->groupid) { 94 // There is already a groupid, so freeze the selector. 95 $groupchoices = array(); 96 $groupchoices[$this->groupid] = groups_get_group_name($this->groupid); 97 $mform->addElement('select', 'groupid', 98 get_string('overridegroup', 'lesson'), $groupchoices); 99 $mform->freeze('groupid'); 100 } else { 101 // Prepare the list of groups. 102 $groups = groups_get_all_groups($cm->course); 103 if (empty($groups)) { 104 // Generate an error. 105 $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id)); 106 print_error('groupsnone', 'lesson', $link); 107 } 108 109 $groupchoices = array(); 110 foreach ($groups as $group) { 111 $groupchoices[$group->id] = $group->name; 112 } 113 unset($groups); 114 115 if (count($groupchoices) == 0) { 116 $groupchoices[0] = get_string('none'); 117 } 118 119 $mform->addElement('select', 'groupid', 120 get_string('overridegroup', 'lesson'), $groupchoices); 121 $mform->addRule('groupid', get_string('required'), 'required', null, 'client'); 122 } 123 } else { 124 // User override. 125 if ($this->userid) { 126 // There is already a userid, so freeze the selector. 127 $user = $DB->get_record('user', array('id' => $this->userid)); 128 $userchoices = array(); 129 $userchoices[$this->userid] = fullname($user); 130 $mform->addElement('select', 'userid', 131 get_string('overrideuser', 'lesson'), $userchoices); 132 $mform->freeze('userid'); 133 } else { 134 // Prepare the list of users. 135 $users = get_enrolled_users($this->context, '', 0, 136 'u.id, u.email, ' . get_all_user_name_fields(true, 'u')); 137 138 // Filter users based on any fixed restrictions (groups, profile). 139 $info = new \core_availability\info_module($cm); 140 $users = $info->filter_user_list($users); 141 142 if (empty($users)) { 143 // Generate an error. 144 $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id)); 145 print_error('usersnone', 'lesson', $link); 146 } 147 148 $userchoices = array(); 149 $canviewemail = in_array('email', get_extra_user_fields($this->context)); 150 foreach ($users as $id => $user) { 151 if (empty($invalidusers[$id]) || (!empty($override) && 152 $id == $override->userid)) { 153 if ($canviewemail) { 154 $userchoices[$id] = fullname($user) . ', ' . $user->email; 155 } else { 156 $userchoices[$id] = fullname($user); 157 } 158 } 159 } 160 unset($users); 161 162 if (count($userchoices) == 0) { 163 $userchoices[0] = get_string('none'); 164 } 165 $mform->addElement('searchableselector', 'userid', 166 get_string('overrideuser', 'lesson'), $userchoices); 167 $mform->addRule('userid', get_string('required'), 'required', null, 'client'); 168 } 169 } 170 171 // Password. 172 // This field has to be above the date and timelimit fields, 173 // otherwise browsers will clear it when those fields are changed. 174 $mform->addElement('passwordunmask', 'password', get_string('usepassword', 'lesson')); 175 $mform->setType('password', PARAM_TEXT); 176 $mform->addHelpButton('password', 'usepassword', 'lesson'); 177 $mform->setDefault('password', $this->lesson->password);; 178 179 // Open and close dates. 180 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional' => true)); 181 $mform->setDefault('available', $this->lesson->available); 182 183 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional' => true)); 184 $mform->setDefault('deadline', $this->lesson->deadline); 185 186 // Lesson time limit. 187 $mform->addElement('duration', 'timelimit', 188 get_string('timelimit', 'lesson'), array('optional' => true)); 189 if ($this->lesson->timelimit != 0) { 190 $mform->setDefault('timelimit', 0); 191 } else { 192 $mform->setDefault('timelimit', $this->lesson->timelimit); 193 } 194 195 // Try a question again. 196 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson')); 197 $mform->addHelpButton('review', 'displayreview', 'lesson'); 198 $mform->setDefault('review', $this->lesson->review); 199 200 // Number of attempts. 201 $numbers = array(); 202 for ($i = 10; $i > 0; $i--) { 203 $numbers[$i] = $i; 204 } 205 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers); 206 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson'); 207 $mform->setDefault('maxattempts', $this->lesson->maxattempts); 208 209 // Retake allowed. 210 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson')); 211 $mform->addHelpButton('retake', 'retakesallowed', 'lesson'); 212 $mform->setDefault('retake', $this->lesson->retake); 213 214 // Submit buttons. 215 $mform->addElement('submit', 'resetbutton', 216 get_string('reverttodefaults', 'lesson')); 217 218 $buttonarray = array(); 219 $buttonarray[] = $mform->createElement('submit', 'submitbutton', 220 get_string('save', 'lesson')); 221 $buttonarray[] = $mform->createElement('submit', 'againbutton', 222 get_string('saveoverrideandstay', 'lesson')); 223 $buttonarray[] = $mform->createElement('cancel'); 224 225 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false); 226 $mform->closeHeaderBefore('buttonbar'); 227 228 } 229 230 /** 231 * Validate the submitted form data. 232 * 233 * @param array $data array of ("fieldname"=>value) of submitted data 234 * @param array $files array of uploaded files "element_name"=>tmp_file_path 235 * @return array of "element_name"=>"error_description" if there are errors 236 */ 237 public function validation($data, $files) { 238 global $COURSE, $DB; 239 $errors = parent::validation($data, $files); 240 241 $mform =& $this->_form; 242 $lesson = $this->lesson; 243 244 if ($mform->elementExists('userid')) { 245 if (empty($data['userid'])) { 246 $errors['userid'] = get_string('required'); 247 } 248 } 249 250 if ($mform->elementExists('groupid')) { 251 if (empty($data['groupid'])) { 252 $errors['groupid'] = get_string('required'); 253 } 254 } 255 256 // Ensure that the dates make sense. 257 if (!empty($data['available']) && !empty($data['deadline'])) { 258 if ($data['deadline'] < $data['available'] ) { 259 $errors['deadline'] = get_string('closebeforeopen', 'lesson'); 260 } 261 } 262 263 // Ensure that at least one lesson setting was changed. 264 $changed = false; 265 $keys = array('available', 'deadline', 'review', 'timelimit', 'maxattempts', 'retake', 'password'); 266 foreach ($keys as $key) { 267 if ($data[$key] != $lesson->{$key}) { 268 $changed = true; 269 break; 270 } 271 } 272 273 if (!$changed) { 274 $errors['available'] = get_string('nooverridedata', 'lesson'); 275 } 276 277 return $errors; 278 } 279 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |