From b88adb55fc1ba3d8e4c43aa6855567f72cde7712 Mon Sep 17 00:00:00 2001 From: Gilles-Philippe Leblanc Date: Thu, 12 Apr 2012 13:32:14 -0400 Subject: [PATCH 1/4] MDL-16982 Administration: Adding data mapping for custom user fields --- admin/auth_config.php | 73 +++++++++++++++++++++++++++++++++++++++++-- auth/cas/auth.php | 14 ++++++++- auth/cas/config.html | 2 +- auth/ldap/auth.php | 57 ++++++++++++++++++++++++++++----- auth/ldap/config.html | 2 +- lib/moodlelib.php | 60 +++++++++++++++++++++++++++++++++-- 6 files changed, 192 insertions(+), 16 deletions(-) diff --git a/admin/auth_config.php b/admin/auth_config.php index c70271124fd..40329da2089 100644 --- a/admin/auth_config.php +++ b/admin/auth_config.php @@ -49,6 +49,7 @@ if ($frm = data_submitted() and confirm_sesskey()) { } $user_fields = $authplugin->userfields; +$custom_fields = $authplugin->custom_fields; //$user_fields = array("firstname", "lastname", "email", "phone1", "phone2", "institution", "department", "address", "city", "country", "description", "idnumber", "lang"); /// Get the auth title (from core or own auth lang files) @@ -72,7 +73,7 @@ echo $OUTPUT->box_start('informationbox'); echo $authdescription; echo $OUTPUT->box_end(); echo "
\n"; -$authplugin->config_form($frm, $err, $user_fields); +$authplugin->config_form($frm, $err, $user_fields, $custom_fields); echo $OUTPUT->box_end(); echo '

\n"; echo "\n"; @@ -87,8 +88,8 @@ exit; // but some may want a custom one if they are offering // other options // Note: lockconfig_ fields have special handling. -function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts) { - global $OUTPUT; +function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts, $custom_fields = array()) { + global $DB, $OUTPUT; echo ''; if ($retrieveopts) { echo $OUTPUT->heading(get_string('auth_data_mapping', 'auth')); @@ -174,6 +175,72 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, } echo ''; } + if(!empty($custom_fields)) { + echo ''; + + echo '

' . get_string('profilefields', 'admin') . '

'; + + echo ''; + + foreach($custom_fields as $field) { + + // Define some vars we'll work with + if (!isset($pluginconfig->{"field_map_$field"})) { + $pluginconfig->{"field_map_$field"} = ''; + } + if (!isset($pluginconfig->{"field_updatelocal_$field"})) { + $pluginconfig->{"field_updatelocal_$field"} = ''; + } + if (!isset($pluginconfig->{"field_updateremote_$field"})) { + $pluginconfig->{"field_updateremote_$field"} = ''; + } + if (!isset($pluginconfig->{"field_lock_$field"})) { + $pluginconfig->{"field_lock_$field"} = ''; + } + + // define the fieldname we display to the user + $fieldname = $field; + if ($fieldname === 'lang') { + $fieldname = get_string('language'); + } elseif (preg_match('/^(.+?)(\d+)$/', $fieldname, $matches)) { + $fieldname = get_string($matches[1]) . ' ' . $matches[2]; + } elseif ($fieldname == 'url') { + $fieldname = get_string('webpage'); + } else { + $fieldname = $DB->get_field('user_info_field', 'name', array('shortname'=>$fieldname)); + } + if ($retrieveopts) { + $varname = 'field_map_' . $field; + + echo ''; + echo ''; + echo ''; + + echo "$varname}\" />"; + echo '
'; + echo ' '; + echo html_writer::select($updatelocaloptions, "lockconfig_field_updatelocal_{$field}", $pluginconfig->{"field_updatelocal_$field"}, false); + echo '
'; + if ($updateopts) { + echo ' '; + echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false); + echo '
'; + + + } + echo ' '; + echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false); + echo '
'; + } else { + echo ''; + echo ''; + echo ''; + echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false); + } + echo ''; + echo ''; + } + } } diff --git a/auth/cas/auth.php b/auth/cas/auth.php index 95ab72523d9..8a316b61ddc 100644 --- a/auth/cas/auth.php +++ b/auth/cas/auth.php @@ -36,14 +36,26 @@ require_once($CFG->dirroot.'/auth/cas/CAS/CAS.php'); */ class auth_plugin_cas extends auth_plugin_ldap { + /** + * moodle custom fields to sync with + * @var array() + */ + var $custom_fields = array(); + /* /** * Constructor. */ function auth_plugin_cas() { + global $DB; $this->authtype = 'cas'; $this->roleauth = 'auth_cas'; $this->errorlogtag = '[AUTH CAS] '; $this->init_plugin($this->authtype); + $custom_fields = $DB->get_records('user_info_field'); + + foreach($custom_fields as $cf) { + $this->custom_fields[] = $cf->shortname; + } } function prevent_local_passwords() { @@ -222,7 +234,7 @@ class auth_plugin_cas extends auth_plugin_ldap { * * @param array $page An object containing all the data for this page. */ - function config_form($config, $err, $user_fields) { + function config_form($config, $err, $user_fields, $custom_fields=array()) { global $CFG, $OUTPUT; if (!function_exists('ldap_connect')) { // Is php-ldap really there? diff --git a/auth/cas/config.html b/auth/cas/config.html index 222845b1628..654e6fef2e4 100644 --- a/auth/cas/config.html +++ b/auth/cas/config.html @@ -491,6 +491,6 @@ $help .= get_string('auth_updateremote_expl', 'auth'); $help .= '
'; $help .= get_string('auth_updateremote_ldap', 'auth'); -print_auth_lock_options($this->authtype, $user_fields, $help, true, true); +print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $custom_fields); ?> diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 54a987ef8cd..ffa58269377 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -130,14 +130,27 @@ class auth_plugin_ldap extends auth_plugin_base { } } + + /** + * moodle custom fields to sync with + * @var array() + */ + var $custom_fields = array(); + /* + /** * Constructor with initialisation. */ function auth_plugin_ldap() { + global $DB; $this->authtype = 'ldap'; $this->roleauth = 'auth_ldap'; $this->errorlogtag = '[AUTH LDAP] '; $this->init_plugin($this->authtype); + $custom_fields = $DB->get_records('user_info_field'); + foreach($custom_fields as $cf) { + $this->custom_fields[] = $cf->shortname; + } } /** @@ -293,7 +306,9 @@ class auth_plugin_ldap extends auth_plugin_base { } else { $newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8'); } - if (!empty($newval)) { // favour ldap entries that are set + + // Need to allow for 0 or '0' will ldap ever return an empty string or will the array_key_exists catch such things? + if (isset($newval) || $newval !== '') { // favour ldap entries that are set $ldapval = $newval; } } @@ -1162,9 +1177,23 @@ class auth_plugin_ldap extends auth_plugin_base { foreach ($attrmap as $key => $ldapkeys) { // Only process if the moodle field ($key) has changed and we // are set to update LDAP with it + + //updating a custom field or a standard field/ 0 for none at all + $update_attr_type = 0; + if (isset($olduser->$key) and isset($newuser->$key) - and $olduser->$key !== $newuser->$key - and !empty($this->config->{'field_updateremote_'. $key})) { + and $olduser->$key !== $newuser->$key) { + //updating a user profile field + $update_attr_type = 2; + $profile_field = $key; + } else if(isset($olduser->{'profile_field_' . $key}) and isset($newuser->{'profile_field_' . $key}) + and $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}){ + //updating a custom profile field + $update_attr_type = 1; + $profile_field = 'profile_field_' . $key; + } + + if (!empty($profile_field) and !empty($this->config->{'field_updateremote_'. $key})) { // For ldap values that could be in more than one // ldap key, we will do our best to match // where they came from @@ -1177,13 +1206,14 @@ class auth_plugin_ldap extends auth_plugin_base { $ambiguous = false; } - $nuvalue = textlib::convert($newuser->$key, 'utf-8', $this->config->ldapencoding); + $nuvalue = textlib::convert($newuser->$profile_field, 'utf-8', $this->config->ldapencoding); empty($nuvalue) ? $nuvalue = array() : $nuvalue; - $ouvalue = textlib::convert($olduser->$key, 'utf-8', $this->config->ldapencoding); + $ouvalue = textlib::convert($olduser->$profile_field, 'utf-8', $this->config->ldapencoding); foreach ($ldapkeys as $ldapkey) { $ldapkey = $ldapkey; - $ldapvalue = $user_entry[$ldapkey][0]; + $ldapvalue = empty($user_entry[$ldapkey][0])? null: $ldapvalue; + if (!$ambiguous) { // Skip update if the values already match if ($nuvalue !== $ldapvalue) { @@ -1450,6 +1480,19 @@ class auth_plugin_ldap extends auth_plugin_base { } } } + + //add custom fields + foreach($this->custom_fields as $field) { + if(!in_array($field, $this->userfields)) { //just in case so we don't overwrite any values in the user fields + if(!empty($this->config->{"field_map_$field"})) { + $moodleattributes[$field] = $this->config->{"field_map_$field"}; + if (preg_match('/,/',$moodleattributes[$field])) { + $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ? + } + } + } + } + $moodleattributes['username'] = textlib::strtolower(trim($this->config->user_attribute)); return $moodleattributes; } @@ -1749,7 +1792,7 @@ class auth_plugin_ldap extends auth_plugin_base { * * @param array $page An object containing all the data for this page. */ - function config_form($config, $err, $user_fields) { + function config_form($config, $err, $user_fields, $custom_fields=array()) { global $CFG, $OUTPUT; if (!function_exists('ldap_connect')) { // Is php-ldap really there? diff --git a/auth/ldap/config.html b/auth/ldap/config.html index 514039787ab..5d44cdddc7a 100644 --- a/auth/ldap/config.html +++ b/auth/ldap/config.html @@ -604,6 +604,6 @@ $help .= get_string('auth_updateremote_expl', 'auth'); $help .= '
'; $help .= get_string('auth_updateremote_ldap', 'auth'); -print_auth_lock_options($this->authtype, $user_fields, $help, true, true); +print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $custom_fields); ?> diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 463787578a2..85d58c80cee 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3846,11 +3846,21 @@ function create_user_record($username, $password, $auth = 'manual') { $authplugin = get_auth_plugin($auth); $newuser = new stdClass(); - + $customfield = new stdClass(); if ($newinfo = $authplugin->get_userinfo($username)) { $newinfo = truncate_userinfo($newinfo); foreach ($newinfo as $key => $value){ - $newuser->$key = $value; + if(in_array($key, $authplugin->userfields)) { + $newuser->$key = $value; + }else if(isset($authplugin->custom_fields) && in_array($key, $authplugin->custom_fields)) { + $info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); + $data = $info_field->defaultdata; + if(strcmp($data, $value) !== 0) { + $customfield->$key = new stdClass(); + $customfield->$key->fieldid = $info_field->id; + $customfield->$key->data = $value; + } + } } } @@ -3880,6 +3890,12 @@ function create_user_record($username, $password, $auth = 'manual') { $newuser->mnethostid = $CFG->mnet_localhost_id; $newuser->id = $DB->insert_record('user', $newuser); + + foreach ($customfield as $key) { + $key->userid = $newuser->id; + $key->id = $DB->insert_record("user_info_data",$key); + } + $user = get_complete_user_data('id', $newuser->id); if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})){ set_user_preference('auth_forcepasswordchange', 1, $user); @@ -3914,7 +3930,8 @@ function update_user_record($username) { $newinfo = truncate_userinfo($newinfo); foreach ($newinfo as $key => $value){ $key = strtolower($key); - if (!property_exists($oldinfo, $key) or $key === 'username' or $key === 'id' + $iscustom = in_array($key, $userauth->custom_fields); + if ((!property_exists($oldinfo, $key) && !$iscustom) or $key === 'username' or $key === 'id' or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') { // unknown or must not be changed continue; @@ -3932,9 +3949,46 @@ function update_user_record($username) { // nothing_ for this field. Thus it makes sense to let this value // stand in until LDAP is giving a value for this field. if (!(empty($value) && $lockval === 'unlockedifempty')) { + if(in_array($key, $userauth->userfields)) { if ((string)$oldinfo->$key !== (string)$value) { $newuser[$key] = (string)$value; } + }else if($iscustom) { + + //if there is no value in the user_info_data then + $info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); + $userid = $DB->get_field('user', 'id', array('username'=>$username)); + $data = $DB->get_field('user_info_data', 'data', array('userid'=>$userid, 'fieldid'=>$info_field->id)); + if($data === false) { + $data = $info_field->defaultdata; + if(strcmp($data, $value) !== 0) { + $row = new stdClass(); + $row->userid = $userid; + $row->fieldid = $info_field->id; + $row->data = $data; + $row->id = $DB->insert_record("user_info_data", $row); + } + } + + if(strcmp($data, $value) !== 0) { + $valid = true; + + //check to make sure that the value we are placing in is a valid one + if(strcmp($info_field->datatype, 'menu') == 0){ + $validValues = explode("\n", $info_field->param1); + if(!in_array($value, $validValues)) { + $valid = false; + } + } else if(strcmp($info_field->datatype, 'checkbox') == 0) { + if($value != 1 && $value != 0) { + $valid = false; + } + } + if($valid) { + $DB->set_field('user_info_data','data',$value, array('userid'=>$userid, 'fieldid'=>$info_field->id)); + } + } + } } } } From 57d135a1c6ade3051a818b53cd4241ef5356468f Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 29 Nov 2012 14:32:53 +0800 Subject: [PATCH 2/4] MDL-16982 Administration: Cleaned whitespaces and alignment in orignal patch --- admin/auth_config.php | 95 ++++++-------------------------- auth/cas/auth.php | 14 +---- auth/cas/config.html | 2 +- auth/ldap/auth.php | 58 +++++++------------- auth/ldap/config.html | 2 +- lib/authlib.php | 32 +++++++++++ lib/moodlelib.php | 125 ++++++++++++++++++++++-------------------- 7 files changed, 136 insertions(+), 192 deletions(-) diff --git a/admin/auth_config.php b/admin/auth_config.php index 40329da2089..f87870d5152 100644 --- a/admin/auth_config.php +++ b/admin/auth_config.php @@ -49,7 +49,6 @@ if ($frm = data_submitted() and confirm_sesskey()) { } $user_fields = $authplugin->userfields; -$custom_fields = $authplugin->custom_fields; //$user_fields = array("firstname", "lastname", "email", "phone1", "phone2", "institution", "department", "address", "city", "country", "description", "idnumber", "lang"); /// Get the auth title (from core or own auth lang files) @@ -73,7 +72,7 @@ echo $OUTPUT->box_start('informationbox'); echo $authdescription; echo $OUTPUT->box_end(); echo "
\n"; -$authplugin->config_form($frm, $err, $user_fields, $custom_fields); +$authplugin->config_form($frm, $err, $user_fields); echo $OUTPUT->box_end(); echo '

\n"; echo "\n"; @@ -88,7 +87,7 @@ exit; // but some may want a custom one if they are offering // other options // Note: lockconfig_ fields have special handling. -function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts, $custom_fields = array()) { +function print_auth_lock_options($auth, $user_fields, $helptext, $retrieveopts, $updateopts, $customfields = array()) { global $DB, $OUTPUT; echo ''; if ($retrieveopts) { @@ -108,14 +107,18 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $pluginconfig = get_config("auth/$auth"); - // helptext is on a field with rowspan + // Helptext is on a field with rowspan. if (empty($helptext)) { - $helptext = ' '; + $helptext = ' '; + } + + // If we have custom fields then merge them with user fields. + if (!empty($customfields)) { + $user_fields = array_merge($user_fields, $customfields); } foreach ($user_fields as $field) { - - // Define some vars we'll work with + // Define some vars we'll work with. if (!isset($pluginconfig->{"field_map_$field"})) { $pluginconfig->{"field_map_$field"} = ''; } @@ -129,7 +132,7 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $pluginconfig->{"field_lock_$field"} = ''; } - // define the fieldname we display to the user + // Define the fieldname we display to the user. $fieldname = $field; if ($fieldname === 'lang') { $fieldname = get_string('language'); @@ -137,6 +140,10 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $fieldname = get_string($matches[1]) . ' ' . $matches[2]; } elseif ($fieldname == 'url') { $fieldname = get_string('webpage'); + } elseif (!empty($customfields) && in_array($field, $customfields)) { + // If custom field then pick name from database. + $fieldshortname = str_replace('profile_field_', '', $fieldname); + $fieldname = $DB->get_field('user_info_field', 'name', array('shortname' => $fieldshortname)); } else { $fieldname = get_string($fieldname); } @@ -156,8 +163,6 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, echo ' '; echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false); echo '
'; - - } echo ' '; echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false); @@ -175,72 +180,4 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, } echo ''; } - if(!empty($custom_fields)) { - echo ''; - - echo '

' . get_string('profilefields', 'admin') . '

'; - - echo ''; - - foreach($custom_fields as $field) { - - // Define some vars we'll work with - if (!isset($pluginconfig->{"field_map_$field"})) { - $pluginconfig->{"field_map_$field"} = ''; - } - if (!isset($pluginconfig->{"field_updatelocal_$field"})) { - $pluginconfig->{"field_updatelocal_$field"} = ''; - } - if (!isset($pluginconfig->{"field_updateremote_$field"})) { - $pluginconfig->{"field_updateremote_$field"} = ''; - } - if (!isset($pluginconfig->{"field_lock_$field"})) { - $pluginconfig->{"field_lock_$field"} = ''; - } - - // define the fieldname we display to the user - $fieldname = $field; - if ($fieldname === 'lang') { - $fieldname = get_string('language'); - } elseif (preg_match('/^(.+?)(\d+)$/', $fieldname, $matches)) { - $fieldname = get_string($matches[1]) . ' ' . $matches[2]; - } elseif ($fieldname == 'url') { - $fieldname = get_string('webpage'); - } else { - $fieldname = $DB->get_field('user_info_field', 'name', array('shortname'=>$fieldname)); - } - if ($retrieveopts) { - $varname = 'field_map_' . $field; - - echo ''; - echo ''; - echo ''; - - echo "$varname}\" />"; - echo '
'; - echo ' '; - echo html_writer::select($updatelocaloptions, "lockconfig_field_updatelocal_{$field}", $pluginconfig->{"field_updatelocal_$field"}, false); - echo '
'; - if ($updateopts) { - echo ' '; - echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false); - echo '
'; - - - } - echo ' '; - echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false); - echo '
'; - } else { - echo ''; - echo ''; - echo ''; - echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false); - } - echo ''; - echo ''; - } - } -} - - +} \ No newline at end of file diff --git a/auth/cas/auth.php b/auth/cas/auth.php index 8a316b61ddc..95ab72523d9 100644 --- a/auth/cas/auth.php +++ b/auth/cas/auth.php @@ -36,26 +36,14 @@ require_once($CFG->dirroot.'/auth/cas/CAS/CAS.php'); */ class auth_plugin_cas extends auth_plugin_ldap { - /** - * moodle custom fields to sync with - * @var array() - */ - var $custom_fields = array(); - /* /** * Constructor. */ function auth_plugin_cas() { - global $DB; $this->authtype = 'cas'; $this->roleauth = 'auth_cas'; $this->errorlogtag = '[AUTH CAS] '; $this->init_plugin($this->authtype); - $custom_fields = $DB->get_records('user_info_field'); - - foreach($custom_fields as $cf) { - $this->custom_fields[] = $cf->shortname; - } } function prevent_local_passwords() { @@ -234,7 +222,7 @@ class auth_plugin_cas extends auth_plugin_ldap { * * @param array $page An object containing all the data for this page. */ - function config_form($config, $err, $user_fields, $custom_fields=array()) { + function config_form($config, $err, $user_fields) { global $CFG, $OUTPUT; if (!function_exists('ldap_connect')) { // Is php-ldap really there? diff --git a/auth/cas/config.html b/auth/cas/config.html index 654e6fef2e4..ff9c8636aa8 100644 --- a/auth/cas/config.html +++ b/auth/cas/config.html @@ -491,6 +491,6 @@ $help .= get_string('auth_updateremote_expl', 'auth'); $help .= '
'; $help .= get_string('auth_updateremote_ldap', 'auth'); -print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $custom_fields); +print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $this->get_custom_user_profile_fields()); ?> diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index ffa58269377..79a36240950 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -129,28 +129,16 @@ class auth_plugin_ldap extends auth_plugin_base { // so leave $this->config->objectclass as is. } } - - - /** - * moodle custom fields to sync with - * @var array() - */ - var $custom_fields = array(); - /* /** * Constructor with initialisation. */ function auth_plugin_ldap() { - global $DB; + global $DB; $this->authtype = 'ldap'; $this->roleauth = 'auth_ldap'; $this->errorlogtag = '[AUTH LDAP] '; $this->init_plugin($this->authtype); - $custom_fields = $DB->get_records('user_info_field'); - foreach($custom_fields as $cf) { - $this->custom_fields[] = $cf->shortname; - } } /** @@ -306,9 +294,10 @@ class auth_plugin_ldap extends auth_plugin_base { } else { $newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8'); } - - // Need to allow for 0 or '0' will ldap ever return an empty string or will the array_key_exists catch such things? - if (isset($newval) || $newval !== '') { // favour ldap entries that are set + + // Need to allow for 0 or '0' will ldap ever return an empty string + // or will the array_key_exists catch such things. + if (isset($newval) && ($newval !== '')) { // Favour ldap entries that are set. $ldapval = $newval; } } @@ -1177,23 +1166,15 @@ class auth_plugin_ldap extends auth_plugin_base { foreach ($attrmap as $key => $ldapkeys) { // Only process if the moodle field ($key) has changed and we // are set to update LDAP with it - - //updating a custom field or a standard field/ 0 for none at all - $update_attr_type = 0; - if (isset($olduser->$key) and isset($newuser->$key) - and $olduser->$key !== $newuser->$key) { - //updating a user profile field - $update_attr_type = 2; + and ($olduser->$key !== $newuser->$key)) { $profile_field = $key; - } else if(isset($olduser->{'profile_field_' . $key}) and isset($newuser->{'profile_field_' . $key}) - and $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}){ - //updating a custom profile field - $update_attr_type = 1; + } else if (isset($olduser->{'profile_field_' . $key}) && isset($newuser->{'profile_field_' . $key}) + && $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}) { $profile_field = 'profile_field_' . $key; } - if (!empty($profile_field) and !empty($this->config->{'field_updateremote_'. $key})) { + if (!empty($profile_field) && !empty($this->config->{'field_updateremote_' . $key})) { // For ldap values that could be in more than one // ldap key, we will do our best to match // where they came from @@ -1212,7 +1193,7 @@ class auth_plugin_ldap extends auth_plugin_base { foreach ($ldapkeys as $ldapkey) { $ldapkey = $ldapkey; - $ldapvalue = empty($user_entry[$ldapkey][0])? null: $ldapvalue; + $ldapvalue = empty($user_entry[$ldapkey][0]) ? null : $ldapvalue; if (!$ambiguous) { // Skip update if the values already match @@ -1480,15 +1461,14 @@ class auth_plugin_ldap extends auth_plugin_base { } } } - - //add custom fields - foreach($this->custom_fields as $field) { - if(!in_array($field, $this->userfields)) { //just in case so we don't overwrite any values in the user fields - if(!empty($this->config->{"field_map_$field"})) { - $moodleattributes[$field] = $this->config->{"field_map_$field"}; - if (preg_match('/,/',$moodleattributes[$field])) { - $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ? - } + + // Add custom fields. + $customfields = $this->get_custom_user_profile_fields(); + foreach ($customfields as $field) { + if (!empty($this->config->{"field_map_$field"})) { + $moodleattributes[$field] = $this->config->{"field_map_$field"}; + if (preg_match('/,/',$moodleattributes[$field])) { + $moodleattributes[$field] = explode(',', $moodleattributes[$field]); } } } @@ -1792,7 +1772,7 @@ class auth_plugin_ldap extends auth_plugin_base { * * @param array $page An object containing all the data for this page. */ - function config_form($config, $err, $user_fields, $custom_fields=array()) { + function config_form($config, $err, $user_fields) { global $CFG, $OUTPUT; if (!function_exists('ldap_connect')) { // Is php-ldap really there? diff --git a/auth/ldap/config.html b/auth/ldap/config.html index 5d44cdddc7a..f23886a2024 100644 --- a/auth/ldap/config.html +++ b/auth/ldap/config.html @@ -604,6 +604,6 @@ $help .= get_string('auth_updateremote_expl', 'auth'); $help .= '
'; $help .= get_string('auth_updateremote_ldap', 'auth'); -print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $custom_fields); +print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $this->get_custom_user_profile_fields()); ?> diff --git a/lib/authlib.php b/lib/authlib.php index f8aa8202612..daf14a0f63e 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -117,6 +117,12 @@ class auth_plugin_base { 'address' ); + /** + * Moodle custom fields to sync with. + * @var array() + */ + var $custom_fields = null; + /** * This is the primary method that is used by the authenticate_user_login() @@ -522,6 +528,32 @@ class auth_plugin_base { return array(); } + /** + * Return custom user profile fields. + * + * @return array list of custom fields. + */ + public function get_custom_user_profile_fields() { + global $DB; + // If already retrived then return. + if (!is_null($this->custom_fields)) { + return $this->custom_fields; + } + + $this->custom_fields = array(); + + $customfields = array(); + if ($proffields = $DB->get_records('user_info_field')) { + foreach ($proffields as $proffield) { + $customfields[] = 'profile_field_'.$proffield->shortname; + } + } + unset($proffields); + $this->custom_fields = $customfields; + + return $customfields; + } + } /** diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 85d58c80cee..0e9e6fb6402 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3846,21 +3846,25 @@ function create_user_record($username, $password, $auth = 'manual') { $authplugin = get_auth_plugin($auth); $newuser = new stdClass(); - $customfield = new stdClass(); + $customfield = new stdClass(); if ($newinfo = $authplugin->get_userinfo($username)) { $newinfo = truncate_userinfo($newinfo); foreach ($newinfo as $key => $value){ - if(in_array($key, $authplugin->userfields)) { - $newuser->$key = $value; - }else if(isset($authplugin->custom_fields) && in_array($key, $authplugin->custom_fields)) { - $info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); - $data = $info_field->defaultdata; - if(strcmp($data, $value) !== 0) { - $customfield->$key = new stdClass(); - $customfield->$key->fieldid = $info_field->id; - $customfield->$key->data = $value; - } - } + if (in_array($key, $authplugin->userfields)) { + $newuser->$key = $value; + } else { + $customfields = $authplugin->get_custom_user_profile_fields(); + if (!empty($customfields) && in_array($key, $customfields)) { + $shortname = str_replace('profile_field_', '', $key); + $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname)); + $data = $infofield->defaultdata; + if(strcmp($data, $value) !== 0) { + $customfield->$key = new stdClass(); + $customfield->$key->fieldid = $infofield->id; + $customfield->$key->data = $value; + } + } + } } } @@ -3890,12 +3894,12 @@ function create_user_record($username, $password, $auth = 'manual') { $newuser->mnethostid = $CFG->mnet_localhost_id; $newuser->id = $DB->insert_record('user', $newuser); - - foreach ($customfield as $key) { - $key->userid = $newuser->id; - $key->id = $DB->insert_record("user_info_data",$key); - } - + + foreach ($customfield as $key) { + $key->userid = $newuser->id; + $key->id = $DB->insert_record("user_info_data", $key); + } + $user = get_complete_user_data('id', $newuser->id); if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})){ set_user_preference('auth_forcepasswordchange', 1, $user); @@ -3928,9 +3932,11 @@ function update_user_record($username) { if ($newinfo = $userauth->get_userinfo($username)) { $newinfo = truncate_userinfo($newinfo); + $customfields = $userauth->get_custom_user_profile_fields(); + foreach ($newinfo as $key => $value){ $key = strtolower($key); - $iscustom = in_array($key, $userauth->custom_fields); + $iscustom = in_array($key, $customfields); if ((!property_exists($oldinfo, $key) && !$iscustom) or $key === 'username' or $key === 'id' or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') { // unknown or must not be changed @@ -3948,47 +3954,48 @@ function update_user_record($username) { // in a value for the selected field _if LDAP is giving // nothing_ for this field. Thus it makes sense to let this value // stand in until LDAP is giving a value for this field. - if (!(empty($value) && $lockval === 'unlockedifempty')) { - if(in_array($key, $userauth->userfields)) { - if ((string)$oldinfo->$key !== (string)$value) { - $newuser[$key] = (string)$value; + if (!empty($value) && ($lockval === 'unlockedifempty')) { + if (in_array($key, $userauth->userfields)) { + if ((string)$oldinfo->$key !== (string)$value) { + $newuser[$key] = (string)$value; + } + } else if ($iscustom) { + $shortname = str_replace('profile_field_', '', $key); + // If there is no value in the user_info_data then. + $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname)); + $userid = $DB->get_field('user', 'id', array('username' => $username)); + $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid, 'fieldid' => $infofield->id)); + if ($data === false) { + $data = $infofield->defaultdata; + if (strcmp($data, $value) !== 0) { + $row = new stdClass(); + $row->userid = $userid; + $row->fieldid = $infofield->id; + $row->data = $data; + $row->id = $DB->insert_record("user_info_data", $row); + } + } + + if (strcmp($data, $value) !== 0) { + $valid = true; + // Check to make sure that the value we are placing in is a valid one. + if (strcmp($info_field->datatype, 'menu') == 0) { + $validValues = explode("\n", $info_field->param1); + if (!in_array($value, $validValues)) { + $valid = false; + } + } else if(strcmp($info_field->datatype, 'checkbox') == 0) { + if ($value != 1 && $value != 0) { + $valid = false; + } + } + + // Update value if it is diffrent then old. + if ($valid) { + $DB->set_field('user_info_data', 'data', $value, array('userid'=>$userid, 'fieldid'=>$infofield->id)); + } + } } - }else if($iscustom) { - - //if there is no value in the user_info_data then - $info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); - $userid = $DB->get_field('user', 'id', array('username'=>$username)); - $data = $DB->get_field('user_info_data', 'data', array('userid'=>$userid, 'fieldid'=>$info_field->id)); - if($data === false) { - $data = $info_field->defaultdata; - if(strcmp($data, $value) !== 0) { - $row = new stdClass(); - $row->userid = $userid; - $row->fieldid = $info_field->id; - $row->data = $data; - $row->id = $DB->insert_record("user_info_data", $row); - } - } - - if(strcmp($data, $value) !== 0) { - $valid = true; - - //check to make sure that the value we are placing in is a valid one - if(strcmp($info_field->datatype, 'menu') == 0){ - $validValues = explode("\n", $info_field->param1); - if(!in_array($value, $validValues)) { - $valid = false; - } - } else if(strcmp($info_field->datatype, 'checkbox') == 0) { - if($value != 1 && $value != 0) { - $valid = false; - } - } - if($valid) { - $DB->set_field('user_info_data','data',$value, array('userid'=>$userid, 'fieldid'=>$info_field->id)); - } - } - } } } } From d836e3ed1f673f017b1df4fbadddd174d21a85ea Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 31 Jan 2013 13:12:17 +0800 Subject: [PATCH 3/4] MDL-16982 Administration: Integrated Inaki's suggestions --- auth/ldap/auth.php | 44 ++++++++++++++++++-------------------------- lib/authlib.php | 17 +++++++---------- lib/moodlelib.php | 37 ++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 53 deletions(-) diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 79a36240950..6b26a36a47a 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -129,12 +129,11 @@ class auth_plugin_ldap extends auth_plugin_base { // so leave $this->config->objectclass as is. } } - + /** * Constructor with initialisation. */ function auth_plugin_ldap() { - global $DB; $this->authtype = 'ldap'; $this->roleauth = 'auth_ldap'; $this->errorlogtag = '[AUTH LDAP] '; @@ -294,10 +293,7 @@ class auth_plugin_ldap extends auth_plugin_base { } else { $newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8'); } - - // Need to allow for 0 or '0' will ldap ever return an empty string - // or will the array_key_exists catch such things. - if (isset($newval) && ($newval !== '')) { // Favour ldap entries that are set. + if (!empty($newval)) { // favour ldap entries that are set $ldapval = $newval; } } @@ -1164,17 +1160,18 @@ class auth_plugin_ldap extends auth_plugin_base { $user_entry = array_change_key_case($user_entry[0], CASE_LOWER); foreach ($attrmap as $key => $ldapkeys) { + $profilefield = ''; // Only process if the moodle field ($key) has changed and we // are set to update LDAP with it if (isset($olduser->$key) and isset($newuser->$key) and ($olduser->$key !== $newuser->$key)) { - $profile_field = $key; + $profilefield = $key; } else if (isset($olduser->{'profile_field_' . $key}) && isset($newuser->{'profile_field_' . $key}) && $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}) { - $profile_field = 'profile_field_' . $key; + $profilefield = 'profile_field_' . $key; } - if (!empty($profile_field) && !empty($this->config->{'field_updateremote_' . $key})) { + if (!empty($profilefield) && !empty($this->config->{'field_updateremote_' . $key})) { // For ldap values that could be in more than one // ldap key, we will do our best to match // where they came from @@ -1187,14 +1184,13 @@ class auth_plugin_ldap extends auth_plugin_base { $ambiguous = false; } - $nuvalue = textlib::convert($newuser->$profile_field, 'utf-8', $this->config->ldapencoding); + $nuvalue = textlib::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding); empty($nuvalue) ? $nuvalue = array() : $nuvalue; - $ouvalue = textlib::convert($olduser->$profile_field, 'utf-8', $this->config->ldapencoding); + $ouvalue = textlib::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding); foreach ($ldapkeys as $ldapkey) { $ldapkey = $ldapkey; - $ldapvalue = empty($user_entry[$ldapkey][0]) ? null : $ldapvalue; - + $ldapvalue = $user_entry[$ldapkey][0]; if (!$ambiguous) { // Skip update if the values already match if ($nuvalue !== $ldapvalue) { @@ -1453,7 +1449,15 @@ class auth_plugin_ldap extends auth_plugin_base { function ldap_attributes () { $moodleattributes = array(); - foreach ($this->userfields as $field) { + // If we have custom fields then merge them with user fields. + $customfields = $this->get_custom_user_profile_fields(); + if (!empty($customfields) && !empty($this->userfields)) { + $userfields = array_merge($this->userfields, $customfields); + } else { + $userfields = $this->userfields; + } + + foreach ($userfields as $field) { if (!empty($this->config->{"field_map_$field"})) { $moodleattributes[$field] = textlib::strtolower(trim($this->config->{"field_map_$field"})); if (preg_match('/,/', $moodleattributes[$field])) { @@ -1461,18 +1465,6 @@ class auth_plugin_ldap extends auth_plugin_base { } } } - - // Add custom fields. - $customfields = $this->get_custom_user_profile_fields(); - foreach ($customfields as $field) { - if (!empty($this->config->{"field_map_$field"})) { - $moodleattributes[$field] = $this->config->{"field_map_$field"}; - if (preg_match('/,/',$moodleattributes[$field])) { - $moodleattributes[$field] = explode(',', $moodleattributes[$field]); - } - } - } - $moodleattributes['username'] = textlib::strtolower(trim($this->config->user_attribute)); return $moodleattributes; } diff --git a/lib/authlib.php b/lib/authlib.php index daf14a0f63e..fd8057e1d10 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -121,7 +121,7 @@ class auth_plugin_base { * Moodle custom fields to sync with. * @var array() */ - var $custom_fields = null; + var $customfields = null; /** @@ -535,23 +535,20 @@ class auth_plugin_base { */ public function get_custom_user_profile_fields() { global $DB; - // If already retrived then return. - if (!is_null($this->custom_fields)) { - return $this->custom_fields; + // If already retrieved then return. + if (!is_null($this->customfields)) { + return $this->customfields; } - $this->custom_fields = array(); - - $customfields = array(); + $this->customfields = array(); if ($proffields = $DB->get_records('user_info_field')) { foreach ($proffields as $proffield) { - $customfields[] = 'profile_field_'.$proffield->shortname; + $this->customfields[] = 'profile_field_'.$proffield->shortname; } } unset($proffields); - $this->custom_fields = $customfields; - return $customfields; + return $this->customfields; } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 0e9e6fb6402..dae404f5e03 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3897,7 +3897,7 @@ function create_user_record($username, $password, $auth = 'manual') { foreach ($customfield as $key) { $key->userid = $newuser->id; - $key->id = $DB->insert_record("user_info_data", $key); + $key->id = $DB->insert_record('user_info_data', $key); } $user = get_complete_user_data('id', $newuser->id); @@ -3961,24 +3961,18 @@ function update_user_record($username) { } } else if ($iscustom) { $shortname = str_replace('profile_field_', '', $key); - // If there is no value in the user_info_data then. $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname)); - $userid = $DB->get_field('user', 'id', array('username' => $username)); - $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid, 'fieldid' => $infofield->id)); + $data = $DB->get_field('user_info_data', 'data', array('userid' => $oldinfo->id, 'fieldid' => $infofield->id)); + // If there is no value in the user_info_data then use default value for comparison. if ($data === false) { - $data = $infofield->defaultdata; - if (strcmp($data, $value) !== 0) { - $row = new stdClass(); - $row->userid = $userid; - $row->fieldid = $infofield->id; - $row->data = $data; - $row->id = $DB->insert_record("user_info_data", $row); - } + $originalvalue = $infofield->defaultdata; + } else { + $originalvalue = $data; } - - if (strcmp($data, $value) !== 0) { + // If passed value is different then original value then update/insert. + if (strcmp($originalvalue, $value) !== 0) { $valid = true; - // Check to make sure that the value we are placing in is a valid one. + // Check to make sure that the value is a valid. if (strcmp($info_field->datatype, 'menu') == 0) { $validValues = explode("\n", $info_field->param1); if (!in_array($value, $validValues)) { @@ -3990,9 +3984,18 @@ function update_user_record($username) { } } - // Update value if it is diffrent then old. + // Insert/update if value is valid. if ($valid) { - $DB->set_field('user_info_data', 'data', $value, array('userid'=>$userid, 'fieldid'=>$infofield->id)); + if ($data === false) { + $row = new stdClass(); + $row->userid = $oldinfo->id; + $row->fieldid = $infofield->id; + $row->data = $value; + $row->id = $DB->insert_record('user_info_data', $row); + } else { + $DB->set_field('user_info_data', 'data', $value, + array('userid' => $oldinfo->id, 'fieldid' => $infofield->id)); + } } } } From d8372b54b9f97ac1549212f80d09228a71e84804 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 11 Jun 2013 10:58:52 +0800 Subject: [PATCH 4/4] MDL-16982 Administration: Moved bulk action outside loop and using profile api to save data --- admin/auth_config.php | 7 ++-- auth/ldap/auth.php | 7 ++-- lib/moodlelib.php | 79 ++++++++----------------------------------- 3 files changed, 23 insertions(+), 70 deletions(-) diff --git a/admin/auth_config.php b/admin/auth_config.php index f87870d5152..50bcfc97db8 100644 --- a/admin/auth_config.php +++ b/admin/auth_config.php @@ -117,6 +117,9 @@ function print_auth_lock_options($auth, $user_fields, $helptext, $retrieveopts, $user_fields = array_merge($user_fields, $customfields); } + if (!empty($customfields)) { + $customfieldname = $DB->get_records('user_info_field', null, '', 'shortname, name'); + } foreach ($user_fields as $field) { // Define some vars we'll work with. if (!isset($pluginconfig->{"field_map_$field"})) { @@ -143,7 +146,7 @@ function print_auth_lock_options($auth, $user_fields, $helptext, $retrieveopts, } elseif (!empty($customfields) && in_array($field, $customfields)) { // If custom field then pick name from database. $fieldshortname = str_replace('profile_field_', '', $fieldname); - $fieldname = $DB->get_field('user_info_field', 'name', array('shortname' => $fieldshortname)); + $fieldname = $customfieldname[$fieldshortname]->name; } else { $fieldname = get_string($fieldname); } @@ -180,4 +183,4 @@ function print_auth_lock_options($auth, $user_fields, $helptext, $retrieveopts, } echo ''; } -} \ No newline at end of file +} diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 6b26a36a47a..314e5bed9b8 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -1163,12 +1163,13 @@ class auth_plugin_ldap extends auth_plugin_base { $profilefield = ''; // Only process if the moodle field ($key) has changed and we // are set to update LDAP with it + $customprofilefield = 'profile_field_' . $key; if (isset($olduser->$key) and isset($newuser->$key) and ($olduser->$key !== $newuser->$key)) { $profilefield = $key; - } else if (isset($olduser->{'profile_field_' . $key}) && isset($newuser->{'profile_field_' . $key}) - && $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}) { - $profilefield = 'profile_field_' . $key; + } else if (isset($olduser->$customprofilefield) && isset($newuser->$customprofilefield) + && $olduser->$customprofilefield !== $newuser->$customprofilefield) { + $profilefield = $customprofilefield; } if (!empty($profilefield) && !empty($this->config->{'field_updateremote_' . $key})) { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index dae404f5e03..0f8d0375ce7 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3839,31 +3839,18 @@ function get_user_fieldnames() { */ function create_user_record($username, $password, $auth = 'manual') { global $CFG, $DB; - + require_once($CFG->dirroot."/user/profile/lib.php"); //just in case check text case $username = trim(textlib::strtolower($username)); $authplugin = get_auth_plugin($auth); - + $customfields = $authplugin->get_custom_user_profile_fields(); $newuser = new stdClass(); - $customfield = new stdClass(); if ($newinfo = $authplugin->get_userinfo($username)) { $newinfo = truncate_userinfo($newinfo); foreach ($newinfo as $key => $value){ - if (in_array($key, $authplugin->userfields)) { + if (in_array($key, $authplugin->userfields) || (in_array($key, $customfields))) { $newuser->$key = $value; - } else { - $customfields = $authplugin->get_custom_user_profile_fields(); - if (!empty($customfields) && in_array($key, $customfields)) { - $shortname = str_replace('profile_field_', '', $key); - $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname)); - $data = $infofield->defaultdata; - if(strcmp($data, $value) !== 0) { - $customfield->$key = new stdClass(); - $customfield->$key->fieldid = $infofield->id; - $customfield->$key->data = $value; - } - } } } } @@ -3895,10 +3882,8 @@ function create_user_record($username, $password, $auth = 'manual') { $newuser->id = $DB->insert_record('user', $newuser); - foreach ($customfield as $key) { - $key->userid = $newuser->id; - $key->id = $DB->insert_record('user_info_data', $key); - } + // Save user profile data. + profile_save_data($newuser); $user = get_complete_user_data('id', $newuser->id); if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})){ @@ -3923,7 +3908,7 @@ function create_user_record($username, $password, $auth = 'manual') { */ function update_user_record($username) { global $DB, $CFG; - + require_once($CFG->dirroot."/user/profile/lib.php"); $username = trim(textlib::strtolower($username)); /// just in case check text case $oldinfo = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST); @@ -3954,50 +3939,10 @@ function update_user_record($username) { // in a value for the selected field _if LDAP is giving // nothing_ for this field. Thus it makes sense to let this value // stand in until LDAP is giving a value for this field. - if (!empty($value) && ($lockval === 'unlockedifempty')) { - if (in_array($key, $userauth->userfields)) { - if ((string)$oldinfo->$key !== (string)$value) { - $newuser[$key] = (string)$value; - } - } else if ($iscustom) { - $shortname = str_replace('profile_field_', '', $key); - $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname)); - $data = $DB->get_field('user_info_data', 'data', array('userid' => $oldinfo->id, 'fieldid' => $infofield->id)); - // If there is no value in the user_info_data then use default value for comparison. - if ($data === false) { - $originalvalue = $infofield->defaultdata; - } else { - $originalvalue = $data; - } - // If passed value is different then original value then update/insert. - if (strcmp($originalvalue, $value) !== 0) { - $valid = true; - // Check to make sure that the value is a valid. - if (strcmp($info_field->datatype, 'menu') == 0) { - $validValues = explode("\n", $info_field->param1); - if (!in_array($value, $validValues)) { - $valid = false; - } - } else if(strcmp($info_field->datatype, 'checkbox') == 0) { - if ($value != 1 && $value != 0) { - $valid = false; - } - } - - // Insert/update if value is valid. - if ($valid) { - if ($data === false) { - $row = new stdClass(); - $row->userid = $oldinfo->id; - $row->fieldid = $infofield->id; - $row->data = $value; - $row->id = $DB->insert_record('user_info_data', $row); - } else { - $DB->set_field('user_info_data', 'data', $value, - array('userid' => $oldinfo->id, 'fieldid' => $infofield->id)); - } - } - } + if (!(empty($value) && $lockval === 'unlockedifempty')) { + if ($iscustom || (in_array($key, $userauth->userfields) && + ((string)$oldinfo->$key !== (string)$value))) { + $newuser[$key] = (string)$value; } } } @@ -4006,6 +3951,10 @@ function update_user_record($username) { $newuser['id'] = $oldinfo->id; $newuser['timemodified'] = time(); $DB->update_record('user', $newuser); + + // Save user profile data. + profile_save_data((object) $newuser); + // fetch full user record for the event, the complete user data contains too much info // and we want to be consistent with other places that trigger this event events_trigger('user_updated', $DB->get_record('user', array('id'=>$oldinfo->id)));