Proyectos de Subversion Moodle

Rev

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