MyBB Community Forums

Full Version: Usergroup Page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Before I start I want to say I did not create this I am just sharing it, the creator is Sazze. Well thats what he said but i'll take his word for it for now. LOL. Ever wanted to have a page to display your sites user groups without you adding them to the page by yourself? Well now you can! Once set up all you need to do is add the user group IDs and that's it. The page will display the group name, description of the group, group image, and how much members are in it. 

Step 1. Create a new file in your forums root called usergroups.php and paste this code into it :
<?php

define( 'IN_MYBB', TRUE );
define( 'NAME', basename( __FILE__ ) );
define( 'THIS_SCRIPT', NAME );
define( 'GUEST', 'Please login or register in order to see this page.' );
require_once __DIR__ . '/global.php';

add_breadcrumb( 'Usergroups', NAME );

if( !$mybb->user['uid'] )
{
	error( GUEST );
}

$allowed_groups = array( 'x','x','x'); // Please group IDs here

if( !$mybb->input['request'] )
{
	foreach ( $allowed_groups as &$groupid )
	{
		$groupcache = $cache->read( 'usergroups' );

		$query = $db->write_query( "
			SELECT *
			FROM ".TABLE_PREFIX."usergroups
			WHERE `gid`='".(int)$groupid."'
			ORDER BY gid
		" );

		if( isset( $query->num_rows ) )
		{
			while( $groups = $db->fetch_array( $query ) )
			{
				/* Fetch group leaders */
				$leaders_array = array();
				$leader_query = $db->write_query("
					SELECT u.username, u.uid, u.usergroup, u.displaygroup, l.gid
					FROM ".TABLE_PREFIX."groupleaders l
					INNER JOIN ".TABLE_PREFIX."users u ON (u.uid=l.uid)
					WHERE `gid`='".(int)$groupid."'
				");

				while( $leader = $db->fetch_array( $leader_query ) )
				{					
					$leaders = build_profile_link( format_name( $leader['username'], ( !empty( $leader['displaygroup'] ) ? $leader['displaygroup'] : $leader['usergroup'] ) ), $leader['uid'] );
					array_push( $leaders_array, $leaders );
				}

				$usergroups['groupleaders'] = rtrim( implode( ', ', $leaders_array ), ', ' );

				if( !$usergroups['groupleaders'] )
				{
					$usergroups['groupleaders'] = 'None';
				}

				$usergroups['groupname'] = htmlspecialchars_uni( $groupcache[$groups['gid']]['title'] );
				$usergroups['groupdesc'] = htmlspecialchars_uni( $groupcache[$groups['gid']]['description'] );
				$usergroups['userbar'] = '<img src="'.$groupcache[$groups['gid']]['image'].'" />';
				$usergroups['gid'] = intval( $groupcache[$groups['gid']]['gid'] );
				$usergroups['members'] = intval( countmembers( $groupid ) );

				eval( "\$groups_row .= \"".$templates->get( 'usergroups_row' )."\";" );
			}
		}
		else
		{
			$groups_row = '<td class="trow1" align="left">No usergroups was found.</td>';
		}
	}
}

eval( "\$usergroups_page = \"".$templates->get( 'usergroups' )."\";" );
output_page( $usergroups_page );


/* Count members function */
function countmembers( $groupid )
{
	global $db, $plugins;

	$query = $db->query("
		SELECT g.gid, COUNT(u.uid) AS users
		FROM ".TABLE_PREFIX."users u
		LEFT JOIN ".TABLE_PREFIX."usergroups g ON (g.gid=u.usergroup)
		GROUP BY g.gid
	");

	while( $groupcount = $db->fetch_array( $query ) )
	{
		$primaryusers[$groupcount['gid']] = $groupcount['users'];
	}

	switch( $db->type )
	{
		case "pgsql":
		case "sqlite":
			$query = $db->query("
				SELECT g.gid, COUNT(u.uid) AS users
				FROM ".TABLE_PREFIX."users u
				LEFT JOIN ".TABLE_PREFIX."usergroups g ON (','|| u.additionalgroups|| ',' LIKE '%,'|| g.gid|| ',%')
				WHERE g.gid != '0' AND g.gid is not NULL GROUP BY g.gid
			");

		break;
		default:
			$query = $db->query("
				SELECT g.gid, COUNT(u.uid) AS users
				FROM ".TABLE_PREFIX."users u
				LEFT JOIN ".TABLE_PREFIX."usergroups g ON (CONCAT(',', u.additionalgroups, ',') LIKE CONCAT('%,', g.gid, ',%'))
				WHERE g.gid != '0' AND g.gid is not NULL GROUP BY g.gid
			");
		}

		while( $groupcount = $db->fetch_array( $query ) )
		{
			$secondaryusers[$groupcount['gid']] = $groupcount['users'];
		}
				
		$numusers = $primaryusers[$groupid];
		$numusers += $secondaryusers[$groupid];
				
		if( !$numusers )
		{
			$numusers = "0";
		}

		return $numusers;
	}
?>

Now find $allowed_groups = array( 'x','x','x'); (Line 16) change the x to the user group ID you want to be displayed and remember you can add more if needed.

Step 2. Now go to

Admin CP -> Templates & Styles -> Templates -> Global Templates create a new template called usergroups and paste this code into it :
<html>
	<head>
		<title>
			{$mybb->settings['bbname']} - Usergroups
		</title>
		{$headerinclude}
	</head>
	<body>
		<style>
			.joingroup {
				color: #fff;
				text-shadow: none;
				padding: 5px 8px;
				border-radius: 2px;
				text-align: center;
				margin-left: 5px;
				background: rgba(0,0,0,0.2);
			}
			.joingroup:hover {
				cursor: pointer;
				background: rgb(167, 70, 70);
			}
		</style>
		{$header}
		<table border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder">
			<tr>
				<td class="thead" colspan="5"><strong>Usergroups</strong></td>
			</tr>
			<tr>
				<td class="trow1">{$groups_row}</td>
			</tr>
		</table>
		{$footer}
	</body>
</html>

Step 3. Now go back and create a new template called usergroups_row and paste this code into it :
<div style="width: 48%; min-height: 150px; float: left; border: 1px #333 solid; margin: 4px; padding: 2px;">
	<table width="100%" cellspacing="0" cellpadding="5" border="0">
		<tr>
			<td width="75%">
				<span style="color: #fff; font-size: 15px;">{$usergroups['groupname']}</span><br />
				</br>{$usergroups['groupdesc']}<br />
		Members: {$usergroups['members']}
		</td>
	<td width="25%" align="right" valign="top">
		{$usergroups['userbar']}
	</td>
	</tr>
</table>
</div>

Note: You might need to change the color to match with your theme to do this just find <span style="color: #fff; font-size: 15px;"> and change the #fff to the color code you want.

That's it your done now go to http://yoursite.com/usergroups.php and enjoy!

Screenshot:

[Image: Untitled.png]
Nice one.
I just wonder is it a good practice to define an error message in a constant? It should be driven by language variable though ...
... and CSS should be defined in a separate css file assigned to the page.
Wonder where you got that code from.. lol. I wrote it a while back.
Cool share.
(2018-04-11, 11:22 PM)Sazze Wrote: [ -> ]Wonder where you got that code from.. lol. I wrote it a while back.
Cool share.

Didn't know who wrote it my bad will update thread with credit.
Brian ..... Brian .....  Toungue