Commit 7dc47834 authored by Clare Lenihan's avatar Clare Lenihan Committed by Clare Lenihan
Browse files

find and myfriends now use functions where possible

parent 647c8549
......@@ -199,6 +199,8 @@ $string['existingfriend'] = 'existing friend';
$string['nosearchresultsfound'] = 'No search results found :(';
$string['friend'] = 'friend';
$string['friends'] = 'friends';
$string['user'] = 'user';
$string['users'] = 'users';
$string['friendlistfailure'] = 'Failed to modify your friends list';
$string['userdoesntwantfriends'] = 'This user doesn\'t want any new friends';
......
......@@ -4,6 +4,25 @@
{include file="columnleftstart.tpl"}
<h2>{$heading}</h2>
<div id="friendslistcontainer">
{$form}
{if $users}
<table id="friendslist">
{foreach from=$users item=user}
<tr class="r{cycle values=1,0}">
{include file="user/user.tpl" user=$user page='find'}
</tr>
{/foreach}
</table>
</div>
<div class="center">
{$pagination}
{else}
</div>
<div class="message">
{$message}
</div>
{/if}
{include file="columnleftend.tpl"}
{include file="footer.tpl"}
......@@ -9,7 +9,7 @@
<table id="friendslist">
{foreach from=$users item=user}
<tr class="r{cycle values=1,0}">
{include file="user/user.tpl" user=$user }
{include file="user/user.tpl" user=$user page='myfriends'}
</tr>
{/foreach}
</table>
......
{if $user->pending}
<td class="pending">
<div class="fl">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}" alt="">
</div>
<table class="friendinfo">
<tr>
......@@ -20,18 +20,14 @@
<tr>
<td id="friendinfo_{$user->id}">
{if $user->introduction}
<p>
{$user->introduction|escape}
</p>
{$user->introduction|escape}
{/if}
<div class="pending">
<div>
<strong>
{str tag='whymakemeyourfriend' section='group'}
{user->reason}
</strong>
</div>
<div/>
<strong>
{str tag='whymakemeyourfriend' section='group'}
{$user->reason}
</strong>
</div>
<div>
<ul>
<li>
......@@ -39,7 +35,7 @@
{str tag='denyrequest' section='group'}
</a>
</li>
{if $user->messages == 1}
{if $user->messages}
<li>
<a href="{$WWWROOT}user/sendmessage.php?id={$user->id}">
{str tag='sendmessage' section='group'}
......@@ -52,20 +48,24 @@
</tr>
</table>
</td>
{else}
{elseif $user->friend}
<td>
<div class="fl">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}" alt="">
</div>
<table class="friendinfo">
<tr>
<th>
<h3>
<a href="{$WWWROOT}user/view.php?id=}$user->id}">
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
</a>
{if $page == 'find'}
- {str tag='existingfriend' section='group'}
{/if}
</h3>
</th>
{if $page == 'myfriends'}
<td rowspan="2" class="viewlist">
<h3>
{str tag='Views' section='group'}
......@@ -81,12 +81,13 @@
</li>
{/foreach}
</ul>
{else}
{else}
<p>
{str tag='noviewstosee' section='group'}
{str tag='noviewstosee' section='group'}
</p>
{/if}
{/if}
</td>
{/if}
</tr>
<tr>
<td id="friendinfo_{$user->id}">
......@@ -96,7 +97,7 @@
</p>
{/if}
<ul>
{if $user->messages > 0}
{if $user->messages}
<li>
<a href="{$WWWROOT}user/sendmessage.php?id={$user->id}">
{str tag='sendmessage' section='group'}
......@@ -113,4 +114,88 @@
</tr>
</table>
</td>
{/if}
\ No newline at end of file
{elseif $user->requestedfriendship}
<td>
<div class="fl">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}" alt="">
</div>
<table class="friendinfo">
<tr>
<th>
<div class="fr">
{str tag='friendshiprequested' section='group'}
</div>
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
</a>
</h3>
</th>
</tr>
<tr>
<td id="friendinfo_{$user->id}">
{if $user->introduction}
{$user->introduction|escape}
{/if}
<div>
<ul>
{if $user->messages}
<li>
<a href="{$WWWROOT}user/sendmessage.php?id={$user->id}">
{str tag='sendmessage' section='group'}
</a>
</li>
{/if}
</ul>
</div>
</td>
</tr>
</table>
</td>
{else}
<td>
<div class="fl">
<img src="{$WWWROOT}thumb.php?type=profileicon&amp;size=40x40&amp;id={$user->id}" alt="">
</div>
<table class="friendinfo">
<tr>
<th>
<div class="fr">
{if $user->friendscontrol == 'auth'}
<a href="{$wwwroot}user/requestfriendship.php?id={$user->id}">
{str tag='sendfriendrequest' section='group'}
</a>
{elseif $user->friendscontrol == 'auto'}
{$user->makefriend}
{else}
{str tag='userdoesntwantfriends' section='group'}
{/if}
</div>
<h3>
<a href="{$WWWROOT}user/view.php?id={$user->id}">
{$user->id|display_name|escape}
</a>
</h3>
</th>
</tr>
<tr>
<td id="friendinfo_{$user->id}">
{if $user->introduction}
{$user->introduction|escape}
{/if}
<div>
<ul>
{if $user->messages}
<li>
<a href="{$WWWROOT}user/sendmessage.php?id={$user->id}">
{str tag='sendmessage' section='group'}
</a>
</li>
{/if}
</ul>
</div>
</td>
</tr>
</table>
</td>
{/if}
......@@ -29,9 +29,63 @@ define('MENUITEM', 'groups/findfriends');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
define('TITLE', get_string('findfriends'));
require('searchlib.php');
safe_require('search', 'internal');
$smarty = smarty();
$query = param_variable('query', '');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$data = search_user($query, $limit, $offset);
if ($data['data']) {
$userlist = '(' . join(',', array_map(create_function('$u','return $u[\'id\'];'), $data['data'])) . ')';
$data['data'] = get_users_data($userlist);
}
$searchform = pieform(array(
'name' => 'search',
'renderer' => 'oneline',
'elements' => array(
'query' => array(
'type' => 'text',
'defaultvalue' => $query
),
'submit' => array(
'type' => 'submit',
'value' => get_string('search')
)
)
));
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'user/find.php?query=' . $query,
'count' => $data['count'],
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('user', 'group'),
'resultcounttextplural' => get_string('users', 'group'),
));
$smarty = smarty(array(), array(), array(), array('sideblocks' => array(friends_control_sideblock())));
$smarty->assign('heading', TITLE);
$smarty->assign('users', $data['data']);
$smarty->assign('form', $searchform);
$smarty->assign('pagination', $pagination['html']);
if (isset($message)) {
$smarty->assign('message', $message);
}
$smarty->display('user/find.tpl');
function search_submit(Pieform $form, $values) {
redirect('/user/find.php' . (isset($values['query']) ? '?query=' . urlencode($values['query']) : ''));
}
function friendscontrol_submit(Pieform $form, $values) {
global $USER, $SESSION;
$USER->set_account_preference('friendscontrol', $values['friendscontrol']);
$SESSION->add_ok_msg(get_string('updatedfriendcontrolsetting', 'group'));
redirect('/user/find.php');
}
?>
......@@ -28,6 +28,7 @@ define('INTERNAL', 1);
define('MENUITEM', 'groups/myfriends');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require('searchlib.php');
define('TITLE', get_string('myfriends'));
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'user');
......@@ -41,113 +42,48 @@ $userid = $USER->get('id');
$data = array();
if ($filter == 'current') {
$count = count_records_select('usr_friend', 'usr1 = ? OR usr2 = ?', array($userid, $userid));
$sql = 'SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname,
(SELECT a.title FROM {artefact} a WHERE a.owner = u.id AND a.artefacttype = \'introduction\') AS introduction,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages
FROM {usr} u
WHERE u.id IN (
SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid
FROM {usr_friend} WHERE (usr1 = ? OR usr2 = ?))
ORDER BY u.id';
$data = get_records_sql_assoc($sql, array($userid, $userid, $userid), $offset, $limit);
$data = get_column_sql(
'SELECT usr1 AS id FROM {usr_friend} WHERE usr1 = ?
UNION SELECT usr2 AS id FROM {usr_friend} WHERE usr2 = ?
ORDER BY id
LIMIT ?
OFFSET ?', array($userid, $userid, $limit, $offset)
);
if (!$data || !$views = get_views(array_keys($data), null, null)) {
$views = array();
}
}
else if ($filter == 'pending') {
$count = count_records('usr_friend_request', 'owner', array($userid));
$sql = 'SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, fr.reason, 1 AS pending,
(SELECT a.title FROM {artefact} a WHERE a.owner = u.id AND a.artefacttype = \'introduction\') AS introduction,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages
FROM {usr} u
JOIN {usr_friend_request} fr ON fr.requester = u.id
WHERE fr.owner = ?
ORDER BY u.id';
$data = get_records_sql_array($sql, array($userid), $offset, $limit);
$views = array();
$data = get_column_sql(
'SELECT requester FROM {usr_friend_request} WHERE owner = ?
ORDER BY requester
LIMIT ?
OFFSET ?', array($userid, $limit, $offset)
);
}
else {
$filter = 'all';
$count = count_records_select('usr_friend', 'usr1 = ? OR usr2 = ?', array($userid, $userid))
+ count_records('usr_friend_request', 'owner', array($userid));
$sql = 'SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, NULL as reason, 0 AS pending,
(SELECT a.title FROM {artefact} a WHERE a.owner = u.id AND a.artefacttype = \'introduction\') AS introduction,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages
FROM {usr} u
WHERE u.id IN (
SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid
FROM {usr_friend} WHERE (usr1 = ? OR usr2 = ?))
UNION
SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, fr.reason, 1 AS pending,
(SELECT a.title FROM {artefact} a WHERE a.owner = u.id AND a.artefacttype = \'introduction\') AS introduction,
COALESCE((SELECT ap.value FROM {usr_account_preference} ap WHERE ap.usr = u.id AND ap.field = \'messages\'), \'allow\') AS messages
FROM {usr} u
JOIN {usr_friend_request} fr ON fr.requester = u.id
WHERE fr.owner = ?
ORDER BY pending DESC, id';
$data = get_records_sql_assoc($sql, array($userid, $userid, $userid, $userid));
$count = count_records_select('usr_friend_request', 'owner = ?', array($userid))
+ count_records_select('usr_friend', 'usr1 = ? OR usr2 = ?', array($userid, $userid));
$data = get_column_sql(
'SELECT id FROM (
SELECT requester AS id, \'1\' AS status FROM {usr_friend_request} WHERE owner = ?
UNION SELECT usr2 AS id, \'2\' AS status FROM {usr_friend} WHERE usr1 = ?
UNION SELECT usr1 AS id, \'2\' AS status FROM {usr_friend} WHERE usr2 = ?
) friend
ORDER BY status DESC, id
LIMIT ?
OFFSET ?', array($userid, $userid, $userid, $limit, $offset)
);
if (!$data || !$views = get_views(array_keys($data), null, null)) {
$views = array();
}
}
if ($data) {
$data = array_values($data);
foreach ($data as $d) {
$d->name = display_name($d);
if (isset($d->introduction)) {
$d->introduction = format_introduction($d->introduction);
}
$d->messages = ($d->messages == 'allow' || is_friend($userid, $d->id) && $d->messages == 'friends' || $USER->get('admin')) ? 1 : 0;
}
}
$viewcount = array_map('count', $views);
// since php is so special and inconsistent, we can't use array_map for this because it breaks the top level indexes.
$cleanviews = array();
foreach ($views as $userindex => $viewarray) {
$cleanviews[$userindex] = array_slice($viewarray, 0, 5);
// Don't reveal any more about the view than necessary
foreach ($cleanviews as $userviews) {
foreach ($userviews as &$view) {
foreach (array_keys(get_object_vars($view)) as $key) {
if ($key != 'id' && $key != 'title') {
unset($view->$key);
}
}
}
}
}
if ($data) {
foreach ($data as $friend) {
if (isset($cleanviews[$friend->id])) {
$friend->views = $cleanviews[$friend->id];
}
}
foreach ($data as $friend) {
if ($friend->pending) {
$friend->accept = pieform(array(
'name' => 'acceptfriend' . $friend->id,
'successcallback' => 'acceptfriend_submit',
'renderer' => 'div',
'autofocus' => 'false',
'elements' => array(
'submit' => array(
'type' => 'submit',
'value' => get_string('approverequest', 'group')
),
'id' => array(
'type' => 'hidden',
'value' => $friend->id
)
)
));
}
}
$userlist = '(' . join(',', $data) . ')';
$data = get_users_data($userlist);
}
$filterform = pieform(array(
......@@ -192,36 +128,14 @@ function filter_submit(Pieform $form, $values) {
redirect('/user/myfriends.php?filter=' . $values['filter']);
}
function acceptfriend_submit(Pieform $form, $values) {
global $USER, $SESSION;
$user = get_record('usr', 'id', $values['id']);
// friend db record
$f = new StdClass;
$f->ctime = db_format_timestamp(time());
$f->usr1 = $user->id;
$f->usr2 = $USER->get('id');
// notification info
$n = new StdClass;
$n->url = get_config('wwwroot') . 'user/view.php?id=' . $USER->get('id');
$n->users = array($user->id);
$lang = get_user_language($user->id);
$displayname = display_name($USER, $user);
$n->message = get_string_from_language($lang, 'friendrequestacceptedmessage', 'group', $displayname, $displayname);
$n->subject = get_string_from_language($lang, 'friendrequestacceptedsubject', 'group');
db_begin();
delete_records('usr_friend_request', 'owner', $USER->get('id'), 'requester', $user->id);
insert_record('usr_friend', $f);
db_commit();
$SESSION->add_ok_msg(get_string('friendformacceptsuccess', 'group'));
redirect('/user/view.php?id=' . $values['id']);
function friendscontrol_submit(Pieform $form, $values) {
global $USER, $SESSION;
$USER->set_account_preference('friendscontrol', $values['friendscontrol']);
$SESSION->add_ok_msg(get_string('updatedfriendcontrolsetting', 'group'));
redirect('/user/myfriends.php');
}
$smarty = smarty();
$smarty = smarty(array(), array(), array(), array('sideblocks' => array(friends_control_sideblock())));
$smarty->assign('heading', TITLE);
$smarty->assign('users', $data);
$smarty->assign('form', $filterform);
......
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