Thread: Miscellaneous Hacks - Yet Another Awards System
View Single Post
  #515  
Old 06-25-2009, 06:14 AM
Gandalf-LoJ Gandalf-LoJ is offline
 
Join Date: Feb 2005
Location: Sussex, UK
Posts: 15
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by titodj View Post
Can you let us know how to do it?
Im trying to have a way for the mods to change a user from groups based on one award.
Sure. A few things to note. Firstly, this assumes you have a reasonable knowledge of php as they are manual files. Secondly, you need to work out what awards (specifically their ID's) need to belong to what group (again you need to know the ID's)

All the groups are set in the arrays at the top. The first column (value) in the array is the award ID. The second column (value) is the usergroup that you want to assign this award to.

The reason there are two arrays is because we have one particular award that we want to assign to two separate usergroups. This may not apply to you, if it doesn't then you can remove $arr2 and comment out the while loop.

Here is the code. I called it award2group.php and placed it in the modules folder of the forum.

PHP Code:
<?php

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!
is_object($vbulletin->db))
{
    exit;
}

// ########################### SET VARIABLES ##############################

$arr1 = array(    
                
=> 50,
                
=> 38,
                
=> 50,
                
=> 53,
                
12 => 23,
                
13 => 43,
                
14 => 43,
                
17 => 51,
                
18 => 37,
                
20 => 53,
                
21 => 53,
                
24 => 53,
                
25 => 53,
                
29 => 48,
                
31 => 40,
                
32 => 39,
                
38 => 46,
                
39 => 52    
            
);
            
$arr2 = array(    
                
=> 7    
            
);

// ########################################################################
// ######################### USERGROUP UPDATES ############################
// #########################    GIVE ACCESS    ############################
// ########################################################################

$i 1;
while (
$i <= 2)
{
    if (
$i == 1)
    {
        
$arr $arr1;
    }
    else
    {
        
$arr $arr2;
    }
    foreach (
$arr as $awardval => $aw2gr)
    {
        
// check for award and update secondary usergroup if required
        
$giveawards $vbulletin->db->query_read("
            SELECT aw.award_id, us.userid, us.membergroupids 
            FROM " 
TABLE_PREFIX "award_user as aw
            LEFT OUTER JOIN " 
TABLE_PREFIX "user AS us ON us.userid = aw.userid AND (aw.award_id = $awardval)
            WHERE FIND_IN_SET(
$aw2gr, us.membergroupids) = 0
        "
);
        
        if (
$vbulletin->db->num_rows($giveawards) > 0)
        {
            while (
$giveawardsrow $vbulletin->db->fetch_array($giveawards))
            { 
// for each award, check to see if they are a member of the relevant group and update accordingly
                
$userdm =& datamanager_init('User'$vbulletinERRTYPE_ARRAY); 
                
$userinfo fetch_userinfo($giveawardsrow['userid']);
                
$userdm->set_existing($userinfo); 
                
$membergroupids $userdm->fetch_field('membergroupids');
                if (
$membergroupids != "")
                {
                    
// Explode to an array
                    
$mbrgrp_array explode(","$membergroupids);
                    
// Add remove/add group to array
                    
$mbrgrp_array[] = $aw2gr;
                    
// Make it a comma seperated string again
                    
$membergroupids implode(","$mbrgrp_array);  
                }
                else
                {
                    
$membergroupids $aw2gr;
                }
                
$userdm->set('membergroupids'$membergroupids);
                
$userdm->pre_save();
                if (
count($userdm->errors))
                {
                    for (
$i 0$i count($userdm->errors); $i++)
                    {
                        echo 
"ERROR{$i}:{$userdm->errors[$i]}\n";
                    }
                }
                else
                {
                    
// If everything is OK
                    
$userdm->save();
                    unset(
$mbrgrp_array);
                }
            }
            
$vbulletin->db->free_result($giveawardsrow);
        }
    }
    
$i++;
}

// ########################################################################
// ######################### USERGROUP UPDATES ############################
// #########################    GIVE ACCESS    ############################
// ########################################################################

unset($giveawards$giveawardsrow$userdm$userinfo$membergroupids$awardval$aw2gr$mbrgrp_array$i);     

log_cron_action(''$nextitem1);
?>
That will assign anyone who has an award to the relevant usergroup. NOTE: It will assign the group as a SECONDARY group only. It will not change or replace the users primary group membership.

Now for the removing part. If a user has an award taken away, you will need a script to remove that user from the assigned usergroup. This is handled in this script which I called delaward2group.php and placed in the same folder as the above.

You may notice that the arrays at the top of each file matches. This is so it only removes groups that have been assigned by awards. The array could be put in a separate php file and included into the two scripts so you only have one list to maintain but that all depends on your coding method.

PHP Code:
<?php

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!
is_object($vbulletin->db))
{
    exit;
}

// ########################### SET VARIABLES ##############################

$arr1 = array(    
                
=> 50,
                
=> 38,
                
=> 50,
                
=> 53,
                
12 => 23,
                
13 => 43,
                
14 => 43,
                
17 => 51,
                
18 => 37,
                
20 => 53,
                
21 => 53,
                
24 => 53,
                
25 => 53,
                
29 => 48,
                
31 => 40,
                
32 => 39,
                
38 => 46,
                
39 => 52    
            
);
            
$arr2 = array(    
                
=> 7    
            
);

// ########################################################################
// ######################### USERGROUP UPDATES ############################
// #########################   REMOVE ACCESS   ############################
// ########################################################################

$i 1;
while (
$i <= 2)
{
    if (
$i == 1)
    {
        
$arr $arr1;
    }
    else
    {
        
$arr $arr2;
    }
    foreach (
$arr as $awardval => $aw2gr)
    {
        
// need to get the keys from the array to use as an or in the query. This will make sure that a user that has one award that belongs to a multi award group does not get removed
        
$awardkeys array_keys($arr$aw2gr);
        
$keycount count($awardkeys);
        if (
$keycount == 1)
        { 
// single key then there only needs to be one value for the query
            
$awardor $awardkeys[0];
        }
        else
        {
            for (
$ac 0$ac $keycount$ac++)
            { 
// more than one key then the query OR needs to be constructed here
                
if ($ac $keycount 1)
                {
                    
$awardor .= $awardkeys[$ac] . " OR aw.award_id = ";
                }
                else
                {
                    
$awardor .= $awardkeys[$ac];
                }
            }
        }
        unset(
$awardkeys$ac);
        
// check for groups that no longer have the award and remove them from the secondary user group
        
$delawards $vbulletin->db->query_read("
            SELECT DISTINCT us.userid, us.membergroupids
            FROM " 
TABLE_PREFIX "user AS us
            WHERE NOT EXISTS (
            SELECT aw.userid, aw.award_id
            FROM " 
TABLE_PREFIX "award_user AS aw
            WHERE (aw.award_id = 
$awardor) AND us.userid = aw.userid) AND FIND_IN_SET($aw2gr, us.membergroupids) <> 0
        "
);
        unset(
$awardor); // unset the OR query here or it'll continue to add to it on the next loop
        
if ($vbulletin->db->num_rows($delawards) > 0)
        {
            while (
$delawardsrow $vbulletin->db->fetch_array($delawards))
            { 
// remove group from users that no longer have the award
                
$userdm =& datamanager_init('User'$vbulletinERRTYPE_ARRAY); 
                
$userinfo fetch_userinfo($delawardsrow['userid']);
                
$userdm->set_existing($userinfo); 
                
$membergroupids $userdm->fetch_field('membergroupids');
                if (
$membergroupids != "")
                {
                    
// Explode to an array
                    
$mbrgrp_array explode(","$membergroupids);
                    
// Add remove/add group to array
                    
$key array_search($aw2gr$mbrgrp_array);
                    unset(
$mbrgrp_array[$key]);
                    
// Make it a comma seperated string again
                    
$membergroupids implode(","$mbrgrp_array);
                }
                
$userdm->set('membergroupids'$membergroupids);
                
$userdm->pre_save();
                if (
count($userdm->errors))
                {
                    for (
$i 0$i count($userdm->errors); $i++)
                    {
                        echo 
"ERROR{$i}:{$userdm->errors[$i]}\n";
                    }
                }
                else
                {
                    
// If everything is OK
                    
$userdm->save();
                    unset(
$mbrgrp_array);
                }
            }
            
$vbulletin->db->free_result($delawardsrow);
        }
    }
    
$i++;
}

// ########################################################################
// ######################### USERGROUP UPDATES ############################
// #########################   REMOVE ACCESS   ############################
// ########################################################################

unset($delawards$delawardsrow$userdm$userinfo$membergroupids$awardval$aw2gr$i);     

log_cron_action(''$nextitem1);
?>
All you need to do now is setup two scheduled tasks to call the two scripts. I have the award2group.php running every hour and the delaward2group.php running every 12 hours. You can choose a schedule to suit your particular needs.
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01530 seconds
  • Memory Usage 1,953KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete