/** * @return Evaluation */ public function getEvaluation() { /* * If the evaluation is null, create it */ if (is_null($this->evaluation)) { /* * $projectService, $evaluationType, $country cannot be null when we want to create a new evaluation */ if (is_null($this->projectService)) { throw new \InvalidArgumentException(sprintf("ProjectService cannot be null to give evaluation in %s", $this->getAction(), __CLASS__)); } if (is_null($this->evaluationType)) { throw new \InvalidArgumentException(sprintf("Evaluation type cannot be null to give evaluation in %s", $this->getAction(), __CLASS__)); } if (is_null($this->country)) { throw new \InvalidArgumentException(sprintf("The country cannot be null to give evaluation in %s", $this->getAction(), __CLASS__)); } $this->evaluation = new Evaluation(); $this->evaluation->setProject($this->projectService->getProject()); $auth = $this->getServiceLocator()->get('zfcuser_auth_service'); $this->evaluation->setContact($auth->getIdentity()); $this->evaluation->setType($this->evaluationType); $this->evaluation->setCountry($this->country); } return $this->evaluation; }
/** * Returns true if and only if the assertion conditions are met. * * This method is passed the ACL, Role, Resource, and privilege to which the authorization query applies. If the * $role, $resource, or $privilege parameters are null, it means that the query applies to all Roles, Resources, or * privileges, respectively. * * @param Acl $acl * @param RoleInterface $role * @param ResourceInterface $resource * @param null $privilege * * @return boolean|null * * @throws \InvalidArgumentException */ public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null) { $countryId = $this->getRouteMatch()->getParam('country'); $evaluationTypeId = $this->getRouteMatch()->getParam('type'); $projectId = $this->getRouteMatch()->getParam('project'); /* * When the privilege is null, we need to grab it from the routeMatch */ if (is_null($privilege)) { $privilege = $this->getRouteMatch()->getParam('privilege'); } return true; /* * You need to be a funder, to see the overview. Return null if this ia not the case */ if (in_array($privilege, ['overview', 'overview-project', 'download-overview'])) { if (!$this->rolesHaveAccess(['funder', 'office'])) { return false; } else { //Stop the script here as we do not need to to know the rest of the evaluation return true; } } if (!$resource instanceof EvaluationEntity) { if (is_null($countryId) || is_null($evaluationTypeId) || is_null($projectId)) { throw new \InvalidArgumentException('The countryId, evaluationTypeId or projectId cannot be null'); } $country = $this->getGeneralService()->findEntityById('country', $countryId); $evaluationType = $this->getEvaluationService()->findEntityById('Evaluation\\Type', $evaluationTypeId); $project = $this->getProjectService()->setProjectId($projectId); if (is_null($country) || is_null($evaluationType) || is_null($project)) { throw new \InvalidArgumentException('The country, evaluationType or project cannot be null'); } $resource = new EvaluationEntity(); $resource->setCountry($country); $resource->setType($evaluationType); $resource->setProject($project->getProject()); } /* * Feed the project to the projectService to grab the information from the array */ $this->getProjectService()->setProject($resource->getProject()); //Give no access when no access to the project itself if (!$this->getProjectAssert()->assert($acl, $role, $resource->getProject(), 'view-community')) { return false; } switch ($privilege) { case 'download-version-documents': return $this->hasContact(); case 'evaluate-project': switch ($resource->getType()->getId()) { case EvaluationType::TYPE_PO_EVALUATION: /* * Check first of the project has a correct version */ $poClosedDate = $resource->getProject()->getCall()->getPoCloseDate(); if ($poClosedDate->add(new \DateInterval('P1M')) < new \DateTime()) { return false; } break; case EvaluationType::TYPE_FPP_EVALUATION: $fppClosedDate = $resource->getProject()->getCall()->getFppCloseDate(); if ($fppClosedDate->add(new \DateInterval('P1M')) < new \DateTime()) { return false; } break; case EvaluationType::TYPE_FUNDING_STATUS: /* * Funding status is office only, the call cannot be open */ break; } /* * Check to see if we have an active version */ $versionType = $this->getVersionService()->findEntityById('Version\\Type', $resource->getType()->getVersionType()); $version = $this->getVersionService()->findLatestVersionByType($resource->getProject(), $versionType); if (is_null($version)) { return false; } /* * Now return only true when the contact/country is participating in the project */ $contactActiveInCountry = false; $contactCountry = $this->getContactService()->parseCountry(); $countries = $this->getGeneralService()->findCountryByProject($resource->getProject()); foreach ($countries as $country) { if (!$contactActiveInCountry && !is_null($contactCountry) && $contactCountry->getId() === $country->getId()) { $contactActiveInCountry = true; } } /* * When the contact is not active in the country, return false because we do not allow evaluation */ if (!$contactActiveInCountry) { return false; } /* * No errors found, return true */ return true; case 'overview-project': case 'download-project': if ($this->rolesHaveAccess([strtolower(Access::ACCESS_OFFICE)])) { return true; } /* * Now return only true when the contact/country is participating in the project */ $contactActiveInCountry = false; $contactCountry = $this->getContactService()->parseCountry(); $countries = $this->getGeneralService()->findCountryByProject($resource->getProject()); foreach ($countries as $country) { if (!$contactActiveInCountry && !is_null($contactCountry) && $contactCountry->getId() === $country->getId()) { $contactActiveInCountry = true; } } /* * When the contact is not active in the country, return false because we do not allow evaluation */ if (!$contactActiveInCountry) { return false; } /* * Check to see if we have an active version */ $versionType = $this->getVersionService()->findEntityById('Version\\Type', $resource->getType()->getVersionType()); $version = $this->getVersionService()->findLatestVersionByType($resource->getProject(), $versionType); if (is_null($version)) { return false; } break; default: throw new \InvalidArgumentException(sprintf('Incorrect privilege (%s) requested', $privilege)); } return false; }
/** * @return ViewModel */ public function evaluateProjectAction() { $routeMatch = $this->getEvent()->getRouteMatch(); $country = $this->getGeneralService()->findEntityById('country', $routeMatch->getParam('country')); $evaluationType = $this->getProjectService()->findEntityById('Evaluation\\Type', $routeMatch->getParam('type')); $projectService = $this->getProjectService()->setProjectId($routeMatch->getParam('project')); $evaluationTypes = $this->getProjectService()->findAll('Evaluation\\Type'); /* * The evaluation can be there, or be null, then we need to create it. */ $evaluation = $this->getEvaluationService()->findEvaluationByCountryAndTypeAndProject($country, $evaluationType, $projectService->getProject()); if (is_null($evaluation)) { $evaluation = new Evaluation(); $evaluation->setProject($projectService->getProject()); $evaluation->setContact($this->zfcUserAuthentication()->getIdentity()); $evaluation->setType($evaluationType); $evaluation->setCountry($country); } $form = $this->getFormService()->prepare($evaluation->get('underscore_entity_name'), $evaluation, $_POST); $form->setAttribute('class', 'form-horizontal'); if ($this->getRequest()->isPost() && $form->isValid()) { if (!isset($_POST['cancel'])) { $this->getProjectService()->updateEntity($form->getData()); } return $this->redirect()->toRoute('community/evaluation/overview-project', ['country' => $country->getId(), 'type' => $evaluationType->getId(), 'project' => $projectService->getProject()->getId()]); } /* * Check to see if we have an active version */ $versionType = $this->getVersionService()->findEntityById('Version\\Type', $evaluationType->getVersionType()); $version = $this->getVersionService()->findLatestVersionByType($projectService->getProject(), $versionType); return new ViewModel(['evaluation' => $evaluation, 'evaluationType' => $evaluationType, 'evaluationTypes' => $evaluationTypes, 'version' => $version, 'form' => $form]); }