Proyectos de Subversion Moodle

Rev

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