PHP Criteria

PHP Criteria 1.0.0

No permission to download

CMTV

Well-known member
CMTV submitted a new resource:

PHP Criteria - Power up trophy/promotion system with custom PHP criteria!

View attachment 163095
This addon allows you to use custom PHP code as a criteria in order to create advanced trophies or user grop promotions.

For example, you can use:
  • Count the number of threads the user has created in specific forum
  • Analyze users signatures
  • Count the number of likes on single messages
  • Retreive data from addons (like Resource Manager or Question Threads)
  • See how many...

Read more about this resource...
 
Hi @CMTV
Thanks for your addon. I find it quite interesting, however, I'm not really confident managing php.
I'd liked this addon: https://xenforo.com/community/threads/cta-criteria.70877/ (but doesn't work for XF2).
What I liked most is the ability to promote/demote users depending on their activity.
In my case, users with more than X posts in last Y days are "VIP" members with premium access:
1527028927867.webp
Just checked the examples. Can you add this to the examples?
It would be great if you can add this through admin panel instead of php, but that's secondary.
Thanks!
 
Hi @CMTV
Thanks for your addon. I find it quite interesting, however, I'm not really confident managing php.
I'd liked this addon: https://xenforo.com/community/threads/cta-criteria.70877/ (but doesn't work for XF2).
What I liked most is the ability to promote/demote users depending on their activity.
In my case, users with more than X posts in last Y days are "VIP" members with premium access:
View attachment 176118
Just checked the examples. Can you add this to the examples?
It would be great if you can add this through admin panel instead of php, but that's secondary.
Thanks!

I'm also interested in this example.
Thanks
 
@Breixo, @Nergizo, @dhormigo here is the function to award user with trophy if he posted X messages in last Y days:
PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();
   
    // Last x days in which user posts will be counted
    $days = 10;
   
    // The number of messages to achieve the trophy
    $messages = 50;
   
    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_post` WHERE `user_id` = ? AND `post_date` > ?";
   
    // Retrieving the number of posts in last "$days" days
    $posts = $db->fetchOne($query, [$user->user_id, time() - ($days * 24 * 60 * 60)]);
   
    if(is_int($posts))
    {
        // Returning true if user has posted "$messages" or more posts in last "$days" days
        return ($posts >= $messages);
    }
    else
    {
        return false;
    }
}

Paste this code in PHPCriteria.php in src/addons directory. Dont' forget to set your own values for $days and $meesages variables.

When creating a trophy on "PHP callback" tab set "Class" to PHPCriteria and "Method" to trophy_ActiveUser.
 
@maszd, Basically the code is almost the same. The only thing is to use a diffenet table: xf_thread:

PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();

    // Last x days in which user posts will be counted
    $days = 10;

    // The number of messages to achieve the trophy
    $threads = 50;

    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ? AND `post_date` > ?";

    // Retrieving the number of threads in last "$days" days
    $threadsNum = $db->fetchOne($query, [$user->user_id, time() - ($days * 24 * 60 * 60)]);

    if(is_int($threadsNum))
    {
        // Returning true if user has posted "$threads" or more threads in last "$days" days
        return ($threadsNum >= $threads);
    }
    else
    {
        return false;
    }
}
 
last question, can i remove this line?
PHP:
    // Last x days in which user posts will be counted
    $days = 10;

iam only need user thread count created without last Y day criteria.
thanks.
 
This is the correct code:
PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();

    // The number of messages to achieve the trophy
    $threads = 50;

    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

    // Retrieving the number of threads in last "$days" days
    $threadsNum = $db->fetchOne($query, [$user->user_id]);

    if(is_int($threadsNum))
    {
        // Returning true if user has posted "$threads" or more threads
        return ($threadsNum >= $threads);
    }
    else
    {
        return false;
    }
}
 
This is the correct code:
thank you! working perfect :) this is my criteria:
PHP:
<?php

class PHPCriteria
{
    /**
     * "Thread Starter Level 1"
     *
     * User has created 10 thread
     *
     * @param \XF\App $app
     * @param \XF\Entity\User $user
     *
     * @return bool
     */
    public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
    {
        // Getting the database
        $db = $app->db();

        // The number of messages to achieve the trophy
        $threads = 10;

        // Database query for selecting the number of posts of given user after given date
        $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

        // Retrieving the number of threads in last "$days" days
        $threadsNum = $db->fetchOne($query, [$user->user_id]);

        if(is_int($threadsNum))
        {
            // Returning true if user has posted "$threads" or more threads
            return ($threadsNum >= $threads);
        }
        else
        {
            return false;
        }
    }

    /**
     * "Thread Starter Level 2"
     *
     * User has created 30 thread
     *
     * @param \XF\App $app
     * @param \XF\Entity\User $user
     *
     * @return bool
     */
    public static function trophy_MostUser(\XF\App $app, \XF\Entity\User $user)
    {
        // Getting the database
        $db = $app->db();

        // The number of messages to achieve the trophy
        $threads = 30;

        // Database query for selecting the number of posts of given user after given date
        $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

        // Retrieving the number of threads in last "$days" days
        $threadsNum = $db->fetchOne($query, [$user->user_id]);

        if(is_int($threadsNum))
        {
            // Returning true if user has posted "$threads" or more threads
            return ($threadsNum >= $threads);
        }
        else
        {
            return false;
        }
    }

    /**
     * "Thread Starter Level 3"
     *
     * User has created 50 thread
     *
     * @param \XF\App $app
     * @param \XF\Entity\User $user
     *
     * @return bool
     */
    public static function trophy_BestUser(\XF\App $app, \XF\Entity\User $user)
    {
        // Getting the database
        $db = $app->db();

        // The number of messages to achieve the trophy
        $threads = 50;

        // Database query for selecting the number of posts of given user after given date
        $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

        // Retrieving the number of threads in last "$days" days
        $threadsNum = $db->fetchOne($query, [$user->user_id]);

        if(is_int($threadsNum))
        {
            // Returning true if user has posted "$threads" or more threads
            return ($threadsNum >= $threads);
        }
        else
        {
            return false;
        }
    }
    
    /**
     * "Thread Starter Level 4"
     *
     * User has created 100 thread
     *
     * @param \XF\App $app
     * @param \XF\Entity\User $user
     *
     * @return bool
     */
    public static function trophy_MasterUser(\XF\App $app, \XF\Entity\User $user)
    {
        // Getting the database
        $db = $app->db();

        // The number of messages to achieve the trophy
        $threads = 100;

        // Database query for selecting the number of posts of given user after given date
        $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

        // Retrieving the number of threads in last "$days" days
        $threadsNum = $db->fetchOne($query, [$user->user_id]);

        if(is_int($threadsNum))
        {
            // Returning true if user has posted "$threads" or more threads
            return ($threadsNum >= $threads);
        }
        else
        {
            return false;
        }
    }
}
 
This can be further simplified to avoid duplications:
PHP:
<?php

class PHPCriteria
{
    static function trophyPreset_threadsNumber($threads_num, $app, $user)
    {
        // Getting the database
        $db = $app->db();

        // Database query for selecting the number of posts of given user after given date
        $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ?";

        // Retrieving the number of threads in last "$days" days
        $threadsNum = $db->fetchOne($query, [$user->user_id]);

        if(is_int($threadsNum))
        {
            // Returning true if user has posted "$threads" or more threads
            return ($threadsNum >= $threads_num);
        }
        else
        {
            return false;
        }
    }

    public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
    {
        return self::trophyPreset_threadsNumber(10, $app, $user);
    }

    public static function trophy_MostUser(\XF\App $app, \XF\Entity\User $user)
    {
        return self::trophyPreset_threadsNumber(30, $app, $user);
    }

    public static function trophy_BestUser(\XF\App $app, \XF\Entity\User $user)
    {
        return self::trophyPreset_threadsNumber(50, $app, $user);
    }
   
    public static function trophy_MasterUser(\XF\App $app, \XF\Entity\User $user)
    {
        return self::trophyPreset_threadsNumber(100, $app, $user);
    }
}
 
Last edited:
Maybe add those PHP Codes as plugin on Resource FAQ section/tab with description so others can utilize it also :).
Thanks!
 
Last edited:
@Breixo, @Nergizo, @dhormigo here is the function to award user with trophy if he posted X messages in last Y days:
PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();
  
    // Last x days in which user posts will be counted
    $days = 10;
  
    // The number of messages to achieve the trophy
    $messages = 50;
  
    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_post` WHERE `user_id` = ? AND `post_date` > ?";
  
    // Retrieving the number of posts in last "$days" days
    $posts = $db->fetchOne($query, [$user->user_id, time() - ($days * 24 * 60 * 60)]);
  
    if(is_int($posts))
    {
        // Returning true if user has posted "$messages" or more posts in last "$days" days
        return ($posts >= $messages);
    }
    else
    {
        return false;
    }
}

Paste this code in PHPCriteria.php in src/addons directory. Dont' forget to set your own values for $days and $meesages variables.

When creating a trophy on "PHP callback" tab set "Class" to PHPCriteria and "Method" to trophy_ActiveUser.
Great! Thanks so much!
Just tested and it works correctly for user group promotion.
So now, everytime a user writes 50 messages in 10 days (example), he is promoted to VIP group.
I wonder now... if next week this user in VIP group writes less than 50 posts, it is automatically demoted/removed from VIP group?

If not... Is there a way around to solve this? Thinking about another User group promotion with oposite criteria PHPcriteria (if in VIP but less than 50messages... to "Normal" usergroup).
Thanks for your help!
 
  • Like
Reactions: rdn
@Breixo, @Nergizo, @dhormigo here is the function to award user with trophy if he posted X messages in last Y days:
PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();

    // Last x days in which user posts will be counted
    $days = 10;

    // The number of messages to achieve the trophy
    $messages = 50;

    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_post` WHERE `user_id` = ? AND `post_date` > ?";

    // Retrieving the number of posts in last "$days" days
    $posts = $db->fetchOne($query, [$user->user_id, time() - ($days * 24 * 60 * 60)]);

    if(is_int($posts))
    {
        // Returning true if user has posted "$messages" or more posts in last "$days" days
        return ($posts >= $messages);
    }
    else
    {
        return false;
    }
}

Paste this code in PHPCriteria.php in src/addons directory. Dont' forget to set your own values for $days and $meesages variables.

When creating a trophy on "PHP callback" tab set "Class" to PHPCriteria and "Method" to trophy_ActiveUser.

@maszd, Basically the code is almost the same. The only thing is to use a diffenet table: xf_thread:

PHP:
public static function trophy_ActiveUser(\XF\App $app, \XF\Entity\User $user)
{
    // Getting the database
    $db = $app->db();

    // Last x days in which user posts will be counted
    $days = 10;

    // The number of messages to achieve the trophy
    $threads = 50;

    // Database query for selecting the number of posts of given user after given date
    $query = "SELECT COUNT(*) FROM `xf_thread` WHERE `user_id` = ? AND `post_date` > ?";

    // Retrieving the number of threads in last "$days" days
    $threadsNum = $db->fetchOne($query, [$user->user_id, time() - ($days * 24 * 60 * 60)]);

    if(is_int($threadsNum))
    {
        // Returning true if user has posted "$threads" or more threads in last "$days" days
        return ($threadsNum >= $threads);
    }
    else
    {
        return false;
    }
}

Seems missing: AND message_state = 'visible' & AND discussion_state = 'visible'?
 
Last edited:
Top Bottom