[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/tasks/ -> promotions.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: promotions.php 5828 2012-05-08 16:06:16Z Tomm $
  10   */
  11  
  12  function task_promotions($task)
  13  {
  14      global $mybb, $db, $lang, $cache;
  15      
  16      $usergroups = $cache->read("usergroups");
  17      // Iterate through all our promotions
  18      $query = $db->simple_select("promotions", "*", "enabled = '1'");
  19      while($promotion = $db->fetch_array($query))
  20      {
  21          // Does the destination usergroup even exist?? If it doesn't and it moves a user to it, the user will get PHP errors.
  22          if(!array_key_exists($promotion['newusergroup'], $usergroups))
  23          {
  24              // Instead of just skipping this promotion, disable it to stop it even being selected when this task is run.
  25              $update = array(
  26                  "enabled" => 0
  27              );
  28              $db->update_query("promotions", $update, "pid = '" . intval($promotion['pid']) . "'");
  29              continue;
  30          }
  31          
  32          $and = "";
  33          $sql_where = "";
  34          
  35          // Based on the promotion generate criteria for user selection
  36          $requirements = explode(',', $promotion['requirements']);
  37          if(in_array('postcount', $requirements) && intval($promotion['posts']) >= 0 && !empty($promotion['posttype']))
  38          {
  39              $sql_where .= "{$and}postnum {$promotion['posttype']} '{$promotion['posts']}'";
  40              
  41              $and = " AND ";
  42          }
  43          
  44          if(in_array('reputation', $requirements) && !empty($promotion['reputationtype']))
  45          {
  46              $sql_where .= "{$and}reputation {$promotion['reputationtype']} '{$promotion['reputations']}'";
  47              
  48              $and = " AND ";
  49          }
  50          
  51          if(in_array('referrals', $requirements) && intval($promotion['referrals']) >= 0 && !empty($promotion['referralstype']))
  52          {
  53              $sql_where .= "{$and}referrals {$promotion['referralstype']} '{$promotion['referrals']}'";
  54              
  55              $and = " AND ";
  56          }
  57          
  58          if(in_array('timeregistered', $requirements) && intval($promotion['registered']) > 0 && !empty($promotion['registeredtype']))
  59          {
  60              switch($promotion['registeredtype'])
  61              {
  62                  case "hours":
  63                      $regdate = $promotion['registered']*60*60;
  64                      break;
  65                  case "days":
  66                      $regdate = $promotion['registered']*60*60*24;
  67                      break;
  68                  case "weeks":
  69                      $regdate = $promotion['registered']*60*60*24*7;
  70                  case "months":
  71                      $regdate = $promotion['registered']*60*60*24*30;
  72                      break;
  73                  case "years":
  74                      $regdate = $promotion['registered']*60*60*24*365;
  75                      break;
  76                  default:
  77                      $regdate = $promotion['registered']*60*60*24;
  78              }
  79              $sql_where .= "{$and}regdate <= '".(TIME_NOW-$regdate)."'";
  80              $and = " AND ";
  81          }
  82          
  83          if(!empty($promotion['originalusergroup']) && $promotion['originalusergroup'] != '*')
  84          {
  85              $sql_where .= "{$and}usergroup IN ({$promotion['originalusergroup']})";
  86                  
  87              $and = " AND ";
  88          }
  89          
  90          if(!empty($promotion['newusergroup']))
  91          {
  92              $sql_where .= "{$and}usergroup != '{$promotion['newusergroup']}'";
  93                      
  94              $and = " AND ";
  95          }
  96          
  97          $sql_where .= "{$and}lastactive >= '{$task['lastrun']}'";
  98          
  99          $uid = array();
 100          $log_inserts = array();
 101          
 102          if($promotion['usergrouptype'] == "secondary")
 103          {
 104              $usergroup_select = "additionalgroups";
 105          }
 106          else
 107          {
 108              $usergroup_select = "usergroup";
 109          }
 110          
 111          $query2 = $db->simple_select("users", "uid,{$usergroup_select}", $sql_where);
 112          while($user = $db->fetch_array($query2))
 113          {
 114              // super admin check?
 115              if($usergroup_select == "additionalgroups")
 116              {
 117                  $log_inserts[] = array(
 118                      'pid' => $promotion['pid'],
 119                      'uid' => $user['uid'],
 120                      'oldusergroup' => $user['additionalgroups'],
 121                      'newusergroup' => $promotion['newusergroup'],
 122                      'dateline' => TIME_NOW,
 123                      'type' => "secondary",
 124                  );
 125              }
 126              else
 127              {
 128                  $log_inserts[] = array(
 129                      'pid' => $promotion['pid'],
 130                      'uid' => $user['uid'],
 131                      'oldusergroup' => $user['usergroup'],
 132                      'newusergroup' => $promotion['newusergroup'],
 133                      'dateline' => TIME_NOW,
 134                      'type' => "primary",
 135                  );
 136              }
 137          
 138              $uids[] = $user['uid'];
 139              
 140              
 141              if($usergroup_select == "additionalgroups")
 142              {
 143                  if(join_usergroup($user['uid'], $promotion['newusergroup']) === false)
 144                  {
 145                      // Did the user already have the additional usergroup?
 146                      array_pop($log_inserts);
 147                      array_pop($uids);
 148                  }
 149              }
 150              
 151              if((count($uids) % 20) == 0)
 152              {
 153                  if($usergroup_select == "usergroup")
 154                  {
 155                      $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(".implode(",", $uids).")");
 156                  }
 157                  
 158                  if(!empty($log_inserts))
 159                  {
 160                      $db->insert_query_multiple("promotionlogs", $log_inserts);
 161                  }
 162                  
 163                  $uids = array();
 164                  $log_inserts = array();
 165              }
 166          }
 167          
 168          if(count($uids) > 0)
 169          {
 170              if($usergroup_select == "usergroup")
 171              {
 172                  $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(".implode(",", $uids).")");
 173              }
 174              
 175              if(!empty($log_inserts))
 176              {
 177                  $db->insert_query_multiple("promotionlogs", $log_inserts);
 178              }
 179                  
 180              $uids = array();
 181              $log_inserts = array();
 182          }
 183      }
 184      
 185      $cache->update_moderators();
 186      
 187      add_task_log($task, $lang->task_promotions_ran);
 188  }
 189  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1