documentation model

documentation
kevin.modejar 3 months ago
parent da6d77f78d
commit 91ffe63c93

@ -14,7 +14,7 @@ $mdp = '';
$racine='/~lebeaulato/WF-Website'; // /~kekentin/WF/WF-Website /~lebeaulato/WF-Website /~kemondejar/WF-Website
$racine='/~kemondejar/WF-Website'; // /~kekentin/WF/WF-Website /~lebeaulato/WF-Website /~kemondejar/WF-Website
//$racine='/WF-Website';

@ -368,7 +368,7 @@ class UserControler {
$gw = new QuoteGateway($co);
$mdl = new QuoteModel($gw);
//$mdl -> insert4User($content, '/imgPath', 'fr', $this -> getIdOfUser(), $source->getIdSource(), $character->getIdCharacter());
$mdl -> insert4User($content, '/imgPath', 'fr', $this -> getIdOfUser(), $source->getIdSource(), $character->getIdCharacter());
return [$content, $_POST['character'], $_POST['src']];
}

@ -6,75 +6,94 @@ use Gateway\CharacterGateway;
use Gateway\Gateway;
class CharacterModel extends Model
{
public function createCharacter(int $id_character, string $name , string $img_char) : bool
{
return $this -> gateway -> create($id_character, $name, $img_char);
class CharacterModel extends Model {
/**
* Creates a character by calling the gateway's create method.
*
* @param int $id_character The unique identifier for the character.
* @param string $name The name of the character.
* @param string $img_char The file path of the image associated with the character.
*
* @return bool Returns true if the character was successfully created, otherwise false.
*/
public function createCharacter(int $id_character, string $name, string $img_char) : bool {
return $this->gateway->create($id_character, $name, $img_char);
}
public function getCharacterById(int $id_character) : ?CharacterEntity
{
$c = $this -> gateway -> findById($id_character);
if ($c)
/**
* Retrieves a character by its unique identifier.
*
* @param int $id_character The unique identifier of the character to retrieve.
*
* @return CharacterEntity|null Returns a CharacterEntity object if found, otherwise null.
*/
public function getCharacterById(int $id_character) : ?CharacterEntity {
// Attempt to find the character by ID using the gateway's findById method.
$c = $this->gateway->findById($id_character);
// If the character is found, create and return a new CharacterEntity object.
if ($c) {
return new CharacterEntity(
$c[0]['id_caracter'],
$c[0]['caracter'],
$c[0]['id_img']
$c[0]['id_caracter'], // Character ID
$c[0]['caracter'], // Character name
$c[0]['id_img'] // Image ID associated with the character
);
}
// If no character is found, return null.
return null;
}
public function getAllPerso() :array{
/**
* Retrieves all characters from the database and returns them as an array of CharacterEntity objects.
*
* @return CharacterEntity[] Returns an array of CharacterEntity objects representing all characters.
*/
public function getAllCharacters() : array {
// Fetch all characters using the gateway's findAll method.
$res = $this->gateway->findAll();
foreach($res as $c){
// Initialize an empty array to store the CharacterEntity objects.
$charac = [];
// Loop through each character in the result and create a CharacterEntity for each.
foreach ($res as $c) {
$charac[] = new CharacterEntity(
$c['id_caracter'],
$c['caracter'],
$c['id_img']
$c['id_caracter'], // Character ID
$c['caracter'], // Character name or description
$c['id_img'] // Image ID associated with the character
);
}
return $charac;
}
public function getCharacterByName(string $name) : ?CharacterEntity
{
$c = $this -> gateway -> findByName($name);
if ($c)
return new CharacterEntity(
$c[0]['id_caracter'],
$c[0]['caracter'],
$c[0]['id_img']
);
return null;
// Return the array of CharacterEntity objects.
return $charac;
}
public function getAllCharacters() : array
{
$c = $this -> gateway -> findAll();
$characters = [];
/**
* Retrieves a character by its name.
*
* @param string $name The name of the character to retrieve.
*
* @return CharacterEntity|null Returns a CharacterEntity object if a character with the given name is found, otherwise null.
*/
public function getCharacterByName(string $name) : ?CharacterEntity {
// Attempt to find the character by name using the gateway's findByName method.
$c = $this->gateway->findByName($name);
foreach ($c as $character)
{
$characters[] = new CharacterEntity(
$character['id_caracter'],
$character['caracter'],
$character['id_img']
// If the character is found, create and return a new CharacterEntity object.
if ($c) {
return new CharacterEntity(
$c[0]['id_caracter'], // Character ID
$c[0]['caracter'], // Character name or description
$c[0]['id_img'] // Image ID or path associated with the character
);
}
return $characters;
}
public function deleteCharacter(int $id_character) : bool
{
return $this -> gateway -> delete($id_character);
// If no character is found, return null.
return null;
}
public function updateCharacter(int $id_character, string $name, string $img_char) : bool
{
return $this -> gateway -> update($id_character, $name, $img_char);
}
}
}
?>

@ -1,45 +1,56 @@
<?php
namespace Model;
use Entity\CommentaryEntity;
use Gateway\CommentaryGateway;
use Gateway\Gateway;
class CommentaryModel extends Model {
/**
* Creates a new comment associated with a specific quote and user.
*
* @param string $comment The content of the comment.
* @param string $idQuote The identifier of the quote the comment is associated with.
* @param string $idUser The identifier of the user who is creating the comment.
*
* @return bool Returns true if the comment was successfully created, otherwise false.
*/
public function createComment(string $comment, string $idQuote, string $idUser): bool {
// Calls the gateway's create method to insert the new comment into the data source.
return $this->gateway->create($comment, $idUser, $idQuote);
}
/**
* Retrieves all comments associated with a specific quote.
*
* @param int $id The unique identifier of the quote.
*
* @return CommentaryEntity[] Returns an array of CommentaryEntity objects representing all the comments for the given quote.
*/
public function getComment(int $id): array {
// Initialize an empty array to store the comment objects.
$com = [];
// Fetch all comments related to the given quote ID using the gateway's findByQuote method.
$res = $this->gateway->findByQuote($id);
foreach ($res as $comments){
$com[] = new CommentaryEntity($comments["id_comment"], $comments["comment"], $comments["datec"], $comments["username"], $comments["imgpath"]);
}
return $com;
}
public function getComments(): array {
$res = $this->gateway->findAll();
// Loop through each comment in the result and create a CommentaryEntity object for each.
foreach ($res as $comments) {
$com[] = new CommentaryEntity($comments["id_comment"], $comments["comment"], $comments["date"]);
$com[] = new CommentaryEntity(
$comments["id_comment"], // Comment ID
$comments["comment"], // The content of the comment
$comments["datec"], // The date the comment was created
$comments["username"], // The username of the user who made the comment
$comments["imgpath"] // The image path associated with the user who made the comment
);
}
return $com;
}
public function deleteComment(int $id_comment): bool {
return $this -> gateway -> delete($id_comment);
}
public function updateComment(int $id_comment, string $comment): bool {
$c = $this -> getComment($id_comment);
if($c){
$c -> setComment($comment);
return $this->gateway -> update($c);
}
return false;
// Return the array of CommentaryEntity objects.
return $com;
}
}
?>

@ -1,65 +0,0 @@
<?php
namespace Model;
use Gateway\FavoriteGateway;
use Entity\FavoriteEntity;
use Gateway\Gateway;
class FavoriteModel extends Model
{
public function createFavoriteModel(int $idUser, int $idQuote) : bool
{
return $this -> gateway -> createFavoriteGateway($idUser, $idQuote);
}
public function getFavoriteById(int $idUser, int $idQuote) : ?FavoriteEntity
{
$res = $this -> gateway -> findFavoriteById($idUser, $idQuote);
if ($res)
{
return new FavoriteEntity (
$res[0]['user_f'],
$res[0]['quote_f'],
);
}
return null;
}
public function getFavoriteByUser(int $idUser) : array
{
$res = $this -> gateway -> findFavoriteByUser($idUser);
$favorites = [];
foreach ($res as $favorite)
{
$favorites = new FavoriteEntity (
$favorite['user_f'],
$favorite['quote_f']
);
}
return $favorites;
}
public function getAllFavorite() : array
{
$res = $this -> gateway -> findAllFavorite();
$favorites = [];
foreach ($res as $favorite)
{
$favorites = new FavoriteEntity (
$favorite['user_f'],
$favorite['quote_f']
);
}
return $favorites;
}
public function removeFavorite(int $idUser, int $idQuote) : bool
{
return $this -> gateway -> deleteFavoriteGateway($idUser, $idQuote);
}
}

@ -1,56 +1,37 @@
<?php
namespace Model;
use Entity\ImageEntity;
use Gateway\ImageGateway;
use Gateway\Gateway;
class ImageModel extends Model
{
public function createImgModel(int $idImg, string $imgPath) : bool
{
return $this -> gateway -> createImgGateway($idImg, $imgPath);
}
public function getImgById(int $idImg) : ?ImageEntity
{
$res = $this -> gateway -> findImgById($idImg);
if ($res)
{
return new ImageEntity(
$res[0]['id_img'],
$res[0]['imgpath']
);
}
return null;
}
public function getAllImg() : array
{
$res = $this -> gateway -> findAllImg();
/**
* Retrieves all images from the database.
*
* @return ImageEntity[] Returns an array of ImageEntity objects representing all the images.
*/
public function getAllImg() : array {
// Fetch all images using the gateway's findAllImg method.
$res = $this->gateway->findAllImg();
// Initialize an empty array to store the ImageEntity objects.
$images = [];
foreach ($res as $img)
{
// Loop through each image in the result and create an ImageEntity object for each.
foreach ($res as $img) {
$images[] = new ImageEntity(
$img['id_img'],
$img['imgpath']
$img['id_img'], // Image ID
$img['imgpath'] // Image path (location or URL of the image)
);
}
return $images;
}
public function deleteImgModel(int $idImg) : bool
{
return $this -> gateway -> deleteImgGateway($idImg);
// Return the array of ImageEntity objects.
return $images;
}
}
public function updateImgModel(int $idImg, string $imgPath) : bool
{
return $this -> gateway -> updateImgGateway($idImg, $imgPath);
}
}
?>

@ -1,12 +1,21 @@
<?php
namespace Model;
use Gateway\Gateway;
class Model
{
// The gateway instance responsible for interacting with the data source.
protected Gateway $gateway;
/**
* Constructor that initializes the Model with a Gateway instance.
*
* @param Gateway $gate The Gateway instance used to interact with the data source.
*/
public function __construct(Gateway $gate){
// Assign the provided Gateway instance to the model's gateway property.
$this->gateway = $gate;
}
}

@ -7,76 +7,60 @@ use Gateway\Gateway;
class QuestionModel extends Model
{
public function createQuestion(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
{
return $this -> gateway -> create($id_question, $question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
}
/**
* Retrieves a question by its unique identifier.
*
* @param int $id_question The unique identifier of the question to retrieve.
*
* @return QuestionEntity|null Returns a QuestionEntity object if found, otherwise null.
*/
public function getQuestion(int $id_question) : ?QuestionEntity {
// Attempt to find the question by its ID using the gateway's findById method.
$q = $this->gateway->findById($id_question);
public function getQuestion(int $id_question) : ?QuestionEntity
{
$q = $this -> gateway -> findById($id_question);
if ($q)
// If the question is found, create and return a new QuestionEntity object with the question details.
if ($q) {
return new QuestionEntity(
$q[0]['id_question'],
$q[0]['texte'],
$q[0]['answera'],
$q[0]['answerb'],
$q[0]['answerc'],
$q[0]['answerd'],
$q[0]['canswer']
$q[0]['id_question'], // Question ID
$q[0]['texte'], // The text/content of the question
$q[0]['answera'], // The first answer option
$q[0]['answerb'], // The second answer option
$q[0]['answerc'], // The third answer option
$q[0]['answerd'], // The fourth answer option
$q[0]['canswer'] // The correct answer
);
return null;
}
public function updateTextQuestion(int $id_question, string $question) : bool
{
return $this -> gateway -> updateText($id_question, $question);
}
public function updateAnswersQuestion(int $id_question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
{
return $this -> gateway -> updateAnswers($id_question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
}
}
public function deleteQuestion(int $id_question) : bool
{
return $this -> gateway -> delete($id_question);
// If no question is found, return null.
return null;
}
public function getAllQuestions() : array
{
$q = $this -> gateway -> findAll();
$questions = [];
/**
* Retrieves a random question from the database.
*
* @return QuestionEntity|null Returns a QuestionEntity object if a random question is found, otherwise null.
*/
public function getRdmQuestion() : ?QuestionEntity {
// Fetch a random question using the gateway's findRdmQuestion method.
$q = $this->gateway->findRdmQuestion();
foreach ($q as $question) {
$questions[] = new QuestionEntity(
$question['id_question'],
$question['question'],
$question['answerA'],
$question['answerB'],
$question['answerC'],
$question['answerD'],
$question['cAnswer']
// If a random question is found, create and return a new QuestionEntity object with the question details.
if ($q) {
return new QuestionEntity(
$q['id_question'], // Question ID
$q['question'], // The text/content of the question
$q['answerA'], // The first answer option
$q['answerB'], // The second answer option
$q['answerC'], // The third answer option
$q['answerD'], // The fourth answer option
$q['cAnswer'] // The correct answer
);
}
return $questions;
}
public function getRdmQuestion() : ?QuestionEntity
{
$q = $this -> gateway -> findRdmQuestion();
if ($q)
return new QuestionEntity(
$q['id_question'],
$q['question'],
$q['answerA'],
$q['answerB'],
$q['answerC'],
$q['answerD'],
$q['cAnswer']
);
// If no random question is found, return null.
return null;
}
}
}
?>

@ -1,49 +1,60 @@
<?php
namespace Model;
use Entity\quizEntity;
use Gateway\QuizGateway;
use Gateway\Gateway;
class QuizModel extends Model{
public function createQuiz(int $id_quiz, int $nb_questions) : bool
{
return $this -> gateway -> create($id_quiz, $nb_questions);
}
public function getQuiz(int $id_quiz): ?quizEntity
{
$q = $this -> gateway -> findQuizById($id_quiz);
/**
* Retrieves a quiz by its unique identifier.
*
* @param int $id_quiz The unique identifier of the quiz to retrieve.
*
* @return quizEntity|null Returns a quizEntity object if found, otherwise null.
*/
public function getQuiz(int $id_quiz): ?quizEntity{
// Attempt to find the quiz by its ID using the gateway's findQuizById method.
$q = $this->gateway->findQuizById($id_quiz);
// If the quiz is found, create and return a new quizEntity object with the quiz details.
if ($q) {
return new quizEntity(
$q[0]['id_quiz'],
$q[0]['nb_quest']
$q[0]['id_quiz'], // Quiz ID
$q[0]['nb_quest'] // The number of questions in the quiz
);
}
return null;
}
public function deleteQuiz(int $id_quiz) : bool
{
return $this -> gateway -> delete($id_quiz);
// If no quiz is found, return null.
return null;
}
public function getAllQuiz() : array
{
$q = $this -> gateway -> findAll();
/**
* Retrieves all quizzes from the database.
*
* @return quizEntity[] Returns an array of quizEntity objects representing all the quizzes.
*/
public function getAllQuiz() : array{
// Fetch all quizzes using the gateway's findAll method.
$q = $this->gateway->findAll();
// Initialize an empty array to store the quiz objects.
$quizzes = [];
// Loop through each quiz in the result and create a quizEntity object for each.
foreach ($q as $quiz) {
$quizzes[] = new quizEntity(
$quiz['id_quiz'],
$quiz['nb_questions']
$quiz['id_quiz'], // Quiz ID
$quiz['nb_questions'] // The number of questions in the quiz
);
}
return $quizzes;
// Return the array of quizEntity objects.
return $quizzes;
}
}
}
?>

@ -14,59 +14,37 @@ use Gateway\Gateway;
class QuizQuestionModel extends Model
{
public function createQuizQuestion(int $idQuiz, int $idQuestion): bool
{
return $this->gateway->create($idQuiz,$idQuestion);
}
public function findQuizQuestionByIdQuiz(int $id): ?QuizQuestionEntity
{
$q = $this ->gateway->findQuizQuestionByIdQuiz($id);
if ($q) {
return new QuizQuestionEntity(
$q['idQuiz'],
$q['idQuestion'],
);
}
return null;
}
public function getAllQuestionByQuiz(int $id, Connection $co): array
{
/**
* Retrieves all questions associated with a specific quiz.
*
* @param int $id The unique identifier of the quiz.
* @param Connection $co The database connection to be used for querying.
*
* @return QuestionEntity[] Returns an array of QuestionEntity objects representing all the questions in the quiz.
*/
public function getAllQuestionByQuiz(int $id, Connection $co): array{
// Fetch all questions related to the given quiz ID using the gateway's findQuestionsFromQuiz method.
$q = $this->gateway->findQuestionsFromQuiz($id);
// Initialize an empty array to store the QuestionEntity objects.
$questions = [];
// Create a new QuestionGateway instance using the provided database connection.
$gateway = new QuestionGateway($co);
// Create a new QuestionModel instance, passing the QuestionGateway for data operations.
$qmdl = new QuestionModel($gateway);
foreach ($q as $question){
$questions [] = $qmdl->getQuestion($question[1]);
// Loop through each question in the result and retrieve its full details using the QuestionModel.
foreach ($q as $question) {
// Add the full QuestionEntity object for each question to the questions array.
$questions[] = $qmdl->getQuestion($question[1]);
}
// Return the array of QuestionEntity objects.
return $questions;
}
public function findAll(): array
{
$q = $this -> gateway -> findAll();
$quizzes = [];
foreach ($q as $quiz) {
$quizzes[] = new QuizQuestionEntity(
$quiz['idQuiz'],
$quiz['idQuestion']
);
}
return $quizzes;
}
public function deleteQuizQuestion(int $id): bool
{
return $this -> gateway -> delete($id);
}
}
}
?>

@ -1,4 +1,5 @@
<?php
namespace Model;
use Entity\Quote;
use Gateway\QuoteGateway;
@ -7,47 +8,154 @@
class QuoteModel extends Model
{
public function searchId(int $id): Quote{
/**
* Searches for a quote by its unique identifier.
*
* @param int $id The unique identifier of the quote to search for.
*
* @return Quote Returns a Quote object. If no quote is found, returns a default Quote with an ID of -1 and default values.
*/
public function searchId(int $id): Quote {
// Fetch the quote data by ID using the gateway's searchId method.
$res = $this->gateway->searchId($id);
if( count($res) == 0)
return new Quote(-1,"NULL","NULL","NULL","NULL","NULL",0,"Default");
else
return new Quote($res[0]["id_quote"],$res[0]["content"],$res[0]["caracter"],$res[0]["imgpath"],$res[0]["title"],$res[0]["dates"],$res[0]["likes"],$res[0]["langue"]);
// If no result is found, return a default Quote with an ID of -1 and placeholder values.
if (count($res) == 0) {
return new Quote(-1, "NULL", "NULL", "NULL", "NULL", "NULL", 0, "Default");
} else {
// Otherwise, return the Quote object populated with data from the search result.
return new Quote(
$res[0]["id_quote"], // Quote ID
$res[0]["content"], // Quote content
$res[0]["caracter"], // Character associated with the quote
$res[0]["imgpath"], // Image path (associated with the quote or character)
$res[0]["title"], // Title of the quote
$res[0]["dates"], // Date the quote was created
$res[0]["likes"], // Number of likes for the quote
$res[0]["langue"] // Language of the quote
);
}
}
public function getSuggest(int $numpage, string $language):array{
$res = $this->gateway->getSuggestions($numpage,$language);
$tabQ=[];
/**
* Retrieves suggestions for quotes based on the specified page number and language.
*
* @param int $numpage The page number for pagination.
* @param string $language The language to filter the suggestions by.
*
* @return Quote[] Returns an array of Quote objects representing the suggested quotes.
*/
public function getSuggest(int $numpage, string $language): array {
// Fetch suggestions from the gateway's getSuggestions method.
$res = $this->gateway->getSuggestions($numpage, $language);
foreach($res as $q ){
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"],0,150).'...' : $q["content"];
$tabQ[]= new Quote($q["id_quote"],$q["content"],$q["caracter"],$q["imgpath"],$q["title"],$q["dates"],$q["likes"],$q["langue"]) ;
// Initialize an empty array to store the resulting Quote objects.
$tabQ = [];
// Loop through the result set and create Quote objects.
foreach ($res as $q) {
// Truncate the content if it's longer than 153 characters, appending '...' if necessary.
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"], 0, 150) . '...' : $q["content"];
// Create a new Quote object and add it to the tabQ array.
$tabQ[] = new Quote(
$q["id_quote"], // Quote ID
$q["content"], // Truncated content of the quote
$q["caracter"], // Character associated with the quote
$q["imgpath"], // Image path related to the quote
$q["title"], // Title of the quote
$q["dates"], // Date the quote was created
$q["likes"], // Number of likes the quote has received
$q["langue"] // Language of the quote
);
}
// Return the array of Quote objects.
return $tabQ;
}
public function getQuoteOfTheDay(string $language):Quote{
/**
* Retrieves the quote of the day for a specified language.
*
* @param string $language The language to filter the quote of the day by.
*
* @return Quote Returns the Quote of the Day. If no quote is found, returns a default Quote with an ID of -1 and default values.
*/
public function getQuoteOfTheDay(string $language): Quote {
// Fetch the quote of the day from the gateway's getQuoteOfTheDay method.
$res = $this->gateway->getQuoteOfTheDay($language);
if( count($res) == 0)
return new Quote(-1,"NULL","NULL","NULL","NULL","NULL",0,"Default");
else
$res["content"] = (strlen($res["content"]) > 153) ? substr($res["content"],0,150).'...' : $res["content"];
return new Quote($res["id_quote"],$res["content"],$res["caracter"],$res["imgpath"],$res["title"],$res["dates"],$res["likes"],$res["langue"]) ;
// If no result is found, return a default Quote with an ID of -1 and placeholder values.
if (count($res) == 0) {
return new Quote(-1, "NULL", "NULL", "NULL", "NULL", "NULL", 0, "Default");
} else {
// Truncate the content if it's longer than 153 characters, appending '...' if necessary.
$res["content"] = (strlen($res["content"]) > 153) ? substr($res["content"], 0, 150) . '...' : $res["content"];
// Return the Quote object populated with data from the quote of the day.
return new Quote(
$res["id_quote"], // Quote ID
$res["content"], // Truncated content of the quote
$res["caracter"], // Character associated with the quote
$res["imgpath"], // Image path associated with the quote
$res["title"], // Title of the quote
$res["dates"], // Date the quote was created
$res["likes"], // Number of likes the quote has received
$res["langue"] // Language of the quote
);
}
}
/**
* Retrieves all favorite quotes for a specified user.
*
* @param string $userId The unique identifier of the user.
*
* @return Quote[] Returns an array of Quote objects representing the user's favorite quotes.
*/
public function getFavorites(string $userId): array {
// Fetch the user's favorite quotes from the gateway's getFavorites method.
$res = $this->gateway->getFavorites($userId);
$tabQ=[];
foreach($res as $q ){
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"],0,150).'...' : $q["content"];
$tabQ[]= new Quote($q["id_quote"],$q["content"],$q["caracter"],$q["imgpath"],$q["title"],$q["dates"],$q["likes"],$q["langue"]) ;
// Initialize an empty array to store the resulting Quote objects.
$tabQ = [];
// Loop through the result set and create Quote objects.
foreach ($res as $q) {
// Truncate the content if it's longer than 153 characters, appending '...' if necessary.
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"], 0, 150) . '...' : $q["content"];
// Create a new Quote object and add it to the tabQ array.
$tabQ[] = new Quote(
$q["id_quote"], // Quote ID
$q["content"], // Truncated content of the quote
$q["caracter"], // Character associated with the quote
$q["imgpath"], // Image path related to the quote
$q["title"], // Title of the quote
$q["dates"], // Date the quote was created
$q["likes"], // Number of likes the quote has received
$q["langue"] // Language of the quote
);
}
// Return the array of favorite Quote objects.
return $tabQ;
}
public function insert4User(string $content, string $img_path, string $langage, int $user, int $source, int $character) : bool
{
/**
* Inserts a new quote for a specific user.
*
* @param string $content The content of the quote.
* @param string $img_path The image path associated with the quote.
* @param string $langage The language of the quote.
* @param int $user The ID of the user associated with the quote.
* @param int $source The source of the quote (e.g., the origin of the quote or where it was sourced from).
* @param int $character The character associated with the quote.
*
* @return bool Returns true if the quote was successfully inserted, otherwise false.
*/
public function insert4User(string $content, string $img_path, string $langage, int $user, int $source, int $character): bool {
// Call the gateway's insert4User method to insert the new quote into the database.
return $this->gateway->insert4User($content, $img_path, $langage, $user, $source, $character);
}

@ -1,91 +0,0 @@
<?php
namespace Model;
use Entity\ResultsEntity;
use Gateway\ResultsGateway;
use Gateway\Gateway;
class ResultsModel extends Model
{
public function createResultsModel(int $idQuiz, int $idUser, int $score, int $time) : bool
{
return $this -> gateway -> createResultsGateway($idQuiz, $idUser, $score, $time);
}
public function getResultsByQuiz(int $idQuiz) : array
{
$res = $this -> gateway -> findResultsByQuiz($idQuiz);
$results = [];
foreach ($res as $result)
{
$results[] = new ResultsEntity (
$result['user_r'],
$result['quiz_r'],
$result['score'],
$result['time']
);
}
return $results;
}
public function getResultsByUser(int $idUser) : array
{
$res = $this -> gateway -> findResultsByUser($idUser);
$results = [];
foreach ($res as $result)
{
$results[] = new ResultsEntity (
$result['user_r'],
$result['quiz_r'],
$result['score'],
$result['time']
);
}
return $results;
}
public function getResultsById(int $idQuiz, int $idUser) : ?ResultsEntity
{
$res = $this -> gateway -> findResultsById($idQuiz, $idUser);
if ($res)
{
return new ResultsEntity (
$res['user_r'],
$res['quiz_r'],
$res['score'],
$res['time']
);
}
return null;
}
public function getAllResults() : array
{
$res = $this -> gateway -> findAllResults();
$results = [];
foreach ($res as $result)
{
$results[] = new ResultsEntity (
$result['user_r'],
$result['quiz_r'],
$result['score'],
$result['time']
);
}
return $results;
}
public function updateResultsModel(int $idQuiz, int $idUser, ?int $score, ?int $time) : bool
{
return $this -> gateway -> updateResults($idQuiz, $idUser, $score, $time);
}
}

@ -7,15 +7,41 @@
class SearchModel extends Model
{
public function searchQuote(?string $type,?string $search,array $filtre): array{
$res = $this->gateway->search($type,$search,$filtre);
$tabQ=[];
/**
* Searches for quotes based on specified filters and search criteria.
*
* @param string|null $type The type of quote to search for (optional).
* @param string|null $search The search term to filter quotes (optional).
* @param array $filtre Additional filters to refine the search (e.g., date range, category).
*
* @return Quote[] Returns an array of Quote objects that match the search criteria and filters.
*/
public function searchQuote(?string $type, ?string $search, array $filtre): array {
// Call the gateway's search method with the provided search criteria and filters.
$res = $this->gateway->search($type, $search, $filtre);
foreach($res as $q ){
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"],0,150).'...' : $q["content"];
$tabQ[]= new Quote($q["id_quote"],$q["content"],$q["caracter"],$q["imgpath"],$q["title"],$q["dates"],$q["likes"],$q["langue"]) ;
// Initialize an empty array to store the resulting Quote objects.
$tabQ = [];
// Loop through the search results.
foreach ($res as $q) {
// Truncate the quote content to 150 characters, adding '...' if it's too long.
$q["content"] = (strlen($q["content"]) > 153) ? substr($q["content"], 0, 150) . '...' : $q["content"];
// Create a new Quote object and add it to the tabQ array.
$tabQ[] = new Quote(
$q["id_quote"], // Quote ID
$q["content"], // The content of the quote (truncated if too long)
$q["caracter"], // The character associated with the quote
$q["imgpath"], // The image path associated with the quote (e.g., character image)
$q["title"], // The title of the quote
$q["dates"], // The date when the quote was made
$q["likes"], // The number of likes the quote has received
$q["langue"] // The language of the quote
);
}
// Return the array of Quote objects.
return $tabQ;
}
}

@ -1,112 +1,126 @@
<?php
namespace Model;
use Entity\SourceEntity;
use Enum\TypeSourceEnum;
class SourceModel extends Model
{
/**
* Creates a new source with the provided title, date, and type.
*
* @param string $title The title of the source.
* @param string $date The date associated with the source.
* @param TypeSourceEnum $type The type of the source (e.g., Movie, Book, etc.).
*
* @return bool Returns true if the source was successfully created, false otherwise.
*/
public function createSource(string $title, string $date, TypeSourceEnum $type) : bool
{
$q = new SourceEntity(-1,$title, $date, TypeSourceEnum::Movie/*$type*/);
// Create a new SourceEntity with the given title, date, and type.
// Note: The type is hardcoded as "Movie" in this implementation.
$q = new SourceEntity(-1, $title, $date, TypeSourceEnum::Movie);
return $this -> gateway -> create($q);
// Use the gateway to insert the new source and return the result.
return $this->gateway->create($q);
}
/**
* Retrieves a source by its unique identifier.
*
* @param int $id_source The unique identifier of the source.
*
* @return SourceEntity|null Returns a SourceEntity object if found, or null if no source is found with the given ID.
*/
public function getSourceById(int $id_source) : ?SourceEntity
{
$res = $this -> gateway -> findById($id_source);
if ($res)
return new sourceEntity(
$res[0]["id_source"],
$res[0]["title"],
$res[0]["dates"],
TypeSourceEnum::Movie//from($res[0]["type"])
// Fetch the source by ID using the gateway's findById method.
$res = $this->gateway->findById($id_source);
// If a source is found, return a SourceEntity object.
if ($res) {
return new SourceEntity(
$res[0]["id_source"], // Source ID
$res[0]["title"], // Title of the source
$res[0]["dates"], // Date associated with the source
TypeSourceEnum::Movie // Hardcoded to "Movie" type; you may consider dynamically mapping this
);
}
// Return null if no source is found.
return null;
}
/**
* Retrieves a source by its title.
*
* @param string $title The title of the source to search for.
*
* @return SourceEntity|null Returns a SourceEntity object if found, or null if no source is found with the given title.
*/
public function getSourceByTitle(string $title) : ?SourceEntity
{
// Fetch the source by title using the gateway's findByTitle method.
$res = $this->gateway->findByTitle($title);
if ($res)
return new sourceEntity(
$res[0]["id_source"],
$res[0]["title"],
$res[0]["dates"],
TypeSourceEnum::Movie//from($res[0]["type"])
);
return null;
}
public function getSourceByDate(string $date) : array
{
$res = $this->gateway->findByDate($date);
$src = [];
foreach ($res as $sources) {
$src[] = new sourceEntity(
$sources["id_source"],
$sources["title"],
$sources["dates"],
TypeSourceEnum::from($sources["type"])
);
}
return $src;
}
public function getSourceByType(TypeSourceEnum $type) : array
{
$res = $this->gateway->findByType($type);
$src = [];
foreach ($res as $sources) {
$src[] = new sourceEntity(
$sources["id_source"],
$sources["title"],
$sources["dates"],
TypeSourceEnum::from($sources["type"])
// If a source is found, return a SourceEntity object.
if ($res) {
return new SourceEntity(
$res[0]["id_source"], // Source ID
$res[0]["title"], // Title of the source
$res[0]["dates"], // Date associated with the source
TypeSourceEnum::Movie // Hardcoded to "Movie" type; dynamic mapping could be considered
);
}
return $src;
// Return null if no source is found.
return null;
}
/**
* Retrieves all sources from the database.
*
* @return SourceEntity[] Returns an array of SourceEntity objects representing all the sources.
*/
public function getAllSources() : array
{
$res = $this -> gateway -> findAll();
// Fetch all sources from the gateway's findAll method.
$res = $this->gateway->findAll();
// Initialize an empty array to store the SourceEntity objects.
$src = [];
// Loop through the result and create a SourceEntity object for each source.
foreach ($res as $sources) {
$src[] = new SourceEntity(
$sources["id_source"],
$sources["title"],
$sources["dates"],
TypeSourceEnum::Movie
//TypeSourceEnum::from($sources["type"])
$sources["id_source"], // Source ID
$sources["title"], // Title of the source
$sources["dates"], // Date associated with the source
TypeSourceEnum::Movie // Hardcoded to "Movie" type; you may consider dynamically mapping this
);
}
return $src;
}
public function deleteSource(int $id_source) : bool
{
return $this -> gateway -> delete($id_source);
// Return the array of SourceEntity objects.
return $src;
}
public function updateSource(int $id_source, string $title, string $date) : bool
/**
* Checks if a source with the given name and type exists.
*
* @param string $name The name of the source to check.
* @param string $type The type of the source to check (e.g., Movie, Book, etc.).
*
* @return bool Returns true if the source exists, false otherwise.
*/
public function existSource(string $name, string $type) : bool
{
$q = $this -> getSourceById($id_source);
// Retrieve the source by title using the getSourceByTitle method.
$q = $this->getSourceByTitle($name);
if ($q){
$q -> setTitle($title);
$q -> setDate($date);
return $this -> gateway -> update($q);
}
return false;
}
public function existSource(string $name, string $type) : bool{
$q = $this -> getSourceByTitle($name);
// Check if the source exists and return the result.
return isset($q[0]);
}
}
?>

@ -8,51 +8,55 @@
class UserModel extends Model
{
public function insertUser(string $username,string $email,string $passwd) : bool{
/*global $rep,$image;*/
return $this->gateway->insertUser( $username, $email, $passwd, false, 1);
/**
* Inserts a new user with the provided username, email, and password.
* The account is created with default settings (active and role 1).
*
* @param string $username The username for the new user.
* @param string $email The email address for the new user.
* @param string $passwd The password for the new user.
*
* @return bool Returns true if the user was successfully created, false otherwise.
*/
public function insertUser(string $username, string $email, string $passwd) : bool {
// Calls the gateway method to insert a new user with default settings.
return $this->gateway->insertUser($username, $email, $passwd, false, 1);
}
public function deleteUser(string $id) : bool{
/**
* Deletes a user by their ID.
*
* @param string $id The ID of the user to be deleted.
*
* @return bool Returns true if the user was successfully deleted, false otherwise.
*/
public function deleteUser(string $id) : bool {
// Calls the gateway method to delete the user by their ID.
return $this->gateway->delete($id);
}
// ===================== Get FUNCTION =====================
public function getNumberOfUsers() : int
{
/**
* Retrieves the total number of users in the system.
*
* @return int The total number of users.
*/
public function getNumberOfUsers() : int {
// Calls the gateway method to get the number of users and returns the count.
return $this->gateway->getNumberOfUsers()[0]['count'] ?? 0;
}
// public function getFavoriteUser(string $id) : array{
// $res[0] = array();
// $data = $this->gateway->getFavorite($id);
// foreach ($data as $favoris) {
// $res[0][] = new Quote();
// }
// }
public function getDataUser(int $id) : ?UserEntity {
$res = $this->gateway->findDataUser($id);
if ($res)
return new UserEntity(
$res[0]['id_user'],
$res[0]['username'],
$res[0]['password'],
$res[0]['email'],
$res[0]['img'],
$res[0]['creation']
);
return null;
}
public function getUsername(string $username) : ?UserEntity
{
/**
* Retrieves a user by their username.
*
* @param string $username The username of the user to retrieve.
*
* @return UserEntity|null Returns a UserEntity object if the user is found, or null if not.
*/
public function getUsername(string $username) : ?UserEntity {
// Fetches the user details using the gateway method findUsername.
$res = $this->gateway->findUsername($username);
// If the user is found, return a UserEntity object with the user's details.
if ($res)
return new UserEntity(
$res[0]['id_user'],
@ -62,12 +66,22 @@
$res[0]['imgpath'],
$res[0]['creation']
);
// Return null if no user is found.
return null;
}
public function getEmail(string $email) : ?UserEntity
{
/**
* Retrieves a user by their email.
*
* @param string $email The email of the user to retrieve.
*
* @return UserEntity|null Returns a UserEntity object if the user is found, or null if not.
*/
public function getEmail(string $email) : ?UserEntity {
// Fetches the user details using the gateway method findEmail.
$res = $this->gateway->findEmail($email);
// If the user is found, return a UserEntity object with the user's details.
if ($res)
return new UserEntity(
$res[0]['id_user'],
@ -77,115 +91,214 @@
$res[0]['img'],
$res[0]['creation']
);
// Return null if no user is found.
return null;
}
public function getIdByUsername(string $username){
/**
* Retrieves the user ID based on the username.
*
* @param string $username The username of the user.
*
* @return int The user ID.
*/
public function getIdByUsername(string $username) {
// Fetches the user ID using the gateway method getIdUser.
$res = $this->gateway->getIdUser($username);
return $res[0]['id_user'];
}
public function getEmailWithUser(string $user){
$res = $this->gateway->emailWithUser($user);
return $res[0]['email'];
}
// ===================== Bool FUNCTION =====================
public function IsExisteUsername(string $username):bool{
/**
* Checks if a given username already exists.
*
* @param string $username The username to check.
*
* @return bool Returns true if the username exists, false otherwise.
*/
public function IsExisteUsername(string $username) : bool {
// Checks if the username exists using the gateway method.
return $this->gateway->IsExisteUsername($username);
}
public function IsExisteEmail(string $email):bool{
/**
* Checks if a given email already exists.
*
* @param string $email The email to check.
*
* @return bool Returns true if the email exists, false otherwise.
*/
public function IsExisteEmail(string $email) : bool {
// Checks if the email exists using the gateway method.
return $this->gateway->IsExisteEmail($email);
}
public function isPassWd(string $username, string $passWd): bool {
/**
* Verifies if the provided password matches the stored password hash for the given username.
*
* @param string $username The username for the user.
* @param string $passWd The password to verify.
*
* @return bool Returns true if the password is correct, false otherwise.
*/
public function isPassWd(string $username, string $passWd) : bool {
// Fetches the stored password hash for the username.
$hash = $this->gateway->getPasswordHash($username);
// Verifies the password using password_verify.
return $hash !== null && password_verify($passWd, $hash);
}
public function isFavorite(?string $username, int $idq): bool {
if($_SESSION["user"] == NULL){
/**
* Checks if a quote is marked as a favorite by the current user.
*
* @param string|null $username The username of the user (can be null).
* @param int $idq The ID of the quote to check.
*
* @return bool Returns true if the quote is a favorite, false otherwise.
*/
public function isFavorite(?string $username, int $idq) : bool {
// If the user is not logged in (session is null), return false.
if ($_SESSION["user"] == NULL) {
return false;
}
else{
$res = $this->gateway->inFavorite($_SESSION["user"],$idq);
} else {
// Checks if the quote is in the user's favorites.
$res = $this->gateway->inFavorite($_SESSION["user"], $idq);
return $res;
}
}
// ===================== Set FUNCTION =====================
public function setUsername(string $username, string $newUsername): string {
if ($this->IsExisteUsername($newUsername)) {// Vérifier si le nouveau nom d'utilisateur existe déjà
return $username;// Retourne l'ancien nom d'utilisateur sans modification
/**
* Updates the username for a user.
*
* @param string $username The current username.
* @param string $newUsername The new username to set.
*
* @return string Returns the new username if it was successfully updated, or the current username if the new one already exists.
*/
public function setUsername(string $username, string $newUsername) : string {
// If the new username already exists, return the current username.
if ($this->IsExisteUsername($newUsername)) {
return $username;
}
$res = $this->gateway->updateUsername($username, $newUsername);// Sinon, mettre à jour le nom d'utilisateur
// Retourner le nouveau nom d'utilisateur après modification
// Updates the username using the gateway method.
$res = $this->gateway->updateUsername($username, $newUsername);
// If the update was successful, return the new username.
if (!empty($res) && isset($res[0]['username'])) {
return $res[0]['username'];
}
return $username; // En cas d'échec, retourne l'ancien nom d'utilisateur
// Return the original username if the update was unsuccessful.
return $username;
}
public function setEmail(string $username, string $newEmail){
/**
* Updates the email for a user.
*
* @param string $username The current username.
* @param string $newEmail The new email to set.
*
* @return string Returns the new email if it was successfully updated, or the current username if the new email already exists.
*/
public function setEmail(string $username, string $newEmail) {
// If the new email already exists, return the current username.
if ($this->IsExisteEmail($newEmail)) {
return $username;
}
$res = $this->gateway->updateEmail($username,$newEmail);
// Updates the email using the gateway method.
$res = $this->gateway->updateEmail($username, $newEmail);
// If the update was successful, return the new email.
if (!empty($res) && isset($res[0]['email'])) {
return $res[0]['email'];
}
return $username;// En cas d'échec, retourne l'ancien email
}
// Return the original username if the update was unsuccessful.
return $username;
}
public function setImage(string $username,string $newImage){
$res = $this->gateway->updateImg($username,$newImage);
/**
* Updates the profile image for a user.
*
* @param string $username The username of the user.
* @param string $newImage The new image path to set.
*
* @return array Returns an array with the new image path.
*/
public function setImage(string $username, string $newImage) {
// Updates the image path using the gateway method.
$res = $this->gateway->updateImg($username, $newImage);
// Returns the updated image path.
$src[] = $res[0]['img'];
return $src;
}
public function setPassWd(string $username, string $newPassWd):void{
$res = $this->gateway->updatePasswd($username,$newPassWd);
/**
* Updates the password for a user.
*
* @param string $username The username of the user.
* @param string $newPassWd The new password to set.
*/
public function setPassWd(string $username, string $newPassWd) : void {
// Updates the password using the gateway method.
$res = $this->gateway->updatePasswd($username, $newPassWd);
}
public function addFavorite(string $username, int $id){
$this->gateway->addFavorite($username,$id);
/**
* Adds a quote to the user's favorites.
*
* @param string $username The username of the user.
* @param int $id The ID of the quote to add.
*/
public function addFavorite(string $username, int $id) {
// Adds the quote to the user's favorites.
$this->gateway->addFavorite($username, $id);
}
public function supFavorite(string $username, int $id){
$this->gateway->supFavorite($username,$id);
/**
* Removes a quote from the user's favorites.
*
* @param string $username The username of the user.
* @param int $id The ID of the quote to remove.
*/
public function supFavorite(string $username, int $id) {
// Removes the quote from the user's favorites.
$this->gateway->supFavorite($username, $id);
}
// ===================== DELETE FUNCTION =====================
public function deleteAllCommentary(string $username){
/**
* Deletes all comments made by the specified user.
*
* @param string $username The username of the user.
*/
public function deleteAllCommentary(string $username) {
// Deletes all comments made by the user.
$this->gateway->deleteAllCommentaryUser($username);
}
public function deleteAllFavorite(string $username){
/**
* Deletes all favorite quotes of the specified user.
*
* @param string $username The username of the user.
*/
public function deleteAllFavorite(string $username) {
// Deletes all favorite quotes of the user.
$this->gateway->deleteAllFavoriteUser($username);
}
public function deleteAccount(string $username){
/**
* Deletes the user's account.
*
* @param string $username The username of the user to delete.
*/
public function deleteAccount(string $username) {
// Deletes the user's account.
$this->gateway->deleteUser($username);
}
}
?>

Loading…
Cancel
Save