C:\xampp\htdocs\kptv2\voting\monthly_cron.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

//***************** Set dev mode
$isDevMode false;
echo 
"Using dev mode ... false \n";

include(
'config.php');
echo 
"Config loaded ... Done \n";


//***************** Update top 20
function updateTop20($top_category_id$proposals_category_id$isDevMode)
{
    echo 
"Run update script ... \n";
    
$datenow date("Y-m-d");

    
//****************** Backup top 20 songs list
    
echo "Backup top 20 songs list ... \n";
    
$sql mysql_query("SELECT ID, artist, song, up, active, prev_month, prev_month_2, months_in_top, total, added_date, mp3, type_of_list FROM top20 WHERE active=1 AND type_of_list='" $top_category_id "' ORDER BY up DESC");

    
// Local
    //$ourFileName = "/Users/florinbardosi/Sites/localhost/kptv/radio/voting/export_" . $top_category_id . "-" . $datenow . ".txt";
    // Prod
    
$ourFileName "/var/www/sites/kptv.ro/public_html/radio/voting/export_" $top_category_id "-" $datenow ".txt";

    echo 
" - file is created ... " $ourFileName "\n";
    
$ourFileHandle fopen($ourFileName'w') or die("Error: can't open file");
    
$putInto "ID;artist;song;up;active;prev_month;prev_month_2;months_in_top;total;added_date;mp3;type_of_list \n";
    while (
$row mysql_fetch_assoc($sql)) {
        
$putInto .= implode(";"$row);
        
$putInto .= "\n";
    }

    
fwrite($ourFileHandle$putInto);
    
fclose($ourFileHandle);
    echo 
"Backup top 20 songs list ... Done \n";


    
//***************** Update months in top value for all songs
    
echo "Update months in top value ... \n";
    if (
$isDevMode) {
        echo 
" - months_in_top are updated ... \n";
    } else {
        
mysql_query("UPDATE top20 SET months_in_top=months_in_top+1 WHERE type_of_list='" $top_category_id "' \n");
        
mysql_query("UPDATE top20 SET months_in_top=months_in_top+1 WHERE type_of_list='" $proposals_category_id "' \n");
    }
    echo 
"Update months in top value ... Done \n";


    
//***************** Get bottom 3 songs
    
echo "Get bottom 3 songs ... \n";
    
$sql mysql_query("select * from (SELECT * FROM (SELECT *, (total + up) AS total_up FROM top20 WHERE type_of_list='" $top_category_id "' ORDER BY total DESC, months_in_top DESC, prev_month DESC LIMIT 20 ) t ORDER BY total ASC, months_in_top ASC, prev_month ASC LIMIT 3) tt ORDER BY total DESC, months_in_top DESC, prev_month DESC;");
    
$last3Positions = array();
    
$i 0;
    while (
$row mysql_fetch_array($sql)) {
        
$last3Positions[$i] = $row;

        
// Calculate average number of votes per month for each song in top 20
        
$last3Positions[$i]["avg_votes_per_month"] = $last3Positions[$i]["total_up"] / $last3Positions[$i]["months_in_top"];
        if (
$isDevMode) {
            echo 
" - calculate average number of votes per month for bottom 3 songs in top 20 ... \n";
            echo 
" - bottom 3 songs - total_up - months_in_top  ... " $last3Positions[$i]['ID'] . ' - ' $last3Positions[$i]['total_up'] . ' - ' $last3Positions[$i]["months_in_top"] . "\n";
            echo 
" - bottom 3 songs - avg_votes_per_month ... " $last3Positions[$i]["avg_votes_per_month"] . "\n";
        }

        
$i++;
    }
    echo 
"Get bottom 3 songs ... Done \n";


    
//****************** Get proposed songs
    
echo "Get proposed songs ... \n";
    
$sql mysql_query("SELECT ID, up FROM top20 WHERE active=1 AND type_of_list='" $proposals_category_id "' ORDER BY up DESC LIMIT 3");
    
$proposed_songs = array();
    while (
$row2 mysql_fetch_array($sql)) {
        
$proposed_songs[$row2['ID']] = $row2['up'];
        if (
$isDevMode) {
            echo 
" - proposed songs ... " $row2['ID'] . ' - ' $proposed_songs[$row2['ID']] . "\n";
        }
    }
    echo 
"Get proposed songs ... Done \n";


    
//****************** Update total, previous month position and previous 2 months position
    
echo "Update total, previous month position and previous 2 months position ... \n";
    
$sql mysql_query("SELECT * FROM top20 WHERE active=1 AND type_of_list='" $top_category_id "' ORDER BY up DESC, ID LIMIT 20 ");
    
$i 0;
    while (
$row mysql_fetch_array($sql)) {
        
$i++;
        if (
$isDevMode) {
            echo 
" - prev_month are updated for top_category_id and row[ID] ... " $top_category_id ' - ' $row['ID'] . " \n";
            echo 
" - current position - prev_month ... " $i ' - ' $row['prev_month'] . " \n";
        } else {
            
mysql_query("UPDATE top20 SET total=total+'" $row['up'] . "', prev_month = '" $i "', prev_month_2 = '" $row['prev_month'] . "' WHERE type_of_list='" $top_category_id "' AND ID = " $row['ID']);
        }
    }
    echo 
"Update total, previous month position and previous 2 months position ... Done \n";


    
//***************** Compare and replace bottom 3 songs from top 20 with proposed songs
    
echo "Compare and replace bottom 3 songs from top 20 with proposed songs ... \n";
    foreach (
$proposed_songs as $proposed_song_id => $proposed_song_votes) {
        for (
$i 0$i 3$i++) {
            if (
$isDevMode) {
                echo 
" - song details " $last3Positions[$i]["avg_votes_per_month"] . " ... last3Positions[i][prev_month] - " $last3Positions[$i]["prev_month"] . " ... last3Positions[i][prev_month_2] " $last3Positions[$i]["prev_month_2"] . "\n";
                echo 
" - prop song details ... proposed_song_id - " $proposed_song_id " ... proposed_song_votes - " $proposed_song_votes "\n";
            }
            if ((
$proposed_song_votes $last3Positions[$i]["avg_votes_per_month"]) && ($last3Positions[$i]["prev_month"] > $last3Positions[$i]["prev_month_2"])) {
                if (
$isDevMode) {
                    echo 
" - YEY ... song with id " $last3Positions[$i]["ID"] . " should be replaced by song with id " $proposed_song_id "\n";
                } else {
                    
// Replace this bottom song with proposed song
                    
mysql_query("UPDATE top20 SET type_of_list='" $top_category_id "', prev_month=0 , prev_month_2=0 , total='" $proposed_song_votes "' WHERE type_of_list='" $proposals_category_id "' AND ID = " $proposed_song_id);
                    
mysql_query("UPDATE top20 SET active=0 WHERE type_of_list='" $top_category_id "' AND ID = " $last3Positions[$i]["ID"]);
                }

                
// Update replacement in $last3Positions until algorithm is finished
                
$last3Positions[$i]["ID"] = $proposed_song_id;
                
$last3Positions[$i]["avg_votes_per_month"] = $proposed_song_votes;

                break;
            }
        }
    }
    echo 
"Compare and replace bottom 3 songs from top 20 with proposed songs ... Done \n";


    
//************ Set proposed songs as inactive
    
echo "Set proposed songs as inactive ... \n";
    if (!
$isDevMode) {
        
mysql_query("UPDATE top20 SET active = 0 WHERE type_of_list=" $proposals_category_id);
    }
    echo 
"Set proposed songs as inactive ... Done \n";


    
//***************** Reset voting session
    
echo "Reset voting session ... \n";
    if (!
$isDevMode) {
        
mysql_query("UPDATE top20 SET up=0 WHERE type_of_list=" $top_category_id);
        
mysql_query("UPDATE top20 SET up=0 WHERE type_of_list=" $proposals_category_id);
        
mysql_query("TRUNCATE vote");
        
mysql_query("TRUNCATE voteplus");
    }
    echo 
"Reset voting session ... Done \n";

    echo 
"Run update script ... Done \n";
    echo 
"*********************************************************************** \n";
}


//***************** Get categories
$categories = array();
$top_category_id 0;
$proposals_category_id 0;

$sql mysql_query("SELECT * FROM toplistcategories where parent_id = -1");
$i 0;

while (
$row mysql_fetch_array($sql)) {
    echo 
"Get categories ... \n";
    
$categories[$i] = $row;

    
$sql1 mysql_query("SELECT * FROM toplistcategories where parent_id = " $categories[$i]["ID"]);

    while (
$row1 mysql_fetch_array($sql1)) {
        if (
$row1["title"] === 'TOP') {
            
$top_category_id $row1['ID'];
        }
        if (
$row1['title'] === 'Proposals') {
            
$proposals_category_id $row1['ID'];
        }
    }
    if (
$isDevMode) {
        echo 
"  - using category id ... " $categories[$i]["ID"] . " \n";
        echo 
"  - using top_category_id ... " $top_category_id " \n";
        echo 
"  - using proposals_category_id ... " $proposals_category_id " \n";
    }
    echo 
"Get categories ... Done \n";

    
updateTop20($top_category_id$proposals_category_id$isDevMode);
    
$i++;
}

?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586