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

block_teacher_graph/block_teacher_graph.class.php (1 issue)

1
<?php
2
/**
3
 * This file is part of teacher graph block plugin for dashboard,
4
 * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
5
 *
6
 * @package chamilo.dashboard
7
 *
8
 * @author Christian Fasanando
9
 */
10
11
/**
12
 * required files for getting data.
13
 */
14
use CpChart\Cache as pCache;
15
use CpChart\Data as pData;
16
use CpChart\Image as pImage;
17
18
/**
19
 * This class is used like controller for teacher graph block plugin,
20
 * the class name must be registered inside path.info file (e.g: controller = "BlockTeacherGraph"), so dashboard controller will be instantiate it.
21
 *
22
 * @package chamilo.dashboard
23
 */
24
class BlockTeacherGraph extends Block
25
{
26
    private $user_id;
27
    private $teachers;
28
    private $path;
29
    private $permission = [DRH];
30
31
    /**
32
     * Controller.
33
     */
34
    public function __construct($user_id)
35
    {
36
        $this->user_id = $user_id;
37
        $this->path = 'block_teacher_graph';
38
        if ($this->is_block_visible_for_user($user_id)) {
39
            $this->teachers = UserManager::get_users_followed_by_drh($user_id, COURSEMANAGER);
40
        }
41
    }
42
43
    /**
44
     * This method check if a user is allowed to see the block inside dashboard interface.
45
     *
46
     * @param int        User id
47
     *
48
     * @return bool Is block visible for user
49
     */
50
    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
59
        return $is_block_visible_for_user;
60
    }
61
62
    /**
63
     * This method return content html containing information about teachers and its position for showing it inside dashboard interface
64
     * it's important to use the name 'get_block' for beeing used from dashboard controller.
65
     *
66
     * @return array column and content html
67
     */
68
    public function get_block()
69
    {
70
        global $charset;
71
        $column = 1;
72
        $data = [];
73
        $teacher_information_graph = $this->get_teachers_information_graph();
74
        $html = '
75
                <div class="panel panel-default" id="intro">
76
                    <div class="panel-heading">'.get_lang('TeachersInformationsGraph').'
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('TimeSpentOnThePlatformLastWeekByDay').'</strong></div>
83
                        '.$teacher_information_graph.'
84
                    </div>
85
                </div>
86
                ';
87
88
        $data['column'] = $column;
89
        $data['content_html'] = $html;
90
91
        return $data;
92
    }
93
94
    /**
95
     * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
96
     *
97
     * @return string content html
98
     */
99
    public function get_teachers_information_graph()
100
    {
101
        $teachers = $this->teachers;
102
        $graph = '';
103
        $user_ids = array_keys($teachers);
104
        $a_last_week = get_last_week();
105
106
        if (is_array($user_ids) && count($user_ids) > 0) {
107
            $dataSet = new pData();
108
            foreach ($user_ids as $user_id) {
109
                $teacher_info = api_get_user_info($user_id);
110
                $username = $teacher_info['username'];
111
                $time_by_days = [];
112
                foreach ($a_last_week as $day) {
113
                    // day is received as y-m-d 12:00:00
114
                    $start_date = api_get_utc_datetime($day);
115
                    $end_date = api_get_utc_datetime($day + (3600 * 24 - 1));
116
117
                    $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform(
118
                        $user_id,
119
                        'custom',
120
                        $start_date,
121
                        $end_date
122
                    );
123
                    $hours = floor($time_on_platform_by_day / 3600);
124
                    $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
125
                    $time_by_days[] = $min;
126
                }
127
                $dataSet->addPoints($time_by_days, $username);
128
            }
129
130
            $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
131
            $days_on_week = [];
132
            foreach ($a_last_week as $weekday) {
133
                $days_on_week[] = date('d/m', $weekday);
134
            }
135
136
            $dataSet->addPoints($days_on_week, 'Days');
137
            $dataSet->setAbscissaName($last_week);
138
            $dataSet->setAxisName(0, get_lang('Minutes'));
139
            $dataSet->setAbscissa('Days');
140
            $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
141
142
            // Cache definition
143
            $cachePath = api_get_path(SYS_ARCHIVE_PATH);
144
            $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
145
            $chartHash = $myCache->getHash($dataSet);
146
            if ($myCache->isInCache($chartHash)) {
147
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
148
                $myCache->saveFromCache($chartHash, $imgPath);
149
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
150
            } else {
151
                /* Create the pChart object */
152
                $widthSize = 440;
153
                $heightSize = 350;
154
                $angle = 50;
155
                $myPicture = new pImage($widthSize, $heightSize, $dataSet);
156
157
                /* Turn of Antialiasing */
158
                $myPicture->Antialias = false;
159
160
                /* Add a border to the picture */
161
                $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
162
163
                /* Set the default font */
164
                $myPicture->setFontProperties(['FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', 'FontSize' => 10]);
165
166
                /* Do NOT Write the chart title */
167
168
                /* Define the chart area */
169
                $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80);
170
171
                /* Draw the scale */
172
                $scaleSettings = [
173
                    'GridR' => 200,
174
                    'GridG' => 200,
175
                    'GridB' => 200,
176
                    'DrawSubTicks' => true,
177
                    'CycleBackground' => true,
178
                    '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...
179
                    'LabelRotation' => $angle,
180
                ];
181
182
                $myPicture->drawScale($scaleSettings);
183
184
                /* Turn on shadow computing */
185
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
186
187
                /* Draw the chart */
188
                $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
189
                $settings = [
190
                    'DisplayValues' => true,
191
                    'DisplayR' => 0,
192
                    'DisplayG' => 0,
193
                    'DisplayB' => 0,
194
                ];
195
                $myPicture->drawFilledSplineChart($settings);
196
                $myPicture->drawLegend(40, 20, ['Mode' => LEGEND_HORIZONTAL]);
197
198
                /* Write and save into cache */
199
                $myCache->writeToCache($chartHash, $myPicture);
200
                $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
201
                $myCache->saveFromCache($chartHash, $imgPath);
202
                $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
203
            }
204
            $graph = '<img src="'.$imgPath.'" >';
205
        } else {
206
            $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
207
        }
208
209
        return $graph;
210
    }
211
212
    /**
213
     * Get number of teachers.
214
     *
215
     * @return int
216
     */
217
    public function get_number_of_teachers()
218
    {
219
        return count($this->teachers);
220
    }
221
}
222