Proyectos de Subversion Moodle

Rev

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

Rev 1059 Rev 1060
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 23... Línea -...
23
 
-
 
24
 
-
 
25
 
23
 
26
    public static function enrolar_usuario_cursos_catalogo($courseid)
24
    public static function enrolar_usuario_cursos_catalogo($courseid)
27
    {
25
    {
Línea 81... Línea 79...
81
 
79
 
82
            return json_encode(['success' => false, 'data' => $e->getMessage()]);
80
            return json_encode(['success' => false, 'data' => $e->getMessage()]);
83
        }
81
        }
Línea -... Línea 82...
-
 
82
    }
84
    }
83
 
85
 
84
 
86
    public static function enrolar_usuario_cursos_catalogo_returns()
85
    public static function enrolar_usuario_cursos_catalogo_returns()
87
    {
86
    {
Línea -... Línea 87...
-
 
87
        return new \external_value(PARAM_RAW, 'The updated JSON output');
-
 
88
    }
88
        return new \external_value(PARAM_RAW, 'The updated JSON output');
89
 
89
    }
90
 
90
 
91
 
91
    public static function get_cursos_catalogo_parameters()
92
    public static function get_cursos_catalogo_parameters()
92
    {
93
    {
93
        return new \external_function_parameters([
-
 
94
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
-
 
95
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de búsqueda', VALUE_DEFAULT, ''),
94
        return new \external_function_parameters([
96
            'page' => new \external_value(PARAM_INT, 'Número de página', VALUE_DEFAULT, 1),
95
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
Línea 97... Línea 96...
97
            'perpage' => new \external_value(PARAM_INT, 'Cursos por página', VALUE_DEFAULT, 10),
96
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
98
        ]);
97
        ]);
99
    }
-
 
Línea 100... Línea -...
100
 
-
 
-
 
98
    }
101
    public static function get_cursos_catalogo($category_id, $search_text, $page, $perpage)
99
 
102
    {
-
 
103
        global $USER, $DB, $CFG, $PAGE;
-
 
104
 
100
    public static function get_cursos_catalogo($category_id, $search_text)
105
        // Verificar parámetros
101
    {
106
        $params = self::validate_parameters(
-
 
107
            self::get_cursos_catalogo_parameters(),
-
 
108
            compact('category_id', 'search_text', 'page', 'perpage')
-
 
109
        );
-
 
Línea 110... Línea 102...
110
 
102
 
111
        // Validar valores para la paginación
103
 
Línea -... Línea 104...
-
 
104
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
-
 
105
 
112
        $page = max(1, $params['page']);
106
 
113
        $perpage = max(1, $params['perpage']);
107
 
114
        $offset = ($page - 1) * $perpage;
108
        $userid = $USER->id;
115
 
109
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
-
 
110
 
-
 
111
        $is_admin = false;
Línea 116... Línea 112...
116
        $userid = $USER->id;
112
        $admins = get_admins();
-
 
113
        foreach ($admins as $admin) {
117
        $url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
114
            if ($USER->id == $admin->id) {
118
 
115
                $is_admin = true;
119
        // Obtener todos los cursos según los filtros
116
                break;
Línea -... Línea 117...
-
 
117
            }
-
 
118
        }
-
 
119
 
120
        $courses_query = "SELECT * FROM {course} WHERE visible = 1";
120
        $all_categories = [];
121
        $conditions = [];
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
 
122
        $params_query = [];
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) {
123
 
148
                        array_push($categories_ids, $recordLevel2->id);
Línea -... Línea 149...
-
 
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
                }
124
        if (!empty($search_text)) {
156
            }
-
 
157
        }
-
 
158
 
125
            $conditions[] = "fullname LIKE :searchtext";
159
 
126
            $params_query['searchtext'] = '%' . $DB->sql_like_escape($search_text) . '%';
160
 
Línea 127... Línea -...
127
        }
-
 
128
 
-
 
129
        if ($category_id > 0) {
-
 
130
            $conditions[] = "category = :categoryid";
-
 
131
            $params_query['categoryid'] = $category_id;
-
 
Línea 132... Línea -...
132
        }
-
 
133
 
-
 
134
        if (!empty($conditions)) {
-
 
135
            $courses_query .= " AND " . implode(" AND ", $conditions);
-
 
Línea 136... Línea 161...
136
        }
161
        $course_ids                 = [];
137
 
162
        $course_category_ids        = [];
-
 
163
        $courseAvailableForAutoRol  = [];
-
 
164
 
Línea -... Línea 165...
-
 
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
            }
-
 
178
 
-
 
179
            if ($search_text) {
-
 
180
                if (stripos($course->fullname, $search_text) === false) {
-
 
181
                    continue;
-
 
182
                }
-
 
183
            }
-
 
184
 
-
 
185
 
-
 
186
            if ($category_filter_active) {
-
 
187
                if (!in_array($course->category, $categories_ids)) {
-
 
188
                    continue;
-
 
189
                }
-
 
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
                }
-
 
207
            }
-
 
208
 
-
 
209
            if ($category_filter_active) {
138
        // Contar todos los cursos que coinciden con los filtros
210
                if (!in_array($course->category, $categories_ids)) {
139
        $total_courses = $DB->count_records_sql(
211
                    continue;
-
 
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) {
140
            "SELECT COUNT(*) FROM {course} WHERE visible = 1" . (count($conditions) ? " AND " . implode(" AND ", $conditions) : ""),
244
                    $image = $url_noimage;
-
 
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 {
141
            $params_query
325
                $categoryName =  $category->name;
-
 
326
            }
-
 
327
            if (strlen($categoryName) > 40) {
-
 
328
 
142
        );
329
                $categoryName =  substr($categoryName, 0, 40) . '...';
-
 
330
            }
-
 
331
 
-
 
332
 
-
 
333
            // echo '<pre>';
-
 
334
            // echo '$categoryName = ' . $categoryName . ' $course->category = ' . $course->category;
143
 
335
            // echo '</pre>';
144
        // Agregar límites para la paginación
336
 
145
        $courses_query .= " ORDER BY fullname ASC LIMIT :perpage OFFSET :offset";
337
 
-
 
338
            array_push($courses, [
146
        $params_query['perpage'] = $perpage;
339
                'courseid' => $course->id,
-
 
340
                'coursecategory' => $categoryName,
-
 
341
                'courseimage' => $image,
-
 
342
                'enddate' => $course->enddate,
-
 
343
                'fullname' => $course->fullname,
-
 
344
                'fullnamedisplay' => get_course_display_name_for_list($course),
147
        $params_query['offset'] = $offset;
345
                'hidden' => false,
148
 
346
                'id' => $course->id,
Línea 149... Línea 347...
149
        // Obtener los cursos
347
                'idnumber' => $course->idnumber,
150
        $courses = $DB->get_records_sql($courses_query, $params_query);
348
                'isfavourite' => false,
-
 
349
                'shortname' => $course->shortname,
Línea -... Línea 350...
-
 
350
                'showshortname' => false,
-
 
351
                'startdate' => $course->startdate,
-
 
352
                'summary' =>  $summary,
151
 
353
                'summaryformat' => $course->summaryformat,
152
        $results = [];
354
                'timeaccess' => $lastaccess,
153
        foreach ($courses as $course) {
355
                'viewurl' => $linkurl,
154
            $results[] = [
356
                'editurl' => $editurl,
155
                'id' => $course->id,
357
                'autoenrol' => $autoenrol,
156
                'fullname' => $course->fullname,
358
                'visible' => true,
157
                'shortname' => $course->shortname,
359
            ]);
-
 
360
        }
-
 
361
 
-
 
362
        usort($courses, function ($a, $b) {
-
 
363
            return $a['fullname'] <=> $b['fullname'];
-
 
364
        });
158
                'category' => $course->category,
365
 
-
 
366
 
159
                'startdate' => $course->startdate,
367
 
-
 
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
 
160
                'enddate' => $course->enddate,
379
 
Línea -... Línea 380...
-
 
380
 
161
            ];
381
        $data = [
162
        }
382
            'courses' => $courses
163
 
-
 
164
        // Calcular páginas totales
-
 
165
        $total_pages = ceil($total_courses / $perpage);
-
 
166
 
383
        ];
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([
-
 
181
            'courses' => new \external_multiple_structure(
384
 
182
                new \external_single_structure([
385
        $cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);