2017-09-12 16:32:01 +00:00
< ? php
namespace router\controller ;
2017-09-14 17:56:24 +00:00
use \database\core\DatabaseDriver ;
2017-09-16 16:03:10 +00:00
use \service\CalendarExtractor as CE ;
2017-09-13 13:03:36 +00:00
2017-09-12 16:32:01 +00:00
class ics {
private $diplome_id ;
/* PRE - CALL
*
* @ url < String > Calling URI
*
*/
public function __construct ( $url ){
$this -> diplome_id = $url [ 'diplome_id' ];
2017-09-13 13:03:36 +00:00
2017-09-12 16:32:01 +00:00
}
/* CALL
*
*/
public function download (){
/* [ 1 ] Check . ics file
=========================================================*/
/* (1) Set file name */
$file_name = __ROOT__ . " /tmp/ " . $this -> diplome_id . " .ics " ;
/* (2) Check if exists */
if ( ! file_exists ( $file_name ) )
die ( " An error occured. Please contact the developers. \n " );
/* [ 2 ] Display file
=========================================================*/
/* (1) Headers */
2017-09-14 15:15:08 +00:00
header ( 'Content-Type: text/calendar; charset=utf-8' );
header ( 'Content-Disposition: attachment; filename=' . $this -> diplome_id . '.ics' );
2017-09-12 16:32:01 +00:00
/* (2) Body */
readfile ( $file_name );
}
2017-09-13 13:03:36 +00:00
2017-09-16 16:03:10 +00:00
public function view (){
/* [ 1 ] Get periods
=========================================================*/ {
/* (1) Get the list of available periods */
$sqlr = DatabaseDriver :: getPDO () -> query ( " SELECT * from period " );
$periods = $sqlr -> fetchAll ();
/* (2) Manage error */
if ( ! $periods )
die ( " Error cannot get config " );
/* (3) Begin html */
echo " <head> " ;
echo " <title>ICS UPPA university</title> " ;
echo " <meta name='author' content='xdrm-brackets (Adrien Marquès)'> " ;
echo " <link type='text/css' rel='stylesheet' href='/css/layout.css' /> " ;
echo " </head> " ;
echo " <body> " ;
}
/* [ 2 ] For each period ( week )
=========================================================*/ {
echo " <div class='null'> " ;
$cal_height = 54 ; // in css:vw
foreach ( $periods as $period ){
echo " </div><div class='period'> " ;
/* (1) Display week number */
$days = [
date ( 'Y-m-d' , strtotime ( $period [ 'monday' ] . ' + 0 days' )),
date ( 'Y-m-d' , strtotime ( $period [ 'monday' ] . ' + 1 days' )),
date ( 'Y-m-d' , strtotime ( $period [ 'monday' ] . ' + 2 days' )),
date ( 'Y-m-d' , strtotime ( $period [ 'monday' ] . ' + 3 days' )),
date ( 'Y-m-d' , strtotime ( $period [ 'monday' ] . ' + 4 days' ))
];
echo " <div class='title'>Week from ${ days[0] } to ${ days[4] } </div> " ;
echo " <div class='null'> " ;
/* (2) For each day */
foreach ( $days as $day_n => $day ){
/* (2.1) Display day grid */
echo " </div><div class='day d $day_n '> " ;
/* (2.2) Get available courses */
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " SELECT
DATE_FORMAT ( c . start_date , '%H:%i' ) as startd ,
DATE_FORMAT ( c . stop_date , '%H:%i' ) as stopd ,
l . basename as lbname ,
l . name as lname ,
e . basename as ebname ,
e . name as ename ,
e . color as color
FROM event as e , location as l , course as c
WHERE e . id_diplome = : idd
AND c . id_event = e . id_event
AND c . id_location = l . id_location
AND DATE_FORMAT ( c . start_date , '%Y-%m-%d' ) = : day
ORDER BY startd ASC " );
$sqlr -> execute ([
':idd' => $this -> diplome_id ,
':day' => $day
]);
$courses = $sqlr -> fetchAll ();
/* (2.3) Manage error */
if ( ! $courses )
continue ;
/* (2.4) For each course */
foreach ( $courses as $course ){
$start_hour = 0 ;
$stop_hour = 0 ;
/* (2.4.1) Extract time in hour */
2017-09-17 12:17:09 +00:00
if ( preg_match ( '@^(\d+):(\d+)$@' , $course [ 'startd' ], $m ) ){
2017-09-16 16:03:10 +00:00
$start_hour = $m [ 1 ] + $m [ 2 ] / 60 ;
2017-09-17 12:17:09 +00:00
$course [ 'startd' ] = ( $m [ 1 ] + 2 ) . ':' . $m [ 2 ];
}
2017-09-16 16:03:10 +00:00
2017-09-17 12:17:09 +00:00
if ( preg_match ( '@^(\d+):(\d+)$@' , $course [ 'stopd' ], $m ) ){
2017-09-16 16:03:10 +00:00
$stop_hour = $m [ 1 ] + $m [ 2 ] / 60 ;
2017-09-17 12:17:09 +00:00
$course [ 'stopd' ] = ( $m [ 1 ] + 2 ) . ':' . $m [ 2 ];
}
2017-09-16 16:03:10 +00:00
/* (2.4.2) Calc positionning */
$top = ( $start_hour - CE :: $start_h ) * $cal_height / ( CE :: $stop_h - CE :: $start_h );
$height = ( $stop_hour - CE :: $start_h ) * $cal_height / ( CE :: $stop_h - CE :: $start_h ) - $top ;
$color = $course [ 'color' ];
/* (2.4.3) Position <div> according to hours */
echo " <div class='course' style='top: ${ top } vw; height: ${ height } vw; background-color: $color ;'> " ;
echo " <div class='start'> " . $course [ 'startd' ] . " </div> " ;
echo " <div class='stop'> " . $course [ 'stopd' ] . " </div> " ;
echo " <div class='container'> " ;
echo " <span><b> " . $course [ 'ename' ] . " </b></span> " ;
echo " <span> " . $course [ 'lname' ] . " </span> " ;
echo " </div> " ;
echo " </div> " ;
}
}
echo " </div> " ;
}
echo " </div> " ;
}
echo " </body> " ;
// echo "<form method='POST' action='/xlessons/".$this->diplome_id."'>";
// echo "<table><thead style='font-weight: bold;'><tr><td>Read name</td><td>Correction</td></tr></thead><tbody>";
// foreach($d_cols as $data){
// $id = $data['id_event'];
// $basename = $data['basename'];
// $color = $data['color'];
// $name = $data['name'];
// echo "<tr><td>$basename</td><td><input type='text' style='display: inline-block; margin: 1em .2em; padding: .2em; border: none; background-color: $color;' name='name[$id]' value='$name'></td></tr>";
// }
// echo "</tbody></table>";
// echo "<input type='submit' value='SAVE'>";
// echo "</form>";
}
2017-09-13 13:03:36 +00:00
2017-09-15 18:21:52 +00:00
public function lessons (){
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
/* [ 1 ] Get database data
2017-09-13 13:03:36 +00:00
=========================================================*/
2017-09-14 17:56:24 +00:00
/* (1) Get the list of available events */
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " SELECT * FROM event WHERE id_diplome = :idd " );
$sqlr -> execute ([ ':idd' => $this -> diplome_id ]);
$d_cols = $sqlr -> fetchAll ();
/* (2) Manage error */
if ( ! $d_cols )
die ( " Correction not available for this diplome " );
2017-09-15 18:21:52 +00:00
echo " <form method='POST' action='/xlessons/ " . $this -> diplome_id . " '> " ;
echo " <table><thead style='font-weight: bold;'><tr><td>Read name</td><td>Correction</td></tr></thead><tbody> " ;
2017-09-14 17:56:24 +00:00
foreach ( $d_cols as $data ){
$id = $data [ 'id_event' ];
$basename = $data [ 'basename' ];
$color = $data [ 'color' ];
$name = $data [ 'name' ];
2017-09-15 18:21:52 +00:00
echo " <tr><td> $basename </td><td><input type='text' style='display: inline-block; margin: 1em .2em; padding: .2em; border: none; background-color: $color ;' name='name[ $id ]' value=' $name '></td></tr> " ;
2017-09-14 17:56:24 +00:00
}
2017-09-15 18:21:52 +00:00
echo " </tbody></table> " ;
2017-09-14 17:56:24 +00:00
echo " <input type='submit' value='SAVE'> " ;
2017-09-15 18:21:52 +00:00
2017-09-14 17:56:24 +00:00
echo " </form> " ;
}
2017-09-15 18:21:52 +00:00
// todo
2017-09-15 18:36:12 +00:00
public function location (){
/* [ 1 ] Get events data
=========================================================*/
/* (1) Get the list of available events */
2017-09-15 23:37:54 +00:00
// $sqlr = DatabaseDriver::getPDO()->prepare("SELECT * FROM event WHERE id_diplome = :idd");
// $sqlr->execute([ ':idd' => $this->diplome_id ]);
// $events = $sqlr->fetchAll();
2017-09-15 18:36:12 +00:00
/* (2) Manage error */
2017-09-15 23:37:54 +00:00
// if( !$events )
// die("Correction not available for this diplome");
2017-09-15 18:36:12 +00:00
/* [ 2 ] Get location data
=========================================================*/
2017-09-15 23:37:54 +00:00
// foreach($events as &$event){
2017-09-15 18:36:12 +00:00
2017-09-15 23:37:54 +00:00
$location = [];
2017-09-15 18:36:12 +00:00
/* (1) Get the list of available locations*/
2017-09-16 10:19:05 +00:00
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " SELECT GROUP_CONCAT(DISTINCT l.id_location SEPARATOR ',') as idl, l.name as lname, l.basename as lbasename, e.name as ename, e.color as color FROM location as l, event as e WHERE l.id_event = e.id_event AND e.id_diplome = :idd GROUP BY lname, lbasename, ename, color ORDER BY ename ASC, lbasename ASC " );
2017-09-15 23:37:54 +00:00
$sqlr -> execute ([ ':idd' => $this -> diplome_id ]);
$fetched = $sqlr -> fetchAll ();
2017-09-15 18:36:12 +00:00
2017-09-16 10:19:05 +00:00
if ( ! $fetched )
2017-09-15 23:37:54 +00:00
$fetched [] = [];
2017-09-15 18:36:12 +00:00
2017-09-16 10:19:05 +00:00
// foreach($fetched as $raw)
// $location[$raw['ename']] = [ $raw['ename'], $raw['idl'], $raw['lname'], $raw['lbasename'], $raw['color'] ];
2017-09-15 18:36:12 +00:00
2017-09-15 23:37:54 +00:00
// }
2017-09-15 18:36:12 +00:00
2017-09-15 23:37:54 +00:00
echo " <form method='POST' action='/xlocation/ " . $this -> diplome_id . " '> " ;
echo " <table><thead style='font-weight: bold;'><tr><td>Lesson</td><td>Read name</td><td>Correction</td></tr></thead><tbody> " ;
2017-09-15 18:36:12 +00:00
2017-09-16 10:19:05 +00:00
foreach ( $fetched as $raw ){
2017-09-15 18:36:12 +00:00
2017-09-16 10:19:05 +00:00
$color = $raw [ 'color' ];
$id = $raw [ 'idl' ];
$name = $raw [ 'lname' ];
$basename = $raw [ 'lbasename' ];
$ename = $raw [ 'ename' ];
2017-09-15 18:36:12 +00:00
2017-09-15 23:37:54 +00:00
echo " <tr><td><b> $ename </b></td><td> $basename </td><td><input type='text' style='display: inline-block; margin: 1em .2em; padding: .2em; border: none; background-color: $color ;' name='name[ $id ]' value=' $name '></td></tr> " ;
2017-09-15 18:36:12 +00:00
}
echo " </tbody></table> " ;
echo " <input type='submit' value='SAVE'> " ;
echo " </form> " ;
}
2017-09-14 17:56:24 +00:00
2017-09-15 18:21:52 +00:00
public function correct_lessons (){
2017-09-14 17:56:24 +00:00
/* [ 1 ] Update data
=========================================================*/ {
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
/* (1) Check $_POST['name'] */
if ( ! isset ( $_POST [ 'name' ]) || ! is_array ( $_POST [ 'name' ]) )
die ( " No data received " );
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
/* (2) Store corrections */
foreach ( $_POST [ 'name' ] as $id => $correct ){
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
if ( ! empty ( trim ( $correct )) ){
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " UPDATE event SET name = :name WHERE id_event = :ide " );
$sqlr -> execute ([ ':name' => $correct , ':ide' => $id ]);
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
}
2017-09-13 13:03:36 +00:00
2017-09-14 17:56:24 +00:00
}
2017-09-13 13:03:36 +00:00
}
2017-09-14 22:12:12 +00:00
/* [ 2 ] Update file one the fly
=========================================================*/
/* (1) Get corrections */
2017-09-15 18:21:52 +00:00
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " SELECT id_event, name FROM event WHERE id_diplome = :idd " );
2017-09-14 22:12:12 +00:00
$sqlr -> execute ([ ':idd' => $this -> diplome_id ]);
/* (2) Manage error */
if ( ! ( $fetched = $sqlr -> fetchAll ()) )
die ( " Cannot update the .ics file now " );
/* (3) Format data */
$corrections = [];
foreach ( $fetched as $c )
2017-09-15 18:21:52 +00:00
$corrections [ $c [ 'id_event' ]] = $c [ 'name' ];
2017-09-14 22:12:12 +00:00
/* (3) Get file pointer */
2017-09-14 22:18:56 +00:00
$fp = @ file_get_contents ( __ROOT__ . " /tmp/ " . $this -> diplome_id . " .ics " );
2017-09-14 22:12:12 +00:00
/* (4) Manage error */
if ( ! $fp )
die ( " Cannot update the .ics file now " );
/* (4) Read line by line */
$line = explode ( " \n " , $fp );
for ( $l = 0 ; $l < count ( $line ) - 1 ; $l ++ ){
// {1} If got the right pointer //
2017-09-15 18:21:52 +00:00
if ( preg_match ( '@^DESCRIPTION:event\@(\d+)$@' , $line [ $l ], $m ) ){
2017-09-14 22:12:12 +00:00
// {2} If is in the list of correction //
if ( ! isset ( $corrections [ $m [ 1 ]]) )
continue ;
// {3} Rewrite name //
$line [ ++ $l ] = " SUMMARY: " . $corrections [ $m [ 1 ]];
}
}
/* (5) Back in file */
file_put_contents ( __ROOT__ . " /tmp/ " . $this -> diplome_id . " .ics " , implode ( " \n " , $line ));
header ( 'Location: /' );
2017-09-13 13:03:36 +00:00
}
2017-09-15 18:21:52 +00:00
// todo
2017-09-15 18:40:29 +00:00
public function correct_location (){
/* [ 1 ] Update data
=========================================================*/ {
/* (1) Check $_POST['name'] */
if ( ! isset ( $_POST [ 'name' ]) || ! is_array ( $_POST [ 'name' ]) )
die ( " No data received " );
/* (2) Store corrections */
2017-09-15 23:37:54 +00:00
foreach ( $_POST [ 'name' ] as $ids => $correct ){
2017-09-15 18:40:29 +00:00
if ( ! empty ( trim ( $correct )) ){
2017-09-15 23:37:54 +00:00
// check if containing id list
if ( ! preg_match ( '@^\d+(,\d+)*$@' , $ids ) )
continue ;
// update for each location
$id_list = explode ( ',' , $ids );
foreach ( $id_list as $id ){
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " UPDATE location SET name = :name WHERE id_location = :idl " );
$sqlr -> execute ([ ':name' => $correct , ':idl' => $id ]);
}
2017-09-15 18:40:29 +00:00
}
}
}
/* [ 2 ] Update file one the fly
=========================================================*/
/* (1) Get corrections */
$sqlr = DatabaseDriver :: getPDO () -> prepare ( " SELECT l.id_location as id_location, l.name as name FROM location as l, event as e WHERE l.id_event = e.id_event AND e.id_diplome = :idd " );
$sqlr -> execute ([ ':idd' => $this -> diplome_id ]);
/* (2) Manage error */
if ( ! ( $fetched = $sqlr -> fetchAll ()) )
die ( " Cannot update the .ics file now " );
/* (3) Format data */
$corrections = [];
foreach ( $fetched as $c )
$corrections [ $c [ 'id_location' ]] = $c [ 'name' ];
/* (3) Get file pointer */
$fp = @ file_get_contents ( __ROOT__ . " /tmp/ " . $this -> diplome_id . " .ics " );
/* (4) Manage error */
if ( ! $fp )
die ( " Cannot update the .ics file now " );
/* (4) Read line by line */
$line = explode ( " \n " , $fp );
for ( $l = 0 ; $l < count ( $line ) - 1 ; $l ++ ){
// {1} If got the right pointer //
if ( preg_match ( '@^DESCRIPTION:location\@(\d+)$@' , $line [ $l ], $m ) ){
// {2} If is in the list of correction //
if ( ! isset ( $corrections [ $m [ 1 ]]) )
continue ;
// {3} Rewrite name //
$line [ ++ $l ] = " LOCATION: " . $corrections [ $m [ 1 ]];
}
}
/* (5) Back in file */
file_put_contents ( __ROOT__ . " /tmp/ " . $this -> diplome_id . " .ics " , implode ( " \n " , $line ));
header ( 'Location: /' );
}
2017-09-15 18:21:52 +00:00
2017-09-12 16:32:01 +00:00
/* POST - CALL
*
*/
public function __destruct (){
}
}