vaujdui
This commit is contained in:
		@@ -95,4 +95,58 @@ class Model_playlist extends CI_Model {
 | 
			
		||||
        $this->db->where('id', $playlistId);
 | 
			
		||||
        return $this->db->update('playlists', $data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createRandomPlaylist($userEmail, $name, $numSongs, $genre) {
 | 
			
		||||
        if ($numSongs <= 0) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Création de la nouvelle playlist
 | 
			
		||||
        $data = array(
 | 
			
		||||
            'user_email' => $userEmail,
 | 
			
		||||
            'name' => $name
 | 
			
		||||
        );
 | 
			
		||||
        $this->db->insert('playlists', $data);
 | 
			
		||||
        $playlistId = $this->db->insert_id();
 | 
			
		||||
 | 
			
		||||
        // Filtrage des chansons par genre
 | 
			
		||||
        $this->db->select('track.id');
 | 
			
		||||
        $this->db->from('track');
 | 
			
		||||
        $this->db->join('song', 'track.songid = song.id');
 | 
			
		||||
        $this->db->join('album', 'track.albumid = album.id');
 | 
			
		||||
        $this->db->join('genre', 'album.genreid = genre.id');
 | 
			
		||||
 | 
			
		||||
        if ($genre) {
 | 
			
		||||
            $this->db->where('genre.name', $genre);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $query = $this->db->get();
 | 
			
		||||
        $songs = $query->result();
 | 
			
		||||
 | 
			
		||||
        if ($numSongs > count($songs)) {
 | 
			
		||||
            $numSongs = count($songs);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Sélection aléatoire de chansons
 | 
			
		||||
        $randomKeys = array_rand($songs, $numSongs);
 | 
			
		||||
        foreach ($randomKeys as $key) {
 | 
			
		||||
            $song = $songs[$key];
 | 
			
		||||
            $this->addItem($playlistId, $song->id, 'song');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getGenres() {
 | 
			
		||||
        $this->db->select('name');
 | 
			
		||||
        $this->db->from('genre');
 | 
			
		||||
        $query = $this->db->get();
 | 
			
		||||
        return $query->result();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPlaylistItemCount($playlistId) {
 | 
			
		||||
        $this->db->where('playlist_id', $playlistId);
 | 
			
		||||
        $this->db->from('playlist_items');
 | 
			
		||||
        return $this->db->count_all_results();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user