+// Get timestamp for given stats type and data
+function getTimestampFromUserStats ($type, $data, $userid = 0) {
+ // Default timestamp is zero
+ $stamp = 0;
+
+ // User id set?
+ if ((isUserIdSet()) && ($userid == 0)) {
+ $userid = getUserId();
+ } // END - if
+
+ // Is the extension installed and updated?
+ if ((!isExtensionActive('sql_patches')) || (isExtensionOlder('sql_patches', '0.5.6'))) {
+ // Return zero here
+ return $stamp;
+ } // END - if
+
+ // Try to find the entry
+ $result = SQL_QUERY_ESC("SELECT
+ UNIX_TIMESTAMP(`inserted`) AS stamp
+FROM
+ `{?_MYSQL_PREFIX?}_user_stats_data`
+WHERE
+ `userid`=%s AND `stats_type`='%s' AND `stats_data`='%s'
+LIMIT 1",
+ array(
+ bigintval($userid),
+ $type,
+ $data
+ ), __FUNCTION__, __LINE__);
+
+ // Is the entry there?
+ if (SQL_NUMROWS($result) == 1) {
+ // Get this stamp
+ list($stamp) = SQL_FETCHROW($result);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return stamp
+ return $stamp;
+}
+
+// Inserts user stats
+function insertUserStatsRecord ($userid, $type, $data) {
+ // Is the extension installed and updated?
+ if ((!isExtensionActive('sql_patches')) || (isExtensionOlder('sql_patches', '0.5.6'))) {
+ // Return zero here
+ return false;
+ } // END - if
+
+ // Does it exist?
+ if ((!getTimestampFromUserStats($type, $data, $userid)) && (!is_array($data))) {
+ // Then insert it!
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_stats_data` (`userid`,`stats_type`,`stats_data`) VALUES (%s,'%s','%s')",
+ array(bigintval($userid), $type, $data), __FUNCTION__, __LINE__);
+ } elseif (is_array($data)) {
+ // Invalid data!
+ logDebugMessage(__FUNCTION__, __LINE__, "userid={$userid},type={$type},data={".gettype($data).": Invalid statistics data type!");
+ }
+}
+
+// "Getter" for array for user refs and points in given level
+function getUserReferalPoints ($userid, $level) {
+ //* DEBUG: */ print("----------------------- <font color=\"#00aa00\">".__FUNCTION__." - ENTRY</font> ------------------------<ul><li>\n");
+ // Default is no refs and no nickname
+ $add = '';
+ $refs = array();
+
+ // Do we have nickname extension installed?
+ if (isExtensionActive('nickname')) {
+ $add = ', ud.nickname';
+ } // END - if
+
+ // Get refs from database
+ $result = SQL_QUERY_ESC("SELECT
+ ur.id, ur.refid, ud.status, ud.last_online, ud.mails_confirmed, ud.emails_received".$add."
+FROM
+ `{?_MYSQL_PREFIX?}_user_refs` AS ur
+LEFT JOIN
+ `{?_MYSQL_PREFIX?}_user_points` AS up
+ON
+ ur.refid=up.userid AND ur.level=0
+LEFT JOIN
+ `{?_MYSQL_PREFIX?}_user_data` AS ud
+ON
+ ur.refid=ud.userid
+WHERE
+ ur.userid=%s AND ur.level=%s
+ORDER BY ur.refid ASC",
+ array(
+ bigintval($userid),
+ bigintval($level)
+ ), __FUNCTION__, __LINE__);
+
+ // Are there some entries?
+ if (SQL_NUMROWS($result) > 0) {
+ // Fetch all entries
+ while ($row = SQL_FETCHARRAY($result)) {
+ // Get total points of this user
+ $row['points'] = countSumTotalData($row['refid'], 'user_points', 'points') - countSumTotalData($row['refid'], 'user_data', 'used_points');
+
+ // Get unconfirmed mails
+ $row['unconfirmed'] = countSumTotalData($row['refid'], 'user_links', 'id', 'userid', true);
+
+ // Init clickrate with zero
+ $row['clickrate'] = 0;
+
+ // Is at least one mail received?
+ if ($row['emails_received'] > 0) {
+ // Calculate clickrate
+ $row['clickrate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
+ } // END - if
+
+ // Activity is 'active' by default because if autopurge is not installed
+ $row['activity'] = getMessage('MEMBER_ACTIVITY_ACTIVE');
+
+ // Is autopurge installed and the user inactive?
+ if ((isExtensionActive('autopurge')) && ((time() - getConfig('ap_inactive_since')) >= $row['last_online'])) {
+ // Inactive user!
+ $row['activity'] = getMessage('MEMBER_ACTIVITY_INACTIVE');
+ } // END - if
+
+ // Remove some entries
+ unset($row['mails_confirmed']);
+ unset($row['emails_received']);
+ unset($row['last_online']);
+
+ // Add row
+ $refs[$row['id']] = $row;
+ } // END - while
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return result
+ //* DEBUG: */ print("</li></ul>----------------------- <font color=\"#aa0000\">".__FUNCTION__." - EXIT</font> ------------------------<br />");
+ return $refs;
+}
+
+// Recuce the amount of received emails for the receipients for given email
+function reduceRecipientReceivedMails ($column, $id, $count) {
+ // Search for mail in database
+ $result = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `%s`=%s ORDER BY `userid` ASC LIMIT %s",
+ array($column, bigintval($id), $count), __FUNCTION__, __LINE__);
+
+ // Are there entries?
+ if (SQL_NUMROWS($result) > 0) {
+ // Now load all userids for one big query!
+ // @TODO This can be somehow rewritten
+ $UIDs = array();
+ while (list($userid) = SQL_FETCHROW($result)) {
+ $UIDs[$userid] = $userid;
+ } // END - while
+
+ // Now update all user accounts
+ SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`-1 WHERE `userid` IN (%s) LIMIT %s",
+ array(implode(',', $UIDs), count($UIDs)), __FUNCTION__, __LINE__);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+}
+
+// Creates a new task
+function createNewTask ($subject, $notes, $taskType, $userid = 0, $adminId = 0, $strip = true) {
+ // Insert the task data into the database
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_task_system` (`assigned_admin`, `userid`, `status`, `task_type`, `subject`, `text`, `task_created`) VALUES (%s,%s,'NEW','%s','%s','%s', UNIX_TIMESTAMP())",
+ array($adminId, $userid, $taskType, $subject, smartAddSlashes($notes)), __FUNCTION__, __LINE__, true, $strip);
+}
+
+// [EOF]