SimplePortal

Customization => Custom Coding => Topic started by: wellwisher on October 17, 2015, 11:29:49 PM

Title: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 17, 2015, 11:29:49 PM
Hello SP team and community,

I am trying to display a list of members in a particular membergroup i.e "Valued contributors" in a block. There's already a "staff list" block but I want one which displays a membergroup of my choice?
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 17, 2015, 11:31:58 PM
Do you want it to look identical to the staff block?
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 18, 2015, 12:39:37 AM
Yes, that would be pretty awesome Sayaka Maizono. Can this be achieved with php block option?

Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 18, 2015, 01:05:41 AM
Yes, make a custom PHP block and place the following code in it.

This is an old version. Please use the updated version here, later in the topic. (http://simpleportal.net/index.php?topic=14140.msg70463#msg70463)

Code: [Select]
<?php

/**
 * @block List Groups in Staff Style
 * @author Rhode Fey (Sayaka Maizono)
 * @version 1.0.0
*/

/*
Usage:
fill the array with group ids of the groups you want to show.
Example for showing just one group, from group 1;
$groups_to_use = array(1);
Example for showing three groups, from group 1, 3, and 8
$groups_to_use = array(1, 3, 8);
*/

$groups_to_use = array(1);
include(
dirname(__FILE__) . '/SSI.php');

global 
$smcFunc$sourcedir$scripturl$modSettings;

if (empty(
$groups_to_use))
return 'No groups to show...';

$request $smcFunc['db_query']('''
SELECT
mem.id_member, mem.real_name, mem.avatar, mem.id_group, mg.group_name,
a.id_attach, a.attachment_type, a.filename
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
WHERE mem.id_group IN ({array_int:groups_to_use})'
,
array(
'groups_to_use' => $groups_to_use,
)
);

$member_list = array();
while (
$row $smcFunc['db_fetch_assoc']($request))
{
if ($modSettings['avatar_action_too_large'] == 'option_html_resize' || $modSettings['avatar_action_too_large'] == 'option_js_resize')
{
$avatar_width = !empty($modSettings['avatar_max_width_external']) ? ' width="' $modSettings['avatar_max_width_external'] . '"' '';
$avatar_height = !empty($modSettings['avatar_max_height_external']) ? ' height="' $modSettings['avatar_max_height_external'] . '"' '';
}
else
{
$avatar_width '';
$avatar_height '';
}

$member_list[] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'link' => '<a href="' $scripturl '?action=profile;u=' $row['id_member'] . '">' $row['real_name'] . '</a>',
'group' => $row['group_name'],
'avatar' => array(
'name' => $row['avatar'],
'image' => $row['avatar'] == '' ? ($row['id_attach'] > '<img src="' . (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) . '" alt="" class="avatar" border="0" />' '') : (stristr($row['avatar'], 'http://') ? '<img src="' $row['avatar'] . '"' $avatar_width $avatar_height ' alt="" class="avatar" border="0" />' '<img src="' $modSettings['avatar_url'] . '/' htmlspecialchars($row['avatar']) . '" alt="" class="avatar" border="0" />'),
'href' => $row['avatar'] == '' ? ($row['id_attach'] > ? (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) : '') : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar']),
'url' => $row['avatar'] == '' '' : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar'])
),
);
}
$smcFunc['db_free_result']($request);

if (empty(
$member_list))
return 'No members to show...';

ksort($member_list);
$member_count count($member_list);
$count 0;

echo 
'
<table class="sp_fullwidth">'
;

foreach (
$member_list as $member)
echo '
<tr>
<td class="sp_staff sp_center">'
, !empty($member['avatar']['href']) ? '<a href="' $scripturl '?action=profile;u=' $member['id'] . '"><img src="' $member['avatar']['href'] . '" alt="' $member['name'] . '" width="40" /></a>' '''</td>
<td class="sp_staff_info'
$member_count != ++$count ' sp_staff_divider' '''">
'
$member['link'], '<br />
'
$member['group'], '
</td>
</tr>'
;

echo '
</table>'
;

?>
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 19, 2015, 01:50:43 AM
Thank you for responding Sayaka Maizono just tried your code and it works brilliantly for "Regular groups" i.e administors, global mods, mods, and basically any group you make.

What about targetting "Post count based groups"? I tried putting in a post count group number:

$groups_to_use = array(7);

But the group doesn't get listed. Any help would be awesome thank you.
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 19, 2015, 07:22:57 PM
Code: [Select]
<?php

/**
 * @block List Groups in Staff Style
 * @author Rhode Fey (Sayaka Maizono)
 * @version 1.0.1
*/

/*
Usage:
fill the array with group ids of the groups you want to show.
Example for showing just one group, from group 1;
$groups_to_use = array(1);
Example for showing three groups, from group 1, 3, and 8
$groups_to_use = array(1, 3, 8);
*/

$groups_to_use = array(1);

/*
 *
*/

global $smcFunc$sourcedir$scripturl$modSettings$txt;

// These are strings for errors...
$txt['no_groups'] = 'No groups have been defined! Please refer to the comments in the Block code!';
$txt['no_members'] = 'No members have been found...';

$groups = array();
foreach (
$groups_to_use as $group)
$groups[] = (int) $group;
$groups array_unique($groups);

if (empty(
$groups))
{
echo '
<span class="smalltext" style="color: red;">
'
$txt['no_groups'] . '
</span>'
;

return;
}

$request $smcFunc['db_query']('''
SELECT
mem.id_member, mem.real_name, mem.avatar, mem.id_group, mem.id_post_group, mg.group_name,
a.id_attach, a.attachment_type, a.filename
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_group_id} THEN mem.id_post_group ELSE mem.id_group END)
WHERE mem.id_group IN ({array_int:groups_to_use}) OR mem.id_post_group IN ({array_int:groups_to_use})'
,
array(
'groups_to_use' => $groups,
'reg_group_id' => 0,
)
);

$member_list = array();
while (
$row $smcFunc['db_fetch_assoc']($request))
{
if ($modSettings['avatar_action_too_large'] == 'option_html_resize' || $modSettings['avatar_action_too_large'] == 'option_js_resize')
{
$avatar_width = !empty($modSettings['avatar_max_width_external']) ? ' width="' $modSettings['avatar_max_width_external'] . '"' '';
$avatar_height = !empty($modSettings['avatar_max_height_external']) ? ' height="' $modSettings['avatar_max_height_external'] . '"' '';
}
else
{
$avatar_width '';
$avatar_height '';
}

$member_list[$row['id_member']] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'link' => '<a href="' $scripturl '?action=profile;u=' $row['id_member'] . '">' $row['real_name'] . '</a>',
'group' => $row['group_name'],
'avatar' => array(
'name' => $row['avatar'],
'image' => $row['avatar'] == '' ? ($row['id_attach'] > '<img src="' . (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) . '" alt="" class="avatar" border="0" />' '') : (stristr($row['avatar'], 'http://') ? '<img src="' $row['avatar'] . '"' $avatar_width $avatar_height ' alt="" class="avatar" border="0" />' '<img src="' $modSettings['avatar_url'] . '/' htmlspecialchars($row['avatar']) . '" alt="" class="avatar" border="0" />'),
'href' => $row['avatar'] == '' ? ($row['id_attach'] > ? (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) : '') : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar']),
'url' => $row['avatar'] == '' '' : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar'])
),
);
}
$smcFunc['db_free_result']($request);

if (empty(
$member_list))
{
echo '
<span class="smalltext" style="color: red;">
'
$txt['no_members'] . '
</span>'
;

return;
}

ksort($member_list);
$member_count count($member_list);
$count 0;

echo 
'
<table class="sp_fullwidth">'
;

foreach (
$member_list as $member)
echo '
<tr>
<td class="sp_staff sp_center">'
, !empty($member['avatar']['href']) ? '<a href="' $scripturl '?action=profile;u=' $member['id'] . '"><img src="' $member['avatar']['href'] . '" alt="' $member['name'] . '" width="40" /></a>' '''</td>
<td class="sp_staff_info'
$member_count != ++$count ' sp_staff_divider' '''">
'
$member['link'], '<br />
'
$member['group'], '
</td>
</tr>'
;

echo '
</table>'
;

?>

Changelog (Version 1.0.1)
- Fixed error handling when members/groups are not defined.
- Sanitized the variable that users edit to make it safe, casting the variable as a different type or putting invalid data in it (such as the names of groups instead of ids) will not cause a database error anymore. However syntax errors are unavoidable, I will look into a way to edit the variable with a small interface in the future.
- Added the ability to define post count based groups, and to show post count based group titles.
- Made the arrays unique so members that are in post count groups and also in other groups defined will not be shown multiple times. However the member will be shown as a part of the group defined first for that member.
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 19, 2015, 08:06:20 PM
@SayakaMaizono thank you for your help, this is a sexy piece of code. Works like a charm, plus you've made it so easy for nubsters like me - for this, I pledge my heartful thank you! Plus this will help out a tone of other people in the same boat as I was. Now I can pretty much add whichever postgroup I wish. Thank you again, this will help promote members hard work within our forums! I can't wait to make membergroups like "Hall of fame", Special members etc and showcase them in the sp sidebar with your help. Cheers Sayaka!

I also didn't have to edit any forum files which can get messy, this was really quick and easy simple instructions.
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 19, 2015, 08:09:32 PM
Of course, let me know if you run into any problems with the code. I can adapt it a lot, like making it so certain members won't appear or making it so only certain conditions are met (like name format, amount of posts, etc).

While I knew you really wanted to only have the code show one group, I thought about making it show any number of groups just to make it more versatile for both you and other members.

If you have other requests feel free to post them here in the Custom Coding board (http://simpleportal.net/index.php?board=39.0).
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 19, 2015, 08:46:44 PM
You've been more than kind mate thank you, yes I will let you know if I run into issues although I doubt this will happen.

One thing that comes to mind (it's not a bug or a SP problem) and I apologise for going off-topic...
When I first created the 'Staff list' block... some of our staff did not have avatars, this made the Staff list block look aesthetically weak. Same problem with SMF forum. I overcame this issue by using the Mod by Jokerâ„¢ called "Default Avatar (http://custom.simplemachines.org/mods/index.php?mod=2665)".

Having a default avatar option in these 'member blocks' would be a much welcomed upgrade in future SP releases. 
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 19, 2015, 10:43:17 PM
The function checkDefaultAvatar() has undefined array keys, and doesn't really seem optimized to use. Really should ask the mod author to update it and make it easier to call. The mod doesn't even use the function itself and it seems that it was designed solely for integration purposes too.

Regardless, try out this edit for my block:

Find:
Code: [Select]
}
$smcFunc['db_free_result']($request);

Code: [Select]
global $settings;
if (empty($member_list[$row['id_member']]['avatar']['href']))
$member_list[$row['id_member']]['avatar']['href'] = $settings['default_images_url'] . '/default_avatar.png';
}
$smcFunc['db_free_result']($request);

Note that this ignores the male/female options of the mod and just uses the default url and not one you define. Maybe I'll look into making my own default avatar modification.
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 19, 2015, 11:36:16 PM
Thank you for responding. Again apologies for side-tracking the topic. I want to give this new avatar code a shot Sayaka! Just to clarify things for my small mind... I find this code below:

What's the file location of this code by the way?

Code: [Select]
}
$smcFunc['db_free_result']($request);

Then all I do is replace the above code with the one below, am I correct Sayaka?:

Code: [Select]
global $settings;
if (empty($member_list[$row['id_member']]['avatar']['href']))
$member_list[$row['id_member']]['avatar']['href'] = $settings['default_images_url'] . '/default_avatar.png';
}
$smcFunc['db_free_result']($request);

Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: ccbtimewiz on October 19, 2015, 11:48:32 PM
It's for the custom PHP block earlier.

Here, I made the edits for you:

Code: [Select]
<?php

/**
 * @block List Groups in Staff Style
 * @author Rhode Fey (Sayaka Maizono)
 * @version 1.0.1
*/

/*
Usage:
fill the array with group ids of the groups you want to show.
Example for showing just one group, from group 1;
$groups_to_use = array(1);
Example for showing three groups, from group 1, 3, and 8
$groups_to_use = array(1, 3, 8);
*/

$groups_to_use = array(1);

/*
 *
*/

global $smcFunc$sourcedir$scripturl$modSettings$txt;

// These are strings for errors...
$txt['no_groups'] = 'No groups have been defined! Please refer to the comments in the Block code!';
$txt['no_members'] = 'No members have been found...';

$groups = array();
foreach (
$groups_to_use as $group)
$groups[] = (int) $group;
$groups array_unique($groups);

if (empty(
$groups))
{
echo '
<span class="smalltext" style="color: red;">
'
$txt['no_groups'] . '
</span>'
;

return;
}

$request $smcFunc['db_query']('''
SELECT
mem.id_member, mem.real_name, mem.avatar, mem.id_group, mem.id_post_group, mg.group_name,
a.id_attach, a.attachment_type, a.filename
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_group_id} THEN mem.id_post_group ELSE mem.id_group END)
WHERE mem.id_group IN ({array_int:groups_to_use}) OR mem.id_post_group IN ({array_int:groups_to_use})'
,
array(
'groups_to_use' => $groups,
'reg_group_id' => 0,
)
);

$member_list = array();
while (
$row $smcFunc['db_fetch_assoc']($request))
{
if ($modSettings['avatar_action_too_large'] == 'option_html_resize' || $modSettings['avatar_action_too_large'] == 'option_js_resize')
{
$avatar_width = !empty($modSettings['avatar_max_width_external']) ? ' width="' $modSettings['avatar_max_width_external'] . '"' '';
$avatar_height = !empty($modSettings['avatar_max_height_external']) ? ' height="' $modSettings['avatar_max_height_external'] . '"' '';
}
else
{
$avatar_width '';
$avatar_height '';
}

$member_list[$row['id_member']] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
'link' => '<a href="' $scripturl '?action=profile;u=' $row['id_member'] . '">' $row['real_name'] . '</a>',
'group' => $row['group_name'],
'avatar' => array(
'name' => $row['avatar'],
'image' => $row['avatar'] == '' ? ($row['id_attach'] > '<img src="' . (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) . '" alt="" class="avatar" border="0" />' '') : (stristr($row['avatar'], 'http://') ? '<img src="' $row['avatar'] . '"' $avatar_width $avatar_height ' alt="" class="avatar" border="0" />' '<img src="' $modSettings['avatar_url'] . '/' htmlspecialchars($row['avatar']) . '" alt="" class="avatar" border="0" />'),
'href' => $row['avatar'] == '' ? ($row['id_attach'] > ? (empty($row['attachment_type']) ? $scripturl '?action=dlattach;attach=' $row['id_attach'] . ';type=avatar' $modSettings['custom_avatar_url'] . '/' $row['filename']) : '') : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar']),
'url' => $row['avatar'] == '' '' : (stristr($row['avatar'], 'http://') ? $row['avatar'] : $modSettings['avatar_url'] . '/' $row['avatar'])
),
);

global $settings;
if (empty($member_list[$row['id_member']]['avatar']['href']))
$member_list[$row['id_member']]['avatar']['href'] = $settings['default_images_url'] . '/default_avatar.png';
}
$smcFunc['db_free_result']($request);

if (empty(
$member_list))
{
echo '
<span class="smalltext" style="color: red;">
'
$txt['no_members'] . '
</span>'
;

return;
}

ksort($member_list);
$member_count count($member_list);
$count 0;

echo 
'
<table class="sp_fullwidth">'
;

foreach (
$member_list as $member)
echo '
<tr>
<td class="sp_staff sp_center">'
, !empty($member['avatar']['href']) ? '<a href="' $scripturl '?action=profile;u=' $member['id'] . '"><img src="' $member['avatar']['href'] . '" alt="' $member['name'] . '" width="40" /></a>' '''</td>
<td class="sp_staff_info'
$member_count != ++$count ' sp_staff_divider' '''">
'
$member['link'], '<br />
'
$member['group'], '
</td>
</tr>'
;

echo '
</table>'
;

?>
Title: Re: Show a list of members in a particular membergroup i.e "Valued contributors"?
Post by: wellwisher on October 21, 2015, 12:08:41 AM
Just tested your code on my local and I can see the default avatars now. Thank you Sayaka Maizono, this works perfectly. Have a happy halloween! This is pretty awesome!
SimplePortal 2.3.8 © 2008-2024, SimplePortal