MDL-16982 Administration: Moved bulk action outside loop and using profile api to save data

This commit is contained in:
Rajesh Taneja 2013-06-11 10:58:52 +08:00
parent d836e3ed1f
commit d8372b54b9
3 changed files with 23 additions and 70 deletions

View File

@ -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);
}

View File

@ -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})) {

View File

@ -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,51 +3939,11 @@ 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) {
if (!(empty($value) && $lockval === 'unlockedifempty')) {
if ($iscustom || (in_array($key, $userauth->userfields) &&
((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));
}
}
}
}
}
}
}
@ -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)));