MDL-31654 users: Added check for custom profile fields, if wrong data is passed then user will not be able to proceed
This commit is contained in:
@@ -971,6 +971,7 @@ echo $OUTPUT->heading(get_string('uploaduserspreview', 'tool_uploaduser'));
|
||||
$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();
|
||||
@@ -1007,7 +1008,8 @@ while ($linenum <= $previewrows and $fields = $cir->next()) {
|
||||
$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;
|
||||
}
|
||||
@@ -1032,9 +1034,10 @@ $table->head[] = get_string('status');
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -390,3 +390,35 @@ function uu_pre_process_custom_profile_data($data) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -94,10 +94,10 @@ class profile_field_menu extends profile_field_base {
|
||||
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
|
||||
// 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 = -1;
|
||||
$retval = null;
|
||||
}
|
||||
return $retval;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user