Proyectos de Subversion Moodle

Rev

Rev 500 | Rev 509 | 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
 
6
global $CFG;
7
require_once $CFG->libdir  . '/externallib.php';
8
require_once $CFG->dirroot . '/course/classes/category.php';
9
require_once $CFG->dirroot . '/blocks/moodleblock.class.php';
10
require_once $CFG->dirroot . '/blocks/course_list/block_course_list.php';
11
 
12
 
13
class block_cursos_recientes_ajax_external extends \external_api
14
{
15
    public static function get_listado_de_cursos_recientes_parameters()
16
    {
17
        return new \external_function_parameters([
18
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
446 ariadna 19
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
1 efrain 20
        ]);
21
    }
22
 
23
    public static function get_listado_de_cursos_recientes($category_id, $search_text)
24
    {
25
 
26
        /*
27
        [
28
        'category_id' => $category_id,
29
        'search_text' => $search_text,
30
        ] = self::validate_parameters(self::execute_parameters(), [
31
            'category_id' => $category_id,
32
            'search_text' => $search_text
33
        ]);
34
        */
446 ariadna 35
 
1 efrain 36
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
446 ariadna 37
 
38
 
1 efrain 39
        $userid = $USER->id;
40
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
41
 
42
        $courses = [];
446 ariadna 43
 
1 efrain 44
        $mycourses = enrol_get_users_courses($userid);
446 ariadna 45
 
46
        foreach ($mycourses as $course) {
47
            if (!$course->visible) {
1 efrain 48
                continue;
49
            }
446 ariadna 50
 
51
 
52
            if ($search_text) {
53
                if (stripos($course->fullname, $search_text) === false) {
1 efrain 54
                    continue;
55
                }
56
            }
446 ariadna 57
 
58
            if ($category_id) {
59
                if ($category_id != $course->category) {
1 efrain 60
                    continue;
61
                }
62
            }
446 ariadna 63
 
64
            $category = $DB->get_record('course_categories', array('id' => $course->category, 'visible' => 1));
65
            if (!$category) {
1 efrain 66
                continue;
67
            }
68
 
446 ariadna 69
 
1 efrain 70
            if ($course instanceof stdClass) {
71
                $courseInList = new core_course_list_element($course);
72
            }
73
            $image = $url_noimage;
446 ariadna 74
            foreach ($courseInList->get_course_overviewfiles() as $file) {
1 efrain 75
                $isimage = $file->is_valid_image();
76
                $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
77
                if (!$isimage) {
78
                    $image = $url_noimage;
79
                }
80
            }
446 ariadna 81
 
1 efrain 82
            $lastaccess = null;
83
            $sql = "select timecreated  from {logstore_standard_log} where courseid  = :courseid and userid  = :userid " .
84
                "  and eventname  = '\\\\core\\\\event\\\\course_viewed' order by id desc limit 1 ";
446 ariadna 85
 
1 efrain 86
            $timecreated = $DB->get_field_sql($sql,  array('courseid' => $course->id, 'userid' => $userid));
446 ariadna 87
            if ($timecreated) {
88
 
1 efrain 89
                $lastaccess = date('d/m/Y h:i a', $timecreated);
90
            }
91
 
446 ariadna 92
 
93
 
94
            $miProgreso = number_format(progress::get_course_progress_percentage($course), 2); // Progreso por curso
95
 
96
 
1 efrain 97
            $first_section = 0;
98
            $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
446 ariadna 99
 
100
            foreach ($sections as $section) {
101
                if (!empty($section->section)) {
1 efrain 102
                    $first_section = $section->id;
103
                    break;
104
                }
105
            }
106
            $course_context = context_course::instance($course->id);
107
            $roles = get_user_roles($course_context, $USER->id, true);
446 ariadna 108
 
1 efrain 109
            $completion_edit_curso = false;
110
            foreach ($roles as $role) {
111
                if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
112
                    $completion_edit_curso  = true;
113
                    break;
114
                }
115
            }
446 ariadna 116
 
1 efrain 117
            if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id) || $completion_edit_curso) {
446 ariadna 118
                $editurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id . '&notifyeditingon=1';
119
 
1 efrain 120
                //$editurl = $CFG->wwwroot . '/course/edit.php?id=' . $course->id;
121
            } else {
122
                $editurl = '';
123
            }
446 ariadna 124
 
125
 
126
 
1 efrain 127
            $modules = get_fast_modinfo($course->id)->get_cms();
446 ariadna 128
 
129
 
130
 
1 efrain 131
            $linkurl = '';
446 ariadna 132
            foreach ($modules as $module) {
133
 
134
 
135
 
136
 
1 efrain 137
                //print "!".$module->uservisible ."||". $module->is_stealth() ."||". $module->url ."||". $module->section."<br>";
138
                if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
139
                    continue;
140
                }
446 ariadna 141
 
142
                if ($module->section == $first_section || $completion_edit_curso) {
143
 
144
 
145
 
146
 
147
 
1 efrain 148
                    $linkurl =  new moodle_url($module->url, array('forceview' => 1));
149
                    //print_r($linkurl);
150
                    //var_dump($linkurl);
151
                    break;
152
                }
153
            }
154
            /*
155
            if( $completion_edit_curso){
156
                $linkurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
157
            }*/
446 ariadna 158
 
1 efrain 159
            $summary = trim(strip_tags($course->summary));
446 ariadna 160
 
161
            if (empty($summary)) {
1 efrain 162
                $summary = `<p></p>`;
446 ariadna 163
            } else if (strlen($summary) > 80) {
164
 
1 efrain 165
                $summary =  substr($summary, 0, 80) . '...';
446 ariadna 166
            }
167
 
498 ariadna 168
            array_push($courses, [
446 ariadna 169
 
1 efrain 170
                'coursecategory' => $category->name,
171
                'courseimage' => $image,
172
                'enddate' => $course->enddate,
173
                'fullname' => $course->fullname,
174
                'fullnamedisplay' => get_course_display_name_for_list($course),
175
                'hasprogress' => true,
176
                'hidden' => false,
177
                'id' => $course->id,
178
                'idnumber' => $course->idnumber,
179
                'isfavourite' => false,
180
                'progress' => $miProgreso,
181
                'shortname' => $course->shortname,
182
                'showshortname' => false,
183
                'startdate' => $course->startdate,
184
                'summary' =>  $summary,
185
                'summaryformat' => $course->summaryformat,
186
                'timeaccess' => $lastaccess,
187
                'viewurl' => $linkurl,
188
                'editurl' => $editurl,
189
                'visible' => true,
446 ariadna 190
 
498 ariadna 191
            ]);
1 efrain 192
        }
446 ariadna 193
 
1 efrain 194
        $data = [
446 ariadna 195
            'courses' => $courses
196
        ];
197
 
1 efrain 198
        $cards = $OUTPUT->render_from_template('block_cursos_recientes_ajax/cards', $data);
199
 
446 ariadna 200
 
1 efrain 201
        return json_encode(['success' => true, 'search_text' => $search_text, 'category_id' => $category_id,  'cards' => $cards]);
202
    }
203
 
204
    public static function get_listado_de_cursos_recientes_returns()
205
    {
206
        return new \external_value(PARAM_RAW, 'The updated JSON output');
207
    }
446 ariadna 208
}