Quote:
Originally Posted by Marco van Herwaarden
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(
3 => 50,
5 => 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', $vbulletin, ERRTYPE_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('', $nextitem, 1);
?>
--------------- 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!