/** * Function to convert Moodle custom profile fields to correct option * to be called after profile_load_data() * @param object $mu Moodle user object with custom menu fields present */ function fix_moodle_profile_fields(&$mu) { global $CFG, $DB, $USER; // Find custom profile menu-type fields & ensure values are a valid option foreach ($mu as $key => $value) { if (preg_match('/^profile_field_/', $key)) { $shortname = str_replace('profile_field_', '', $key); if ($fieldid = $DB->get_field('user_info_field', 'id', array('shortname' => $shortname, 'datatype' => 'menu'))) { require_once $CFG->dirroot . '/user/profile/field/menu/field.class.php'; $menufield = new profile_field_menu($fieldid, isset($mu->id) ? $mu->id : 0); if (($mu->{$key} = $menufield->convert_external_data($value)) === null) { unset($mu->{$key}); // illegal value so unset it } } } } }
/** * Validates user profile field data and performs any required * data transformation in-place * * @param object $record The import record * @param string $filename The import file name, used for logging * * @return boolean true if the record validates, otherwise false */ protected function validate_user_profile_data(&$record, $filename) { global $CFG, $DB, $USER; //go through each profile field in the header foreach ($this->fields as $shortname => $field) { $key = 'profile_field_' . $shortname; $data = $record->{$key}; //perform type-specific validation and transformation if ($field->datatype == 'checkbox') { if ($data != 0 && $data != 1) { $this->fslogger->log_failure("\"{$data}\" is not one of the available options for a checkbox profile field {$shortname} (0, 1).", 0, $filename, $this->linenumber, $record, "user"); return false; } } else { if ($field->datatype == 'menu') { // ELIS-8306: Must support multi-lang options require_once $CFG->dirroot . '/user/profile/field/menu/field.class.php'; $menufield = new profile_field_menu($field->id, $USER->id); if ($menufield->convert_external_data($data) === null) { $this->fslogger->log_failure("\"{$data}\" is not one of the available options for a menu of choices profile field {$shortname}.", 0, $filename, $this->linenumber, $record, "user"); return false; } } else { if ($field->datatype == 'datetime') { $value = $this->parse_date($data); if ($value === false) { $identifier = $this->mappings["profile_field_{$shortname}"]; $this->fslogger->log_failure("{$identifier} value of \"{$data}\" " . "is not a valid date in MMM/DD/YYYY or MM/DD/YYYY format.", 0, $filename, $this->linenumber, $record, "user"); return false; } $record->{$key} = $value; } } } } return true; }