Passed
Push — master ( 34bdad...b152c5 )
by Julito
20:20
created

BlockTeacherGraph::getContent()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 111
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 73
nc 3
nop 0
dl 0
loc 111
rs 7.6557
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use CpChart\Cache as pCache;
4
use CpChart\Data as pData;
5
use CpChart\Image as pImage;
6
7
/**
8
 * This file is part of teacher graph block plugin for dashboard,
9
 * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
10
 *
11
 * @package chamilo.dashboard
12
 *
13
 * @author Christian Fasanando
14
 */
15
16
/**
17
 * required files for getting data.
18
 */
19
20
21
/**
22
 * This class is used like controller for teacher graph block plugin,
23
 * the class name must be registered inside path.info file (e.g: controller = "BlockTeacherGraph"),
24
 * so dashboard controller will be instantiate it.
25
 *
26
 * @package chamilo.dashboard
27
 */
28
class BlockTeacherGraph extends Block
29
{
30
    private $user_id;
31
    private $teachers;
32
    private $permission = [DRH];
33
34
    /**
35
     * Controller.
36
     */
37
    public function __construct($user_id)
38
    {
39
        $this->user_id = $user_id;
40
        $this->path = 'block_teacher_graph';
41
        if ($this->is_block_visible_for_user($user_id)) {
42
            $this->teachers = UserManager::get_users_followed_by_drh($user_id, COURSEMANAGER);
43
        }
44
    }
45
46
    /**
47
     * This method check if a user is allowed to see the block inside dashboard interface.
48
     *
49
     * @param int        User id
50
     *
51
     * @return bool Is block visible for user
52
     */
53
    public function is_block_visible_for_user($user_id)
54
    {
55
        $user_info = api_get_user_info($user_id);
56
        $user_status = $user_info['status'];
57
        $is_block_visible_for_user = false;
58
        if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
59
            $is_block_visible_for_user = true;
60
        }
61
62
        return $is_block_visible_for_user;
63
    }
64
65
    /**
66
     * This method return content html containing information about teachers and its position for showing it inside dashboard interface
67
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
68
     *
69
     * @return array column and content html
70
     */
71
    public function get_block()
72
    {
73
        $column = 1;
74
        $data = [];
75
        $html = $this->getBlockCard(
76
            get_lang('TeachersInformationsGraph'),
77
            $this->getContent()
78
        );
79
80
        $data['column'] = $column;
81
        $data['content_html'] = $html;
82
83
        return $data;
84
    }
85
86
    /**
87
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
88
     *
89
     * @return string content html
90
     */
91
    public function getContent()
92
    {
93
        $teachers = $this->teachers;
94
        $graph = '';
95
        $user_ids = array_keys($teachers);
96
        $a_last_week = get_last_week();
97
98
        if (is_array($user_ids) && count($user_ids) > 0) {
99
            $dataSet = new pData();
100
            foreach ($user_ids as $user_id) {
101
                $teacher_info = api_get_user_info($user_id);
102
                $username = $teacher_info['username'];
103
                $time_by_days = [];
104
                foreach ($a_last_week as $day) {
105
                    // day is received as y-m-d 12:00:00
106
                    $start_date = api_get_utc_datetime($day);
107
                    $end_date = api_get_utc_datetime($day + (3600 * 24 - 1));
108
109
                    $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform(
110
                        $user_id,
111
                        'custom',
112
                        $start_date,
113
                        $end_date
114
                    );
115
                    $hours = floor($time_on_platform_by_day / 3600);
116
                    $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
117
                    $time_by_days[] = $min;
118
                }
119
                $dataSet->addPoints($time_by_days, $username);
120
            }
121
122
            $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
123
            $days_on_week = [];
124
            foreach ($a_last_week as $weekday) {
125
                $days_on_week[] = date('d/m', $weekday);
126
            }
127
128
            $dataSet->addPoints($days_on_week, 'Days');
129
            $dataSet->setAbscissaName($last_week);
130
            $dataSet->setAxisName(0, get_lang('Minutes'));
131
            $dataSet->setAbscissa('Days');
132
            $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
133
134
            // Cache definition
135
            $cachePath = api_get_path(SYS_ARCHIVE_PATH);
136
            $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
137
            $chartHash = $myCache->getHash($dataSet);
138
            if ($myCache->isInCache($chartHash)) {
139
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
140
                $myCache->saveFromCache($chartHash, $imgPath);
141
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
142
            } else {
143
                /* Create the pChart object */
144
                $widthSize = 440;
145
                $heightSize = 350;
146
                $angle = 50;
147
                $myPicture = new pImage($widthSize, $heightSize, $dataSet);
148
149
                /* Turn of Antialiasing */
150
                $myPicture->Antialias = false;
151
152
                /* Add a border to the picture */
153
                $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
154
155
                /* Set the default font */
156
                $myPicture->setFontProperties(['FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', 'FontSize' => 10]);
157
158
                /* Do NOT Write the chart title */
159
160
                /* Define the chart area */
161
                $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80);
162
163
                /* Draw the scale */
164
                $scaleSettings = [
165
                    'GridR' => 200,
166
                    'GridG' => 200,
167
                    'GridB' => 200,
168
                    'DrawSubTicks' => true,
169
                    'CycleBackground' => true,
170
                    'Mode' => SCALE_MODE_ADDALL_START0,
0 ignored issues
show
Bug introduced by
The constant SCALE_MODE_ADDALL_START0 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
171
                    'LabelRotation' => $angle,
172
                ];
173
174
                $myPicture->drawScale($scaleSettings);
175
176
                /* Turn on shadow computing */
177
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
178
179
                /* Draw the chart */
180
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
181
                $settings = [
182
                    'DisplayValues' => true,
183
                    'DisplayR' => 0,
184
                    'DisplayG' => 0,
185
                    'DisplayB' => 0,
186
                ];
187
                $myPicture->drawFilledSplineChart($settings);
188
                $myPicture->drawLegend(40, 20, ['Mode' => LEGEND_HORIZONTAL]);
0 ignored issues
show
Bug introduced by
The constant LEGEND_HORIZONTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
189
190
                /* Write and save into cache */
191
                $myCache->writeToCache($chartHash, $myPicture);
192
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
193
                $myCache->saveFromCache($chartHash, $imgPath);
194
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
195
            }
196
            $graph = '<img src="'.$imgPath.'" >';
197
        } else {
198
            $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
199
        }
200
201
        return $graph;
202
    }
203
204
    /**
205
     * Get number of teachers.
206
     *
207
     * @return int
208
     */
209
    public function get_number_of_teachers()
210
    {
211
        return count($this->teachers);
212
    }
213
}
214