Proyectos de Subversion Moodle

Rev

Rev 1245 | Rev 1247 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1241 ariadna 18
        // Consulta SQL optimizada para obtener el último modulo visto por el usuario
1242 ariadna 19
        $sql  = "SELECT DISTINCT(coursemoduleid) AS coursemoduleid FROM {course_modules_viewed} WHERE ";
1241 ariadna 20
        $sql .= "userid = :userid ORDER BY timecreated DESC LIMIT 1";
21
        $last_module_viewed = $DB->get_record_sql($sql, ['userid' => $userid]);
1231 ariadna 22
 
1245 ariadna 23
        if ($last_module_viewed) {
24
            $data["course"] = [
1246 ariadna 25
                'fullname' => "Existe last viewed" . $last_module_viewed->coursemoduleid
1245 ariadna 26
            ];
27
        }
28
 
29
        $module_viewed = $DB->get_record('course_modules', array('id' => $last_module_viewed->coursemoduleid), '*');
30
 
1246 ariadna 31
        if ($module_viewed) {
32
            $data["course"] = [
33
                'fullname' => "Existe module" . $module_viewed->course
34
            ];
35
        } else {
36
            $data["course"] = [
37
                'fullname' => "Existe last viewed" . $last_module_viewed->coursemoduleid
38
            ];
39
        }
1231 ariadna 40
 
1245 ariadna 41
 
1241 ariadna 42
        if ($module_viewed) {
43
            $course = get_course($module_viewed->course);
1231 ariadna 44
 
1236 ariadna 45
            if ($course) {
1237 ariadna 46
                if ($course instanceof stdClass) {
47
                    $courseInList = new core_course_list_element($course);
48
                }
49
 
50
                // Obtener la imagen del curso o usar la imagen por defecto
1236 ariadna 51
                $image = $url_noimage;
52
                foreach ($courseInList->get_course_overviewfiles() as $file) {
1239 ariadna 53
                    $isimage = $file->is_valid_image();
54
                    $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), ! $isimage);
55
                    if (! $isimage) {
56
                        $image = $url_noimage;
1236 ariadna 57
                    }
1 efrain 58
                }
1237 ariadna 59
 
60
                // Obtener el último acceso al curso por el usuario
61
                $sql = "SELECT timecreated FROM {logstore_standard_log} WHERE courseid = :courseid AND userid = :userid ";
62
                $sql .= "AND eventname = '\\\\core\\\\event\\\\course_viewed' ORDER BY id DESC LIMIT 1";
63
                $timecreated = $DB->get_field_sql($sql, ['courseid' => $course->id, 'userid' => $userid]);
64
                $lastaccess = $timecreated ? date('d/m/Y h:i a', $timecreated) : null;
65
 
66
                // Obtener el progreso del usuario en el curso
67
                $progress = progress::get_course_progress_percentage($course);
68
                $progress = $progress ? floatval($progress) : 0.0;
69
                $miProgreso = number_format($progress, 2);
70
 
71
                // Verificar roles del usuario en el curso
72
                $course_context = context_course::instance($course->id);
73
                $roles = get_user_roles($course_context, $USER->id, true);
74
                $completion_edit_curso = false;
75
                foreach ($roles as $role) {
76
                    if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
77
                        $completion_edit_curso = true;
78
                        break;
79
                    }
80
                }
81
 
82
                // Si el usuario tiene ciertos roles, forzar la URL al curso
83
                if ($completion_edit_curso) {
84
                    $linkurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
85
                }
86
 
87
                // Obtener la primera sección visible del curso
88
                $first_section = 0;
89
                $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
1236 ariadna 90
                foreach ($sections as $section) {
1237 ariadna 91
                    if (!empty($section->section)) {
92
                        $first_section = $section->id;
1236 ariadna 93
                        break;
94
                    }
1235 ariadna 95
                }
1237 ariadna 96
 
97
                // Obtener el primer módulo visible del curso
98
                $modules = get_fast_modinfo($course->id)->get_cms();
1236 ariadna 99
                $linkurl = '';
100
                foreach ($modules as $module) {
1237 ariadna 101
                    if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
102
                        continue;
103
                    }
104
 
105
                    if ($module->section == $first_section || $completion_edit_curso) {
1236 ariadna 106
                        $linkurl = new moodle_url($module->url, ['forceview' => 1]);
107
                        break;
108
                    }
1235 ariadna 109
                }
1231 ariadna 110
 
1237 ariadna 111
                // Obtener categoría
112
                $category = $DB->get_record('course_categories', ['id' => $course->category]);
1231 ariadna 113
 
1236 ariadna 114
                // Obtener un resumen del curso
115
                $summary = trim(strip_tags($course->summary));
116
                if (empty($summary)) {
117
                    $summary = '&nbsp';
118
                } elseif (strlen($summary) > 80) {
119
                    $summary = substr($summary, 0, 80) . '...';
1235 ariadna 120
                }
121
 
1236 ariadna 122
                // Asignar datos del curso al array de respuesta
123
                $data['course'] = [
124
                    'courseid' => $course->id,
125
                    'coursecategory' => $category->name,
126
                    'courseimage' => $image,
127
                    'enddate' => $course->enddate,
128
                    'fullname' => $course->fullname,
129
                    'fullnamedisplay' => get_course_display_name_for_list($course),
130
                    'hasprogress' => true,
131
                    'hidden' => false,
132
                    'id' => $course->id,
133
                    'idnumber' => $course->idnumber,
134
                    'isfavourite' => false,
135
                    'progress' => $miProgreso,
136
                    'shortname' => $course->shortname,
137
                    'showshortname' => false,
138
                    'startdate' => $course->startdate,
139
                    'summary' => $summary,
140
                    'summaryformat' => $course->summaryformat,
141
                    'timeaccess' => $lastaccess,
142
                    'viewurl' => $CFG->wwwroot . '/course/view.php?id=' . $course->id,
143
                    'viewurlnew' => $linkurl,
144
                    'visible' => true,
145
                ];
1237 ariadna 146
            }
1 efrain 147
        }
1231 ariadna 148
 
1234 ariadna 149
        // Renderizar la plantilla con los datos del curso
1 efrain 150
        return $this->render_from_template('block_cesa_lastcourse/full', $data);
151
    }
152
}