Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
use core_completion\progress;
3
 
4
class block_progreso_detalles_renderer extends plugin_renderer_base {
5
 
6
    public function procesar($instance_id) {
7
 
8
        global $USER, $CFG, $DB, $PAGE, $OUTPUT;
9
 
10
        require_once $CFG->libdir . '/completionlib.php';
11
        require_once $CFG->dirroot.'/completion/classes/progress.php';
12
 
13
        require_once $CFG->libdir . '/gradelib.php';
14
        require_once $CFG->dirroot . '/grade/querylib.php';
15
 
16
        require_once $CFG->libdir . '/coursecatlib.php';
17
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
18
 
19
        if (!file_exists($CFG->dirroot.'/cesa/progreso_detalles/')) {
20
            mkdir($CFG->dirroot.'/cesa/progreso_detalles/', 0755, true);
21
        }
22
        if (!file_exists($CFG->dirroot.'/cesa/progreso_detalles/'.$USER->id)) {
23
            mkdir($CFG->dirroot.'/cesa/progreso_detalles/'.$USER->id, 0755, true);
24
        }
25
        $oldfiles = glob($CFG->dirroot.'/cesa/progreso_detalles/'.$USER->id.'/*');
26
        foreach ($oldfiles as $file) {
27
            if (is_file($file)) {
28
                unlink($file);
29
            }
30
        }
31
 
32
        $user       = $DB->get_record('user',array('id'=> $USER->id));
33
        $mycourses  = enrol_get_users_courses($USER->id);
34
        $max_time   = time();
35
        $min_time   = strtotime('-1 YEAR');
36
        $limit      = 3600;
37
 
38
        $data = [
39
            'user_name'         => $user->firstname . ' ' . $user->lastname  ,
40
            'user_image'        => $OUTPUT->user_picture($user, array('size' => 100, 'link' => false, 'alttext' => false)),
41
            'user_email'        => $user->email,
42
            'graph_dedication'  => '',
43
            'completed'         => 0,
44
            'inprogress'        => 0,
45
            'total_dedication'  => '',
46
            'courses'           => []
47
        ];
48
 
49
 
50
 
51
 
52
        $graph_percentage = [];
53
        $graph_dedication = [];
54
        $graph_labels = [];
55
 
56
 
57
        $completed = 0;
58
        $inprogress = 0;
59
	$dedication = 0;
60
        $total_dedication = 0;
61
        foreach($mycourses as $key => $val) {
62
            $course = get_course($val->id);
63
 
64
            if ($course instanceof stdClass) {
65
                $courseInList = new course_in_list($course);
66
            }
67
            $image = $url_noimage;
68
            foreach ($courseInList->get_course_overviewfiles() as $file)
69
            {
70
                $isimage = $file->is_valid_image();
71
                $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
72
                if (!$isimage) {
73
                    $image = $url_noimage;
74
                }
75
            }
76
 
77
            $lastaccess = $DB->get_field('report_training_lastaccess', 'timeaccess', array('courseid' => $course->id, 'userid' => $userid));
78
 
79
            $category = $DB->get_record('course_categories',array('id'=>$course->category));
80
 
81
 
82
            $completion = new \completion_info($course);
83
            if ($completion->is_enabled()) {
84
                $percentage = progress::get_course_progress_percentage($course);
85
                $percentage = floatval(!$percentage ? 0 :  $percentage);
86
 
87
            } else {
88
                $percentage = 0;
89
            }
90
 
91
 
92
            if($percentage >= 100) {
93
                $completed++;
94
            } else {
95
                $inprogress++;
96
            }
97
 
98
            $resultkrb =  grade_get_course_grades($course->id, $USER->id);
99
 
100
 
101
            $grd = $resultkrb->grades[$USER->id];
102
            $dedication = $this->get_students_dedication($USER->id, $course->id, $min_time, $max_time, $limit);
103
 
104
            $total_dedication = $total_dedication + $dedication;
105
 
106
            $graph_percentage[] = number_format($percentage, 2);
107
            $graph_labels[] = $course->shortname;
108
 
109
 
110
            $data['courses'][] = [
111
 
112
                'coursecategory' => $category->name,
113
                'courseimage' => $image,
114
                'enddate' => $course->enddate,
115
                'shortname' => $course->shortname,
116
                'fullname' => $course->fullname,
117
                'fullnamedisplay' => get_course_display_name_for_list($course),
118
                'hasprogress' => $percentage > 0,
119
                'hidden' => false,
120
                'id' => $course->id,
121
                'idnumber' => $course->idnumber,
122
                'isfavourite' => false,
123
                'progress' => $percentage,
124
                'shortname' => $course->shortname,
125
                'showshortname' => false,
126
                'startdate' => $course->startdate,
127
                'summary' => $course->summary,
128
                'summaryformat' => $course->summaryformat,
129
                'timeaccess' => $lastaccess,
130
                'viewurl' => $CFG->wwwroot . '/course/view.php?id=' . $course->id,
131
                'visible' => true,
132
                'grade' => $grd->str_grade,
133
                'dedication' => $dedication,
134
                'percentage' => number_format($percentage, 2),
135
            ];
136
        }
137
 
138
	$dedication = 0;
139
        for($i = 0, $max = count($data['courses']); $i < $max; $i++)
140
        {
141
            $dedication = ($data['courses'][$i][ 'dedication'] * 100) / $total_dedication;
142
 
143
            $data['courses'][$i][ 'dedication']  = $this->format_dedication($data['courses'][$i][ 'dedication']);
144
            array_push($graph_dedication, number_format($dedication, 2));
145
        }
146
 
147
 
148
 
149
        $serie_dedication = new core\chart_series('Dedicación', $graph_dedication);
150
        $serie_percentage = new core\chart_series('Progreso', $graph_percentage);
151
 
152
 
153
 
154
        $chart = new core\chart_bar();
155
 
156
        $chart->set_title('Dedicación/Progreso en %');
157
        $chart->set_horizontal(true); // Calling set_horizontal() passing true as parameter will display horizontal bar charts.
158
        $chart->add_series( $serie_dedication );
159
        $chart->add_series( $serie_percentage);
160
        $chart->set_labels($graph_labels);
161
 
162
        //Cambias las colores de las series
163
        //$CFG->chart_colorset = ['#001f3f', '#01ff70', '#F012BE', '#85144b', '#B10DC9'];
164
        //var_dump ($chart);
165
        //die();
166
 
167
        $data['graph_dedication'] = $OUTPUT->render_chart($chart, true); //true muesta tabla de datos, false no
168
 
169
 
170
        $data['completed']          = $completed;
171
        $data['inprogress']         = $inprogress;
172
        $data['total_dedication']   = $this->format_dedication($total_dedication);
173
 
174
        $content .= html_writer::start_tag('div', array('class' => 'container')); //'d-flex flex-row bd-highlight mb-3'));
175
        $content .= $this->render_from_template('block_progreso_detalles/full', $data);
176
        $content .= html_writer::end_tag('div');
177
        $content = str_replace('<!--USER-IMAGE-->', $data['user_image'], $content);
178
        $content = str_replace('<!--GRAPH-DEDICATION-->', $data['graph_dedication'], $content);
179
 
180
 
181
        return $content;
182
    }
183
 
184
    public static function format_dedication($totalsecs) {
185
        $totalsecs = abs($totalsecs);
186
 
187
        $str = new stdClass();
188
        $str->hour = get_string('hour');
189
        $str->hours = get_string('hours');
190
        $str->min = get_string('min');
191
        $str->mins = get_string('mins');
192
        $str->sec = get_string('sec');
193
        $str->secs = get_string('secs');
194
 
195
        $hours = floor($totalsecs / HOURSECS);
196
        $remainder = $totalsecs - ($hours * HOURSECS);
197
        $mins = floor($remainder / MINSECS);
198
        $secs = round($remainder - ($mins * MINSECS), 2);
199
 
200
        $ss = ($secs == 1) ? $str->sec : $str->secs;
201
        $sm = ($mins == 1) ? $str->min : $str->mins;
202
        $sh = ($hours == 1) ? $str->hour : $str->hours;
203
 
204
        $ohours = '';
205
        $omins = '';
206
        $osecs = '';
207
 
208
        if ($hours) {
209
            $ohours = $hours . ' ' . $sh;
210
        }
211
        if ($mins) {
212
            $omins = $mins . ' ' . $sm;
213
        }
214
        if ($secs) {
215
            $osecs = $secs . ' ' . $ss;
216
        }
217
 
218
        if ($hours) {
219
            return trim($ohours . ' ' . $omins);
220
        }
221
        if ($mins) {
222
            return trim($omins . ' ' . $osecs);
223
        }
224
        if ($secs) {
225
            return $osecs;
226
        }
227
        return get_string('none');
228
    }
229
 
230
    public function get_events_select($selectwhere, array $params)
231
    {
232
        $logstores  = ['logstore_standard', 'logstore_legacy'];
233
        $return     = [];
234
 
235
        static $allreaders = null;
236
 
237
        if (is_null($allreaders)) {
238
            $allreaders = get_log_manager()->get_readers();
239
        }
240
 
241
        $processedreaders = 0;
242
 
243
        foreach ($logstores as $name) {
244
            if (isset($allreaders[$name])) {
245
                $reader = $allreaders[$name];
246
                $events = $reader->get_events_select($selectwhere, $params, 'timecreated ASC', 0, 0);
247
                foreach ($events as $event) {
248
                    // Note: see \core\event\base to view base class of event.
249
                    $obj = new stdClass();
250
                    $obj->time = $event->timecreated;
251
                    $obj->ip = $event->get_logextra()['ip'];
252
                    $return[] = $obj;
253
                }
254
                if (!empty($events)) {
255
                    $processedreaders++;
256
                }
257
            }
258
        }
259
 
260
        // Sort mixed array by time ascending again only when more of a reader has added events to return array.
261
        if ($processedreaders > 1) {
262
            usort($return, function($a, $b) {
263
                return $a->time > $b->time;
264
            });
265
        }
266
 
267
        return $return;
268
    }
269
 
270
    public function get_students_dedication($user_id, $course_id, $min_time, $max_time, $limit)
271
    {
272
 
273
        $where = 'courseid = :courseid AND userid = :userid AND timecreated >= :mintime AND timecreated <= :maxtime';
274
        $params = array(
275
            'courseid'  => $course_id,
276
            'userid'    => $user_id,
277
            'mintime'   => $min_time,
278
            'maxtime'   => $max_time
279
        );
280
 
281
 
282
        $logs = $this->get_events_select($where, $params);
283
        if ($logs) {
284
            $previouslog = array_shift($logs);
285
            $previouslogtime = $previouslog->time;
286
            $sessionstart = $previouslog->time;
287
            $dedication = 0;
288
 
289
            foreach ($logs as $log) {
290
                if (($log->time - $previouslogtime) > $limit) {
291
                    $dedication += $previouslogtime - $sessionstart;
292
                    $sessionstart = $log->time;
293
                }
294
                $previouslogtime = $log->time;
295
            }
296
            $dedication += $previouslogtime - $sessionstart;
297
        } else {
298
            $dedication = 0;
299
        }
300
        return $dedication;
301
 
302
    }
303
 
304
 
305
 
306
}