summaryrefslogtreecommitdiffstats
path: root/lib/class/random.class.php
diff options
context:
space:
mode:
authorKarl 'vollmerk' Vollmer <vollmer@ampache.org>2007-12-16 21:37:23 +0000
committerKarl 'vollmerk' Vollmer <vollmer@ampache.org>2007-12-16 21:37:23 +0000
commit3887ab1f65fa556368d555e33b5c3e9ab59459da (patch)
tree3c006c802b0693a110806d184e038e336d48c35e /lib/class/random.class.php
parent9dc866338aede131ece294b2dcd939d61a60d187 (diff)
downloadampache-3887ab1f65fa556368d555e33b5c3e9ab59459da.tar.gz
ampache-3887ab1f65fa556368d555e33b5c3e9ab59459da.tar.bz2
ampache-3887ab1f65fa556368d555e33b5c3e9ab59459da.zip
fixed some playlist issues, (mime type and selection) cleaned up the preferences lib a little more moved a lot into the class, updated references to old functions
Diffstat (limited to 'lib/class/random.class.php')
-rw-r--r--lib/class/random.class.php121
1 files changed, 77 insertions, 44 deletions
diff --git a/lib/class/random.class.php b/lib/class/random.class.php
index d46aa6e2..8a042bad 100644
--- a/lib/class/random.class.php
+++ b/lib/class/random.class.php
@@ -259,7 +259,7 @@ class Random {
/* If they've passed -1 as limit then don't get everything */
if ($data['random'] == "-1") { unset($data['random']); }
- else { $limit_sql = "LIMIT " . $limit; }
+ else { $limit_sql = "LIMIT " . Dba::escape($limit); }
$where = "1=1 ";
if (is_array($matchlist)) {
@@ -279,52 +279,85 @@ class Random {
}
} // end foreach
} // end if matchlist
-
- if ($data['random_type'] == 'full_album') {
- $query = "SELECT `album`.`id` FROM `song` INNER JOIN `album` ON `song`.`album`=`album`.`id` " .
- "WHERE $where GROUP BY `song`.`album` ORDER BY RAND() $limit_sql";
- $db_results = Dba::query($query);
- while ($row = Dba::fetch_assoc($db_results)) {
- $albums_where .= " OR `song`.`album`=" . $row['id'];
- }
- $albums_where = ltrim($albums_where," OR");
- $sql = "SELECT `song`.`id`,`song`.`size`,`song`.`time` FROM `song` WHERE $albums_where ORDER BY `song`.`album`,`song`.`track` ASC";
-
- }
- elseif ($data['random_type'] == 'full_artist') {
- $query = "SELECT `artist`.`id` FROM `song` INNER JOIN `artist` ON `song`.`artist`=`artist`.`id` " .
- "WHERE $where GROUP BY `song`.`artist` ORDER BY RAND() $limit_sql";
- $db_results = Dba::query($query);
- while ($row = Dba::fetch_row($db_results)) {
- $artists_where .= " OR song.artist=" . $row[0];
- }
- $artists_where = ltrim($artists_where," OR");
- $sql = "SELECT song.id,song.size,song.time FROM song WHERE $artists_where ORDER BY RAND()";
- }
- elseif ($data['random_type'] == 'unplayed') {
- $uid = Dba::escape($GLOBALS['user']->id);
- $sql = "SELECT object_id,COUNT(`id`) AS `total` FROM `object_count` WHERE `user`='$uid' GROUP BY `object_id`";
- $db_results = Dba::query($sql);
-
- $in_sql = "`id` IN (";
-
- while ($row = Dba::fetch_assoc($db_results)) {
- $in_sql .= "'" . $row['object_id'] . "',";
- }
-
- $in_sql = rtrim($in_sql,',') . ')';
-
- $sql = "SELECT song.id,song.size,song.time FROM song " .
- "WHERE ($where) AND $in_sql ORDER BY RAND() $limit_sql";
+
+ switch ($data['random_type']) {
+ case 'full_aldum':
+ $query = "SELECT `album`.`id` FROM `song` INNER JOIN `album` ON `song`.`album`=`album`.`id` " .
+ "WHERE $where GROUP BY `song`.`album` ORDER BY RAND() $limit_sql";
+ $db_results = Dba::query($query);
+ while ($row = Dba::fetch_assoc($db_results)) {
+ $albums_where .= " OR `song`.`album`=" . $row['id'];
+ }
+ $albums_where = ltrim($albums_where," OR");
+ $sql = "SELECT `song`.`id`,`song`.`size`,`song`.`time` FROM `song` WHERE $albums_where ORDER BY `song`.`album`,`song`.`track` ASC";
+ break;
+ case 'full_artist':
+ $query = "SELECT `artist`.`id` FROM `song` INNER JOIN `artist` ON `song`.`artist`=`artist`.`id` " .
+ "WHERE $where GROUP BY `song`.`artist` ORDER BY RAND() $limit_sql";
+ $db_results = Dba::query($query);
+ while ($row = Dba::fetch_row($db_results)) {
+ $artists_where .= " OR song.artist=" . $row[0];
+ }
+ $artists_where = ltrim($artists_where," OR");
+ $sql = "SELECT song.id,song.size,song.time FROM song WHERE $artists_where ORDER BY RAND()";
+ break;
+ case 'unplayed':
+ $uid = Dba::escape($GLOBALS['user']->id);
+ $sql = "SELECT object_id,COUNT(`id`) AS `total` FROM `object_count` WHERE `user`='$uid' GROUP BY `object_id`";
+ $db_results = Dba::query($sql);
+
+ $in_sql = "`id` IN (";
+
+ while ($row = Dba::fetch_assoc($db_results)) {
+ $row['object_id'] = Dba::escape($row['object_id']);
+ $in_sql .= "'" . $row['object_id'] . "',";
+ }
+
+ $in_sql = rtrim($in_sql,',') . ')';
+
+ $sql = "SELECT song.id,song.size,song.time FROM song " .
+ "WHERE ($where) AND $in_sql ORDER BY RAND() $limit_sql";
+ break;
+ case 'high_rating':
+ $sql = "SELECT `rating`.`object_id`,`rating`.`rating` FROM `rating` " .
+ "WHERE `rating`.`object_type`='song' ORDER BY `rating` DESC";
+ $db_results = Dba::query($sql);
+
+ // Get all of the ratings for songs
+ while ($row = Dba::fetch_assoc($db_results)) {
+ $results[$row['object_id']][] = $row['rating'];
+ }
+ // Calculate the averages
+ foreach ($results as $key=>$rating_array) {
+ $average = intval(array_sum($rating_array) / count($rating_array));
+ // We have to do this because array_slice doesn't maintain indexes
+ $new_key = $average . $key;
+ $ratings[$new_key] = $key;
+ }
+
+ // Sort it by the value and slice at $limit * 2 so we have a little bit of randomness
+ krsort($ratings);
+ $ratings = array_slice($ratings,0,$limit*2);
+
+ $in_sql = "`song`.`id` IN (";
+
+ // Build the IN query, cause if you're OUT it ain't cool
+ foreach ($ratings as $song_id) {
+ $key = Dba::escape($song_id);
+ $in_sql .= "'$key',";
+ }
- } // If unplayed
- elseif ($data['random_type'] == 'high_rating') {
+ $in_sql = rtrim($in_sql,',') . ')';
+ // Apply true limit and order by rand
+ $sql = "SELECT song.id,song.size,song.time FROM song " .
+ "WHERE ($where) AND $in_sql ORDER BY RAND() $limit_sql";
+ break;
+ default:
+ $sql = "SELECT `id`,`size`,`time` FROM `song` WHERE $where ORDER BY RAND() $limit_sql";
- }
- else {
- $sql = "SELECT `id`,`size`,`time` FROM `song` WHERE $where ORDER BY RAND() $limit_sql";
- }
+ break;
+ } // end switch on type of random play
// Run the query generated above so we can while it
$db_results = Dba::query($sql);