From e9579c895647655c359ea4e5586cab7315c69916 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 10 May 2018 15:09:40 +0200 Subject: [PATCH] Implemented Excel export of version --- .../module/department/exportController.php | 176 ++++++++++++++++++ build/database/core/PDOWrapper/PDOWrapper.php | 6 +- build/database/repo/ue.php | 9 + config/modules.json | 7 + 4 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 build/api/module/department/exportController.php diff --git a/build/api/module/department/exportController.php b/build/api/module/department/exportController.php new file mode 100644 index 0000000..d14a8f7 --- /dev/null +++ b/build/api/module/department/exportController.php @@ -0,0 +1,176 @@ +exportUE(); + $formations = []; + + //cache formations labels + foreach ($formationRepo->get(null) as $form){ + $formations[$form["idForm"]] = $form["labelForm"]; + } + + $formatFormations = function(String $formationsId) use ($formations) : String{ + $returned = ""; + foreach (json_decode($formationsId,true) as $form){ + $returned .= $formations[$form]."+"; + } + + return rtrim($returned,"+"); + }; + + $spreadsheet = new Spreadsheet(); + $excel = $spreadsheet->getActiveSheet(); + + //set up base document + $arrayHeaders = ["Code","Intitulé","Statut","V.H.","Cours","NbGr","Enseignant","TD","NbGr","Enseignant","TP","NbGr","Enseignant","Equ. TD","Total"]; + $excel->setCellValue("B2","CODIFICATION DES UE"); + $i = 1; + foreach ($arrayHeaders as $header){ + $excel->setCellValueByColumnAndRow($i,4,$header); + $i++; + } + + $excel->freezePane("O5"); + + //set up state variables + $currentFormation = null; + $currentLine = 5; + + foreach ($ues as $ue){ + if($currentFormation != $ue["labelFormation"]){ + $currentFormation = $ue["labelFormation"]; + $excel->setCellValue("A$currentLine",$ue["labelFormation"]); + $excel->getStyle("A$currentLine")->getFont()->setBold(true)->setSize(11); + $excel->getStyle("A$currentLine:O$currentLine")->getBorders()->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOUBLE); + $currentLine += 2; + } + + //get all groups data + $cm = $cmRepo->getGroups($ue["code"]); + $td = $tdRepo->getGroups($ue["code"]); + $tp = $tpRepo->getGroups($ue["code"]); + + //set UE header + $nbrGroupeCM = count($cm); + $nbrGroupeTD = count($td); + $nbrGroupeTP = count($tp); + + $volumeUE = $ue["volumeCours"] + $ue["volumeTD"] + $ue["volumeTP"]; + $equiTDUE = $nbrGroupeCM*1.5*$ue["volumeCours"] + $nbrGroupeTD*$ue["volumeTD"] + $nbrGroupeTP*$ue["volumeTP"]; + + $excel->setCellValue("A$currentLine",$ue["code"]); + $excel->setCellValue("B$currentLine",$ue["label"]); + $excel->setCellValue("C$currentLine",$ue["required"] == 1? "OBL" : "OPT"); + $excel->setCellValue("D$currentLine","$volumeUE"); + $excel->setCellValue("E$currentLine","{$ue["volumeCours"]}"); + $excel->setCellValue("F$currentLine","$nbrGroupeCM"); + $excel->setCellValue("H$currentLine","{$ue["volumeTD"]}"); + $excel->setCellValue("I$currentLine","$nbrGroupeTD"); + $excel->setCellValue("K$currentLine","{$ue["volumeTP"]}"); + $excel->setCellValue("L$currentLine","$nbrGroupeTP"); + $excel->setCellValue("O$currentLine","$equiTDUE"); + + $excel->getStyle("A$currentLine:O$currentLine")->getFont()->setBold( true )->setSize(9); + $currentLine++; + + $nbrLine = max($nbrGroupeCM,$nbrGroupeTD,$nbrGroupeTP)-1; + foreach (range(0,$nbrLine) as $n){ + $excel->setCellValue("A$currentLine",$ue["code"]); + $excel->setCellValue("B$currentLine",$ue["label"]); + + $equiTD = 0; + if(isset($cm[$n])){ + $excel->setCellValue("E$currentLine","{$cm[$n]["volume"]}"); + $excel->setCellValue("F$currentLine",$formatFormations($cm[$n]["formations"])); + $excel->setCellValue("G$currentLine",$cm[$n]["lastName"]." ".$cm[$n]["firstName"]); + $equiTD += 1.5*$cm[$n]["volume"]; + } + + if(isset($td[$n])){ + $excel->setCellValue("H$currentLine","{$td[$n]["volume"]}"); + $excel->setCellValue("I$currentLine",$formatFormations($td[$n]["formations"])); + $excel->setCellValue("J$currentLine",$td[$n]["lastName"]." ".$td[$n]["firstName"]); + $equiTD += $td[$n]["volume"]; + } + + if(isset($tp[$n])){ + $excel->setCellValue("K$currentLine","{$tp[$n]["volume"]}"); + $excel->setCellValue("L$currentLine",$formatFormations($tp[$n]["formations"])); + $excel->setCellValue("M$currentLine",$tp[$n]["lastName"]." ".$tp[$n]["firstName"]); + $equiTD += $tp[$n]["volume"]; + } + + $excel->setCellValue("N$currentLine","$equiTD"); + + $excel->getStyle("A$currentLine:O$currentLine")->getFont()->setSize(8); + + $currentLine++; + } + $currentLine++; + } + + + //resize all columns + foreach(range('A','O') as $columnID) { + $excel->getColumnDimension($columnID)->setAutoSize(true); + } + + //set content type headers + header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + + //reating writer + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet); + + //as phpSpreadSheet do not support output on the buffer, we have to write in a temp file then read it + + //create temporary file; + $file = tmpfile(); + + //get URI + $metaDatas = stream_get_meta_data($file); + $tmpFilename = $metaDatas['uri']; + + //close file pointer + fclose($file); + + //write data + $writer->save("$tmpFilename"); + + //get file content + echo file_get_contents($tmpFilename); + } + +} \ No newline at end of file diff --git a/build/database/core/PDOWrapper/PDOWrapper.php b/build/database/core/PDOWrapper/PDOWrapper.php index 8d64b60..757def0 100644 --- a/build/database/core/PDOWrapper/PDOWrapper.php +++ b/build/database/core/PDOWrapper/PDOWrapper.php @@ -49,7 +49,11 @@ class PDOWrapper extends \PDO if($this->stacking){ return new PDOStatementWrapper($statement, $this); }else{ - return parent::prepare($statement, $options); + $st = parent::prepare($statement, $options); + if($st === false){ + throw new \PDOException("There is an error in your SQL statement"); + } + return $st; } } diff --git a/build/database/repo/ue.php b/build/database/repo/ue.php index 5f00462..9893a4b 100644 --- a/build/database/repo/ue.php +++ b/build/database/repo/ue.php @@ -290,4 +290,13 @@ class ue extends Repo_i { return $fetched; } + + public function exportUE() : array{ + $st = $this->pdo->prepare("SELECT * FROM UE + LEFT JOIN Formation + ON UE.Formation_idFormation = Formation.idFormation + ORDER BY Formation_idFormation IS NULL DESC, Formation_idFormation ASC"); + $st->execute(); + return $st->fetchAll(); + } } \ No newline at end of file diff --git a/config/modules.json b/config/modules.json index 108e9b3..b68a155 100644 --- a/config/modules.json +++ b/config/modules.json @@ -117,6 +117,13 @@ } } }, + "export":{ + "GET": { + "des": "Export the data of the current department and version to a Excel file", + "per": [["cas_admin"]], + "par": {} + } + }, "version":{ "switch":{ "GET": {