2007-01-25 00:04:02 +00:00
|
|
|
<?php //$Id$
|
|
|
|
|
|
|
|
require_once($CFG->dirroot.'/lib/formslib.php');
|
|
|
|
|
|
|
|
class user_editadvanced_form extends moodleform {
|
|
|
|
|
|
|
|
// Define the form
|
2007-01-26 13:11:06 +00:00
|
|
|
function definition() {
|
2007-01-25 18:02:48 +00:00
|
|
|
global $USER, $CFG, $COURSE;
|
2007-01-25 00:04:02 +00:00
|
|
|
|
|
|
|
$mform =& $this->_form;
|
|
|
|
$this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
|
|
|
|
$strrequired = get_string('required');
|
|
|
|
|
|
|
|
/// Add some extra hidden fields
|
|
|
|
$mform->addElement('hidden', 'id');
|
2007-01-25 18:02:48 +00:00
|
|
|
$mform->addElement('hidden', 'course', $COURSE->id);
|
2007-01-25 00:04:02 +00:00
|
|
|
|
|
|
|
/// Print the required moodle fields first
|
|
|
|
$mform->addElement('header', 'moodle', $strrequired);
|
|
|
|
|
|
|
|
$mform->addElement('text', 'username', get_string('username'), 'size="20"');
|
|
|
|
$mform->addRule('username', $strrequired, 'required', null, 'client');
|
|
|
|
$mform->setType('username', PARAM_RAW);
|
|
|
|
|
|
|
|
$modules = get_list_of_plugins('auth');
|
|
|
|
$auth_options = array();
|
|
|
|
foreach ($modules as $module) {
|
|
|
|
$auth_options[$module] = get_string("auth_$module"."title", "auth");
|
|
|
|
}
|
|
|
|
$mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
|
|
|
|
$mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
|
|
|
|
$mform->setAdvanced('auth');
|
|
|
|
|
2007-04-06 16:23:48 +00:00
|
|
|
$mform->addElement('passwordreveal', 'newpassword', get_string('newpassword'), 'size="20"');
|
2007-01-25 00:04:02 +00:00
|
|
|
$mform->setType('newpassword', PARAM_RAW);
|
|
|
|
//TODO: add missing help - empty means no change
|
|
|
|
|
|
|
|
$mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
|
|
|
|
//TODO: add missing help - user will be forced to change password
|
|
|
|
|
2007-01-26 18:42:47 +00:00
|
|
|
/// shared fields
|
|
|
|
useredit_shared_definition($mform);
|
2007-01-25 00:04:02 +00:00
|
|
|
|
2007-01-26 13:11:06 +00:00
|
|
|
/// Next the customisable profile fields
|
|
|
|
profile_definition($mform);
|
|
|
|
|
2007-01-25 00:04:02 +00:00
|
|
|
$this->add_action_buttons(false, get_string('updatemyprofile'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function definition_after_data() {
|
|
|
|
global $USER, $CFG;
|
|
|
|
|
|
|
|
$mform =& $this->_form;
|
2007-01-25 11:03:33 +00:00
|
|
|
$userid = $mform->getElementValue('id');
|
|
|
|
$user = get_record('user', 'id', $userid);
|
2007-01-25 00:04:02 +00:00
|
|
|
|
2007-04-16 18:30:52 +00:00
|
|
|
// if language does not exist, use site default lang
|
|
|
|
if ($langsel = $mform->getElementValue('lang')) {
|
|
|
|
$lang = reset($langsel);
|
|
|
|
if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
|
|
|
|
!file_exists($CFG->dirroot .'/lang/'.$lang)) {
|
|
|
|
$lang_el =& $mform->getElement('lang');
|
|
|
|
$lang_el->setValue($CFG->lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-25 11:03:33 +00:00
|
|
|
// user can not change own auth method
|
|
|
|
if ($userid == $USER->id) {
|
|
|
|
$mform->hardFreeze('auth');
|
|
|
|
$mform->hardFreeze('preference_auth_forcepasswordchange');
|
2007-01-25 00:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// admin must choose some password and supply correct email
|
|
|
|
if (!empty($USER->newadminuser)) {
|
|
|
|
$mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
|
|
|
|
|
2007-03-15 12:42:39 +00:00
|
|
|
$email_el =& $mform->getElement('email');
|
2007-01-25 11:03:33 +00:00
|
|
|
if ($email_el->getValue() == 'root@localhost') {
|
|
|
|
$email_el->setValue('');
|
2007-01-25 00:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-25 11:03:33 +00:00
|
|
|
// require password for new users
|
|
|
|
if ($userid == -1) {
|
|
|
|
$mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
|
|
|
|
}
|
|
|
|
|
|
|
|
// print picture
|
2007-01-25 00:04:02 +00:00
|
|
|
if (!empty($CFG->gdversion)) {
|
2007-03-15 12:42:39 +00:00
|
|
|
$image_el =& $mform->getElement('currentpicture');
|
2007-01-25 11:03:33 +00:00
|
|
|
if ($user and $user->picture) {
|
|
|
|
$image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
|
|
|
|
} else {
|
|
|
|
$image_el->setValue(get_string('none'));
|
|
|
|
}
|
|
|
|
}
|
2007-01-26 13:11:06 +00:00
|
|
|
|
|
|
|
/// Next the customisable profile fields
|
|
|
|
profile_definition_after_data($mform);
|
2007-01-25 11:03:33 +00:00
|
|
|
}
|
|
|
|
|
2007-01-26 13:11:06 +00:00
|
|
|
function validation($usernew) {
|
2007-01-25 11:03:33 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$usernew = (object)$usernew;
|
2007-01-26 16:19:33 +00:00
|
|
|
$usernew->username = trim($usernew->username);
|
2007-01-25 11:03:33 +00:00
|
|
|
|
2007-01-26 16:19:33 +00:00
|
|
|
$user = get_record('user', 'id', $usernew->id);
|
|
|
|
$err = array();
|
|
|
|
|
2007-04-25 19:36:47 +00:00
|
|
|
if (!empty($usernew->newpassword)) {
|
|
|
|
$errmsg = '';//prevent eclipse warning
|
|
|
|
if (!check_password_policy($usernew->newpassword, $errmsg)) {
|
|
|
|
$err['newpassword'] = $errmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-26 16:19:33 +00:00
|
|
|
if (empty($usernew->username)) {
|
|
|
|
//might be only whitespace
|
|
|
|
$err['username'] = get_string('required');
|
|
|
|
} else if (!$user or $user->username !== $usernew->username) {
|
2007-01-25 11:03:33 +00:00
|
|
|
//check new username does not exist
|
|
|
|
if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
|
|
|
|
$err['username'] = get_string('usernameexists');
|
|
|
|
}
|
|
|
|
//check allowed characters
|
|
|
|
if ($usernew->username !== moodle_strtolower($usernew->username)) {
|
|
|
|
$err['username'] = get_string('usernamelowercase');
|
2007-01-25 00:04:02 +00:00
|
|
|
} else {
|
2007-01-25 11:03:33 +00:00
|
|
|
if (empty($CFG->extendedusernamechars)) {
|
|
|
|
$string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
|
|
|
|
if ($usernew->username !== $string) {
|
|
|
|
$err['username'] = get_string('alphanumerical');
|
|
|
|
}
|
|
|
|
}
|
2007-01-25 00:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-25 11:03:33 +00:00
|
|
|
|
|
|
|
if (!$user or $user->email !== $usernew->email) {
|
|
|
|
if (!validate_email($usernew->email)) {
|
|
|
|
$err['email'] = get_string('invalidemail');
|
|
|
|
} else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
|
|
|
|
$err['email'] = get_string('emailexists');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-26 13:11:06 +00:00
|
|
|
/// Next the customisable profile fields
|
|
|
|
$err += profile_validation($usernew);
|
|
|
|
|
2007-01-25 11:03:33 +00:00
|
|
|
if (count($err) == 0){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $err;
|
|
|
|
}
|
2007-01-25 00:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_um() {
|
|
|
|
return $this->_upload_manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|