-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
426 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Exports; | ||
|
||
enum EvaluationResult | ||
{ | ||
case A; | ||
case NA; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace App\Exports; | ||
|
||
use App\Services\SummariesService; | ||
use Illuminate\Support\Collection; | ||
use Maatwebsite\Excel\Concerns\FromCollection; | ||
use Maatwebsite\Excel\Concerns\ShouldAutoSize; | ||
use Maatwebsite\Excel\Concerns\WithStyles; | ||
use Maatwebsite\Excel\Concerns\WithTitle; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | ||
|
||
class EvaluationSheet implements WithTitle,FromCollection,ShouldAutoSize,WithStyles | ||
{ | ||
|
||
const NAME_PARTS_SEPARATOR = '|'; | ||
private Collection $data; | ||
private string $title; | ||
|
||
public function __construct(string $title,Collection $data) | ||
{ | ||
$this->data=$data; | ||
$this->title =$title; | ||
} | ||
|
||
public function collection() | ||
{ | ||
$SUMMARY =0; | ||
$PERCENTAGE=1; | ||
$TIME_A=2; | ||
$TIME_NA=3; | ||
|
||
//Format is [bob][[1.1.2021,55%,...,projectName,clients]] | ||
$projects = [ | ||
$SUMMARY=>['name'=>'bilan'], | ||
$PERCENTAGE=>['name'=>'%'], | ||
$TIME_A=>['name'=> 'nb pér. '.EvaluationResult::A->name], | ||
$TIME_NA=>['name'=> 'nb pér. '. EvaluationResult::NA->name] | ||
]; | ||
|
||
//list all projects | ||
$this->data->each(function($studentEvaluations) use(&$projects){ | ||
foreach($studentEvaluations as $studentEvaluation){ | ||
$project = $studentEvaluation[SummariesService::PI_PROJECT]; | ||
$workload = $studentEvaluation[SummariesService::PI_ACCUMULATED_TIME]; | ||
$date = $studentEvaluation[SummariesService::PI_DATE]; | ||
$client = $studentEvaluation[SummariesService::PI_CLIENTS]; | ||
if(!in_array($project,$projects)){ | ||
$projects[]=[ | ||
'name'=>$project, | ||
'workload'=>$workload, | ||
'date'=>$date, | ||
'client'=>$client]; | ||
} | ||
} | ||
}); | ||
|
||
//Map students / projects data for row/column format | ||
$studentsProjectsMap = []; | ||
foreach($this->data->keys() as $studentId){ | ||
|
||
$studentEvals = $this->data[$studentId]; | ||
$studentEval=[]; | ||
|
||
//Store result for each project | ||
for($i=0;$i<sizeof($studentEvals);$i++) | ||
{ | ||
$studentEval = $studentEvals[$i]; | ||
|
||
$studentsProjectsMap[$studentId][$studentEval[SummariesService::PI_PROJECT]]= | ||
$studentEval[SummariesService::PI_SUCCESS_TIME]>0?EvaluationResult::A->name : EvaluationResult::NA->name; | ||
} | ||
|
||
//Compute summary for student (fake project but still using columns...) | ||
//as data is sorted by date, last eval corresponds to latest status | ||
$summary = $studentEval[SummariesService::PI_CURRENT_PERCENTAGE] >= SummariesService::SUCCESS_REQUIREMENT_IN_PERCENTAGE ? | ||
EvaluationResult::A->name: | ||
EvaluationResult::NA->name; | ||
$studentsProjectsMap[$studentId][$projects[$SUMMARY]['name']]=$summary; | ||
|
||
$studentsProjectsMap[$studentId][$projects[$PERCENTAGE]['name']]= | ||
$studentEval[SummariesService::PI_CURRENT_PERCENTAGE].""; //force string for excel (0->'' wihout) | ||
|
||
$totalSuccessTime = $studentEval[SummariesService::PI_ACCUMULATED_SUCCESS_TIME].""; //force string for excel (0->'' wihout) | ||
$studentsProjectsMap[$studentId][$projects[$TIME_A]['name']]=$totalSuccessTime; | ||
$studentsProjectsMap[$studentId][$projects[$TIME_NA]['name']]= | ||
($studentEval[SummariesService::PI_ACCUMULATED_TIME]-$totalSuccessTime).""; //force string for excel (0->'' wihout) | ||
} | ||
|
||
//Build excel rows | ||
$rows = []; | ||
$header = array_merge(['prénom','nom'],collect($projects)->map(function($p) { | ||
$label =$p['name']; | ||
if(array_key_exists('workload',$p)){ | ||
$label .= ' (' . $p['workload'] . 'p, ' . | ||
$p['date'] . ', ' . | ||
$p['client'] . ')'; | ||
} | ||
return $label; | ||
|
||
})->all()); | ||
|
||
$rows[0]=$header; | ||
foreach($studentsProjectsMap as $studentId=>$studentProjectsEvals){ | ||
[$firstname,$lastname] = explode(self::NAME_PARTS_SEPARATOR,$studentId); | ||
$columns = [$firstname,$lastname]; | ||
foreach($projects as $project){ | ||
$projectName = $project['name']; | ||
if(array_key_exists($projectName,$studentProjectsEvals)){ | ||
$columns[]=$studentProjectsEvals[$projectName]; | ||
}else{ | ||
$columns[]=''; | ||
} | ||
} | ||
$rows[]=$columns; | ||
} | ||
|
||
return collect($rows); | ||
} | ||
|
||
public function title(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function styles(Worksheet $sheet) | ||
{ | ||
//Set style | ||
$sheet->getStyle('A1:CC1')->getFont()->setItalic(true)->setBold(true); | ||
$sheet->getStyle('G1:CC1')->getAlignment()->setTextRotation(90); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Exports; | ||
|
||
use Illuminate\Support\Collection; | ||
use Maatwebsite\Excel\Concerns\FromCollection; | ||
use Maatwebsite\Excel\Concerns\ShouldAutoSize; | ||
use Maatwebsite\Excel\Concerns\WithMultipleSheets; | ||
|
||
class EvaluationsExport implements FromCollection,WithMultipleSheets,ShouldAutoSize | ||
{ | ||
|
||
private Collection $data; | ||
|
||
public function __construct(Collection $data) | ||
{ | ||
$this->data=$data; | ||
} | ||
|
||
public function collection() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function sheets(): array | ||
{ | ||
$sheets = []; | ||
|
||
//Format is [cin1b][bob][[1.1.2021,55%,...,projectName,clients]] | ||
$this->data->each( | ||
function ($groupEvaluations,$groupName) use (&$sheets){ | ||
$sheets[]=new EvaluationSheet($groupName,collect($groupEvaluations));; | ||
} | ||
); | ||
|
||
return $sheets; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Exports\EvaluationsExport; | ||
use App\Services\SummariesService; | ||
use App\SwissFrenchDateFormat; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Auth; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
|
||
class EvaluationExportController extends Controller | ||
{ | ||
/** | ||
* Handle the incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function __invoke(Request $request, SummariesService $summariesService) | ||
{ | ||
/* @var $evalData Collection*/ | ||
$evalData = $summariesService->getEvaluationsSummary( | ||
Auth::user(), | ||
$request->get("academicPeriodId"), | ||
$request->get("timeUnit"), | ||
json:false | ||
); | ||
|
||
return Excel::download(new EvaluationsExport($evalData), | ||
'inf-prat-'.now()->format(SwissFrenchDateFormat::DATE_TIME_FS).'.xlsx'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.