<?php
namespace App\Service\Widget\Handlers;
use App\DataExport\AppraisalSummaryWorksheetFactory;
use App\DataExport\AppraisalWorksheetFactory;
use App\Entity\Appraisal;
use App\Entity\User;
use App\Entity\Widget;
use App\Enum\Status;
use App\Exception\ValidationException;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\Security\Core\User\UserInterface;
class AppraisalAssessmentCyclesWidget extends AssessmentCyclesWidget
{
private $worksheetFactory;
public function __construct(
EntityManagerInterface $em,
AppraisalWorksheetFactory $appraisalWorksheet,
AppraisalSummaryWorksheetFactory $appraisalSummaryWorksheet
)
{
$this->worksheetFactory = $appraisalWorksheet;
parent::__construct($em, $appraisalSummaryWorksheet, $appraisalWorksheet);
}
private function getAppraisals(Widget $widget)
{
$config = $widget->getConfigKeyValuePairs();
$appraisalCycleId = $this->getSourceId($config['source']);
return $this->em->getRepository(Appraisal::class)
->findByCycleAndUsers($appraisalCycleId, $this->users);
}
public function getData(Widget $widget, User $user, bool $export = false, $dataset = null)
{
$config = $widget->getConfigKeyValuePairs();
$appraisalCycleId = $this->getSourceId($config['source']);
$data = [
'title' => $widget->getName(),
'completedCount' => 0,
'partiallyCompletedCount' => 0,
'notStartedCount' => 0
];
if (empty($this->users)) {
return $data;
}
$completed = [];
$partiallyCompleted = [];
$notStarted = [];
foreach ($this->users as $user) {
$appraisal = $this->em->getRepository(Appraisal::class)->findOneBy([
'cycle' => $appraisalCycleId,
'user' => $user
]);
if (!$appraisal) {
$notStarted[] = $user;
} else {
// @todo remove this widget once Aura-1157 has been deployed to Marcura
$assessmentCycle = $appraisal->getFirstAssessmentCycle();
if ($assessmentCycle->getStatus() === Status::INT_COMPLETE) {
$completed[] = $assessmentCycle;
} elseif ($assessmentCycle->getAssessments()->exists(function ($k, $a) {
return $a->getTimeCompleted();
})) {
$partiallyCompleted[] = $assessmentCycle;
} else {
$notStarted[] = $user;
}
}
}
if ($export) {
switch ($dataset) {
case "completed":
return $this->getAssessmentTableData($completed);
case "partially_completed":
return $this->getAssessmentTableData($partiallyCompleted);
case "not_started":
return $this->getNotStartedAssessmentTableData($notStarted);
default:
throw new ValidationException("Unknown dataset selected");
}
}
$denominator = sizeof($this->users);
$data['completedCount'] = (int) number_format((sizeof($completed) / $denominator) * 100);
$data['partiallyCompletedCount'] = (int) number_format((sizeof($partiallyCompleted) / $denominator) * 100);
$data['notStartedCount'] = (int) number_format((sizeof($notStarted) / $denominator) * 100);
return $data;
}
public function export(Widget $widget, UserInterface $user)
{
$appraisals = $this->getAppraisals($widget);
$spreadSheet = new Spreadsheet();
$spreadSheet->removeSheetByIndex(0);
$spreadSheet->addSheet($this->worksheetFactory->make($appraisals, $spreadSheet));
$filename = $this->getFilename($widget);
return $this->download($spreadSheet, $filename);
}
}