Looking to delete a user from a group

Discussion forum for MOD Writers regarding MOD Development.
Locked
BlueCollar
Registered User
Posts: 67
Joined: Sun Dec 05, 2010 11:54 pm

Looking to delete a user from a group

Post by BlueCollar » Sun Jan 16, 2011 1:44 am

A little background...

All I wanted was to make a cool sports forum with unique features that other forums didn't have!

3 days ago if you told me to install a mod to a site I wouldn't have the slightest clue what you wanted.

Now I have 3 mods installed, looking at 2 more (in beta unfortunately), and am looking at customizing a plugin. (What have I gotten myself into...)


Which brings me to right now, here's my dilemma.


I'm working on a plugin for the Easy Shop Mod that runs with the Ultimate Points System (UPS). This plugin is to allow users to buy an item that gives them specific group permissions (essentially they are buying their way into a group). I did a lot of searching around and found some great coding that will allow users to buy their way into the group. As it stands right now that coding has a few minor flaws, but that's for another time. What I want to add to this feature is another option where you can buy an item that will allow you to return to your original group and delete your status from a group that you bought into.

For example:

Step 1) User 1 belongs to 'Registered Users'.
Step 2) User 1 buys the item and now belongs to 'Special Group' as well as 'Registered Users'
Step 3) User 1 buys a different item that will delete his status from 'Special Group'. He will remain in 'Registered Users'


Here's the coding for what works so far (user buys into special group)

Code: Select all

<?php
/**
*
* @package - phpbb3 UPS Easy Shop
* @version $Id: user_color_black.php 138 2010-02-22 10:39:40Z femu $
* @copyright (c) Wuerzi (http://spieleresidenz.de), (c) femu (http://die-muellers.org)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/*
* @ignore
*/
if (!defined('IN_PHPBB'))
{
   exit;
}

class user_group_add_1 extends item
{
	function init()
	{}

	//function get_actions()
	//{
		//global $user;

		//$actions = array();

		//In most cases, items that have an action with them should use the "use" action.
		//To enable it, uncomment the following code:
		//$actions['use'] = array(
			//'name'      => $user->lang['SHOP_ITEM_USE'],
			//'confirm'   => sprintf($user->lang['SHOP_ITEM_CONFIRM_USE'], $this->data['name']),
			//'function'   => 'use_item'
		//);

		$actions = array_merge($actions, parent::get_actions());
		return $actions;
	}

	function use_item()
	{
		global $user, $shop, $db, $phpEx, $phpbb_root_path;

		// Enter here the group_id, where the user should go in
		$grp_id = '8';

		// Check, if the user is already in the group
		$sql = 'SELECT *
			FROM ' . USER_GROUP_TABLE . '
			WHERE user_id = ' . (int) $user->data['user_id'] . '
				AND group_id = ' . $grp_id;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$group_id = $row['group_id'];
		
		if ($group_id)
		{
			// Remove item from users inventory
			$this->remove_item();

			// Sorting back the item into the shop
			$sql = 'UPDATE ' . SHOP_ITEMS_TABLE . '
				SET quantity = quantity + 1
				WHERE item=' . (int) $this->data['item'];
			$db->sql_query($sql);

			// Giving back the users points
			$shop->cash->deposit_user($this->data['price']);

			// Inform user
			$meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
			meta_refresh(3, $meta_info);
			$message = $user->lang['SHOP_ITEM_GROUP'] . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
			trigger_error($message);
			return;
		}
		else
		{
			$this->remove_item();

			$uid = (int) $user->data['user_id'];

			// Check, if need function exists
			if (!function_exists('group_user_add'))
			{
				include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

				// And now add to group using the phpBB internal function
				group_user_add($grp_id, $uid);

				$message = sprintf($user->lang['SHOP_ITEM_NOW_USE'], $this->data['name']);
				return $message;
			}
		}
	}
}

?>

And because I am so new to php coding I did some searching around and it led me to the includes/functions_user.php file, more specifically lines 3632-3654 (see below). I think this is close to the kind of coding I will need in order to delete a user from a group he bought into.

Code: Select all

	// We need to call group_user_del here, because this function makes sure everything is correctly changed.
	// A downside for a call within the session handler is that the language is not set up yet - so no log entry
	group_user_del($group_id, $user_id);

	// Set user_new to 0 to let this not be triggered again
	$sql = 'UPDATE ' . USERS_TABLE . '
		SET user_new = 0
		WHERE user_id = ' . $user_id;
	$db->sql_query($sql);

	// The new users group was the users default group?
	if ($user_data['group_id'] == $group_id)
	{
		// Which group is now the users default one?
		$sql = 'SELECT group_id
			FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);
		$user_data['group_id'] = $db->sql_fetchfield('group_id');
		$db->sql_freeresult($result);
	}

	return $user_data['group_id'];

I would love to get some opinions on this. I feel like I am close to being able to delete a user from a group, but honestly I'm learning as I go here. Any feedback is appreciated and I welcome any critiques and comments. Just want to learn as much as I can here. Thanks!

BlueCollar
Registered User
Posts: 67
Joined: Sun Dec 05, 2010 11:54 pm

Re: Looking to delete a user from a group

Post by BlueCollar » Tue Jan 18, 2011 7:16 am

Any ideas? Could use some pointers, trying to learn all I can.

User avatar
hah
Registered User
Posts: 85
Joined: Sat Feb 13, 2010 11:49 am
Location: Pernik, Bulgaria
Name: Alex Georgiev
Contact:

Re: Looking to delete a user from a group

Post by hah » Tue Jan 18, 2011 7:43 am

Code: Select all

group_user_del($group_id, $user->data['user_id'], false, false);
$user->data['user_id'] = loggedin user_id;
You need to include functions_user.php to work that.

Locked

Return to “[3.0.x] MOD Writers Discussion”