Permalink
Browse files

Merging changes from 1.8

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...
1 parent 08c58ff commit 334415e93f144244851ded97161735aceab3179a ikawhero committed Apr 16, 2007
Showing with 239 additions and 72 deletions.
  1. +30 −0 lib/db/upgrade.php
  2. +44 −11 user/profile/field/menu/field.class.php
  3. +1 −1 user/profile/field/text/field.class.php
  4. +158 −59 user/profile/lib.php
  5. +5 −0 user/view.php
  6. +1 −1 version.php
View
@@ -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;
}
@@ -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,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;
Oops, something went wrong.

0 comments on commit 334415e

Please sign in to comment.