diff options
-rw-r--r-- | democratic.php | 2 | ||||
-rw-r--r-- | lib/class/democratic.class.php | 119 | ||||
-rw-r--r-- | lib/class/stream.class.php | 6 | ||||
-rw-r--r-- | lib/class/tmpplaylist.class.php | 81 | ||||
-rw-r--r-- | templates/show_democratic_playlist.inc.php | 14 |
5 files changed, 124 insertions, 98 deletions
diff --git a/democratic.php b/democratic.php index abd7ab68..67953f03 100644 --- a/democratic.php +++ b/democratic.php @@ -117,7 +117,7 @@ switch ($_REQUEST['action']) { case 'show_playlist': default: $tmp_playlist = Democratic::get_current_playlist(); - $songs = $tmp_playlist->get_items(); + $objects = $tmp_playlist->get_items(); require_once Config::get('prefix') . '/templates/show_democratic.inc.php'; break; } // end switch on action diff --git a/lib/class/democratic.class.php b/lib/class/democratic.class.php index 276a74e1..78bf5fde 100644 --- a/lib/class/democratic.class.php +++ b/lib/class/democratic.class.php @@ -59,12 +59,127 @@ class Democratic extends tmpPlaylist { // If not passed user global $user_id = $user_id ? $user_id : $GLOBALS['user']->id; + /* Find the - 1 one for now */ + $sql = "SELECT `id` FROM `tmp_playlist` WHERE `session`='-1'"; + $db_results = Dba::query($sql); + $row = Dba::fetch_assoc($db_results); - $object = new tmpPlaylist($playlist_id); + $object = new Democratic($row['id']); return $object; - } // get_playlist + } // get_current_playlist + + /** + * get_items + * This returns an array of all object_ids currently in this tmpPlaylist. This + * has gotten a little more complicated because of type, the values are an array + * 0 being ID 1 being TYPE + */ + public function get_items() { + + $order = "GROUP BY tmp_playlist_data.id ORDER BY `count` DESC, user_vote.date ASC"; + $vote_select = ", COUNT(user_vote.user) AS `count`"; + $vote_join = "INNER JOIN user_vote ON user_vote.object_id=tmp_playlist_data.id"; + + /* Select all objects from this playlist */ + $sql = "SELECT tmp_playlist_data.object_type, tmp_playlist_data.id, tmp_playlist_data.object_id $vote_select " . + "FROM tmp_playlist_data $vote_join " . + "WHERE tmp_playlist_data.tmp_playlist='" . Dba::escape($this->id) . "' $order"; + $db_results = Dba::query($sql); + + /* Define the array */ + $items = array(); + while ($results = Dba::fetch_assoc($db_results)) { + $key = $results['id']; + $items[$key] = array($results['object_id'],$results['object_type']); + } + + return $items; + + } // get_items + + + /** + * vote + * This function is called by users to vote on a system wide playlist + * This adds the specified objects to the tmp_playlist and adds a 'vote' + * by this user, naturally it checks to make sure that the user hasn't + * already voted on any of these objects + */ + public function vote($items) { + + /* Itterate through the objects if no vote, add to playlist and vote */ + foreach ($items as $type=>$object_id) { + if (!$this->has_vote($object_id,$type)) { + $this->add_vote($object_id,$this->id,$type); + } + } // end foreach + + } // vote + + /** + * has_vote + * This checks to see if the current user has already voted on this object + */ + public function has_vote($object_id,$type='') { + + $tmp_id = Dba::escape($this->id); + $object_id = Dba::escape($object_id); + $type = $type ? Dba::escape($type) : 'song'; + $user_id = Dba::escape($GLOBALS['user']->id); + + /* Query vote table */ + $sql = "SELECT tmp_playlist_data.id FROM `user_vote` " . + "INNER JOIN tmp_playlist_data ON tmp_playlist_data.id=user_vote.object_id " . + "WHERE user_vote.user='$user_id' AND tmp_playlist_data.object_type='$type' " . + "AND tmp_playlist_data.object_id='$object_id' " . + "AND tmp_playlist_data.tmp_playlist='$tmp_id'"; + $db_results = Dba::query($sql); + + /* If we find row, they've voted!! */ + if (Dba::num_rows($db_results)) { + return true; + } + + return false; + + } // has_vote + + /** + * add_vote + * This takes a object id and user and actually inserts the row + */ + public function add_vote($object_id,$tmp_playlist,$object_type='') { + + $object_id = Dba::escape($object_id); + $tmp_playlist = Dba::escape($tmp_playlist); + $object_type = $object_type ? Dba::escape($object_type) : 'song'; + + /* If it's on the playlist just vote */ + $sql = "SELECT `id` FROM `tmp_playlist_data` " . + "WHERE `tmp_playlist_data`.`object_id`='$object_id'"; + $db_results = Dba::query($sql); + + /* If it's not there, add it and pull ID */ + if (!$results = Dba::fetch_assoc($db_results)) { + $sql = "INSERT INTO `tmp_playlist_data` (`tmp_playlist`,`object_id`,`object_type`) " . + "VALUES ('$tmp_playlist','$object_id','$object_type')"; + $db_results = Dba::query($sql); + $results['id'] = Dba::insert_id(); + } + + /* Vote! */ + $time = time(); + $sql = "INSERT INTO user_vote (`user`,`object_id`,`date`) " . + "VALUES ('" . Dba::escape($GLOBALS['user']->id) . "','" . $results['id'] . "','$time')"; + $db_results = Dba::query($sql); + + return true; + + } // add_vote + + } // Democratic class ?> diff --git a/lib/class/stream.class.php b/lib/class/stream.class.php index 06f9c45a..9aa1f76a 100644 --- a/lib/class/stream.class.php +++ b/lib/class/stream.class.php @@ -469,13 +469,11 @@ class Stream { * This 'votes' on the songs it inserts them into * a tmp_playlist with user of -1 (System) */ - function create_democratic() { + public function create_democratic() { - $tmp_playlist = get_democratic_playlist('-1'); + $tmp_playlist = Democratic::get_current_playlist(); $tmp_playlist->vote($this->songs); - header("Location: " . return_referer()); - } // create_democratic /** diff --git a/lib/class/tmpplaylist.class.php b/lib/class/tmpplaylist.class.php index a5e22897..1ab3ea39 100644 --- a/lib/class/tmpplaylist.class.php +++ b/lib/class/tmpplaylist.class.php @@ -129,12 +129,6 @@ class tmpPlaylist { $order = 'ORDER BY id ASC'; - if ($this->type == 'vote') { - $order = "GROUP BY tmp_playlist_data.id ORDER BY `count` DESC, user_vote.date ASC"; - $vote_select = ", COUNT(user_vote.user) AS `count`"; - $vote_join = "INNER JOIN user_vote ON user_vote.object_id=tmp_playlist_data.id"; - } - /* Select all objects from this playlist */ $sql = "SELECT tmp_playlist_data.object_type, tmp_playlist_data.id, tmp_playlist_data.object_id $vote_select " . "FROM tmp_playlist_data $vote_join " . @@ -366,81 +360,6 @@ class tmpPlaylist { } // add_object /** - * vote - * This function is called by users to vote on a system wide playlist - * This adds the specified objects to the tmp_playlist and adds a 'vote' - * by this user, naturally it checks to make sure that the user hasn't - * already voted on any of these objects - */ - public function vote($items) { - - /* Itterate through the objects if no vote, add to playlist and vote */ - foreach ($items as $object_id) { - if (!$this->has_vote($object_id)) { - $this->add_vote($object_id,$this->id); - } - } // end foreach - - } // vote - - /** - * add_vote - * This takes a object id and user and actually inserts the row - */ - public function add_vote($object_id,$tmp_playlist) { - - $object_id = Dba::escape($object_id); - $tmp_playlist = Dba::escape($tmp_playlist); - - /* If it's on the playlist just vote */ - $sql = "SELECT id FROM tmp_playlist_data " . - "WHERE tmp_playlist_data.object_id='$object_id'"; - $db_results = Dba::query($sql); - - /* If it's not there, add it and pull ID */ - if (!$results = Dba::fetch_assoc($db_results)) { - $sql = "INSERT INTO tmp_playlist_data (`tmp_playlist`,`object_id`) " . - "VALUES ('$tmp_playlist','$object_id')"; - $db_results = Dba::query($sql); - $results['id'] = Dba::insert_id(); - } - - /* Vote! */ - $time = time(); - $sql = "INSERT INTO user_vote (`user`,`object_id`,`date`) " . - "VALUES ('" . Dba::escape($GLOBALS['user']->id) . "','" . $results['id'] . "','$time')"; - $db_results = Dba::query($sql); - - return true; - - } // add_vote - - /** - * has_vote - * This checks to see if the current user has already voted on this object - */ - public function has_vote($object_id) { - - $tmp_id = Dba::escape($this->id); - - /* Query vote table */ - $sql = "SELECT tmp_playlist_data.id FROM `user_vote` " . - "INNER JOIN tmp_playlist_data ON tmp_playlist_data.id=user_vote.object_id " . - "WHERE user_vote.user='" . Dba::escape($GLOBALS['user']->id) . "' " . - "AND tmp_playlist_data.object_id='" . Dba::escape($object_id) . "' " . - "AND tmp_playlist_data.tmp_playlist='$tmp_id'"; - $db_results = Dba::query($sql); - - /* If we find row, they've voted!! */ - if (Dba::num_rows($db_results)) { - return true; - } - - return false; - - } // has_vote - - /** * get_vote * This returns the current count for a specific song on this tmp_playlist */ diff --git a/templates/show_democratic_playlist.inc.php b/templates/show_democratic_playlist.inc.php index 4838457b..eba142f4 100644 --- a/templates/show_democratic_playlist.inc.php +++ b/templates/show_democratic_playlist.inc.php @@ -30,7 +30,7 @@ <?php } ?> </colgroup> <?php -if (!count($songs)) { +if (!count($objects)) { $playlist = new Playlist($tmp_playlist->base_playlist); ?> <tr> @@ -57,17 +57,14 @@ else { </tr> <?php - -foreach($songs as $row_id=>$song_id) { - $song = new Song($song_id); - $song->format_song(); +foreach($objects as $row_id=>$object_data) { + $song = new Song($object_data['0']); + $song->format(); ?> <tr class="<?php echo flip_class(); ?>"> <td class="cel_action"> <?php if ($tmp_playlist->has_vote($song_id)) { ?> - <input class="button" type="button" value="-" onclick="ajaxPut('<?php echo conf('ajax_url'); ?>?action=vote&object_id=<?php echo $song_id; ?>&vote=-1<?php echo conf('ajax_info'); ?>')" /> <?php } else { ?> - <input class="button" type="button" value="+" onclick="ajaxPut('<?php echo conf('ajax_url'); ?>?action=vote&object_id=<?php echo $song_id; ?>&vote=1<?php echo conf('ajax_info'); ?>')" /> <?php } ?> </td> <td class="cel_votes"><?php echo scrub_out($tmp_playlist->get_vote($row_id)); ?></td> @@ -75,9 +72,6 @@ foreach($songs as $row_id=>$song_id) { <td class="cel_time"><?php echo $song->f_time; ?></td> <?php if ($GLOBALS['user']->has_access(100)) { ?> <td class="cel_admin"> - <span onclick="ajaxPut('<?php echo conf('ajax_url'); ?>?action=tv_admin&cmd=delete&track_id=<?php echo $song_id; ?><?php echo conf('ajax_info'); ?>')" /> - <?php echo get_user_icon('delete'); ?> - </span> </td> <?php } ?> </tr> |