1 |
efrain |
1 |
<?php
|
|
|
2 |
defined('MOODLE_INTERNAL') || die();
|
|
|
3 |
|
|
|
4 |
use core_completion\progress;
|
|
|
5 |
|
1234 |
ariadna |
6 |
// Definición de la clase que extiende plugin_renderer_base
|
1231 |
ariadna |
7 |
class block_cesa_lastcourse_renderer extends plugin_renderer_base
|
|
|
8 |
{
|
|
|
9 |
public function procesar()
|
|
|
10 |
{
|
1 |
efrain |
11 |
global $USER, $DB, $CFG, $PAGE;
|
1231 |
ariadna |
12 |
|
1 |
efrain |
13 |
$userid = $USER->id;
|
|
|
14 |
$url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
|
1231 |
ariadna |
15 |
|
1234 |
ariadna |
16 |
$data = ['course' => ''];
|
1231 |
ariadna |
17 |
|
1248 |
ariadna |
18 |
// Consulta SQL optimizada para obtener el último curso accedido por el usuario
|
1253 |
ariadna |
19 |
$sql = "SELECT *
|
|
|
20 |
FROM {logstore_standard_log}
|
1263 |
ariadna |
21 |
WHERE (eventname LIKE :courseviewedevent OR eventname LIKE :moduleviewedevent)
|
1253 |
ariadna |
22 |
AND userid = :userid
|
|
|
23 |
ORDER BY timecreated DESC
|
|
|
24 |
LIMIT 1";
|
1263 |
ariadna |
25 |
$params = array('courseviewedevent' => '%course_viewed%', 'moduleviewedevent' => '%course_module_viewed%', 'userid' => $userid);
|
1253 |
ariadna |
26 |
$lastcourse = $DB->get_record_sql($sql, $params);
|
1245 |
ariadna |
27 |
|
1252 |
ariadna |
28 |
if ($lastcourse) {
|
|
|
29 |
$course = get_course($lastcourse->courseid);
|
1251 |
ariadna |
30 |
|
1236 |
ariadna |
31 |
if ($course) {
|
1237 |
ariadna |
32 |
if ($course instanceof stdClass) {
|
|
|
33 |
$courseInList = new core_course_list_element($course);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Obtener la imagen del curso o usar la imagen por defecto
|
1236 |
ariadna |
37 |
$image = $url_noimage;
|
|
|
38 |
foreach ($courseInList->get_course_overviewfiles() as $file) {
|
1239 |
ariadna |
39 |
$isimage = $file->is_valid_image();
|
|
|
40 |
$image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), ! $isimage);
|
|
|
41 |
if (! $isimage) {
|
|
|
42 |
$image = $url_noimage;
|
1236 |
ariadna |
43 |
}
|
1 |
efrain |
44 |
}
|
1237 |
ariadna |
45 |
|
|
|
46 |
// Obtener el último acceso al curso por el usuario
|
|
|
47 |
$sql = "SELECT timecreated FROM {logstore_standard_log} WHERE courseid = :courseid AND userid = :userid ";
|
|
|
48 |
$sql .= "AND eventname = '\\\\core\\\\event\\\\course_viewed' ORDER BY id DESC LIMIT 1";
|
|
|
49 |
$timecreated = $DB->get_field_sql($sql, ['courseid' => $course->id, 'userid' => $userid]);
|
|
|
50 |
$lastaccess = $timecreated ? date('d/m/Y h:i a', $timecreated) : null;
|
|
|
51 |
|
|
|
52 |
// Obtener el progreso del usuario en el curso
|
|
|
53 |
$progress = progress::get_course_progress_percentage($course);
|
|
|
54 |
$progress = $progress ? floatval($progress) : 0.0;
|
|
|
55 |
$miProgreso = number_format($progress, 2);
|
|
|
56 |
|
|
|
57 |
// Verificar roles del usuario en el curso
|
|
|
58 |
$course_context = context_course::instance($course->id);
|
|
|
59 |
$roles = get_user_roles($course_context, $USER->id, true);
|
|
|
60 |
$completion_edit_curso = false;
|
|
|
61 |
foreach ($roles as $role) {
|
|
|
62 |
if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
|
|
|
63 |
$completion_edit_curso = true;
|
|
|
64 |
break;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Si el usuario tiene ciertos roles, forzar la URL al curso
|
|
|
69 |
if ($completion_edit_curso) {
|
|
|
70 |
$linkurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Obtener la primera sección visible del curso
|
|
|
74 |
$first_section = 0;
|
|
|
75 |
$sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
|
1236 |
ariadna |
76 |
foreach ($sections as $section) {
|
1237 |
ariadna |
77 |
if (!empty($section->section)) {
|
|
|
78 |
$first_section = $section->id;
|
1236 |
ariadna |
79 |
break;
|
|
|
80 |
}
|
1235 |
ariadna |
81 |
}
|
1237 |
ariadna |
82 |
|
|
|
83 |
// Obtener el primer módulo visible del curso
|
|
|
84 |
$modules = get_fast_modinfo($course->id)->get_cms();
|
1236 |
ariadna |
85 |
$linkurl = '';
|
|
|
86 |
foreach ($modules as $module) {
|
1237 |
ariadna |
87 |
if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
|
|
|
88 |
continue;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
if ($module->section == $first_section || $completion_edit_curso) {
|
1236 |
ariadna |
92 |
$linkurl = new moodle_url($module->url, ['forceview' => 1]);
|
|
|
93 |
break;
|
|
|
94 |
}
|
1235 |
ariadna |
95 |
}
|
1231 |
ariadna |
96 |
|
1237 |
ariadna |
97 |
// Obtener categoría
|
|
|
98 |
$category = $DB->get_record('course_categories', ['id' => $course->category]);
|
1231 |
ariadna |
99 |
|
1236 |
ariadna |
100 |
// Obtener un resumen del curso
|
|
|
101 |
$summary = trim(strip_tags($course->summary));
|
|
|
102 |
if (empty($summary)) {
|
|
|
103 |
$summary = ' ';
|
|
|
104 |
} elseif (strlen($summary) > 80) {
|
|
|
105 |
$summary = substr($summary, 0, 80) . '...';
|
1235 |
ariadna |
106 |
}
|
|
|
107 |
|
1236 |
ariadna |
108 |
// Asignar datos del curso al array de respuesta
|
|
|
109 |
$data['course'] = [
|
|
|
110 |
'courseid' => $course->id,
|
|
|
111 |
'coursecategory' => $category->name,
|
|
|
112 |
'courseimage' => $image,
|
|
|
113 |
'enddate' => $course->enddate,
|
|
|
114 |
'fullname' => $course->fullname,
|
|
|
115 |
'fullnamedisplay' => get_course_display_name_for_list($course),
|
|
|
116 |
'hasprogress' => true,
|
|
|
117 |
'hidden' => false,
|
|
|
118 |
'id' => $course->id,
|
|
|
119 |
'idnumber' => $course->idnumber,
|
|
|
120 |
'isfavourite' => false,
|
|
|
121 |
'progress' => $miProgreso,
|
|
|
122 |
'shortname' => $course->shortname,
|
|
|
123 |
'showshortname' => false,
|
|
|
124 |
'startdate' => $course->startdate,
|
|
|
125 |
'summary' => $summary,
|
|
|
126 |
'summaryformat' => $course->summaryformat,
|
|
|
127 |
'timeaccess' => $lastaccess,
|
|
|
128 |
'viewurl' => $CFG->wwwroot . '/course/view.php?id=' . $course->id,
|
|
|
129 |
'viewurlnew' => $linkurl,
|
|
|
130 |
'visible' => true,
|
|
|
131 |
];
|
1237 |
ariadna |
132 |
}
|
1251 |
ariadna |
133 |
} else {
|
|
|
134 |
$data["course"] = [
|
|
|
135 |
'fullname' => "Sin respuesta de la base de datos"
|
|
|
136 |
];
|
1 |
efrain |
137 |
}
|
1231 |
ariadna |
138 |
|
1234 |
ariadna |
139 |
// Renderizar la plantilla con los datos del curso
|
1 |
efrain |
140 |
return $this->render_from_template('block_cesa_lastcourse/full', $data);
|
|
|
141 |
}
|
|
|
142 |
}
|