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

block_evaluation_graph.class.php (8 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use CpChart\Cache as pCache;
5
use CpChart\Data as pData;
6
use CpChart\Image as pImage;
7
8
/**
9
 * Class BlockEvaluationGraph
10
 * This class is used like controller for this evaluations graph block plugin,
11
 * the class name must be registered inside path.info file
12
 * (e.g: controller = "BlockEvaluationGraph"),
13
 * so dashboard controller will be instantiate it.
14
 *
15
 * This file is part of evaluation graph block plugin for dashboard,
16
 * it should be required inside dashboard controller for showing it
17
 * into dashboard interface from platform
18
 *
19
 * @package chamilo.dashboard
20
 *
21
 * @author Christian Fasanando
22
 */
23
class BlockEvaluationGraph extends Block
24
{
25
    private $user_id;
26
    private $courses;
27
    private $sessions;
28
    private $path;
29
    private $permission = [DRH, SESSIONADMIN];
30
31
    /**
32
     * Constructor.
33
     */
34
    public function __construct($user_id)
35
    {
36
        $this->path = 'block_evaluation_graph';
37
        $this->user_id = $user_id;
38
        $this->bg_width = 450;
39
        $this->bg_height = 350;
40
        if ($this->is_block_visible_for_user($user_id)) {
41
            if (!api_is_session_admin()) {
42
                $this->courses = CourseManager::get_courses_followed_by_drh($user_id);
43
            }
44
            $this->sessions = SessionManager::get_sessions_followed_by_drh($user_id);
45
        }
46
    }
47
48
    /**
49
     * This method check if a user is allowed to see the block inside dashboard interface.
50
     *
51
     * @param int        User id
52
     *
53
     * @return bool Is block visible for user
54
     */
55
    public function is_block_visible_for_user($user_id)
56
    {
57
        $user_info = api_get_user_info($user_id);
58
        $user_status = $user_info['status'];
59
        $is_block_visible_for_user = false;
60
        if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
61
            $is_block_visible_for_user = true;
62
        }
63
64
        return $is_block_visible_for_user;
65
    }
66
67
    /**
68
     * This method return content html containing
69
     * information about sessions and its position for showing it inside dashboard interface
70
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
71
     *
72
     * @return array column and content html
73
     */
74
    public function get_block()
75
    {
76
        global $charset;
77
        $column = 1;
78
        $data = [];
79
        $evaluations_base_courses_graph = $this->get_evaluations_base_courses_graph();
80
        $evaluations_courses_in_sessions_graph = $this->get_evaluations_courses_in_sessions_graph();
81
82
        $html = '<div class="panel panel-default" id="intro">
83
                    <div class="panel-heading">
84
                        '.get_lang('EvaluationsGraph').'
85
                        <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.'">
86
                        <em class="fa fa-times"></em>
87
                        </a></div>
88
                    </div>
89
                    <div class="panel-body">';
90
        if (empty($evaluations_base_courses_graph) && empty($evaluations_courses_in_sessions_graph)) {
91
            $html .= '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
92
        } else {
93
            // display evaluations base courses graph
94
            if (!empty($evaluations_base_courses_graph)) {
95
                foreach ($evaluations_base_courses_graph as $course_code => $img_html) {
96
                    $html .= '<div><strong>'.$course_code.'</strong></div>';
97
                    $html .= $img_html;
98
                }
99
            }
100
            // display evaluations base courses graph
101
            if (!empty($evaluations_courses_in_sessions_graph)) {
102
                foreach ($evaluations_courses_in_sessions_graph as $session_id => $courses) {
103
                    $session_name = api_get_session_name($session_id);
104
                    $html .= '<div><strong>'.$session_name.':'.get_lang('Evaluations').'</strong></div>';
105
                    foreach ($courses as $course_code => $img_html) {
106
                        $html .= '<div><strong>'.$course_code.'</strong></div>';
107
                        $html .= $img_html;
108
                    }
109
                }
110
            }
111
        }
112
        $html .= '</div>
113
                 </div>';
114
115
        $data['column'] = $column;
116
        $data['content_html'] = $html;
117
118
        return $data;
119
    }
120
121
    /**
122
     * This method return a graph containing informations about evaluations
123
     * inside base courses, it's used inside get_block method for showing
124
     * it inside dashboard interface.
125
     *
126
     * @return string img html
127
     */
128
    public function get_evaluations_base_courses_graph()
129
    {
130
        $graphs = [];
131
        if (!empty($this->courses)) {
132
            $courses_code = array_keys($this->courses);
133
            foreach ($courses_code as $course_code) {
134
                $cats = Category::load(
135
                    null,
136
                    null,
137
                    $course_code,
138
                    null,
139
                    null,
140
                    null,
141
                    false
142
                );
143
144
                if (isset($cats) && isset($cats[0])) {
145
                    $alleval = $cats[0]->get_evaluations(null, true, $course_code);
146
                    $alllinks = $cats[0]->get_links(null, true);
147
                    $users = GradebookUtils::get_all_users($alleval, $alllinks);
148
                    $datagen = new FlatViewDataGenerator($users, $alleval, $alllinks);
149
                    $evaluation_sumary = $datagen->getEvaluationSummaryResults();
150
                    if (!empty($evaluation_sumary)) {
151
                        $items = array_keys($evaluation_sumary);
152
                        $max = $min = $avg = [];
153
                        foreach ($evaluation_sumary as $evaluation) {
154
                            $max[] = $evaluation['max'];
155
                            $min[] = !empty($evaluation['min']) ? $evaluation['min'] : 0;
156
                            $avg[] = $evaluation['avg'];
157
                        }
158
                        // Dataset definition
159
                        $dataSet = new pData();
160
                        $dataSet->addPoints($min, 'Serie3');
161
                        $dataSet->addPoints($avg, 'Serie2');
162
                        $dataSet->addPoints($max, 'Serie1');
163
                        $dataSet->addPoints($items, 'Labels');
164
165
                        $dataSet->setSerieDescription('Serie1', get_lang('Max'));
166
                        $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
167
                        $dataSet->setSerieDescription('Serie3', get_lang('Min'));
168
                        $dataSet->setAbscissa('Labels');
169
170
                        $dataSet->setAbscissaName(get_lang('EvaluationName'));
171
172
                        $dataSet->normalize(100, '%');
173
174
                        $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
175
176
                        // Cache definition
177
                        $cachePath = api_get_path(SYS_ARCHIVE_PATH);
178
                        $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
179
                        $chartHash = $myCache->getHash($dataSet);
180
                        if ($myCache->isInCache($chartHash)) {
181
                            $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
182
                            $myCache->saveFromCache($chartHash, $imgPath);
183
                            $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
184
                        } else {
185
                            /* Create the pChart object */
186
                            $widthSize = $this->bg_width;
187
                            $heightSize = $this->bg_height;
188
                            $fontSize = 8;
189
                            $angle = 50;
190
191
                            $myPicture = new pImage($widthSize, $heightSize, $dataSet);
192
193
                            /* Turn of Antialiasing */
194
                            $myPicture->Antialias = false;
195
196
                            /* Add a border to the picture */
197
                            $myPicture->drawRectangle(
198
                                0,
199
                                0,
200
                                $widthSize - 1,
201
                                $heightSize - 1,
202
                                [
203
                                    'R' => 0,
204
                                    'G' => 0,
205
                                    'B' => 0,
206
                                ]
207
                            );
208
209
                            /* Set the default font */
210
                            $myPicture->setFontProperties(
211
                                [
212
                                    'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
213
                                    'FontSize' => 10,
214
                                ]
215
                            );
216
217
                            /* Do NOT Write the chart title */
218
219
                            /* Define the chart area */
220
                            $myPicture->setGraphArea(
221
                                50,
222
                                30,
223
                                $widthSize - 20,
224
                                $heightSize - 100
225
                            );
226
227
                            /* Draw the scale */
228
                            $scaleSettings = [
229
                                'GridR' => 200,
230
                                'GridG' => 200,
231
                                'GridB' => 200,
232
                                'DrawSubTicks' => true,
233
                                'CycleBackground' => true,
234
                                'Mode' => SCALE_MODE_MANUAL,
0 ignored issues
show
The constant SCALE_MODE_MANUAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
235
                                'ManualScale' => [
236
                                    '0' => [
237
                                        'Min' => 0,
238
                                        'Max' => 100,
239
                                    ],
240
                                ],
241
                                'LabelRotation' => $angle,
242
                            ];
243
                            $myPicture->drawScale($scaleSettings);
244
245
                            /* Turn on shadow computing */
246
                            $myPicture->setShadow(
247
                                true,
248
                                [
249
                                    'X' => 1,
250
                                    'Y' => 1,
251
                                    'R' => 0,
252
                                    'G' => 0,
253
                                    'B' => 0,
254
                                    'Alpha' => 10,
255
                                ]
256
                            );
257
258
                            /* Draw the chart */
259
                            $myPicture->setShadow(
260
                                true,
261
                                [
262
                                    'X' => 1,
263
                                    'Y' => 1,
264
                                    'R' => 0,
265
                                    'G' => 0,
266
                                    'B' => 0,
267
                                    'Alpha' => 10,
268
                                ]
269
                            );
270
                            $settings = [
271
                                'DisplayValues' => true,
272
                                'DisplaySize' => $fontSize,
273
                                'DisplayR' => 0,
274
                                'DisplayG' => 0,
275
                                'DisplayB' => 0,
276
                                'DisplayOrientation' => ORIENTATION_HORIZONTAL,
0 ignored issues
show
The constant ORIENTATION_HORIZONTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
277
                                'Gradient' => false,
278
                                'Surrounding' => 30,
279
                                'InnerSurrounding' => 25,
280
                            ];
281
                            $myPicture->drawStackedBarChart($settings);
282
283
                            $legendSettings = [
284
                                'Mode' => LEGEND_HORIZONTAL,
0 ignored issues
show
The constant LEGEND_HORIZONTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
285
                                'Style' => LEGEND_NOBORDER,
0 ignored issues
show
The constant LEGEND_NOBORDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
286
                            ];
287
                            $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
288
289
                            /* Write and save into cache */
290
291
                            $myCache->writeToCache($chartHash, $myPicture);
292
                            $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
293
                            $myCache->saveFromCache($chartHash, $imgPath);
294
                            $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
295
                        }
296
                        if (!empty($imgPath)) {
297
                            $courses_graph[$course_code] = '<img src="'.$imgPath.'">';
298
                        }
299
                    }
300
                }
301
            } // end for
302
        }
303
304
        return $graphs;
305
    }
306
307
    /**
308
     * This method return a graph containing information about evaluations
309
     * inside courses in sessions, it's used inside get_block method for
310
     * showing it inside dashboard interface.
311
     *
312
     * @return string img html
313
     */
314
    public function get_evaluations_courses_in_sessions_graph()
315
    {
316
        $graphs = [];
317
        if (!empty($this->sessions)) {
318
            $session_ids = array_keys($this->sessions);
319
            foreach ($session_ids as $session_id) {
320
                $courses_code = array_keys(Tracking::get_courses_list_from_session($session_id));
321
                $courses_graph = [];
322
                foreach ($courses_code as $course_code) {
323
                    $cats = Category::load(null, null, $course_code, null, null, $session_id);
324
                    if (isset($cats) && isset($cats[0])) {
325
                        $alleval = $cats[0]->get_evaluations(null, true, $course_code);
326
                        $alllinks = $cats[0]->get_links(null, true);
327
                        $users = GradebookUtils::get_all_users($alleval, $alllinks);
328
                        $datagen = new FlatViewDataGenerator($users, $alleval, $alllinks);
329
                        $evaluation_sumary = $datagen->getEvaluationSummaryResults();
330
                        if (!empty($evaluation_sumary)) {
331
                            $items = array_keys($evaluation_sumary);
332
                            $max = $min = $avg = [];
333
                            foreach ($evaluation_sumary as $evaluation) {
334
                                $max[] = $evaluation['max'];
335
                                $min[] = $evaluation['min'];
336
                                $avg[] = $evaluation['avg'];
337
                            }
338
                            // Dataset definition
339
                            $dataSet = new pData();
340
                            $dataSet->addPoints($min, 'Serie3');
341
                            $dataSet->addPoints($avg, 'Serie2');
342
                            $dataSet->addPoints($max, 'Serie1');
343
                            $dataSet->addPoints($items, 'Labels');
344
345
                            $dataSet->setSerieDescription('Serie1', get_lang('Max'));
346
                            $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
347
                            $dataSet->setSerieDescription('Serie3', get_lang('Min'));
348
                            $dataSet->setAbscissa('Labels');
349
                            $dataSet->setAbscissaName(get_lang('EvaluationName'));
350
                            $dataSet->normalize(100, '%');
351
                            $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
352
353
                            // Cache definition
354
                            $cachePath = api_get_path(SYS_ARCHIVE_PATH);
355
                            $myCache = new pCache(
356
                                [
357
                                    'CacheFolder' => substr(
358
                                        $cachePath,
359
                                        0,
360
                                        strlen($cachePath) - 1
361
                                    ),
362
                                ]
363
                            );
364
                            $chartHash = $myCache->getHash($dataSet);
365
                            if ($myCache->isInCache($chartHash)) {
366
                                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
367
                                $myCache->saveFromCache($chartHash, $imgPath);
368
                                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
369
                            } else {
370
                                /* Create the pChart object */
371
                                $widthSize = $this->bg_width;
372
                                $heightSize = $this->bg_height;
373
                                $fontSize = 8;
374
                                $angle = 50;
375
376
                                $myPicture = new pImage($widthSize, $heightSize, $dataSet);
377
378
                                /* Turn of Antialiasing */
379
                                $myPicture->Antialias = false;
380
381
                                /* Add a border to the picture */
382
                                $myPicture->drawRectangle(
383
                                    0,
384
                                    0,
385
                                    $widthSize - 1,
386
                                    $heightSize - 1,
387
                                    [
388
                                        'R' => 0,
389
                                        'G' => 0,
390
                                        'B' => 0,
391
                                    ]
392
                                );
393
394
                                /* Set the default font */
395
                                $myPicture->setFontProperties(
396
                                    [
397
                                        'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
398
                                        'FontSize' => 10,
399
                                    ]
400
                                );
401
402
                                /* Do NOT Write the chart title */
403
404
                                /* Define the chart area */
405
                                $myPicture->setGraphArea(50, 30, $widthSize - 20, $heightSize - 100);
406
407
                                /* Draw the scale */
408
                                $scaleSettings = [
409
                                    'GridR' => 200,
410
                                    'GridG' => 200,
411
                                    'GridB' => 200,
412
                                    'DrawSubTicks' => true,
413
                                    'CycleBackground' => true,
414
                                    'Mode' => SCALE_MODE_MANUAL,
0 ignored issues
show
The constant SCALE_MODE_MANUAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
415
                                    'ManualScale' => [
416
                                        '0' => [
417
                                            'Min' => 0,
418
                                            'Max' => 100,
419
                                        ],
420
                                    ],
421
                                    'LabelRotation' => $angle,
422
                                ];
423
                                $myPicture->drawScale($scaleSettings);
424
425
                                /* Turn on shadow computing */
426
                                $myPicture->setShadow(
427
                                    true,
428
                                    [
429
                                        'X' => 1,
430
                                        'Y' => 1,
431
                                        'R' => 0,
432
                                        'G' => 0,
433
                                        'B' => 0,
434
                                        'Alpha' => 10,
435
                                    ]
436
                                );
437
438
                                /* Draw the chart */
439
                                $myPicture->setShadow(
440
                                    true,
441
                                    [
442
                                        'X' => 1,
443
                                        'Y' => 1,
444
                                        'R' => 0,
445
                                        'G' => 0,
446
                                        'B' => 0,
447
                                        'Alpha' => 10,
448
                                    ]
449
                                );
450
                                $settings = [
451
                                    'DisplayValues' => true,
452
                                    'DisplaySize' => $fontSize,
453
                                    'DisplayR' => 0,
454
                                    'DisplayG' => 0,
455
                                    'DisplayB' => 0,
456
                                    'DisplayOrientation' => ORIENTATION_HORIZONTAL,
0 ignored issues
show
The constant ORIENTATION_HORIZONTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
457
                                    'Gradient' => false,
458
                                    'Surrounding' => 30,
459
                                    'InnerSurrounding' => 25,
460
                                ];
461
                                $myPicture->drawStackedBarChart($settings);
462
463
                                $legendSettings = [
464
                                    'Mode' => LEGEND_HORIZONTAL,
0 ignored issues
show
The constant LEGEND_HORIZONTAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
465
                                    'Style' => LEGEND_NOBORDER,
0 ignored issues
show
The constant LEGEND_NOBORDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
466
                                ];
467
                                $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
468
469
                                /* Write and save into cache */
470
                                $myCache->writeToCache($chartHash, $myPicture);
471
                                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
472
                                $myCache->saveFromCache($chartHash, $imgPath);
473
                                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
474
                            }
475
                            if (!empty($imgPath)) {
476
                                $courses_graph[$course_code] = '<img src="'.$imgPath.'">';
477
                            }
478
                        }
479
                    }
480
                }
481
                if (!empty($courses_graph)) {
482
                    $graphs[$session_id] = $courses_graph;
483
                }
484
            }
485
        }
486
487
        return $graphs;
488
    }
489
}
490