|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 if (!defined('MOODLE_INTERNAL')) { 00004 die('Direct access to this script is forbidden.'); 00005 } 00006 00007 require_once($CFG->dirroot.'/lib/formslib.php'); 00008 00009 class user_editadvanced_form extends moodleform { 00010 00011 // Define the form 00012 function definition() { 00013 global $USER, $CFG, $COURSE; 00014 00015 $mform =& $this->_form; 00016 00017 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) { 00018 $editoroptions = $this->_customdata['editoroptions']; 00019 } else { 00020 $editoroptions = null; 00021 } 00022 00023 //Accessibility: "Required" is bad legend text. 00024 $strgeneral = get_string('general'); 00025 $strrequired = get_string('required'); 00026 00028 $mform->addElement('hidden', 'id'); 00029 $mform->setType('id', PARAM_INT); 00030 $mform->addElement('hidden', 'course', $COURSE->id); 00031 $mform->setType('course', PARAM_INT); 00032 00034 $mform->addElement('header', 'moodle', $strgeneral); 00035 00036 $mform->addElement('text', 'username', get_string('username'), 'size="20"'); 00037 $mform->addRule('username', $strrequired, 'required', null, 'client'); 00038 $mform->setType('username', PARAM_RAW); 00039 00040 $auths = get_plugin_list('auth'); 00041 $auth_options = array(); 00042 foreach ($auths as $auth => $unused) { 00043 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}"); 00044 } 00045 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options); 00046 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth'); 00047 00048 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth')); 00049 $mform->addHelpButton('suspended', 'suspended', 'auth'); 00050 00051 if (!empty($CFG->passwordpolicy)){ 00052 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy()); 00053 } 00054 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"'); 00055 $mform->addHelpButton('newpassword', 'newpassword'); 00056 $mform->setType('newpassword', PARAM_RAW); 00057 00058 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange')); 00059 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange'); 00061 useredit_shared_definition($mform, $editoroptions); 00062 00064 profile_definition($mform); 00065 00066 $this->add_action_buttons(false, get_string('updatemyprofile')); 00067 } 00068 00069 function definition_after_data() { 00070 global $USER, $CFG, $DB, $OUTPUT; 00071 00072 $mform =& $this->_form; 00073 if ($userid = $mform->getElementValue('id')) { 00074 $user = $DB->get_record('user', array('id'=>$userid)); 00075 } else { 00076 $user = false; 00077 } 00078 00079 // if language does not exist, use site default lang 00080 if ($langsel = $mform->getElementValue('lang')) { 00081 $lang = reset($langsel); 00082 // check lang exists 00083 if (!get_string_manager()->translation_exists($lang, false)) { 00084 $lang_el =& $mform->getElement('lang'); 00085 $lang_el->setValue($CFG->lang); 00086 } 00087 } 00088 00089 // user can not change own auth method 00090 if ($userid == $USER->id) { 00091 $mform->hardFreeze('auth'); 00092 $mform->hardFreeze('preference_auth_forcepasswordchange'); 00093 } 00094 00095 // admin must choose some password and supply correct email 00096 if (!empty($USER->newadminuser)) { 00097 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client'); 00098 if ($mform->elementExists('suspended')) { 00099 $mform->removeElement('suspended'); 00100 } 00101 } 00102 00103 // require password for new users 00104 if ($userid == -1) { 00105 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client'); 00106 } 00107 00108 if ($user and is_mnet_remote_user($user)) { 00109 // only local accounts can be suspended 00110 if ($mform->elementExists('suspended')) { 00111 $mform->removeElement('suspended'); 00112 } 00113 } 00114 if ($user and ($user->id == $USER->id or is_siteadmin($user))) { 00115 // prevent self and admin mess ups 00116 if ($mform->elementExists('suspended')) { 00117 $mform->hardFreeze('suspended'); 00118 } 00119 } 00120 00121 // print picture 00122 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) { 00123 if ($user) { 00124 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST); 00125 $fs = get_file_storage(); 00126 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg')); 00127 if (!empty($user->picture) && $hasuploadedpicture) { 00128 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64)); 00129 } else { 00130 $imagevalue = get_string('none'); 00131 } 00132 } else { 00133 $imagevalue = get_string('none'); 00134 } 00135 $imageelement = $mform->getElement('currentpicture'); 00136 $imageelement->setValue($imagevalue); 00137 00138 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) { 00139 $mform->removeElement('deletepicture'); 00140 } 00141 } 00142 00144 profile_definition_after_data($mform, $userid); 00145 } 00146 00147 function validation($usernew, $files) { 00148 global $CFG, $DB; 00149 00150 $usernew = (object)$usernew; 00151 $usernew->username = trim($usernew->username); 00152 00153 $user = $DB->get_record('user', array('id'=>$usernew->id)); 00154 $err = array(); 00155 00156 if (!empty($usernew->newpassword)) { 00157 $errmsg = '';//prevent eclipse warning 00158 if (!check_password_policy($usernew->newpassword, $errmsg)) { 00159 $err['newpassword'] = $errmsg; 00160 } 00161 } 00162 00163 if (empty($usernew->username)) { 00164 //might be only whitespace 00165 $err['username'] = get_string('required'); 00166 } else if (!$user or $user->username !== $usernew->username) { 00167 //check new username does not exist 00168 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) { 00169 $err['username'] = get_string('usernameexists'); 00170 } 00171 //check allowed characters 00172 if ($usernew->username !== moodle_strtolower($usernew->username)) { 00173 $err['username'] = get_string('usernamelowercase'); 00174 } else { 00175 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) { 00176 $err['username'] = get_string('invalidusername'); 00177 } 00178 } 00179 } 00180 00181 if (!$user or $user->email !== $usernew->email) { 00182 if (!validate_email($usernew->email)) { 00183 $err['email'] = get_string('invalidemail'); 00184 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) { 00185 $err['email'] = get_string('emailexists'); 00186 } 00187 } 00188 00190 $err += profile_validation($usernew, $files); 00191 00192 if (count($err) == 0){ 00193 return true; 00194 } else { 00195 return $err; 00196 } 00197 } 00198 } 00199 00200