More improved SQL queries
[mailer.git] / inc / monthly / monthly_beg.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/22/2005 *
4  * ===============                              Last change: 10/22/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : monthly_beg.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Monthly begging rallye                           *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Monatliche Bettelrallye                          *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!EXT_IS_ACTIVE("beg")) {
39         return;
40 }
41
42 // Do not execute when script is in CSS mode or no daily reset
43 if (($CSS == 1) || (!defined('__DAILY_RESET')) || (getConfig('beg_rallye') == "N")) return;
44 //* DEBUG: */ echo basename(__FILE__)."<br />\n";
45
46 // Get current month (2 digits)
47 $curr = date("m", time());
48
49 // Check if month is done
50 if (($curr != getConfig('last_month')) && (getConfig('last_month') > 0) && (getConfig('beg_ranks') > 0) && ($CSS != 1)) {
51         // Extension "autopurge" is inactive or purging of inactive accounts is deactivated
52         $whereStatement1 = "";
53         $whereStatement2 = bigintval(getConfig('beg_ranks'));
54
55         // Shall we exclude webmaster's own userid?
56         if ((getConfig('beg_include_own') == "N") && (getConfig('beg_uid') > 0)) {
57                 // Exclude it
58                 $whereStatement1 = sprintf(" AND userid != %s", bigintval(getConfig('beg_uid')));
59         } // END - if
60
61         // Let's check if there are some points left we can "pay"...
62         if (EXT_IS_ACTIVE("autopurge")) {
63                 // Use last online stamp only when autopurge for inactive members is activated
64                 if ((getConfig('ap_inactive_since') > 0) && (getConfig('beg_active') == "Y")) {
65                         // Okay, include last online timestamp
66                         $whereStatement1 .= sprintf(" AND last_online >= (UNIX_TIMESTAMP() - %s)", bigintval(getConfig('ap_inactive_since')));;
67                 } // END - if
68         } // END - if
69
70         // SQL string to check for accounts
71         $result_main = SQL_QUERY_ESC("SELECT userid, email, gender, surname, family, beg_points AS points
72 FROM `{!_MYSQL_PREFIX!}_user_data`
73 WHERE `status`='CONFIRMED'".$whereStatement1." AND beg_points > 0
74 ORDER BY beg_points DESC, userid
75 LIMIT %s",
76                 array($whereStatement2), __FILE__, __LINE__);
77
78         if (SQL_NUMROWS($result_main) > 0) {
79                 // Load our winners...
80                 while ($content = SQL_FETCHARRAY($result_main)) {
81                         // Add points to user's account directly
82                         ADD_POINTS_REFSYSTEM("monthly_beg", $content['userid'], $content['points'], false, "0", false, "direct");
83
84                         // Translate gender/points
85                         $content['gender'] = TRANSLATE_GENDER($content['gender']);
86                         $content['points'] = TRANSLATE_COMMA($content['points']);
87
88                         // Load email template and email it away
89                         $msg = LOAD_EMAIL_TEMPLATE("member_beg", $content, bigintval($content['userid']));
90                         SEND_EMAIL($content['email'], BEG_MONTHLY_RALLYE, $msg);
91                 } // END - while
92
93                 // Reset accounts
94                 $result = SQL_QUERY("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET beg_points=0.00000 WHERE beg_points > 0", __FILE__, __LINE__);
95         } // END - if
96
97         // Free memory
98         SQL_FREERESULT($result_main);
99 } // END - if
100
101 //
102 ?>