Test Setup Failed
Push — master ( f71949...6c6bd7 )
by Julito
55:21
created

BlockStudentGraph::get_block()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 23
Ratio 100 %

Importance

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