Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiiwen committed Mar 24, 2022
1 parent c3b9e00 commit ab50457
Show file tree
Hide file tree
Showing 17 changed files with 713 additions and 0 deletions.
75 changes: 75 additions & 0 deletions app/Entity/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

class Answer
{
private int $id;
private string $title;
private bool $isRight;

public function __construct($array)
{
$this->setTitle($array['answer_title']);
$this->setId($array['answer_id']);
$this->setIsRight($array['answer_isRight']);
}

/**
* Get the value of answer
*/
public function getTitle()
{
return $this->title;
}

/**
* Set the value of answer
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get the value of id
*/
public function getId()
{
return $this->id;
}

/**
* Set the value of id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;

return $this;
}

/**
* Get the value of isRight
*/
public function getIsRight()
{
return $this->isRight;
}

/**
* Set the value of isRight
*
* @return self
*/
public function setIsRight($isRight)
{
$this->isRight = $isRight;

return $this;
}
}
77 changes: 77 additions & 0 deletions app/Entity/QCM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

class QCM
{
private int $id;
private string $title;
private array $questions;

/**
* Get the value of questions
*
* @return array $questions
*/
public function __construct($array)
{
$this->setTitle($array['qcm_title'])->setId($array['qcm_id']);
}
public function getQuestions()
{
return $this->questions;
}
/**
* Add a question
* @param Question $question
* @return Question
*/
public function addQuestions(Question $question)
{
$this->questions[] = $question;
return $this->questions[sizeof($this->questions) - 1];
}

public function show()
{
include '../app/includes/show.php';
}

/**
* Get the value of title
*/
public function getTitle()
{
return $this->title;
}

/**
* Set the value of title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get the value of id
*/
public function getId()
{
return $this->id;
}

/**
* Set the value of id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;

return $this;
}
}
76 changes: 76 additions & 0 deletions app/Entity/Question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

class Question
{
private int $id;
private string $title;
private array $answers;

public function __construct($array)
{
$this->setTitle($array['question_title']);
$this->setId($array['question_id']);
}
/**
* Get the value of answers
*
* @return array $answers
*/

public function getAnswers()
{
return $this->answers;
}

/**
* Add an Answer
* @param Answer $answer
* @return self
*/
public function addAnswers(Answer $answer)
{
$this->answers[] = $answer;

return $this;
}

/**
* Get the value of question
*/
public function getTitle()
{
return $this->title;
}

/**
* Set the value of question
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get the value of id
*/
public function getId()
{
return $this->id;
}

/**
* Set the value of id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;

return $this;
}
}
68 changes: 68 additions & 0 deletions app/Manager/AnswerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

class AnswerManager
{

private static $pdo;

public static function getPdoInstance()
{
if (self::$pdo == NULL) // Je créer un singleton de PDO ici dans le but de ne pas l'instancier à chaque appel de la méthode
{
try {
self::$pdo = new PDO('mysql:host=localhost;dbname=qcm', 'root', '');
} catch (PDOException $e) {
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
}

return self::$pdo;
}

public static function getAll()
{
$pdo = self::getPdoInstance();

$sql = "SELECT * FROM answers";
$req = $pdo->query($sql);
$products = $req->fetchAll(PDO::FETCH_ASSOC);

return self::hydrateCollection($products);
}

public static function get(int $id)
{
$pdo = self::getPdoInstance();

$sql = "SELECT * FROM answers WHERE answer_id = :id";
$req = $pdo->prepare($sql);
$req->execute([
'id' => $id
]);
$product = $req->fetch(PDO::FETCH_ASSOC);
return new Answer($product);
}

public static function getFromQuestion(int $id)
{
$pdo = self::getPdoInstance();

$sql = "SELECT * FROM answers WHERE question_id = :id";
$req = $pdo->prepare($sql);
$req->execute([
'id' => $id
]);
$items = $req->fetchAll(PDO::FETCH_ASSOC);
return self::hydrateCollection($items);
}

private static function hydrateCollection(array $collection)
{
foreach ($collection as $index => $productInfo) {
$collection[$index] = new Answer($productInfo);
}

return $collection;
}
}
56 changes: 56 additions & 0 deletions app/Manager/QcmManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class QcmManager
{

private static $pdo;

public static function getPdoInstance()
{
if (self::$pdo == NULL) // Je créer un singleton de PDO ici dans le but de ne pas l'instancier à chaque appel de la méthode
{
try {
self::$pdo = new PDO('mysql:host=localhost;dbname=qcm', 'root', '');
} catch (PDOException $e) {
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
}

return self::$pdo;
}

public static function getAll()
{
$pdo = self::getPdoInstance();

$sql = "SELECT * FROM qcm";
$req = $pdo->query($sql);
$products = $req->fetchAll(PDO::FETCH_ASSOC);

// return $products;
return self::hydrateCollection($products);
}

public static function get(int $id)
{
$pdo = self::getPdoInstance();

$sql = "SELECT * FROM qcm WHERE qcm_id = :id";
$req = $pdo->prepare($sql);
$req->execute([
'id' => $id
]);
$product = $req->fetch(PDO::FETCH_ASSOC);
return new QCM($product);
}

private static function hydrateCollection(array $collection)
{
foreach ($collection as $index => $productInfo) {
$collection[$index] = new QCM($productInfo);
}

return $collection;
}
}
Loading

0 comments on commit ab50457

Please sign in to comment.