ptut-vhost/build/api/module/professor/pdfController.php

437 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace api\module\professor;
use database\core\Repo;
use database\repo\prof;
use database\repo\cours;
use database\repo\td;
use database\repo\tp;
use database\repo\formation;
use error\core\Error;
use error\core\Err;
class pdfController{
/* (1) Get professor PDF fiche matching a specific ID
*
* @prof_id<array> Professor ID
*
* @return download<File> The PDF fiche
---------------------------------------------------------*/
public static function get($args){
$prof_id = -1;
extract($args);
/* (0) Initialize
---------------------------------------------------------*/
/* (1) Initialize data structure */
$data = [
'prof' => [],
'cours' => [],
'td' => [],
'tp' => [],
'form' => []
];
/* (2) Get repositories */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/** @var formation $form_repo */
$form_repo = Repo::getRepo('formation');
/** @var cours $cours_repo */
$cours_repo = Repo::getRepo('cours');
/** @var td $td_repo */
$td_repo = Repo::getRepo('td');
/** @var tp $tp_repo */
$tp_repo = Repo::getRepo('tp');
/* (1) Get professor data
---------------------------------------------------------*/
/* (1) Request */
$fetched = $prof_repo->getWithVH($prof_id);
/* (2) Error */
if( count($fetched) < 1 )
return ['error' => new Error(Err::NoMatchFound)];
/* (3) Extract data */
$data['prof'] = $fetched[0];
/* (2) Get each Cours
---------------------------------------------------------*/
/* (1) Request */
$fetched = $cours_repo->getForProfessor($prof_id);
/* (2) Store data if no error */
if( !is_null($fetched) )
$data['cours'] = $fetched;
/* (3) For each Cours -> parse formation list */
foreach($data['cours'] as $kcours=>$cours)
$data['cours'][$kcours]['formations'] = json_decode($cours['formations']);
/* (3) get each TD
---------------------------------------------------------*/
/* (1) Request */
$fetched = $td_repo->getForProfessor($prof_id);
/* (2) Store data if no error */
if( !is_null($fetched) )
$data['td'] = $fetched;
/* (3) For each TD -> parse formation list */
foreach($data['td'] as $ktd=>$td)
$data['td'][$ktd]['formations'] = json_decode($td['formations']);
/* (4) get each TP
---------------------------------------------------------*/
/* (1) Request */
$fetched = $tp_repo->getForProfessor($prof_id);
/* (2) Store data if no error */
if( !is_null($fetched) )
$data['tp'] = $fetched;
/* (3) For each TP -> parse formation list */
foreach($data['tp'] as $ktp=>$tp)
$data['tp'][$ktp]['formations'] = json_decode($tp['formations']);
/* (5) Get formations (id => label)
---------------------------------------------------------*/
/* (1) Request */
$fetched = $form_repo->get(null);
/* (2) Error: no formation found */
if( count($fetched) < 1 )
return ['error' => new Error(Err::RepoError)];
/* (3) Reference formations by key = idForm */
foreach($fetched as $form)
$data['form'][intval($form['idForm'])] = $form;
/* (6) Render PDF
---------------------------------------------------------*/
/* (1) Generate body */
$body = self::generate_pdf_body($data);
/* (2) Set headers */
$fullname = $data['prof']['firstName'].' '.$data['prof']['lastName'];
$date = date('d-m-Y');
$title = "Fiche enseignant - $fullname ($date)";
$headers = [
// 'Content-Description' => 'File Transfer',
'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'attachment; filename=\"'.$title.'\.pdf"',
'Cache-Control' => 'public, must-revalidate, max-age=0',
'Pragma' => 'public',
'X-Generator' => 'mPDF',
'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT',
'Last-Modified' => gmdate('D, d M Y H:i:s').' GMT',
'Content-Type' => 'application/pdf'
];
/* (3) Generate download */
return [
'headers' => $headers,
'body' => $body
];
}
private static function generate_pdf_body($data){
/* (1) Format data
---------------------------------------------------------*/
/* (1) Get fullname */
$fullname = $data['prof']['firstName'].' '.$data['prof']['lastName'];
/* (2) Get formatted date */
$date = date('d-m-Y');
/* (3) Total horaire */
$total_h = floatval($data['prof']['VHCours'])
+ floatval($data['prof']['VHTd'])
+ floatval($data['prof']['VHTp']);
$equiTD = floatval($data['prof']['equiTD']);
/* (4) Heures manquantes */
// TOTAL_H or equiTD ???
$missing_h = floatval($data['prof']['hoursToDo']) - $equiTD;
$missing_h = $missing_h < 0 ? 0 : $missing_h;
/* (5) Heures sup */
$sup_h = $equiTD > floatval($data['prof']['hoursToDo']) ? $equiTD - floatval($data['prof']['hoursToDo']) : 0;
/* (2) Initialize PDF
---------------------------------------------------------*/
/* (1) Get Font default directory */
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
/* (2) Get Font data */
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
/* (3) Create PDF file */
$pdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'defaultCssFile' => __PUBLIC__.'/css/pdf.css',
'tempDir' => __ROOT__.'/tmp',
'fontDir' => array_merge($fontDirs, [ __PUBLIC__.'/font/' ]),
'fontdata' => $fontData + [
'Fira Sans' => [
'R' => 'FiraSans-Regular.ttf'
]
],
'default_font' => 'Fira Sans'
]);
/* (4) Set PDF title */
$pdf->SetTitle("Fiche enseignant - $fullname ($date)");
/* (5) Store SVG */
$svg_warning = '<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" height="10" width="10" viewBox="0 0 1792 1792">
<path d="m 1024,1375 v -190 q 0,-14 -9.5,-23.5 Q 1005,1152 992,1152 H 800 q -13,0 -22.5,9.5 -9.5,9.5 -9.5,23.5 v 190 q 0,14 9.5,23.5 9.5,9.5 22.5,9.5 h 192 q 13,0 22.5,-9.5 9.5,-9.5 9.5,-23.5 z m -2,-374 18,-459 q 0,-12 -10,-19 -13,-11 -24,-11 H 786 q -11,0 -24,11 -10,7 -10,21 l 17,457 q 0,10 10,16.5 10,6.5 24,6.5 h 185 q 14,0 23.5,-6.5 9.5,-6.5 10.5,-16.5 z m -14,-934 768,1408 q 35,63 -2,126 -17,29 -46.5,46 -29.5,17 -63.5,17 H 128 Q 94,1664 64.5,1647 35,1630 18,1601 -19,1538 16,1475 L 784,67 q 17,-31 47,-49 30,-18 65,-18 35,0 65,18 30,18 47,49 z" style="fill: #ea4b35" />
</svg>';
/* (3) Set Header|Footer
---------------------------------------------------------*/
/* (1) Set Header content (Left, Right) */
$header_content = [
'C' => [ 'color' => '#aaa', 'content' => "Fiche enseignant $fullname" ],
'line' => 1
];
/* (2) Apply Header */
$pdf->setHeader([
'odd' => $header_content,
'even' => $header_content
]);
/* (1) Set Footer content (Left, Right) */
$footer_content = [
'L' => [ 'color' => '#aaa', 'content' => "Généré le $date" ],
'R' => [ 'color' => '#aaa', 'content' => 'Page {PAGENO} sur {nbpg}' ],
'line' => 1
];
/* (2) Apply Footer */
$pdf->setFooter([
'odd' => $footer_content,
'even' => $footer_content
]);
/* (4) Page 1 - Récapitulatif
---------------------------------------------------------*/
/* (1) Initialize page */
$pdf->AddPage();
/* (2) Write content */
$pdf->WriteHTML('<br>
<h3>1. Récapitulatif</h3>
<hr>
<article>
<p>
La liste des informations récapitulative est basée sur les données de tous les enseignants enregistrés à ce jour.
</p>
<blockquote>
Il est à noter qu\'elle contient les données générées au '.$date.'. Elle peut ne plus être à jour.
</blockquote>
<p>
Si un problème persiste veuillez contacter les responsables à l\'addresse mail suivante <span style="color: red;">&lt;mail_display_error&gt;</span>.
</p>
<br><br><br>
<table>
<thead>
<tr>
<td class="color ac">Donnée</td>
<td class="color ac">Valeur</td>
</tr>
</thead>
<tbody>
<tr>
<td class="ar">Heures à faire</td>
<td>'.$data['prof']['hoursToDo'].' h</td>
</tr>
<tr>
<td class="ar">Equivalents TD</td>
<td>'.$data['prof']['equiTD'].' h</td>
</tr>
<tr>
<td class="ar">Heures programmées</td>
<td>'.$total_h.' h</td>
</tr>
<tr>
<td class="ar">Heures manquantes</td>
<td>'.( $missing_h>0 ? "$svg_warning $missing_h" : $missing_h).' h</td>
</tr>
<tr>
<td class="ar">Heures sup.</td>
<td>'.$sup_h.' h</td>
</tr>
</tbody>
</table>
</article>
');
/* (5) Page 2 - Enseignements
---------------------------------------------------------*/
/* (1) Initialize page */
$pdf->AddPage();
/* (2) Write content */
$pdf->WriteHTML('<br>
<h3>2. Enseignements</h3>
<hr>
<article>
<p>
La liste des enseignements générée dans le tableau ci-dessous est uniquement à titre informatif et n\'est pas contractuelle.
</p>
<blockquote>
Il est à noter qu\'elle contient les données générées au '.$date.'. Elle peut ne plus être à jour.
</blockquote>
<p>
Si un problème persiste veuillez contacter les responsables à l\'addresse mail suivante <span style="color: red;">&lt;mail_display_error&gt;</span>.
</p>
<br><br><br>
<table>
<thead>
<tr>
<th colspan="5" class="ac">Liste des enseignements programmés</th>
</tr>
<tr>
<td class="color ac">Code UE</td>
<td class="color ac">Nom UE</td>
<td class="color ac">Prestation</td>
<td class="color ac">volume horaire</td>
<td class="color ac">formations</td>
</tr>
</thead>
<tbody>');
/* (3) List Cours */
foreach($data['cours'] as $kc=>$c){
$pdf->WriteHTML('<tr>
<td>'.$c['code'].'</td>
<td>'.$c['label'].'</td>
<td>Cours</td>
<td>'.$c['volume'].' heures</td>
<td><ul>');
// print formations
foreach($c['formations'] as $f)
if( isset($data['form'][$f]) )
$pdf->WriteHTML('<li>'.$data['form'][$f]['labelForm'].'</li>');
$pdf->WriteHTML('</ul></td></tr>');
}
/* (4) List TD */
foreach($data['td'] as $kc=>$c){
$pdf->WriteHTML('<tr>
<td>'.$c['code'].'</td>
<td>'.$c['label'].'</td>
<td>TD</td>
<td>'.$c['volume'].' heures</td>
<td><ul>');
// print formations
foreach($c['formations'] as $f)
if( isset($data['form'][$f]) )
$pdf->WriteHTML('<li>'.$data['form'][$f]['labelForm'].'</li>');
$pdf->WriteHTML('</ul></td></tr>');
}
/* (5) List TP */
foreach($data['tp'] as $kc=>$c){
$pdf->WriteHTML('<tr>
<td>'.$c['code'].'</td>
<td>'.$c['label'].'</td>
<td>TP</td>
<td>'.$c['volume'].' heures</td>
<td><ul>');
// print formations
foreach($c['formations'] as $f)
if( isset($data['form'][$f]) )
$pdf->WriteHTML('<li>'.$data['form'][$f]['labelForm'].'</li>');
$pdf->WriteHTML('</ul></td></tr>');
}
/* (6) End HTML */
$pdf->WriteHTML('</tr>
</tbody>
</table>
</article>');
/* (N) Print out PDF file
---------------------------------------------------------*/
return $pdf->Output("Fiche enseignant - $fullname ($date)", 'S');
}
}