MDL-8096 - some more fixes and minor refactoring in custom profile fields

This commit is contained in:
skodak 2007-01-26 13:49:32 +00:00
parent 1bf65e5880
commit a5d3b07224
5 changed files with 28 additions and 32 deletions

View file

@ -33,7 +33,6 @@ class profile_define_base {
$form->addElement('htmleditor', 'description', get_string('profiledescription', 'admin'));
$form->setHelpButton('description', array('text', get_string('helptext')));
$form->setType('description', PARAM_MULTILANG);
$form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin'));

View file

@ -16,15 +16,15 @@ class profile_field_menu extends profile_field_base {
}
function display_field_add(&$form) {
function display_field_add(&$mform) {
/// Create the form field
$form->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
$mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
}
/// Override base class method
function display_field_default(&$form) {
function display_field_default(&$mform) {
$defaultkey = (int)array_search($field->defaultdata, $this->options);
$form->setDefault($this->inputname, $defaultkey);
$mform->setDefault($this->inputname, $defaultkey);
}
}

View file

@ -9,12 +9,12 @@ class profile_define_text extends profile_define_base {
/// Param 1 for text type is the size of the field
$form->addElement('text', 'param1', get_string('profilefieldsize', 'admin'), 'size="6"');
$form->setDefault('param1', 50);
$form->setDefault('param1', 30);
$form->setType('param1', PARAM_INT);
/// Param 2 for text type is the maxlength of the field
$form->addElement('text', 'param2', get_string('profilefieldmaxlength', 'admin'), 'size="6"');
$form->setDefault('param2', 100);
$form->setDefault('param2', 2048);
$form->setType('param2', PARAM_INT);
}

View file

@ -2,16 +2,13 @@
class profile_field_text extends profile_field_base {
function display_field_add(&$form) {
/// Param 1 for text type is the size of the field
$size = (empty($this->field->param1)) ? '30' : $this->field->param1;
/// Param 2 for text type is the maxlength of the field
$maxlength = (empty($this->field->param2)) ? '2048' : $this->field->param2;
function display_field_add(&$mform) {
$size = $this->field->param1;
$maxlength = $this->field->param2;
/// Create the form field
$form->addElement('text', $this->inputname, format_string($this->field->name), 'maxlength="'.$maxlength.'" size="'.$size.'" ');
$form->setType($this->inputname, PARAM_MULTILANG);
$mform->addElement('text', $this->inputname, format_string($this->field->name), 'maxlength="'.$maxlength.'" size="'.$size.'" ');
$mform->setType($this->inputname, PARAM_MULTILANG);
}
}

View file

@ -50,15 +50,15 @@ class profile_field_base {
* @param object instance of the moodleform class
* $return boolean
*/
function display_field(&$form) {
function display_field(&$mform) {
if ($this->field->visible != PROFILE_VISIBLE_NONE
or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$this->display_field_add($form);
$this->display_field_set_default($form);
$this->display_field_set_required($form);
$this->display_field_set_locked($form);
$this->display_field_add($mform);
$this->display_field_set_default($mform);
$this->display_field_set_required($mform);
$this->display_field_set_locked($mform);
}
}
@ -97,7 +97,7 @@ class profile_field_base {
* Adds the profile field to the moodle form class
* @param form instance of the moodleform class
*/
function display_field_add(&$form) {
function display_field_add(&$mform) {
error('This abstract method must be overriden');
}
@ -121,9 +121,9 @@ class profile_field_base {
* Sets the default data for the field in the form object
* @param object instance of the moodleform class
*/
function display_field_set_default(&$form) {
function display_field_set_default(&$mform) {
if (!empty($default)) {
$form->setDefault($this->inputname, $this->field->defaultdata);
$mform->setDefault($this->inputname, $this->field->defaultdata);
}
}
@ -131,9 +131,9 @@ class profile_field_base {
* Sets the required flag for the field in the form object
* @param object instance of the moodleform class
*/
function display_field_set_required(&$form) {
function display_field_set_required(&$mform) {
if ($this->field->required and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$form->addRule($this->inputname, get_string('required'), 'required', null, 'client');
$mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
}
}
@ -141,9 +141,9 @@ class profile_field_base {
* HardFreeze the field if locked.
* @param object instance of the moodleform class
*/
function display_field_set_locked(&$form) {
function display_field_set_locked(&$mform) {
if ($this->field->locked and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
$form->hardFreeze($this->inputname);
$mform->hardFreeze($this->inputname);
}
}
@ -178,18 +178,18 @@ function profile_load_data(&$user) {
* Print out the customisable categories and fields for a users profile
* @param object instance of the moodleform class
*/
function profile_definition(&$form) {
function profile_definition(&$mform) {
global $CFG;
if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
$form->addElement('header', 'category_'.$category->id, format_string($category->name));
$mform->addElement('header', 'category_'.$category->id, format_string($category->name));
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id);
$formfield->display_field($form);
$formfield->display_field($mform);
}
}