ptut-vhost/build/api/module/Excel.php

235 lines
8.1 KiB
PHP
Raw Normal View History

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 19/02/18
* Time: 21:31
*/
namespace api\module;
use error\core\Err;
use error\core\Error;
use PhpOffice\PhpSpreadsheet\Exception;
class Excel
{
private const startLineUE = 5;
private const startLineProf = 10;
public function post($args){
if(isset($_FILES["file"]["tmp_name"])){
try{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($_FILES["file"]["tmp_name"]);
$spreadsheet->setActiveSheetIndex(0);
$UESpreadsheet = $spreadsheet->getActiveSheet();
/*
* Setting Up state variables
*/
//current formation
$formation = "";
//current UE code
$UECode = "";
//current UE
$UE = [];
//array containing all the UEs
$allUE = [];
/*
* declare the lambda that will add finalized UE to the array
*/
$addEU = function() use (&$UECode,&$allUE,&$UE){
if($UECode != ""){
if(isset($allUE[$UECode])){
$counter = 1;
while(isset($allUE[$UECode.$counter])) $counter++;
$allUE[$UECode.$counter] = $UE;
}else{
$allUE[$UECode] = $UE;
}
$UE = [];
}
};
/*
* declaring the lambda tha twill extract the list of formation involved in the group
*/
$getFormations = function(?string $group) use (&$formation) : array{
if(!$group) return [$formation];
//replace the generic "INFO" keyword by the actual formation
$group = str_replace("INFO",$formation,$group);
//split the string
$groups = explode("+",$group);
//trim the strings
$groups = array_map('trim', $groups);
//delete empty strings
return array_filter($groups);
};
/*
* declaring the lambda that will compute the part of internal students in the course (used for stats purpose)
*/
$getInternalStudentPart = function(?string $group) use (&$formation, $getFormations) : float{
if(!$group) return 1.0;
$groups = $getFormations($group);
if(!in_array($formation,$groups)){
return 0.0;
}else{
return 1/count($groups);
}
};
//starting the iteration
foreach($UESpreadsheet->getRowIterator() as $row){
//skip the first rows
if($row->getRowIndex() < Excel::startLineUE) continue;
$firstCellValue = $UESpreadsheet->getCellByColumnAndRow(1,$row->getRowIndex())->getValue();
$secondCellValue = $UESpreadsheet->getCellByColumnAndRow(2,$row->getRowIndex())->getValue();
//if the first value is not null and the second is, this means we change formation and rest the UUCode
if($secondCellValue == "" and $firstCellValue != "") {$formation = $firstCellValue; $addEU(); $UECode = ""; continue; }
//if no UE is set yet, find it
if($UECode == "" and $firstCellValue != "") $UECode = $firstCellValue;
//if the line is empty this means we change UE
if($firstCellValue == "" and $secondCellValue == "") {$addEU(); $UECode = ""; continue;}
//now we have a formation, a UE and we are sure the line is not empty, let's fill up this bad boy
//if the required field is not set this means we are at the header of the UE
if(!isset($UE["required"])){
$UE["name"] = $UESpreadsheet->getCellByColumnAndRow(2,$row->getRowIndex())->getCalculatedValue();
$UE["required"] = $UESpreadsheet->getCellByColumnAndRow(3,$row->getRowIndex())->getValue();
$UE["TotalVH"] = $UESpreadsheet->getCellByColumnAndRow(4,$row->getRowIndex())->getCalculatedValue();
$UE["CourseVH"] = $UESpreadsheet->getCellByColumnAndRow(5,$row->getRowIndex())->getValue() ?: 0.0;
$UE["CourseGroup"] = $UESpreadsheet->getCellByColumnAndRow(6,$row->getRowIndex())->getValue() ?: 0;
$UE["TdVH"] = $UESpreadsheet->getCellByColumnAndRow(8,$row->getRowIndex())->getValue() ?: 0.0;
$UE["TdGroup"] = $UESpreadsheet->getCellByColumnAndRow(9,$row->getRowIndex())->getValue() ?: 0;
$UE["TpVH"] = $UESpreadsheet->getCellByColumnAndRow(11,$row->getRowIndex())->getValue() ?: 0.0;
$UE["TpGroup"] = $UESpreadsheet->getCellByColumnAndRow(12,$row->getRowIndex())->getValue() ?: 0;
continue;
}
//we have a new group! if the array is not set, initialize it
if(!isset($UE["groups"])) $UE["groups"] = [
"Course" => [],
"TD" => [],
"TP" => []
];
//compute Course
if ($UESpreadsheet->getCellByColumnAndRow(5,$row->getRowIndex())->getCalculatedValue()){
$UE["groups"]["Course"][] = [
"VH" => $UESpreadsheet->getCellByColumnAndRow(5,$row->getRowIndex())->getCalculatedValue(),
"internalStudentPart" => $getInternalStudentPart($UESpreadsheet->getCellByColumnAndRow(6,$row->getRowIndex())->getValue()),
"formations" => $getFormations($UESpreadsheet->getCellByColumnAndRow(6,$row->getRowIndex())->getValue()),
"professor" => $UESpreadsheet->getCellByColumnAndRow(7,$row->getRowIndex())->getValue() ?: null
];
}
//compute TDs
if($UESpreadsheet->getCellByColumnAndRow(8,$row->getRowIndex())->getCalculatedValue()){
$UE["groups"]["TD"][] = [
"VH" => $UESpreadsheet->getCellByColumnAndRow(8,$row->getRowIndex())->getCalculatedValue(),
"internalStudentPart" => $getInternalStudentPart($UESpreadsheet->getCellByColumnAndRow(9,$row->getRowIndex())->getValue()),
"formations" => $getFormations($UESpreadsheet->getCellByColumnAndRow(6,$row->getRowIndex())->getValue()),
"professor" => $UESpreadsheet->getCellByColumnAndRow(10,$row->getRowIndex())->getValue() ?: null
];
}
//compute TPs
if($UESpreadsheet->getCellByColumnAndRow(11,$row->getRowIndex())->getCalculatedValue()){
$UE["groups"]["TP"][] = [
"VH" => $UESpreadsheet->getCellByColumnAndRow(11,$row->getRowIndex())->getCalculatedValue(),
"internalStudentPart" => $getInternalStudentPart($UESpreadsheet->getCellByColumnAndRow(12,$row->getRowIndex())->getValue()),
"formations" => $getFormations($UESpreadsheet->getCellByColumnAndRow(6,$row->getRowIndex())->getValue()),
"professor" => $UESpreadsheet->getCellByColumnAndRow(13,$row->getRowIndex())->getValue() ?: null
];
}
}
/*
* Now we compute the professor list to get the full name , the number of hour he must do and the category he is in
*/
$spreadsheet->setActiveSheetIndex(1);
$ProfSpreadshit = $spreadsheet->getActiveSheet();
//array containing all the professors
$allProf = [];
//current professor category
$currentCategory = "";
//category as represented in the excel (as a number)
$currentCategoryIndex = 0;
foreach($ProfSpreadshit->getRowIterator() as $row){
//skip the first lines
if($row->getRowIndex() < Excel::startLineProf) continue;
$firstCellValue = $ProfSpreadshit->getCellByColumnAndRow(2,$row->getRowIndex())->getValue();
$secondCellValue = $ProfSpreadshit->getCellByColumnAndRow(3,$row->getRowIndex())->getValue();
//skip empty lines
if(!$firstCellValue and !$secondCellValue) continue;
//if the first cell contains something and not the second, it means that we change category
if($firstCellValue && !$secondCellValue){
$currentCategory = $firstCellValue;
$currentCategoryIndex++;
}
//if the line is valid
if($ProfSpreadshit->getCellByColumnAndRow(3,$row->getRowIndex())->getValue() and $ProfSpreadshit->getCellByColumnAndRow(4,$row->getRowIndex())->getValue()){
$allProf[$ProfSpreadshit->getCellByColumnAndRow(3,$row->getRowIndex())->getValue()] = [
"categoryLabel" => $currentCategory,
"categoryIndex" => $currentCategoryIndex,
"lastName" => explode(" ",$ProfSpreadshit->getCellByColumnAndRow(4,$row->getRowIndex())->getValue(),2)[1],
"firstName" => explode(" ",$ProfSpreadshit->getCellByColumnAndRow(4,$row->getRowIndex())->getValue(),2)[0],
"hoursToDo" => $ProfSpreadshit->getCellByColumnAndRow(5,$row->getRowIndex())->getValue() ?: 0
];
}
}
return [ 'data' => ["professors" => $allProf, "UEs" => $allUE ] ];
}catch (Exception $e){
return [ 'error' => new Error(Err::UnknownError) ];
}
}else{
return [ 'error' => new Error(Err::UploadError) ];
}
}
}