|
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_edit_form extends moodleform { 00010 00011 // Define the form 00012 function definition () { 00013 global $CFG, $COURSE; 00014 00015 $mform =& $this->_form; 00016 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) { 00017 $editoroptions = $this->_customdata['editoroptions']; 00018 } else { 00019 $editoroptions = null; 00020 } 00021 //Accessibility: "Required" is bad legend text. 00022 $strgeneral = get_string('general'); 00023 $strrequired = get_string('required'); 00024 00026 $mform->addElement('hidden', 'id'); 00027 $mform->setType('id', PARAM_INT); 00028 $mform->addElement('hidden', 'course', $COURSE->id); 00029 $mform->setType('course', PARAM_INT); 00030 00032 $mform->addElement('header', 'moodle', $strgeneral); 00033 00035 useredit_shared_definition($mform, $editoroptions); 00036 00038 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) { 00039 $mform->removeElement('deletepicture'); 00040 $mform->removeElement('imagefile'); 00041 $mform->removeElement('imagealt'); 00042 } 00043 00045 profile_definition($mform); 00046 00047 $this->add_action_buttons(false, get_string('updatemyprofile')); 00048 } 00049 00050 function definition_after_data() { 00051 global $CFG, $DB, $OUTPUT; 00052 00053 $mform =& $this->_form; 00054 $userid = $mform->getElementValue('id'); 00055 00056 // if language does not exist, use site default lang 00057 if ($langsel = $mform->getElementValue('lang')) { 00058 $lang = reset($langsel); 00059 // check lang exists 00060 if (!get_string_manager()->translation_exists($lang, false)) { 00061 $lang_el =& $mform->getElement('lang'); 00062 $lang_el->setValue($CFG->lang); 00063 } 00064 } 00065 00066 00067 if ($user = $DB->get_record('user', array('id'=>$userid))) { 00068 00069 // remove description 00070 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) { 00071 $mform->removeElement('description_editor'); 00072 } 00073 00074 // print picture 00075 if (!empty($CFG->gdversion)) { 00076 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST); 00077 $fs = get_file_storage(); 00078 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg')); 00079 if (!empty($user->picture) && $hasuploadedpicture) { 00080 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64)); 00081 } else { 00082 $imagevalue = get_string('none'); 00083 } 00084 $imageelement = $mform->getElement('currentpicture'); 00085 $imageelement->setValue($imagevalue); 00086 00087 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) { 00088 $mform->removeElement('deletepicture'); 00089 } 00090 } 00091 00093 $fields = get_user_fieldnames(); 00094 $authplugin = get_auth_plugin($user->auth); 00095 foreach ($fields as $field) { 00096 if (!$mform->elementExists($field)) { 00097 continue; 00098 } 00099 $configvariable = 'field_lock_' . $field; 00100 if (isset($authplugin->config->{$configvariable})) { 00101 if ($authplugin->config->{$configvariable} === 'locked') { 00102 $mform->hardFreeze($field); 00103 $mform->setConstant($field, $user->$field); 00104 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') { 00105 $mform->hardFreeze($field); 00106 $mform->setConstant($field, $user->$field); 00107 } 00108 } 00109 } 00110 00112 profile_definition_after_data($mform, $user->id); 00113 00114 } else { 00115 profile_definition_after_data($mform, 0); 00116 } 00117 } 00118 00119 function validation($usernew, $files) { 00120 global $CFG, $DB; 00121 00122 $errors = parent::validation($usernew, $files); 00123 00124 $usernew = (object)$usernew; 00125 $user = $DB->get_record('user', array('id'=>$usernew->id)); 00126 00127 // validate email 00128 if (!isset($usernew->email)) { 00129 // mail not confirmed yet 00130 } else if (!validate_email($usernew->email)) { 00131 $errors['email'] = get_string('invalidemail'); 00132 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) { 00133 $errors['email'] = get_string('emailexists'); 00134 } 00135 00136 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) { 00137 $errors['email'] = get_string('toomanybounces'); 00138 } 00139 00140 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) { 00141 $errorstr = email_is_not_allowed($usernew->email); 00142 if ($errorstr !== false) { 00143 $errors['email'] = $errorstr; 00144 } 00145 } 00146 00148 $errors += profile_validation($usernew, $files); 00149 00150 return $errors; 00151 } 00152 } 00153 00154