| 7218 | eleazar | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | declare(strict_types=1);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | namespace LeadersLinked\Controller;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           |  |  | 8 | use Laminas\Cache\Storage\Adapter\AbstractAdapter;
 | 
        
           |  |  | 9 | use Laminas\Mvc\Controller\AbstractActionController;
 | 
        
           |  |  | 10 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 11 | use Laminas\View\Model\ViewModel;
 | 
        
           |  |  | 12 | use Laminas\View\Model\JsonModel;
 | 
        
           |  |  | 13 | use LeadersLinked\Mapper\QueryMapper;
 | 
        
           |  |  | 14 | use Laminas\Db\Sql\Select;
 | 
        
           |  |  | 15 | use LeadersLinked\Library\Functions;
 | 
        
           |  |  | 16 | use LeadersLinked\Mapper\OrganizationalClimateTestMapper;
 | 
        
           |  |  | 17 | use LeadersLinked\Mapper\OrganizationalClimateMapper;
 | 
        
           |  |  | 18 | use LeadersLinked\Mapper\OrganizationalClimateFormMapper;
 | 
        
           |  |  | 19 | use LeadersLinked\Form\OrganizationalClimateTestForm;
 | 
        
           |  |  | 20 | use LeadersLinked\Model\OrganizationalClimateTest;
 | 
        
           |  |  | 21 | use LeadersLinked\Mapper\UserMapper;
 | 
        
           |  |  | 22 | use LeadersLinked\Hydrator\ObjectPropertyHydrator;
 | 
        
           | 7530 | eleazar | 23 | use LeadersLinked\Library\SurveyReport;
 | 
        
           | 7218 | eleazar | 24 | use PhpOffice\PhpSpreadsheet\Spreadsheet;
 | 
        
           |  |  | 25 | use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 | 
        
           |  |  | 26 | use PhpOffice\PhpSpreadsheet\IOFactory;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | class OrganizationalClimateReportController extends AbstractActionController {
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |     /**
 | 
        
           |  |  | 31 |      *
 | 
        
           |  |  | 32 |      * @var AdapterInterface
 | 
        
           |  |  | 33 |      */
 | 
        
           |  |  | 34 |     private $adapter;
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     /**
 | 
        
           |  |  | 37 |      *
 | 
        
           |  |  | 38 |      * @var AbstractAdapter
 | 
        
           |  |  | 39 |      */
 | 
        
           |  |  | 40 |     private $cache;
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |     /**
 | 
        
           |  |  | 43 |      *
 | 
        
           |  |  | 44 |      * @var  LoggerInterface
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     private $logger;
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      *
 | 
        
           |  |  | 50 |      * @var array
 | 
        
           |  |  | 51 |      */
 | 
        
           |  |  | 52 |     private $config;
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      *
 | 
        
           |  |  | 56 |      * @param AdapterInterface $adapter
 | 
        
           |  |  | 57 |      * @param AbstractAdapter $cache
 | 
        
           |  |  | 58 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 59 |      * @param array $config
 | 
        
           |  |  | 60 |      */
 | 
        
           |  |  | 61 |     public function __construct($adapter, $cache, $logger, $config) {
 | 
        
           |  |  | 62 |         $this->adapter = $adapter;
 | 
        
           |  |  | 63 |         $this->cache = $cache;
 | 
        
           |  |  | 64 |         $this->logger = $logger;
 | 
        
           |  |  | 65 |         $this->config = $config;
 | 
        
           |  |  | 66 |     }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     public function indexAction() {
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         $request = $this->getRequest();
 | 
        
           |  |  | 71 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 72 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 73 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |         try{
 | 
        
           |  |  | 76 |         $request = $this->getRequest();
 | 
        
           |  |  | 77 |         if ($request->isGet()) {
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |             $headers = $request->getHeaders();
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |             $isJson = false;
 | 
        
           |  |  | 82 |             if ($headers->has('Accept')) {
 | 
        
           |  |  | 83 |                 $accept = $headers->get('Accept');
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |                 $prioritized = $accept->getPrioritized();
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |                 foreach ($prioritized as $key => $value) {
 | 
        
           |  |  | 88 |                     $raw = trim($value->getRaw());
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |                     if (!$isJson) {
 | 
        
           |  |  | 91 |                         $isJson = strpos($raw, 'json');
 | 
        
           |  |  | 92 |                     }
 | 
        
           |  |  | 93 |                 }
 | 
        
           |  |  | 94 |             }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |             if ($isJson) {
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |                 $search = $this->params()->fromQuery('search', []);
 | 
        
           |  |  | 99 |                 $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
 | 
        
           |  |  | 100 |   | 
        
           | 15371 | efrain | 101 |                 $start = intval($this->params()->fromQuery('start', 0), 10);
 | 
        
           | 7218 | eleazar | 102 |                 $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
 | 
        
           | 15371 | efrain | 103 |                 $page =  intval($start / $records_x_page);
 | 
        
           |  |  | 104 |                 $page++;
 | 
        
           |  |  | 105 |   | 
        
           | 7218 | eleazar | 106 |                 $order = $this->params()->fromQuery('order', []);
 | 
        
           |  |  | 107 |                 $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
 | 
        
           |  |  | 108 |                 $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |                 $fields = ['name'];
 | 
        
           |  |  | 111 |                 $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |                 if (!in_array($order_direction, ['ASC', 'DESC'])) {
 | 
        
           |  |  | 114 |                     $order_direction = 'ASC';
 | 
        
           |  |  | 115 |                 }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |                 $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 118 |                 $paginator = $organizationalClimateMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |                 $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |                 $items = [];
 | 
        
           |  |  | 123 |                 $records = $paginator->getCurrentItems();
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |                 foreach ($records as $record) {
 | 
        
           |  |  | 126 |                     $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($record->form_id);
 | 
        
           |  |  | 127 |                     $params = [
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 |                         'id' => $record->uuid,
 | 
        
           |  |  | 130 |                     ];
 | 
        
           |  |  | 131 |                     $item = [
 | 
        
           |  |  | 132 |                         'id' => $record->id,
 | 
        
           |  |  | 133 |                         'name' => $record->name,
 | 
        
           |  |  | 134 |                         'form' => $organizationalClimateForm->name,
 | 
        
           |  |  | 135 |                         'status' => $record->status,
 | 
        
           |  |  | 136 |                         'actions' => [
 | 
        
           | 12997 | efrain | 137 |                             'link_report_all' => $this->url()->fromRoute('organizational-climate/report/all', ['organizational_climate_id' => $record->uuid]),
 | 
        
           |  |  | 138 |                             'link_report_csv' => $this->url()->fromRoute('organizational-climate/report/csv', ['organizational_climate_id' => $record->uuid]),
 | 
        
           |  |  | 139 |                             'link_overview' => $this->url()->fromRoute('organizational-climate/report/overview', ['organizational_climate_id' => $record->uuid])
 | 
        
           | 7218 | eleazar | 140 |                         ]
 | 
        
           |  |  | 141 |                     ];
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |                     array_push($items, $item);
 | 
        
           |  |  | 144 |                 }
 | 
        
           |  |  | 145 |   | 
        
           |  |  | 146 |                 return new JsonModel([
 | 
        
           |  |  | 147 |                     'success' => true,
 | 
        
           |  |  | 148 |                     'data' => [
 | 
        
           |  |  | 149 |                         'items' => $items,
 | 
        
           |  |  | 150 |                         'total' => $paginator->getTotalItemCount(),
 | 
        
           |  |  | 151 |                     ]
 | 
        
           |  |  | 152 |                 ]);
 | 
        
           |  |  | 153 |             } else {
 | 
        
           |  |  | 154 |                 $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 155 |                 $survies = $organizationalClimateMapper->fetchAllByCompanyId($currentCompany->id);
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 |                 $form = new OrganizationalClimateTestForm($this->adapter, $currentCompany->id);
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |                 $this->layout()->setTemplate('layout/layout-backend');
 | 
        
           |  |  | 160 |                 $viewModel = new ViewModel();
 | 
        
           |  |  | 161 |                 $viewModel->setTemplate('leaders-linked/organizational-climate-report/index.phtml');
 | 
        
           |  |  | 162 |                 $viewModel->setVariables([
 | 
        
           |  |  | 163 |                     'form'      => $form,
 | 
        
           |  |  | 164 |                     'survies' => $survies
 | 
        
           |  |  | 165 |                 ]);
 | 
        
           |  |  | 166 |                 return $viewModel;
 | 
        
           |  |  | 167 |             }
 | 
        
           |  |  | 168 |         } else {
 | 
        
           |  |  | 169 |             return new JsonModel([
 | 
        
           |  |  | 170 |                 'success' => false,
 | 
        
           |  |  | 171 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 172 |             ]);
 | 
        
           |  |  | 173 |   | 
        
           |  |  | 174 |         }
 | 
        
           |  |  | 175 |         } catch (\Throwable $e) {
 | 
        
           |  |  | 176 |             $e->getMessage();
 | 
        
           |  |  | 177 |             return new JsonModel([
 | 
        
           |  |  | 178 |                 'success' => false,
 | 
        
           |  |  | 179 |                 'data' => $e
 | 
        
           |  |  | 180 |             ]);
 | 
        
           |  |  | 181 |         }
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |     }
 | 
        
           |  |  | 184 |   | 
        
           |  |  | 185 |     public function overviewAction(){
 | 
        
           |  |  | 186 |         $request = $this->getRequest();
 | 
        
           |  |  | 187 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 188 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 189 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |         $request = $this->getRequest();
 | 
        
           | 12997 | efrain | 192 |         $uuid = $this->params()->fromRoute('organizational_climate_id');
 | 
        
           | 7218 | eleazar | 193 |   | 
        
           |  |  | 194 |         if (!$uuid) {
 | 
        
           |  |  | 195 |             $data = [
 | 
        
           |  |  | 196 |                 'success' => false,
 | 
        
           |  |  | 197 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 198 |             ];
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 |             return new JsonModel($data);
 | 
        
           |  |  | 201 |         }
 | 
        
           |  |  | 202 |   | 
        
           |  |  | 203 |         $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 204 |         $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 |         $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 207 |         $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |         $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           | 7532 | eleazar | 210 |         $organizationalClimateTests = $organizationalClimateTestMapper->fetchAllBySurveyId($organizationalClimate->id);
 | 
        
           | 7218 | eleazar | 211 |   | 
        
           |  |  | 212 |         $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 213 |         $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
 | 
        
           |  |  | 214 |   | 
        
           |  |  | 215 |         $sections = json_decode($organizationalClimateForm->content, true);
 | 
        
           |  |  | 216 |   | 
        
           |  |  | 217 |         $allTests = array_map(
 | 
        
           |  |  | 218 |             function($response) {
 | 
        
           |  |  | 219 |                 return json_decode($response->content, true);
 | 
        
           |  |  | 220 |             },
 | 
        
           |  |  | 221 |             $organizationalClimateTests,
 | 
        
           |  |  | 222 |         );
 | 
        
           |  |  | 223 |   | 
        
           |  |  | 224 |         $averages = [];
 | 
        
           |  |  | 225 |   | 
        
           |  |  | 226 |         foreach($sections as $section) {
 | 
        
           |  |  | 227 |             foreach($section['questions'] as $question) {
 | 
        
           |  |  | 228 |                 switch ($question['type']) {
 | 
        
           |  |  | 229 |                     case 'multiple':
 | 
        
           |  |  | 230 |                         $totals = [];
 | 
        
           |  |  | 231 |   | 
        
           |  |  | 232 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 233 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 234 |                         }
 | 
        
           |  |  | 235 |   | 
        
           |  |  | 236 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 237 |                             $totals[$option['slug_option']] = count(array_filter(
 | 
        
           |  |  | 238 |                                 $allTests,
 | 
        
           |  |  | 239 |                                 function ($test) use($option, $question) {
 | 
        
           |  |  | 240 |                                     return in_array($option['slug_option'], $test[$question['slug_question']]);
 | 
        
           |  |  | 241 |                                 }
 | 
        
           |  |  | 242 |                             ));
 | 
        
           |  |  | 243 |                         }
 | 
        
           |  |  | 244 |   | 
        
           |  |  | 245 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 246 |   | 
        
           |  |  | 247 |                         break;
 | 
        
           |  |  | 248 |   | 
        
           |  |  | 249 |                     case 'simple':
 | 
        
           |  |  | 250 |                         $totals = [];
 | 
        
           |  |  | 251 |   | 
        
           |  |  | 252 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 253 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 254 |                         }
 | 
        
           |  |  | 255 |   | 
        
           |  |  | 256 |                         foreach ($allTests as $test) {
 | 
        
           |  |  | 257 |                             $totals[$test[$question['slug_question']]]++;
 | 
        
           |  |  | 258 |                         }
 | 
        
           |  |  | 259 |   | 
        
           |  |  | 260 |                         foreach($totals as $slug => $amount) {
 | 
        
           |  |  | 261 |                             $totals[$slug] = ($amount / count($allTests)) * 100;
 | 
        
           |  |  | 262 |                         }
 | 
        
           |  |  | 263 |   | 
        
           |  |  | 264 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 265 |                     break;
 | 
        
           |  |  | 266 |   | 
        
           |  |  | 267 |                 }
 | 
        
           |  |  | 268 |             }
 | 
        
           |  |  | 269 |         }
 | 
        
           |  |  | 270 |   | 
        
           |  |  | 271 |         $this->layout()->setTemplate('layout/layout-backend.phtml');
 | 
        
           |  |  | 272 |         $viewModel = new ViewModel();
 | 
        
           |  |  | 273 |         $viewModel->setTemplate('leaders-linked/organizational-climate-report/overview.phtml');
 | 
        
           |  |  | 274 |         $viewModel->setVariables([
 | 
        
           |  |  | 275 |             'sections' => $sections,
 | 
        
           |  |  | 276 |             'averages' => $averages,
 | 
        
           |  |  | 277 |         ]);
 | 
        
           |  |  | 278 |         return $viewModel ;
 | 
        
           |  |  | 279 |   | 
        
           |  |  | 280 |     }
 | 
        
           |  |  | 281 |   | 
        
           |  |  | 282 |     public function allAction() {
 | 
        
           |  |  | 283 |         $request = $this->getRequest();
 | 
        
           |  |  | 284 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 285 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 286 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 287 |   | 
        
           |  |  | 288 |         $request = $this->getRequest();
 | 
        
           | 12997 | efrain | 289 |         $uuid = $this->params()->fromRoute('organizational_climate_id');
 | 
        
           | 7218 | eleazar | 290 |   | 
        
           |  |  | 291 |   | 
        
           |  |  | 292 |   | 
        
           |  |  | 293 |         if (!$uuid) {
 | 
        
           |  |  | 294 |             $data = [
 | 
        
           |  |  | 295 |                 'success' => false,
 | 
        
           |  |  | 296 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 297 |             ];
 | 
        
           |  |  | 298 |   | 
        
           |  |  | 299 |             return new JsonModel($data);
 | 
        
           |  |  | 300 |         }
 | 
        
           |  |  | 301 |   | 
        
           |  |  | 302 |         $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 303 |         $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 304 |   | 
        
           |  |  | 305 |         $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           | 7528 | eleazar | 306 |         $organizationalClimateTests = $organizationalClimateTestMapper->fetchAllBySurveyId($organizationalClimate->id);
 | 
        
           | 7218 | eleazar | 307 |   | 
        
           |  |  | 308 |         if (!$organizationalClimateTests) {
 | 
        
           |  |  | 309 |             $data = [
 | 
        
           |  |  | 310 |                 'success' => false,
 | 
        
           |  |  | 311 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 312 |             ];
 | 
        
           |  |  | 313 |   | 
        
           |  |  | 314 |             return new JsonModel($data);
 | 
        
           |  |  | 315 |         }
 | 
        
           |  |  | 316 |   | 
        
           |  |  | 317 |   | 
        
           |  |  | 318 |         if ($request->isGet()) {
 | 
        
           |  |  | 319 |             $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 320 |   | 
        
           |  |  | 321 |             //get form data
 | 
        
           |  |  | 322 |             $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 323 |             $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
 | 
        
           |  |  | 324 |   | 
        
           |  |  | 325 |             if ($organizationalClimateForm) {
 | 
        
           |  |  | 326 |   | 
        
           |  |  | 327 |                 return $this->renderPDF($organizationalClimateForm, $organizationalClimateTests, $organizationalClimate);
 | 
        
           |  |  | 328 |             } else {
 | 
        
           |  |  | 329 |   | 
        
           |  |  | 330 |                 $data = [
 | 
        
           |  |  | 331 |                     'success' => false,
 | 
        
           |  |  | 332 |                     'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 333 |                 ];
 | 
        
           |  |  | 334 |   | 
        
           |  |  | 335 |                 return new JsonModel($data);
 | 
        
           |  |  | 336 |             }
 | 
        
           |  |  | 337 |         } else {
 | 
        
           |  |  | 338 |             $data = [
 | 
        
           |  |  | 339 |                 'success' => false,
 | 
        
           |  |  | 340 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 341 |             ];
 | 
        
           |  |  | 342 |   | 
        
           |  |  | 343 |             return new JsonModel($data);
 | 
        
           |  |  | 344 |         }
 | 
        
           |  |  | 345 |   | 
        
           |  |  | 346 |         return new JsonModel($data);
 | 
        
           |  |  | 347 |     }
 | 
        
           |  |  | 348 |   | 
        
           |  |  | 349 |   | 
        
           |  |  | 350 |     public function renderPDF($organizationalClimateForm, $organizationalClimateTests, $organizationalClimate) {
 | 
        
           |  |  | 351 |   | 
        
           |  |  | 352 |         $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $organizationalClimate->uuid;
 | 
        
           |  |  | 353 |   | 
        
           |  |  | 354 |   | 
        
           |  |  | 355 |         if(file_exists($target_path)) {
 | 
        
           |  |  | 356 |             Functions::deleteFiles($target_path);
 | 
        
           |  |  | 357 |         } else {
 | 
        
           |  |  | 358 |             @mkdir($target_path, 0755, true);
 | 
        
           |  |  | 359 |         }
 | 
        
           |  |  | 360 |   | 
        
           |  |  | 361 |         // Set Data
 | 
        
           |  |  | 362 |         $headerFormName = utf8_decode($organizationalClimateForm->name);
 | 
        
           | 7531 | eleazar | 363 |         $headerSurveyName = utf8_decode('Informe de Encuesta: ' . trim($organizationalClimate->name) . ' al ' . date("m-d-Y H:i:s", strtotime($organizationalClimate->added_on)));
 | 
        
           | 7218 | eleazar | 364 |         $sections = json_decode($organizationalClimateForm->content, true);
 | 
        
           |  |  | 365 |   | 
        
           |  |  | 366 |         $allTests = array_map(
 | 
        
           |  |  | 367 |             function($response) {
 | 
        
           |  |  | 368 |                 return json_decode($response->content, true);
 | 
        
           |  |  | 369 |             },
 | 
        
           |  |  | 370 |             $organizationalClimateTests,
 | 
        
           |  |  | 371 |         );
 | 
        
           |  |  | 372 |   | 
        
           |  |  | 373 |         $averages = [];
 | 
        
           |  |  | 374 |   | 
        
           |  |  | 375 |         foreach($sections as $section) {
 | 
        
           |  |  | 376 |             foreach($section['questions'] as $question) {
 | 
        
           |  |  | 377 |                 switch ($question['type']) {
 | 
        
           |  |  | 378 |                     case 'multiple':
 | 
        
           |  |  | 379 |                         $totals = [];
 | 
        
           |  |  | 380 |   | 
        
           |  |  | 381 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 382 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 383 |                         }
 | 
        
           |  |  | 384 |   | 
        
           |  |  | 385 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 386 |                             $totals[$option['slug_option']] = count(array_filter(
 | 
        
           |  |  | 387 |                                 $allTests,
 | 
        
           |  |  | 388 |                                 function ($test) use($option, $question) {
 | 
        
           |  |  | 389 |                                     return in_array($option['slug_option'], $test[$question['slug_question']]);
 | 
        
           |  |  | 390 |                                 }
 | 
        
           |  |  | 391 |                             ));
 | 
        
           |  |  | 392 |                         }
 | 
        
           |  |  | 393 |   | 
        
           |  |  | 394 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 395 |   | 
        
           |  |  | 396 |                         break;
 | 
        
           |  |  | 397 |   | 
        
           |  |  | 398 |                     case 'simple':
 | 
        
           |  |  | 399 |                         $totals = [];
 | 
        
           |  |  | 400 |   | 
        
           |  |  | 401 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 402 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 403 |                         }
 | 
        
           |  |  | 404 |   | 
        
           |  |  | 405 |                         foreach ($allTests as $test) {
 | 
        
           |  |  | 406 |                             $totals[$test[$question['slug_question']]]++;
 | 
        
           |  |  | 407 |                         }
 | 
        
           |  |  | 408 |   | 
        
           |  |  | 409 |                         foreach($totals as $slug => $amount) {
 | 
        
           |  |  | 410 |                             $totals[$slug] = ($amount / count($allTests)) * 100;
 | 
        
           |  |  | 411 |                         }
 | 
        
           |  |  | 412 |   | 
        
           |  |  | 413 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 414 |                     break;
 | 
        
           |  |  | 415 |   | 
        
           |  |  | 416 |                 }
 | 
        
           |  |  | 417 |             }
 | 
        
           |  |  | 418 |         }
 | 
        
           |  |  | 419 |   | 
        
           |  |  | 420 |         //Generate New PDF
 | 
        
           | 7529 | eleazar | 421 |         $pdf = new SurveyReport();
 | 
        
           | 7218 | eleazar | 422 |   | 
        
           |  |  | 423 |         $pdf->AliasNbPages();
 | 
        
           |  |  | 424 |         $pdf->AddPage();
 | 
        
           |  |  | 425 |   | 
        
           |  |  | 426 |         // Set header secundary
 | 
        
           | 7529 | eleazar | 427 |         $pdf->customHeader($headerFormName, $headerSurveyName);
 | 
        
           | 7218 | eleazar | 428 |   | 
        
           |  |  | 429 |         $countSection = 0;
 | 
        
           |  |  | 430 |   | 
        
           |  |  | 431 |         for ($i = 0; $i < count($sections); $i++) {
 | 
        
           |  |  | 432 |             if ($countSection > 1) {
 | 
        
           |  |  | 433 |                 $countSection = 0;
 | 
        
           |  |  | 434 |                 $pdf->AddPage();
 | 
        
           |  |  | 435 |                 $pdf->customHeader($headerFormName, $headerUserName);
 | 
        
           |  |  | 436 |             }
 | 
        
           |  |  | 437 |             $section = $sections[$i];
 | 
        
           |  |  | 438 |   | 
        
           |  |  | 439 |             foreach ($section['questions'] as $question) {
 | 
        
           |  |  | 440 |   | 
        
           |  |  | 441 |                     switch ($question['type']) {
 | 
        
           |  |  | 442 |                         case 'simple':
 | 
        
           |  |  | 443 |                             $labels = [];
 | 
        
           |  |  | 444 |                             $values = [];
 | 
        
           |  |  | 445 |   | 
        
           |  |  | 446 |                             foreach($question['options'] as $option) {
 | 
        
           |  |  | 447 |                                 $labels []= strip_tags($option['text']) . "\n(%.1f%%)";
 | 
        
           |  |  | 448 |   | 
        
           |  |  | 449 |                                 $values []= $averages[$question['slug_question']][$option['slug_option']];
 | 
        
           |  |  | 450 |                             }
 | 
        
           |  |  | 451 |   | 
        
           |  |  | 452 |                             $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
 | 
        
           |  |  | 453 |                             $pdf->Cell(0,10,strip_tags(trim(str_replace(' ', ' ', html_entity_decode($question['text'])))),0,1);
 | 
        
           |  |  | 454 |                             $pdf->pieChart($labels, $values, '', $filename);
 | 
        
           |  |  | 455 |                             $pdf->SetFont('Arial', '', 12);
 | 
        
           |  |  | 456 |                             $pdf->Image($filename, 45, $pdf->getY(), 120);
 | 
        
           |  |  | 457 |                             $pdf->setY($pdf->getY() + 90);
 | 
        
           |  |  | 458 |   | 
        
           |  |  | 459 |   | 
        
           |  |  | 460 |                             break;
 | 
        
           |  |  | 461 |   | 
        
           |  |  | 462 |                         case 'multiple':
 | 
        
           |  |  | 463 |                             $labels = [];
 | 
        
           |  |  | 464 |                             $values = [];
 | 
        
           |  |  | 465 |   | 
        
           |  |  | 466 |                             foreach($question['options'] as $option) {
 | 
        
           |  |  | 467 |                                 $labels []= strip_tags($option['text']);
 | 
        
           |  |  | 468 |   | 
        
           |  |  | 469 |                                 $values []= $averages[$question['slug_question']][$option['slug_option']];
 | 
        
           |  |  | 470 |                             }
 | 
        
           |  |  | 471 |   | 
        
           |  |  | 472 |                             $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
 | 
        
           |  |  | 473 |                             $pdf->Cell(0,10,strip_tags($question['text']),0,1);
 | 
        
           |  |  | 474 |                             $pdf->barChart($labels, $values, '', $filename);
 | 
        
           |  |  | 475 |                             $pdf->SetFont('Arial', '', 12);
 | 
        
           |  |  | 476 |                             $pdf->Image($filename, 12, $pdf->getY());
 | 
        
           |  |  | 477 |                             $pdf->setY($pdf->getY() + (count($values) * 13) + 10);
 | 
        
           |  |  | 478 |   | 
        
           |  |  | 479 |                             break;
 | 
        
           |  |  | 480 |                     }
 | 
        
           |  |  | 481 |                 $countSection++;
 | 
        
           |  |  | 482 |             }
 | 
        
           |  |  | 483 |   | 
        
           |  |  | 484 |   | 
        
           |  |  | 485 |         }
 | 
        
           |  |  | 486 |   | 
        
           |  |  | 487 |         return $pdf->Output();
 | 
        
           |  |  | 488 |     }
 | 
        
           |  |  | 489 |   | 
        
           |  |  | 490 |     public function csvAction() {
 | 
        
           |  |  | 491 |         $request = $this->getRequest();
 | 
        
           |  |  | 492 |         $currentUserPlugin = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 493 |         $currentCompany = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 494 |         $currentUser = $currentUserPlugin->getUser();
 | 
        
           |  |  | 495 |   | 
        
           |  |  | 496 |         $request = $this->getRequest();
 | 
        
           | 12997 | efrain | 497 |         $uuid = $this->params()->fromRoute('organizational_climate_id');
 | 
        
           | 7218 | eleazar | 498 |   | 
        
           |  |  | 499 |   | 
        
           |  |  | 500 |   | 
        
           |  |  | 501 |         if (!$uuid) {
 | 
        
           |  |  | 502 |             $data = [
 | 
        
           |  |  | 503 |                 'success' => false,
 | 
        
           |  |  | 504 |                 'data' => 'ERROR_INVALID_PARAMETER'
 | 
        
           |  |  | 505 |             ];
 | 
        
           |  |  | 506 |   | 
        
           |  |  | 507 |             return new JsonModel($data);
 | 
        
           |  |  | 508 |         }
 | 
        
           |  |  | 509 |   | 
        
           |  |  | 510 |         $organizationalClimateMapper = OrganizationalClimateMapper::getInstance($this->adapter);
 | 
        
           |  |  | 511 |         $organizationalClimate = $organizationalClimateMapper->fetchOneByUuid($uuid);
 | 
        
           |  |  | 512 |   | 
        
           |  |  | 513 |         $organizationalClimateTestMapper = OrganizationalClimateTestMapper::getInstance($this->adapter);
 | 
        
           | 7529 | eleazar | 514 |         $organizationalClimateTests = $organizationalClimateTestMapper->fetchAllBySurveyId($organizationalClimate->id);
 | 
        
           | 7218 | eleazar | 515 |   | 
        
           |  |  | 516 |         if (!$organizationalClimateTests) {
 | 
        
           |  |  | 517 |             $data = [
 | 
        
           |  |  | 518 |                 'success' => false,
 | 
        
           |  |  | 519 |                 'data' => 'ERROR_RECORD_NOT_FOUND'
 | 
        
           |  |  | 520 |             ];
 | 
        
           |  |  | 521 |   | 
        
           |  |  | 522 |             return new JsonModel($data);
 | 
        
           |  |  | 523 |         }
 | 
        
           |  |  | 524 |   | 
        
           |  |  | 525 |   | 
        
           |  |  | 526 |         if ($request->isGet()) {
 | 
        
           |  |  | 527 |             $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 528 |   | 
        
           |  |  | 529 |             //get form data
 | 
        
           |  |  | 530 |             $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
 | 
        
           |  |  | 531 |             $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
 | 
        
           |  |  | 532 |   | 
        
           |  |  | 533 |             if ($organizationalClimateForm) {
 | 
        
           |  |  | 534 |   | 
        
           |  |  | 535 |                 return $this->csvRender($organizationalClimateForm, $organizationalClimateTests, $organizationalClimate);
 | 
        
           |  |  | 536 |             } else {
 | 
        
           |  |  | 537 |   | 
        
           |  |  | 538 |                 $data = [
 | 
        
           |  |  | 539 |                     'success' => false,
 | 
        
           |  |  | 540 |                     'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 541 |                 ];
 | 
        
           |  |  | 542 |   | 
        
           |  |  | 543 |                 return new JsonModel($data);
 | 
        
           |  |  | 544 |             }
 | 
        
           |  |  | 545 |         } else {
 | 
        
           |  |  | 546 |             $data = [
 | 
        
           |  |  | 547 |                 'success' => false,
 | 
        
           |  |  | 548 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 549 |             ];
 | 
        
           |  |  | 550 |   | 
        
           |  |  | 551 |             return new JsonModel($data);
 | 
        
           |  |  | 552 |         }
 | 
        
           |  |  | 553 |   | 
        
           |  |  | 554 |         return new JsonModel($data);
 | 
        
           |  |  | 555 |     }
 | 
        
           |  |  | 556 |   | 
        
           |  |  | 557 |   | 
        
           |  |  | 558 |     public function csvRender($organizationalClimateForm, $organizationalClimateTests, $organizationalClimate)
 | 
        
           |  |  | 559 |     {
 | 
        
           |  |  | 560 |         $sections = json_decode($organizationalClimateForm->content, true);
 | 
        
           |  |  | 561 |   | 
        
           |  |  | 562 |         $spreadsheet = new Spreadsheet();
 | 
        
           |  |  | 563 |         $sheet = $spreadsheet->getActiveSheet();
 | 
        
           |  |  | 564 |   | 
        
           |  |  | 565 |         $allTests = array_map(
 | 
        
           |  |  | 566 |             function($response) {
 | 
        
           |  |  | 567 |                 return json_decode($response->content, true);
 | 
        
           |  |  | 568 |             },
 | 
        
           |  |  | 569 |             $organizationalClimateTests,
 | 
        
           |  |  | 570 |         );
 | 
        
           |  |  | 571 |   | 
        
           |  |  | 572 |         $averages = [];
 | 
        
           |  |  | 573 |   | 
        
           |  |  | 574 |         foreach($sections as $section) {
 | 
        
           |  |  | 575 |             foreach($section['questions'] as $question) {
 | 
        
           |  |  | 576 |                 switch ($question['type']) {
 | 
        
           |  |  | 577 |                     case 'multiple':
 | 
        
           |  |  | 578 |                         $totals = [];
 | 
        
           |  |  | 579 |   | 
        
           |  |  | 580 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 581 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 582 |                         }
 | 
        
           |  |  | 583 |   | 
        
           |  |  | 584 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 585 |                             $totals[$option['slug_option']] = count(array_filter(
 | 
        
           |  |  | 586 |                                 $allTests,
 | 
        
           |  |  | 587 |                                 function ($test) use($option, $question) {
 | 
        
           |  |  | 588 |                                     return in_array($option['slug_option'], $test[$question['slug_question']]);
 | 
        
           |  |  | 589 |                                 }
 | 
        
           |  |  | 590 |                             ));
 | 
        
           |  |  | 591 |                         }
 | 
        
           |  |  | 592 |   | 
        
           |  |  | 593 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 594 |   | 
        
           |  |  | 595 |                         break;
 | 
        
           |  |  | 596 |   | 
        
           |  |  | 597 |                     case 'simple':
 | 
        
           |  |  | 598 |                         $totals = [];
 | 
        
           |  |  | 599 |   | 
        
           |  |  | 600 |                         foreach($question['options'] as $option) {
 | 
        
           |  |  | 601 |                             $totals[$option['slug_option']] = 0;
 | 
        
           |  |  | 602 |                         }
 | 
        
           |  |  | 603 |   | 
        
           |  |  | 604 |                         foreach ($allTests as $test) {
 | 
        
           |  |  | 605 |                             $totals[$test[$question['slug_question']]]++;
 | 
        
           |  |  | 606 |                         }
 | 
        
           |  |  | 607 |   | 
        
           |  |  | 608 |                         foreach($totals as $slug => $amount) {
 | 
        
           |  |  | 609 |                             $totals[$slug] = ($amount / count($allTests)) * 100;
 | 
        
           |  |  | 610 |                         }
 | 
        
           |  |  | 611 |   | 
        
           |  |  | 612 |                         $averages[$question['slug_question']] = $totals;
 | 
        
           |  |  | 613 |                     break;
 | 
        
           |  |  | 614 |   | 
        
           |  |  | 615 |                 }
 | 
        
           |  |  | 616 |             }
 | 
        
           |  |  | 617 |         }
 | 
        
           |  |  | 618 |   | 
        
           |  |  | 619 |         $row = 1;
 | 
        
           |  |  | 620 |   | 
        
           |  |  | 621 |         for ($i = 0; $i < count($sections); $i++) {
 | 
        
           |  |  | 622 |             $section = $sections[$i];
 | 
        
           |  |  | 623 |   | 
        
           |  |  | 624 |            for($j = 0; $j < count($section['questions']); $j++) {
 | 
        
           |  |  | 625 |                 $question = $section['questions'][$j];
 | 
        
           |  |  | 626 |   | 
        
           |  |  | 627 |                 if (!in_array($question['type'], ['simple', 'multiple'])){
 | 
        
           |  |  | 628 |                     continue;
 | 
        
           |  |  | 629 |                 }
 | 
        
           |  |  | 630 |                 $sheet->setCellValueByColumnAndRow(1, $row++, strip_tags($question['text']));
 | 
        
           |  |  | 631 |   | 
        
           |  |  | 632 |                 switch ($question['type']) {
 | 
        
           |  |  | 633 |                     case 'simple':
 | 
        
           |  |  | 634 |                         for($k = 0; $k < count($question['options']); $k++) {
 | 
        
           |  |  | 635 |                             $option = $question['options'][$k];
 | 
        
           |  |  | 636 |                             $sheet->setCellValueByColumnAndRow(1, $row, strip_tags($option['text']));
 | 
        
           |  |  | 637 |                             $sheet->setCellValueByColumnAndRow(2, $row, round($averages[$question['slug_question']][$option['slug_option']], 2) . '%');
 | 
        
           |  |  | 638 |                             $row++;
 | 
        
           |  |  | 639 |                         }
 | 
        
           |  |  | 640 |   | 
        
           |  |  | 641 |                         break;
 | 
        
           |  |  | 642 |   | 
        
           |  |  | 643 |                     case 'multiple':
 | 
        
           |  |  | 644 |                         for($k = 0; $k < count($question['options']); $k++) {
 | 
        
           |  |  | 645 |                             $option = $question['options'][$k];
 | 
        
           |  |  | 646 |                             $sheet->setCellValueByColumnAndRow(1, $row, strip_tags($option['text']));
 | 
        
           |  |  | 647 |                             $sheet->setCellValueByColumnAndRow(2, $row,$averages[$question['slug_question']][$option['slug_option']]);
 | 
        
           |  |  | 648 |                             $row++;
 | 
        
           |  |  | 649 |                         }
 | 
        
           |  |  | 650 |   | 
        
           |  |  | 651 |                         break;
 | 
        
           |  |  | 652 |                 }
 | 
        
           |  |  | 653 |             }
 | 
        
           |  |  | 654 |   | 
        
           |  |  | 655 |   | 
        
           |  |  | 656 |         }
 | 
        
           |  |  | 657 |   | 
        
           |  |  | 658 |         header('Content-Description: File Transfer');
 | 
        
           |  |  | 659 |         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 | 
        
           |  |  | 660 |         header('Content-Disposition: attachment; filename=report.xls');
 | 
        
           |  |  | 661 |         header('Content-Transfer-Encoding: binary');
 | 
        
           |  |  | 662 |         header('Expires: 0');
 | 
        
           |  |  | 663 |         header('Cache-Control: must-revalidate');
 | 
        
           |  |  | 664 |         header('Pragma: public');
 | 
        
           |  |  | 665 |   | 
        
           |  |  | 666 |         $writer = new Xlsx($spreadsheet);
 | 
        
           |  |  | 667 |   | 
        
           |  |  | 668 |         ob_clean();
 | 
        
           |  |  | 669 |         flush();
 | 
        
           |  |  | 670 |         $writer->save('php://output');
 | 
        
           |  |  | 671 |         return;
 | 
        
           |  |  | 672 |     }
 | 
        
           |  |  | 673 | }
 |