diff --git a/README.md b/README.md index 4cbf4a1c513b1a6b538a9f3d719ae8fd1a6444e4..6c19e41c6174172bcae8d03e9406c27731077d39 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/index.php b/index.php index c1261a64ed45df9cfd0213590580f8aaaebddde0..f3c6fbf806a5098d8deb38905c98dc641c153db8 100644 --- a/index.php +++ b/index.php @@ -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']); ?> diff --git a/lib/admin.php b/lib/admin.php new file mode 100644 index 0000000000000000000000000000000000000000..46c91added98eb91c53dfbd08551a0adcbe5a792 --- /dev/null +++ b/lib/admin.php @@ -0,0 +1,16 @@ +db=$db; + } + + public function isActive() { + return $this->db->config()['isactive']; + } + +} diff --git a/lib/config.php b/lib/config.php deleted file mode 100644 index 188750229f8c37b2e2a1f26227cdf5ec5f02f523..0000000000000000000000000000000000000000 --- a/lib/config.php +++ /dev/null @@ -1,10 +0,0 @@ -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']; } } diff --git a/lib/views.php b/lib/views.php index a79db5c9647f36e20d0c43fd7775f74ad3d17b1e..8bb539045d4ed35e00cedfa41b89fe8f56fc8d24 100644 --- a/lib/views.php +++ b/lib/views.php @@ -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 ''; echo ''; echo ''; @@ -17,30 +29,36 @@ class Views { echo '

Inscriptions Api

'; } - public static function printInstructions() { + public function printInstructions($row) { echo '

Instructions

'; echo '

Les Api sont décrites ici : https://apint.utc.fr/cat

'; - echo '

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).

'; + echo "

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].

"; echo '

Vous ne pouvez pas suivre deux Api qui se chevauchent (sauf pour le cas particulier de l\'Api Animer une Api). Si vous choisissez deux Api qui se chevauchent, indiquez vos préférences dans la zone de commentaire.

'; } - public static function printUser($utclogin, $surname, $firstname) { - echo '

'.$firstname.' '. $surname.' ('.$utclogin.')

'; + public function printUser($isResp, $isAdmin) { + echo "

$this->firstname $this->surname ($this->utclogin)

"; + if ($isResp) { + echo '[Gérer ses Api] '; + } + if ($isAdmin) { + echo '[Super-pouvoirs]'; + } } - 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 '

Vos inscriptions

'; foreach ($list as $row) { echo '

'; - if (Views::isActive()) { - echo ' [désinscription] '; + if ($this->isActive()) { + echo "[désinscription] "; } - echo Views::printApi($row); + echo $this->printApi($row); echo ' ['.$row['semester'].$row['year'].', inscrit le '.$row['subdate'].']'; echo '

'; } @@ -50,8 +68,8 @@ class Views { } } - public static function printExplanation($comment, $utclogin) { - if (Views::isActive()) { + public function printExplanation($comment, $utclogin) { + if ($this->isActive()) { echo '

Commentaire

'; echo '
'; echo ''; @@ -65,15 +83,15 @@ class Views { } } - public static function printApiList($list, $utclogin) { + public function printApiList($list) { if ($list) { echo '

Liste des Api

'; foreach ($list as $row) { echo '

'; - if (Views::isActive()) { - echo '[inscription] '; + if ($this->isActive()) { + echo "[inscription] "; } - echo Views::printApi($row); + echo $this->printApi($row); echo '

'; } } diff --git a/sql/apisub.sql b/sql/apisub.sql index cee430774253f9e8c67ccfd0f7c05311f0a39867..172d38ce179e1b97337e2b6c96a4137c84495dd1 100644 --- a/sql/apisub.sql +++ b/sql/apisub.sql @@ -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;