/** * Add elements for editing the profile field value. * @param moodleform $mform */ public function edit_field_add($mform) { // Create the form field. $checkbox = $mform->addElement('advcheckbox', $this->inputname, format_string($this->field->name)); if ($this->data == '1') { $checkbox->setChecked(true); } $mform->setType($this->inputname, PARAM_BOOL); if ($this->is_required() and !has_capability('moodle/user:update', context_system::instance())) { $mform->addRule($this->inputname, get_string('required'), 'nonzero', null, 'client'); } }
/** * Prints out the form snippet for the part of creating or editing a profile field common to all data types. * * @param moodleform $form instance of the moodleform class */ public function define_form_common(&$form) { $strrequired = get_string('required'); $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"'); $form->addRule('shortname', $strrequired, 'required', null, 'client'); $form->setType('shortname', PARAM_ALPHANUM); $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"'); $form->addRule('name', $strrequired, 'required', null, 'client'); $form->setType('name', PARAM_TEXT); $form->addElement('editor', 'description', get_string('profiledescription', 'admin'), null, null); $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin')); $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin')); $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin')); $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin')); $choices = array(); $choices[PROFILE_VISIBLE_NONE] = get_string('profilevisiblenone', 'admin'); $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin'); $choices[PROFILE_VISIBLE_ALL] = get_string('profilevisibleall', 'admin'); $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices); $form->addHelpButton('visible', 'profilevisible', 'admin'); $form->setDefault('visible', PROFILE_VISIBLE_ALL); $choices = profile_list_categories(); $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices); }
/** * Sets the required flag for the field in the form object * * @param moodleform $mform instance of the moodleform class */ public function edit_field_set_required($mform) { global $USER; if ($this->is_required() && ($this->userid == $USER->id || isguestuser())) { $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client'); } }
/** * Called by HTML_QuickForm whenever form event is made on this element. * * @param string $event Name of event * @param mixed $arg event arguments * @param moodleform $caller calling object * @return mixed */ public function onQuickFormEvent($event, $arg, &$caller) { switch ($event) { case 'createElement': // The first argument is the name. $name = $arg[0]; // Set disable actions. $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale'); $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point'); // Set validation rules for the sub-elements belonging to this element. // A handy note: the parent scope of a closure is the function in which the closure was declared. // Because of this using $this is safe despite the closures being called statically. // A nasty magic hack! $checkmaxgrade = function ($val) { // Closure to validate a max points value. See the note above about scope if this confuses you. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'point') { if (!isset($val['modgrade_point'])) { return false; } return $this->validate_point($val['modgrade_point']); } return true; }; $checkvalidscale = function ($val) { // Closure to validate a scale value. See the note above about scope if this confuses you. if (isset($val['modgrade_type']) && $val['modgrade_type'] === 'scale') { if (!isset($val['modgrade_scale'])) { return false; } return $this->validate_scale($val['modgrade_scale']); } return true; }; $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax')); $invalidscale = get_string('modgradeerrorbadscale', 'grades'); // When creating the rules the sixth arg is $force, we set it to true because otherwise the form // will attempt to validate the existence of the element, we don't want this because the element // is being created right now and doesn't actually exist as a registered element yet. $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true); $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true); break; case 'updateValue': // As this is a group element with no value of its own we are only interested in situations where the // default value or a constant value are being provided to the actual element. // In this case we expect an int that is going to translate to a scale if negative, or to max points // if positive. // A constant value should be given as an int. // The default value should be an int and should really be $CFG->gradepointdefault. $value = $this->_findValue($caller->_constantValues); if (null === $value) { if ($caller->isSubmitted()) { break; } $value = $this->_findValue($caller->_defaultValues); } if (!is_null($value) && !is_scalar($value)) { // Something unexpected (likely an array of subelement values) has been given - this will be dealt // with somewhere else - where exactly... likely the subelements. debugging('An invalid value (type ' . gettype($value) . ') has arrived at ' . __METHOD__, DEBUG_DEVELOPER); break; } // Set element state for existing data. // This is really a pretty hacky thing to do, when data is being set the group element is called // with the data first and the subelements called afterwards. // This means that the subelements data (inc const and default values) can be overridden by form code. // So - when we call this code really we can't be sure that will be the end value for the element. if (!empty($this->_elements)) { if (!empty($value)) { if ($value < 0) { $this->_elements[1]->setValue('scale'); $this->_elements[4]->setValue($value * -1); } else { if ($value > 0) { $this->_elements[1]->setValue('point'); $this->_elements[7]->setValue($value); } } } else { $this->_elements[1]->setValue('none'); $this->_elements[7]->setValue(''); } } break; } // Always let the parent do its thing! return parent::onQuickFormEvent($event, $arg, $caller); }
/** * 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')); }
/** * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc. * * @param moodleform $mform * @param array|null $editoroptions * @param array|null $filemanageroptions */ function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) { global $CFG, $USER, $DB; $user = $DB->get_record('user', array('id' => $USER->id)); useredit_load_preferences($user, false); $strrequired = get_string('required'); // Add the necessary names. foreach (useredit_get_required_name_fields() as $fullname) { $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"'); $mform->addRule($fullname, $strrequired, '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 (!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); } $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', $CFG->country); } $choices = get_list_of_timezones(); $choices['99'] = get_string('serverlocaltime'); if ($CFG->forcetimezone != 99) { $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]); } else { $mform->addElement('select', 'timezone', get_string('timezone'), $choices); $mform->setDefault('timezone', '99'); $mform->addHelpButton('timezone', 'timezone'); } // Multi-Calendar Support - see MDL-18375. $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types(); // We do not want to show this option unless there is more than one calendar type to display. if (count($calendartypes) > 1) { $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes); $mform->setDefault('calendartype', $CFG->calendartype); } 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'); $mform->addElement('header', 'moodle_userpreferences', get_string('preferences')); useredit_shared_definition_preferences($user, $mform, $editoroptions, $filemanageroptions); if (empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_picture', get_string('pictureofuser')); 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 (!empty($CFG->usetags) and empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_interests', get_string('interests')); $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial')); $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', PARAM_URL); $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"'); $mform->setType('icq', PARAM_NOTAGS); $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"'); $mform->setType('skype', PARAM_NOTAGS); $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"'); $mform->setType('aim', PARAM_NOTAGS); $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"'); $mform->setType('yahoo', PARAM_NOTAGS); $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"'); $mform->setType('msn', PARAM_NOTAGS); $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"'); $mform->setType('idnumber', PARAM_NOTAGS); $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"'); $mform->setType('institution', PARAM_TEXT); $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"'); $mform->setType('department', PARAM_TEXT); $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"'); $mform->setType('phone1', PARAM_NOTAGS); $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); $mform->setType('phone2', PARAM_NOTAGS); $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"'); $mform->setType('address', PARAM_TEXT); }
/** * Add Instance settings input to Moodle form * * @param moodleform $mform */ public static function instance_config_form($mform) { $mform->addElement('text', 'equella_url', get_string('equellaurl', 'repository_equella')); $mform->setType('equella_url', PARAM_URL); $strrequired = get_string('required'); $mform->addRule('equella_url', $strrequired, 'required', null, 'client'); $mform->addElement('text', 'equella_options', get_string('equellaoptions', 'repository_equella')); $mform->setType('equella_options', PARAM_NOTAGS); $choices = array( 'none' => get_string('restrictionnone', 'repository_equella'), 'itemonly' => get_string('restrictionitemsonly', 'repository_equella'), 'attachmentonly' => get_string('restrictionattachmentsonly', 'repository_equella'), ); $mform->addElement('select', 'equella_select_restriction', get_string('selectrestriction', 'repository_equella'), $choices); $mform->addElement('header', '', get_string('group', 'repository_equella', get_string('groupdefault', 'repository_equella'))); $mform->addElement('text', 'equella_shareid', get_string('sharedid', 'repository_equella')); $mform->setType('equella_shareid', PARAM_RAW); $mform->addRule('equella_shareid', $strrequired, 'required', null, 'client'); $mform->addElement('text', 'equella_sharedsecret', get_string('sharedsecrets', 'repository_equella')); $mform->setType('equella_sharedsecret', PARAM_RAW); $mform->addRule('equella_sharedsecret', $strrequired, 'required', null, 'client'); foreach (self::get_all_editing_roles() as $role) { $mform->addElement('header', '', get_string('group', 'repository_equella', format_string($role->name))); $mform->addElement('text', "equella_{$role->shortname}_shareid", get_string('sharedid', 'repository_equella')); $mform->setType("equella_{$role->shortname}_shareid", PARAM_RAW); $mform->addElement('text', "equella_{$role->shortname}_sharedsecret", get_string('sharedsecrets', 'repository_equella')); $mform->setType("equella_{$role->shortname}_sharedsecret", PARAM_RAW); } }
/** * Called by HTML_QuickForm whenever form event is made on this element. * * @param string $event Name of event * @param mixed $arg event arguments * @param moodleform $caller calling object * @return mixed */ public function onQuickFormEvent($event, $arg, &$caller) { switch ($event) { case 'createElement': // The first argument is the name. $name = $arg[0]; // Set disable actions. $caller->disabledIf($name . '[modgrade_scale]', $name . '[modgrade_type]', 'neq', 'scale'); $caller->disabledIf($name . '[modgrade_point]', $name . '[modgrade_type]', 'neq', 'point'); $caller->disabledIf($name . '[modgrade_rescalegrades]', $name . '[modgrade_type]', 'neq', 'point'); // Set validation rules for the sub-elements belonging to this element. // A handy note: the parent scope of a closure is the function in which the closure was declared. // Because of this using $this is safe despite the closures being called statically. // A nasty magic hack! $checkgradetypechange = function ($val) { // Nothing is affected by changes to the grade type if there are no grades yet. if (!$this->hasgrades) { return true; } // Check if we are changing the grade type when grades are present. if (isset($val['modgrade_type']) && $val['modgrade_type'] !== $this->currentgradetype) { return false; } return true; }; $checkscalechange = function ($val) { // Nothing is affected by changes to the scale if there are no grades yet. if (!$this->hasgrades) { return true; } // Check if we are changing the scale type when grades are present. // If modgrade_type is empty then use currentgradetype. $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype; if ($gradetype === 'scale') { if (isset($val['modgrade_scale']) && $val['modgrade_scale'] !== $this->currentscaleid) { return false; } } return true; }; $checkmaxgradechange = function ($val) { // Nothing is affected by changes to the max grade if there are no grades yet. if (!$this->hasgrades) { return true; } // If we are not using ratings we can change the max grade. if (!$this->useratings) { return true; } // Check if we are changing the max grade if we are using ratings and there is a grade. // If modgrade_type is empty then use currentgradetype. $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype; if ($gradetype === 'point') { if (isset($val['modgrade_point']) && grade_floats_different($this->currentgrade, $val['modgrade_point'])) { return false; } } return true; }; $checkmaxgrade = function ($val) { // Closure to validate a max points value. See the note above about scope if this confuses you. // If modgrade_type is empty then use currentgradetype. $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype; if ($gradetype === 'point') { if (isset($val['modgrade_point'])) { return $this->validate_point($val['modgrade_point']); } } return true; }; $checkvalidscale = function ($val) { // Closure to validate a scale value. See the note above about scope if this confuses you. // If modgrade_type is empty then use currentgradetype. $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype; if ($gradetype === 'scale') { if (isset($val['modgrade_scale'])) { return $this->validate_scale($val['modgrade_scale']); } } return true; }; $checkrescale = function ($val) { // Nothing is affected by changes to grademax if there are no grades yet. if (!$this->isupdate || !$this->hasgrades || !$this->canrescale) { return true; } // Closure to validate a scale value. See the note above about scope if this confuses you. // If modgrade_type is empty then use currentgradetype. $gradetype = isset($val['modgrade_type']) ? $val['modgrade_type'] : $this->currentgradetype; if ($gradetype === 'point' && isset($val['modgrade_point'])) { // Work out if the value was actually changed in the form. if (grade_floats_different($this->currentgrade, $val['modgrade_point'])) { if (empty($val['modgrade_rescalegrades'])) { // This was an "edit", the grademax was changed and the process existing setting was not set. return false; } } } return true; }; $cantchangegradetype = get_string('modgradecantchangegradetype', 'grades'); $cantchangemaxgrade = get_string('modgradecantchangeratingmaxgrade', 'grades'); $maxgradeexceeded = get_string('modgradeerrorbadpoint', 'grades', get_config('core', 'gradepointmax')); $invalidscale = get_string('modgradeerrorbadscale', 'grades'); $cantchangescale = get_string('modgradecantchangescale', 'grades'); $mustchooserescale = get_string('mustchooserescaleyesorno', 'grades'); // When creating the rules the sixth arg is $force, we set it to true because otherwise the form // will attempt to validate the existence of the element, we don't want this because the element // is being created right now and doesn't actually exist as a registered element yet. $caller->addRule($name, $cantchangegradetype, 'callback', $checkgradetypechange, 'server', false, true); $caller->addRule($name, $cantchangemaxgrade, 'callback', $checkmaxgradechange, 'server', false, true); $caller->addRule($name, $maxgradeexceeded, 'callback', $checkmaxgrade, 'server', false, true); $caller->addRule($name, $invalidscale, 'callback', $checkvalidscale, 'server', false, true); $caller->addRule($name, $cantchangescale, 'callback', $checkscalechange, 'server', false, true); $caller->addRule($name, $mustchooserescale, 'callback', $checkrescale, 'server', false, true); break; case 'updateValue': // As this is a group element with no value of its own we are only interested in situations where the // default value or a constant value are being provided to the actual element. // In this case we expect an int that is going to translate to a scale if negative, or to max points // if positive. // Set the maximum points field to disabled if the rescale option has not been chosen and there are grades. $caller->disabledIf($this->getName() . '[modgrade_point]', $this->getName() . '[modgrade_rescalegrades]', 'eq', ''); // A constant value should be given as an int. // The default value should be an int and be either $CFG->gradepointdefault or whatever was set in set_data(). $value = $this->_findValue($caller->_constantValues); if (null === $value) { if ($caller->isSubmitted() && $this->_findValue($caller->_submitValues) !== null) { // Submitted values are array, one value for each individual element in this group. // When there is submitted data let parent::onQuickFormEvent() process it. break; } $value = $this->_findValue($caller->_defaultValues); } if (!is_null($value) && !is_scalar($value)) { // Something unexpected (likely an array of subelement values) has been given - this will be dealt // with somewhere else - where exactly... likely the subelements. debugging('An invalid value (type ' . gettype($value) . ') has arrived at ' . __METHOD__, DEBUG_DEVELOPER); break; } // Set element state for existing data. // This is really a pretty hacky thing to do, when data is being set the group element is called // with the data first and the subelements called afterwards. // This means that the subelements data (inc const and default values) can be overridden by form code. // So - when we call this code really we can't be sure that will be the end value for the element. if (!empty($this->_elements)) { if (!empty($value)) { if ($value < 0) { $this->gradetypeformelement->setValue('scale'); $this->scaleformelement->setValue($value * -1); } else { if ($value > 0) { $this->gradetypeformelement->setValue('point'); $this->maxgradeformelement->setValue($value); } } } else { $this->gradetypeformelement->setValue('none'); $this->maxgradeformelement->setValue(''); } } break; } // Always let the parent do its thing! return parent::onQuickFormEvent($event, $arg, $caller); }
/** * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc. * * @param moodleform $mform * @param array|null $editoroptions * @param array|null $filemanageroptions */ function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) { global $CFG, $USER, $DB; $user = $DB->get_record('user', array('id' => $USER->id)); useredit_load_preferences($user, false); $strrequired = get_string('required'); // Add the necessary names. foreach (useredit_get_required_name_fields() as $fullname) { $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"'); $mform->addRule($fullname, $strrequired, '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 (!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_EMAIL); } $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', 2); $choices = array(); $choices['0'] = get_string('textformat'); $choices['1'] = get_string('htmlformat'); // $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices); //$mform->setDefault('mailformat', 1); if (!empty($CFG->allowusermailcharset)) { $choices = array(); $charsets = get_list_of_charsets(); if (!empty($CFG->sitemailcharset)) { $choices['0'] = get_string('site') . ' (' . $CFG->sitemailcharset . ')'; } else { $choices['0'] = get_string('site') . ' (UTF-8)'; } $choices = array_merge($choices, $charsets); $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices); } /* $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', 0); $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', 1); 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', 0); } $editors = editors_get_enabled(); if (count($editors) > 1) { $choices = array('' => get_string('defaulteditor')); $firsteditor = ''; foreach (array_keys($editors) as $editor) { if (!$firsteditor) { $firsteditor = $editor; } $choices[$editor] = get_string('pluginname', 'editor_' . $editor); } $mform->addElement('select', 'preference_htmleditor', get_string('textediting'), $choices); $mform->setDefault('preference_htmleditor', ''); } else { // Empty string means use the first chosen text editor. $mform->addElement('hidden', 'preference_htmleditor'); $mform->setDefault('preference_htmleditor', ''); $mform->setType('preference_htmleditor', PARAM_PLUGIN); } $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', $CFG->country); } /* GWL - Remove Extra */ /* $choices = get_list_of_timezones(); $choices['99'] = get_string('serverlocaltime'); if ($CFG->forcetimezone != 99) { $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]); } else { $mform->addElement('select', 'timezone', get_string('timezone'), $choices); $mform->setDefault('timezone', '99'); } */ $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations()); $mform->setDefault('lang', $CFG->lang); // Multi-Calendar Support - see MDL-18375. $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types(); // We do not want to show this option unless there is more than one calendar type to display. if (count($calendartypes) > 1) { $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes); $mform->setDefault('calendartype', $CFG->calendartype); } /*GWL - Remove Edit Profile Extra Field*/ /* 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); } */ /*GWL - Remove Edit Profile Extra Field*/ $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')); 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); /* GWL - Remove Extra Fields While registration */ /* 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); } } */ /* GWL - Remove Extra Fields While registration */ /* GWL - Remove Extra Fields While registration */ /* if (!empty($CFG->usetags) and empty($USER->newadminuser)) { $mform->addElement('header', 'moodle_interests', get_string('interests')); $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial')); $mform->addHelpButton('interests', 'interestslist'); } */ /* GWL - Remove Extra Fields While registration */ /* GWL - Remove Extra Fields While registration */ // 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', PARAM_URL); $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"'); $mform->setType('icq', PARAM_NOTAGS); $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"'); $mform->setType('skype', PARAM_NOTAGS); $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"'); $mform->setType('aim', PARAM_NOTAGS); $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"'); $mform->setType('yahoo', PARAM_NOTAGS); $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"'); $mform->setType('msn', PARAM_NOTAGS); $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"'); $mform->setType('idnumber', PARAM_NOTAGS); $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"'); $mform->setType('institution', PARAM_TEXT); $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"'); $mform->setType('department', PARAM_TEXT); */ /* GWL - Remove Extra Fields While registration */ //$mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"'); //$mform->setType('phone1', PARAM_NOTAGS); //$mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); //$mform->setType('phone2', PARAM_NOTAGS); //$mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"'); //$mform->setType('address', PARAM_TEXT); }