ajout SAE PHP 19/20 Meilleure note de la promo

This commit is contained in:
EmmanuelTiamzon
2025-12-05 10:30:43 +01:00
parent b05817dbcd
commit 0ae7d097f9
290 changed files with 69511 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Critique_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
/**
* Ajouter une critique
*/
public function add_critique($data)
{
if (empty($data['user_id']) || empty($data['note'])) {
return false;
}
// Vérifie si une critique existe déjà pour cette série et cette saison (ou pour la série entière si saison_id est NULL)
$sql_check = "
SELECT id FROM critiques
WHERE user_id = ? AND tvshow_id = ? AND (
(season_id IS NULL AND ? IS NULL) OR season_id = ?
)
";
$season_id = $data['season_id'] ?? null;
$existing = $this->db->query($sql_check, [
$data['user_id'],
$data['tvshow_id'],
$season_id,
$season_id
])->row();
if ($existing) {
return false; // Critique déjà existante
}
// Insertion de la critique
$sql_insert = "
INSERT INTO critiques (user_id, tvshow_id, season_id, note, commentaire, date_creation)
VALUES (?, ?, ?, ?, ?, ?)
";
$this->db->query($sql_insert, [
$data['user_id'],
$data['tvshow_id'],
$season_id,
$data['note'],
$data['commentaire'],
$data['date_creation']
]);
return $this->db->affected_rows() > 0;
}
/**
* Moyenne des notes pour une série
*/
public function getAverageRating($tvshow_id) {
$sql = "
SELECT AVG(note) AS moyenne, COUNT(*) AS nb_votes
FROM critiques
WHERE tvshow_id = ?
";
return $this->db->query($sql, [$tvshow_id])->row_array();
}
/**
* Récupère les critiques d'une série avec email auteur
*/
public function get_critiques_by_tvshow($tvshow_id) {
$sql = "
SELECT c.*, u.email AS auteur
FROM critiques c
JOIN users u ON u.id = c.user_id
WHERE c.tvshow_id = ?
";
return $this->db->query($sql, [$tvshow_id])->result();
}
/**
* Récupère les critiques d'une saison
*/
public function get_critiques_by_season($season_id) {
$sql = "
SELECT c.*, u.username
FROM critiques c
JOIN users u ON u.id = c.user_id
WHERE c.season_id = ?
ORDER BY c.date_creation DESC
";
return $this->db->query($sql, [$season_id])->result();
}
/**
* Récupère les critiques d'un utilisateur
*/
public function get_critiques_by_user($user_id) {
$sql = "
SELECT c.*,
t.name AS tvshow_name,
t.id AS tvshow_id,
p.jpeg,
u.email AS auteur
FROM critiques c
JOIN users u ON u.id = c.user_id
JOIN tvshow t ON t.id = c.tvshow_id
LEFT JOIN poster p ON p.id = t.posterId
WHERE c.user_id = ?
ORDER BY c.id DESC
";
return $this->db->query($sql, [$user_id])->result();
}
/**
* Récupère toutes les critiques
*/
public function get_all_critiques() {
$sql = "
SELECT c.*, u.username, t.name AS tvshow_name, s.season_number
FROM critiques c
JOIN users u ON u.id = c.user_id
LEFT JOIN tvshow t ON t.id = c.tvshow_id
LEFT JOIN season s ON s.id = c.season_id
ORDER BY c.date_creation DESC
";
return $this->db->query($sql)->result();
}
/**
* Récupère une critique par ID
*/
public function get_by_id($id) {
$sql = "
SELECT c.*,
t.name AS tvshow_name,
t.id AS tvshow_id,
p.jpeg,
u.email AS auteur
FROM critiques c
JOIN users u ON u.id = c.user_id
JOIN tvshow t ON t.id = c.tvshow_id
LEFT JOIN poster p ON p.id = t.posterId
WHERE c.id = ?
LIMIT 1
";
return $this->db->query($sql, [$id])->row();
}
/**
* Mise à jour d'une critique
*/
public function update_critique($id, $data) {
$sql = "
UPDATE critiques
SET note = ?, commentaire = ?, date_creation = NOW()
WHERE id = ?
";
return $this->db->query($sql, [
$data['note'],
$data['commentaire'],
$id
]);
}
}

View File

@@ -0,0 +1,46 @@
<?php
class Favoris_model extends CI_Model {
public function get_favoris_by_user($user_id)
{
$sql = "
SELECT f.*, t.name, t.id as tvshow_id
FROM favoris f
JOIN tvshow t ON t.id = f.tvshow_id
WHERE f.user_id = ?
";
return $this->db->query($sql, [$user_id])->result();
}
public function is_favori($user_id, $tvshow_id)
{
$sql = "
SELECT 1
FROM favoris
WHERE user_id = ? AND tvshow_id = ?
LIMIT 1
";
$query = $this->db->query($sql, [$user_id, $tvshow_id]);
return $query->num_rows() > 0;
}
public function ajouter_favori($user_id, $tvshow_id)
{
if (!$this->is_favori($user_id, $tvshow_id)) {
$sql = "
INSERT INTO favoris (user_id, tvshow_id)
VALUES (?, ?)
";
$this->db->query($sql, [$user_id, $tvshow_id]);
}
}
public function supprimer_favori($user_id, $tvshow_id)
{
$sql = "
DELETE FROM favoris
WHERE user_id = ? AND tvshow_id = ?
";
$this->db->query($sql, [$user_id, $tvshow_id]);
}
}

View File

@@ -0,0 +1,257 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_tvshow extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
/** Toutes les séries + poster + nb de saisons */
public function getTvshows()
{
$sql = "
SELECT tvshow.id,
tvshow.name,
poster.jpeg,
(
SELECT COUNT(*)
FROM season
WHERE season.tvShowId = tvshow.id
AND season.seasonNumber != 2147483647
) AS seasons_count
FROM tvshow
JOIN poster ON poster.id = tvshow.posterId
";
return $this->db->query($sql)->result();
}
/** Suggestions basées sur les genres en commun */
public function getSuggestionsByTvshowId($tvshowId)
{
$sql = "
SELECT ts.id,
ts.name,
p.jpeg,
COUNT(DISTINCT tsg1.genreId) AS common_genres,
COUNT(DISTINCT s.id) AS seasons_count
FROM tvshow ts
JOIN poster p ON p.id = ts.posterId
JOIN tvshow_genre tsg1 ON tsg1.tvShowId = ts.id
JOIN tvshow_genre tsg2 ON tsg2.genreId = tsg1.genreId
LEFT JOIN season s ON s.tvShowId = ts.id
WHERE tsg2.tvShowId = ?
AND ts.id != ?
GROUP BY ts.id, ts.name, p.jpeg
ORDER BY common_genres DESC, ts.name
LIMIT 8
";
return $this->db->query($sql, [$tvshowId, $tvshowId])->result();
}
/** Une série par son id */
public function getTvshowById($id)
{
$sql = "
SELECT tvshow.*, poster.jpeg
FROM tvshow
JOIN poster ON poster.id = tvshow.posterId
WHERE tvshow.id = ?
";
return $this->db->query($sql, [$id])->row();
}
/** Homepage d'une série */
public function getHomepageById($id)
{
$sql = "
SELECT homepage
FROM tvshow
WHERE id = ?
";
$query = $this->db->query($sql, [$id]);
return $query->row() ? $query->row()->homepage : null;
}
/** Tous les épisodes dune série, triés par saison puis numéro dépisode */
public function getEpisodesByTvshowId($tvShowId)
{
$sql = "
SELECT season.seasonNumber AS season_number,
episode.episodeNumber AS episode_number,
episode.name AS episode_name,
episode.overview AS episode_overview
FROM season
JOIN episode ON episode.seasonId = season.id
WHERE season.tvShowId = ?
ORDER BY season.seasonNumber ASC, episode.episodeNumber ASC
";
return $this->db->query($sql, [$tvShowId])->result();
}
/** Toutes les séries d'un genre donné */
public function getTvshowsByGenre($genre)
{
$sql = "
SELECT tvshow.id,
tvshow.name,
poster.jpeg,
(
SELECT COUNT(*)
FROM season
WHERE season.tvShowId = tvshow.id
AND season.seasonNumber != 2147483647
) AS seasons_count
FROM tvshow
JOIN poster ON poster.id = tvshow.posterId
JOIN tvshow_genre ON tvshow_genre.tvShowId = tvshow.id
JOIN genre ON genre.id = tvshow_genre.genreId
WHERE genre.name = ?
AND EXISTS (
SELECT 1 FROM season
WHERE season.tvShowId = tvshow.id
AND season.seasonNumber != 2147483647
)
";
return $this->db->query($sql, [$genre])->result();
}
public function getAllFiltered($genre = null, $search = null, $order_by = null, $min_rating = null)
{
$sql = "
SELECT
tvshow.id,
tvshow.name,
tvshow.overview,
tvshow.homepage,
poster.jpeg,
AVG(critiques.note) AS average_rating,
COUNT(DISTINCT season.id) AS seasons_count
FROM tvshow
JOIN poster ON poster.id = tvshow.posterId
LEFT JOIN tvshow_genre ON tvshow_genre.tvShowId = tvshow.id
LEFT JOIN genre ON genre.id = tvshow_genre.genreId
LEFT JOIN critiques ON critiques.tvshow_id = tvshow.id
LEFT JOIN season ON season.tvShowId = tvshow.id
";
$conditions = [];
$params = [];
if ($genre) {
$conditions[] = "genre.name = ?";
$params[] = $genre;
}
if ($search) {
$conditions[] = "tvshow.name LIKE ?";
$params[] = '%' . $search . '%';
}
if (!empty($conditions)) {
$sql .= " WHERE " . implode(" AND ", $conditions);
}
$sql .= " GROUP BY tvshow.id";
if ($min_rating) {
$sql .= " HAVING average_rating >= ?";
$params[] = $min_rating;
}
if ($order_by) {
foreach ($order_by as $column => $direction) {
$sql .= " ORDER BY $column $direction";
break;
}
}
return $this->db->query($sql, $params)->result();
}
/** Tous les genres associés à une série */
public function getGenresByTvshowId($tvshowId)
{
$sql = "
SELECT genre.name
FROM genre
JOIN tvshow_genre ON tvshow_genre.genreId = genre.id
WHERE tvshow_genre.tvShowId = ?
ORDER BY genre.name
";
return $this->db->query($sql, [$tvshowId])->result();
}
/** Tous les genres disponibles */
public function getAllGenres()
{
$sql = "
SELECT name
FROM genre
ORDER BY name
";
return $this->db->query($sql)->result();
}
/** Recherche de séries par nom, avec filtre optionnel par genre */
public function searchTvshows($query, $genre = null)
{
$sql = "
SELECT tvshow.id,
tvshow.name,
poster.jpeg,
(
SELECT COUNT(*)
FROM season
WHERE season.tvShowId = tvshow.id
) AS seasons_count
FROM tvshow
JOIN poster ON poster.id = tvshow.posterId
WHERE tvshow.name LIKE ?
";
$params = ['%' . $query . '%'];
if (!empty($genre)) {
$sql .= "
AND tvshow.id IN (
SELECT tvshow_genre.tvShowId
FROM tvshow_genre
JOIN genre ON genre.id = tvshow_genre.genreId
WHERE genre.name = ?
)
";
$params[] = $genre;
}
return $this->db->query($sql, $params)->result();
}
/** Récupère une saison précise par série et numéro */
public function getSeasonByNumber($tvshow_id, $season_number)
{
$sql = "
SELECT season.*, poster.jpeg
FROM season
LEFT JOIN poster ON poster.id = season.posterId
WHERE season.tvShowId = ? AND season.seasonNumber = ?
LIMIT 1
";
return $this->db->query($sql, [$tvshow_id, $season_number])->row();
}
/** Récupère les épisodes dune saison spécifique */
public function getEpisodesBySeason($tvshow_id, $season_number)
{
$sql = "
SELECT episode.episodeNumber, episode.name, episode.overview
FROM episode
JOIN season ON season.id = episode.seasonId
WHERE season.tvShowId = ? AND season.seasonNumber = ?
ORDER BY episode.episodeNumber
";
return $this->db->query($sql, [$tvshow_id, $season_number])->result();
}
}

View File

@@ -0,0 +1,85 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
/**
* Enregistre un nouvel utilisateur
*/
public function register($firstname, $lastname, $email, $password) {
// Vérifie si l'email existe déjà
$sql_check = "SELECT id FROM users WHERE email = ? LIMIT 1";
$query = $this->db->query($sql_check, [$email]);
if ($query->num_rows() > 0) {
return false; // Email déjà utilisé
}
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
$sql_insert = "
INSERT INTO users (firstname, lastname, email, password)
VALUES (?, ?, ?, ?)
";
return $this->db->query($sql_insert, [$firstname, $lastname, $email, $hashed_password]);
}
/**
* Vérifie si un email est unique
*/
public function is_email_unique($email) {
$sql = "SELECT id FROM users WHERE email = ? LIMIT 1";
$query = $this->db->query($sql, [$email]);
return $query->num_rows() === 0;
}
/**
* Récupère les séries en favoris avec le nombre de saisons
*/
public function get_favoris_with_season_count($user_id) {
$sql = "
SELECT tvshow.id, tvshow.name, poster.jpeg, COUNT(season.id) AS season_count
FROM favoris
JOIN tvshow ON tvshow.id = favoris.tvshow_id
LEFT JOIN poster ON poster.id = tvshow.posterId
LEFT JOIN season ON season.tvShowId = tvshow.id
WHERE favoris.user_id = ?
GROUP BY tvshow.id, tvshow.name, poster.jpeg
";
return $this->db->query($sql, [$user_id])->result();
}
/**
* Connexion utilisateur
*/
public function login($email, $password) {
$sql = "
SELECT * FROM users
WHERE email = ?
LIMIT 1
";
$query = $this->db->query($sql, [$email]);
$user = $query->row();
if ($user && password_verify($password, $user->password)) {
return $user;
}
return false;
}
/**
* Récupère un utilisateur par ID
*/
public function get_user($id) {
$sql = "
SELECT * FROM users
WHERE id = ?
LIMIT 1
";
return $this->db->query($sql, [$id])->row();
}
}