Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

Attendance_model::getUrl()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 21

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 9
nop 2
dl 30
loc 30
rs 6.7272
c 0
b 0
f 0
1
<?php
2
use mod_stats\classes\MyDateInterval;
3
4
/**
5
 * Class Attendance_model for mod_stats module
6
 * @uses \CI_Model
7
 * @author DevImageCms
8
 * @copyright (c) 2014, ImageCMS
9
 * @property CI_DB_active_record $db
10
 * @package ImageCMSModule
11
 */
12
class Attendance_model extends CI_Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
13
{
14
15
    /**
16
     * Common attendance by unique users per day(month|year)
17
     * @param array $params_
18
     *  - interval (string) day|month|year
0 ignored issues
show
introduced by
Parameter comment must start with a capital letter
Loading history...
19
     *  - dateFrom (string) YYYY-MM-DD
20
     *  - dateTo (string) YYYY-MM-DD
21
     *  - type (string) registered|unregistered|all
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
22
     * @return boolean
23
     */
24
    public function getCommonAttendance(array $params_ = []) {
25
        $params = [
26
                   'interval' => 'day',
27
                   'dateFrom' => NULL,
28
                   'dateTo'   => NULL,
29
                   'type'     => 'all',
30
                  ];
31
32
        foreach ($params_ as $key => $value) {
33
            if (array_key_exists($key, $params)) {
34
                $params[$key] = $params_[$key];
35
            }
36
        }
37
38
        $registeredCondition = '';
39
        if ($params['type'] != 'all') {
40
            $sign = $params['type'] == 'registered' ? '>' : '<';
41
            $registeredCondition = "AND `id_user` {$sign} 0 ";
42
        }
43
44
        $query = "
45
            SELECT 
46
                `time_add` as `unix_date`,
47
                DATE_FORMAT(FROM_UNIXTIME(`time_add`), '" . MyDateInterval::getDatePattern($params['interval']) . "') as `date`,
48
                COUNT(DISTINCT `id_user`) as `users_count`
49
            FROM 
50
                `mod_stats_attendance`
51
            WHERE 1 
52
                " . MyDateInterval::prepareDateBetweenCondition('time_add', $params) . "
53
                {$registeredCondition}
54
            GROUP BY 
55
                `date`
56
        ";
57
58
        $result = $this->db->query($query);
59
60
        if ($result === FALSE) {
61
            return FALSE;
62
        }
63
64
        return $result->result_array();
65
    }
66
67
    /**
68
     * Return array with users wich last activity was less then 2 minutes ago
69
     * @return boolean
70
     */
71 View Code Duplication
    public function getOnline() {
72
        $locale = MY_Controller::getCurrentLocale();
73
        $query = "
74
            SELECT 
75
                `mod_stats_attendance`.`id_user`,
76
                `mod_stats_attendance`.`type_id`,
77
                `mod_stats_attendance`.`id_entity`,
78
                IF (`mod_stats_attendance`.`id_user` < 0, 'Guest', `users`.`username`) as `username`,
79
                IF (`mod_stats_attendance`.`id_user` < 0, '', `users`.`email`) as `email`,
80
                FROM_UNIXTIME(`mod_stats_attendance`.`time_add`) as `last_activity`,
81
               
82
                -- ---- for urls ----
83
               route.url AS `last_url`,   
84
                -- ------------------
85
86
                -- ---- for names ----
87
            CASE `mod_stats_attendance`.`type_id`
88
            WHEN 1
89
              THEN IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url)
90
            WHEN 2
91
              THEN IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url)
92
            WHEN 3
93
              THEN `shop_category_i18n`.`name`
94
            WHEN 4
95
              THEN `shop_products_i18n`.`name`
96
            END as `page_name`   
97
                -- ------------------
98
            FROM 
99
                (SELECT * FROM `mod_stats_attendance` ORDER BY `id` DESC) as `mod_stats_attendance` 
100
            LEFT JOIN `users` ON `users`.`id` = `mod_stats_attendance`.`id_user`
101
102
            -- ---- for urls ----
103
            LEFT JOIN `content` ON `content`.`id` = `mod_stats_attendance`.`id_entity` 
104
                AND `mod_stats_attendance`.`type_id` = 1
105
            LEFT JOIN `category` ON `category`.`id` = `mod_stats_attendance`.`id_entity` 
106
                AND `mod_stats_attendance`.`type_id` = 2
107
            LEFT JOIN `shop_category` ON `shop_category`.`id` = `mod_stats_attendance`.`id_entity` 
108
                AND `mod_stats_attendance`.`type_id` = 3
109
            LEFT JOIN `shop_products` ON `shop_products`.`id` = `mod_stats_attendance`.`id_entity` 
110
                AND `mod_stats_attendance`.`type_id` = 4
111
            -- ------------------
112
            
113
             -- ---- for names ----
114
            LEFT JOIN `shop_category_i18n` ON `shop_category`.`id` = `shop_category_i18n`.`id` 
115
                AND `shop_category_i18n`.`locale` = '{$locale}'
116
            LEFT JOIN `shop_products_i18n` ON `shop_products`.`id` = `shop_products_i18n`.`id` 
117
                AND `shop_products_i18n`.`locale` = '{$locale}'
118
            -- ------------------
119
120
            LEFT JOIN route ON route.entity_id = `mod_stats_attendance`.id_entity AND type = (
121
              CASE `mod_stats_attendance`.`type_id`
122
              WHEN 1
123
                THEN 'page'
124
              WHEN 2
125
                THEN 'category'
126
              WHEN 3
127
                THEN 'shop_category'
128
              WHEN 4
129
                THEN 'product'
130
              END
131
            )
132
133
            WHERE 1
134
                AND FROM_UNIXTIME(`mod_stats_attendance`.`time_add`) >= NOW() - INTERVAL 120 SECOND
135
            GROUP BY 
136
                `mod_stats_attendance`.`id_user`
137
            ORDER BY 
138
                `mod_stats_attendance`.`id` DESC
139
        ";
140
141
        $results = $this->db->query($query);
142
143
        if ($results) {
144
            $results = $results->result_array();
145
            foreach ($results as &$result) {
146
                if ($result['type_id'] == Attendance::PAGE) {
147
                    $result['last_url'] = $this->getUrl($result['id_entity'], \core\models\Route::TYPE_PAGE);
148
                } elseif ($result['type_id'] == Attendance::CATEGORY) {
149
                    $result['last_url'] = $this->getUrl($result['id_entity'], \core\models\Route::TYPE_CATEGORY);
150
                } elseif ($result['type_id'] == Attendance::SHOP_CATEGORY) {
151
                    $result['last_url'] = $this->getUrl($result['id_entity'], \core\models\Route::TYPE_SHOP_CATEGORY);
152
                } elseif ($result['type_id'] == Attendance::PRODUCT) {
153
                    $result['last_url'] = $this->getUrl($result['id_entity'], \core\models\Route::TYPE_PRODUCT);
154
                }
155
            }
156
157
            return $results;
158
        }
159
160
        return false;
161
    }
162
163 View Code Duplication
    protected function getUrl($id, $type) {
164
        $urlConfiguration = \core\src\CoreFactory::getConfiguration()->getUrlRules();
165
        $url              = \core\models\RouteQuery::create()->filterByEntityId($id)->filterByType($type)->findOneOrCreate();
166
        if ($type == \core\models\Route::TYPE_SHOP_CATEGORY) {
167
            if ($urlConfiguration['shop_category']['parent'] === '1') {
168
                $url = $url->getFullUrl();
169
            } else {
170
                $url = $url->getUrl();
171
            }
172
            if ($urlConfiguration['shop_category']['prefix'] != '') {
173
                $url = rtrim($urlConfiguration['shop_category']['prefix'], '/') . '/' . $url;
174
            }
175
176
            return $url;
177
        } elseif ($type == \core\models\Route::TYPE_PRODUCT) {
178
            if ($urlConfiguration['product']['parent'] === '1') {
179
                $url = $url->getFullUrl();
180
            } else {
181
                $url = $url->getUrl();
182
            }
183
            if ($urlConfiguration['product']['prefix'] != '') {
184
                $url = rtrim($urlConfiguration['product']['prefix'], '/') . '/' . $url;
185
            }
186
187
            return $url;
188
        } else {
189
            return $url->getFullUrl();
190
        }
191
192
    }
193
194
    /**
195
     * Dynamic of categories attendance
196
     * @param array $params standart params (date, interval)
197
     * @param array $categoriesIds ids of categories witch attendance return
198
     * @return array
199
     */
200
    public function getCategoriesAttendance(array $params, array $categoriesIds) {
201
        $datePattern = MyDateInterval::getDatePattern($params['interval']);
202
        $dateBetween = MyDateInterval::prepareDateBetweenCondition('time_add', $params);
203
204
        $categoriesAttendance = [];
205
        foreach ($categoriesIds as $categoryId => $categoryIds) {
206
            $condition = 'AND `id_entity` IN (' . implode(',', $categoryIds) . ')';
207
208
            $query = "
209
                SELECT 
210
                    `time_add` as `unix_date`,
211
                    DATE_FORMAT(FROM_UNIXTIME(`time_add`), '{$datePattern}') as `date`,
212
                    COUNT(DISTINCT `id_user`) as `users_count`
213
                FROM 
214
                    `mod_stats_attendance`
215
                WHERE 1 
216
                    AND `type_id` = 3
217
                    {$condition}
218
                    {$dateBetween}
219
                GROUP BY 
220
                    `date`
221
            ";
222
223
            $categoriesAttendance[$categoryId] = $this->db->query($query)->result_array();
224
        }
225
226
        return $categoriesAttendance;
227
    }
228
229
    /**
230
     * Returns "serfing" history of specified user
231
     * @param integer $userId
232
     * @return array
233
     */
234
    public function getUserHistory($userId) {
235
        if (!is_numeric($userId)) {
236
            return FALSE;
237
        }
238
239
        $locale = MY_Controller::getCurrentLocale();
240
241
        $query = "
242
            SELECT 
243
                `mod_stats_attendance`.`type_id`,
244
                `mod_stats_attendance`.`id_entity`,
245
                FROM_UNIXTIME(`mod_stats_attendance`.`time_add`) as `time`,
246
                
247
                -- ---- for urls ----
248
                CASE `mod_stats_attendance`.`type_id`
249
                    WHEN 1 THEN `content`.`title`
250
                    WHEN 2 THEN `category`.`title`
251
                    WHEN 3 THEN CONCAT('shop/category/',`shop_category`.`full_path`)
252
                    WHEN 4 THEN CONCAT('shop/product/',`shop_products`.`url`)
253
                END as `url`,
254
                -- ------------------
255
                
256
                -- ---- for names ----
257
                CASE `mod_stats_attendance`.`type_id`
258
                    WHEN 1 THEN CONCAT(`content`.`cat_url`, `content`.`url`)
259
                    WHEN 2 THEN `category`.`url`
260
                    WHEN 3 THEN `shop_category_i18n`.`name`
261
                    WHEN 4 THEN `shop_products_i18n`.`name`
262
                END as `page_name`   
263
                -- ------------------
264
            FROM 
265
                `mod_stats_attendance`
266
                
267
            -- ---- for urls ----
268
            LEFT JOIN `content` ON `content`.`id` = `mod_stats_attendance`.`id_entity` 
269
                AND `mod_stats_attendance`.`type_id` = 1
270
            LEFT JOIN `category` ON `category`.`id` = `mod_stats_attendance`.`id_entity` 
271
                AND `mod_stats_attendance`.`type_id` = 2
272
            LEFT JOIN `shop_category` ON `shop_category`.`id` = `mod_stats_attendance`.`id_entity` 
273
                AND `mod_stats_attendance`.`type_id` = 3
274
            LEFT JOIN `shop_products` ON `shop_products`.`id` = `mod_stats_attendance`.`id_entity` 
275
                AND `mod_stats_attendance`.`type_id` = 4
276
            -- ------------------
277
            
278
279
            -- ---- for names ----
280
            LEFT JOIN `shop_category_i18n` ON `shop_category`.`id` = `shop_category_i18n`.`id` 
281
                AND `shop_category_i18n`.`locale` = '{$locale}'
282
            LEFT JOIN `shop_products_i18n` ON `shop_products`.`id` = `shop_products_i18n`.`id` 
283
                AND `shop_products_i18n`.`locale` = '{$locale}'
284
            -- ------------------
285
            
286
287
            WHERE 
288
                `id_user` = $userId
289
            ORDER BY
290
                `time_add` DESC
291
                
292
            LIMIT 200
293
                
294
        ";
295
        $result = $this->db->query($query);
296
297
        if ($result) {
298
            return $result->result_array();
299
        }
300
        return FALSE;
301
    }
302
303
    public function getRobotAttendance($robotId, $date) {
304
        $locale = MY_Controller::getCurrentLocale();
305
306
        $from = strtotime($date . ' 00:00:00');
307
        $to = strtotime($date . ' 23:59:59');
308
309
        $query = "
310
            SELECT 
311
                `mod_stats_attendance_robots`.`type_id`,
312
                `mod_stats_attendance_robots`.`id_entity`,
313
                FROM_UNIXTIME(`mod_stats_attendance_robots`.`time_add`) as `time`,
314
                
315
                -- ---- for urls ----
316
                CASE `mod_stats_attendance_robots`.`type_id`
317
                    WHEN 1 THEN `content`.`title`
318
                    WHEN 2 THEN `category`.`title`
319
                    WHEN 3 THEN CONCAT('shop/category/',`shop_category`.`full_path`)
320
                    WHEN 4 THEN CONCAT('shop/product/',`shop_products`.`url`)
321
                END as `url`,
322
                -- ------------------
323
                
324
                -- ---- for names ----
325
                CASE `mod_stats_attendance_robots`.`type_id`
326
                    WHEN 1 THEN CONCAT(`content`.`cat_url`, `content`.`url`)
327
                    WHEN 2 THEN `category`.`url`
328
                    WHEN 3 THEN `shop_category_i18n`.`name`
329
                    WHEN 4 THEN `shop_products_i18n`.`name`
330
                END as `page_name`   
331
                -- ------------------
332
            FROM 
333
                `mod_stats_attendance_robots`
334
                
335
            -- ---- for urls ----
336
            LEFT JOIN `content` ON `content`.`id` = `mod_stats_attendance_robots`.`id_entity` 
337
                AND `mod_stats_attendance_robots`.`type_id` = 1
338
            LEFT JOIN `category` ON `category`.`id` = `mod_stats_attendance_robots`.`id_entity` 
339
                AND `mod_stats_attendance_robots`.`type_id` = 2
340
            LEFT JOIN `shop_category` ON `shop_category`.`id` = `mod_stats_attendance_robots`.`id_entity` 
341
                AND `mod_stats_attendance_robots`.`type_id` = 3
342
            LEFT JOIN `shop_products` ON `shop_products`.`id` = `mod_stats_attendance_robots`.`id_entity` 
343
                AND `mod_stats_attendance_robots`.`type_id` = 4
344
            -- ------------------
345
            
346
            -- ---- for names ----
347
            LEFT JOIN `shop_category_i18n` ON `shop_category`.`id` = `shop_category_i18n`.`id` 
348
                AND `shop_category_i18n`.`locale` = '{$locale}'
349
            LEFT JOIN `shop_products_i18n` ON `shop_products`.`id` = `shop_products_i18n`.`id` 
350
                AND `shop_products_i18n`.`locale` = '{$locale}'
351
            -- ------------------
352
353
            WHERE 1
354
                AND `mod_stats_attendance_robots`.`id_robot` = {$robotId}
355
                AND `mod_stats_attendance_robots`.`time_add` > {$from}
356
                AND `mod_stats_attendance_robots`.`time_add` < {$to}
357
            ORDER BY
358
                `mod_stats_attendance_robots`.`time_add` DESC
359
                
360
        ";
361
362
        $result = $this->db->query($query);
363
364
        if ($result) {
365
            return $result->result_array();
366
        }
367
        return FALSE;
368
    }
369
370
}