This commit is contained in:
2024-06-19 14:08:59 +02:00
parent 9436fd05cc
commit 6bfbf46dcc
21 changed files with 854 additions and 236 deletions

View File

@@ -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();
}
}