Commit 99842fe0 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add pagination to my friends block


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 29417a6f
......@@ -51,6 +51,30 @@ class PluginBlocktypeMyfriends extends SystemBlocktype {
return array('profile');
}
public static function build_myfriends_html(&$friends, $userid) {
$friendarray = array_chunk($friends['data'], 4); // get the friends into a 4x4 array
$smarty = smarty_core();
$smarty->assign_by_ref('friends', $friendarray);
$friends['tablerows'] = $smarty->fetch('blocktype:myfriends:myfriendrows.tpl');
$baseurl = $_SERVER['REQUEST_URI']; // hmm
$baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'user=' . (int) $userid;
$pagination = build_pagination(array(
'id' => 'userfriendstable_pagination',
'class' => 'center nojs-hidden-block',
'datatable' => 'userfriendstable',
'url' => $baseurl,
'jsonscript' => 'blocktype/myfriends/myfriends.json.php',
'count' => $friends['count'],
'limit' => $friends['limit'],
'offset' => $friends['offset'],
'numbersincludefirstlast' => false,
'resultcounttextsingular' => get_string('friend', 'group'),
'resultcounttextplural' => get_string('friends', 'group'),
));
$friends['pagination'] = $pagination['html'];
$friends['pagination_js'] = $pagination['javascript'];
}
public static function render_instance(BlockInstance $instance, $editing=false) {
global $USER;
$userid = $instance->get_view()->get('owner');
......@@ -59,27 +83,15 @@ class PluginBlocktypeMyfriends extends SystemBlocktype {
return '';
}
$smarty = smarty_core();
$records = get_records_sql_array('
SELECT f.* FROM (
SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, u.profileicon
FROM {usr} u JOIN {usr_friend} f ON u.id = f.usr1
WHERE f.usr2 = ? AND u.deleted = 0
UNION
SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, u.profileicon
FROM {usr} u JOIN {usr_friend} f ON u.id = f.usr2
WHERE f.usr1 = ? AND u.deleted = 0
) f
ORDER BY ' . db_random() . '
LIMIT ?',
array($userid, $userid, MAXFRIENDDISPLAY)
);
if ($records) {
$friends = array_chunk($records, 4); // get the friends into a 4x4 array
$friends = get_friends($userid, MAXFRIENDDISPLAY, 0);
if ($friends['count']) {
self::build_myfriends_html($friends, $userid);
}
else {
$friends = false;
}
$smarty = smarty_core();
$smarty->assign('friends', $friends);
$smarty->assign('searchingforfriends', array('<a href="' . get_config('wwwroot') . 'user/find.php">', '</a>'));
......@@ -149,23 +161,10 @@ class PluginBlocktypeMyfriends extends SystemBlocktype {
$ownerid = $instance->get_view()->get('owner');
if ($ownerid === null || $ownerid == $USER->get('id')) {
$title = get_string('title', 'blocktype.myfriends');
}
else {
$title = get_string('otherusertitle', 'blocktype.myfriends', display_name($ownerid, null, true));
}
$numberoffriends = count_records_sql('SELECT COUNT(usr1) FROM {usr_friend}
JOIN {usr} u1 ON (u1.id = usr1 AND u1.deleted = 0)
JOIN {usr} u2 ON (u2.id = usr2 AND u2.deleted = 0)
WHERE usr1 = ? OR usr2 = ?',
array($ownerid, $ownerid)
);
if ($numberoffriends > MAXFRIENDDISPLAY) {
$title .= ' ' . get_string('numberoffriends', 'blocktype.myfriends', MAXFRIENDDISPLAY, $numberoffriends);
return get_string('title', 'blocktype.myfriends');
}
return $title;
return get_string('otherusertitle', 'blocktype.myfriends', display_name($ownerid, null, true));
}
}
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage blocktype-myfriends
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('blocktype', 'myfriends');
require_once('user.php');
$userid = param_integer('user');
$offset = param_integer('offset');
$limit = param_integer('limit', MAXFRIENDDISPLAY);
$friends = get_friends($userid, $limit, $offset);
PluginBlocktypeMyfriends::build_myfriends_html($friends, $userid);
json_reply(false, array('data' => $friends));
?>
{foreach from=$friends item=row}
<tr>
{foreach from=$row item=friend}
<td class="{cycle values='r0,r1'} friendcell">
<a href="{$WWWROOT}user/view.php?id={$friend->id}">
<img src="{profile_icon_url user=$friend maxwidth=60 maxheight=60}" alt="">
</a>
<div><a href="{$WWWROOT}user/view.php?id={$friend->id}">{$friend|display_default_name|escape}</a></div>
</td>
{/foreach}
</tr>
{/foreach}
<div class="friends">
{if $friends}
<table id="userfriendstable" class="center fullwidth">
{foreach from=$friends item=row}
<tr>
{foreach from=$row item=friend}
<td class="{cycle values='r0,r1'} friendcell">
<a href="{$WWWROOT}user/view.php?id={$friend->id}">
<img src="{profile_icon_url user=$friend maxwidth=60 maxheight=60}" alt="">
</a>
<div><a href="{$WWWROOT}user/view.php?id={$friend->id}">{$friend|display_default_name|escape}</a></div>
</td>
{/foreach}
</tr>
{/foreach}
<tbody>
{$friends.tablerows}
</tbody>
</table>
<div id="myfriends_page_container" class="hidden">{$friends.pagination}</div>
<script>
addLoadEvent(function() {literal}{{/literal}
{$friends.pagination_js}
removeElementClass('myfriends_page_container', 'hidden');
{literal}}{/literal});
</script>
{else}
{if $lookingatownpage}
<div class="message">{str tag="trysearchingforfriends" section=group args=$searchingforfriends}</div>
......
......@@ -1725,4 +1725,41 @@ function gravatar_icon($email, $size, $notfound) {
return "http://www.gravatar.com/avatar/{$md5sum}.jpg?r=g&s=$s&d=" . urlencode($notfound);
}
?>
/**
* Get user records for a user's friends
*
* @param integer $userid id of the user whose friends we're after
* @param integer $limit
* @param integer $offset
*
* @returns array Total number of friends, along with $limit or fewer user records.
*/
function get_friends($userid, $limit=10, $offset=0) {
$result = array('count' => 0, 'limit' => $limit, 'offset' => $offset, 'data' => false);
$from = 'FROM (
SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, u.profileicon
FROM {usr} u JOIN {usr_friend} f ON u.id = f.usr1
WHERE f.usr2 = ? AND u.deleted = 0
UNION
SELECT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, u.profileicon
FROM {usr} u JOIN {usr_friend} f ON u.id = f.usr2
WHERE f.usr1 = ? AND u.deleted = 0
) f';
$values = array($userid, $userid);
if (!$result['count'] = count_records_sql('SELECT COUNT(*) ' . $from, $values)) {
return $result;
}
$sql = '
SELECT f.* ' . $from . "
ORDER BY CASE WHEN NOT f.preferredname IS NULL AND f.preferredname <> '' THEN f.preferredname ELSE f.firstname || f.lastname END";
$result['data'] = get_records_sql_array($sql, $values, $offset, $limit);
return $result;
}
......@@ -76,7 +76,7 @@ $stylesheets = array('<link rel="stylesheet" type="text/css" href="' . get_confi
$name = display_name($user);
define('TITLE', $name);
$smarty = smarty(
array('lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js'),
array('paginator', 'lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js'),
$stylesheets,
array(),
array(
......
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