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

UsersController::attendance()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 0
dl 6
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'interfaces' . DIRECTORY_SEPARATOR . 'FileImport' . EXT;
4
5
/**
6
 * Class UsersController for mod_stats module
7
 * @uses ControllerBase
8
 * @author DevImageCms
9
 * @copyright (c) 2014, ImageCMS
10
 * @package ImageCMSModule
11
 */
12
class UsersController extends ControllerBase implements FileImport
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...
13
{
14
15
    public $params = [];
16
17
    public function __construct($controller) {
18
        parent::__construct($controller);
19
        $controller->import('traits/DateIntervalTrait.php');
20
        $this->params = [
21
                         'dateFrom' => CI::$APP->input->get('from') ? CI::$APP->input->get('from') : '2005-05-05',
22
                         'dateTo'   => CI::$APP->input->get('to') ? CI::$APP->input->get('to') : date('Y-m-d'),
23
                         'interval' => CI::$APP->input->get('group') ? CI::$APP->input->get('group') : 'day',
24
                        ];
25
    }
26
27
    /**
28
     * Show template for users online with data
29
     */
30
    public function online() {
31
        $this->controller->load->model('attendance_model');
32
        $onlineUsers = $this->controller->attendance_model->getOnline();
33
34
        $this->renderAdmin(
35
            'online',
36
            ['data' => $onlineUsers]
37
        );
38
    }
39
40
    /**
41
     * Show template for users online with data
42
     */
43
    public function history() {
44
        $this->controller->load->model('attendance_model');
45
        $data = $this->controller->attendance_model->getUserHistory(CI::$APP->input->post('userId'));
46
        $this->controller->assetManager->setData(['data' => $data]);
47
        $this->controller->assetManager->render('admin/users/history');
48
    }
49
50
    /**
51
     * Render template for users info with data
52
     */
53
    public function info() {
54
        $this->controller->load->model('users_model');
55
        $this->controller->users_model->setParams($this->params);
56
        $data = $this->controller->users_model->getInfo();
57
        $this->renderAdmin(
58
            'info',
59
            ['data' => $data]
60
        );
61
    }
62
63
    /**
64
     * Render template for users attendance with data
65
     */
66
    public function attendance() {
67
        // getting view type
68 View Code Duplication
        if (CI::$APP->input->get('view_type')) {
69
            $vt = CI::$APP->input->get('view_type');
70
            $viewType = $vt == 'table' || $vt == 'chart' ? $vt : 'chart';
71
        } else {
72
            $viewType = 'table';
73
        }
74
75
        $this->controller->load->model('attendance_model');
76
77
        $data = $this->controller->attendance_model->getCommonAttendance($this->params);
78
79
        $this->renderAdmin(
80
            'attendance',
81
            [
82
             'data'     => $data,
83
             'viewType' => $viewType,
84
            ]
85
        );
86
    }
87
88
    /**
89
     * Output chart data for users attendance
90
     */
91
    public function getAttendanceData() {
92
        $params = $this->params;
93
94
        $this->controller->load->model('attendance_model');
95
96
        $params['type'] = 'registered';
97
        $data = $this->controller->attendance_model->getCommonAttendance($params);
98
        $registered = [];
99 View Code Duplication
        foreach ($data as $row) {
100
            $registered[] = [
101
                             'x' => $row['unix_date'] * 1000,
102
                             'y' => (int) $row['users_count'],
103
                            ];
104
        }
105
106
        $params['type'] = 'unregistered';
107
        $data = $this->controller->attendance_model->getCommonAttendance($params);
108
        $unregistered = [];
109 View Code Duplication
        foreach ($data as $row) {
110
            $unregistered[] = [
111
                               'x' => $row['unix_date'] * 1000,
112
                               'y' => (int) $row['users_count'],
113
                              ];
114
        }
115
116
        $this->controller->import('classes/ZeroFiller');
117
118
        $response = [];
119 View Code Duplication
        if ($registered) {
120
            $response[] = [
121
                           'key'    => lang('Count of unique registered users', 'mod_stats'),
122
                           'values' => ZeroFiller::fill($registered, 'x', 'y', $this->params['interval']),
123
                          ];
124
        }
125 View Code Duplication
        if ($unregistered) {
126
            [
127
             'key'    => lang('Count of unique unregistered users', 'mod_stats'),
128
             'values' => ZeroFiller::fill($unregistered, 'x', 'y', $this->params['interval']),
129
            ];
130
        }
131
        echo json_encode($response);
132
    }
133
134
    /**
135
     * Render template for users registration
136
     */
137
    public function registered() {
138
        // getting view type
139 View Code Duplication
        if (CI::$APP->input->get('view_type')) {
140
            $vt = CI::$APP->input->get('view_type');
141
            $viewType = $vt == 'table' || $vt == 'chart' ? $vt : 'chart';
142
        } else {
143
            $viewType = 'table';
144
        }
145
146
        $params = [
147
                   'dateFrom' => CI::$APP->input->get('from') ? CI::$APP->input->get('from') : '2005-05-05',
148
                   'dateTo'   => CI::$APP->input->get('to') ? CI::$APP->input->get('to') : date('Y-m-d'),
149
                   'interval' => CI::$APP->input->get('group') ? CI::$APP->input->get('group') : 'day',
150
                  ];
151
152
        $this->controller->load->model('users_model');
153
        $this->controller->users_model->setParams($params);
154
        $data = $this->controller->users_model->getRegister();
155
156
        $this->renderAdmin(
157
            'registered',
158
            [
159
             'data'     => $data,
160
             'viewType' => $viewType,
161
            ]
162
        );
163
    }
164
165
    /**
166
     * Output chart data for users registration
167
     */
168
    public function getRegisterData() {
169
        $params = [
170
                   'dateFrom' => CI::$APP->input->get('from') ? CI::$APP->input->get('from') : '2005-05-05',
171
                   'dateTo'   => CI::$APP->input->get('to') ? CI::$APP->input->get('to') : date('Y-m-d'),
172
                   'interval' => CI::$APP->input->get('group') ? CI::$APP->input->get('group') : 'day',
173
                  ];
174
175
        $this->controller->load->model('users_model');
176
        $this->controller->users_model->setParams($params);
177
        $data = $this->controller->users_model->getRegister();
178
        $chartValues = [];
179
        foreach ($data as $row) {
180
            $chartValues[] = [
181
                              'x' => (int) $row['unix_date'] * 1000,
182
                              'y' => (int) $row['count'],
183
                             ];
184
        }
185
        $this->controller->import('classes/ZeroFiller');
186
        echo json_encode(
187
            [
188
             [
189
              'key'    => lang('Registration dynamic', 'mod_stats'),
190
              'values' => ZeroFiller::fill($chartValues, 'x', 'y', CI::$APP->input->get('group') ? CI::$APP->input->get('group') : 'day'),
191
             ],
192
            ]
193
        );
194
    }
195
196
    /**
0 ignored issues
show
introduced by
Doc comment is empty
Loading history...
197
     *
198
     */
199
    public function robots_attendance() {
200
        $date = CI::$APP->input->get('date') ? CI::$APP->input->get('date') : date('Y-m-d');
201
        $this->controller->import('classes/RobotsAttendance');
202
        $robots = RobotsAttendance::getInstance()->getRobots();
203
        $currentRobot = CI::$APP->input->get('currentRobot') ? CI::$APP->input->get('currentRobot') : $robots[0];
204
205
        $this->controller->load->model('attendance_model');
206
        $data = $this->controller->attendance_model->getRobotAttendance($currentRobot, $date);
207
208
        $this->renderAdmin(
209
            'robots_attendance',
210
            [
211
             'data'         => $data,
212
             'robots'       => $robots,
213
             'currentRobot' => $currentRobot,
214
            ]
215
        );
216
    }
217
218
    /**
219
     * Include file (or all recursively files in dir)
220
     * The starting directory is the directory where the class is (witch using trait)
221
     * @param string $filePath
222
     */
223 View Code Duplication
    public function import($filePath) {
224
        $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
225
        if ($ext != 'php' && $ext != '') {
226
            return;
227
        }
228
229
        $filePath = str_replace('.php', '', $filePath);
230
        $reflection = new ReflectionClass($this);
231
        $workingDir = pathinfo($reflection->getFileName(), PATHINFO_DIRNAME);
232
        $filePath = $workingDir . DIRECTORY_SEPARATOR . str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filePath);
233
234
        if (strpos($filePath, '*') === FALSE) {
235
            include_once $filePath . EXT;
236
        } else {
237
            $filesOfDir = get_filenames(str_replace('*', '', $filePath), TRUE);
238
            foreach ($filesOfDir as $file) {
239
                if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'php') {
240
                    include_once str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $file);
241
                }
242
            }
243
        }
244
    }
245
246
}