Skip to content

Commit

Permalink
Merging changes from 1.8
Browse files Browse the repository at this point in the history
Display custom profile fields in profile page - see MDL-9285
Menu type stores the value rather than the index - see comments on MDL-9285
General tidy up and renaming of funtions in field class.
  • Loading branch information
ikawhero committed Apr 16, 2007
1 parent 08c58ff commit 334415e
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 72 deletions.
30 changes: 30 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -712,6 +712,36 @@ function xmldb_main_upgrade($oldversion=0) {

}

/* Changes to the custom profile menu type - store values rather than indices.
We could do all this with one tricky SQL statement but it's a one-off so no
harm in using PHP loops */
if ($result && $oldversion < 2007041600) {

/// Get the menu fields
if ($fields = get_records('user_info_field', 'datatype', 'menu')) {
foreach ($fields as $field) {

/// Get user data for the menu field
if ($data = get_records('user_info_data', 'fieldid', $field->id)) {

/// Get the menu options
$options = explode("\n", $this->field->param1);
foreach ($data as $d) {
$key = array_search($d->data, $options);

/// If the data is an integer and is not one of the options,
/// set the respective option value
if (is_int($d->data) and (($key === NULL) or ($key === false)) and isset($options[$d->data])) {
$d->data = $options[$d->data];
$result = $result && update_record('user_info_data', $d);
}
}
}
}
}

}

return $result;

}
Expand Down
55 changes: 44 additions & 11 deletions user/profile/field/menu/field.class.php
Expand Up @@ -2,35 +2,68 @@

class profile_field_menu extends profile_field_base {
var $options;
var $selected;
var $datakey;

function profile_field_menu($fieldid) {
/**
* Constructor method.
* Pulls out the options for the menu from the database and sets the
* the corresponding key for the data if it exists
*/
function profile_field_menu($fieldid, $userid) {
//first call parent constructor
$this->profile_field_base($fieldid);
$this->profile_field_base($fieldid, $userid);

/// Param 1 for menu type is the options
$options = explode("\n", $this->field->param1);
$this->options = array();
foreach($options as $key => $option) {
$this->options[$key] = format_string($option);//multilang formatting
if ($option == $this->field->defaultdata) {
$this->selected = $key;
}
}

/// Set the data key
if ($this->data !== NULL) {
$this->datakey = (int)array_search($this->data, $this->options);
}
}

function display_field_add(&$mform) {
/// Create the form field
/**
* Create the code snippet for this field instance
* Overwrites the base class method
* @param object moodleform instance
*/
function edit_field_add(&$mform) {
$mform->addElement('select', $this->inputname, format_string($this->field->name), $this->options);
$mform->setDefault($this->inputname, $this->selected);
}

/// Override base class method
function display_field_default(&$mform) {
/**
* Set the default value for this field instance
* Overwrites the base class method
*/
function edit_field_set_default(&$mform) {
$defaultkey = (int)array_search($field->defaultdata, $this->options);
$mform->setDefault($this->inputname, $defaultkey);
}

/**
* The data from the form returns the key. This should be converted to the
* respective option string to be saved in database
* Overwrites base class accessor method
* @param integer the key returned from the select input in the form
*/
function edit_save_data_preprocess($key) {
return isset($this->options[$key]) ? $this->options[$key] : NULL;
}

/**
* When passing the user object to the form class for the edit profile page
* we should load the key for the saved data
* Overwrites the base class method
* @param object user object
*/
function edit_load_user_data(&$user) {
$user->{$this->inputname} = $this->datakey;
}

}

?>
2 changes: 1 addition & 1 deletion user/profile/field/text/field.class.php
Expand Up @@ -2,7 +2,7 @@

class profile_field_text extends profile_field_base {

function display_field_add(&$mform) {
function edit_field_add(&$mform) {
$size = $this->field->param1;
$maxlength = $this->field->param2;

Expand Down

0 comments on commit 334415e

Please sign in to comment.