Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / referral-functions.php
index 72e53b622ad549c90d10772476317f4b7ef67af6..788e40cfa80bbabba4de8349a181f7d283f4f8a5 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Alle zum Referral-System gehoerenden Funktionen  *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -200,7 +195,7 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ',sendNotify=' . intval($sendNotify) . ',isLocked=' . intval($isLocked));
 
                        // Send "referral confirmed" mails out?
-                       if ((isValidUserid($refid)) && ($refid != $userid) && ($sendNotify === TRUE)) {
+                       if ((isValidId($refid)) && ($refid != $userid) && ($sendNotify === TRUE)) {
                                // Calculate the referral's points and percents
                                $percentsReferral = getReferralLevelPercents($GLOBALS['ref_level'] + 1);
 
@@ -279,14 +274,14 @@ function addPointsThroughReferralSystem ($subject, $userid, $points, $refid = NU
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',refid(var|data)=' . convertNullToZero($refid) . '|' . convertNullToZero(getUserData('refid')) . ',userid=' . $userid . ',paymentMethod=' . $paymentMethod . ',subject=' . $subject . ',ref_level=' . $GLOBALS['ref_level']);
                        if (($paymentMethod == 'REFERRAL') && (isValidId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid)) {
                                // Is _ref there?
-                               if (substr($subject, -4, 4) == '_ref') {
+                               if (ifSubjectHasReferralSuffix($subject)) {
                                        // Then remove it, no double _ref suffix!
                                        $subject = substr($subject, 0, -4);
                                } // END - if
 
                                // Then let's credit him here...
                                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',refid=' . convertNullToZero(getUserData('refid')) . ',points=' . $points . ',ref_points=' . $ref_points . ',added[' . gettype($added) . ']=' . intval($added) . ',ref_level=' . $GLOBALS['ref_level'] . ' - ADVANCE!');
-                               $added = ($added && addPointsThroughReferralSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getFetchedUserData('userid', getUserData('refid'), 'refid')));
+                               $added = ($added && addPointsThroughReferralSystem(sprintf('%s_ref:%s', $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getFetchedUserData('userid', getUserData('refid'), 'refid')));
                        } // END - if
                } // END - if
        } // END - if
@@ -419,17 +414,11 @@ ORDER BY
                ), __FUNCTION__, __LINE__);
 
        // Are there some entries?
-       if (!ifSqlHasZeroNums($result)) {
+       if (!ifSqlHasZeroNumRows($result)) {
                // Fetch all entries
                while ($row = sqlFetchArray($result)) {
                        // Init click rate with zero
-                       $row['click_rate'] = '0';
-
-                       // Is at least one mail received?
-                       if ($row['emails_received'] > 0) {
-                               // Calculate click rate
-                               $row['click_rate'] = ($row['mails_confirmed'] / $row['emails_received'] * 100);
-                       } // END - if
+                       $row['click_rate'] = calculatePercentageRate($row['mails_confirmed'], $row['emails_received']);
 
                        // Activity is 'active' by default because if ext-autopurge is not installed
                        $row['activity'] = '{--MEMBER_ACTIVITY_ACTIVE--}';
@@ -482,8 +471,22 @@ function getPointsDataArrayFromSubject ($subject) {
        } // END - if
 
        // Now checkout the entry in database table
-       $result = sqlQueryEscaped("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
-               array($subject), __FUNCTION__, __LINE__);
+       $result = sqlQueryEscaped("SELECT
+       `id`,
+       `subject`,
+       `column_name`,
+       `locked_mode`,
+       `payment_method`,
+       `notify_recipient`
+FROM
+       `{?_MYSQL_PREFIX?}_points_data`
+WHERE
+       `subject`='%s'
+LIMIT 1",
+               array(
+                       $subject
+               ), __FUNCTION__, __LINE__
+       );
 
        // Is there an entry?
        if (sqlNumRows($result) == 1) {
@@ -682,7 +685,7 @@ function flushReferralTableToDatabase ($userid) {
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referral_refid'][$userid]));
 
        // If no entries are there, skip this whole step
-       if (count($GLOBALS['referral_refid'][$userid]) == 0) {
+       if (!isFilledArray($GLOBALS['referral_refid'][$userid])) {
                // No entries found
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ABORTING...');
                return;
@@ -799,8 +802,8 @@ function setReferralId ($refid) {
 }
 
 // Checks if 'refid' is valid
-function isReferralIdValid () {
-       return ((isset($GLOBALS['__refid'])) && (getReferralId() !== NULL) && (getReferralId() > 0));
+function isValidReferralId () {
+       return ((isset($GLOBALS['__refid'])) && (isValidId(getReferralId())));
 }
 
 // Getter for referral id
@@ -811,16 +814,16 @@ function getReferralId () {
 // Determines referral id and sets it
 function determineReferralId () {
        // Is it already detected?
-       if (isReferralIdValid()) {
+       if (isValidReferralId()) {
                // Do not determine it, just return it
                return getReferralId();
-       } elseif ((!isHtmlOutputMode()) && (basename($_SERVER['PHP_SELF']) != 'ref.php')) {
+       } elseif ((!isHtmlOutputMode()) && (getModule() != 'ref')) {
                // Skip this in non-html-mode and outside ref.php
                return FALSE;
        }
 
        // Check if refid is set
-       if (isReferralIdValid()) {
+       if (isValidReferralId()) {
                // This is fine...
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using refid from GLOBALS (' . getReferralId() . ')');
        } elseif (isPostRequestElementSet('refid')) {
@@ -835,7 +838,7 @@ function determineReferralId () {
                // Set refid=ref (the referral link uses such variable)
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using ref from GET data (' . getRequestElement('ref') . ')');
                setReferralId(getRequestElement('ref'));
-       } elseif ((isGetRequestElementSet('user')) && (basename($_SERVER['PHP_SELF']) == 'click.php')) {
+       } elseif ((isGetRequestElementSet('user')) && (getModule() == 'click')) {
                // The variable user comes from  click.php
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using user from GET data (' . getRequestElement('user') . ')');
                setReferralId(bigintval(getRequestElement('user')));
@@ -889,7 +892,7 @@ function determineReferralId () {
                // Set cookie
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Saving refid to session (' . getReferralId() . ') #1');
                setSession('refid', getReferralId());
-       } elseif ((!isReferralIdValid()) || (!fetchUserData(getReferralId()))) {
+       } elseif ((!isValidReferralId()) || (!fetchUserData(getReferralId()))) {
                // Not valid!
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not valid referral id (' . getReferralId() . '), setting NULL in session');
                setReferralId(NULL);