Guest User

Cron2

a guest
Jun 21st, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.95 KB | None | 0 0
  1. <?php
  2.  
  3. // Created by DrMath AKA James Will
  4. // ######################## SET PHP ENVIRONMENT ###########################
  5. error_reporting(E_ALL & ~E_NOTICE);
  6. if (!is_object($vbulletin->db)) {
  7.     exit;
  8. }
  9.  
  10. // ########################################################################
  11. // ######################### START MAIN SCRIPT ############################
  12. // ########################################################################
  13. // $nextrun is the time difference between runs. Should be sent over from cron.php!!
  14. // We only check the users that have been active since the lastrun to save a bit of cpu time.
  15.  
  16. $thisrun = TIMENOW;
  17. $lastrun = $vbulletin->crondata['promos_lastrun'] ? $vbulletin->crondata['promos_lastrun'] : $thisrun - ($nextrun - $thisrun);
  18.  
  19. // Define Basic Membergroups
  20. $membergroups = array(2, 35, 51, 52, 53);
  21.  
  22. // Define Usergroup Levels
  23. $grouplevels = array();
  24. // Group ID, Required Points, Rank Image Source
  25. $grouplevels[] = array(35, 3, "<img src='images/ranks/current/mem_stea.png' alt='Steadfast Member' />", "Steadfast Member");
  26. $grouplevels[] = array(51, 8, "<img src='images/ranks/current/mem_seni.png' alt='Senior Member' />", "Senior Member");
  27. $grouplevels[] = array(52, 15, "<img src='images/ranks/current/mem_reno.png' alt='Renowned Member' />", "Renowned Member");
  28. $grouplevels[] = array(53, 25, "<img src='images/ranks/current/mem_visi.png' alt='Visionary Member' />", "Visionary Member");
  29.  
  30. // Get list of userids
  31. $userid_q = $vbulletin->db->query_read("SELECT userid FROM " . TABLE_PREFIX . "user WHERE usergroupid NOT IN (3,4,5,20,21,23,45)");
  32. while ($row = mysql_fetch_array($userid_q, MYSQL_ASSOC)) {
  33.     $userids[] = $row["userid"];
  34. }
  35. // Gather the Info
  36.  
  37. //$totalpromos = 0;
  38.  
  39. foreach ($userids as $userid) {
  40.  
  41.     // Query DB for Post Content //
  42.     $threads_q = $vbulletin->db->query_read("SELECT COUNT(*) FROM " . TABLE_PREFIX . "thread WHERE postuserid = $userid AND forumid NOT IN (13, 19, 28, 43, 47, 49, 71, 86, 126, 141,162, 167, 168, 178, 180, 181, 219, 222, 233, 237, 238, 239, 243, 254, 255, 256, 259, 267, 284, 285, 287, 301, 304, 306, 307, 312, 313, 314, 316, 319, 337, 338, 345, 346, 375, 381, 384, 385, 391, 396, 402)");
  43.     $recruits_q = $vbulletin->db->query_read("SELECT COUNT(*) FROM " . TABLE_PREFIX . "user WHERE referrerid = $userid");
  44.     $awards_q = $vbulletin->db->query_read("SELECT COUNT(*) FROM " . TABLE_PREFIX . "award_user WHERE userid = $userid AND award_id NOT IN (6, 7, 10, 20)");
  45.     $info_q = $vbulletin->db->query_read("SELECT user.username, user.posts, user.joindate, user.usergroupid, user.membergroupids, userfield.field49, userfield.field50 FROM " . TABLE_PREFIX . "user as user LEFT JOIN " . TABLE_PREFIX . "userfield as userfield ON (userfield.userid=user.userid) WHERE user.userid = $userid");
  46.  
  47.  
  48.     // Transfer to Arrays
  49.     $u_threads = $vbulletin->db->fetch_array($threads_q);
  50.     $u_recruits = $vbulletin->db->fetch_array($recruits_q);
  51.     $u_awards = $vbulletin->db->fetch_array($awards_q);
  52.     $u_info = $vbulletin->db->fetch_array($info_q);
  53.  
  54.     $posts = $u_info[1];
  55.     $joindate = new DateTime(date("Y-m-d", $u_info[2]));
  56.     $currentdate = new DateTime();
  57.     $displaydate = vbdate($vbulletin->options['dateformat'], $u_info[2], true);
  58.     //date
  59.     $datediff = $currentdate->diff($joindate);
  60.     $datediffm = $datediff->format('%m');
  61.     $datediffy = $datediff->format('%y');
  62.     $datediff = $datediffm + (12 * $datediffy);
  63.     //end date
  64.     $usergroup = $u_info[3];
  65.     $othergroups = $u_info[4];
  66.     $staffyesno = $u_info[5];
  67.     $adminyesno = $u_info[6];
  68.     $threads = $u_threads[0];
  69.     $recruits = $u_recruits[0];
  70.     $awards = $u_awards[0];
  71.     $totalpoints = 0;
  72.  
  73.     // Post Points
  74.  
  75.     if ($posts >= 10) {
  76.         $totalpoints += 1;
  77.     }
  78.     if ($posts >= 25) {
  79.         $totalpoints += 1;
  80.     }
  81.     if ($posts >= 50) {
  82.         $totalpoints += 1;
  83.     }
  84.     if ($posts >= 100) {
  85.         $totalpoints += 1;
  86.     }
  87.     if ($posts >= 300) {
  88.         $totalpoints += 5;
  89.     }
  90.     if ($posts >= 500) {
  91.         $ppoints = 2 * floor($posts / 500);
  92.         $totalpoints += $ppoints;
  93.     }
  94.  
  95.     // Thread Points
  96.  
  97.     if ($threads >= 10) {
  98.         $totalpoints += 1;
  99.     }
  100.     if ($threads >= 20) {
  101.         $totalpoints += 1;
  102.     }
  103.     if ($threads >= 50) {
  104.         $totalpoints += 2;
  105.     }
  106.     if ($threads >= 100) {
  107.         $tpoints = floor($threads / 100);
  108.         $totalpoints += $tpoints;
  109.     }
  110.  
  111.     // Recruited Member Points
  112.  
  113.     if ($recruits >= 1 && $recruits <= 4) {
  114.         $totalpoints += $recruits;
  115.     } elseif ($recruits >= 5) {
  116.         $r5points = floor($recruits / 5);
  117.         $totalpoints += (4 + $r5points);
  118.     }
  119.  
  120.     // Membership Length Points
  121.  
  122.     if ($datediff >= 1) {
  123.         $totalpoints += 1;
  124.     }
  125.     if ($datediff >= 3) {
  126.         $totalpoints += 1;
  127.     }
  128.     if ($datediff >= 6) {
  129.         $totalpoints += 2;
  130.     }
  131.     if ($datediff >= 12) {
  132.         $totalpoints += 5;
  133.     }
  134.     if ($datediff >= 18) {
  135.         $m6ppoints = 3 * floor(($datediff - 12) / 6);
  136.         $totalpoints += $m6ppoints;
  137.     }
  138.  
  139.     // Award Points
  140.  
  141.     if ($awards > 0) {
  142.         $apoints = 2 * $awards;
  143.         $totalpoints += $apoints;
  144.     }
  145.  
  146.     // Points for Positions
  147.  
  148.     if ($staffyesno == "Yes") {
  149.         $staffpts = $totalpoints += 2;
  150.     }
  151.     if ($adminyesno == "Yes") {
  152.         $adminpts = $totalpoints += 5;
  153.     }
  154.  
  155.     // Begin Promotions
  156.     $newgroup = $newrank = $newranktitle = "";
  157.  
  158.     // If they are not in leadership
  159.     if (in_array($usergroup, $membergroups)) {
  160.         // Go Through Each Rank and Check if They Can Achieve It
  161.         foreach ($grouplevels as $level) {
  162.             $groupid = $level[0];
  163.             $reqpoints = $level[1];
  164.             $rankcode = $level[2];
  165.             $ranktitle = $level[3];
  166.  
  167.             // If they have points to rank up
  168.             if ($totalpoints >= $reqpoints && $usergroup < $groupid) {
  169.                 $newgroup = $groupid;
  170.                 $newrank = $rankcode;
  171.                 $newranktitle = $ranktitle;
  172.             }
  173.  
  174.             // Update Chron Log
  175.             $log = array($newgroup, '*', $newranktitle);
  176.             // the "1" indicates to use the second line of the phrase specified for this task
  177.             log_cron_action(serialize($log), $nextitem, 1);
  178.         }
  179.  
  180.         // If they earned a new usergroup, change it
  181.         if ($newgroup != null) {
  182.             $userinfo = fetch_userinfo($userid);
  183.             $userdata = & datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
  184.             $userdata->set_existing($userinfo);
  185.             $userdata->setr('usergroupid', $newgroup);
  186.             $userdata->setr('usertitle', $newrank);
  187.             $userdata->save();
  188.             //$totalpromos ++;
  189.         }
  190.     } elseif (in_array($usergroup, $membergroups) == false) {
  191.         // If they are in leadership, check additional groups
  192.         // If no additional usergroup, set apprentice
  193.         if ($othergroups == null) {
  194.             $othergroups = "2";
  195.         }
  196.  
  197.         foreach ($membergroups as $groupid) {
  198.             $group = (string) $groupid;
  199.             if (strpos($othergroups, $group) !== false) {
  200.                 $secgroup = $group;
  201.                 $secgroupid = $groupid;
  202.                 break;
  203.             }
  204.         }
  205.  
  206.         // Go Through Each Rank and Check if They Can Achieve It
  207.         foreach ($grouplevels as $level) {
  208.             $groupid = $level[0];
  209.             $reqpoints = $level[1];
  210.             $ranktitle = $level[3];
  211.  
  212.             // If they have points to rank up
  213.             if ($totalpoints >= $reqpoints && $secgroupid < $groupid) {
  214.                 $newgroup = (string) $groupid;
  215.                 $newranktitle = $ranktitle;
  216.             }
  217.  
  218.             $log = array($newgroup, '*', $newranktitle);
  219.             // the "1" indicates to use the second line of the phrase specified for this task
  220.             log_cron_action(serialize($log), $nextitem, 1);
  221.         }
  222.  
  223.         $othergroups = str_replace($secgroup, $newgroup, $othergroups);
  224.  
  225.         // If they earned a new usergroup, change it
  226.         if ($newgroup != null) {
  227.             $userinfo = fetch_userinfo($userid);
  228.             $userdata = & datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
  229.             $userdata->set_existing($userinfo);
  230.             $userdata->setr('membergroupids', $othergroups);
  231.             $userdata->save();
  232.             //$totalpromos ++;
  233.         }
  234.     }
  235. }
  236.  
  237. $vbulletin->crondata['promos_lastrun'] = $thisrun;
  238. build_datastore('crondata', serialize($vbulletin->crondata), 1);
  239.  
  240.  
  241. if ($vbulletin->debug AND VB_AREA == 'AdminCP') {
  242.     #echo '<pre>'; print_r($sql); print_r($sql_id); echo '</pre>';
  243. }
  244.  
  245.  
  246. /* ======================================================================*\
  247.   || ####################################################################
  248.   || # Updated, Friday, June 20, 2014
  249.   || ####################################################################
  250.   \*====================================================================== */
  251. ?>
Advertisement
Add Comment
Please, Sign In to add comment