Rev 16817 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\Log\LoggerInterface;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Library\Functions;use LeadersLinked\Mapper\IndustryMapper;use LeadersLinked\Mapper\JobDescriptionMapper;use LeadersLinked\Mapper\SkillMapper;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use LeadersLinked\Mapper\SurveyCampaignMapper;use LeadersLinked\Mapper\SurveyFormMapper;use LeadersLinked\Model\SurveyCampaign;use LeadersLinked\Form\Survey\SurveyCampaignForm;use LeadersLinked\Mapper\LocationMapper;use LeadersLinked\Model\Location;use LeadersLinked\Mapper\SurveyCampaignJobDescriptionMapper;use LeadersLinked\Model\SurveyCampaignJobDescription;use LeadersLinked\Mapper\SurveyCampaignSkillMapper;use LeadersLinked\Model\SurveyCampaignSkill;use LeadersLinked\Mapper\SurveyCampaignIndustryMapper;use LeadersLinked\Model\SurveyCampaignIndustry;use LeadersLinked\Mapper\SurveyCampaignLanguageMapper;use LeadersLinked\Model\SurveyCampaignLanguage;use LeadersLinked\Mapper\CompanyUserMapper;use LeadersLinked\Mapper\CalendarEventMapper;use LeadersLinked\Model\CalendarEvent;use LeadersLinked\Mapper\SurveyTestMapper;use LeadersLinked\Model\SurveyTest;use Laminas\Http\Response;use LeadersLinked\Library\SurveyReport;use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;class OrganizationalClimateCampaignController extends AbstractActionController {/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @var \LeadersLinked\Cache\CacheInterface*/private $cache;/**** @var \Laminas\Log\LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var \Laminas\Mvc\I18n\Translator*/private $translator;/**** @param \Laminas\Db\Adapter\AdapterInterface $adapter* @param \LeadersLinked\Cache\CacheInterface $cache* @param \Laminas\Log\LoggerInterface LoggerInterface $logger* @param array $config* @param \Laminas\Mvc\I18n\Translator $translator*/public function __construct($adapter, $cache, $logger, $config, $translator){$this->adapter = $adapter;$this->cache = $cache;$this->logger = $logger;$this->config = $config;$this->translator = $translator;}public function indexAction() {$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();if ($request->isGet()) {$sandbox = $this->config['leaderslinked.runmode.sandbox'];if($sandbox) {$google_map_key = $this->config['leaderslinked.google_map.sandbox_api_key'];} else {$google_map_key = $this->config['leaderslinked.google_map.production_api_key'];}$headers = $request->getHeaders();$isJson = false;if ($headers->has('Accept')) {$accept = $headers->get('Accept');$prioritized = $accept->getPrioritized();foreach ($prioritized as $key => $value) {$raw = trim($value->getRaw());if (!$isJson) {$isJson = strpos($raw, 'json');}}}if ($isJson) {$search = $this->params()->fromQuery('search', []);$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);$start = intval($this->params()->fromQuery('start', 0), 10);$records_x_page = intval($this->params()->fromQuery('length', 10), 10);$page = intval($start / $records_x_page);$page++;$order = $this->params()->fromQuery('order', []);$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);$order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));$fields = ['name'];$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';if (!in_array($order_direction, ['ASC', 'DESC'])) {$order_direction = 'ASC';}$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/delete');$allowOverview = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/overview');$allowPdf = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/pdf');$allowExcel = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/excel');$surveyForms = [];$surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$paginator = $surveyCampaignMapper->fetchAllDataTableOrganizationClimateByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);$items = [];$records = $paginator->getCurrentItems();foreach ($records as $record){if(isset($surveyForms[$record->form_id])) {$surveyForm = $surveyForms[$record->form_id];} else {$surveyForm = $surveyFormMapper->fetchOne($record->form_id);$surveyForms[$record->form_id] = $surveyForm;}$dtStartDate = \DateTime::createFromFormat('Y-m-d', $record->start_date);$dtEndDate = \DateTime::createFromFormat('Y-m-d', $record->end_date);$item = ['id' => $record->id,'name' => $record->name,'form' => $surveyForm->name,'details' => ['start_date' => $dtStartDate ? $dtStartDate->format('d/m/Y') : '','end_date' => $dtEndDate ? $dtEndDate->format('d/m/Y') : '','total_test' => $record->total_test,'completed_test' => $record->completed_test,],'status' => $record->status,'actions' => ['link_delete' => $allowDelete ? $this->url()->fromRoute('organizational-climate/campaign/delete', ['id' => $record->uuid]) : '','link_overview' => $allowOverview && $record->completed_test ? $this->url()->fromRoute('organizational-climate/campaign/overview', ['id' => $record->uuid]) : '','link_pdf' => $allowPdf && $record->completed_test ? $this->url()->fromRoute('organizational-climate/campaign/pdf', ['id' => $record->uuid]) : '','link_excel' => $allowExcel && $record->completed_test ? $this->url()->fromRoute('organizational-climate/campaign/excel', ['id' => $record->uuid]) : '',]];array_push($items, $item);}return new JsonModel(['success' => true,'data' => ['items' => $items,'total' => $paginator->getTotalItemCount(),]]);} else {$form = new SurveyCampaignForm($this->adapter, $currentCompany->id, SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE);$this->layout()->setTemplate('layout/layout-backend');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/organizational-climate-campaign/index.phtml');$viewModel->setVariable('form', $form);$viewModel->setVariable('google_map_key', $google_map_key);return $viewModel;}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}public function addAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();if ($request->isPost()) {$dataPost = $request->getPost()->toArray();$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyCampaign::STATUS_INACTIVE;$start_date = empty($dataPost['start_date']) ? '' : $dataPost['start_date'];$dt = \DateTime::createFromFormat('d/m/Y', $start_date);if($dt) {$dataPost['start_date'] = $dt->format('Y-m-d');} else {$dataPost['start_date'] = '';}$end_date = empty($dataPost['end_date']) ? '' : $dataPost['end_date'];$dt = \DateTime::createFromFormat('d/m/Y', $end_date);if($dt) {$dataPost['end_date'] = $dt->format('Y-m-d');} else {$dataPost['end_date'] = '';}$form = new SurveyCampaignForm($this->adapter, $currentCompany->id, SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE);$form->setData($dataPost);if ($form->isValid()) {$dataPost = (array) $form->getData();$surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);$surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);$content = json_encode(['name' => $surveyForm->name,'text' => $surveyForm->text,'content' => json_decode($surveyForm->content)]);$surveyCampaign = new SurveyCampaign();$surveyCampaign->type = SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE;$surveyCampaign->company_id = $currentCompany->id;$surveyCampaign->form_id = $surveyForm->id;$surveyCampaign->name = $dataPost['name'];$surveyCampaign->start_date = $dataPost['start_date'];$surveyCampaign->end_date = $dataPost['end_date'];$surveyCampaign->status = $dataPost['status'];$surveyCampaign->identity = $dataPost['identity'];$surveyCampaign->content = $content;$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);if($surveyCampaignMapper->insert($surveyCampaign)) {$ok = true;if(!empty($dataPost['location_search'])){$location = new Location();$location->formatted_address = $dataPost['formatted_address'];$location->city1 = $dataPost['city1'];$location->city2 = $dataPost['city2'];$location->address1 = $dataPost['address1'];$location->address2 = $dataPost['address2'];$location->country = $dataPost['country'];$location->latitude = $dataPost['latitude'];$location->longitude = $dataPost['longitude'];$location->postal_code = $dataPost['postal_code'];$location->state = $dataPost['state'];$locationMapper = LocationMapper::getInstance($this->adapter);if($locationMapper->insert($location)) {$surveyCampaign->location_id = $location->id;$surveyCampaignMapper->update($surveyCampaign);} else {$ok = false;}}$user_ids = [];if(!empty($dataPost['job_description_id'])) {$surveyCampaignJobDescriptionMapper = SurveyCampaignJobDescriptionMapper::getInstance($this->adapter);$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);foreach($dataPost['job_description_id'] as $jobDescriptionUuid){$jobDescription = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionUuid);if($jobDescription && $jobDescription->company_id = $surveyCampaign->company_id) {$surveyCampaignJobDescription = new SurveyCampaignJobDescription();$surveyCampaignJobDescription->job_description_id = $jobDescription->id;$surveyCampaignJobDescription->campaign_id = $surveyCampaign->id;$ok = $ok && $surveyCampaignJobDescriptionMapper->insert($surveyCampaignJobDescription);}}$aux_ids = $surveyCampaignJobDescriptionMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if(!empty($aux_ids)) {$user_ids = array_merge($user_ids, $aux_ids);}}if(!empty($dataPost['skill_id'])) {$surveyCampaignSkillMapper = SurveyCampaignSkillMapper::getInstance($this->adapter);$skillMapper = SkillMapper::getInstance($this->adapter);foreach($dataPost['skill_id'] as $skillUuid) {$skill = $skillMapper->fetchOneByUuid($skillUuid);if($skill) {$surveyCampaignSkill = new SurveyCampaignSkill();$surveyCampaignSkill->skill_id = $skill->id;$surveyCampaignSkill->campaign_id = $surveyCampaign->id;$ok = $ok && $surveyCampaignSkillMapper->insert($surveyCampaignSkill);}}$aux_ids = $surveyCampaignSkillMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if(!empty($aux_ids)) {$user_ids = array_merge($user_ids, $aux_ids);}}if(!empty($dataPost['industry_id'])) {$surveyCampaignIndustryMapper = SurveyCampaignIndustryMapper::getInstance($this->adapter);$industryMapper = IndustryMapper::getInstance($this->adapter);foreach($dataPost['industry_id'] as $industryUuid){$industry = $industryMapper->fetchOneByUuid($industryUuid);if($industry) {$surveyCampaignIndustry = new SurveyCampaignIndustry();$surveyCampaignIndustry->industry_id = $industry->id;$surveyCampaignIndustry->campaign_id = $surveyCampaign->id;$ok = $ok && $surveyCampaignIndustryMapper->insert($surveyCampaignIndustry);}}$aux_ids = $surveyCampaignIndustryMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if(!empty($aux_ids)) {$user_ids = array_merge($user_ids, $aux_ids);}}if(!empty($dataPost['language_id'])){$surveyCampaignLanguageMapper = SurveyCampaignLanguageMapper::getInstance($this->adapter);foreach($dataPost['language_id'] as $language_id){$surveyCampaignLanguage = new SurveyCampaignLanguage();$surveyCampaignLanguage->language_id = $language_id;$surveyCampaignLanguage->campaign_id = $surveyCampaign->id;$ok = $ok && $surveyCampaignLanguageMapper->insert($surveyCampaignLanguage);}$aux_ids = $surveyCampaignLanguageMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if(!empty($aux_ids)) {$user_ids = array_merge($user_ids, $aux_ids);}}if($surveyCampaign->location_id) {$locationMapper = LocationMapper::getInstance($this->adapter);$location = $locationMapper->fetchOne($surveyCampaign->location_id);if($location) {$aux_ids = $locationMapper->fetchAllUserIdsByCompanyId($surveyCampaign->company_id, $location->latitude, $location->longitude);if(!empty($aux_ids)) {$user_ids = array_merge($user_ids, $aux_ids);}}}if(!empty($user_ids)) {$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);$calendarEventMapper = CalendarEventMapper::getInstance($this->adapter);$user_ids = array_unique($user_ids);foreach($user_ids as $user_id){$surveyTest = new SurveyTest();$surveyTest->company_id = $surveyCampaign->company_id;$surveyTest->campaign_id = $surveyCampaign->id;$surveyTest->user_id = $user_id;$surveyTest->content = $content;$surveyTest->status = SurveyTest::STATUS_PENDING;if($surveyTestMapper->insert($surveyTest)) {$calendarEvent = new CalendarEvent();$calendarEvent->relational_id = $surveyTest->id;$calendarEvent->user_id = $user_id;$calendarEvent->type = CalendarEvent::TYPE_SURVEY_ORGANIZATIONAL_CLIMATE;$calendarEvent->start_time = $surveyCampaign->end_date;$calendarEventMapper->insert($calendarEvent);}}}$data = ['success' => true,'data' => 'LABEL_RECORD_ADDED'];} else {$data = ['success' => false,'data' => $surveyCampaignMapper->getError()];}return new JsonModel($data);} else {$messages = [];$form_messages = (array) $form->getMessages();foreach ($form_messages as $fieldname => $field_messages) {$messages[$fieldname] = array_values($field_messages);}return new JsonModel(['success' => false,'data' => $messages]);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}/*public function viewAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$organizationalClimate = $surveyCampaignMapper->fetchOneByUuid($uuid);if (!$organizationalClimate) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($organizationalClimate->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if ($request->isGet()) {$hydrator = new ObjectPropertyHydrator();$organizationalClimatelenguageMapper = OrganizationalClimateLanguageMapper::getInstance($this->adapter);$languages = $organizationalClimatelenguageMapper->fetchAllBySurveyId($organizationalClimate->id);$lenguage = [];foreach($languages as $language){array_push($lenguage, $language->language_id);}$organizationalClimateSkillMapper = OrganizationalClimateSkillMapper::getInstance($this->adapter);$skills = $organizationalClimateSkillMapper->fetchAllBySurveyId($organizationalClimate->id);$skilla = [];foreach($skills as $skill){array_push($skilla, $skill->skill_id);}$skillMaster = [];foreach($skilla as $skillb){$skillMapper = SkillMapper::getInstance($this->adapter);$skillm = $skillMapper->fetchOne($skillb);array_push($skillMaster, $skillm->uuid);}$OrganizationalClimateJobDescriptionMapper = OrganizationalClimateJobDescriptionMapper::getInstance($this->adapter);$OrganizationalClimateJobDescriptions = $OrganizationalClimateJobDescriptionMapper->fetchAllBySurveyId($organizationalClimate->id);$OrganizationalClimateJobDescriptionA = [];foreach($OrganizationalClimateJobDescriptions as $OrganizationalClimateJobDescription){array_push($OrganizationalClimateJobDescriptionA, $OrganizationalClimateJobDescription->job_description_id);}$OrganizationalClimateJobDescriptionMaster = [];foreach($OrganizationalClimateJobDescriptionA as $OrganizationalClimateJobDescriptionB){$JobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);$JobDescriptionm = $JobDescriptionMapper->fetchOne($OrganizationalClimateJobDescriptionB);array_push($OrganizationalClimateJobDescriptionMaster, $JobDescriptionm->uuid);}$organizationalClimateIndustryMapper = OrganizationalClimateIndustryMapper::getInstance($this->adapter);$industrys = $organizationalClimateIndustryMapper->fetchAllBySurveyId($organizationalClimate->id);$industrya = [];foreach($industrys as $industry){array_push($industrya, $industry->industry_id);}$industryMaster = [];foreach($industrya as $industryb){$industryMapper = IndustryMapper::getInstance($this->adapter);$industrym = $industryMapper->fetchOne($industryb);array_push($industryMaster, $industrym->uuid);}$organizationalClimateLocationMapper = OrganizationalClimateLocationMapper::getInstance($this->adapter);$location = $organizationalClimateLocationMapper->fetchAllBySurveyId($organizationalClimate->id);// return new JsonModel([// 'success' => false,// 'data' => $location// ]);$organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);$organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);// return new JsonModel([// 'success' => false,// 'data' => $OrganizationalClimateJobDescriptionA// ]);$data = ['success' => true,'data' => ['name' => $organizationalClimate->name,'form_id' => $organizationalClimateForm->uuid,'target' => $organizationalClimate->target,'identity' => $organizationalClimate->identity,'since_date' => $organizationalClimate->since_date,'last_date' => $organizationalClimate->last_date,'status' => $organizationalClimate->status,'lenguage_id' => $lenguage,'skill_id' => $skillMaster,'job_description_id' => $OrganizationalClimateJobDescriptionMaster,'industry_id' => $industryMaster,//'location' => $location->city1,]];return new JsonModel($data);} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}*/public function deleteAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);if (!$surveyCampaign) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($surveyCampaign->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if ($request->isPost()) {$result = $surveyCampaignMapper->delete($surveyCampaign->id);if ($result) {$this->logger->info('Se borro la capaña : ' . $surveyCampaign->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$data = ['success' => true,'data' => 'LABEL_RECORD_DELETED'];} else {$data = ['success' => false,'data' => $surveyCampaignMapper->getError()];return new JsonModel($data);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}public function overviewAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);if (!$surveyCampaign) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($surveyCampaign->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);$surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if (!$surveyTests) {$data = ['success' => false,'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'];return new JsonModel($data);}if ($request->isGet()) {$allTests = array_map(function($response) {$content = json_decode($response->content, true);return $content['content'];},$surveyTests,);$data = [];$countTests = count($allTests);foreach($allTests as $test){foreach($test as $section){if(!isset($data[ $section['slug_section'] ] )) {$data[ $section['slug_section'] ] = ['name' => $section['name'],'text' => $section['text'],'questions' => []];}foreach($section['questions'] as $question){switch ($question['type']){case 'range1to5':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => ['option1' => ['text' => '1','total' => 0,'average' => 0,],'option2' => ['text' => '2','total' => 0,'average' => 0,],'option3' => ['text' => '3','total' => 0,'average' => 0,],'option4' => ['text' => '4','total' => 0,'average' => 0,],'option5' => ['text' => '5','total' => 0,'average' => 0,]]];}$total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;break;case 'multiple':case 'simple':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => []];foreach($question['options'] as $option){$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = ['text' => $option['text'],'total' => 0,'average' => 0,];}}foreach($question['options'] as $option){if($option['checked']) {$total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;}}break;}}}}$this->layout()->setTemplate('layout/layout-backend.phtml');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/organizational-climate-campaign/overview.phtml');$viewModel->setVariables(['data' => $data,'campaign' => $surveyCampaign,]);return $viewModel ;} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}}public function pdfAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);if (!$surveyCampaign) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($surveyCampaign->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);$surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if (!$surveyTests) {$data = ['success' => false,'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'];return new JsonModel($data);}if ($request->isGet()) {$allTests = array_map(function($response) {$content = json_decode($response->content, true);return $content['content'];},$surveyTests,);$data = [];$countTests = count($allTests);foreach($allTests as $test){foreach($test as $section){if(!isset($data[ $section['slug_section'] ] )) {$data[ $section['slug_section'] ] = ['name' => $section['name'],'text' => $section['text'],'questions' => []];}foreach($section['questions'] as $question){switch ($question['type']){case 'range1to5':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => ['option1' => ['text' => '1','total' => 0,'average' => 0,],'option2' => ['text' => '2','total' => 0,'average' => 0,],'option3' => ['text' => '3','total' => 0,'average' => 0,],'option4' => ['text' => '4','total' => 0,'average' => 0,],'option5' => ['text' => '5','total' => 0,'average' => 0,]]];}$total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;break;case 'multiple':case 'simple':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => []];foreach($question['options'] as $option){$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = ['text' => $option['text'],'total' => 0,'average' => 0,];}}foreach($question['options'] as $option){if($option['checked']) {$total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;}}break;}}}}$target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $currentCompany->uuid;$header = $currentCompany->header ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->header : '';if(empty($header) || !file_exists($header)) {$header = $this->config['leaderslinked.images_default.company_pdf_header'];}$footer = $currentCompany->footer ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->footer : '';if(empty($footer) || !file_exists($footer)) {$footer = $this->config['leaderslinked.images_default.company_pdf_footer'];}$content = json_decode($surveyCampaign->content, true);//Generate New PDF$pdf = new SurveyReport();$pdf->header = $header;$pdf->footer = $footer;$pdf->campaignName = $surveyCampaign->name;$pdf->formName = $content['name'];$pdf->formText = $content['text'];$pdf->SetTitle(Functions::utf8_decode($surveyCampaign->name));$pdf->translator = $this->translator;$target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;if(!file_exists($target_path)) {mkdir($target_path, 0755, true);} else {Functions::deleteFiles($target_path);}$pdf->AliasNbPages();foreach($data as $slug_section => $section){$pdf->AddPage();$pdf->addSection($section['name'], $section['text']);$countQuestion = 0;foreach ($section['questions'] as $slug_question => $question){if($countQuestion == 2) {$pdf->AddPage();$countQuestion = 0;}$labels = [];$values = [];$existEmptyOption = false;foreach($question['options'] as $slug_option => $option){if(empty($option['average'])) {$existEmptyOption = true;continue;}array_push($labels, $option['text']);array_push($values, $option['average']);}if($existEmptyOption) {array_push($labels, $this->translator->translate('LABEL_SURVEY_OTHER_EMPTY_OPTIONS'));array_push($values, 0);}$filename = $target_path . DIRECTORY_SEPARATOR . $slug_question . '.png';$pdf->Cell(0,10, $question['text'],0,1);$pdf->pieChart($labels, $values, '', $filename);$pdf->SetFont('Arial', '', 12);$pdf->Image($filename, 10, $pdf->getY(), 190);$pdf->setY($pdf->getY() + 90);$countQuestion++;}}$content = $pdf->Output('S');$response = new Response();$response->setStatusCode(200);$response->setContent($content);$headers = $response->getHeaders();$headers->clearHeaders();$headers->addHeaderLine('Content-Description: File Transfer');$headers->addHeaderLine('Content-Type: application/pdf');//$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);$headers->addHeaderLine('Content-Transfer-Encoding: binary');$headers->addHeaderLine('Expires: 0');$headers->addHeaderLine('Cache-Control: must-revalidate');$headers->addHeaderLine('Pragma: public');return $response;} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}}public function excelAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);$surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);if (!$surveyCampaign) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($surveyCampaign->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);$surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);if (!$surveyTests) {$data = ['success' => false,'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'];return new JsonModel($data);}if ($request->isGet()) {$allTests = array_map(function($response) {$content = json_decode($response->content, true);return $content['content'];},$surveyTests,);$data = [];$countTests = count($allTests);foreach($allTests as $test){foreach($test as $section){if(!isset($data[ $section['slug_section'] ] )) {$data[ $section['slug_section'] ] = ['name' => $section['name'],'text' => $section['text'],'questions' => []];}foreach($section['questions'] as $question){switch ($question['type']){case 'range1to5':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => ['option1' => ['text' => '1','total' => 0,'average' => 0,],'option2' => ['text' => '2','total' => 0,'average' => 0,],'option3' => ['text' => '3','total' => 0,'average' => 0,],'option4' => ['text' => '4','total' => 0,'average' => 0,],'option5' => ['text' => '5','total' => 0,'average' => 0,]]];}$total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;break;case 'multiple':case 'simple':if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {$data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = ['text' => $question['text'],'options' => []];foreach($question['options'] as $option){$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = ['text' => $option['text'],'total' => 0,'average' => 0,];}}foreach($question['options'] as $option){if($option['checked']) {$total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];$total++;$average = ($total * 100) / $countTests;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;$data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;}}break;}}}}$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();$row = 1;foreach($data as $slug_section => $section){$sheet->setCellValue('A' . $row , $section['name']);$row++;$sheet->setCellValue('A' . $row , $section['text']);$row++;foreach ($section['questions'] as $slug_question => $question){$sheet->setCellValue('B' . $row , $question['text']);$row++;$sheet->setCellValue('C' . $row , $this->translator->translate('LABEL_OPTION'));$sheet->setCellValue('D' . $row , $this->translator->translate('LABEL_TOTAL'));$sheet->setCellValue('E' . $row , $this->translator->translate('LABEL_AVERAGE') . ' %');$row++;foreach($question['options'] as $slug_option => $option){$sheet->setCellValue('C' . $row , $option['text']);$sheet->setCellValue('D' . $row , $option['total']);$sheet->setCellValue('E' . $row , round($option['average'], 2));$row++;}$row++;}$row++;}$target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;if(!file_exists($target_path)) {mkdir($target_path, 0755, true);}$filename = Functions::normalizeStringFilename($surveyCampaign->uuid . '.xls');$fullFilename = $target_path . DIRECTORY_SEPARATOR . $filename;$writer = new Xlsx($spreadsheet);$writer->save($fullFilename);$content = file_get_contents($fullFilename);$response = new Response();$response->setStatusCode(200);$response->setContent($content);$headers = $response->getHeaders();$headers->clearHeaders();$headers->addHeaderLine('Content-Description: File Transfer');$headers->addHeaderLine('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');$headers->addHeaderLine('Content-Disposition: attachment; filename='. $filename);$headers->addHeaderLine('Content-Transfer-Encoding: binary');$headers->addHeaderLine('Expires: 0');$headers->addHeaderLine('Cache-Control: must-revalidate');$headers->addHeaderLine('Pragma: public');return $response;} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}}}