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; $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"); //$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) /// Get the auth title (from core or own auth lang files)
@ -73,7 +72,7 @@ echo $OUTPUT->box_start('informationbox');
echo $authdescription; echo $authdescription;
echo $OUTPUT->box_end(); echo $OUTPUT->box_end();
echo "<hr />\n"; echo "<hr />\n";
$authplugin->config_form($frm, $err, $user_fields, $custom_fields); $authplugin->config_form($frm, $err, $user_fields);
echo $OUTPUT->box_end(); echo $OUTPUT->box_end();
echo '<p style="text-align: center"><input type="submit" value="' . get_string("savechanges") . "\" /></p>\n"; echo '<p style="text-align: center"><input type="submit" value="' . get_string("savechanges") . "\" /></p>\n";
echo "</div>\n"; echo "</div>\n";
@ -88,7 +87,7 @@ exit;
// but some may want a custom one if they are offering // but some may want a custom one if they are offering
// other options // other options
// Note: lockconfig_ fields have special handling. // 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; global $DB, $OUTPUT;
echo '<tr><td colspan="3">'; echo '<tr><td colspan="3">';
if ($retrieveopts) { if ($retrieveopts) {
@ -108,14 +107,18 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
$pluginconfig = get_config("auth/$auth"); $pluginconfig = get_config("auth/$auth");
// helptext is on a field with rowspan // Helptext is on a field with rowspan.
if (empty($helptext)) { if (empty($helptext)) {
$helptext = '&nbsp;'; $helptext = '&nbsp;';
} }
foreach ($user_fields as $field) { // If we have custom fields then merge them with user fields.
if (!empty($customfields)) {
$user_fields = array_merge($user_fields, $customfields);
}
// Define some vars we'll work with foreach ($user_fields as $field) {
// Define some vars we'll work with.
if (!isset($pluginconfig->{"field_map_$field"})) { if (!isset($pluginconfig->{"field_map_$field"})) {
$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"} = ''; $pluginconfig->{"field_lock_$field"} = '';
} }
// define the fieldname we display to the user // Define the fieldname we display to the user.
$fieldname = $field; $fieldname = $field;
if ($fieldname === 'lang') { if ($fieldname === 'lang') {
$fieldname = get_string('language'); $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]; $fieldname = get_string($matches[1]) . ' ' . $matches[2];
} elseif ($fieldname == 'url') { } elseif ($fieldname == 'url') {
$fieldname = get_string('webpage'); $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 { } else {
$fieldname = get_string($fieldname); $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 '<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 html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false);
echo '<br />'; echo '<br />';
} }
echo '<label for="menulockconfig_field_lock_'.$field.'">'.get_string('auth_fieldlock', 'auth') . '</label>&nbsp;'; 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 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>'; 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 { class auth_plugin_cas extends auth_plugin_ldap {
/**
* moodle custom fields to sync with
* @var array()
*/
var $custom_fields = array();
/*
/** /**
* Constructor. * Constructor.
*/ */
function auth_plugin_cas() { function auth_plugin_cas() {
global $DB;
$this->authtype = 'cas'; $this->authtype = 'cas';
$this->roleauth = 'auth_cas'; $this->roleauth = 'auth_cas';
$this->errorlogtag = '[AUTH CAS] '; $this->errorlogtag = '[AUTH CAS] ';
$this->init_plugin($this->authtype); $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() { 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. * @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; global $CFG, $OUTPUT;
if (!function_exists('ldap_connect')) { // Is php-ldap really there? 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 .= '<hr />';
$help .= get_string('auth_updateremote_ldap', 'auth'); $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> </table>

View File

@ -130,14 +130,6 @@ class auth_plugin_ldap extends auth_plugin_base {
} }
} }
/**
* moodle custom fields to sync with
* @var array()
*/
var $custom_fields = array();
/*
/** /**
* Constructor with initialisation. * Constructor with initialisation.
*/ */
@ -147,10 +139,6 @@ class auth_plugin_ldap extends auth_plugin_base {
$this->roleauth = 'auth_ldap'; $this->roleauth = 'auth_ldap';
$this->errorlogtag = '[AUTH LDAP] '; $this->errorlogtag = '[AUTH LDAP] ';
$this->init_plugin($this->authtype); $this->init_plugin($this->authtype);
$custom_fields = $DB->get_records('user_info_field');
foreach($custom_fields as $cf) {
$this->custom_fields[] = $cf->shortname;
}
} }
/** /**
@ -307,8 +295,9 @@ class auth_plugin_ldap extends auth_plugin_base {
$newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8'); $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? // Need to allow for 0 or '0' will ldap ever return an empty string
if (isset($newval) || $newval !== '') { // favour ldap entries that are set // or will the array_key_exists catch such things.
if (isset($newval) && ($newval !== '')) { // Favour ldap entries that are set.
$ldapval = $newval; $ldapval = $newval;
} }
} }
@ -1177,23 +1166,15 @@ class auth_plugin_ldap extends auth_plugin_base {
foreach ($attrmap as $key => $ldapkeys) { foreach ($attrmap as $key => $ldapkeys) {
// Only process if the moodle field ($key) has changed and we // Only process if the moodle field ($key) has changed and we
// are set to update LDAP with it // 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) if (isset($olduser->$key) and isset($newuser->$key)
and $olduser->$key !== $newuser->$key) { and ($olduser->$key !== $newuser->$key)) {
//updating a user profile field
$update_attr_type = 2;
$profile_field = $key; $profile_field = $key;
} else if(isset($olduser->{'profile_field_' . $key}) and isset($newuser->{'profile_field_' . $key}) } else if (isset($olduser->{'profile_field_' . $key}) && isset($newuser->{'profile_field_' . $key})
and $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}){ && $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}) {
//updating a custom profile field
$update_attr_type = 1;
$profile_field = '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 // For ldap values that could be in more than one
// ldap key, we will do our best to match // ldap key, we will do our best to match
// where they came from // where they came from
@ -1212,7 +1193,7 @@ class auth_plugin_ldap extends auth_plugin_base {
foreach ($ldapkeys as $ldapkey) { foreach ($ldapkeys as $ldapkey) {
$ldapkey = $ldapkey; $ldapkey = $ldapkey;
$ldapvalue = empty($user_entry[$ldapkey][0])? null: $ldapvalue; $ldapvalue = empty($user_entry[$ldapkey][0]) ? null : $ldapvalue;
if (!$ambiguous) { if (!$ambiguous) {
// Skip update if the values already match // Skip update if the values already match
@ -1481,14 +1462,13 @@ class auth_plugin_ldap extends auth_plugin_base {
} }
} }
//add custom fields // Add custom fields.
foreach($this->custom_fields as $field) { $customfields = $this->get_custom_user_profile_fields();
if(!in_array($field, $this->userfields)) { //just in case so we don't overwrite any values in the user fields foreach ($customfields as $field) {
if(!empty($this->config->{"field_map_$field"})) { if (!empty($this->config->{"field_map_$field"})) {
$moodleattributes[$field] = $this->config->{"field_map_$field"}; $moodleattributes[$field] = $this->config->{"field_map_$field"};
if (preg_match('/,/',$moodleattributes[$field])) { if (preg_match('/,/',$moodleattributes[$field])) {
$moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ? $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. * @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; global $CFG, $OUTPUT;
if (!function_exists('ldap_connect')) { // Is php-ldap really there? 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 .= '<hr />';
$help .= get_string('auth_updateremote_ldap', 'auth'); $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> </table>

View File

@ -117,6 +117,12 @@ class auth_plugin_base {
'address' '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() * This is the primary method that is used by the authenticate_user_login()
@ -522,6 +528,32 @@ class auth_plugin_base {
return array(); 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

@ -3850,19 +3850,23 @@ function create_user_record($username, $password, $auth = 'manual') {
if ($newinfo = $authplugin->get_userinfo($username)) { if ($newinfo = $authplugin->get_userinfo($username)) {
$newinfo = truncate_userinfo($newinfo); $newinfo = truncate_userinfo($newinfo);
foreach ($newinfo as $key => $value){ foreach ($newinfo as $key => $value){
if(in_array($key, $authplugin->userfields)) { if (in_array($key, $authplugin->userfields)) {
$newuser->$key = $value; $newuser->$key = $value;
}else if(isset($authplugin->custom_fields) && in_array($key, $authplugin->custom_fields)) { } else {
$info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); $customfields = $authplugin->get_custom_user_profile_fields();
$data = $info_field->defaultdata; 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) { if(strcmp($data, $value) !== 0) {
$customfield->$key = new stdClass(); $customfield->$key = new stdClass();
$customfield->$key->fieldid = $info_field->id; $customfield->$key->fieldid = $infofield->id;
$customfield->$key->data = $value; $customfield->$key->data = $value;
} }
} }
} }
} }
}
if (!empty($newuser->email)) { if (!empty($newuser->email)) {
if (email_is_not_allowed($newuser->email)) { if (email_is_not_allowed($newuser->email)) {
@ -3893,7 +3897,7 @@ function create_user_record($username, $password, $auth = 'manual') {
foreach ($customfield as $key) { foreach ($customfield as $key) {
$key->userid = $newuser->id; $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); $user = get_complete_user_data('id', $newuser->id);
@ -3928,9 +3932,11 @@ function update_user_record($username) {
if ($newinfo = $userauth->get_userinfo($username)) { if ($newinfo = $userauth->get_userinfo($username)) {
$newinfo = truncate_userinfo($newinfo); $newinfo = truncate_userinfo($newinfo);
$customfields = $userauth->get_custom_user_profile_fields();
foreach ($newinfo as $key => $value){ foreach ($newinfo as $key => $value){
$key = strtolower($key); $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' if ((!property_exists($oldinfo, $key) && !$iscustom) or $key === 'username' or $key === 'id'
or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') { or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') {
// unknown or must not be changed // unknown or must not be changed
@ -3948,44 +3954,45 @@ function update_user_record($username) {
// in a value for the selected field _if LDAP is giving // in a value for the selected field _if LDAP is giving
// nothing_ for this field. Thus it makes sense to let this value // nothing_ for this field. Thus it makes sense to let this value
// stand in until LDAP is giving a value for this field. // stand in until LDAP is giving a value for this field.
if (!(empty($value) && $lockval === 'unlockedifempty')) { if (!empty($value) && ($lockval === 'unlockedifempty')) {
if(in_array($key, $userauth->userfields)) { if (in_array($key, $userauth->userfields)) {
if ((string)$oldinfo->$key !== (string)$value) { if ((string)$oldinfo->$key !== (string)$value) {
$newuser[$key] = (string)$value; $newuser[$key] = (string)$value;
} }
}else if($iscustom) { } else if ($iscustom) {
$shortname = str_replace('profile_field_', '', $key);
//if there is no value in the user_info_data then // If there is no value in the user_info_data then.
$info_field = $DB->get_record('user_info_field',array('shortname'=>$key)); $infofield = $DB->get_record('user_info_field', array('shortname' => $shortname));
$userid = $DB->get_field('user', 'id', array('username'=>$username)); $userid = $DB->get_field('user', 'id', array('username' => $username));
$data = $DB->get_field('user_info_data', 'data', array('userid'=>$userid, 'fieldid'=>$info_field->id)); $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid, 'fieldid' => $infofield->id));
if($data === false) { if ($data === false) {
$data = $info_field->defaultdata; $data = $infofield->defaultdata;
if(strcmp($data, $value) !== 0) { if (strcmp($data, $value) !== 0) {
$row = new stdClass(); $row = new stdClass();
$row->userid = $userid; $row->userid = $userid;
$row->fieldid = $info_field->id; $row->fieldid = $infofield->id;
$row->data = $data; $row->data = $data;
$row->id = $DB->insert_record("user_info_data", $row); $row->id = $DB->insert_record("user_info_data", $row);
} }
} }
if(strcmp($data, $value) !== 0) { if (strcmp($data, $value) !== 0) {
$valid = true; $valid = true;
// Check to make sure that the value we are placing in is a valid one.
//check to make sure that the value we are placing in is a valid one if (strcmp($info_field->datatype, 'menu') == 0) {
if(strcmp($info_field->datatype, 'menu') == 0){
$validValues = explode("\n", $info_field->param1); $validValues = explode("\n", $info_field->param1);
if(!in_array($value, $validValues)) { if (!in_array($value, $validValues)) {
$valid = false; $valid = false;
} }
} else if(strcmp($info_field->datatype, 'checkbox') == 0) { } else if(strcmp($info_field->datatype, 'checkbox') == 0) {
if($value != 1 && $value != 0) { if ($value != 1 && $value != 0) {
$valid = false; $valid = false;
} }
} }
if($valid) {
$DB->set_field('user_info_data','data',$value, array('userid'=>$userid, 'fieldid'=>$info_field->id)); // Update value if it is diffrent then old.
if ($valid) {
$DB->set_field('user_info_data', 'data', $value, array('userid'=>$userid, 'fieldid'=>$infofield->id));
} }
} }
} }