Implémentation de l'architecture MVC + le site affiche les questions mises sur sqlite + correction des réponses de l'utilisateur

master
Mehdi 5 years ago
parent 3d1d186e80
commit 73f8522837

@ -4,19 +4,28 @@
require_once('SqliteDb.php'); require_once('SqliteDb.php');
header('Content-type: text/plain');
$db = new SqliteDb(); $db = new SqliteDb();
$db->exec('DROP TABLE Correct'); $cmpt = 0;
$db->exec('CREATE TABLE Correct (question STRING, reponse STRING)'); $i = 0;
$db->exec("INSERT INTO Correct VALUES ('Ceci est la question 1 ', 'réponse 1')"); foreach ($_POST['textbox'] as $textbox) {
$i++;
$reponseUser = $_POST['R1']; //$reponseUser = $_POST[''];
$reponseDb = $db->querySingle('SELECT reponse FROM Correct'); $query = $db->prepare('SELECT reponse FROM Correct WHERE rowid= ? ');
$query->bindParam(1, $i);
if ($reponseDb == $reponseUser) { $result = $query->execute();
echo 'Bonne réponse !'; while($row = $result->fetchArray()){
} else { if ($row['reponse'] == $textbox) {
echo 'Mauvaise réponse !'; echo "La réponse à la question n° ".$i." est JUSTE\n";
$cmpt++;
}
else echo 'La réponse à la question n° '.$i." est FAUSSE\n\n";
}
} }
echo 'Résultat ' . $cmpt . '/' . $i;

@ -0,0 +1,23 @@
<?php
//require_once ('Question.php');
class QuestionsGateway {
private $db;
function __construct(SqliteDb $db) {
$this->db=$db;
}
public function afficherQuestions() {
$i = 0;
$query = 'SELECT * FROM Correct';
$query = $this->db->prepare($query);
$result = $query->execute();
//$resultats = $this->db->query('SELECT found_rows()');
while($q = $result->fetchArray()){
$i= $i+1;
$tabQuestions[] = new Question($i,$q['question'],$q['reponse']);
}
return $tabQuestions;
}
}

@ -2,9 +2,18 @@
class SqliteDb extends SQLite3 class SqliteDb extends SQLite3
{ {
function __construct() function __construct()
{ {
$this->open('test.db'); $this->open('test.db');
//$this->createTable();
}
function createTable(){
$this->exec('DROP TABLE Correct');
$this->exec('CREATE TABLE Correct ( question STRING, reponse STRING)');
$this->exec("INSERT INTO Correct VALUES ('Ceci est la question 1 ', 'r1')");
$this->exec("INSERT INTO Correct VALUES ('Ceci est la question 2 ', 'r2')");
$this->exec("INSERT INTO Correct VALUES ('Ceci est la question 3 ', 'r3')");
} }
} }

@ -0,0 +1,55 @@
<?php
class Autoload
{
private static $_instance = null;
public static $limite=3;
public static function setLimite($valeur){
Autoload::$limite=$valeur;
}
public static function getLimite(){
return Autoload::$limite;
}
public static function charger()
{
if(null !== self::$_instance) {
throw new RuntimeException(sprintf('%s is already started', __CLASS__));
}
self::$_instance = new self();
if(!spl_autoload_register(array(self::$_instance, '_autoload'), false)) {
throw RuntimeException(sprintf('%s : Could not start the autoload', __CLASS__));
}
}
public static function shutDown()
{
if(null !== self::$_instance) {
if(!spl_autoload_unregister(array(self::$_instance, '_autoload'))) {
throw new RuntimeException('Could not stop the autoload');
}
self::$_instance = null;
}
}
private static function _autoload($class)
{
global $rep;
$filename = $class.'.php';
$dir =array('modeles/','./','config/','controleur/','DAL/','metier/');
foreach ($dir as $d){
$file=$rep.$d.$filename;
//echo $file;
if (file_exists($file))
{
include $file;
}
}
}
}

@ -0,0 +1,10 @@
<?php
//gen
$rep=__DIR__.'/../';
//Vues
$vues['vuePrincipale']='vues/vuePrincipale.php';

@ -0,0 +1,22 @@
<?php
class Controleur {
function __construct() {
global $rep,$vues;
session_start();
$this->afficherQuestions();
}
function afficherQuestions(){
global $rep,$vues;
$model = new Modele();
$dVueQuestions = $model->afficherQuestions();
require ($rep.$vues['vuePrincipale']);
}
}

@ -10,15 +10,13 @@ and open the template in the editor.
<title></title> <title></title>
</head> </head>
<body> <body>
<?php <?php //chargement config
require_once('SqliteDb.php'); require_once(__DIR__.'/config/config.php');
$db = new SqliteDb(); //chargement autoloader pour autochargement des classes
$Question1 = $db->querySingle('SELECT question FROM Correct'); require_once(__DIR__.'/config/Autoload.php');
?> Autoload::charger();
<form action="Correcteur.php" method="post">
<p><?php echo $Question1?> <br/> <input type="text" name="R1" /></p> $cont = new Controleur();?>
<p><input type="submit" value="Valider"></p>
</form>
</body> </body>
</html> </html>

@ -0,0 +1,44 @@
<?php
class Question {
private $numQuestion;
private $question;
private $reponse;
function __construct($numQuestion,$question,$reponse){
$this->numQuestion = $numQuestion;
$this->question = $question;
$this->reponse = $reponse;
}
// Getters
function getNumQuestion() {
return $this->numQuestion;
}
function getQuestion() {
return $this->question;
}
function getReponse() {
return $this->reponse;
}
// Setters
function setNumQuestion($numQuestion) {
$this->numQuestion = $numQuestion;
}
function setQuestion($question) {
$this->question = $question;
}
function setReponse($reponse) {
$this->reponse = $reponse;
}
}

@ -0,0 +1,16 @@
<?php
class Modele {
function afficherQuestions(){
$db = new SqliteDb();
$db->createTable();
$qg = new QuestionsGateway($db);
$tabQuestions = $qg->afficherQuestions();
return $tabQuestions;
}
}

@ -1,3 +1,6 @@
auxiliary.org-netbeans-modules-php-editor.fluent_2e_setter_2e_project_2e_property=false
auxiliary.org-netbeans-modules-php-editor.getter_2e_setter_2e_method_2e_name_2e_generation=AS_JAVA
auxiliary.org-netbeans-modules-php-editor.public_2e_modifier_2e_project_2e_property=false
copy.src.files=false copy.src.files=false
copy.src.on.open=false copy.src.on.open=false
copy.src.target=C:\\xampp\\htdocs\\PhpProject1 copy.src.target=C:\\xampp\\htdocs\\PhpProject1

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/xampp/htdocs/BddCorrect/controleur/Controleur.php</file>
<file>file:/C:/xampp/htdocs/BddCorrect/modeles/Modele.php</file>
<file>file:/C:/xampp/htdocs/BddCorrect/vues/VuePrincipale.php</file>
<file>file:/C:/xampp/htdocs/BddCorrect/SqliteDb.php</file>
<file>file:/C:/xampp/htdocs/BddCorrect/DAL/QuestionsGateway.php</file>
<file>file:/C:/xampp/htdocs/BddCorrect/Correcteur.php</file>
</group>
</open-files>
</project-private>

Binary file not shown.

@ -0,0 +1,29 @@
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
/*$db = new SqliteDb();
$result = $db->prepare('SELECT question FROM Correct');
$result->execute(array());
$listeQuestion = $result->fetchAll();*/
//$Question1 = $db->querySingle('SELECT question FROM Correct');
?>
<form action="Correcteur.php" method="post">
<?php foreach ($dVueQuestions as $q) { ?>
<p><?php $i=0; $i++; echo $q->getQuestion(); ?> <br/> <input type="text" name="textbox[]" /></p><?php }?>
<p><input type="submit" value="Valider"></p>
</form>
</body>
</html>
Loading…
Cancel
Save