Completed
Push — master ( c9546d...95f607 )
by Julito
09:41
created

block_evaluation_graph.class.php (8 issues)

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