/**
  * Iterates through elements of $each and renders child nodes
  *
  * @param \GIB\GradingTool\Domain\Model\Project $project
  * @param string $as The name of the iteration variable
  * @param string $key The name of the variable to store the current array key
  * @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
  * @return string Rendered string
  * @api
  */
 public function render($project, $as, $key = '', $iteration = NULL)
 {
     $this->arguments['each'] = $this->dataSheetService->getProcessedDataSheet($project);
     return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
 }
 /**
  * Export a PDF report for a project
  *
  * @param \GIB\GradingTool\Domain\Model\Project $project
  */
 public function exportReportAction(\GIB\GradingTool\Domain\Model\Project $project)
 {
     // access check
     $this->checkOwnerOrAdministratorAndDenyIfNeeded($project);
     // The processed submission
     $submission = $this->submissionService->getProcessedSubmission($project);
     if ($submission['hasError']) {
         // Don't export the Grading if is has errors
         $message = new \TYPO3\Flow\Error\Message('The Grading has errors and therefore it cannot be exported. Review and correct the Grading.', \TYPO3\Flow\Error\Message::SEVERITY_ERROR);
         $this->flashMessageContainer->addMessage($message);
         $this->redirect('index', 'Standard');
     }
     // The flat data sheet
     $dataSheet = $this->dataSheetService->getFlatProcessedDataSheet($project);
     $pdf = new \GIB\GradingTool\Utility\TcPdf();
     // set font
     \TCPDF_FONTS::addTTFfont('resource://GIB.GradingTool/Private/Fonts/Cambria.ttf', 'TrueTypeUnicode');
     \TCPDF_FONTS::addTTFfont('resource://GIB.GradingTool/Private/Fonts/Cambria Bold.ttf', 'TrueTypeUnicode');
     \TCPDF_FONTS::addTTFfont('resource://GIB.GradingTool/Private/Fonts/Cambria Italic.ttf', 'TrueTypeUnicode');
     \TCPDF_FONTS::addTTFfont('resource://GIB.GradingTool/Private/Fonts/Cambria Bold Italic.ttf', 'TrueTypeUnicode');
     // set margins
     $pdf->SetMargins(20, 45);
     $pdf->SetHeaderMargin(20);
     $pdf->SetFooterMargin(20);
     $pdf->SetFont('Cambria', '', 10);
     $pdf->SetHeaderFont(array('Cambria', '', 10));
     $pdf->SetFooterFont(array('Cambria', '', 10));
     $pdf->setHtmlVSpace(array('h1' => array(array('h' => 0, 'n' => 0), array('h' => 0, 'n' => 0)), 'h2' => array(array('h' => 0, 'n' => 0), array('h' => 0, 'n' => 0)), 'h3' => array(array('h' => 0, 'n' => 0), array('h' => 1, 'n' => 3)), 'h6' => array(array('h' => 0, 'n' => 0), array('h' => 0, 'n' => 0)), 'p' => array(array('h' => 0, 'n' => 0), array('h' => 1, 'n' => 2.5)), 'ul' => array(array('h' => 0, 'n' => 0), array('h' => 1, 'n' => 2.5))));
     $pdf->setListIndentWidth(3);
     $pdf->SetPrintHeader(TRUE);
     $pdf->SetPrintFooter(TRUE);
     // set document information
     $pdf->SetCreator(PDF_CREATOR);
     $pdf->SetAuthor('Global Infrastructure Basel Foundation');
     $pdf->SetTitle($project->getProjectTitle());
     $pdf->projectTitle = $project->getProjectTitle();
     $pdf->exportDate = strftime('%Y-%m-%d');
     // set auto page breaks
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     // set image scale factor
     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
     // Must be an Illustrator 3 file
     $epsLogoResource = 'resource://GIB.GradingTool/Private/Images/logo_gib_print.eps';
     // one pixel png
     $onePixelResource = 'resource://GIB.GradingTool/Private/Images/one-pixel.png';
     // partners png
     $gibPartnersResource = 'resource://GIB.GradingTool/Private/Images/gib-partners.png';
     /*** FRONT PAGE ***/
     $pdf->addPage();
     $arguments = array('dataSheet' => $dataSheet, 'project' => $project, 'epsLogoResource' => $epsLogoResource, 'onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('Front', $arguments), TRUE, FALSE, TRUE);
     /*** PARTNERS PAGE ***/
     $pdf->addPage();
     $arguments = array('gibPartnersResource' => $gibPartnersResource, 'onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('Partners', $arguments), TRUE, FALSE, TRUE);
     /*** TOC PAGE IS INSERTED AT PAGE 3 ***/
     /*** DATA SHEET FRONT ***/
     $pdf->addPage();
     $pdf->SetAutoPageBreak(FALSE);
     $arguments = array('onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('DataSheetFront', $arguments), TRUE, FALSE, TRUE);
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     /*** DATA SHEET ***/
     $pdf->addPage();
     $arguments = array('dataSheet' => $dataSheet, 'project' => $project, 'onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('DataSheet', $arguments), TRUE, FALSE, TRUE);
     /*** GRADING FRONT ***/
     $pdf->addPage();
     $pdf->SetAutoPageBreak(FALSE);
     $arguments = array('onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('GradingFront', $arguments), TRUE, FALSE, TRUE);
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     /*** GRADING TOOL ***/
     $pdf->addPage();
     $arguments = array('submission' => $submission, 'project' => $project, 'scoreData' => $this->submissionService->getScoreData(), 'onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('Grading', $arguments), TRUE, FALSE, TRUE);
     /*** ANALYSIS FRONT ***/
     $pdf->addPage();
     $pdf->SetAutoPageBreak(FALSE);
     $arguments = array('onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('AnalysisFront', $arguments), TRUE, FALSE, TRUE);
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     /*** ANALYSIS ***/
     $pdf->addPage();
     $radarChartFileName = $this->submissionService->getRadarImage($project);
     $lineGraphFileName = $this->submissionService->getLineGraphImage($project);
     $answerLevelGraphFileName = $this->submissionService->getAnswerLevelBarChartImage($project);
     $arguments = array('radarChartFileName' => $radarChartFileName, 'lineGraphFileName' => $lineGraphFileName, 'answerLevelGraphFileName' => $answerLevelGraphFileName, 'onePixelResource' => $onePixelResource, 'submission' => $submission);
     $pdf->writeHTML($this->pdfTemplateRenderer('Analysis', $arguments), TRUE, FALSE, TRUE);
     /** This was the last page */
     $pdf->lastPage();
     /*** TOC PAGE ***/
     $pdf->addTOCPage();
     $arguments = array('dataSheet' => $dataSheet, 'project' => $project, 'onePixelResource' => $onePixelResource);
     $pdf->writeHTML($this->pdfTemplateRenderer('TOCBeforeTOC', $arguments), TRUE, FALSE, TRUE);
     $arguments = array('onePixelResource' => $onePixelResource);
     $afterContent = $this->pdfTemplateRenderer('TOCAfterTOC', $arguments);
     $bookmarkTemplates = array();
     $bookmarkTemplates[0] = '<style>td.blue { color: #0f4fa2; }	td.orange { color: #f36e21; } td.red { color: #c92938; } td.black { color: #000000; } td.grey { color: #555555; }</style><table border="0" cellpadding="0" cellspacing="0"><tr><td class="#TOC_CSSCLASS#" width="12%"><strong>#TOC_CHAPTERNUMBER#</strong></td><td class="black" width="78%"><strong>#TOC_DESCRIPTION#</strong></td><td width="10%"><span style="font-weight:bold;font-size:10pt;text-align:right;" align="right">#TOC_PAGE_NUMBER#</span></td></tr></table>';
     $bookmarkTemplates[1] = '<style>td.blue { color: #0f4fa2; }	td.orange { color: #f36e21; } td.red { color: #c92938; } td.black { color: #000000; } td.grey { color: #555555; }</style><table border="0" cellpadding="0" cellspacing="0"><tr><td class="#TOC_CSSCLASS#" width="12%"><strong>#TOC_CHAPTERNUMBER#</strong></td><td class="grey" width="78%">#TOC_DESCRIPTION#</td><td width="10%"><span style="font-size:10pt;text-align:right;" align="right">#TOC_PAGE_NUMBER#</span></td></tr></table>';
     $pdf->addHTMLTOC(3, 'INDEX', $bookmarkTemplates, TRUE, 'B', array(128, 0, 0), $afterContent);
     $pdf->endTOCPage();
     $pdf->Output('export.pdf', 'I');
 }
 /**
  * Executes this finisher
  * @see AbstractFinisher::execute()
  *
  * @return void
  * @throws \TYPO3\Flow\Mvc\Exception\StopActionException();
  */
 protected function executeInternal()
 {
     /** @var \TYPO3\Form\Core\Runtime\FormRuntime $formRuntime */
     $formRuntime = $this->finisherContext->getFormRuntime();
     $formValueArray = $formRuntime->getFormState()->getFormValues();
     if ($formRuntime->getRequest()->getParentRequest()->getControllerActionName() == 'editDataSheet') {
         // we need to update the data sheet, we assume that the person is authenticated because a data sheet can only be edited by a authenticated user
         /** @var \GIB\GradingTool\Domain\Model\Project $project */
         $project = $this->projectRepository->findByIdentifier($formRuntime->getRequest()->getParentRequest()->getArgument('project'));
         // make a HTML representation of a diff of the old and new data
         $diffContent = DiffUtility::arrayDiffRecursive($project->getDataSheetContentArray(), $formValueArray);
         // store changes to project
         $project->setDataSheetContent($formValueArray);
         $project->setLastUpdated(new \TYPO3\Flow\Utility\Now());
         // update e-mail address (could have changed in the data sheet)
         $projectManagerElectronicAddress = new \TYPO3\Party\Domain\Model\ElectronicAddress();
         $projectManagerElectronicAddress->setIdentifier($formValueArray['projectManagerEmail']);
         $projectManagerElectronicAddress->setType(\TYPO3\Party\Domain\Model\ElectronicAddress::TYPE_EMAIL);
         $project->getProjectManager()->setPrimaryElectronicAddress($projectManagerElectronicAddress);
         $this->partyRepository->update($project->getProjectManager());
         $this->projectRepository->update($project);
         $this->persistenceManager->persistAll();
         // send a notification mail to the Administrator containing the changes
         $templateIdentifierOverlay = $this->templateService->getTemplateIdentifierOverlay('editDataSheetNotification', $project);
         $this->notificationMailService->sendNotificationMail($templateIdentifierOverlay, $project, NULL, '', '', $diffContent);
         // add a flash message
         $message = new \TYPO3\Flow\Error\Message('Your data sheet for project "%s" was successfully edited.', \TYPO3\Flow\Error\Message::SEVERITY_OK, array($project->getProjectTitle()));
         $this->flashMessageContainer->addMessage($message);
     } else {
         // we need to add a new data sheet
         /** @var \GIB\GradingTool\Domain\Model\Project $project */
         $project = new \GIB\GradingTool\Domain\Model\Project();
         $project->setProjectTitle($formValueArray['projectTitle']);
         $project->setDataSheetFormIdentifier($this->settings['forms']['dataSheet']['default']);
         $project->setSubmissionFormIdentifier($this->settings['forms']['submission']['default']);
         // store identifier=userName and password for later usage
         $identifier = $formValueArray['userName'];
         $password = $formValueArray['password'];
         // remove userName and password from data array so it doesn't get saved unencrypted
         unset($formValueArray['userName']);
         unset($formValueArray['password']);
         $project->setDataSheetContent($formValueArray);
         $project->setCreated(new \TYPO3\Flow\Utility\Now());
         $this->projectRepository->add($project);
         // add a flash message
         $message = new \TYPO3\Flow\Error\Message('Your data sheet for project "%s" was successfully submitted.', \TYPO3\Flow\Error\Message::SEVERITY_OK, array($formValueArray['projectTitle']));
         $this->flashMessageContainer->addMessage($message);
         if (!$this->authenticationManager->isAuthenticated() || $this->authenticationManager->isAuthenticated() && $this->authenticationManager->getSecurityContext()->hasRole('GIB.GradingTool:Administrator')) {
             // the product manager (supposedly) doesn't have an account yet, so we create one
             $projectManager = new \GIB\GradingTool\Domain\Model\ProjectManager();
             $projectManagerName = new \TYPO3\Party\Domain\Model\PersonName('', $formValueArray['projectManagerFirstName'], '', $formValueArray['projectManagerLastName']);
             $projectManager->setName($projectManagerName);
             $projectManagerElectronicAddress = new \TYPO3\Party\Domain\Model\ElectronicAddress();
             $projectManagerElectronicAddress->setIdentifier($formValueArray['projectManagerEmail']);
             $projectManagerElectronicAddress->setType(\TYPO3\Party\Domain\Model\ElectronicAddress::TYPE_EMAIL);
             $projectManager->addElectronicAddress($projectManagerElectronicAddress);
             $projectManager->setPrimaryElectronicAddress($projectManagerElectronicAddress);
             // add account
             $roles = array('GIB.GradingTool:ProjectManager');
             $authenticationProviderName = 'DefaultProvider';
             $account = $this->accountFactory->createAccountWithPassword($identifier, $password, $roles, $authenticationProviderName);
             $this->accountRepository->add($account);
             // add account to ProjectManager
             $projectManager->addAccount($account);
             // add project to ProjectManager
             $projectManager->addProject($project);
             // finally add the complete ProjectManager
             $this->partyRepository->add($projectManager);
             if (!$this->authenticationManager->getSecurityContext()->hasRole('GIB.GradingTool:Administrator')) {
                 // authenticate user if no Administrator is authenticated
                 $authenticationTokens = $this->securityContext->getAuthenticationTokensOfType('TYPO3\\Flow\\Security\\Authentication\\Token\\UsernamePassword');
                 if (count($authenticationTokens) === 1) {
                     $authenticationTokens[0]->setAccount($account);
                     $authenticationTokens[0]->setAuthenticationStatus(\TYPO3\Flow\Security\Authentication\TokenInterface::AUTHENTICATION_SUCCESSFUL);
                 }
                 // add a flash message
                 $message = new \TYPO3\Flow\Error\Message('The account "%s" was created and you were successfully logged in.', \TYPO3\Flow\Error\Message::SEVERITY_OK, array($identifier));
                 $this->flashMessageContainer->addMessage($message);
             }
         } elseif ($this->authenticationManager->isAuthenticated() && $this->authenticationManager->getSecurityContext()->hasRole('GIB.GradingTool:ProjectManager')) {
             // a productManager is adding a new project to his account
             /** @var \GIB\GradingTool\Domain\Model\ProjectManager $projectManager */
             $projectManager = $this->authenticationManager->getSecurityContext()->getParty();
             $projectManager->addProject($project);
             $this->partyRepository->update($projectManager);
         }
         $this->persistenceManager->persistAll();
         // send notification mail to project manager (bcc to team)
         $templateIdentifierOverlay = $this->templateService->getTemplateIdentifierOverlay('newDataSheetProjectManagerNotification', $project);
         $this->notificationMailService->sendNotificationMail($templateIdentifierOverlay, $project, $projectManager, $formValueArray['projectManagerFirstName'] . ' ' . $formValueArray['projectManagerLastName'], $formValueArray['projectManagerEmail']);
         // send notification mail to the GIB team
         $templateIdentifierOverlay = $this->templateService->getTemplateIdentifierOverlay('newDataSheetTeamNotification', $project);
         $dataSheetArray = $this->dataSheetService->getProcessedDataSheet($project);
         $this->notificationMailService->sendNotificationMail($templateIdentifierOverlay, $project, $projectManager, '', '', $dataSheetArray);
     }
     $this->persistenceManager->persistAll();
     // redirect to dashboard
     $formRuntime = $this->finisherContext->getFormRuntime();
     $request = $formRuntime->getRequest()->getMainRequest();
     $uriBuilder = new \TYPO3\Flow\Mvc\Routing\UriBuilder();
     $uriBuilder->setRequest($request);
     $uriBuilder->reset();
     $uri = $uriBuilder->uriFor('editDatasheet', array('project' => $project), 'Project');
     $response = $formRuntime->getResponse();
     $mainResponse = $response;
     while ($response = $response->getParentResponse()) {
         $mainResponse = $response;
     }
     $mainResponse->setStatus(303);
     $mainResponse->setHeader('Location', (string) $uri);
     throw new \TYPO3\Flow\Mvc\Exception\StopActionException();
 }