1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Add-on Run query on user downgrade / expiry

Discussion in 'Resource and Add-on Requests' started by trilogy33, Sep 13, 2012.

  1. trilogy33

    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 ;)
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    You would add the downgrade function to the extended class:

    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.
     
  3. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The downgrades are run as a cron already. This is just adding stuff to be done when that cron is run.
     
    fredrikse likes this.
  4. fredrikse

    fredrikse Active Member

    This is exactly what I've been looking for. In what file should this code go into?
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    fredrikse likes this.
  6. fredrikse

    fredrikse Active Member

  7. fredrikse

    fredrikse Active Member

    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.
     
  8. Andy.N

    Andy.N Well-Known Member

    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 . "
                    ");
    //}
            }
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    Andy.N likes this.
  10. Andy.N

    Andy.N Well-Known Member

    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 . "
                           ");
                     }
         }
    }
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    $userId is undefined in your code.
     
    Andy.N likes this.
  12. Andy.N

    Andy.N Well-Known Member

    How do I get the userid of the member I'm downgrading, Jake?
    Thanks
     
  13. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    In your code it should be $id['user_id']
     
    Andy.N likes this.
  14. Andy.N

    Andy.N Well-Known Member

    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.
     
  15. Andy.N

    Andy.N Well-Known Member

    Success!!!

    I have to add the database connection to this function
    $db = XenForo_Application::get('db');
     
  16. Littlebigdunc

    Littlebigdunc Member

    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 :(
     
  17. Robust

    Robust Well-Known Member

    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.
     

Share This Page