Guest User

Untitled

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