| 134 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | /**
 | 
        
           |  |  | 3 |  *
 | 
        
           |  |  | 4 |  * Controlador: Mis Perfiles
 | 
        
           |  |  | 5 |  *
 | 
        
           |  |  | 6 |  */
 | 
        
           |  |  | 7 | declare(strict_types=1);
 | 
        
           |  |  | 8 |   | 
        
           |  |  | 9 | namespace LeadersLinked\Controller;
 | 
        
           |  |  | 10 |   | 
        
           |  |  | 11 | use Laminas\Db\Adapter\AdapterInterface;
 | 
        
           | 16766 | efrain | 12 | use LeadersLinked\Cache\CacheInterface;
 | 
        
           | 134 | efrain | 13 | use Laminas\Mvc\Controller\AbstractActionController;
 | 
        
           |  |  | 14 | use Laminas\Log\LoggerInterface;
 | 
        
           |  |  | 15 | use Laminas\View\Model\ViewModel;
 | 
        
           |  |  | 16 | use Laminas\View\Model\JsonModel;
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | use LeadersLinked\Library\Functions;
 | 
        
           |  |  | 19 | use LeadersLinked\Mapper\QueryMapper;
 | 
        
           |  |  | 20 | use LeadersLinked\Mapper\CompanyMapper;
 | 
        
           |  |  | 21 | use LeadersLinked\Mapper\CompanyMicrolearningQuizMapper;
 | 
        
           |  |  | 22 | use LeadersLinked\Form\AnswerCreateOrEditForm;
 | 
        
           |  |  | 23 | use Laminas\Hydrator\ObjectPropertyHydrator;
 | 
        
           |  |  | 24 | use LeadersLinked\Model\CompanyMicrolearningQuestion;
 | 
        
           |  |  | 25 | use LeadersLinked\Mapper\CompanyMicrolearningQuestionMapper;
 | 
        
           |  |  | 26 | use LeadersLinked\Model\CompanyMicrolearningAnswer;
 | 
        
           |  |  | 27 | use LeadersLinked\Mapper\CompanyMicrolearningAnswerMapper;
 | 
        
           |  |  | 28 | use LeadersLinked\Model\CompanyMicrolearningQuiz;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | class MicrolearningAnswerController extends AbstractActionController
 | 
        
           |  |  | 31 | {
 | 
        
           |  |  | 32 |     /**
 | 
        
           |  |  | 33 |      *
 | 
        
           |  |  | 34 |      * @var AdapterInterface
 | 
        
           |  |  | 35 |      */
 | 
        
           |  |  | 36 |     private $adapter;
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      *
 | 
        
           | 16766 | efrain | 41 |      * @var CacheInterface
 | 
        
           | 134 | efrain | 42 |      */
 | 
        
           |  |  | 43 |     private $cache;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /**
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @var  LoggerInterface
 | 
        
           |  |  | 48 |      */
 | 
        
           |  |  | 49 |     private $logger;
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     /**
 | 
        
           |  |  | 53 |      *
 | 
        
           |  |  | 54 |      * @var array
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     private $config;
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     /**
 | 
        
           |  |  | 59 |      *
 | 
        
           |  |  | 60 |      * @param AdapterInterface $adapter
 | 
        
           | 16766 | efrain | 61 |      * @param CacheInterface $cache
 | 
        
           | 134 | efrain | 62 |      * @param LoggerInterface $logger
 | 
        
           |  |  | 63 |      * @param array $config
 | 
        
           |  |  | 64 |      */
 | 
        
           |  |  | 65 |     public function __construct($adapter, $cache , $logger,  $config)
 | 
        
           |  |  | 66 |     {
 | 
        
           |  |  | 67 |         $this->adapter      = $adapter;
 | 
        
           |  |  | 68 |         $this->cache        = $cache;
 | 
        
           |  |  | 69 |         $this->logger       = $logger;
 | 
        
           |  |  | 70 |         $this->config       = $config;
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |     }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |     public function indexAction()
 | 
        
           |  |  | 75 |     {
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         $request            = $this->getRequest();
 | 
        
           |  |  | 78 |         $currentUserPlugin  = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 79 |         $currentUser        = $currentUserPlugin->getUser();
 | 
        
           |  |  | 80 |         $currentCompany     = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 81 |         $quiz_id            = $this->params()->fromRoute('quiz_id');
 | 
        
           |  |  | 82 |         $question_id        = $this->params()->fromRoute('question_id');
 | 
        
           |  |  | 83 |         $flashMessenger     = $this->plugin('FlashMessenger');
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
 | 
        
           |  |  | 87 |         $quiz = $quizMapper->fetchOneByUuid($quiz_id);
 | 
        
           |  |  | 88 |         if(!$quiz) {
 | 
        
           |  |  | 89 |             $flashMessenger->addErrorMessage('ERROR_FORM_NOT_FOUND');
 | 
        
           |  |  | 90 |             return $this->redirect()->toRoute('dashboard');
 | 
        
           |  |  | 91 |         }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |         if($quiz->company_id != $currentCompany->id) {
 | 
        
           |  |  | 94 |             $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
 | 
        
           |  |  | 95 |             return $this->redirect()->toRoute('dashboard');
 | 
        
           |  |  | 96 |         }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
 | 
        
           |  |  | 100 |         $question = $questionMapper->fetchOneByUuid($question_id);
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |         if(!$question) {
 | 
        
           |  |  | 103 |             $flashMessenger->addErrorMessage('ERROR_QUESTION_NOT_FOUND');
 | 
        
           |  |  | 104 |             return $this->redirect()->toRoute('dashboard');
 | 
        
           |  |  | 105 |         }
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         if($question->quiz_id != $quiz->id) {
 | 
        
           |  |  | 108 |             $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
 | 
        
           |  |  | 109 |             return $this->redirect()->toRoute('dashboard');
 | 
        
           |  |  | 110 |         }
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |         $request = $this->getRequest();
 | 
        
           |  |  | 113 |         if($request->isGet()) {
 | 
        
           |  |  | 114 |             $headers  = $request->getHeaders();
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |             $isJson = false;
 | 
        
           |  |  | 117 |             if($headers->has('Accept')) {
 | 
        
           |  |  | 118 |                 $accept = $headers->get('Accept');
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |                 $prioritized = $accept->getPrioritized();
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |                 foreach($prioritized as $key => $value) {
 | 
        
           |  |  | 123 |                     $raw = trim($value->getRaw());
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |                     if(!$isJson) {
 | 
        
           |  |  | 126 |                         $isJson = strpos($raw, 'json');
 | 
        
           |  |  | 127 |                     }
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 |                 }
 | 
        
           |  |  | 130 |             }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |             if($isJson) {
 | 
        
           |  |  | 133 |                 $acl = $this->getEvent()->getViewModel()->getVariable('acl');
 | 
        
           |  |  | 134 |                 $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/answers/add');
 | 
        
           |  |  | 135 |                 $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/answers/edit');
 | 
        
           |  |  | 136 |                 $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/answers/delete');
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |                 $search = $this->params()->fromQuery('search', []);
 | 
        
           | 16766 | efrain | 140 |                 $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
 | 
        
           | 134 | efrain | 141 |   | 
        
           |  |  | 142 |                 $page               = intval($this->params()->fromQuery('start', 1), 10);
 | 
        
           |  |  | 143 |                 $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
 | 
        
           |  |  | 144 |                 $order =  $this->params()->fromQuery('order', []);
 | 
        
           |  |  | 145 |                 $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
 | 
        
           | 16766 | efrain | 146 |                 $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
 | 
        
           | 134 | efrain | 147 |   | 
        
           |  |  | 148 |                 $fields =  ['text'];
 | 
        
           |  |  | 149 |                 $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'text';
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |                 if(!in_array($order_direction, ['ASC', 'DESC'])) {
 | 
        
           |  |  | 152 |                     $order_direction = 'ASC';
 | 
        
           |  |  | 153 |                 }
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 |                 $companyMicrolearningAnswerMapper = CompanyMicrolearningAnswerMapper::getInstance($this->adapter);
 | 
        
           |  |  | 158 |                 $paginator = $companyMicrolearningAnswerMapper->fetchAllDataTableByCompanyIdAndQuizId($question->company_id, $question->quiz_id, $question->id, $search, $page, $records_x_page, $order_field, $order_direction);
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |                 $items = [];
 | 
        
           |  |  | 161 |                 foreach($paginator as $record)
 | 
        
           |  |  | 162 |                 {
 | 
        
           |  |  | 163 |                     $params = [
 | 
        
           |  |  | 164 |                         'quiz_id' => $quiz->uuid,
 | 
        
           |  |  | 165 |                         'question_id' => $question->uuid,
 | 
        
           |  |  | 166 |                         'answer_id' => $record->uuid,
 | 
        
           |  |  | 167 |                     ];
 | 
        
           |  |  | 168 |   | 
        
           |  |  | 169 |                     $item = [
 | 
        
           |  |  | 170 |                         'text' => substr(strip_tags($record->text), 0, 50),
 | 
        
           |  |  | 171 |                         'details' => [
 | 
        
           |  |  | 172 |                             'correct' => '',
 | 
        
           |  |  | 173 |                             'points' => $record->points > 0 ? $record->points : 0,
 | 
        
           |  |  | 174 |                         ],
 | 
        
           |  |  | 175 |                         'actions' => [
 | 
        
           |  |  | 176 |                             'link_edit' => $this->url()->fromRoute('microlearning/content/quizzes/questions/answers/edit', $params),
 | 
        
           |  |  | 177 |                             'link_delete' => $this->url()->fromRoute('microlearning/content/quizzes/questions/answers/delete', $params),
 | 
        
           |  |  | 178 |                         ],
 | 
        
           |  |  | 179 |                     ];
 | 
        
           |  |  | 180 |   | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |                     if($record->correct == CompanyMicrolearningAnswer::CORRECT_YES) {
 | 
        
           |  |  | 184 |                         $item['details']['correct'] = 'LABEL_ANSWER_CORRECT';
 | 
        
           |  |  | 185 |                     } else {
 | 
        
           |  |  | 186 |                         $item['details']['correct'] = 'LABEL_ANSWER_INCORRECT';
 | 
        
           |  |  | 187 |                     }
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |                     array_push($items, $item);
 | 
        
           |  |  | 191 |                 }
 | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |                 $data = [];
 | 
        
           |  |  | 196 |                 $data['items'] = $items;
 | 
        
           |  |  | 197 |                 $data['total'] = $paginator->getTotalItemCount();
 | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 |                 if($allowAdd) {
 | 
        
           |  |  | 200 |                     $data['actions']['link_add'] = $this->url()->fromRoute('microlearning/content/quizzes/questions/answers/add', ['quiz_id' => $question->quiz_id, 'question_id' => $question->id]);
 | 
        
           |  |  | 201 |                 }  else {
 | 
        
           |  |  | 202 |                     $data['actions']['link_add'] = '';
 | 
        
           |  |  | 203 |                 }
 | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 |                 $response = [
 | 
        
           |  |  | 206 |                     'success' => true,
 | 
        
           |  |  | 207 |                     'data' => $data
 | 
        
           |  |  | 208 |                 ];
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 |                 return new JsonModel($response);
 | 
        
           |  |  | 211 |   | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 |             } else {
 | 
        
           |  |  | 214 |   | 
        
           |  |  | 215 |                 if($question->type == CompanyMicrolearningQuestion::TYPE_SINGLE_SELECTION) {
 | 
        
           |  |  | 216 |                     $showPoints = 0;
 | 
        
           |  |  | 217 |                 } else {
 | 
        
           |  |  | 218 |                     $showPoints = 1;
 | 
        
           |  |  | 219 |                 }
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 |                 $quizCreateOrEdit = new AnswerCreateOrEditForm();
 | 
        
           |  |  | 224 |   | 
        
           |  |  | 225 |                 $this->layout()->setTemplate('layout/layout-backend.phtml');
 | 
        
           |  |  | 226 |                 $viewModel = new ViewModel();
 | 
        
           |  |  | 227 |                 $viewModel->setTemplate('leaders-linked/microlearning-answers/index.phtml');
 | 
        
           |  |  | 228 |                 $viewModel->setVariables([
 | 
        
           |  |  | 229 |                     'formCreateOrEdit' => $quizCreateOrEdit,
 | 
        
           |  |  | 230 |                     'quiz_uuid' => $quiz->uuid,
 | 
        
           |  |  | 231 |                     'quiz_name'  => $quiz->name,
 | 
        
           |  |  | 232 |                     'quiz_status' => $quiz->status,
 | 
        
           |  |  | 233 |                     'question_uuid' => $question->uuid,
 | 
        
           |  |  | 234 |                     'question_name' => strip_tags($question->text),
 | 
        
           |  |  | 235 |                     'show_points' => $showPoints
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 |                 ]);
 | 
        
           |  |  | 238 |                 return $viewModel ;
 | 
        
           |  |  | 239 |             }
 | 
        
           |  |  | 240 |   | 
        
           |  |  | 241 |         } else {
 | 
        
           |  |  | 242 |             return new JsonModel([
 | 
        
           |  |  | 243 |                 'success' => false,
 | 
        
           |  |  | 244 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 245 |             ]);
 | 
        
           |  |  | 246 |         }
 | 
        
           |  |  | 247 |     }
 | 
        
           |  |  | 248 |   | 
        
           |  |  | 249 |     public function addAction()
 | 
        
           |  |  | 250 |     {
 | 
        
           |  |  | 251 |         $request            = $this->getRequest();
 | 
        
           |  |  | 252 |         $currentUserPlugin  = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 253 |         $currentUser        = $currentUserPlugin->getUser();
 | 
        
           |  |  | 254 |         $currentCompany     = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 255 |         $quiz_id            = $this->params()->fromRoute('quiz_id');
 | 
        
           |  |  | 256 |         $question_id        = $this->params()->fromRoute('question_id');
 | 
        
           |  |  | 257 |   | 
        
           |  |  | 258 |   | 
        
           |  |  | 259 |         $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
 | 
        
           |  |  | 260 |         $quiz = $quizMapper->fetchOneByUuid($quiz_id);
 | 
        
           |  |  | 261 |         if(!$quiz) {
 | 
        
           |  |  | 262 |             return new JsonModel([
 | 
        
           |  |  | 263 |                 'success'   => false,
 | 
        
           |  |  | 264 |                 'data'   => 'ERROR_FORM_NOT_FOUND'
 | 
        
           |  |  | 265 |             ]);
 | 
        
           |  |  | 266 |         }
 | 
        
           |  |  | 267 |   | 
        
           |  |  | 268 |         if($quiz->company_id != $currentCompany->id) {
 | 
        
           |  |  | 269 |             return new JsonModel([
 | 
        
           |  |  | 270 |                 'success'   => false,
 | 
        
           |  |  | 271 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 272 |             ]);
 | 
        
           |  |  | 273 |         }
 | 
        
           |  |  | 274 |   | 
        
           |  |  | 275 |         $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
 | 
        
           |  |  | 276 |         $question = $questionMapper->fetchOneByUuid($question_id);
 | 
        
           |  |  | 277 |   | 
        
           |  |  | 278 |         if(!$question) {
 | 
        
           |  |  | 279 |             return new JsonModel([
 | 
        
           |  |  | 280 |                 'success'   => false,
 | 
        
           |  |  | 281 |                 'data'   => 'ERROR_QUESTION_NOT_FOUND'
 | 
        
           |  |  | 282 |             ]);
 | 
        
           |  |  | 283 |         }
 | 
        
           |  |  | 284 |   | 
        
           |  |  | 285 |         if($question->quiz_id != $quiz->id) {
 | 
        
           |  |  | 286 |             return new JsonModel([
 | 
        
           |  |  | 287 |                 'success'   => false,
 | 
        
           |  |  | 288 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 289 |             ]);
 | 
        
           |  |  | 290 |         }
 | 
        
           |  |  | 291 |   | 
        
           |  |  | 292 |         if($request->isPost()) {
 | 
        
           |  |  | 293 |             $quizCreateOrEdit = new  AnswerCreateOrEditForm();
 | 
        
           |  |  | 294 |             $quizCreateOrEdit->setData($request->getPost()->toArray());
 | 
        
           |  |  | 295 |   | 
        
           |  |  | 296 |             if($quizCreateOrEdit->isValid()) {
 | 
        
           |  |  | 297 |                 $dataPost = (array) $quizCreateOrEdit->getData();
 | 
        
           |  |  | 298 |   | 
        
           |  |  | 299 |                 $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 300 |                 $answer = new CompanyMicrolearningAnswer();
 | 
        
           |  |  | 301 |                 $hydrator->hydrate($dataPost, $answer);
 | 
        
           |  |  | 302 |   | 
        
           |  |  | 303 |                 $answer->company_id = $question->company_id;
 | 
        
           |  |  | 304 |                 $answer->quiz_id = $question->quiz_id;
 | 
        
           |  |  | 305 |                 $answer->question_id = $question->id;
 | 
        
           |  |  | 306 |   | 
        
           |  |  | 307 |                 $answerMapper = CompanyMicrolearningAnswerMapper::getInstance($this->adapter);
 | 
        
           |  |  | 308 |                 if($answerMapper->insert($answer)) {
 | 
        
           |  |  | 309 |   | 
        
           |  |  | 310 |                     $question->check = CompanyMicrolearningQuestion::CHECK_PENDING;
 | 
        
           |  |  | 311 |                     $questionMapper->update($question);
 | 
        
           |  |  | 312 |   | 
        
           |  |  | 313 |                     $quiz->check = CompanyMicrolearningQuiz::CHECK_PENDING;
 | 
        
           |  |  | 314 |                     $quizMapper->update($quiz);
 | 
        
           |  |  | 315 |   | 
        
           |  |  | 316 |                     $this->logger->info('Se agrego la respuesta ' . substr(strip_tags($answer->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 317 |   | 
        
           |  |  | 318 |                     $data = [
 | 
        
           |  |  | 319 |                         'success'   => true,
 | 
        
           |  |  | 320 |                         'data'   => 'LABEL_RECORD_ADDED'
 | 
        
           |  |  | 321 |                     ];
 | 
        
           |  |  | 322 |                 } else {
 | 
        
           |  |  | 323 |                     $data = [
 | 
        
           |  |  | 324 |                         'success'   => false,
 | 
        
           |  |  | 325 |                         'data'      => $answerMapper->getError()
 | 
        
           |  |  | 326 |                     ];
 | 
        
           |  |  | 327 |   | 
        
           |  |  | 328 |                 }
 | 
        
           |  |  | 329 |   | 
        
           |  |  | 330 |                 return new JsonModel($data);
 | 
        
           |  |  | 331 |   | 
        
           |  |  | 332 |             } else {
 | 
        
           |  |  | 333 |                 $messages = [];
 | 
        
           |  |  | 334 |                 $quiz_messages = (array) $quizCreateOrEdit->getMessages();
 | 
        
           |  |  | 335 |                 foreach($quiz_messages  as $fieldname => $field_messages)
 | 
        
           |  |  | 336 |                 {
 | 
        
           |  |  | 337 |   | 
        
           |  |  | 338 |                     $messages[$fieldname] = array_values($field_messages);
 | 
        
           |  |  | 339 |                 }
 | 
        
           |  |  | 340 |   | 
        
           |  |  | 341 |                 return new JsonModel([
 | 
        
           |  |  | 342 |                     'success'   => false,
 | 
        
           |  |  | 343 |                     'data'   => $messages
 | 
        
           |  |  | 344 |                 ]);
 | 
        
           |  |  | 345 |             }
 | 
        
           |  |  | 346 |   | 
        
           |  |  | 347 |         } else {
 | 
        
           |  |  | 348 |             $data = [
 | 
        
           |  |  | 349 |                 'success' => false,
 | 
        
           |  |  | 350 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 351 |             ];
 | 
        
           |  |  | 352 |   | 
        
           |  |  | 353 |             return new JsonModel($data);
 | 
        
           |  |  | 354 |         }
 | 
        
           |  |  | 355 |   | 
        
           |  |  | 356 |         return new JsonModel($data);
 | 
        
           |  |  | 357 |     }
 | 
        
           |  |  | 358 |   | 
        
           |  |  | 359 |     /**
 | 
        
           |  |  | 360 |      *
 | 
        
           |  |  | 361 |      * Borrar un perfil excepto el público
 | 
        
           |  |  | 362 |      * @return \Laminas\View\Model\JsonModel
 | 
        
           |  |  | 363 |      */
 | 
        
           |  |  | 364 |     public function deleteAction()
 | 
        
           |  |  | 365 |     {
 | 
        
           |  |  | 366 |         $request            = $this->getRequest();
 | 
        
           |  |  | 367 |         $currentUserPlugin  = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 368 |         $currentUser        = $currentUserPlugin->getUser();
 | 
        
           |  |  | 369 |         $currentCompany     = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 370 |         $quiz_id            = $this->params()->fromRoute('quiz_id');
 | 
        
           |  |  | 371 |         $question_id        = $this->params()->fromRoute('question_id');
 | 
        
           |  |  | 372 |         $answer_id          = $this->params()->fromRoute('answer_id');
 | 
        
           |  |  | 373 |   | 
        
           |  |  | 374 |   | 
        
           |  |  | 375 |         $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
 | 
        
           |  |  | 376 |         $quiz = $quizMapper->fetchOneByUuid($quiz_id);
 | 
        
           |  |  | 377 |         if(!$quiz) {
 | 
        
           |  |  | 378 |             return new JsonModel([
 | 
        
           |  |  | 379 |                 'success'   => false,
 | 
        
           |  |  | 380 |                 'data'   => 'ERROR_FORM_NOT_FOUND'
 | 
        
           |  |  | 381 |             ]);
 | 
        
           |  |  | 382 |         }
 | 
        
           |  |  | 383 |   | 
        
           |  |  | 384 |         if($quiz->company_id != $currentCompany->id) {
 | 
        
           |  |  | 385 |             return new JsonModel([
 | 
        
           |  |  | 386 |                 'success'   => false,
 | 
        
           |  |  | 387 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 388 |             ]);
 | 
        
           |  |  | 389 |         }
 | 
        
           |  |  | 390 |   | 
        
           |  |  | 391 |         $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
 | 
        
           |  |  | 392 |         $question = $questionMapper->fetchOneByUuid($question_id);
 | 
        
           |  |  | 393 |         if(!$question) {
 | 
        
           |  |  | 394 |             return new JsonModel([
 | 
        
           |  |  | 395 |                 'success'   => false,
 | 
        
           |  |  | 396 |                 'data'   => 'ERROR_QUESTION_NOT_FOUND'
 | 
        
           |  |  | 397 |             ]);
 | 
        
           |  |  | 398 |         }
 | 
        
           |  |  | 399 |   | 
        
           |  |  | 400 |         if($question->quiz_id != $quiz->id) {
 | 
        
           |  |  | 401 |             return new JsonModel([
 | 
        
           |  |  | 402 |                 'success'   => false,
 | 
        
           |  |  | 403 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 404 |             ]);
 | 
        
           |  |  | 405 |         }
 | 
        
           |  |  | 406 |   | 
        
           |  |  | 407 |         $answerMapper = CompanyMicrolearningAnswerMapper::getInstance($this->adapter);
 | 
        
           |  |  | 408 |         $answer = $answerMapper->fetchOneByUuid($answer_id);
 | 
        
           |  |  | 409 |         if(!$answer) {
 | 
        
           |  |  | 410 |             return new JsonModel([
 | 
        
           |  |  | 411 |                 'success'   => false,
 | 
        
           |  |  | 412 |                 'data'   => 'ERROR_ANSWER_NOT_FOUND'
 | 
        
           |  |  | 413 |             ]);
 | 
        
           |  |  | 414 |         }
 | 
        
           |  |  | 415 |   | 
        
           |  |  | 416 |         if($answer->question_id != $question->id) {
 | 
        
           |  |  | 417 |             return new JsonModel([
 | 
        
           |  |  | 418 |                 'success'   => false,
 | 
        
           |  |  | 419 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 420 |             ]);
 | 
        
           |  |  | 421 |         }
 | 
        
           |  |  | 422 |   | 
        
           |  |  | 423 |         if($request->isPost()) {
 | 
        
           |  |  | 424 |             $result = $answerMapper->delete($answer);
 | 
        
           |  |  | 425 |             if($result) {
 | 
        
           |  |  | 426 |                 $question->check = CompanyMicrolearningQuestion::CHECK_PENDING;
 | 
        
           |  |  | 427 |                 $questionMapper->update($question);
 | 
        
           |  |  | 428 |   | 
        
           |  |  | 429 |                 $quiz->check = CompanyMicrolearningQuiz::CHECK_PENDING;
 | 
        
           |  |  | 430 |                 $quizMapper->update($quiz);
 | 
        
           |  |  | 431 |   | 
        
           |  |  | 432 |                 $this->logger->info('Se borro la respuesta : ' .  substr(strip_tags($answer->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 433 |   | 
        
           |  |  | 434 |                 $data = [
 | 
        
           |  |  | 435 |                     'success' => true,
 | 
        
           |  |  | 436 |                     'data' => 'LABEL_RECORD_DELETED'
 | 
        
           |  |  | 437 |                 ];
 | 
        
           |  |  | 438 |             } else {
 | 
        
           |  |  | 439 |   | 
        
           |  |  | 440 |                 $data = [
 | 
        
           |  |  | 441 |                     'success'   => false,
 | 
        
           |  |  | 442 |                     'data'      => $answerMapper->getError()
 | 
        
           |  |  | 443 |                 ];
 | 
        
           |  |  | 444 |   | 
        
           |  |  | 445 |                 return new JsonModel($data);
 | 
        
           |  |  | 446 |             }
 | 
        
           |  |  | 447 |   | 
        
           |  |  | 448 |         } else {
 | 
        
           |  |  | 449 |             $data = [
 | 
        
           |  |  | 450 |                 'success' => false,
 | 
        
           |  |  | 451 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 452 |             ];
 | 
        
           |  |  | 453 |   | 
        
           |  |  | 454 |             return new JsonModel($data);
 | 
        
           |  |  | 455 |         }
 | 
        
           |  |  | 456 |   | 
        
           |  |  | 457 |         return new JsonModel($data);
 | 
        
           |  |  | 458 |     }
 | 
        
           |  |  | 459 |   | 
        
           |  |  | 460 |   | 
        
           |  |  | 461 |     public function editAction()
 | 
        
           |  |  | 462 |     {
 | 
        
           |  |  | 463 |         $request            = $this->getRequest();
 | 
        
           |  |  | 464 |         $currentUserPlugin  = $this->plugin('currentUserPlugin');
 | 
        
           |  |  | 465 |         $currentUser        = $currentUserPlugin->getUser();
 | 
        
           |  |  | 466 |         $currentCompany     = $currentUserPlugin->getCompany();
 | 
        
           |  |  | 467 |         $quiz_id            = $this->params()->fromRoute('quiz_id');
 | 
        
           |  |  | 468 |         $question_id        = $this->params()->fromRoute('question_id');
 | 
        
           |  |  | 469 |         $answer_id          = $this->params()->fromRoute('answer_id');
 | 
        
           |  |  | 470 |   | 
        
           |  |  | 471 |   | 
        
           |  |  | 472 |         $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
 | 
        
           |  |  | 473 |         $quiz = $quizMapper->fetchOneByUuid($quiz_id);
 | 
        
           |  |  | 474 |         if(!$quiz) {
 | 
        
           |  |  | 475 |             return new JsonModel([
 | 
        
           |  |  | 476 |                 'success'   => false,
 | 
        
           |  |  | 477 |                 'data'   => 'ERROR_FORM_NOT_FOUND'
 | 
        
           |  |  | 478 |             ]);
 | 
        
           |  |  | 479 |         }
 | 
        
           |  |  | 480 |   | 
        
           |  |  | 481 |         if($quiz->company_id != $currentCompany->id) {
 | 
        
           |  |  | 482 |             return new JsonModel([
 | 
        
           |  |  | 483 |                 'success'   => false,
 | 
        
           |  |  | 484 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 485 |             ]);
 | 
        
           |  |  | 486 |         }
 | 
        
           |  |  | 487 |   | 
        
           |  |  | 488 |         $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
 | 
        
           |  |  | 489 |         $question = $questionMapper->fetchOneByUuid($question_id);
 | 
        
           |  |  | 490 |         if(!$question) {
 | 
        
           |  |  | 491 |             return new JsonModel([
 | 
        
           |  |  | 492 |                 'success'   => false,
 | 
        
           |  |  | 493 |                 'data'   => 'ERROR_QUESTION_NOT_FOUND'
 | 
        
           |  |  | 494 |             ]);
 | 
        
           |  |  | 495 |         }
 | 
        
           |  |  | 496 |   | 
        
           |  |  | 497 |         if($question->quiz_id!= $quiz->id) {
 | 
        
           |  |  | 498 |             return new JsonModel([
 | 
        
           |  |  | 499 |                 'success'   => false,
 | 
        
           |  |  | 500 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 501 |             ]);
 | 
        
           |  |  | 502 |         }
 | 
        
           |  |  | 503 |   | 
        
           |  |  | 504 |         $answerMapper = CompanyMicrolearningAnswerMapper::getInstance($this->adapter);
 | 
        
           |  |  | 505 |         $answer = $answerMapper->fetchOneByUuid($answer_id);
 | 
        
           |  |  | 506 |         if(!$answer) {
 | 
        
           |  |  | 507 |             return new JsonModel([
 | 
        
           |  |  | 508 |                 'success'   => false,
 | 
        
           |  |  | 509 |                 'data'   => 'ERROR_ANSWER_NOT_FOUND'
 | 
        
           |  |  | 510 |             ]);
 | 
        
           |  |  | 511 |         }
 | 
        
           |  |  | 512 |   | 
        
           |  |  | 513 |         if($answer->question_id != $question->id) {
 | 
        
           |  |  | 514 |             return new JsonModel([
 | 
        
           |  |  | 515 |                 'success'   => false,
 | 
        
           |  |  | 516 |                 'data'   => 'ERROR_UNAUTHORIZED'
 | 
        
           |  |  | 517 |             ]);
 | 
        
           |  |  | 518 |         }
 | 
        
           |  |  | 519 |   | 
        
           |  |  | 520 |   | 
        
           |  |  | 521 |         if($request->isGet()) {
 | 
        
           |  |  | 522 |             $data = [
 | 
        
           |  |  | 523 |                 'success' => true,
 | 
        
           |  |  | 524 |                 'data' => [
 | 
        
           |  |  | 525 |                     'text' => $answer->text,
 | 
        
           |  |  | 526 |                     'correct' => $answer->correct,
 | 
        
           |  |  | 527 |                     'points' => $answer->points,
 | 
        
           |  |  | 528 |   | 
        
           |  |  | 529 |                 ]
 | 
        
           |  |  | 530 |             ];
 | 
        
           |  |  | 531 |   | 
        
           |  |  | 532 |             return new JsonModel($data);
 | 
        
           |  |  | 533 |         }
 | 
        
           |  |  | 534 |         else if($request->isPost()) {
 | 
        
           |  |  | 535 |             $quizCreateOrEdit = new  AnswerCreateOrEditForm();
 | 
        
           |  |  | 536 |             $quizCreateOrEdit->setData($request->getPost()->toArray());
 | 
        
           |  |  | 537 |   | 
        
           |  |  | 538 |             if($quizCreateOrEdit->isValid()) {
 | 
        
           |  |  | 539 |                 $dataPost = (array) $quizCreateOrEdit->getData();
 | 
        
           |  |  | 540 |   | 
        
           |  |  | 541 |                 $hydrator = new ObjectPropertyHydrator();
 | 
        
           |  |  | 542 |                 $hydrator->hydrate($dataPost, $answer);
 | 
        
           |  |  | 543 |   | 
        
           |  |  | 544 |                 if($answerMapper->update($answer)) {
 | 
        
           |  |  | 545 |                     $question->check = CompanyMicrolearningQuestion::CHECK_PENDING;
 | 
        
           |  |  | 546 |                     $questionMapper->update($question);
 | 
        
           |  |  | 547 |   | 
        
           |  |  | 548 |                     $quiz->check = CompanyMicrolearningQuiz::CHECK_PENDING;
 | 
        
           |  |  | 549 |                     $quizMapper->update($quiz);
 | 
        
           |  |  | 550 |   | 
        
           |  |  | 551 |                     $this->logger->info('Se edito la respuesta ' .  substr(strip_tags($answer->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
 | 
        
           |  |  | 552 |   | 
        
           |  |  | 553 |                     $data = [
 | 
        
           |  |  | 554 |                         'success'   => true,
 | 
        
           |  |  | 555 |                         'data'   => 'LABEL_RECORD_UPDATED'
 | 
        
           |  |  | 556 |                     ];
 | 
        
           |  |  | 557 |                 } else {
 | 
        
           |  |  | 558 |                     $data = [
 | 
        
           |  |  | 559 |                         'success'   => false,
 | 
        
           |  |  | 560 |                         'data'      => $answerMapper->getError()
 | 
        
           |  |  | 561 |                     ];
 | 
        
           |  |  | 562 |   | 
        
           |  |  | 563 |                 }
 | 
        
           |  |  | 564 |   | 
        
           |  |  | 565 |                 return new JsonModel($data);
 | 
        
           |  |  | 566 |   | 
        
           |  |  | 567 |             } else {
 | 
        
           |  |  | 568 |                 $messages = [];
 | 
        
           |  |  | 569 |                 $quiz_messages = (array) $quizCreateOrEdit->getMessages();
 | 
        
           |  |  | 570 |                 foreach($quiz_messages  as $fieldname => $field_messages)
 | 
        
           |  |  | 571 |                 {
 | 
        
           |  |  | 572 |   | 
        
           |  |  | 573 |                     $messages[$fieldname] = array_values($field_messages);
 | 
        
           |  |  | 574 |                 }
 | 
        
           |  |  | 575 |   | 
        
           |  |  | 576 |                 return new JsonModel([
 | 
        
           |  |  | 577 |                     'success'   => false,
 | 
        
           |  |  | 578 |                     'data'   => $messages
 | 
        
           |  |  | 579 |                 ]);
 | 
        
           |  |  | 580 |             }
 | 
        
           |  |  | 581 |         } else {
 | 
        
           |  |  | 582 |             $data = [
 | 
        
           |  |  | 583 |                 'success' => false,
 | 
        
           |  |  | 584 |                 'data' => 'ERROR_METHOD_NOT_ALLOWED'
 | 
        
           |  |  | 585 |             ];
 | 
        
           |  |  | 586 |   | 
        
           |  |  | 587 |             return new JsonModel($data);
 | 
        
           |  |  | 588 |         }
 | 
        
           |  |  | 589 |   | 
        
           |  |  | 590 |         return new JsonModel($data);
 | 
        
           |  |  | 591 |     }
 | 
        
           |  |  | 592 |   | 
        
           |  |  | 593 |   | 
        
           |  |  | 594 | }
 |