Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

block_student_graph/block_student_graph.class.php (4 issues)

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