MDL-31654 users: removed hardcoding and added docblock on top of tim's patch

This commit is contained in:
Rajesh Taneja 2012-03-01 11:27:24 +08:00
parent d6aea4cc34
commit bd8dc9ba0a
3 changed files with 46 additions and 27 deletions

View File

@ -98,31 +98,6 @@ $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email',
$PRF_FIELDS = array();
function pre_process_profile_data($data) {
global $CFG, $DB;
foreach ($data as $key => $value) {
if (preg_match('/^profile_field_/', $key)) {
$shortname = str_replace('profile_field_', '', $key);
if ($fields = $DB->get_records('user_info_field', array('shortname' => $shortname))) {
foreach ($fields as $field) {
if (is_file($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php'))
{
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
if ($field->datatype == 'menu') {
$formfield = new $newfield($field->id, $data->id);
$data->$key = $formfield->convert_csv_data($value);
}
}
}
}
}
}
return $data;
}
if ($prof_fields = $DB->get_records('user_info_field')) {
foreach ($prof_fields as $prof_field) {
$PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname;
@ -621,7 +596,7 @@ if ($formdata = $mform2->is_cancelled()) {
$upt->track('status', $struserupdated);
$usersupdated++;
// pre-process custom profile menu fields data from csv file
pre_process_profile_data($existinguser);
$existinguser = uu_pre_process_custom_profile_data($existinguser);
// save custom profile fields data from csv file
profile_save_data($existinguser);
@ -740,7 +715,7 @@ if ($formdata = $mform2->is_cancelled()) {
$upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);
// pre-process custom profile menu fields data from csv file
pre_process_profile_data($user);
$user = uu_pre_process_custom_profile_data($user);
// save custom profile fields data
profile_save_data($user);

View File

@ -363,3 +363,30 @@ function uu_allowed_roles_cache() {
}
return $rolecache;
}
/**
* Pre process custom profile data, and update it with corrected value
*
* @param stdClass $data user profile data
* @return stdClass pre-processed custom profile data
*/
function uu_pre_process_custom_profile_data($data) {
global $CFG, $DB;
// find custom profile fields and check if data needs to converted.
foreach ($data as $key => $value) {
if (preg_match('/^profile_field_/', $key)) {
$shortname = str_replace('profile_field_', '', $key);
if ($fields = $DB->get_records('user_info_field', array('shortname' => $shortname))) {
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, $data->id);
if (method_exists($formfield, 'convert_external_data')) {
$data->$key = $formfield->convert_external_data($value);
}
}
}
}
}
return $data;
}

View File

@ -84,6 +84,23 @@ class profile_field_menu extends profile_field_base {
$mform->setConstant($this->inputname, $this->datakey);
}
}
/**
* Convert external data (csv file) from value to key for processing later
* by edit_save_data_preprocess
*
* @param string $value one of the values in menu options.
* @return int options key for the menu
*/
function convert_external_data($value) {
$retval = array_search($value, $this->options);
// If value is not found in options then return -1, so that it can be handled
// later by edit_save_data_preprocess
if ($retval === false) {
$retval = -1;
}
return $retval;
}
}