summaryrefslogtreecommitdiffstats
path: root/lib/class/random.class.php
blob: 58be0463b0d1327a15464321c8b6b6379a9cf45c (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/*

 Copyright 2001 - 2007 Ampache.org
 All Rights Reserved

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 

*/

/**
 * Random Class
 * All of the 'random' type events, elements, voodoo done by ampache is done
 * by this class, there isn't a table for this class so most of it's functions
 * are static
 */
class Random {

	/**
	 * Constructor
	 * nothing to see here, move along
	 */
	public function __construct() { 

		// Rien a faire

	} // constructor

	/**
	 * play_url
	 * This generates a random play url based on the passed type
	 * and returns it
	 */
	public static function play_url($type) { 

		if (!$type = self::validate_type($type)) { 
			return false; 
		} 
	
		if (Config::get('require_session')) { 
			$session_string = '&sid=' . session_id(); 
		} 

                $web_path = Config::get('web_path');

                if (Config::get('force_http_play') OR !empty($force_http)) {
                        $port = Config::get('http_port');
                        if (preg_match("/:\d+/",$web_path)) {
                                $web_path = str_replace("https://", "http://",$web_path);
                        }
                        else {
                                $web_path = str_replace("https://", "http://",$web_path);
                        }
                }
		
		$uid = $GLOBALS['user']->id; 
	
		$url = $web_path . "/play/index.php?random=1&type=$type&uid=$uid$session_string";

		return $url; 

	} // play_url 

	/**
	 * get_single_song
	 * This returns a single song pulled based on the passed random method
	 */
	public static function get_single_song($type) { 

		if (!$type = self::validate_type($type)) { 
			return false; 
		} 

		$method_name = 'get_' . $type; 

		if (method_exists('Random',$method_name)) { 
			$song_ids = self::$method_name(1); 
			$song_id = array_pop($song_ids);  
		} 
		
		return $song_id; 

	} // get_single_song

	/**
	 * get_default
	 * This just randomly picks a song at whim from all catalogs
	 * nothing special here...
	 */ 
	public static function get_default($limit) { 

		$results = array(); 

		$sql = "SELECT `id` FROM `song` ORDER BY RAND() LIMIT $limit"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$results[] = $row['id']; 
		} 
	
		return $results; 

	} // get_default

	/**
	 * get_genre
	 * This looks at the last object played by the current user and
	 * then picks a song of the same genre at random...
	 */
	public static function get_genre($limit) { 

		$results = array(); 

		// Get the last genre played by us
		$data = $GLOBALS['user']->get_recently_played('1','genre'); 
		if ($data['0']) { 
			$where_sql = " WHERE `genre`='" . $data['0'] . "' "; 	
		} 

		$sql = "SELECT `id` FROM `song` $where_sql ORDER BY RAND() LIMIT $limit"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
	 		$results[] = $row['id']; 
		} 

		return $results; 

	} // get_genre

	/**
	 * get_album
	 * This looks at the last album played by the current user and
	 * picks something else in the same album
	 */
	public static function get_album($limit) { 

		$results = array(); 

		// Get the last album playbed by us
		$data = $GLOBALS['user']->get_recently_played('1','album'); 
		if ($data['0']) { 
			$where_sql = " WHERE `album`='" . $data['0'] . "' ";
		} 

		$sql = "SELECT `id` FROM `song` $where_sql ORDER BY RAND() LIMIT $limit"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$results[] = $row['id']; 
		} 

		return $results; 

	} // get_album

	/**
	 * get_artist
	 * This looks at the last artist played and then randomly picks a song from the
	 * same artist
	 */
	public static function get_artist($limit) { 

		$results = array(); 

		$data = $GLOBALS['user']->get_recently_played('1','artist'); 
		if ($data['0']) { 
			$where_sql = " WHERE `artist`='" . $data['0'] . "' "; 
		} 

		$sql = "SELECT `id` FROM `song` $where_sql ORDER BY RAND() LIMIT $limit"; 
		$db_results = Dba::query($sql); 

		while ($row = Dba::fetch_assoc($db_results)) { 
			$results[] = $row['id']; 
		} 
		
		return $results; 

	} // get_artist

	/**
	 * get_type_name
	 * This returns a 'purrty' name for the differnt random types
	 */
	public static function get_type_name($type) { 

		switch ($type) { 
			case 'album':
				return _('Related Album'); 
			break;
			case 'genre': 
				return _('Related Genre'); 
			break;
			case 'artist': 
				return _('Related Artist'); 
			break;
			default: 
				return _('Pure Random'); 
			break;
		} // end switch 

	} // get_type_name

	/**
	 * validiate_type
	 * this validates the random type
	 */
	public static function validate_type($type) { 

                switch ($type) {
			case 'default':
                        case 'genre':
                        case 'album':
                        case 'artist':
                        case 'rated':
				return $type; 
                        break;
                        default:
                                return 'default';
                        break;
                } // end switch
		
		return $type; 

	} // validate_type

} //end of random class

?>