I have recently done some neat little features for my community which may benefit others. I have currently done all the hard coding myself and have done certain features how I want them. I was wondering if anyone could point me towards an article on making a plugin or maybe walk me through it? I'd love to give back to the community in some way! Thanks!
In addition to the above request, I have an issue with a CRON task I am trying to do. It isn't being logged, and I'm not even sure that it is actually doing the process each night. Any help would be great!
PHP Code:
<?php
// Created by DrMath AKA James Will
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db)) {
exit;
}
// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################
// $nextrun is the time difference between runs. Should be sent over from cron.php!!
// We only check the users that have been active since the lastrun to save a bit of cpu time.
$thisrun = TIMENOW;
$lastrun = $vbulletin->crondata['promos_lastrun'] ? $vbulletin->crondata['promos_lastrun'] : $thisrun - ($nextrun - $thisrun);
// Define Basic Membergroups
$membergroups = array(2, 35, 51, 52, 53);
$leadergroups = array(5, 6, 17, 18, 24, 38, 50, 54, 55, 56, 58, 62);
// Define Usergroup Levels
$grouplevels = array();
// Group ID, Required Points, Rank Image Source
$grouplevels[] = array(35, 3, "<img src='images/ranks/v9/mem_stea.png' alt='Steadfast Member' />", "Steadfast Member");
$grouplevels[] = array(51, 8, "<img src='images/ranks/v9/mem_seni.png' alt='Senior Member' />", "Senior Member");
$grouplevels[] = array(52, 15, "<img src='images/ranks/v9/mem_reno.png' alt='Renowned Member' />", "Renowned Member");
$grouplevels[] = array(53, 25, "<img src='images/ranks/v9/mem_visi.png' alt='Visionary Member' />", "Visionary Member");
// Get list of userids
$userid_q = $vbulletin->db->query_read("SELECT userid FROM " . TABLE_PREFIX . "user WHERE usergroupid NOT IN (3,4,5,20,21,23,45)");
while ($row = mysql_fetch_array($userid_q, MYSQL_ASSOC)) {
$userids[] = $row["userid"];
}
// Gather the Info
//$totalpromos = 0;
foreach ($userids as $userid) {
// Query DB for Post Content //
$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)");
$recruits_q = $vbulletin->db->query_read("SELECT COUNT(*) FROM " . TABLE_PREFIX . "user WHERE referrerid = $userid");
$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, 23)");
$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");
// Transfer to Arrays
$u_threads = $vbulletin->db->fetch_array($threads_q);
$u_recruits = $vbulletin->db->fetch_array($recruits_q);
$u_awards = $vbulletin->db->fetch_array($awards_q);
$u_info = $vbulletin->db->fetch_array($info_q);
$posts = $u_info[1];
$joindate = new DateTime(date("Y-m-d", $u_info[2]));
$currentdate = new DateTime();
$displaydate = vbdate($vbulletin->options['dateformat'], $u_info[2], true);
//date
$datediff = $currentdate->diff($joindate);
$datediffm = $datediff->format('%m');
$datediffy = $datediff->format('%y');
$datediff = $datediffm + (12 * $datediffy);
//end date
$usergroup = $u_info[3];
$othergroups = $u_info[4];
$staffyesno = $u_info[5];
$adminyesno = $u_info[6];
$threads = $u_threads[0];
$recruits = $u_recruits[0];
$awards = $u_awards[0];
$totalpoints = 0;
// Post Points
if ($posts >= 10) {
$totalpoints += 1;
}
if ($posts >= 25) {
$totalpoints += 1;
}
if ($posts >= 50) {
$totalpoints += 1;
}
if ($posts >= 100) {
$totalpoints += 1;
}
if ($posts >= 300) {
$totalpoints += 5;
}
if ($posts >= 500) {
$ppoints = 2 * floor($posts / 500);
$totalpoints += $ppoints;
}
// Thread Points
if ($threads >= 10) {
$totalpoints += 1;
}
if ($threads >= 20) {
$totalpoints += 1;
}
if ($threads >= 50) {
$totalpoints += 2;
}
if ($threads >= 100) {
$tpoints = floor($threads / 100);
$totalpoints += $tpoints;
}
// Recruited Member Points
if ($recruits >= 1 && $recruits <= 4) {
$totalpoints += $recruits;
} elseif ($recruits >= 5) {
$r5points = floor($recruits / 5);
$totalpoints += (4 + $r5points);
}
// Membership Length Points
if ($datediff >= 1) {
$totalpoints += 1;
}
if ($datediff >= 3) {
$totalpoints += 1;
}
if ($datediff >= 6) {
$totalpoints += 2;
}
if ($datediff >= 12) {
$totalpoints += 5;
}
if ($datediff >= 18) {
$m6ppoints = 3 * floor(($datediff - 12) / 6);
$totalpoints += $m6ppoints;
}
// Award Points
if ($awards > 0) {
$apoints = 2 * $awards;
$totalpoints += $apoints;
}
// Points for Positions
if ($staffyesno == "Yes") {
$staffpts = $totalpoints += 2;
}
if ($adminyesno == "Yes") {
$adminpts = $totalpoints += 5;
}
// Begin Promotions
$newgroup = $newrank = $newranktitle = "";
// If they are not in leadership
if (in_array($usergroup, $membergroups)) {
// Go Through Each Rank and Check if They Can Achieve It
foreach ($grouplevels as $level) {
$groupid = $level[0];
$reqpoints = $level[1];
$rankcode = $level[2];
$ranktitle = $level[3];
// If they have points to rank up
if ($totalpoints >= $reqpoints && $usergroup < $groupid) {
$newgroup = $groupid;
$newrank = $rankcode;
$newranktitle = $ranktitle;
}
// Update Chron Log
$log = array($newgroup, '*', $newranktitle);
// the "1" indicates to use the second line of the phrase specified for this task
log_cron_action(serialize($log), $nextitem, 1);
}
// If they earned a new usergroup, change it
if ($newgroup != null) {
$userinfo = fetch_userinfo($userid);
$userdata = & datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->set_existing($userinfo);
$userdata->setr('usergroupid', $newgroup);
$userdata->setr('usertitle', $newrank);
$userdata->save();
//$totalpromos ++;
}
}
// If they are in leadership, check additional groups
if (in_array($usergroup, $leadergroups)) {
// If no additional usergroup, set apprentice
if ($othergroups == null) {
$othergroups = "2";
}
foreach ($membergroups as $gid) {
$group = (string) $gid;
if (in_array($group, explode(",",$othergroups)) !== false) {
$secgroup = $group;
$secgroupid = $gid;
break;
}
}
// Go Through Each Rank and Check if They Can Achieve It
foreach ($grouplevels as $level) {
$groupid = $level[0];
$reqpoints = $level[1];
$ranktitle = $level[3];
// If they have points to rank up
if ($totalpoints >= $reqpoints && $secgroupid < $groupid) {
$newgroup = (string) $groupid;
$newranktitle = $ranktitle;
}
$log = array($newgroup, '*', $newranktitle);
// the "1" indicates to use the second line of the phrase specified for this task
log_cron_action(serialize($log), $nextitem, 1);
}
$othergroups = str_replace($secgroup, $newgroup, $othergroups);
// If they earned a new usergroup, change it
if ($newgroup != null) {
$userinfo = fetch_userinfo($userid);
$userdata = & datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->set_existing($userinfo);
$userdata->setr('membergroupids', $othergroups);
$userdata->save();
//$totalpromos ++;
}
}
}
$vbulletin->crondata['promos_lastrun'] = $thisrun;
build_datastore('crondata', serialize($vbulletin->crondata), 1);
if ($vbulletin->debug AND VB_AREA == 'AdminCP') {
#echo '<pre>'; print_r($sql); print_r($sql_id); echo '</pre>';
}
/* ======================================================================*\
|| ####################################################################
|| # Updated, July 26, 2014
|| ####################################################################
\*====================================================================== */
?>