Proyectos de Subversion Moodle

Rev

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