Commit 8352dc98 authored by Stephane Crozat's avatar Stephane Crozat

Refactoring and fixing : issues #10 #12 #13 #16 #15 ; préparation des appels...

Refactoring and fixing : issues #10 #12 #13 #16 #15 ; préparation des appels aux pages resp et admin
parent a984270b
......@@ -2,6 +2,6 @@
## Configuration
### `lib/config.php`
### `TABLE config`
Set IS_ACTIVE to `false` to disable subscription (and unsubscription).
Set `closed` to `TRUE` to disable subscription (and unsubscription).
......@@ -26,25 +26,28 @@ if (!isset($_SESSION['ticket'])) {
include 'lib/db.php';
include 'lib/views.php';
include 'lib/config.php';
include 'lib/admin.php';
$db = new DB();
if ($_SESSION['localcopy']) {
$db->copyUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);
$db->copyUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname'], $_SESSION['mail']);
$_SESSION['localcopy']=false;
}
Views::printHtmlBegin();
Views::printUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);
Views::printInstructions();
$admin = new Admin($db);
$view = new Views($admin, $_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);
$view->printHtmlBegin();
$view->printUser($db->isResp($_SESSION['utclogin']), $db->isAdmin($_SESSION['utclogin']));
$view->printInstructions($db->config());
/** Subscription and unsubscription management **/
if (isset($_GET['api']) && isset($_GET['utclogin'])) {
if (isset($_GET['api']) && $admin->isActive()) {
if (isset($_GET['action'])) {
if ($_GET['action']=='sub') {
$db->subToApi($_GET['utclogin'], $_GET['api']);
$db->subToApi($_SESSION['utclogin'], $_GET['api']);
}
elseif ($_GET['action']=='unsub') {
$db->unsubToApi($_GET['utclogin'], $_GET['api']);
$db->unsubToApi($_SESSION['utclogin'], $_GET['api']);
}
}
}
......@@ -54,8 +57,8 @@ if (isset($_GET['explanation'])) {
$db->setExplanation($_SESSION['utclogin'],$_GET['explanation']);
}
Views::printSubList($db->subList($_SESSION['utclogin']), $_SESSION['utclogin']);
Views::printExplanation($db->getExplanation($_SESSION['utclogin']), $_SESSION['utclogin']);
Views::printApiList($db->apiList('H', 2019), $_SESSION['utclogin']);
$view->printSubList($db->subList($_SESSION['utclogin']));
$view->printExplanation($db->getExplanation($_SESSION['utclogin']), $_SESSION['utclogin']);
$view->printApiList($db->apiList('H', 2019), $_SESSION['utclogin']);
?>
<?php
const IS_ACTIVE = true;
class Admin {
private $db;
public function __construct ($db) {
$this->db=$db;
}
public function isActive() {
return $this->db->config()['isactive'];
}
}
<?php
const IS_ACTIVE = true;
class Config {
public static function isActive() {
return IS_ACTIVE;
}
}
......@@ -46,8 +46,6 @@ class DB {
return $res;
}
public function apiList($semester, $year) {
$sql = 'SELECT *
FROM vapi
......@@ -80,7 +78,7 @@ class DB {
return $res;
}
public function copyUser($utclogin, $surname, $firstname) {
public function copyUser($utclogin, $surname, $firstname, $email) {
// Function used to create a local copy of surname and firstname of each user, in order to link to DFP files without utclogin
$sql = 'SELECT utclogin FROM localuser WHERE utclogin=:utclogin';
$st1 = $this->conn->prepare($sql);
......@@ -88,18 +86,45 @@ class DB {
$st1->execute();
if (!$st1->fetch(PDO::FETCH_ASSOC)) {
// If user has never logged in yet, he is added to local copy
$sql = 'INSERT INTO localuser(utclogin, firstname, surname) VALUES (:utclogin, :firstname, :surname)';
$sql = 'INSERT INTO localuser(utclogin, firstname, surname, email) VALUES (:utclogin, :firstname, :surname, :email)';
$st2 = $this->conn->prepare($sql);
$st2->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
$st2->bindValue(':firstname',$firstname,PDO::PARAM_STR);
$st2->bindValue(':surname',$surname,PDO::PARAM_STR);
$st2->bindValue(':email',$email,PDO::PARAM_STR);
$res = $st2->execute();
return $res;
}
else {
return 0;
};
}
}
/** Returns T is subscriptions and unsubscription are allowed **/
public function config() {
$sql = 'SELECT * FROM vconfig';
$st = $this->conn->prepare($sql);
$st->execute();
$res = $st->fetch(PDO::FETCH_ASSOC);
return $res;
}
public function isResp ($resplogin) {
$sql = 'SELECT resplogin FROM api WHERE resplogin=:resplogin';
$st = $this->conn->prepare($sql);
$st->bindValue(':resplogin',$resplogin,PDO::PARAM_STR);
$st->execute();
$res = $st->fetch(PDO::FETCH_ASSOC);
return $res['resplogin'];
}
public function isAdmin ($utclogin) {
$sql = 'SELECT utclogin FROM admins WHERE utclogin=:utclogin';
$st = $this->conn->prepare($sql);
$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
$st->execute();
$res = $st->fetch(PDO::FETCH_ASSOC);
return $res['utclogin'];
}
}
......@@ -2,11 +2,23 @@
class Views {
public static function isActive() {
return Config::isActive();
private $admin;
private $utclogin;
private $surname;
private $firstname;
public function __construct ($admin, $utclogin, $surname, $firstname) {
$this->admin=$admin;
$this->utclogin=$utclogin;
$this->surname=$surname;
$this->firstname=$firstname;
}
public function isActive() {
return $this->admin->isActive();
}
public static function printHtmlBegin() {
public function printHtmlBegin() {
echo '<html>';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
......@@ -17,30 +29,36 @@ class Views {
echo '<h1>Inscriptions Api</h1>';
}
public static function printInstructions() {
public function printInstructions($row) {
echo '<h2>Instructions</h2>';
echo '<p>Les Api sont décrites ici : <b><a href="https://apint.utc.fr/cat">https://apint.utc.fr/cat</a></b></p>';
echo '<p>Veillez à avoir bien lu la fiche avant de vous inscrire (pré-requis, dates...). Vous pouvez vous inscrire et vous désinscrire librement jusqu\'au 10 décembre 2018 (20h).</p>';
echo "<p>Veillez à avoir bien lu la fiche avant de vous inscrire (pré-requis, dates...). Vous pouvez vous inscrire et vous désinscrire librement entre le $row[openbegin] et le $row[openend].</p>";
echo '<p>Vous ne <b>pouvez pas</b> suivre deux Api qui se chevauchent (sauf pour le cas particulier de l\'Api <i>Animer une Api</i>). Si vous choisissez deux Api qui se chevauchent, indiquez vos préférences dans la zone de commentaire.</p>';
}
public static function printUser($utclogin, $surname, $firstname) {
echo '<p><i>'.$firstname.' '. $surname.'</i> (<b>'.$utclogin.'</b>)</p>';
public function printUser($isResp, $isAdmin) {
echo "<p><i>$this->firstname $this->surname</i> (<b>$this->utclogin</b>)</p>";
if ($isResp) {
echo '<a href="resp.php">[Gérer ses Api]</a> ';
}
if ($isAdmin) {
echo '<a href="admin.php">[Super-pouvoirs]</a>';
}
}
private static function printApi($row) {
private function printApi($row) {
return "$row[normcode] - $row[name] (du $row[dbegin] au $row[dend]) [Inscriptions : $row[nbsub]/$row[size]]";
}
public static function printSubList($list, $utclogin) {
public function printSubList($list) {
if ($list) {
echo '<h2>Vos inscriptions</h2>';
foreach ($list as $row) {
echo '<p>';
if (Views::isActive()) {
echo ' <a href="index.php?action=unsub&api='.$row['code'].'&utclogin='.$utclogin.'">[désinscription]</a> ';
if ($this->isActive()) {
echo "<a href='index.php?action=unsub&api=$row[code]'>[désinscription]</a> ";
}
echo Views::printApi($row);
echo $this->printApi($row);
echo ' ['.$row['semester'].$row['year'].', inscrit le '.$row['subdate'].']';
echo '</p>';
}
......@@ -50,8 +68,8 @@ class Views {
}
}
public static function printExplanation($comment, $utclogin) {
if (Views::isActive()) {
public function printExplanation($comment, $utclogin) {
if ($this->isActive()) {
echo '<h2>Commentaire</h2>';
echo '<form method="get" action="index.php">';
echo '<textarea name="explanation" cols="100" rows="5">'.$comment.'</textarea>';
......@@ -65,15 +83,15 @@ class Views {
}
}
public static function printApiList($list, $utclogin) {
public function printApiList($list) {
if ($list) {
echo '<h2>Liste des Api</h2>';
foreach ($list as $row) {
echo '<p>';
if (Views::isActive()) {
echo '<a href="index.php?action=sub&api='.$row['code'].'&utclogin='.$utclogin.'">[inscription]</a> ';
if ($this->isActive()) {
echo "<a href='index.php?action=sub&api=$row[code]'>[inscription]</a> ";
}
echo Views::printApi($row);
echo $this->printApi($row);
echo '</p>';
}
}
......
......@@ -15,6 +15,7 @@ CREATE TABLE api (
dend DATE NOT NULL,
size INTEGER,
ects INTEGER NOT NULL,
resplogin TEXT NOT NULL,
mail TEXT NOT NULL,
CHECK (code>0),
CHECK (year>2018 AND year<2100),
......@@ -30,6 +31,7 @@ CREATE TABLE localuser (
utclogin TEXT PRIMARY KEY,
surname TEXT,
firstname TEXT,
email TEXT,
explanation TEXT
);
/* MAJ Apisub H19
......@@ -72,9 +74,8 @@ SELECT translate(
$$;
CREATE OR REPLACE VIEW vlocaluser AS
SELECT l.utclogin, u.surname, u.firstname, MAX(u.speciality) AS speciality, MAX(u.level) AS level,
SELECT l.utclogin, u.surname, u.firstname, l.email, MAX(u.speciality) AS speciality, MAX(u.level) AS level,
CASE WHEN COUNT(*)>1 THEN 'duplication' END AS warning,
l.utclogin || 'etu.utc.fr' AS utcmail,
l.explanation
FROM localuser l
LEFT JOIN utcstudent u ON l.surname=u.surname AND l.firstname=unaccent_string(u.firstname)
......@@ -86,7 +87,7 @@ ap.code,
CASE WHEN ap.code<10 THEN '000'||ap.code WHEN ap.code>=10 THEN '00'||ap.code END AS normcode,
ap.name, ap.year, ap.semester,
TO_CHAR(ap.dbegin,'TMday FMDD TMmonth') AS dbegin, TO_CHAR(ap.dend,'TMday FMDD TMmonth') AS dend,
ap.mail, ap.ects, ap.size,
ap.resplogin, ap.mail, ap.ects, ap.size,
COUNT(su.utclogin) AS nbsub
FROM api ap LEFT JOIN subscribe su ON ap.code=su.api
GROUP BY ap.code, normcode, ap.name, ap.year, ap.semester, dbegin, dend, ap.size, ap.ects, ap.mail
......@@ -101,6 +102,32 @@ ORDER BY ap.year, ap.semester, ap.dbegin, ap.dend, ap.code, lo.firstname, lo.sur
CREATE OR REPLACE VIEW vsubscription_anonymous AS
SELECT normcode, name, dbegin, dend, size, nbsub, speciality, level
FROM vsubscription
FROM vsubscription;
/**
Tables liées à l'administration de la base
**/
CREATE TABLE admins (
utclogin TEXT PRIMARY KEY
);
INSERT INTO admins VALUES ('crozatst');
INSERT INTO admins VALUES ('dore');
CREATE TABLE config (
closed BOOLEAN NOT NULL,
openbegin DATE NOT NULL,
openend DATE NOT NULL
);
INSERT INTO config VALUES ('FALSE', TO_DATE('20180101','YYYYMMDD'), TO_DATE('20191231','YYYYMMDD'));
CREATE OR REPLACE VIEW vconfig AS
SELECT
TO_CHAR(openbegin,'TMday FMDD TMmonth FMYYYY') AS openbegin,
TO_CHAR(openend,'TMday FMDD TMmonth FMYYYY') AS openend,
NOT(closed) AND CURRENT_DATE >= openbegin AND CURRENT_DATE <= openend AS isactive
FROM config;
COMMIT;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment