/** * Define the form. */ public function definition() { global $CFG, $COURSE; $mform = $this->_form; $choices = array(); $choices['0'] = get_string('emaildigestoff'); $choices['1'] = get_string('emaildigestcomplete'); $choices['2'] = get_string('emaildigestsubjects'); $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices); $mform->setDefault('maildigest', core_user::get_property_default('maildigest')); $mform->addHelpButton('maildigest', 'emaildigest'); $choices = array(); $choices['1'] = get_string('autosubscribeyes'); $choices['0'] = get_string('autosubscribeno'); $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe')); if (!empty($CFG->forum_trackreadposts)) { $choices = array(); $choices['0'] = get_string('trackforumsno'); $choices['1'] = get_string('trackforumsyes'); $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices); $mform->setDefault('trackforums', core_user::get_property_default('trackforums')); } // Add some extra hidden fields. $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'course', $COURSE->id); $mform->setType('course', PARAM_INT); $this->add_action_buttons(true, get_string('savechanges')); }
/** * Extend the form definition after the data has been parsed. */ public function definition_after_data() { global $CFG, $DB, $OUTPUT; $mform = $this->_form; // If language does not exist, use site default lang. if ($langsel = $mform->getElementValue('lang')) { $lang = reset($langsel); // Check lang exists. if (!get_string_manager()->translation_exists($lang, false)) { $langel =& $mform->getElement('lang'); $langel->setValue(core_user::get_property_default('lang')); } } }
function definition() { global $CFG, $USER; $mform = $this->_form; $columns = $this->_customdata['columns']; $data = $this->_customdata['data']; // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-) $templateuser = $USER; // upload settings and file $mform->addElement('header', 'settingsheader', get_string('settings')); $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'), UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'), UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'), UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser')); $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices); $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth')); $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices); $mform->setDefault('uupasswordnew', 1); $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE); $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'), UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'), UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'), UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser')); $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices); $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES); $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW); $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC); $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update')); $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices); $mform->setDefault('uupasswordold', 0); $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW); $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC); $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0); $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3); $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'), UU_PWRESET_NONE => get_string('none'), UU_PWRESET_ALL => get_string('all')); if (empty($CFG->passwordpolicy)) { unset($choices[UU_PWRESET_WEAK]); } $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices); $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser')); $mform->setDefault('uuallowrenames', 0); $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW); $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC); $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser')); $mform->setDefault('uuallowdeletes', 0); $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW); $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC); $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser')); $mform->setDefault('uuallowsuspends', 1); $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW); $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC); if (!empty($CFG->allowaccountssameemail)) { $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser')); $mform->setDefault('uunoemailduplicates', 1); } else { $mform->addElement('hidden', 'uunoemailduplicates', 1); } $mform->setType('uunoemailduplicates', PARAM_BOOL); $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser')); $mform->setDefault('uustandardusernames', 1); $choices = array(UU_BULK_NONE => get_string('no'), UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'), UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'), UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser')); $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices); $mform->setDefault('uubulk', 0); // roles selection $showroles = false; foreach ($columns as $column) { if (preg_match('/^type\\d+$/', $column)) { $showroles = true; break; } } if ($showroles) { $mform->addElement('header', 'rolesheader', get_string('roles')); $choices = uu_allowed_roles(true); $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices); if ($studentroles = get_archetype_roles('student')) { foreach ($studentroles as $role) { if (isset($choices[$role->id])) { $mform->setDefault('uulegacy1', $role->id); break; } } unset($studentroles); } $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices); if ($editteacherroles = get_archetype_roles('editingteacher')) { foreach ($editteacherroles as $role) { if (isset($choices[$role->id])) { $mform->setDefault('uulegacy2', $role->id); break; } } unset($editteacherroles); } $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices); if ($teacherroles = get_archetype_roles('teacher')) { foreach ($teacherroles as $role) { if (isset($choices[$role->id])) { $mform->setDefault('uulegacy3', $role->id); break; } } unset($teacherroles); } } // default values $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser')); $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"'); $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later. $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client'); $mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE); $mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE); $mform->setForceLtr('username'); $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"'); $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later. $mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE); $mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE); $mform->setForceLtr('email'); // only enabled and known to work plugins $choices = uu_supported_auths(); $mform->addElement('select', 'auth', get_string('chooseauthmethod', 'auth'), $choices); $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default $mform->addHelpButton('auth', 'chooseauthmethod', 'auth'); $mform->setAdvanced('auth'); $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse')); $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay')); $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat')); $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices); $mform->setDefault('mailformat', core_user::get_property_default('mailformat')); $mform->setAdvanced('mailformat'); $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects')); $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices); $mform->setDefault('maildigest', core_user::get_property_default('maildigest')); $mform->setAdvanced('maildigest'); $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno')); $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe')); $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"'); $mform->setType('city', PARAM_TEXT); if (empty($CFG->defaultcity)) { $mform->setDefault('city', $templateuser->city); } else { $mform->setDefault('city', core_user::get_property_default('city')); } $choices = get_string_manager()->get_list_of_countries(); $choices = array('' => get_string('selectacountry') . '...') + $choices; $mform->addElement('select', 'country', get_string('selectacountry'), $choices); if (empty($CFG->country)) { $mform->setDefault('country', $templateuser->country); } else { $mform->setDefault('country', core_user::get_property_default('country')); } $mform->setAdvanced('country'); $choices = core_date::get_list_of_timezones($templateuser->timezone, true); $mform->addElement('select', 'timezone', get_string('timezone'), $choices); $mform->setDefault('timezone', $templateuser->timezone); $mform->setAdvanced('timezone'); $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations()); $mform->setDefault('lang', $templateuser->lang); $mform->setAdvanced('lang'); $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false); $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions); $mform->setType('description', PARAM_CLEANHTML); $mform->addHelpButton('description', 'userdescription'); $mform->setAdvanced('description'); $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"'); $mform->setType('url', PARAM_URL); $mform->setAdvanced('url'); $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"'); $mform->setType('idnumber', PARAM_NOTAGS); $mform->setForceLtr('idnumber'); $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"'); $mform->setType('institution', PARAM_TEXT); $mform->setDefault('institution', $templateuser->institution); $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"'); $mform->setType('department', PARAM_TEXT); $mform->setDefault('department', $templateuser->department); $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"'); $mform->setType('phone1', PARAM_NOTAGS); $mform->setAdvanced('phone1'); $mform->setForceLtr('phone1'); $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); $mform->setType('phone2', PARAM_NOTAGS); $mform->setAdvanced('phone2'); $mform->setForceLtr('phone2'); $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"'); $mform->setType('address', PARAM_TEXT); $mform->setAdvanced('address'); // Next the profile defaults profile_definition($mform); // hidden fields $mform->addElement('hidden', 'iid'); $mform->setType('iid', PARAM_INT); $mform->addElement('hidden', 'previewrows'); $mform->setType('previewrows', PARAM_INT); $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser')); $this->set_data($data); }
/** * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc. * * @param moodleform $mform * @param array $editoroptions * @param array $filemanageroptions * @param stdClass $user */ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions, $user) { global $CFG, $USER, $DB; if ($user->id > 0) { useredit_load_preferences($user, false); } $strrequired = get_string('required'); $stringman = get_string_manager(); // Add the necessary names. foreach (useredit_get_required_name_fields() as $fullname) { $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"'); if ($stringman->string_exists('missing' . $fullname, 'core')) { $strmissingfield = get_string('missing' . $fullname, 'core'); } else { $strmissingfield = $strrequired; } $mform->addRule($fullname, $strmissingfield, 'required', null, 'client'); $mform->setType($fullname, PARAM_NOTAGS); } $enabledusernamefields = useredit_get_enabled_name_fields(); // Add the enabled additional name fields. foreach ($enabledusernamefields as $addname) { $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"'); $mform->setType($addname, PARAM_NOTAGS); } // Do not show email field if change confirmation is pending. if ($user->id > 0 and !empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) { $notice = get_string('emailchangepending', 'auth', $user); $notice .= '<br /><a href="edit.php?cancelemailchange=1&id=' . $user->id . '">' . get_string('emailchangecancel', 'auth') . '</a>'; $mform->addElement('static', 'emailpending', get_string('email'), $notice); } else { $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"'); $mform->addRule('email', $strrequired, 'required', null, 'client'); $mform->setType('email', PARAM_RAW_TRIMMED); } $choices = array(); $choices['0'] = get_string('emaildisplayno'); $choices['1'] = get_string('emaildisplayyes'); $choices['2'] = get_string('emaildisplaycourse'); $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay')); $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"'); $mform->setType('city', PARAM_TEXT); if (!empty($CFG->defaultcity)) { $mform->setDefault('city', $CFG->defaultcity); } $choices = get_string_manager()->get_list_of_countries(); $choices = array('' => get_string('selectacountry') . '...') + $choices; $mform->addElement('select', 'country', get_string('selectacountry'), $choices); if (!empty($CFG->country)) { $mform->setDefault('country', core_user::get_property_default('country')); } if (isset($CFG->forcetimezone) and $CFG->forcetimezone != 99) { $choices = core_date::get_list_of_timezones($CFG->forcetimezone); $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]); $mform->addElement('hidden', 'timezone'); $mform->setType('timezone', core_user::get_property_type('timezone')); } else { $choices = core_date::get_list_of_timezones($user->timezone, true); $mform->addElement('select', 'timezone', get_string('timezone'), $choices); } if (!empty($CFG->allowuserthemes)) { $choices = array(); $choices[''] = get_string('default'); $themes = get_list_of_themes(); foreach ($themes as $key => $theme) { if (empty($theme->hidefromselector)) { $choices[$key] = get_string('pluginname', 'theme_' . $theme->name); } } $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices); } $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions); $mform->setType('description_editor', PARAM_CLEANHTML); $mform->addHelpButton('description_editor', 'userdescription'); if (empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_picture', get_string('pictureofuser')); $mform->setExpanded('moodle_picture', true); if (!empty($CFG->enablegravatar)) { $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled'))); } $mform->addElement('static', 'currentpicture', get_string('currentpicture')); $mform->addElement('checkbox', 'deletepicture', get_string('delete')); $mform->setDefault('deletepicture', 0); $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions); $mform->addHelpButton('imagefile', 'newpicture'); $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"'); $mform->setType('imagealt', PARAM_TEXT); } // Display user name fields that are not currenlty enabled here if there are any. $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields); if (count($disabledusernamefields) > 0) { $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames')); foreach ($disabledusernamefields as $allname) { $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"'); $mform->setType($allname, PARAM_NOTAGS); } } if (core_tag_tag::is_enabled('core', 'user') and empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_interests', get_string('interests')); $mform->addElement('tags', 'interests', get_string('interestslist'), array('itemtype' => 'user', 'component' => 'core')); $mform->addHelpButton('interests', 'interestslist'); } // Moodle optional fields. $mform->addElement('header', 'moodle_optional', get_string('optional', 'form')); $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"'); $mform->setType('url', core_user::get_property_type('url')); $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"'); $mform->setType('icq', core_user::get_property_type('icq')); $mform->setForceLtr('icq'); $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"'); $mform->setType('skype', core_user::get_property_type('skype')); $mform->setForceLtr('skype'); $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"'); $mform->setType('aim', core_user::get_property_type('aim')); $mform->setForceLtr('aim'); $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"'); $mform->setType('yahoo', core_user::get_property_type('yahoo')); $mform->setForceLtr('yahoo'); $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"'); $mform->setType('msn', core_user::get_property_type('msn')); $mform->setForceLtr('msn'); $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"'); $mform->setType('idnumber', core_user::get_property_type('idnumber')); $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"'); $mform->setType('institution', core_user::get_property_type('institution')); $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"'); $mform->setType('department', core_user::get_property_type('department')); $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"'); $mform->setType('phone1', core_user::get_property_type('phone1')); $mform->setForceLtr('phone1'); $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); $mform->setType('phone2', core_user::get_property_type('phone2')); $mform->setForceLtr('phone2'); $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"'); $mform->setType('address', core_user::get_property_type('address')); }
/** * Test get_property_default(). * * * @expectedException coding_exception * @expectedExceptionMessage Invalid property requested, or the property does not has a default value. */ public function test_get_property_default() { global $CFG; $this->resetAfterTest(); $country = core_user::get_property_default('country'); $this->assertEquals($CFG->country, $country); set_config('country', 'AU'); core_user::reset_caches(); $country = core_user::get_property_default('country'); $this->assertEquals($CFG->country, $country); $lang = core_user::get_property_default('lang'); $this->assertEquals($CFG->lang, $lang); set_config('lang', 'en'); $lang = core_user::get_property_default('lang'); $this->assertEquals($CFG->lang, $lang); $this->setTimezone('Europe/London', 'Pacific/Auckland'); core_user::reset_caches(); $timezone = core_user::get_property_default('timezone'); $this->assertEquals('Europe/London', $timezone); $this->setTimezone('99', 'Pacific/Auckland'); core_user::reset_caches(); $timezone = core_user::get_property_default('timezone'); $this->assertEquals('Pacific/Auckland', $timezone); core_user::get_property_default('firstname'); }
/** * Returns description of method parameters * * @return external_function_parameters * @since Moodle 2.2 */ public static function create_users_parameters() { global $CFG; return new external_function_parameters(array('users' => new external_multiple_structure(new external_single_structure(array('username' => new external_value(core_user::get_property_type('username'), 'Username policy is defined in Moodle security config.'), 'password' => new external_value(core_user::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL), 'createpassword' => new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.', VALUE_OPTIONAL), 'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'), 'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user'), 'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address'), 'auth' => new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 'manual', core_user::get_property_null('auth')), 'idnumber' => new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, ''), 'lang' => new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_DEFAULT, core_user::get_property_default('lang'), core_user::get_property_null('lang')), 'calendartype' => new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL), 'theme' => new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL), 'timezone' => new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL), 'mailformat' => new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL), 'description' => new external_value(core_user::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL), 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL), 'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 'firstnamephonetic' => new external_value(core_user::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL), 'lastnamephonetic' => new external_value(core_user::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL), 'middlename' => new external_value(core_user::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL), 'alternatename' => new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL), 'preferences' => new external_multiple_structure(new external_single_structure(array('type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'), 'value' => new external_value(PARAM_RAW, 'The value of the preference'))), 'User preferences', VALUE_OPTIONAL), 'customfields' => new external_multiple_structure(new external_single_structure(array('type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), 'value' => new external_value(PARAM_RAW, 'The value of the custom field'))), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)))))); }
/** * Test get_property_default(). */ public function test_get_property_default() { global $CFG; $this->resetAfterTest(); $country = core_user::get_property_default('country'); $this->assertEquals($CFG->country, $country); set_config('country', 'AU'); core_user::reset_caches(); $country = core_user::get_property_default('country'); $this->assertEquals($CFG->country, $country); $lang = core_user::get_property_default('lang'); $this->assertEquals($CFG->lang, $lang); set_config('lang', 'en'); $lang = core_user::get_property_default('lang'); $this->assertEquals($CFG->lang, $lang); $this->setTimezone('Europe/London', 'Pacific/Auckland'); core_user::reset_caches(); $timezone = core_user::get_property_default('timezone'); $this->assertEquals('Europe/London', $timezone); $this->setTimezone('99', 'Pacific/Auckland'); core_user::reset_caches(); $timezone = core_user::get_property_default('timezone'); $this->assertEquals('Pacific/Auckland', $timezone); $this->setExpectedException('coding_exception', 'Invalid property requested, or the property does not has a default value.'); core_user::get_property_default('firstname'); }
/** * Creates a user * * @throws moodle_exception * @param stdClass $user user to create * @param bool $updatepassword if true, authentication plugin will update password. * @param bool $triggerevent set false if user_created event should not be triggred. * This will not affect user_password_updated event triggering. * @return int id of the newly created user */ function user_create_user($user, $updatepassword = true, $triggerevent = true) { global $DB; // Set the timecreate field to the current time. if (!is_object($user)) { $user = (object) $user; } // Check username. if ($user->username !== core_text::strtolower($user->username)) { throw new moodle_exception('usernamelowercase'); } else { if ($user->username !== core_user::clean_field($user->username, 'username')) { throw new moodle_exception('invalidusername'); } } // Save the password in a temp value for later. if ($updatepassword && isset($user->password)) { // Check password toward the password policy. if (!check_password_policy($user->password, $errmsg)) { throw new moodle_exception($errmsg); } $userpassword = $user->password; unset($user->password); } // Apply default values for user preferences that are stored in users table. if (!isset($user->calendartype)) { $user->calendartype = core_user::get_property_default('calendartype'); } if (!isset($user->maildisplay)) { $user->maildisplay = core_user::get_property_default('maildisplay'); } if (!isset($user->mailformat)) { $user->mailformat = core_user::get_property_default('mailformat'); } if (!isset($user->maildigest)) { $user->maildigest = core_user::get_property_default('maildigest'); } if (!isset($user->autosubscribe)) { $user->autosubscribe = core_user::get_property_default('autosubscribe'); } if (!isset($user->trackforums)) { $user->trackforums = core_user::get_property_default('trackforums'); } if (!isset($user->lang)) { $user->lang = core_user::get_property_default('lang'); } $user->timecreated = time(); $user->timemodified = $user->timecreated; // Validate user data object. $uservalidation = core_user::validate($user); if ($uservalidation !== true) { foreach ($uservalidation as $field => $message) { debugging("The property '{$field}' has invalid data and has been cleaned.", DEBUG_DEVELOPER); $user->{$field} = core_user::clean_field($user->{$field}, $field); } } // Insert the user into the database. $newuserid = $DB->insert_record('user', $user); // Create USER context for this user. $usercontext = context_user::instance($newuserid); // Update user password if necessary. if (isset($userpassword)) { // Get full database user row, in case auth is default. $newuser = $DB->get_record('user', array('id' => $newuserid)); $authplugin = get_auth_plugin($newuser->auth); $authplugin->user_update_password($newuser, $userpassword); } // Trigger event If required. if ($triggerevent) { \core\event\user_created::create_from_userid($newuserid)->trigger(); } return $newuserid; }
$courseid = optional_param('course', SITEID, PARAM_INT); // Course id (defaults to Site). $PAGE->set_url('/user/language.php', array('id' => $userid, 'course' => $courseid)); list($user, $course) = useredit_setup_preference_page($userid, $courseid); // Create form. $languageform = new user_edit_language_form(null, array('userid' => $user->id)); $languageform->set_data($user); $redirect = new moodle_url("/user/preferences.php", array('userid' => $user->id)); if ($languageform->is_cancelled()) { redirect($redirect); } else { if ($data = $languageform->get_data()) { $lang = $data->lang; // If the specified language does not exist, use the site default. if (!get_string_manager()->translation_exists($lang, false)) { $lang = core_user::get_property_default('lang'); } $user->lang = $lang; // Update user with new language. user_update_user($user, false, false); // Trigger event. \core\event\user_updated::create_from_userid($user->id)->trigger(); if ($USER->id == $user->id) { $USER->lang = $lang; } redirect($redirect); } } // Display page header. $streditmylanguage = get_string('preferredlanguage'); $userfullname = fullname($user, true);