/** Get quizz data for id $quizzId
  * (here demo with a SQL request about an existing table)
  * @param int $quizzId id of the quizz to be retrieved
  * @return associative_array table row
  */
 public static function get($quizzId)
 {
     $db = DemoDB::getConnection();
     $sql = "SELECT person_id, name\n              FROM person\n              WHERE person_id = :quizz_id";
     $stmt = $db->prepare($sql);
     $stmt->bindValue(":quizz_id", $quizzId);
     $ok = $stmt->execute();
     if ($ok) {
         return $stmt->fetch(PDO::FETCH_ASSOC);
     }
 }
function getStudentClassData($student_id)
{
    try {
        $db = DemoDB::getConnection();
        $sql = "SELECT s.class_id, c.name FROM STUDENT as s, Class as c where s.class_id=c.id and s.student_id=:id";
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $student_id, PDO::PARAM_INT);
        $ok = $stmt->execute();
        if ($ok) {
            $nb = $stmt->rowCount();
            if ($nb == 0) {
                // student does not belong to any class
                return;
            }
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($row != null) {
                $_SESSION["login_user_class_id"] = $row["class_id"];
                $_SESSION["login_user_class_name"] = $row["name"];
            } else {
                echo 'should not happen';
                return;
            }
        } else {
            echo 'error while getting student class information';
            return;
        }
    } catch (PDOException $e) {
        print_r($e->getMessage());
        return false;
    }
}
 protected function do_delete()
 {
     if (!$this->is_admin()) {
         $this->exit_error(401);
     }
     if (empty($_GET["id"])) {
         $this->exit_error(400, "idRequired");
     }
     try {
         $db = DemoDB::getConnection();
         $sql = "DELETE FROM person WHERE person_id=:id";
         $stmt = $db->prepare($sql);
         $stmt->bindValue(":id", $this->id);
         $ok = $stmt->execute();
         if ($ok) {
             $this->statusCode = 204;
             $this->body = "";
             $nb = $stmt->rowCount();
             if ($nb == 0) {
                 $this->exit_error(404);
             }
         } else {
             $erreur = $stmt->errorInfo();
             $this->exit_error(409, $erreur[1] . " : " . $erreur[2]);
         }
     } catch (PDOException $e) {
         $this->exit_error(500, $e->getMessage());
     }
 }
function getProjectIds()
{
    try {
        $db = DemoDB::getConnection();
        $sql = "select id, title FROM project WHERE id not in (select project_id from team where team_owner_id=" . $_SESSION["login_user_id"] . ") and id not in (select project_id from team_membership where student_id=" . $_SESSION["login_user_id"] . ")" . " and class_id=" . $_SESSION["login_user_class_id"] . ";";
        //$sql = "SELECT id FROM project;";
        $stmt = $db->prepare($sql);
        // $stmt->bindValue(":team_owner_id", $_GET["team_owner_id"], PDO::PARAM_INT);
        $ok = $stmt->execute();
        if ($ok) {
            $nb = $stmt->rowCount();
            $result = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                if ($row != null) {
                    array_push($result, $row);
                    //$result[$row["id"]][] =$row;
                    //return $row;
                }
            }
            return $result;
        } else {
            echo 'dddd';
            $erreur = $stmt->errorInfo();
            print_r($erreur);
            // si doublon
            if ($erreur[1] == 1062) {
                print_r($erreur[1]);
            } else {
                print_r($erreur[2]);
            }
            //$this->exit_error(500, print_r($db->errorInfo(), true));
        }
    } catch (PDOException $e) {
        //$this->exit_error(500, $e->getMessage());
    }
}
 function getStudentById()
 {
     try {
         $db = DemoDB::getConnection();
         $sql = "SELECT * FROM student WHERE student_id=:student_id";
         $stmt = $db->prepare($sql);
         $stmt->bindValue(":student_id", $this->id);
         $ok = $stmt->execute();
         if ($ok) {
             $row = $stmt->fetch(PDO::FETCH_ASSOC);
             $nb = $stmt->rowCount();
             if ($nb == 0) {
                 $this->exit_error(404, 'studentNotFound');
             }
             if ($row != null) {
                 $this->statusCode = 200;
                 // Produce utf8 encoded json
                 $this->headers[] = "Content-type: text/json; charset=utf-8";
                 $this->body = json_encode($row);
             } else {
                 $this->exit_error(404);
             }
         } else {
             echo 'here';
             $this->exit_error(500, print_r($db->errorInfo(), true));
         }
     } catch (PDOException $e) {
         $this->exit_error(500, $e->getMessage());
     }
 }