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

BlockStudentGraph::getContent()   C

Complexity

Conditions 10
Paths 16

Size

Total Lines 148
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 95
nc 16
nop 0
dl 0
loc 148
rs 6.2424
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/* For licensing terms, see /license.txt */
3
4
/**
5
 * This file is part of student graph block plugin for dashboard,
6
 * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
7
 *
8
 * @package chamilo.dashboard
9
 *
10
 * @author Christian Fasanando
11
 * @author Julio Montoya <[email protected]>
12
 */
13
use CpChart\Cache as pCache;
14
use CpChart\Data as pData;
15
use CpChart\Image as pImage;
16
17
/**
18
 * This class is used like controller for student graph block plugin,
19
 * the class name must be registered inside path.info file
20
 * (e.g: controller = "BlockStudentGraph"), so dashboard controller will be instantiate it.
21
 *
22
 * @package chamilo.dashboard
23
 */
24
class BlockStudentGraph extends Block
25
{
26
    private $user_id;
27
    private $students;
28
    private $permission = [DRH];
29
30
    /**
31
     * Constructor.
32
     */
33
    public function __construct($user_id)
34
    {
35
        $this->user_id = $user_id;
36
        $this->path = 'block_student_graph';
37
        if ($this->is_block_visible_for_user($user_id)) {
38
            /*if (api_is_platform_admin()) {
39
                $this->students = UserManager::get_user_list(array('status' => STUDENT));
40
            } else if (api_is_drh()) {*/
41
            $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
42
            //}
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 students
67
     * and its position for showing it inside dashboard interface
68
     * it's important to use the name 'get_block' for being used from dashboard controller.
69
     *
70
     * @return array column and content html
71
     */
72
    public function get_block()
73
    {
74
        $column = 1;
75
        $data = [];
76
        $html = $this->getBlockCard(
77
            get_lang('StudentsInformationsGraph'),
78
            $this->getContent()
79
        );
80
81
        $data['column'] = $column;
82
        $data['content_html'] = $html;
83
84
        return $data;
85
    }
86
87
    /**
88
     * This method return a graph containing information about students evaluation,
89
     * it's used inside get_block method for showing it inside dashboard interface.
90
     *
91
     * @return string img html
92
     */
93
    public function getContent()
94
    {
95
        $students = $this->students;
96
        $attendance = new Attendance();
97
98
        // get data
99
        $attendances_faults_avg = [];
100
        if (is_array($students) && count($students) > 0) {
101
            foreach ($students as $student) {
102
                $student_id = $student['user_id'];
103
                //$student_info = api_get_user_info($student_id);
104
                // get average of faults in attendances by student
105
                $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
106
107
                if (!empty($results_faults_avg)) {
108
                    $attendances_faults_avg[$student['lastname']] = $results_faults_avg['porcent'];
109
                } else {
110
                    $attendances_faults_avg[$student['lastname']] = 0;
111
                }
112
            }
113
        }
114
115
        arsort($attendances_faults_avg);
116
        $usernames = array_keys($attendances_faults_avg);
117
118
        $faults = [];
119
        foreach ($usernames as $username) {
120
            $faults[] = $attendances_faults_avg[$username];
121
        }
122
123
        $graph = '';
124
        $img_file = '';
125
        if (is_array($usernames) && count($usernames) > 0) {
126
            // Defining data
127
            $dataSet = new pData();
128
            $dataSet->addPoints($faults, 'Serie1');
129
            $dataSet->addPoints($usernames, 'Labels');
130
            $dataSet->setSerieDescription('Series1', get_lang('Average'));
131
            $dataSet->setSerieDescription('Labels', get_lang('User'));
132
            $dataSet->setAbscissa('Labels');
133
            $dataSet->setAbscissaName(get_lang('User'));
134
            $dataSet->setAxisName(0, get_lang('Attendance'));
135
            $palette = [
136
                '0' => ['R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100],
137
                '1' => ['R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100],
138
                '2' => ['R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100],
139
                '3' => ['R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100],
140
                '4' => ['R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100],
141
                '5' => ['R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100],
142
                '6' => ['R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100],
143
                '7' => ['R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100],
144
                '8' => ['R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100],
145
            ];
146
            // Cache definition
147
            $cachePath = api_get_path(SYS_ARCHIVE_PATH);
148
            $myCache = new pCache(
149
                [
150
                    'CacheFolder' => substr(
151
                        $cachePath,
152
                        0,
153
                        strlen($cachePath) - 1
154
                    ),
155
                ]
156
            );
157
            $chartHash = $myCache->getHash($dataSet);
158
            if ($myCache->isInCache($chartHash)) {
159
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
160
                $myCache->saveFromCache($chartHash, $imgPath);
161
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
162
            } else {
163
                $maxCounts = max(count($usernames), count($faults));
164
                if ($maxCounts < 5) {
165
                    $heightSize = 200;
166
                } else {
167
                    $heightSize = $maxCounts * 40;
168
                }
169
170
                /* Create the pChart object */
171
                $widthSize = 480;
172
                $angle = 40;
173
174
                $myPicture = new pImage($widthSize, $heightSize, $dataSet);
175
176
                /* Turn of Antialiasing */
177
                $myPicture->Antialias = false;
178
179
                /* Add a border to the picture */
180
                $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
181
182
                /* Set the default font */
183
                $myPicture->setFontProperties(
184
                    [
185
                        'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
186
                        'FontSize' => 10,
187
                    ]
188
                );
189
190
                /* Do NOT Write the chart title */
191
192
                /* Define the chart area */
193
                $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40);
194
195
                /* Draw the scale */
196
                $scaleSettings = [
197
                    'GridR' => 200,
198
                    'GridG' => 200,
199
                    'GridB' => 200,
200
                    'DrawSubTicks' => true,
201
                    'CycleBackground' => true,
202
                    '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...
203
                    'Pos' => SCALE_POS_TOPBOTTOM,
0 ignored issues
show
Bug introduced by
The constant SCALE_POS_TOPBOTTOM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
204
                    'DrawXLines' => false,
205
                    'LabelRotation' => $angle,
206
                ];
207
208
                $myPicture->drawScale($scaleSettings);
209
210
                /* Turn on shadow computing */
211
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
212
213
                /* Draw the chart */
214
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
215
                $settings = [
216
                    'OverrideColors' => $palette,
217
                    'Gradient' => false,
218
                    'GradientMode' => GRADIENT_SIMPLE,
0 ignored issues
show
Bug introduced by
The constant GRADIENT_SIMPLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
219
                    'DisplayPos' => LABEL_POS_TOP,
0 ignored issues
show
Bug introduced by
The constant LABEL_POS_TOP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
                    'DisplayValues' => true,
221
                    'DisplayR' => 0,
222
                    'DisplayG' => 0,
223
                    'DisplayB' => 0,
224
                    'DisplayShadow' => true,
225
                    'Surrounding' => 10,
226
                ];
227
                $myPicture->drawBarChart($settings);
228
229
                /* Write and save into cache */
230
                $myCache->writeToCache($chartHash, $myPicture);
231
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
232
                $myCache->saveFromCache($chartHash, $imgPath);
233
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
234
            }
235
            $graph = '<img src="'.$imgPath.'" >';
236
        } else {
237
            $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
238
        }
239
240
        return $graph;
241
    }
242
243
    /**
244
     * Get number of students.
245
     *
246
     * @return int
247
     */
248
    public function get_number_of_students()
249
    {
250
        return count($this->students);
251
    }
252
}
253