Proyectos de Subversion LeadersLinked - Services

Rev

Rev 119 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 *
4
 * Controlador: Microlearning
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
 
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
use LeadersLinked\Mapper\CompanyMicrolearningUserLogMapper;
18
use LeadersLinked\Model\CompanyMicrolearningUserLog;
19
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
20
use LeadersLinked\Mapper\CompanyMicrolearningUserProgressMapper;
21
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;
22
 
23
class ProfileMicrolearningController extends AbstractActionController
24
{
25
    /**
26
     *
27
     * @var \Laminas\Db\Adapter\AdapterInterface
28
     */
29
    private $adapter;
30
 
31
    /**
32
     *
33
     * @var \LeadersLinked\Cache\CacheInterface
34
     */
35
    private $cache;
36
 
37
 
38
    /**
39
     *
40
     * @var \Laminas\Log\LoggerInterface
41
     */
42
    private $logger;
43
 
44
    /**
45
     *
46
     * @var array
47
     */
48
    private $config;
49
 
50
 
51
    /**
52
     *
53
     * @var \Laminas\Mvc\I18n\Translator
54
     */
55
    private $translator;
56
 
57
 
58
    /**
59
     *
60
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
61
     * @param \LeadersLinked\Cache\CacheInterface $cache
62
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
63
     * @param array $config
64
     * @param \Laminas\Mvc\I18n\Translator $translator
65
     */
66
    public function __construct($adapter, $cache, $logger, $config, $translator)
67
    {
68
        $this->adapter      = $adapter;
69
        $this->cache        = $cache;
70
        $this->logger       = $logger;
71
        $this->config       = $config;
72
        $this->translator   = $translator;
73
    }
74
 
75
    /**
76
     *
77
     * Generación del listado de perfiles
78
     * {@inheritDoc}
79
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
80
     */
81
    public function indexAction()
82
    {
83
        //$currentUserPlugin = $this->plugin('currentUserPlugin');
84
        //$currentUser = $currentUserPlugin->getUser();
85
 
86
        $request = $this->getRequest();
87
        if($request->isGet()) {
88
 
89
 
90
            return new JsonModel([
91
                'activities' => [
92
                    'ACTIVITY_SIGNIN' =>  CompanyMicrolearningUserLog::ACTIVITY_SIGNIN,
93
                    'ACTIVITY_SIGNOUT' => CompanyMicrolearningUserLog::ACTIVITY_SIGNOUT,
94
                    'ACTIVITY_START_TOPIC' => CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
95
                    'ACTIVITY_START_CAPSULE' => CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
96
                    'ACTIVITY_VIEW_SLIDE' => CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
97
                    'ACTIVITY_TAKE_A_TEST' => CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
98
                    'ACTIVITY_RETAKE_A_TEST' => CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
99
                    'ACTIVITY_APPROVED_TEST' => CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
100
                    'ACTIVITY_COMPLETED_CAPSULE' => CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
101
                    'ACTIVITY_COMPLETED_TOPIC' => CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC
102
                ]
103
            ]);
104
 
105
 
106
 
107
        } else {
108
            return new JsonModel([
109
                'success' => false,
110
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
111
            ]);
112
        }
113
    }
114
 
115
 
116
    /**
117
     *
118
     * Generación del timeline de microaprendizaje para el usuario actual
119
     * solo con agregar ?page=XX
120
     *
121
     * Para las repuesta afirmativa
122
     * [
123
     *    'success' => true,
124
     *    'data' => [
125
     *      'total' => [
126
     *          'count' => cantidad de registros totales,
127
     *          'pages' => cantidad de páginas totales,
128
     *      ],
129
     *      'current' => [
130
     *          'items'    => [
131
     *              [
132
     *                  'activity' => actividad realizada,
133
     *                  'added_on' => fecha en la que fué realizada
134
     *              ]
135
     *           ] ,
136
     *          'page'     => página actual,
137
     *          'count'    => cantidad de registros de la página actual,
138
     *       ]
139
     *    ]
140
     *
141
     *  En caso contrario
142
     *  [
143
     *      'success' => false,
144
     *      'data' => mensaje de error
145
     *  ]
146
     *
147
     *
148
     * @return \Laminas\View\Model\JsonModel
149
     */
150
    public function timelineAction()
151
    {
152
 
153
        $request = $this->getRequest();
154
        if($request->isGet()) {
155
            $currentUserPlugin = $this->plugin('currentUserPlugin');
156
            $currentUser = $currentUserPlugin->getUser();
157
 
158
 
159
            $page = intval($this->params()->fromQuery('page'), 10);
160
 
161
            $companyMicrolearningUserLogMapper = CompanyMicrolearningUserLogMapper::getInstance($this->adapter);
162
            $paginator = $companyMicrolearningUserLogMapper->getAllMessagesPaginatorByUserId($currentUser->id, $page);
163
 
164
            $items = [];
165
            foreach($paginator as $record)
166
            {
167
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
168
 
169
                array_push($items, [
170
                    'activity' => $record->activity,
171
                    'added_on' => $dt->format('d/m/Y H:i a')
172
                ]);
173
            }
174
 
175
 
176
            return new JsonModel([
177
                    'success' => true,
178
                    'data' => [
179
                        'total' => [
180
                            'count' => $paginator->getTotalItemCount(),
181
                            'pages' => $paginator->getPages()->pageCount,
182
                        ],
183
                        'current' => [
184
                            'items'    => $items,
185
                            'page'     => $paginator->getCurrentPageNumber(),
186
                            'count'    => $paginator->getCurrentItemCount(),
187
                        ]
188
                    ]
189
            ]);
190
 
191
 
192
        } else {
193
            return new JsonModel([
194
                'success' => false,
195
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
196
            ]);
197
        }
198
    }
199
 
200
    /**
201
     * Valores para la generación de los gráficos de progreso
202
     * Para las repuesta afirmativa
203
     * [
204
     *  'success' => true,
205
     *      'data' => [
206
     *          'topicTotal' => cantidad total de tópicos,
207
     *          'topicStarted' => cantidad de tópicos iniciados,
208
     *          'topicIncompleted' => cantidad de tópicos incompletos,
209
     *          'topicCompleted' => cantidad de tópicos completos,
210
     *          'percentCompleted' => % de diapositivas completados ,
211
     *          'percentIncompleted' => % de diapositivas incompletos ,
212
     *          'percentWithoutReturning' => % de cápsulas sin retorno después de completada,
213
     *          'percentWithReturning' => % de cápsulas con retorno después de completada,
214
     *       ],
215
     * ]
216
     *
217
     *
218
     *  En caso contrario
219
     *  [
220
     *      'success' => false,
221
     *      'data' => mensaje de error
222
     *  ]
223
     *
224
     *
225
     * @return \Laminas\View\Model\JsonModel
226
     */
227
    public function progressAction()
228
    {
229
 
230
        $request = $this->getRequest();
231
        if($request->isGet()) {
232
            $currentUserPlugin = $this->plugin('currentUserPlugin');
233
            $currentUser = $currentUserPlugin->getUser();
234
 
235
            $topicIdsUsers = [];
236
            $capsuleIdsUser = [];
237
 
238
            $topicTotal = 0;
239
            $topicStarted = 0;
240
            $topicIncompleted = 0;
241
            $topicCompleted =  0;
242
            $totalSlides = 0;
243
            $totalSlidesCompleted = 0;
244
            //$totalSlideWithReturning = 0;
245
 
246
            $totalCapsules = 0;
247
            $totalCapsulesCompleted = 0;
248
            $totalCapsulesCompletedWithReturn = 0;
249
            $totalCapsulesCompletedWithoutReturn = 0;
250
 
251
            //$companyMicrolearningTopicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
252
            $companyMicrolearningSlideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
253
 
254
            $companyMicrolearningUserProgressMapper = CompanyMicrolearningUserProgressMapper::getInstance($this->adapter);
255
 
256
 
257
            $companyMicrolearningCapsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
258
            $capsulesUser = $companyMicrolearningCapsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
259
 
260
            foreach($capsulesUser as $capsuleUser)
261
            {
262
                $company_id = $capsuleUser->company_id;
263
                $topic_id   = $capsuleUser->topic_id;
264
                $capsule_id = $capsuleUser->capsule_id;
265
 
266
                if(!in_array($topic_id, $topicIdsUsers)) {
267
                    $topicTotal++;
268
                    array_push($topicIdsUsers, $topic_id);
269
 
270
                    $progress = $companyMicrolearningUserProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic_id);
271
                    if($progress) {
272
                        $topicStarted++;
273
                        if(100 > $progress->progress) {
274
                            $topicIncompleted++;
275
                        } else {
276
                            $topicCompleted++;
277
                        }
278
 
279
                    }
280
                }
281
 
282
                if(!in_array($capsule_id, $capsuleIdsUser)) {
283
 
284
                    $totalSlides += $companyMicrolearningSlideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($company_id, $topic_id, $capsule_id);
285
                    $totalSlidesCompleted += $companyMicrolearningUserProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $capsule_id);
286
 
287
                    array_push($capsuleIdsUser, $capsule_id);
288
 
289
                    $totalCapsules++;
290
                    $progress = $companyMicrolearningUserProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
291
                    if($progress && $progress->completed) {
292
 
293
                        $totalCapsulesCompleted++;
294
                        if($progress->returning_after_completed) {
295
                            $totalCapsulesCompletedWithReturn++;
296
                        } else {
297
                            $totalCapsulesCompletedWithoutReturn++;
298
                        }
299
 
300
                    }
301
 
302
 
303
                }
304
 
305
 
306
            }
307
 
308
            $percentCompleted = 0;
309
            $percentIncompleted = 100;
310
 
311
            if( $totalSlides > 0) {
312
                $percentCompleted = ($totalSlidesCompleted  * 100) /  $totalSlides;
313
                $percentIncompleted = 100 - $percentCompleted;
314
            }
315
 
316
 
317
            $percentWithoutReturning  = 0;
318
            $percentWithReturning = 0;
319
 
320
            if($totalCapsulesCompleted > 0) {
321
                $percentWithReturning = ($totalCapsulesCompletedWithReturn  * 100) / $totalCapsulesCompleted;
322
                $percentWithoutReturning = ($totalCapsulesCompletedWithoutReturn  * 100) / $totalCapsulesCompleted;
323
            }
324
 
325
 
326
 
327
            return new JsonModel([
328
                'success' => true,
329
                'data' => [
330
                    'topicTotal' => $topicTotal,
331
                    'topicStarted' => $topicStarted,
332
                    'topicIncompleted' => $topicIncompleted,
333
                    'topicCompleted' => $topicCompleted,
334
                    'percentCompleted' => number_format($percentCompleted, 2, '.', ','),
335
                    'percentIncompleted' => number_format($percentIncompleted, 2, '.', ','),
336
                    'percentWithoutReturning' => number_format($percentWithoutReturning, 2, '.', ','),
337
                    'percentWithReturning' => number_format($percentWithReturning, 2, '.', ','),
338
                ],
339
            ]);
340
 
341
 
342
        } else {
343
            return new JsonModel([
344
                'success' => false,
345
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
346
            ]);
347
        }
348
    }
349
 
350
 
351
 
352
}