Proyectos de Subversion Moodle

Rev

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