[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 * Form for editing a users profile 19 * 20 * @copyright 1999 Martin Dougiamas http://dougiamas.com 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 * @package core_user 23 */ 24 25 if (!defined('MOODLE_INTERNAL')) { 26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. 27 } 28 29 require_once($CFG->dirroot.'/lib/formslib.php'); 30 31 /** 32 * Class user_editadvanced_form. 33 * 34 * @copyright 1999 Martin Dougiamas http://dougiamas.com 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class user_editadvanced_form extends moodleform { 38 39 /** 40 * Define the form. 41 */ 42 public function definition() { 43 global $USER, $CFG, $COURSE; 44 45 $mform = $this->_form; 46 $editoroptions = null; 47 $filemanageroptions = null; 48 $userid = $USER->id; 49 50 if (is_array($this->_customdata)) { 51 if (array_key_exists('editoroptions', $this->_customdata)) { 52 $editoroptions = $this->_customdata['editoroptions']; 53 } 54 if (array_key_exists('filemanageroptions', $this->_customdata)) { 55 $filemanageroptions = $this->_customdata['filemanageroptions']; 56 } 57 if (array_key_exists('userid', $this->_customdata)) { 58 $userid = $this->_customdata['userid']; 59 } 60 } 61 62 // Accessibility: "Required" is bad legend text. 63 $strgeneral = get_string('general'); 64 $strrequired = get_string('required'); 65 66 // Add some extra hidden fields. 67 $mform->addElement('hidden', 'id'); 68 $mform->setType('id', PARAM_INT); 69 $mform->addElement('hidden', 'course', $COURSE->id); 70 $mform->setType('course', PARAM_INT); 71 72 // Print the required moodle fields first. 73 $mform->addElement('header', 'moodle', $strgeneral); 74 75 $mform->addElement('text', 'username', get_string('username'), 'size="20"'); 76 $mform->addRule('username', $strrequired, 'required', null, 'client'); 77 $mform->setType('username', PARAM_RAW); 78 79 $auths = core_component::get_plugin_list('auth'); 80 $enabled = get_string('pluginenabled', 'core_plugin'); 81 $disabled = get_string('plugindisabled', 'core_plugin'); 82 $authoptions = array($enabled => array(), $disabled => array()); 83 $cannotchangepass = array(); 84 foreach ($auths as $auth => $unused) { 85 $authinst = get_auth_plugin($auth); 86 $passwordurl = $authinst->change_password_url(); 87 if (!($authinst->can_change_password() && empty($passwordurl))) { 88 if (!$userid and $authinst->is_internal()) { 89 // This is unlikely but we can not create account without password 90 // when plugin uses passwords, we need to set it initially at least. 91 } else { 92 $cannotchangepass[] = $auth; 93 } 94 } 95 if (is_enabled_auth($auth)) { 96 $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}"); 97 } else { 98 $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}"); 99 } 100 } 101 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions); 102 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth'); 103 104 $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth')); 105 $mform->addHelpButton('suspended', 'suspended', 'auth'); 106 107 $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth')); 108 $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass); 109 110 if (!empty($CFG->passwordpolicy)) { 111 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy()); 112 } 113 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"'); 114 $mform->addHelpButton('newpassword', 'newpassword'); 115 $mform->setType('newpassword', PARAM_RAW); 116 $mform->disabledIf('newpassword', 'createpassword', 'checked'); 117 118 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass); 119 120 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange')); 121 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange'); 122 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked'); 123 124 // Shared fields. 125 useredit_shared_definition($mform, $editoroptions, $filemanageroptions); 126 127 // Next the customisable profile fields. 128 profile_definition($mform, $userid); 129 130 if ($userid == -1) { 131 $btnstring = get_string('createuser'); 132 } else { 133 $btnstring = get_string('updatemyprofile'); 134 } 135 136 $this->add_action_buttons(false, $btnstring); 137 } 138 139 /** 140 * Extend the form definition after data has been parsed. 141 */ 142 public function definition_after_data() { 143 global $USER, $CFG, $DB, $OUTPUT; 144 145 $mform = $this->_form; 146 if ($userid = $mform->getElementValue('id')) { 147 $user = $DB->get_record('user', array('id' => $userid)); 148 } else { 149 $user = false; 150 } 151 152 // If language does not exist, use site default lang. 153 if ($langsel = $mform->getElementValue('lang')) { 154 $lang = reset($langsel); 155 // Check lang exists. 156 if (!get_string_manager()->translation_exists($lang, false)) { 157 $langel =& $mform->getElement('lang'); 158 $langel->setValue($CFG->lang); 159 } 160 } 161 162 // User can not change own auth method. 163 if ($userid == $USER->id) { 164 $mform->hardFreeze('auth'); 165 $mform->hardFreeze('preference_auth_forcepasswordchange'); 166 } 167 168 // Admin must choose some password and supply correct email. 169 if (!empty($USER->newadminuser)) { 170 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client'); 171 if ($mform->elementExists('suspended')) { 172 $mform->removeElement('suspended'); 173 } 174 } 175 176 // Require password for new users. 177 if ($userid > 0) { 178 if ($mform->elementExists('createpassword')) { 179 $mform->removeElement('createpassword'); 180 } 181 } 182 183 if ($user and is_mnet_remote_user($user)) { 184 // Only local accounts can be suspended. 185 if ($mform->elementExists('suspended')) { 186 $mform->removeElement('suspended'); 187 } 188 } 189 if ($user and ($user->id == $USER->id or is_siteadmin($user))) { 190 // Prevent self and admin mess ups. 191 if ($mform->elementExists('suspended')) { 192 $mform->hardFreeze('suspended'); 193 } 194 } 195 196 // Print picture. 197 if (empty($USER->newadminuser)) { 198 if ($user) { 199 $context = context_user::instance($user->id, MUST_EXIST); 200 $fs = get_file_storage(); 201 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg')); 202 if (!empty($user->picture) && $hasuploadedpicture) { 203 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64)); 204 } else { 205 $imagevalue = get_string('none'); 206 } 207 } else { 208 $imagevalue = get_string('none'); 209 } 210 $imageelement = $mform->getElement('currentpicture'); 211 $imageelement->setValue($imagevalue); 212 213 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) { 214 $mform->removeElement('deletepicture'); 215 } 216 } 217 218 // Next the customisable profile fields. 219 profile_definition_after_data($mform, $userid); 220 } 221 222 /** 223 * Validate the form data. 224 * @param array $usernew 225 * @param array $files 226 * @return array|bool 227 */ 228 public function validation($usernew, $files) { 229 global $CFG, $DB; 230 231 $usernew = (object)$usernew; 232 $usernew->username = trim($usernew->username); 233 234 $user = $DB->get_record('user', array('id' => $usernew->id)); 235 $err = array(); 236 237 if (!$user and !empty($usernew->createpassword)) { 238 if ($usernew->suspended) { 239 // Show some error because we can not mail suspended users. 240 $err['suspended'] = get_string('error'); 241 } 242 } else { 243 if (!empty($usernew->newpassword)) { 244 $errmsg = ''; // Prevent eclipse warning. 245 if (!check_password_policy($usernew->newpassword, $errmsg)) { 246 $err['newpassword'] = $errmsg; 247 } 248 } else if (!$user) { 249 $auth = get_auth_plugin($usernew->auth); 250 if ($auth->is_internal()) { 251 // Internal accounts require password! 252 $err['newpassword'] = get_string('required'); 253 } 254 } 255 } 256 257 if (empty($usernew->username)) { 258 // Might be only whitespace. 259 $err['username'] = get_string('required'); 260 } else if (!$user or $user->username !== $usernew->username) { 261 // Check new username does not exist. 262 if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) { 263 $err['username'] = get_string('usernameexists'); 264 } 265 // Check allowed characters. 266 if ($usernew->username !== core_text::strtolower($usernew->username)) { 267 $err['username'] = get_string('usernamelowercase'); 268 } else { 269 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) { 270 $err['username'] = get_string('invalidusername'); 271 } 272 } 273 } 274 275 if (!$user or $user->email !== $usernew->email) { 276 if (!validate_email($usernew->email)) { 277 $err['email'] = get_string('invalidemail'); 278 } else if ($DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) { 279 $err['email'] = get_string('emailexists'); 280 } 281 } 282 283 // Next the customisable profile fields. 284 $err += profile_validation($usernew, $files); 285 286 if (count($err) == 0) { 287 return true; 288 } else { 289 return $err; 290 } 291 } 292 } 293 294
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |