Add-on Run query on user downgrade / expiry

trilogy33

Well-known member
Taking a look at something that Jake wrote a few months back:
http://xenforo.com/community/resources/run-query-on-user-upgrade-code-example.396/

I wondered if the opposite could be made into a modification?

To, Update the xf_user table when a user's upgrade has expired

Possible options (usage scenario):

• Blank the user's Custom Title field (empty string)
and/or
• Blank the user's Signature field (so again, to enter an empty string)
and/or
• Set their mood_id to x (ideal when XenMoods is being used as an upgrade)


Ideally the plug-in would be set to a cron. <- Bit obvious but I thought I'd mention it ;)
 
You would add the downgrade function to the extended class:

Rich (BB code):
<?php

class ExtendUserUpgrade_UserUpgradeModel extends XFCP_ExtendUserUpgrade_UserUpgradeModel
{
	public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
	{
		$retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);

		$db = XenForo_Application::get('db');

		$db->query("
			UPDATE iConomy
			SET Balance = Balance + 3000
			WHERE username = (
				SELECT username
				FROM xf_user
				WHERE user_id = " . $userId . "
			)
		");

		return $retval;
	}

	public function downgradeUserUpgrades(array $upgrades)
	{
		parent::downgradeUserUpgrades($upgrades);

		if (!$upgrades)
		{
			return;
		}

		// YOUR STUFF HERE
	}
}

That's your skeleton. You just need to fill in your code to do whatever extra stuff you want when a user is downgraded.
 
You would add the downgrade function to the extended class:

Rich (BB code):
<?php
 
class ExtendUserUpgrade_UserUpgradeModel extends XFCP_ExtendUserUpgrade_UserUpgradeModel
{
public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
{
$retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);
 
$db = XenForo_Application::get('db');
 
$db->query("
UPDATE iConomy
SET Balance = Balance + 3000
WHERE username = (
SELECT username
FROM xf_user
WHERE user_id = " . $userId . "
)
");
 
return $retval;
}
 
public function downgradeUserUpgrades(array $upgrades)
 {
 parent::downgradeUserUpgrades($upgrades);
 
 if (!$upgrades)
 {
 return;
 }
 
 // YOUR STUFF HERE
 }
}

That's your skeleton. You just need to fill in your code to do whatever extra stuff you want when a user is downgraded.
This is exactly what I've been looking for. In what file should this code go into?
 
Is it possible to use a custom user group for testings purposes instead of going through the upgrade cycle? First I want to make sure that database settings are set correctly depending on wether you are in a certain user group or not. After that I will move on and test the upgrade cycle.
 
You would add the downgrade function to the extended class:

Rich (BB code):
<?php
 
class ExtendUserUpgrade_UserUpgradeModel extends XFCP_ExtendUserUpgrade_UserUpgradeModel
{
public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
{
$retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);
 
$db = XenForo_Application::get('db');
 
$db->query("
UPDATE iConomy
SET Balance = Balance + 3000
WHERE username = (
SELECT username
FROM xf_user
WHERE user_id = " . $userId . "
)
");
 
return $retval;
}
 
public function downgradeUserUpgrades(array $upgrades)
 {
 parent::downgradeUserUpgrades($upgrades);
 
 if (!$upgrades)
 {
 return;
 }
 
 // YOUR STUFF HERE
 }
}

That's your skeleton. You just need to fill in your code to do whatever extra stuff you want when a user is downgraded.
Hi Jake,
How do I get the userId variable into the piece of code below so that I can run a query to remove some record from the DB
1) I can't see to use the [user_upgrade_id] because it will have an error of undefined variable
2) The same error for userId variable.
Code:
        public function downgradeUserUpgrades(array $upgrade)
        {
                parent::downgradeUserUpgrades($upgrade);
                if (!$upgrade)
                {
                        return;
                }
// if ($upgrade['user_upgrade_id'] == 2)
        $db->query("
        DELETE FROM xf_qntta_relation
        WHERE ta_user_id = 2486
        AND student_user_id =  " . $userId . "
                ");
//}
        }
 
Refer to the default function in XenForo_Model_UserUpgrade. $upgrades is an array of upgrades. Foreach through those. Each individual record contains the user_id.
 
Refer to the default function in XenForo_Model_UserUpgrade. $upgrades is an array of upgrades. Foreach through those. Each individual record contains the user_id.
Thanks Jake.
Can you take a quick look. I got a blank error page when I tried this code.
Code:
public function downgradeUserUpgrades(array $upgrades)
     {
              parent::downgradeUserUpgrades($upgrades);
              if (!$upgrades)
              {
                        return;
                }
        foreach ($upgrades as $id)
    {
               if ($id['user_upgrade_id'] == 2)
              {
                      $db->query("
                      DELETE FROM xf_qntta_relation
                      WHERE ta_user_id = 2486
                      AND student_user_id =  " . $userId . "
                       ");
                 }
     }
}
 
Arg. Thanks very much.
Still got stuck somewhere. I will get blank page when I downgrade it, refresh the page will have this error
The requested user account upgrade could not be found.
Success!!!

I have to add the database connection to this function
$db = XenForo_Application::get('db');
 
Would you be able to help? I just changed from vb4 to xenforo and now my users won't downgrade, will that script help fix this issue? Once the user upgrade has expired they still have there secondary group ticked :(
 
Would you be able to help? I just changed from vb4 to xenforo and now my users won't downgrade, will that script help fix this issue? Once the user upgrade has expired they still have there secondary group ticked :(
That one is pretty outdated and has some things we call bad practices now.

When the user upgrade expires, it should remove that group automatically. You can use "User Group Promotions" to remove them from other associated user groups too.
 
Back
Top Bottom