MDL-16982 Administration: Cleaned whitespaces and alignment in orignal patch

This commit is contained in:
Rajesh Taneja 2012-11-29 14:32:53 +08:00
parent b88adb55fc
commit 57d135a1c6
7 changed files with 136 additions and 192 deletions

View File

@ -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 "<hr />\n";
$authplugin->config_form($frm, $err, $user_fields, $custom_fields);
$authplugin->config_form($frm, $err, $user_fields);
echo $OUTPUT->box_end();
echo '<p style="text-align: center"><input type="submit" value="' . get_string("savechanges") . "\" /></p>\n";
echo "</div>\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 '<tr><td colspan="3">';
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 = '&nbsp;';
$helptext = '&nbsp;';
}
// 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 '<label for="menulockconfig_field_updateremote_'.$field.'">'.get_string('auth_updateremote', 'auth') . '</label>&nbsp;';
echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false);
echo '<br />';
}
echo '<label for="menulockconfig_field_lock_'.$field.'">'.get_string('auth_fieldlock', 'auth') . '</label>&nbsp;';
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 '</tr>';
}
if(!empty($custom_fields)) {
echo '<tr><td colspan="3">';
echo '<h4>' . get_string('profilefields', 'admin') . '</h4>';
echo '</td></tr>';
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 '<tr valign="top"><td align="right">';
echo '<label for="lockconfig_'.$varname.'">'.$fieldname.'</label>';
echo '</td><td>';
echo "<input id=\"lockconfig_{$varname}\" name=\"lockconfig_{$varname}\" type=\"text\" size=\"30\" value=\"{$pluginconfig->$varname}\" />";
echo '<div style="text-align: right">';
echo '<label for="menulockconfig_field_updatelocal_'.$field.'">'.get_string('auth_updatelocal', 'auth') . '</label>&nbsp;';
echo html_writer::select($updatelocaloptions, "lockconfig_field_updatelocal_{$field}", $pluginconfig->{"field_updatelocal_$field"}, false);
echo '<br />';
if ($updateopts) {
echo '<label for="menulockconfig_field_updateremote_'.$field.'">'.get_string('auth_updateremote', 'auth') . '</label>&nbsp;';
echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false);
echo '<br />';
}
echo '<label for="menulockconfig_field_lock_'.$field.'">'.get_string('auth_fieldlock', 'auth') . '</label>&nbsp;';
echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false);
echo '</div>';
} else {
echo '<tr valign="top"><td align="right">';
echo '<label for="menulockconfig_field_lock_'.$field.'">'.$fieldname.'</label>';
echo '</td><td>';
echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false);
}
echo '</td>';
echo '</tr>';
}
}
}
}

View File

@ -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?

View File

@ -491,6 +491,6 @@ $help .= get_string('auth_updateremote_expl', 'auth');
$help .= '<hr />';
$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());
?>
</table>

View File

@ -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?

View File

@ -604,6 +604,6 @@ $help .= get_string('auth_updateremote_expl', 'auth');
$help .= '<hr />';
$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());
?>
</table>

View File

@ -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;
}
}
/**

View File

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