Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Command;
6
 
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
//use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Log\LoggerInterface;
14
use LeadersLinked\Mapper\CompanyMicrolearningUserProgressMapper;
15
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;
16
use LeadersLinked\Mapper\QueryMapper;
17
use LeadersLinked\Model\CompanyMicrolearningUserProgress;
18
 
19
 
20
class RecalculateMicrolearningProgressCommand extends Command
21
{
22
    /**
23
     *
24
     * @var AdapterInterface
25
     */
26
    private $adapter;
27
 
28
 
29
    /**
30
     *
31
     * @var AbstractAdapter
32
     */
33
    private $cache;
34
 
35
    /**
36
     *
37
     * @var  LoggerInterface
38
     */
39
    private $logger;
40
 
41
    /**
42
     *
43
     * @var array
44
     */
45
    private $config;
46
 
47
 
48
    /**
49
     *
50
     * @param AdapterInterface $adapter
51
     * @param AbstractAdapter $cache
52
     * @param LoggerInterface $logger
53
     * @param array $config
54
     */
55
     public function __construct($adapter, $cache, $logger, $config)
56
    {
57
        $this->adapter      = $adapter;
58
        $this->cache        = $cache;
59
        $this->logger       = $logger;
60
        $this->config       = $config;
61
 
62
        parent::__construct();
63
    }
64
 
65
 
66
    protected function execute(InputInterface $input, OutputInterface $output) : int
67
    {
68
 
69
        /***** INICIO TOPICOS DUPLICADOS *****/
70
 
71
        $output->writeln('Comenzamos a buscar los registros duplicados de los Tópicos');
72
        $queryMapper = QueryMapper::getInstance($this->adapter);
73
 
74
 
75
        $sql  = " SELECT DISTINCT(user_id) AS user_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
76
        echo $sql . PHP_EOL;
77
 
78
        $record_users = $queryMapper->fetchAllUsingParameters($sql);
79
 
80
        foreach($record_users as $record_user)
81
        {
82
            $user_id = $record_user['user_id'];
83
 
84
 
85
            $sql  = " SELECT DISTINCT(topic_id) AS topic_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
86
            $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_TOPIC . "' ";
87
            $sql .= " AND user_id = $user_id";
88
 
89
            echo $sql . PHP_EOL;
90
            $records_topic = $queryMapper->fetchAllUsingParameters($sql);
91
 
92
 
93
            foreach($records_topic as $record_topic)
94
            {
95
                $topic_id = $record_topic['topic_id'];
96
 
97
                $sql  = " SELECT id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
98
                $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_TOPIC . "' ";
99
                $sql .= " AND user_id = $user_id";
100
                $sql .= " AND topic_id = $topic_id";
101
                $sql .= " ORDER BY completed DESC LIMIT 0, 1";
102
                echo $sql . PHP_EOL;
103
 
104
                $record = $queryMapper->fetchOneUsingParameters($sql);
105
                $id = $record['id'];
106
 
107
                $sql  = " DELETE FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
108
                $sql .= " WHERE type = '" . CompanyMicrolearningUserProgress::TYPE_TOPIC ."' ";
109
                $sql .= " AND user_id = $user_id ";
110
                $sql .= " AND topic_id = $topic_id ";
111
                $sql .= " AND id <> $id ";
112
                echo $sql . PHP_EOL;
113
 
114
                $queryMapper->delete($sql);
115
 
116
            }
117
        }
118
        $output->writeln('Terminamos de procesar los registros duplicados de los Tópicos');
119
 
120
        /***** FIN TOPICOS DUPLICADOS *****/
121
 
122
 
123
 
124
        /***** INICIO CAPSULAS DUPLICADOS *****/
125
 
126
        $output->writeln('Comenzamos a buscar los registros duplicados de las Capsulas');
127
        $queryMapper = QueryMapper::getInstance($this->adapter);
128
 
129
 
130
        $sql  = " SELECT DISTINCT(user_id) AS user_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
131
        echo $sql . PHP_EOL;
132
 
133
        $record_users = $queryMapper->fetchAllUsingParameters($sql);
134
 
135
        foreach($record_users as $record_user)
136
        {
137
            $user_id = $record_user['user_id'];
138
 
139
 
140
            $sql  = " SELECT DISTINCT(capsule_id) AS capsule_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
141
            $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_CAPSULE . "' ";
142
            $sql .= " AND user_id = $user_id";
143
 
144
            echo $sql . PHP_EOL;
145
            $records_capsule = $queryMapper->fetchAllUsingParameters($sql);
146
 
147
 
148
            foreach($records_capsule as $record_capsule)
149
            {
150
                $capsule_id = $record_capsule['capsule_id'];
151
 
152
                $sql  = " SELECT id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
153
                $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_CAPSULE . "' ";
154
                $sql .= " AND user_id = $user_id";
155
                $sql .= " AND capsule_id = $capsule_id";
156
                $sql .= " ORDER BY completed DESC LIMIT 0, 1";
157
                echo $sql . PHP_EOL;
158
 
159
                $record = $queryMapper->fetchOneUsingParameters($sql);
160
                $id = $record['id'];
161
 
162
                $sql  = " DELETE FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
163
                $sql .= " WHERE type = '" . CompanyMicrolearningUserProgress::TYPE_CAPSULE ."' ";
164
                $sql .= " AND user_id = $user_id ";
165
                $sql .= " AND capsule_id = $capsule_id ";
166
                $sql .= " AND id <> $id ";
167
                echo $sql . PHP_EOL;
168
 
169
                $queryMapper->delete($sql);
170
 
171
            }
172
        }
173
        $output->writeln('Terminamos de procesar los registros duplicados de las Capsulas');
174
 
175
        /***** FIN CAPSULAS DUPLICADOS *****/
176
 
177
 
178
 
179
        /***** INICIO DIAPOSITIVAS DUPLICADOS *****/
180
 
181
        $output->writeln('Comenzamos a buscar los registros duplicados de las Diapositivas');
182
        $queryMapper = QueryMapper::getInstance($this->adapter);
183
 
184
 
185
        $sql  = " SELECT DISTINCT(user_id) AS user_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
186
        echo $sql . PHP_EOL;
187
 
188
        $record_users = $queryMapper->fetchAllUsingParameters($sql);
189
 
190
        foreach($record_users as $record_user)
191
        {
192
            $user_id = $record_user['user_id'];
193
 
194
 
195
            $sql  = " SELECT DISTINCT(slide_id) AS slide_id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
196
            $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_SLIDE . "' ";
197
            $sql .= " AND user_id = $user_id";
198
 
199
            echo $sql . PHP_EOL;
200
            $records_slides = $queryMapper->fetchAllUsingParameters($sql);
201
 
202
 
203
            foreach($records_slides as $record_slide)
204
            {
205
                $slide_id = $record_slide['slide_id'];
206
 
207
                $sql  = " SELECT id FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
208
                $sql .= " WHERE  type = '" . CompanyMicrolearningUserProgress::TYPE_SLIDE . "' ";
209
                $sql .= " AND user_id = $user_id";
210
                $sql .= " AND slide_id = $slide_id";
211
                $sql .= " ORDER BY completed DESC LIMIT 0, 1";
212
                echo $sql . PHP_EOL;
213
 
214
                $record = $queryMapper->fetchOneUsingParameters($sql);
215
                $id = $record['id'];
216
 
217
                $sql  = " DELETE FROM " . CompanyMicrolearningUserProgressMapper::_TABLE;
218
                $sql .= " WHERE type = '" . CompanyMicrolearningUserProgress::TYPE_SLIDE ."' ";
219
                $sql .= " AND user_id = $user_id ";
220
                $sql .= " AND slide_id = $slide_id ";
221
                $sql .= " AND id <> $id ";
222
                echo $sql . PHP_EOL;
223
 
224
                $queryMapper->delete($sql);
225
 
226
            }
227
        }
228
        $output->writeln('Terminamos de procesar los registros duplicados de las Diapositivas');
229
 
230
        /***** FIN DIAPOSITIVAS DUPLICADOS *****/
231
 
232
        $progressMapper = CompanyMicrolearningUserProgressMapper::getInstance($this->adapter);
233
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
234
 
235
 
236
        $output->writeln('Recalculo del progreso de los Tópicos');
237
 
238
        $records = $progressMapper->fetchAllTopics();
239
 
240
       // print_r($records); exit;
241
 
242
        foreach($records as $record)
243
        {   $total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($record->company_id,$record->topic_id);
244
            $view_slides = $progressMapper->fetchCountAllSlideCompletedByUserIdAndTopicId($record->user_id, $record->topic_id);
245
 
246
            $record->total_slides = $total_slides;
247
            $record->view_slides = $view_slides;
248
            $record->progress = $total_slides > 0 ? (($view_slides * 100) / $total_slides) : 0;
249
 
250
            $sql  = " UPDATE  " . CompanyMicrolearningUserProgressMapper::_TABLE;
251
            $sql .= " SET progress =  " . $record->progress;
252
            $sql .= " , total_slides =  " . $total_slides;
253
            $sql .= " , view_slides  =  " . $view_slides;
254
            $sql .= " WHERE id =  " . $record->id;
255
            echo $sql . PHP_EOL;
256
 
257
            $queryMapper->update($sql);
258
        }
259
 
260
 
261
        $output->writeln('Inicio del proceso de Recalculo de las Capsulas');
262
        $records = $progressMapper->fetchAllCapsules();
263
        foreach($records as $record)
264
        {
265
            $total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($record->company_id, $record->topic_id, $record->capsule_id);
266
            $view_slides = $progressMapper->fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($record->user_id, $record->topic_id, $record->capsule_id);
267
 
268
            $record->total_slides = $total_slides;
269
            $record->view_slides = $view_slides;
270
            $record->progress = $total_slides > 0 ? (($view_slides * 100) / $total_slides) : 0;
271
 
272
 
273
            $sql  = " UPDATE  " . CompanyMicrolearningUserProgressMapper::_TABLE;
274
            $sql .= " SET progress =  " . $record->progress;
275
            $sql .= " , total_slides =  " . $total_slides;
276
            $sql .= " , view_slides  =  " . $view_slides;
277
            $sql .= " WHERE id =  " . $record->id;
278
            echo $sql . PHP_EOL;
279
 
280
            $queryMapper->update($sql);
281
 
282
            //$progressMapper->update($record);
283
        }
284
 
285
 
286
        $output->writeln('Fin del proceso de Recalculo');
287
 
288
        return 0;
289
    }
290
 
291
 
292
 
293
}