Browse Source

Merge remote-tracking branch 'origin/master'

PCYoshi 9 years ago
parent
commit
c2684d4664
7 changed files with 145 additions and 26 deletions
  1. 9
    2
      web/index.php
  2. 40
    8
      web/model/class.connector.php
  3. 75
    0
      web/model/class.score.php
  4. 1
    0
      web/model/classes.php
  5. 9
    0
      web/tests.php
  6. 10
    14
      web/view/palmares.php
  7. 1
    2
      web/view/questions.php

+ 9
- 2
web/index.php View File

@@ -4,5 +4,12 @@ require_once('model/classes.php');
4 4
 
5 5
 header("Content-Type:application/json");
6 6
 
7
-echo json_encode(array("Message" => "Hello world;"));
8
-
7
+if(!empty($_GET['page'])) {
8
+  if($_GET['page'] == "palmares") {
9
+    include("view/palmares.php");
10
+    echo json_encode($scArray);
11
+  }
12
+} else {
13
+  include("view/questions.php");
14
+	echo json_encode($categories);
15
+}

+ 40
- 8
web/model/class.connector.php View File

@@ -1,9 +1,9 @@
1
-<?php 
1
+<?php
2 2
 
3 3
 class Connector {
4 4
 
5 5
 	private $bdd;
6
-	
6
+
7 7
 	function __construct() {
8 8
 		$host = "localhost";
9 9
 		$db = "burgerquizz";
@@ -41,13 +41,9 @@ class Connector {
41 41
 					$whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
42 42
 					$arrayVerif[":".$array[0]] = $array[2];
43 43
 				}
44
-				if($substring = substr($whereClause, 0, -5)) {
45
-					$request .= $substring;
46
-				} else {
47
-					throw new Exception('Problème lors de la création du substring');
48
-				}
44
+				$request .= substr($whereClause, 0, -5);
49 45
 			} else if(($upName = strtoupper($name)) == "ORDER BY") {
50
-				if(sizeof($value) != 2 && substr($value[0], -2) != "()") { 
46
+				if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
51 47
 					throw new Exception('Nombre de paramètres incorrects (ORDER BY). Les paramètres passés sont : '
52 48
 						.implode(',', $value));
53 49
 				}
@@ -78,6 +74,42 @@ class Connector {
78 74
 		}
79 75
 	}
80 76
 
77
+	function Insert($table, $values) {
78
+		$request = "INSERT INTO $table(";
79
+		$valeurs = "VALUES(";
80
+		$arrayVerif = array();
81
+		foreach($values as $name=>$value) {
82
+			$request .= $name.",";
83
+			$valeurs .= "?,";
84
+				array_push($arrayVerif, $value);
85
+		}
86
+
87
+		$request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
88
+
89
+		$stmt = $this->bdd->prepare($request);
90
+
91
+		$stmt->execute($arrayVerif);
92
+	}
93
+
94
+	function Update($table, $update) {
95
+		$request = "UPDATE $table SET ";
96
+		$arrayVerif = array();
97
+		foreach($update['set'] as $name=>$value) {
98
+			$request .= $name."=?,";
99
+			array_push($arrayVerif, $value);
100
+		}
101
+		$request = substr($request, 0, -1)." WHERE ";
102
+		var_dump($request);
103
+		foreach($update['where'] as $value) {
104
+			$request .= $value[0].$value[1]."? AND ";
105
+			array_push($arrayVerif, $value[2]);			
106
+		}
107
+		$request = substr($request, 0, -5);
108
+
109
+		$stmt = $this->bdd->prepare($request);
110
+		$stmt->execute($arrayVerif);
111
+	}
112
+
81 113
 	function beginTransaction() {
82 114
 		$this->bdd->beginTransaction();
83 115
 	}

+ 75
- 0
web/model/class.score.php View File

@@ -0,0 +1,75 @@
1
+<?php
2
+
3
+class Score {
4
+  private $login;
5
+  private $score;
6
+  private $bdd;
7
+
8
+  function __construct($login) {
9
+    $this->bdd = new Connector();
10
+
11
+    $options = array(
12
+      "where" => array(
13
+        array("login", "=", $login)
14
+      )
15
+    );
16
+
17
+    $data = $this->bdd->Select("*", "scores", $options);
18
+    $this->login = $data[0]['login'];
19
+    $this->score = $data[0]['score'];
20
+  }
21
+
22
+  function getLogin() {
23
+    return $this->login;
24
+  }
25
+
26
+  function getScore() {
27
+    return $this->score;
28
+  }
29
+
30
+  public static function getScores($nRows, $direction = "desc") {
31
+    $bdd = new Connector();
32
+
33
+    $options = array(
34
+      "order by" => array("score", $direction),
35
+      "limit" => array($nRows)
36
+    );
37
+
38
+    $array = $bdd->Select("*", "scores", $options);
39
+    $scores = array();
40
+    foreach($array as $score) {
41
+      array_push($scores, new Score($score['login']));
42
+    }
43
+
44
+    return $scores;
45
+  }
46
+
47
+  public static function add($login, $score) {
48
+    $bdd = new Connector();
49
+
50
+    $options = array(
51
+      "where" => array(
52
+        array("login", "=", $login)
53
+      )
54
+    );
55
+
56
+    if(!$scores = $bdd->Select("*", "scores", $options)) {
57
+      $values = array(
58
+        "login" => $login,
59
+        "score" => $score
60
+      );
61
+
62
+      $bdd->Insert("scores", $values);
63
+    } else {
64
+      if($score > $scores[0]['score']) {
65
+        $update = array(
66
+          "where" => array(
67
+            array("login", "=", $login)
68
+          ),
69
+          "set" => array("score" => $score)
70
+        );
71
+        $bdd->Update("scores", $update);
72
+      }
73
+    }
74
+  }
75
+}

+ 1
- 0
web/model/classes.php View File

@@ -5,3 +5,4 @@ require_once('model/class.connector.php');
5 5
 require_once('model/class.question.php');
6 6
 require_once('model/class.questset.php');
7 7
 require_once('model/class.categorie.php');
8
+require_once('model/class.score.php');

+ 9
- 0
web/tests.php View File

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+require_once('model/classes.php');
4
+
5
+try {
6
+	Score::add("Yolo", 200);
7
+}catch(Exception $e) {
8
+	echo("Erreur : ".$e->getMessage());
9
+}

+ 10
- 14
web/view/palmares.php View File

@@ -1,18 +1,14 @@
1 1
 <?php
2 2
 
3
-require_once('../model/classes.php');
4
-
5
-$bdd = new Connector();
6
-
7
-$options = array(
8
-	"order by" => array("score", "desc"),
9
-	"limit" => array(10)
10
-);
11
-
12 3
 try {
13
-	$res = $bdd->Select("*", "scores", $options);
14
-} catch(Exception $e) {
15
-	echo("Erreur : ".$e->getMessage());
4
+	$scores = Score::getScores(10);
5
+	$scArray = array();
6
+	for($i = 0; $i < sizeof($scores); $i++) {
7
+		array_push($scArray, array(
8
+			"login" => utf8_encode($scores[$i]->getLogin()),
9
+			"score" => $scores[$i]->getScore()
10
+		));
11
+	}
12
+}catch(Exception $e) {
13
+	echo json_encode(array("erreur" => $e->getMessage()));
16 14
 }
17
-
18
-// $res contient les résultats

web/questions.php → web/view/questions.php View File

@@ -11,7 +11,6 @@ try{
11 11
 	foreach($categoriesObj as $categorie) {
12 12
 		array_push($categories, $categorie->getArray());
13 13
 	}
14
-	echo json_encode($categories);
15 14
 }catch(Exception $e) {
16
-	echo("<strong>Erreur : </strong>".$e->getMessage());
15
+	echo json_encode(array("erreur" => $e->getMessage()));
17 16
 }