View Single Post
  #9  
Old 02-12-2009, 12:22 PM
Gandalf-LoJ Gandalf-LoJ is offline
 
Join Date: Feb 2005
Location: Sussex, UK
Posts: 15
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Marco van Herwaarden View Post
Instead of doing all the str_replace's on the membergroupids, why not explode it to an array, manipulate and implode back again.

Ie. something like:

PHP Code:
// Explode to an array
$mbrgrp_array explode(","$membergroupids);
 
// Add remove/add group to array
....code here...
 
// Make it a comma seperated string again
$membergroupids implode(","$mbrgrp_array); 
Just trying to simplify your code first as that makes it easier to troubleshoot.
Thanks, didn't think of that! Does make it neater. Added and looks good on the echoed data but it's still not happy with saving the data.

--------------- Added [DATE]1234453404[/DATE] at [TIME]1234453404[/TIME] ---------------

ok, struggling with this and I think it's either because of the amount of updates or the length of the script.

I've blocked it out as explained above and set the group id's manually for each block and it works fine upto a point. Once I get to four blocks it stops working, even though the echo to screen is fine.

I'm sure this is what is happening with the array loop too. Are there any limitations to the schedule tasks at all?

--------------- Added [DATE]1234453821[/DATE] at [TIME]1234453821[/TIME] ---------------

just confirmed. Added the array loop back in with a much shorter array (two entries) and it runs perfectly.

So I'm guessing the amount of updates that the script is trying to do is overwhelming something. But what, the datamanager? or the server? There are no errors showing in the error log at all.

PHP Code:
<?php

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

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

$arr = array(    
                
=> 50,
                
=> 38    
            
);

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

foreach ($arr as $awardval => $aw2gr)
{
    
//$awardval = 3; $aw2gr = 50;
    //$aw2gr = $grouparr[$awardval];
    
echo "<br><br>Award=".$awardval.", Group=".$aw2gr;
    
// check for award and update secondary usergroup if required
    
$giveawards $vbulletin->db->query_read("
        SELECT award_id, userid 
        FROM " 
TABLE_PREFIX "award_user
        WHERE award_id = 
$awardval
    "
);
    
    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');
            echo 
"<br>Existing Groups=".$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;
            }
            echo 
"UserID=".$giveawardsrow['userid']." Membergroups=".$membergroupids;
            
$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();
                echo 
" | Account updated!";
                
$userdm->set_existing($userinfo);
                
$membergroupids $userdm->fetch_field('membergroupids');
                echo 
" | UserID=".$giveawardsrow['userid']." Membergroups=".$membergroupids;
                unset(
$mbrgrp_array);
            }
        }
        
$vbulletin->db->free_result($giveawardsrow);
    }
}

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

log_cron_action(''$nextitem1);
?>
--------------- Added [DATE]1234456496[/DATE] at [TIME]1234456496[/TIME] ---------------

/facepalm!

I am so dumb! Solved it!

ok, so this is to be a scheduled task that updates users membergroups. However, if they're already a member then there is no reason to do anything with them.

However, I totally neglected to take this into account with the query!

So, give awards query becomes;
PHP Code:
    $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
    "
); 
and now it works perfectly with a full array

well, it's been a fun afternoon, please don't laugh at me too much!
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01193 seconds
  • Memory Usage 1,849KB
  • 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
  • (3)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