Proyectos de Subversion Moodle

Rev

Rev 1058 | Rev 1060 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1058 Rev 1059
Línea 19... Línea 19...
19
        return new \external_function_parameters([
19
        return new \external_function_parameters([
20
            'courseid' => new \external_value(PARAM_INT, 'ID del curso', VALUE_DEFAULT, 0),
20
            'courseid' => new \external_value(PARAM_INT, 'ID del curso', VALUE_DEFAULT, 0),
21
        ]);
21
        ]);
22
    }
22
    }
Línea -... Línea 23...
-
 
23
 
-
 
24
 
23
 
25
 
24
    public static function enrolar_usuario_cursos_catalogo($courseid)
26
    public static function enrolar_usuario_cursos_catalogo($courseid)
25
    {
27
    {
Línea 79... Línea 81...
79
 
81
 
80
            return json_encode(['success' => false, 'data' => $e->getMessage()]);
82
            return json_encode(['success' => false, 'data' => $e->getMessage()]);
81
        }
83
        }
Línea 82... Línea -...
82
    }
-
 
83
 
84
    }
84
 
85
 
85
    public static function enrolar_usuario_cursos_catalogo_returns()
86
    public static function enrolar_usuario_cursos_catalogo_returns()
86
    {
87
    {
Línea 87... Línea -...
87
        return new \external_value(PARAM_RAW, 'The updated JSON output');
-
 
88
    }
-
 
89
 
88
        return new \external_value(PARAM_RAW, 'The updated JSON output');
90
 
89
    }
91
 
90
 
92
    public static function get_cursos_catalogo_parameters()
91
    public static function get_cursos_catalogo_parameters()
93
    {
92
    {
-
 
93
        return new \external_function_parameters([
-
 
94
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
94
        return new \external_function_parameters([
95
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de búsqueda', VALUE_DEFAULT, ''),
95
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
96
            'page' => new \external_value(PARAM_INT, 'Número de página', VALUE_DEFAULT, 1),
Línea 96... Línea 97...
96
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
97
            'perpage' => new \external_value(PARAM_INT, 'Cursos por página', VALUE_DEFAULT, 10),
97
        ]);
98
        ]);
-
 
99
    }
Línea 98... Línea -...
98
    }
-
 
-
 
100
 
99
 
101
    public static function get_cursos_catalogo($category_id, $search_text, $page, $perpage)
-
 
102
    {
-
 
103
        global $USER, $DB, $CFG, $PAGE;
100
    public static function get_cursos_catalogo($category_id, $search_text)
104
 
101
    {
105
        // Verificar parámetros
-
 
106
        $params = self::validate_parameters(
-
 
107
            self::get_cursos_catalogo_parameters(),
-
 
108
            compact('category_id', 'search_text', 'page', 'perpage')
-
 
109
        );
Línea 102... Línea 110...
102
 
110
 
103
 
111
        // Validar valores para la paginación
104
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
-
 
105
 
-
 
106
 
-
 
107
 
-
 
108
        $userid = $USER->id;
-
 
109
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
-
 
110
 
-
 
111
        $is_admin = false;
-
 
112
        $admins = get_admins();
-
 
113
        foreach ($admins as $admin) {
-
 
114
            if ($USER->id == $admin->id) {
-
 
115
                $is_admin = true;
-
 
116
                break;
-
 
117
            }
-
 
118
        }
-
 
119
 
-
 
120
        $all_categories = [];
-
 
121
        $records = $DB->get_records('course_categories', ['visible' => 1], 'sortorder, name');
-
 
122
        foreach ($records as $record) {
-
 
123
            $all_categories[$record->id] = $record;
-
 
124
        }
-
 
125
 
-
 
126
 
-
 
127
        $categories_ids = [];
-
 
128
        $category_filter_active = false;
-
 
129
        if ($category_id) {
-
 
130
            $category_filter_active = true;
-
 
131
 
-
 
132
            $recordLevel0 = $DB->get_record('course_categories', ['id' => $category_id]);
-
 
133
 
-
 
134
 
-
 
135
            if ($recordLevel0) {
-
 
136
                array_push($categories_ids, $recordLevel0->id);
-
 
137
 
-
 
138
 
-
 
139
 
-
 
140
                $recordsLevel1 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel0->id]);
-
 
141
 
-
 
142
                foreach ($recordsLevel1 as $recordLevel1) {
-
 
143
 
-
 
144
                    array_push($categories_ids, $recordLevel1->id);
-
 
145
 
-
 
146
                    $recordsLevel2 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel1->id]);
-
 
147
                    foreach ($recordsLevel2 as $recordLevel2) {
-
 
148
                        array_push($categories_ids, $recordLevel2->id);
-
 
149
 
-
 
150
                        $recordsLevel3 = $DB->get_records('course_categories', ['visible' => 2, 'parent' => $recordLevel2->id]);
-
 
151
                        foreach ($recordsLevel3 as $recordLevel3) {
-
 
152
                            array_push($categories_ids, $recordLevel3->id);
-
 
153
                        }
-
 
154
                    }
-
 
155
                }
-
 
156
            }
-
 
157
        }
-
 
Línea -... Línea 112...
-
 
112
        $page = max(1, $params['page']);
-
 
113
        $perpage = max(1, $params['perpage']);
-
 
114
        $offset = ($page - 1) * $perpage;
-
 
115
 
Línea 158... Línea 116...
158
 
116
        $userid = $USER->id;
159
 
117
        $url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
160
 
-
 
161
        $course_ids                 = [];
118
 
162
        $course_category_ids        = [];
119
        // Obtener todos los cursos según los filtros
Línea 163... Línea -...
163
        $courseAvailableForAutoRol  = [];
-
 
164
 
-
 
165
 
-
 
166
        if ($is_admin) {
-
 
167
            $mycourses = get_courses();
-
 
168
        } else {
-
 
169
            $mycourses  = enrol_get_users_courses($USER->id);
-
 
170
        }
-
 
171
 
-
 
172
 
-
 
173
 
-
 
174
        foreach ($mycourses as $course) {
-
 
175
            if (!$course->visible) {
-
 
176
                continue;
-
 
177
            }
120
        $courses_query = "SELECT * FROM {course} WHERE visible = 1";
178
 
121
        $conditions = [];
179
            if ($search_text) {
-
 
180
                if (stripos($course->fullname, $search_text) === false) {
-
 
181
                    continue;
-
 
182
                }
-
 
183
            }
-
 
184
 
122
        $params_query = [];
185
 
123
 
Línea 186... Línea -...
186
            if ($category_filter_active) {
-
 
187
                if (!in_array($course->category, $categories_ids)) {
-
 
188
                    continue;
-
 
189
                }
124
        if (!empty($search_text)) {
190
            }
-
 
191
 
-
 
192
 
-
 
193
            array_push($course_ids, $course->id);
-
 
194
        }
-
 
195
 
-
 
196
        $courses_with_autoenrol  = $DB->get_records('enrol', ['enrol' => 'self', 'status' => 0]);
-
 
197
        foreach ($courses_with_autoenrol as $course_with_autoenrol) {
-
 
198
            $course = get_course($course_with_autoenrol->courseid);
-
 
199
            if (!$course->visible) {
-
 
200
                continue;
-
 
201
            }
-
 
202
 
-
 
203
            if ($search_text) {
-
 
204
                if (stripos($course_with_autoenrol->fullname, $search_text) === false) {
-
 
205
                    continue;
-
 
206
                }
125
            $conditions[] = "fullname LIKE :searchtext";
207
            }
126
            $params_query['searchtext'] = '%' . $DB->sql_like_escape($search_text) . '%';
Línea -... Línea 127...
-
 
127
        }
-
 
128
 
-
 
129
        if ($category_id > 0) {
-
 
130
            $conditions[] = "category = :categoryid";
-
 
131
            $params_query['categoryid'] = $category_id;
Línea -... Línea 132...
-
 
132
        }
-
 
133
 
-
 
134
        if (!empty($conditions)) {
-
 
135
            $courses_query .= " AND " . implode(" AND ", $conditions);
Línea -... Línea 136...
-
 
136
        }
-
 
137
 
Línea 208... Línea -...
208
 
-
 
209
            if ($category_filter_active) {
-
 
210
                if (!in_array($course->category, $categories_ids)) {
138
        // Contar todos los cursos que coinciden con los filtros
211
                    continue;
139
        $total_courses = $DB->count_records_sql(
212
                }
-
 
213
            }
-
 
214
 
-
 
215
            array_push($course_ids, $course->id);
-
 
216
            array_push($courseAvailableForAutoRol, $course->id);
-
 
217
        }
-
 
218
 
-
 
219
 
-
 
220
 
-
 
221
 
-
 
222
 
-
 
223
 
-
 
224
        $courses = [];
-
 
225
        foreach ($course_ids as $course_id) {
-
 
226
            $course = get_course($course_id);
-
 
227
 
-
 
228
            if ($course instanceof stdClass) {
-
 
229
                $coreCourseList = new core_course_list_element($course);
-
 
230
            }
-
 
231
 
-
 
232
            if (!in_array($course->category, $course_category_ids)) {
-
 
233
 
-
 
234
 
-
 
235
                array_push($course_category_ids, $course->category);
-
 
236
            }
-
 
237
 
-
 
238
 
-
 
239
            $image = $url_noimage;
-
 
240
            foreach ($coreCourseList->get_course_overviewfiles() as $file) {
-
 
241
                $isimage = $file->is_valid_image();
-
 
242
                $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
-
 
243
                if (!$isimage) {
-
 
244
                    $image = $url_noimage;
140
            "SELECT COUNT(*) FROM {course} WHERE visible = 1" . (count($conditions) ? " AND " . implode(" AND ", $conditions) : ""),
245
                }
-
 
246
            }
-
 
247
 
-
 
248
            $lastaccess = null;
-
 
249
            $sql = "select timecreated  from {logstore_standard_log} where courseid  = :courseid and userid  = :userid " .
-
 
250
                "order by id desc limit 1 ";
-
 
251
 
-
 
252
            $timecreated = $DB->get_field_sql($sql,  array('courseid' => $course->id, 'userid' => $userid));
-
 
253
            if ($timecreated) {
-
 
254
 
-
 
255
                $lastaccess = date('d/m/Y h:i a', $timecreated);
-
 
256
            }
-
 
257
 
-
 
258
            $first_section = 0;
-
 
259
            $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
-
 
260
 
-
 
261
            foreach ($sections as $section) {
-
 
262
                if (!empty($section->section)) {
-
 
263
                    $first_section = $section->id;
-
 
264
                    break;
-
 
265
                }
-
 
266
            }
-
 
267
 
-
 
268
            $course_context = context_course::instance($course->id);
-
 
269
            $roles = get_user_roles($course_context, $USER->id, true);
-
 
270
 
-
 
271
            $completion_edit_curso = false;
-
 
272
            foreach ($roles as $role) {
-
 
273
                if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
-
 
274
                    $completion_edit_curso  = true;
-
 
275
                    break;
-
 
276
                }
-
 
277
            }
-
 
278
 
-
 
279
            if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id) || $completion_edit_curso) {
-
 
280
                $editurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id . '&notifyeditingon=1';
-
 
281
            } else {
-
 
282
                $editurl = '';
-
 
283
            }
-
 
284
 
-
 
285
 
-
 
286
            $modules = get_fast_modinfo($course->id)->get_cms();
-
 
287
 
-
 
288
            $linkurl = '';
-
 
289
            foreach ($modules as $module) {
-
 
290
                if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
-
 
291
                    continue;
-
 
292
                }
-
 
293
 
-
 
294
                if ($module->section == $first_section || $completion_edit_curso) {
-
 
295
                    $linkurl =  new moodle_url($module->url, array('forceview' => 1));
-
 
296
                    break;
-
 
297
                }
-
 
298
            }
-
 
299
 
-
 
300
            $summary = trim(strip_tags($course->summary));
-
 
301
 
-
 
302
            if (empty($summary)) {
-
 
303
                $summary = '<p></p>';
-
 
304
            } else if (strlen($summary) > 80) {
-
 
305
 
-
 
306
                $summary =  substr($summary, 0, 80) . '...';
-
 
307
            }
-
 
308
 
-
 
309
            if (empty($editurl) && in_array($course->id, $courseAvailableForAutoRol)) {
-
 
310
                $autoenrol = 'yes';
-
 
311
            } else {
-
 
312
                $autoenrol = '';
-
 
313
            }
-
 
314
 
-
 
315
 
-
 
316
            $category = $all_categories[$course->category];
-
 
317
            if ($category->parent) {
-
 
318
                $categoryParent = $all_categories[$category->parent];
-
 
319
                if ($categoryParent) {
-
 
320
                    $categoryName = $categoryParent->name . ' / ' . $category->name;
-
 
321
                } else {
-
 
322
                    $categoryName = $category->name;
-
 
323
                }
-
 
324
            } else {
-
 
325
                $categoryName =  $category->name;
-
 
326
            }
-
 
327
            if (strlen($categoryName) > 40) {
-
 
328
 
-
 
329
                $categoryName =  substr($categoryName, 0, 40) . '...';
-
 
330
            }
-
 
331
 
-
 
332
 
141
            $params_query
333
            // echo '<pre>';
142
        );
334
            // echo '$categoryName = ' . $categoryName . ' $course->category = ' . $course->category;
-
 
335
            // echo '</pre>';
143
 
336
 
144
        // Agregar límites para la paginación
337
 
145
        $courses_query .= " ORDER BY fullname ASC LIMIT :perpage OFFSET :offset";
338
            array_push($courses, [
-
 
339
                'courseid' => $course->id,
146
        $params_query['perpage'] = $perpage;
340
                'coursecategory' => $categoryName,
-
 
341
                'courseimage' => $image,
-
 
342
                'enddate' => $course->enddate,
-
 
343
                'fullname' => $course->fullname,
-
 
344
                'fullnamedisplay' => get_course_display_name_for_list($course),
-
 
345
                'hidden' => false,
147
        $params_query['offset'] = $offset;
346
                'id' => $course->id,
-
 
347
                'idnumber' => $course->idnumber,
-
 
348
                'isfavourite' => false,
-
 
349
                'shortname' => $course->shortname,
-
 
350
                'showshortname' => false,
-
 
351
                'startdate' => $course->startdate,
-
 
352
                'summary' =>  $summary,
-
 
353
                'summaryformat' => $course->summaryformat,
-
 
354
                'timeaccess' => $lastaccess,
-
 
355
                'viewurl' => $linkurl,
-
 
356
                'editurl' => $editurl,
-
 
357
                'autoenrol' => $autoenrol,
-
 
358
                'visible' => true,
-
 
359
            ]);
-
 
360
        }
-
 
361
 
148
 
Línea -... Línea 149...
-
 
149
        // Obtener los cursos
-
 
150
        $courses = $DB->get_records_sql($courses_query, $params_query);
Línea 362... Línea -...
362
        usort($courses, function ($a, $b) {
-
 
363
            return $a['fullname'] <=> $b['fullname'];
-
 
364
        });
-
 
365
 
151
 
366
 
152
        $results = [];
-
 
153
        foreach ($courses as $course) {
-
 
154
            $results[] = [
-
 
155
                'id' => $course->id,
-
 
156
                'fullname' => $course->fullname,
-
 
157
                'shortname' => $course->shortname,
-
 
158
                'category' => $course->category,
367
 
159
                'startdate' => $course->startdate,
368
 
-
 
369
        $categories = [];
-
 
370
        foreach ($course_category_ids as $course_category_id) {
-
 
371
            $category = $all_categories[$course_category_id];
-
 
372
            if ($category) {
-
 
373
                array_push($categories, ['id' => $category->id, 'name' => $category->name]);
-
 
374
            }
-
 
375
        }
-
 
376
 
-
 
377
 
-
 
378
 
-
 
379
 
160
                'enddate' => $course->enddate,
Línea 380... Línea -...
380
 
-
 
381
        $data = [
161
            ];
382
            'courses' => $courses
162
        }
-
 
163
 
-
 
164
        // Calcular páginas totales
-
 
165
        $total_pages = ceil($total_courses / $perpage);
383
        ];
166
 
-
 
167
        return [
-
 
168
            'courses' => $results,
-
 
169
            'pagination' => [
-
 
170
                'total_courses' => $total_courses,
-
 
171
                'total_pages' => $total_pages,
-
 
172
                'current_page' => $page,
-
 
173
                'per_page' => $perpage,
-
 
174
            ],
-
 
175
        ];
-
 
176
    }
-
 
177
 
-
 
178
    public static function get_cursos_catalogo_returns()
-
 
179
    {
-
 
180
        return new \external_single_structure([
384
 
181
            'courses' => new \external_multiple_structure(
385
        $cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);
182
                new \external_single_structure([