Proyectos de Subversion Moodle

Rev

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