src/Service/Widget/Handlers/AppraisalAssessmentCyclesWidget.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Service\Widget\Handlers;
  3. use App\DataExport\AppraisalSummaryWorksheetFactory;
  4. use App\DataExport\AppraisalWorksheetFactory;
  5. use App\Entity\Appraisal;
  6. use App\Entity\User;
  7. use App\Entity\Widget;
  8. use App\Enum\Status;
  9. use App\Exception\ValidationException;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class AppraisalAssessmentCyclesWidget extends AssessmentCyclesWidget
  14. {
  15.     private $worksheetFactory;
  16.     public function __construct(
  17.         EntityManagerInterface $em,
  18.         AppraisalWorksheetFactory $appraisalWorksheet,
  19.         AppraisalSummaryWorksheetFactory $appraisalSummaryWorksheet
  20.     )
  21.     {
  22.         $this->worksheetFactory $appraisalWorksheet;
  23.         parent::__construct($em$appraisalSummaryWorksheet$appraisalWorksheet);
  24.     }
  25.     private function getAppraisals(Widget $widget)
  26.     {
  27.         $config $widget->getConfigKeyValuePairs();
  28.         $appraisalCycleId $this->getSourceId($config['source']);
  29.         return $this->em->getRepository(Appraisal::class)
  30.             ->findByCycleAndUsers($appraisalCycleId$this->users);
  31.     }
  32.     public function getData(Widget $widgetUser $userbool $export false$dataset null)
  33.     {
  34.         $config $widget->getConfigKeyValuePairs();
  35.         $appraisalCycleId $this->getSourceId($config['source']);
  36.         $data = [
  37.             'title' => $widget->getName(),
  38.             'completedCount' => 0,
  39.             'partiallyCompletedCount' => 0,
  40.             'notStartedCount' => 0
  41.         ];
  42.         if (empty($this->users)) {
  43.             return $data;
  44.         }
  45.         $completed = [];
  46.         $partiallyCompleted = [];
  47.         $notStarted = [];
  48.         foreach ($this->users as $user) {
  49.             $appraisal $this->em->getRepository(Appraisal::class)->findOneBy([
  50.                 'cycle' => $appraisalCycleId,
  51.                 'user' => $user
  52.             ]);
  53.             if (!$appraisal) {
  54.                 $notStarted[] = $user;
  55.             } else {
  56.                 // @todo remove this widget once Aura-1157 has been deployed to Marcura
  57.                 $assessmentCycle $appraisal->getFirstAssessmentCycle();
  58.                 if ($assessmentCycle->getStatus() === Status::INT_COMPLETE) {
  59.                     $completed[] = $assessmentCycle;
  60.                 } elseif ($assessmentCycle->getAssessments()->exists(function ($k$a) {
  61.                     return $a->getTimeCompleted();
  62.                 })) {
  63.                     $partiallyCompleted[] = $assessmentCycle;
  64.                 } else {
  65.                     $notStarted[] = $user;
  66.                 }
  67.             }
  68.         }
  69.         if ($export) {
  70.             switch ($dataset) {
  71.                 case "completed":
  72.                     return $this->getAssessmentTableData($completed);
  73.                 case "partially_completed":
  74.                     return $this->getAssessmentTableData($partiallyCompleted);
  75.                 case "not_started":
  76.                     return $this->getNotStartedAssessmentTableData($notStarted);
  77.                 default:
  78.                     throw new ValidationException("Unknown dataset selected");
  79.             }
  80.         }
  81.         $denominator sizeof($this->users);
  82.         $data['completedCount'] = (int) number_format((sizeof($completed) / $denominator) * 100);
  83.         $data['partiallyCompletedCount'] = (int) number_format((sizeof($partiallyCompleted) / $denominator) * 100);
  84.         $data['notStartedCount'] = (int) number_format((sizeof($notStarted) / $denominator) * 100);
  85.         return $data;
  86.     }
  87.     public function export(Widget $widgetUserInterface $user)
  88.     {
  89.         $appraisals $this->getAppraisals($widget);
  90.         $spreadSheet = new Spreadsheet();
  91.         $spreadSheet->removeSheetByIndex(0);
  92.         $spreadSheet->addSheet($this->worksheetFactory->make($appraisals$spreadSheet));
  93.         $filename $this->getFilename($widget);
  94.         return $this->download($spreadSheet$filename);
  95.     }
  96. }