Proyectos de Subversion Moodle

Rev

Rev 497 | Ir a la última revisión | | Ultima modificación | Ver Log |

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