Commit 4339ecf6 authored by Richard Mansfield's avatar Richard Mansfield

Add setting to make users searchable by real names

Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 75bdefd7
......@@ -164,6 +164,14 @@ if (get_config('showtagssideblock')) {
'rules' => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000),
);
}
if (get_config('userscanhiderealnames')) {
$elements['hiderealname'] = array(
'type' => 'checkbox',
'title' => get_string('hiderealname', 'account'),
'description' => get_string('hiderealnamedescription', 'account'),
'defaultvalue' => $prefs->hiderealname,
);
}
$elements['submit'] = array(
'type' => 'submit',
'value' => get_string('save')
......
......@@ -257,6 +257,12 @@ $siteoptionform = array(
'defaultvalue' => get_config('remoteavatars'),
'help' => true,
),
'userscanhiderealnames' => array(
'type' => 'checkbox',
'title' => get_string('userscanhiderealnames', 'admin'),
'description' => get_string('userscanhiderealnamesdescription', 'admin'),
'defaultvalue' => get_config('userscanhiderealnames'),
),
)
);
......@@ -282,7 +288,7 @@ function siteoptions_submit(Pieform $form, $values) {
'registration_sendweeklyupdates', 'institutionexpirynotification', 'institutionautosuspend',
'captchaonregisterform', 'captchaoncontactform', 'showselfsearchsideblock', 'showtagssideblock',
'tagssideblockmaxtags', 'country', 'viewmicroheaders', 'userscanchooseviewthemes',
'remoteavatars',
'remoteavatars', 'userscanhiderealnames'
);
$oldlanguage = get_config('lang');
$oldtheme = get_config('theme');
......
......@@ -60,6 +60,9 @@ $string['showviewcolumns'] = 'Show controls to add and remove columns when editi
$string['tagssideblockmaxtags'] = 'Maximum tags in cloud';
$string['tagssideblockmaxtagsdescription'] = 'Maximum number of tags to display in your Tag Cloud';
$string['hiderealname'] = 'Hide real name';
$string['hiderealnamedescription'] = 'Check this box if you have set a display name and you do not want other users to be able to find you by your real name in user searches.';
$string['prefssaved'] = 'Preferences saved';
$string['prefsnotsaved'] = 'Failed to save your Preferences!';
......
......@@ -198,6 +198,8 @@ $string['usersallowedmultipleinstitutions'] = 'Users allowed multiple institutio
$string['usersallowedmultipleinstitutionsdescription'] = 'If checked, users can be members of several institutions at the same time';
$string['userscanchooseviewthemes'] = 'Users can choose View themes';
$string['userscanchooseviewthemesdescription'] = 'If enabled, users will be allowed to select a theme when editing a View. The View will be displayed to other users using the selected theme.';
$string['userscanhiderealnames'] = 'Users can hide real names';
$string['userscanhiderealnamesdescription'] = 'If checked, users who have set a display name may choose to be searchable only by their display name, and will not be found in searches for their real name. (In the site administration section of the site, users are always searchable by their real names).';
$string['usersseenewthemeonlogin'] = 'Other users will see the new theme the next time they log in.';
$string['viruschecking'] = 'Virus checking';
$string['viruscheckingdescription'] = 'If checked, virus checking will be enabled for all uploaded files using ClamAV';
......
......@@ -1358,5 +1358,17 @@ function xmldb_core_upgrade($oldversion=0) {
set_remoteavatars_default();
}
if ($oldversion < 2010031000) {
// For existing sites, preserve current user search behaviour:
// Users are only searchable by their display names.
set_config('userscanhiderealnames', 1);
execute_sql("
INSERT INTO {usr_account_preference} (usr, field, value)
SELECT u.id, 'hiderealname', 1
FROM {usr} u LEFT JOIN {usr_account_preference} p ON (u.id = p.usr AND p.field = 'hiderealname')
WHERE NOT u.preferredname IS NULL AND u.preferredname != '' AND p.field IS NULL
");
}
return $status;
}
......@@ -203,6 +203,7 @@ function expected_account_preferences() {
'addremovecolumns' => 0,
'maildisabled' => 0,
'tagssideblockmaxtags' => get_config('tagssideblockmaxtags'),
'hiderealname' => 0,
);
}
......@@ -629,10 +630,11 @@ function process_email($address) {
* @param object $userto the user that is looking at the string representation (if left
* blank, will default to the currently logged in user).
* @param boolean $nameonly do not append the user's username even if $userto can see it.
* @param boolean $realname show the user's real name even if preferredname exists
*
* @returns string name to display
*/
function display_name($user, $userto=null, $nameonly=false) {
function display_name($user, $userto=null, $nameonly=false, $realname=false) {
global $USER;
static $resultcache = array();
static $usercache = array();
......@@ -710,25 +712,25 @@ function display_name($user, $userto=null, $nameonly=false) {
$user->id = (isset($user->id)) ? $user->id : null;
$userto->id = (isset($userto->id)) ? $userto->id : null;
if (isset($resultcache[$user->id][$userto->id][$nameonly])) {
return $resultcache[$user->id][$userto->id][$nameonly];
if (isset($resultcache[$user->id][$userto->id][$nameonly][$realname])) {
return $resultcache[$user->id][$userto->id][$nameonly][$realname];
}
// if they don't have a preferred name set, just return here
$firstlast = (isset($user->deleted) && $user->deleted) ? get_string('deleteduser') : ($user->firstname . ' ' . $user->lastname);
if (empty($user->preferredname)) {
if ((!empty($userto->admin) || !empty($userto->staff)) && !$nameonly) {
return ($resultcache[$user->id][$userto->id][$nameonly] = $firstlast . ' (' . $user->username . ')');
return ($resultcache[$user->id][$userto->id][$nameonly][$realname] = $firstlast . ' (' . $user->username . ')');
}
return ($resultcache[$user->id][$userto->id][$nameonly] = $firstlast);
return ($resultcache[$user->id][$userto->id][$nameonly][$realname] = $firstlast);
}
else if ($user->id == $userto->id) {
// If viewing our own name, show it how we like it
return ($resultcache[$user->id][$userto->id][$nameonly] = $user->preferredname);
return ($resultcache[$user->id][$userto->id][$nameonly][$realname] = $user->preferredname);
}
if ((!empty($userto->admin) || !empty($userto->staff)) && !$nameonly) {
return ($resultcache[$user->id][$userto->id][$nameonly]
return ($resultcache[$user->id][$userto->id][$nameonly][$realname]
= $user->preferredname . ' (' . $firstlast . ' - ' . $user->username . ')');
}
......@@ -738,11 +740,11 @@ function display_name($user, $userto=null, $nameonly=false) {
ON g1.group = g2.group
JOIN {group} g ON (g.id = g1.group AND g.deleted = 0)
WHERE g1.member = ? AND g2.member = ? AND g2.role = 'tutor'";
if (record_exists_sql($sql, array($user->id, $userto->id))) {
return ($resultcache[$user->id][$userto->id][$nameonly]
if ($realname || record_exists_sql($sql, array($user->id, $userto->id))) {
return ($resultcache[$user->id][$userto->id][$nameonly][$realname]
= $user->preferredname . ($nameonly ? '' : ' (' . $firstlast . ')'));
}
return ($resultcache[$user->id][$userto->id][$nameonly] = $user->preferredname);
return ($resultcache[$user->id][$userto->id][$nameonly][$realname] = $user->preferredname);
}
/**
......@@ -1235,7 +1237,7 @@ function get_new_username($desired) {
*/
function get_users_data($userlist, $getviews=true) {
global $USER;
$sql = 'SELECT u.id, 0 AS pending,
$sql = 'SELECT u.id, 0 AS pending, ap.value AS hidenamepref,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'friendscontrol\'), \'auth\') AS friendscontrol,
(SELECT 1 FROM {usr_friend} WHERE ((usr1 = ? AND usr2 = u.id) OR (usr2 = ? AND usr1 = u.id))) AS friend,
......@@ -1243,9 +1245,10 @@ function get_users_data($userlist, $getviews=true) {
(SELECT title FROM {artefact} WHERE artefacttype = \'introduction\' AND owner = u.id) AS introduction,
NULL AS message
FROM {usr} u
LEFT JOIN {usr_account_preference} ap ON (u.id = ap.usr AND ap.field = \'hiderealname\')
WHERE u.id IN (' . $userlist . ')
UNION
SELECT u.id, 1 AS pending,
SELECT u.id, 1 AS pending, ap.value AS hidenamepref,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages,
NULL AS friendscontrol,
NULL AS friend,
......@@ -1253,11 +1256,13 @@ function get_users_data($userlist, $getviews=true) {
(SELECT title FROM {artefact} WHERE artefacttype = \'introduction\' AND owner = u.id) AS introduction,
message
FROM {usr} u
LEFT JOIN {usr_account_preference} ap ON (u.id = ap.usr AND ap.field = \'hiderealname\')
JOIN {usr_friend_request} fr ON fr.requester = u.id
WHERE fr.owner = ?
AND u.id IN (' . $userlist . ')';
$userid = $USER->get('id');
$data = get_records_sql_assoc($sql, array($userid, $userid, $userid, $userid));
$allowhidename = get_config('userscanhiderealnames');
foreach ($data as &$record) {
if (isset($record->introduction)) {
......@@ -1266,6 +1271,7 @@ function get_users_data($userlist, $getviews=true) {
$record->messages = ($record->messages == 'allow' || $record->friend && $record->messages == 'friends' || $USER->get('admin')) ? 1 : 0;
$record->institutions = get_institution_string_for_user($record->id);
$record->display_name = display_name($record->id, null, false, !$allowhidename || !$record->hidenamepref);
}
if (!$data || !$getviews || !$views = get_views(array_keys($data), null, null)) {
......
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2010021600;
$config->version = 2010031000;
$config->release = '1.3.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
......@@ -102,9 +102,12 @@ class PluginSearchInternal extends PluginSearch {
(gm.member = u.id AND gm.group = ' . (int)$data['group'] . $groupadminsql . ")\n";
$sql .= $groupjoin;
}
$sql .= "
LEFT OUTER JOIN {usr_account_preference} h ON (u.id = h.usr AND h.field = 'hiderealname')";
$querydata = split(' ', preg_replace('/\s\s+/', ' ', strtolower(trim($query_string))));
$hidenameallowed = get_config('userscanhiderealnames') ? 'TRUE' : 'FALSE';
$namesql = "(u.preferredname $ilike '%' || ? || '%')
OR ((u.preferredname IS NULL OR u.preferredname = '')
OR ((u.preferredname IS NULL OR u.preferredname = '' OR NOT $hidenameallowed OR h.value <> 1)
AND (u.firstname $ilike '%' || ? || '%' OR u.lastname $ilike '%' || ? || '%'))
OR (a.artefacttype IN $fieldlist
AND ( a.title $ilike '%' || ? || '%'))";
......@@ -164,6 +167,8 @@ class PluginSearchInternal extends PluginSearch {
if (isset($data['group'])) {
$sql .= $groupjoin;
}
$sql .= "
LEFT OUTER JOIN {usr_account_preference} h ON (u.id = h.usr AND h.field = 'hiderealname')";
$sql .= $where . '
ORDER BY ' . $data['orderby'];
......
......@@ -22,7 +22,7 @@
<div class="rightdiv">
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
{$user->display_name|escape}
</a>
- {str tag='pending' section='group'}
</h3>
......@@ -60,7 +60,7 @@
<div class="rightdiv">
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
{$user->display_name|escape}
</a>
{if $page == 'find'}
- {str tag='existingfriend' section='group'}
......@@ -110,7 +110,7 @@
<div class="rightdiv">
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
{$user->display_name|escape}
</a>
</h3>
{if $user->introduction}
......@@ -147,7 +147,7 @@
<div class="rightdiv">
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
{$user->display_name|escape}
</a>
</h3>
{if $user->introduction}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment