+// Get timestamp for given stats type and data
+function USER_STATS_GET_TIMESTAMP ($type, $data, $uid = 0) {
+ // Default timestamp is zero
+ $stamp = 0;
+
+ // User id set?
+ if ((isset($GLOBALS['userid'])) && ($uid == 0)) {
+ $uid = $GLOBALS['userid'];
+ } // END - if
+
+ // Is the extension installed and updated?
+ if ((!EXT_IS_ACTIVE("sql_patches")) || (EXT_VERSION_IS_OLDER("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($uid), $type, $data), __FILE__, __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 USER_STATS_INSERT_RECORD ($uid, $type, $data) {
+ // Is the extension installed and updated?
+ if ((!EXT_IS_ACTIVE("sql_patches")) || (EXT_VERSION_IS_OLDER("sql_patches", "0.5.6"))) {
+ // Return zero here
+ return false;
+ } // END - if
+
+ // Does it exist?
+ if ((!USER_STATS_GET_TIMESTAMP($type, $data, $uid)) && (!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($uid), $type, $data), __FILE__, __LINE__);
+ } elseif (is_array($data)) {
+ // Invalid data!
+ DEBUG_LOG(__FUNCTION__, __LINE__, " uid={$uid},type={$type},data={".gettype($data).": Invalid statistics data type!");
+ }
+}
+
+// "Getter" for array for user refs and points in given level
+function GET_USER_REF_POINTS ($uid, $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 (EXT_IS_ACTIVE("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($uid), bigintval($level)), __FILE__, __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'] = GET_TOTAL_DATA($row['refid'], "user_points", "points") - GET_TOTAL_DATA($row['refid'], "user_data", "used_points");
+
+ // Get unconfirmed mails
+ $row['unconfirmed'] = GET_TOTAL_DATA($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'] = MEMBER_ACTIVITY_ACTIVE;
+
+ // Is autopurge installed and the user inactive?
+ if ((EXT_IS_ACTIVE("autopurge")) && ((time() - getConfig('ap_inactive_since')) >= $row['last_online'])) {
+ // Inactive user!
+ $row['activity'] = 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 />\n";
+ return $refs;
+}
+
+// Recuced the amount of received emails for the receipients for given email
+function REDUCT_RECIPIENT_RECEIVED_MAILS ($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), __FILE__, __LINE__);
+
+ // Are there entries?
+ if (SQL_NUMROWS($result) > 0) {
+ // Now load all userids for one big query!
+ $UIDs = array();
+ while (list($uid) = SQL_FETCHROW($result)) {
+ $UIDs[$uid] = $uid;
+ } // 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)), __FILE__, __LINE__);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+}
+
+// [EOF]