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 @@ ...@@ -2,6 +2,6 @@
## Configuration ## 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'])) { ...@@ -26,25 +26,28 @@ if (!isset($_SESSION['ticket'])) {
include 'lib/db.php'; include 'lib/db.php';
include 'lib/views.php'; include 'lib/views.php';
include 'lib/config.php'; include 'lib/admin.php';
$db = new DB(); $db = new DB();
if ($_SESSION['localcopy']) { if ($_SESSION['localcopy']) {
$db->copyUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']); $db->copyUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname'], $_SESSION['mail']);
$_SESSION['localcopy']=false; $_SESSION['localcopy']=false;
} }
Views::printHtmlBegin(); $admin = new Admin($db);
Views::printUser($_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']); $view = new Views($admin, $_SESSION['utclogin'], $_SESSION['surname'], $_SESSION['firstname']);
Views::printInstructions();
$view->printHtmlBegin();
$view->printUser($db->isResp($_SESSION['utclogin']), $db->isAdmin($_SESSION['utclogin']));
$view->printInstructions($db->config());
/** Subscription and unsubscription management **/ /** Subscription and unsubscription management **/
if (isset($_GET['api']) && isset($_GET['utclogin'])) { if (isset($_GET['api']) && $admin->isActive()) {
if (isset($_GET['action'])) { if (isset($_GET['action'])) {
if ($_GET['action']=='sub') { if ($_GET['action']=='sub') {
$db->subToApi($_GET['utclogin'], $_GET['api']); $db->subToApi($_SESSION['utclogin'], $_GET['api']);
} }
elseif ($_GET['action']=='unsub') { elseif ($_GET['action']=='unsub') {
$db->unsubToApi($_GET['utclogin'], $_GET['api']); $db->unsubToApi($_SESSION['utclogin'], $_GET['api']);
} }
} }
} }
...@@ -54,8 +57,8 @@ if (isset($_GET['explanation'])) { ...@@ -54,8 +57,8 @@ if (isset($_GET['explanation'])) {
$db->setExplanation($_SESSION['utclogin'],$_GET['explanation']); $db->setExplanation($_SESSION['utclogin'],$_GET['explanation']);
} }
Views::printSubList($db->subList($_SESSION['utclogin']), $_SESSION['utclogin']); $view->printSubList($db->subList($_SESSION['utclogin']));
Views::printExplanation($db->getExplanation($_SESSION['utclogin']), $_SESSION['utclogin']); $view->printExplanation($db->getExplanation($_SESSION['utclogin']), $_SESSION['utclogin']);
Views::printApiList($db->apiList('H', 2019), $_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 { ...@@ -46,8 +46,6 @@ class DB {
return $res; return $res;
} }
public function apiList($semester, $year) { public function apiList($semester, $year) {
$sql = 'SELECT * $sql = 'SELECT *
FROM vapi FROM vapi
...@@ -80,7 +78,7 @@ class DB { ...@@ -80,7 +78,7 @@ class DB {
return $res; 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 // 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'; $sql = 'SELECT utclogin FROM localuser WHERE utclogin=:utclogin';
$st1 = $this->conn->prepare($sql); $st1 = $this->conn->prepare($sql);
...@@ -88,18 +86,45 @@ class DB { ...@@ -88,18 +86,45 @@ class DB {
$st1->execute(); $st1->execute();
if (!$st1->fetch(PDO::FETCH_ASSOC)) { if (!$st1->fetch(PDO::FETCH_ASSOC)) {
// If user has never logged in yet, he is added to local copy // 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 = $this->conn->prepare($sql);
$st2->bindValue(':utclogin',$utclogin,PDO::PARAM_STR); $st2->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
$st2->bindValue(':firstname',$firstname,PDO::PARAM_STR); $st2->bindValue(':firstname',$firstname,PDO::PARAM_STR);
$st2->bindValue(':surname',$surname,PDO::PARAM_STR); $st2->bindValue(':surname',$surname,PDO::PARAM_STR);
$st2->bindValue(':email',$email,PDO::PARAM_STR);
$res = $st2->execute(); $res = $st2->execute();
return $res; return $res;
} }
else { else {
return 0; 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 @@ ...@@ -2,11 +2,23 @@
class Views { class Views {
public static function isActive() { private $admin;
return Config::isActive(); 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 '<html>';
echo '<head>'; echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'; echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
...@@ -17,30 +29,36 @@ class Views { ...@@ -17,30 +29,36 @@ class Views {
echo '<h1>Inscriptions Api</h1>'; echo '<h1>Inscriptions Api</h1>';
} }
public static function printInstructions() { public function printInstructions($row) {
echo '<h2>Instructions</h2>'; 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>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>'; 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) { public function printUser($isResp, $isAdmin) {
echo '<p><i>'.$firstname.' '. $surname.'</i> (<b>'.$utclogin.'</b>)</p>'; 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]]"; 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) { if ($list) {
echo '<h2>Vos inscriptions</h2>'; echo '<h2>Vos inscriptions</h2>';
foreach ($list as $row) { foreach ($list as $row) {
echo '<p>'; echo '<p>';
if (Views::isActive()) { if ($this->isActive()) {
echo ' <a href="index.php?action=unsub&api='.$row['code'].'&utclogin='.$utclogin.'">[désinscription]</a> '; 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 ' ['.$row['semester'].$row['year'].', inscrit le '.$row['subdate'].']';
echo '</p>'; echo '</p>';
} }
...@@ -50,8 +68,8 @@ class Views { ...@@ -50,8 +68,8 @@ class Views {
} }
} }
public static function printExplanation($comment, $utclogin) { public function printExplanation($comment, $utclogin) {
if (Views::isActive()) { if ($this->isActive()) {
echo '<h2>Commentaire</h2>'; echo '<h2>Commentaire</h2>';
echo '<form method="get" action="index.php">'; echo '<form method="get" action="index.php">';
echo '<textarea name="explanation" cols="100" rows="5">'.$comment.'</textarea>'; echo '<textarea name="explanation" cols="100" rows="5">'.$comment.'</textarea>';
...@@ -65,15 +83,15 @@ class Views { ...@@ -65,15 +83,15 @@ class Views {
} }
} }
public static function printApiList($list, $utclogin) { public function printApiList($list) {
if ($list) { if ($list) {
echo '<h2>Liste des Api</h2>'; echo '<h2>Liste des Api</h2>';
foreach ($list as $row) { foreach ($list as $row) {
echo '<p>'; echo '<p>';
if (Views::isActive()) { if ($this->isActive()) {
echo '<a href="index.php?action=sub&api='.$row['code'].'&utclogin='.$utclogin.'">[inscription]</a> '; echo "<a href='index.php?action=sub&api=$row[code]'>[inscription]</a> ";
} }
echo Views::printApi($row); echo $this->printApi($row);
echo '</p>'; echo '</p>';
} }
} }
......
...@@ -15,6 +15,7 @@ CREATE TABLE api ( ...@@ -15,6 +15,7 @@ CREATE TABLE api (
dend DATE NOT NULL, dend DATE NOT NULL,
size INTEGER, size INTEGER,
ects INTEGER NOT NULL, ects INTEGER NOT NULL,
resplogin TEXT NOT NULL,
mail TEXT NOT NULL, mail TEXT NOT NULL,
CHECK (code>0), CHECK (code>0),
CHECK (year>2018 AND year<2100), CHECK (year>2018 AND year<2100),
...@@ -30,6 +31,7 @@ CREATE TABLE localuser ( ...@@ -30,6 +31,7 @@ CREATE TABLE localuser (
utclogin TEXT PRIMARY KEY, utclogin TEXT PRIMARY KEY,
surname TEXT, surname TEXT,
firstname TEXT, firstname TEXT,
email TEXT,
explanation TEXT explanation TEXT
); );
/* MAJ Apisub H19 /* MAJ Apisub H19
...@@ -72,9 +74,8 @@ SELECT translate( ...@@ -72,9 +74,8 @@ SELECT translate(
$$; $$;
CREATE OR REPLACE VIEW vlocaluser AS 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, CASE WHEN COUNT(*)>1 THEN 'duplication' END AS warning,
l.utclogin || 'etu.utc.fr' AS utcmail,
l.explanation l.explanation
FROM localuser l FROM localuser l
LEFT JOIN utcstudent u ON l.surname=u.surname AND l.firstname=unaccent_string(u.firstname) LEFT JOIN utcstudent u ON l.surname=u.surname AND l.firstname=unaccent_string(u.firstname)
...@@ -86,7 +87,7 @@ ap.code, ...@@ -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, 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, ap.name, ap.year, ap.semester,
TO_CHAR(ap.dbegin,'TMday FMDD TMmonth') AS dbegin, TO_CHAR(ap.dend,'TMday FMDD TMmonth') AS dend, 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 COUNT(su.utclogin) AS nbsub
FROM api ap LEFT JOIN subscribe su ON ap.code=su.api 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 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 ...@@ -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 CREATE OR REPLACE VIEW vsubscription_anonymous AS
SELECT normcode, name, dbegin, dend, size, nbsub, speciality, level 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; 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