Skip to content

Commit

Permalink
MDL-31776 - lib: Alternate name fields
Browse files Browse the repository at this point in the history
Add support for additional name fields and flexible formating of
user names.
  • Loading branch information
abgreeve authored and Damyon Wiese committed Jul 9, 2013
1 parent 9d87149 commit a327f25
Show file tree
Hide file tree
Showing 37 changed files with 611 additions and 177 deletions.
6 changes: 1 addition & 5 deletions admin/settings/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@
1800 => new lang_string('numminutes', '', 30),
2700 => new lang_string('numminutes', '', 45),
3600 => new lang_string('numminutes', '', 60))));
$temp->add(new admin_setting_configselect('fullnamedisplay', new lang_string('fullnamedisplay', 'admin'), new lang_string('configfullnamedisplay', 'admin'),
'language', array('language' => new lang_string('language'),
'firstname lastname' => new lang_string('firstname').' + '.new lang_string('lastname'),
'lastname firstname' => new lang_string('lastname').' + '.new lang_string('firstname'),
'firstname' => new lang_string('firstname'))));

$temp->add(new admin_setting_configcheckbox('extendedusernamechars', new lang_string('extendedusernamechars', 'admin'), new lang_string('configextendedusernamechars', 'admin'), 0));
$temp->add(new admin_setting_configtext('sitepolicy', new lang_string('sitepolicy', 'admin'), new lang_string('sitepolicy_help', 'admin'), '', PARAM_RAW));
$temp->add(new admin_setting_configtext('sitepolicyguest', new lang_string('sitepolicyguest', 'admin'), new lang_string('sitepolicyguest_help', 'admin'), (isset($CFG->sitepolicy) ? $CFG->sitepolicy : ''), PARAM_RAW));
Expand Down
1 change: 1 addition & 0 deletions admin/settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'department' => new lang_string('department'),
'institution' => new lang_string('institution'),
)));
$temp->add(new admin_setting_configtext('fullnamedisplay', new lang_string('fullnamedisplay', 'admin'), new lang_string('configfullnamedisplay', 'admin'), 'language', PARAM_TEXT, 50));
$temp->add(new admin_setting_configtext('maxusersperpage', new lang_string('maxusersperpage','admin'), new lang_string('configmaxusersperpage','admin'), 100, PARAM_INT));
$temp->add(new admin_setting_configcheckbox('enablegravatar', new lang_string('enablegravatar', 'admin'), new lang_string('enablegravatar_help', 'admin'), 0));
$temp->add(new admin_setting_configtext('gravatardefaulturl', new lang_string('gravatardefaulturl', 'admin'), new lang_string('gravatardefaulturl_help', 'admin'), 'mm'));
Expand Down
4 changes: 3 additions & 1 deletion admin/user/user_bulk_cohortadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@
}

$countries = get_string_manager()->get_list_of_countries(true);
$namefields = get_all_user_name_fields(true);
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, firstname, lastname, username, email, country, lastaccess, city');
$user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, ' . $namefields . ', username,
email, country, lastaccess, city');
$user->fullname = fullname($user, true);
$user->country = @$countries[$user->country];
unset($user->firstname);
Expand Down
3 changes: 2 additions & 1 deletion admin/user/user_bulk_display.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

$countries = get_string_manager()->get_list_of_countries(true);

$namefields = get_all_user_name_fields(true);
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id), 'id, firstname, lastname, username, email, country, lastaccess, city');
$user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city');
$user->fullname = fullname($user, true);
$user->country = @$countries[$user->country];
unset($user->firstname);
Expand Down
4 changes: 2 additions & 2 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1194,12 +1194,12 @@ protected function define_structure() {

// Then, the fields potentially needing anonymization
$anonfields = array(
'username', 'idnumber', 'firstname', 'lastname',
'email', 'icq', 'skype',
'username', 'idnumber', 'email', 'icq', 'skype',
'yahoo', 'aim', 'msn', 'phone1',
'phone2', 'institution', 'department', 'address',
'city', 'country', 'lastip', 'picture',
'url', 'description', 'descriptionformat', 'imagealt', 'auth');
$anonfields = array_merge($anonfields, get_all_user_name_fields());

// Add anonymized fields to $userfields with custom final element
foreach ($anonfields as $field) {
Expand Down
4 changes: 2 additions & 2 deletions group/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

case 'ajax_getmembersingroup':
$roles = array();
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, u.firstname, u.lastname')) {
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
foreach($groupmemberroles as $roleid=>$roledata) {
$shortroledata = new stdClass();
$shortroledata->name = $roledata->name;
Expand Down Expand Up @@ -246,7 +246,7 @@

$atleastonemember = false;
if ($singlegroup) {
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, u.firstname, u.lastname')) {
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
foreach($groupmemberroles as $roleid=>$roledata) {
echo '<optgroup label="'.s($roledata->name).'">';
foreach($roledata->users as $member) {
Expand Down
6 changes: 5 additions & 1 deletion group/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@

list($sort, $sortparams) = users_order_by_sql('u');

$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
$allnames = get_all_user_name_fields(true, 'u');
$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
FROM {groups} g
LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
LEFT JOIN {groups_members} gm ON g.id = gm.groupid
Expand All @@ -106,6 +107,9 @@
$user->lastname = $row->lastname;
$user->username = $row->username;
$user->idnumber = $row->idnumber;
foreach (get_all_user_name_fields() as $addname) {
$user->$addname = $row->$addname;
}
if (!$row->groupingid) {
$row->groupingid = -1;
}
Expand Down
3 changes: 2 additions & 1 deletion lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
$string['configfrontpagecourselimit'] = 'Maximum number of courses';
$string['configfrontpagecourselimithelp'] = 'Maximum number of courses to be displayed on the site\'s front page in course listings.';
$string['configfrontpageloggedin'] = 'The items selected above will be displayed on the site\'s front page when a user is logged in.';
$string['configfullnamedisplay'] = 'This defines how names are shown when they are displayed in full. For most mono-lingual sites the most efficient setting is "First name + Surname", but you may choose to hide surnames altogether, or to leave it up to the current language pack to decide (some languages have different conventions).';
$string['configfullnamedisplay'] = 'This defines how names are shown when they are displayed in full. For most mono-lingual sites the most efficient setting is "firstname lastname", but you may choose to hide surnames altogether, or to leave it up to the current language pack to decide (some languages have different conventions). Placeholders that can be used are: firstname, lastname, firstnamephonetic, lastnamephonetic, middlename, and alternatename.';
$string['configgeoipfile'] = 'Location of GeoIP City binary data file. This file is not part of Moodle distribution and must be obtained separately from <a href="http://www.maxmind.com/">MaxMind</a>. You can either buy a commercial version or use the free version.<br />Simply download <a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz" >http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz</a> and extract it into "{$a}" directory on your server.';
$string['configgetremoteaddrconf'] = 'If your server is behind a reverse proxy, you can use this setting to specify which HTTP headers can be trusted to contain the remote IP address. The headers are read in order, using the first one that is available.';
$string['configgradebookroles'] = 'This setting allows you to control who appears on the gradebook. Users need to have at least one of these roles in a course to be shown in the gradebook for that course.';
Expand Down Expand Up @@ -548,6 +548,7 @@
$string['frontpageroles'] = 'Front page roles';
$string['frontpagesettings'] = 'Front page settings';
$string['fullnamedisplay'] = 'Full name format';
$string['fullnamedisplayprivate'] = 'Full name format - private';
$string['gdrecommended'] = 'GD extension is used for conversion of images, some features such as user profile images will not be available if missing.';
$string['gdrequired'] = 'The GD extension is now required by Moodle for image conversion.';
$string['generalsettings'] = 'General settings';
Expand Down
5 changes: 5 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
$string['addinganew'] = 'Adding a new {$a}';
$string['addinganewto'] = 'Adding a new {$a->what} to {$a->to}';
$string['addingdatatoexisting'] = 'Adding data to existing';
$string['additionalnames'] = 'Additional names';
$string['addlinkhere'] = 'Add link here';
$string['addnewcategory'] = 'Add new category';
$string['addnewcourse'] = 'Add a new course';
Expand Down Expand Up @@ -135,6 +136,7 @@
$string['allteachers'] = 'All teachers';
$string['alphanumerical'] = 'Can only contain alphanumeric characters, hyphen (-) or period (.)';
$string['alreadyconfirmed'] = 'Registration has already been confirmed';
$string['alternatename'] = 'Alternate name';
$string['always'] = 'Always';
$string['and'] = '{$a->one} and {$a->two}';
$string['answer'] = 'Answer';
Expand Down Expand Up @@ -680,6 +682,7 @@
$string['findmorecourses'] = 'Find more courses...';
$string['firstaccess'] = 'First access';
$string['firstname'] = 'First name';
$string['firstnamephonetic'] = 'First name - phonetic';
$string['firsttime'] = 'Is this your first time here?';
$string['folder'] = 'Folder';
$string['folderclosed'] = 'Closed folder';
Expand Down Expand Up @@ -910,6 +913,7 @@
$string['lastlogin'] = 'Last login';
$string['lastmodified'] = 'Last modified';
$string['lastname'] = 'Surname';
$string['lastnamephonetic'] = 'Surname - phonetic';
$string['lastyear'] = 'Last year';
$string['latestlanguagepack'] = 'Check for latest language pack on moodle.org';
$string['layouttable'] = 'Layout table';
Expand Down Expand Up @@ -1019,6 +1023,7 @@
$string['messageprovider:instantmessage_help'] = 'This section configures what happens to messages that are sent to you directly from other users on this site.';
$string['messageselect'] = 'Select this user as a message recipient';
$string['messageselectadd'] = 'Send a message';
$string['middlename'] = 'Middle name';
$string['migratinggrades'] = 'Migrating grades';
$string['min'] = 'min';
$string['mins'] = 'mins';
Expand Down
3 changes: 2 additions & 1 deletion lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3964,7 +3964,8 @@ function get_role_users($roleid, context $context, $parent = false, $fields = ''
global $DB;

if (empty($fields)) {
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
$allnames = get_all_user_name_fields(true, 'u');
$fields = 'u.id, u.confirmed, u.username, '. $allnames . ', ' .
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
'u.lang, u.timezone, u.lastaccess, u.mnethostid, r.name AS rolename, r.sortorder, '.
Expand Down
4 changes: 3 additions & 1 deletion lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,10 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash
$userfrom = new stdClass();
$userfrom->id = $admin->id;
$userfrom->email = !empty($CFG->badges_defaultissuercontact) ? $CFG->badges_defaultissuercontact : $admin->email;
foreach (get_all_user_name_fields() as $addname) {
$userfrom->$addname = !empty($CFG->badges_defaultissuername) ? '' : $admin->$addname;
}
$userfrom->firstname = !empty($CFG->badges_defaultissuername) ? $CFG->badges_defaultissuername : $admin->firstname;
$userfrom->lastname = !empty($CFG->badges_defaultissuername) ? '' : $admin->lastname;
$userfrom->maildisplay = true;

$issuedlink = html_writer::link(new moodle_url('/badges/badge.php', array('hash' => $issued)), $badge->name);
Expand Down
8 changes: 5 additions & 3 deletions lib/coursecatlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,10 @@ public static function preload_course_contacts(&$courses) {
list($sql2, $params2) = $DB->get_in_or_equal($managerroles, SQL_PARAMS_NAMED, 'rid');
list($sort, $sortparams) = users_order_by_sql('u');
$notdeleted = array('notdeleted'=>0);
$allnames = get_all_user_name_fields(true, 'u');
$sql = "SELECT ra.contextid, ra.id AS raid,
r.id AS roleid, r.name AS rolename, r.shortname AS roleshortname,
rn.name AS rolecoursealias, u.id, u.username, u.firstname, u.lastname
rn.name AS rolecoursealias, u.id, u.username, $allnames
FROM {role_assignments} ra
JOIN {user} u ON ra.userid = u.id
JOIN {role} r ON ra.roleid = r.id
Expand Down Expand Up @@ -2109,8 +2110,9 @@ public function get_course_contacts() {
$user = new stdClass();
$user->id = $ruser->id;
$user->username = $ruser->username;
$user->firstname = $ruser->firstname;
$user->lastname = $ruser->lastname;
foreach (get_all_user_name_fields() as $addname) {
$user->$addname = $ruser->$addname;
}
$role = new stdClass();
$role->id = $ruser->roleid;
$role->name = $ruser->rolename;
Expand Down
9 changes: 5 additions & 4 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,11 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp
array('id', 'username', 'email', 'firstname', 'lastname', 'city', 'country',
'lastaccess', 'confirmed', 'mnethostid'));
}
$namefields = get_all_user_name_fields(true);
$extrafields = "$extrafields, $namefields";

// warning: will return UNCONFIRMED USERS
return $DB->get_records_sql("SELECT id, username, email, firstname, lastname, city, country,
lastaccess, confirmed, mnethostid, suspended $extrafields
return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields
FROM {user}
WHERE $select
$sort", $params, $page, $recordsperpage);
Expand Down Expand Up @@ -1781,8 +1782,8 @@ function get_logs($select, array $params=null, $order='l.time DESC', $limitfrom=
$select";

$totalcount = $DB->count_records_sql($sql, $params);

$sql = "SELECT l.*, u.firstname, u.lastname, u.picture
$allnames = get_all_user_name_fields(true, 'u');
$sql = "SELECT l.*, $allnames, u.picture
FROM {log} l
LEFT JOIN {user} u ON l.userid = u.id
$select
Expand Down
8 changes: 8 additions & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="trustbitmask" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="imagealt" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="alt tag for user uploaded image"/>
<FIELD NAME="lastnamephonetic" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Last name phonetic"/>
<FIELD NAME="firstnamephonetic" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="First name phonetic"/>
<FIELD NAME="middlename" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Middle name"/>
<FIELD NAME="alternatename" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Alternate name - Useful for three-name countries."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand All @@ -809,6 +813,10 @@
<INDEX NAME="email" UNIQUE="false" FIELDS="email"/>
<INDEX NAME="auth" UNIQUE="false" FIELDS="auth"/>
<INDEX NAME="idnumber" UNIQUE="false" FIELDS="idnumber"/>
<INDEX NAME="firstnamephonetic" UNIQUE="false" FIELDS="firstnamephonetic"/>
<INDEX NAME="lastnamephonetic" UNIQUE="false" FIELDS="lastnamephonetic"/>
<INDEX NAME="middlename" UNIQUE="false" FIELDS="middlename"/>
<INDEX NAME="alternatename" UNIQUE="false" FIELDS="alternatename"/>
</INDEXES>
</TABLE>
<TABLE NAME="user_preferences" COMMENT="Allows modules to store arbitrary user preferences">
Expand Down
50 changes: 50 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2209,5 +2209,55 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2013070800.00);
}

if ($oldversion < 2013070800.01) {

// Define field lastnamephonetic to be added to user.
$table = new xmldb_table('user');
$field = new xmldb_field('lastnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imagealt');
$index = new xmldb_index('lastnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('lastnamephonetic'));

// Conditionally launch add field lastnamephonetic.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
$dbman->add_index($table, $index);
}

// Define field firstnamephonetic to be added to user.
$table = new xmldb_table('user');
$field = new xmldb_field('firstnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'lastnamephonetic');
$index = new xmldb_index('firstnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('firstnamephonetic'));

// Conditionally launch add field firstnamephonetic.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
$dbman->add_index($table, $index);
}

// Define field alternatename to be added to user.
$table = new xmldb_table('user');
$field = new xmldb_field('middlename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'firstnamephonetic');
$index = new xmldb_index('middlename', XMLDB_INDEX_NOTUNIQUE, array('middlename'));

// Conditionally launch add field firstnamephonetic.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
$dbman->add_index($table, $index);
}

// Define field alternatename to be added to user.
$table = new xmldb_table('user');
$field = new xmldb_field('alternatename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'middlename');
$index = new xmldb_index('alternatename', XMLDB_INDEX_NOTUNIQUE, array('alternatename'));

// Conditionally launch add field alternatename.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
$dbman->add_index($table, $index);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2013070800.01);
}

return true;
}
Loading

0 comments on commit a327f25

Please sign in to comment.