Resource icon

How to extend user criteria (for use in trophies, upgrades and notices/notifications) 1.0

No permission to download

Chris D

XenForo developer
Staff member
Chris Deeming submitted a new resource:

How to extend user criteria (for use in trophies, upgrades and notices/notifications) (version 1.0) - Extend the standard user criteria used in trophies, upgrades and notices/notifications

Introduction

I recently was asked the question, how would you go about extending the criteria for awarding trophies?

I remember looking a few months back and for some reason I thought it wasn't possible. I was wrong.

It very much is possible, and actually quite straight forward. Included for download is a very small add-on that adds a very simple new criteria.

We currently have a user criteria called "User has no avatar":...

Read more about this resource...
 
nice one chris! :) so this basically can made for whatever you'd like.. "user needs avatar" ..."user needs to select gender".. etc etc and gain trophy points or whatever, with alerts...? I wanted something like this for longest time now... I'll have to try this. :)
 
Well yeah you will get alerts if the trophy is issued.

So if you're comfortable altering PHP code then you will get the result you need.
 
  • Like
Reactions: vVv
Well yeah you will get alerts if the trophy is issued.

So if you're comfortable altering PHP code then you will get the result you need.

Nice, I'm not a professional coder/PHP coder like you and most here, but if I'm shown what to do, the code, etc.. I don't mind working with it. lol.
 
Did this guide but just made the mistake of naming the file in the library/UserCriteriaUploadAvatar/ to Library.php, when it should have been Listener.php (silly me, lacking coffee / dumb blond moment I guess lmao) but it's working like charm now though. Now, need to make few more events/alerts .. then I'll be set. :D
 
Chris, in this example, will it award a trophy each time a user has uploaded an avatar? Or, only upon their first instance of uploading an avatar?
 
Well you can only be awarded a trophy once. So the question is if the trophy is taken away if you remove your avatar, I guess.

The way to test this, is if you set up a trophy for the existing "user has no avatar" criteria and remove your avatar, run the cron, then add your avatar back and run the cron again, do you lose the trophy? if you remove the avatar and then run the cron again do you get the trophy back?

If so, then the same will happen with this criteria.
 
Here are some takeaway codes for people that need extra usergroup matching. This is originally written for my addon [3.studIo] Usergroup Ranks and is released under the BSD 2-Clause modified license, but if you need this code in paid products just ask me and I'll give you permission. (Come to think of it, I might as well release this code as an independent addon. More on that later.)

The templates required is in the Usergroup Ranks XML. Too lazy to add in here.

The extra criteria covered are:
  • Usergroup matching (is/is not Primary/Display/Secondary (any)/Secondary (all)/in All usergroup(s))
  • Display Usergroup is Primary/in Secondary Usergroup(s)
  • Display Style Priority of user's Primary/Display/Secondary (any)/Secondary (all)/All usergroup(s) is higher/lower than set value
https://github.com/tyteen4a03/3ps_u...ntStudio/UsergroupRanks/Listener/Criteria.php
 
I've got this working where I can create trophies for the number of resources submitted, but is anyone able to suggest a better/cleaner way of doing this?

PHP:
<?php
 
class CustomTrophies_Listener
{
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'user_criteria_content')
        {
                $userCriteria = $template->getParam('userCriteria');
                $checked1 = $userCriteria['has_resource'] ? 'checked' : '';
                $checked2 = $userCriteria['resource_5'] ? 'checked' : '';
                $checked3 = $userCriteria['resource_10'] ? 'checked' : '';
                $checked4 = $userCriteria['resource_25'] ? 'checked' : '';
                $checked5 = $userCriteria['resource_50'] ? 'checked' : '';
                $contents .= '<li><label><input type="checkbox" name="user_criteria[has_resource][rule]" value="has_resource"' . $checked1 .' /> User has a Resource</label></li>
                <li><label><input type="checkbox" name="user_criteria[resource_5][rule]" value="resource_5"' . $checked2 .' /> User has 5 Resources</label></li>
                <li><label><input type="checkbox" name="user_criteria[resource_10][rule]" value="resource_10"' . $checked3 .' /> User has 10 Resources</label></li>
                <li><label><input type="checkbox" name="user_criteria[resource_25][rule]" value="resource_25"' . $checked4 .' /> User has 25 Resources</label></li>
                <li><label><input type="checkbox" name="user_criteria[resource_50][rule]" value="resource_50"' . $checked5 .' /> User has 50 Resources</label></li>';
        }
    }
 
    public static function criteriaUser($rule, array $data, array $user, &$returnValue)
    {
        switch ($rule)
        {
                case 'has_resource':
                        if (!empty($user['resource_count']))
                        {
                        $returnValue = true;
                        }
                break;
 
                case 'resource_5':
                        if (!empty($user['resource_count']) && ($user['resource_count'] >= 5))
                        {
                        $returnValue = true;
                        }
                break;
 
                case 'resource_10':
                        if (!empty($user['resource_count']) && ($user['resource_count'] >= 10))
                        {
                        $returnValue = true;
                        }
                break;
 
                case 'resource_25':
                        if (!empty($user['resource_count']) && ($user['resource_count'] >= 25))
                        {
                        $returnValue = true;
                        }
                break;
 
                case 'resource_50':
                        if (!empty($user['resource_count']) && ($user['resource_count'] >= 50))
                        {
                        $returnValue = true;
                        }
                break;
 
       
        }
    }
}
 
Failed trying to follow your tutorial Chris. I want to give people a trophy point or two for adding an avatar.
So I followed your instructions.
temp1.webp
Created the Listener.php and saved it in library/AVForums/UserCriteriaHasAvatar/Listener.php
And Listener.php looks like this:
PHP:
<?php

class UserCriteriaHasAvatar_Listener
{
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'user_criteria_profile')
      {
            $userCriteria = $template->getParam('userCriteria');
            $checked = $userCriteria['has_avatar'] ? 'checked' : '';
            $contents .= '<li><label><input type="checkbox" name="user_criteria[has_avatar][rule]" value="has_avatar"' . $checked .' /> User has an avatar</label></li>';
        }
    }
}

?>
Uploaded the file to the sandbox.

When I try to add the event listener, I get this error:
temp2.webp
What am I doing wrong?
 
Your callback class needs to be named the same as your Listener PHP class:

UserCriteriaHasAvatar_Listener
 
Top Bottom