Calling URI * */ public function __construct($url){ $this->diplome_id = $url['diplome_id']; } /* 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 */ header('Content-Type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename='.$this->diplome_id.'.ics'); /* (2) Body */ readfile($file_name); } public function lessons(){ /* [1] Get database data =========================================================*/ /* (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"); echo "
"; echo ""; foreach($d_cols as $data){ $id = $data['id_event']; $basename = $data['basename']; $color = $data['color']; $name = $data['name']; echo ""; } echo "
Read nameCorrection
$basename
"; echo ""; echo "
"; } // todo public function location(){ /* [1] Get events data =========================================================*/ /* (1) Get the list of available events */ $sqlr = DatabaseDriver::getPDO()->prepare("SELECT * FROM event WHERE id_diplome = :idd"); $sqlr->execute([ ':idd' => $this->diplome_id ]); $events = $sqlr->fetchAll(); /* (2) Manage error */ if( !$events ) die("Correction not available for this diplome"); /* [2] Get location data =========================================================*/ foreach($events as &$event){ $event['location'] = []; /* (1) Get the list of available locations*/ $sqlr = DatabaseDriver::getPDO()->prepare("SELECT * FROM location WHERE id_event = :ide"); $sqlr->execute([ ':ide' => $event['id_event'] ]); $locations = $sqlr->fetchAll(); if( !$locations ) $locations = []; foreach($locations as $location) $event['location'][] = $location; } echo "
"; echo ""; foreach($events as $event){ foreach($event['location'] as $location){ $color = $event['color']; $id = $location['id_location']; $basename = $location['basename']; $name = $location['name']; $e_name = $event['name']; echo ""; } } echo "
LessonRead nameCorrection
$e_name$basename
"; echo ""; echo "
"; } public function correct_lessons(){ /* [1] Update data =========================================================*/ { /* (1) Check $_POST['name'] */ if( !isset($_POST['name']) || !is_array($_POST['name']) ) die("No data received"); /* (2) Store corrections */ foreach($_POST['name'] as $id=>$correct){ if( !empty(trim($correct)) ){ $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE event SET name = :name WHERE id_event = :ide"); $sqlr->execute([ ':name' => $correct, ':ide' => $id ]); } } } /* [2] Update file one the fly =========================================================*/ /* (1) Get corrections */ $sqlr = DatabaseDriver::getPDO()->prepare("SELECT id_event, name FROM event WHERE 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_event']] = $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:event\@(\d+)$@', $line[$l], $m) ){ // {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: /'); } // todo 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 */ foreach($_POST['name'] as $id=>$correct){ if( !empty(trim($correct)) ){ $sqlr = DatabaseDriver::getPDO()->prepare("UPDATE location SET name = :name WHERE id_location = :idl"); $sqlr->execute([ ':name' => $correct, ':idl' => $id ]); } } } /* [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: /'); } /* POST-CALL * */ public function __destruct(){ } }