Skip to content
Permalink
Browse files

Merge branch 'wip-mdl-31654' of git://github.com/rajeshtaneja/moodle

  • Loading branch information...
Aparup Banerjee
Aparup Banerjee committed Mar 27, 2012
2 parents 4767d32 + 1b4d2d5 commit c2bc04233028537bc4408670fb1d544d36668c0a
Showing with 87 additions and 4 deletions.
  1. +11 −4 admin/tool/uploaduser/index.php
  2. +59 −0 admin/tool/uploaduser/locallib.php
  3. +17 −0 user/profile/field/menu/field.class.php
@@ -595,6 +595,8 @@
$upt->track('status', $struserupdated);
$usersupdated++;
// pre-process custom profile menu fields data from csv file
$existinguser = uu_pre_process_custom_profile_data($existinguser);
// save custom profile fields data from csv file
profile_save_data($existinguser);
@@ -712,6 +714,8 @@
$user->id = $DB->insert_record('user', $user);
$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
$user = uu_pre_process_custom_profile_data($user);
// save custom profile fields data
profile_save_data($user);
@@ -967,6 +971,7 @@
$data = array();
$cir->init();
$linenum = 1; //column header is first line
$noerror = true; // Keep status of any error.
while ($linenum <= $previewrows and $fields = $cir->next()) {
$linenum++;
$rowcols = array();
@@ -1003,7 +1008,8 @@
$rowcols['status'][] = get_string('fieldrequired', 'error', 'city');
}
}
// Check if rowcols have custom profile field with correct data and update error state.
$noerror = uu_check_custom_profile_data($rowcols) && $noerror;
$rowcols['status'] = implode('<br />', $rowcols['status']);
$data[] = $rowcols;
}
@@ -1028,9 +1034,10 @@
echo html_writer::tag('div', html_writer::table($table), array('class'=>'flexible-wrap'));
/// Print the form
$mform2->display();
// Print the form if valid values are available
if ($noerror) {
$mform2->display();
}
echo $OUTPUT->footer();
die;
@@ -363,3 +363,62 @@ 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;
}
/**
* Checks if data provided for custom fields is correct
* Currently checking for custom profile field or type menu
*
* @param array $data user profile data
* @return bool true if no error else false
*/
function uu_check_custom_profile_data(&$data) {
global $CFG, $DB;
$noerror = true;
// 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, 0);
if (method_exists($formfield, 'convert_external_data') &&
is_null($formfield->convert_external_data($value))) {
$data['status'][] = get_string('invaliduserfield', 'error', $shortname);
$noerror = false;
}
}
}
}
}
return $noerror;
}
@@ -84,6 +84,23 @@ function edit_field_set_locked(&$mform) {
$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 null, so that it can be handled
// later by edit_save_data_preprocess
if ($retval === false) {
$retval = null;
}
return $retval;
}
}

0 comments on commit c2bc042

Please sign in to comment.
You can’t perform that action at this time.