| 1453 |
ariadna |
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_catalogo_ajax_external extends \external_api
|
|
|
13 |
{
|
|
|
14 |
|
|
|
15 |
public static function enrolar_usuario_cursos_catalogo_parameters()
|
|
|
16 |
{
|
|
|
17 |
return new \external_function_parameters([
|
|
|
18 |
'courseid' => new \external_value(PARAM_INT, 'ID del curso', VALUE_DEFAULT, 0)
|
|
|
19 |
]);
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
public static function enrolar_usuario_cursos_catalogo($courseid)
|
|
|
23 |
{
|
|
|
24 |
global $USER, $DB, $CFG;
|
|
|
25 |
|
|
|
26 |
$userid = $USER->id;
|
|
|
27 |
$course = get_course($courseid);
|
|
|
28 |
$enrol = false;
|
|
|
29 |
|
|
|
30 |
if (! $course) {
|
|
|
31 |
return json_encode([
|
|
|
32 |
'success' => false,
|
|
|
33 |
'data' => 'No existe el curso'
|
|
|
34 |
]);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
$enrolmethod = 'self';
|
|
|
38 |
|
|
|
39 |
$instance = $DB->get_record('enrol', [
|
|
|
40 |
'courseid' => $course->id,
|
|
|
41 |
'enrol' => $enrolmethod,
|
|
|
42 |
'status' => 0
|
|
|
43 |
]);
|
|
|
44 |
if (! $instance) {
|
|
|
45 |
return json_encode([
|
|
|
46 |
'success' => false,
|
|
|
47 |
'data' => 'El curso no permite auto inscribirse'
|
|
|
48 |
]);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
try {
|
|
|
52 |
$user = $DB->get_record('user', [
|
|
|
53 |
'id' => $userid
|
|
|
54 |
]);
|
|
|
55 |
$role = $DB->get_record('role', array(
|
|
|
56 |
'archetype' => 'student'
|
|
|
57 |
));
|
|
|
58 |
$context = context_course::instance($course->id);
|
|
|
59 |
|
|
|
60 |
$context = context_course::instance($course->id);
|
|
|
61 |
if (is_enrolled($context, $user)) {
|
|
|
62 |
return json_encode([
|
|
|
63 |
'success' => true
|
|
|
64 |
]);
|
|
|
65 |
} else {
|
|
|
66 |
$enrol = enrol_get_plugin($enrolmethod);
|
|
|
67 |
if ($enrol === null) {
|
|
|
68 |
return false;
|
|
|
69 |
}
|
|
|
70 |
$instances = enrol_get_instances($course->id, true);
|
|
|
71 |
$manualinstance = null;
|
|
|
72 |
foreach ($instances as $instance) {
|
|
|
73 |
if ($instance->name == $enrolmethod) {
|
|
|
74 |
$manualinstance = $instance;
|
|
|
75 |
break;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
if ($manualinstance !== null) {
|
|
|
79 |
$instanceid = $enrol->add_default_instance($course);
|
|
|
80 |
if ($instanceid === null) {
|
|
|
81 |
$instanceid = $enrol->add_instance($course);
|
|
|
82 |
}
|
|
|
83 |
$instance = $DB->get_record('enrol', array(
|
|
|
84 |
'id' => $instanceid
|
|
|
85 |
));
|
|
|
86 |
}
|
|
|
87 |
$enrol->enrol_user($instance, $user->id, $role->id);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return json_encode([
|
|
|
91 |
'success' => true
|
|
|
92 |
]);
|
|
|
93 |
} catch (\Exception $e) {
|
|
|
94 |
error_log($e->getTraceAsString());
|
|
|
95 |
|
|
|
96 |
return json_encode([
|
|
|
97 |
'success' => false,
|
|
|
98 |
'data' => $e->getMessage()
|
|
|
99 |
]);
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public static function enrolar_usuario_cursos_catalogo_returns()
|
|
|
104 |
{
|
|
|
105 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public static function get_cursos_catalogo_parameters()
|
|
|
109 |
{
|
|
|
110 |
return new \external_function_parameters([
|
|
|
111 |
'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
|
|
|
112 |
'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
|
|
|
113 |
'page' => new \external_value(PARAM_INT, 'Número de página', VALUE_DEFAULT, 1),
|
|
|
114 |
'perpage' => new \external_value(PARAM_INT, 'Cursos por página', VALUE_DEFAULT, 10)
|
|
|
115 |
]);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public static function get_cursos_catalogo($category_id, $search_text, $page = 1, $perpage = 10)
|
|
|
119 |
{
|
|
|
120 |
global $USER, $DB, $CFG, $OUTPUT, $PAGE;
|
|
|
121 |
|
|
|
122 |
$params = self::validate_parameters(self::get_cursos_catalogo_parameters(), compact('category_id', 'search_text', 'page', 'perpage'));
|
|
|
123 |
|
|
|
124 |
$page = max(1, $params['page']);
|
|
|
125 |
$perpage = max(10, $params['perpage']);
|
|
|
126 |
|
|
|
127 |
$userid = $USER->id;
|
|
|
128 |
$url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
|
|
|
129 |
|
|
|
130 |
$is_admin = false;
|
|
|
131 |
$admins = get_admins();
|
|
|
132 |
foreach ($admins as $admin) {
|
|
|
133 |
if ($USER->id == $admin->id) {
|
|
|
134 |
$is_admin = true;
|
|
|
135 |
break;
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
$all_categories = [];
|
|
|
140 |
$records = $DB->get_records('course_categories', [
|
|
|
141 |
'visible' => 1
|
|
|
142 |
], 'sortorder, name');
|
|
|
143 |
foreach ($records as $record) {
|
|
|
144 |
$all_categories[$record->id] = $record;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$categories_ids = [];
|
|
|
148 |
$category_filter_active = false;
|
|
|
149 |
if ($category_id) {
|
|
|
150 |
$category_filter_active = true;
|
|
|
151 |
|
|
|
152 |
$recordLevel0 = $DB->get_record('course_categories', [
|
|
|
153 |
'id' => $category_id
|
|
|
154 |
]);
|
|
|
155 |
|
|
|
156 |
if ($recordLevel0) {
|
|
|
157 |
array_push($categories_ids, $recordLevel0->id);
|
|
|
158 |
|
|
|
159 |
$recordsLevel1 = $DB->get_records('course_categories', [
|
|
|
160 |
'visible' => 1,
|
|
|
161 |
'parent' => $recordLevel0->id
|
|
|
162 |
]);
|
|
|
163 |
|
|
|
164 |
foreach ($recordsLevel1 as $recordLevel1) {
|
|
|
165 |
|
|
|
166 |
array_push($categories_ids, $recordLevel1->id);
|
|
|
167 |
|
|
|
168 |
$recordsLevel2 = $DB->get_records('course_categories', [
|
|
|
169 |
'visible' => 1,
|
|
|
170 |
'parent' => $recordLevel1->id
|
|
|
171 |
]);
|
|
|
172 |
foreach ($recordsLevel2 as $recordLevel2) {
|
|
|
173 |
array_push($categories_ids, $recordLevel2->id);
|
|
|
174 |
|
|
|
175 |
$recordsLevel3 = $DB->get_records('course_categories', [
|
|
|
176 |
'visible' => 2,
|
|
|
177 |
'parent' => $recordLevel2->id
|
|
|
178 |
]);
|
|
|
179 |
foreach ($recordsLevel3 as $recordLevel3) {
|
|
|
180 |
array_push($categories_ids, $recordLevel3->id);
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
$course_ids = [];
|
|
|
188 |
$course_category_ids = [];
|
|
|
189 |
$courseAvailableForAutoRol = [];
|
|
|
190 |
|
|
|
191 |
if ($is_admin) {
|
|
|
192 |
$mycourses = get_courses(); // get_courses_search($searchterms, $sort, $page, $perpage, $total_courses);
|
|
|
193 |
} else {
|
|
|
194 |
$mycourses = enrol_get_users_courses($USER->id);
|
|
|
195 |
$courses_with_autoenrol = $DB->get_records('enrol', [
|
|
|
196 |
'enrol' => 'self',
|
|
|
197 |
'status' => 0
|
|
|
198 |
]);
|
|
|
199 |
foreach ($courses_with_autoenrol as $course_with_autoenrol) {
|
|
|
200 |
$course = get_course($course_with_autoenrol->courseid);
|
|
|
201 |
array_push($mycourses, $course);
|
|
|
202 |
|
|
|
203 |
array_push($courseAvailableForAutoRol, $course->id);
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$mycourses = array_filter($mycourses, function ($course) use ($search_text, $category_filter_active, $categories_ids) {
|
|
|
208 |
if (! $course->visible) {
|
|
|
209 |
return false;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
if ($search_text) {
|
|
|
213 |
if (stripos($course->fullname, $search_text) === false) {
|
|
|
214 |
return false;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if ($category_filter_active) {
|
|
|
219 |
if (! in_array($course->category, $categories_ids)) {
|
|
|
220 |
return false;
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
return true;
|
|
|
225 |
});
|
|
|
226 |
|
|
|
227 |
usort($mycourses, function ($a, $b) {
|
|
|
228 |
|
|
|
229 |
if ($a->timecreated == $b->timecreated) {
|
|
|
230 |
return $a->fullname <=> $b->fullname;
|
|
|
231 |
} else {
|
|
|
232 |
return $a->timecreated > $b->timecreated ? -1 : 1;
|
|
|
233 |
}
|
|
|
234 |
});
|
|
|
235 |
|
|
|
236 |
/*
|
|
|
237 |
* $searchterms = array();
|
|
|
238 |
* $sort = "c.sortorder DESC";
|
|
|
239 |
*/
|
|
|
240 |
|
|
|
241 |
$total_courses = count($mycourses);
|
|
|
242 |
$total_pages = ceil($total_courses / $perpage);
|
|
|
243 |
if ($total_pages * $perpage < $total_courses) {
|
|
|
244 |
$total_pages++;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
$page = isset($page) ? max(1, min($page, $total_pages)) : 1;
|
|
|
248 |
|
|
|
249 |
$max_links = 3;
|
|
|
250 |
$start_page = max(1, $page - floor($max_links / 2));
|
|
|
251 |
$end_page = min($total_pages, $start_page + $max_links - 1);
|
|
|
252 |
|
|
|
253 |
if ($end_page - $start_page + 1 < $max_links) {
|
|
|
254 |
$start_page = max(1, $end_page - $max_links + 1);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
$start = $page == 1 ? 0 : ($page - 1) * $perpage;
|
|
|
258 |
$mycourses = array_slice($mycourses, $start, $perpage);
|
|
|
259 |
|
|
|
260 |
$courses = [];
|
|
|
261 |
/*
|
|
|
262 |
* foreach ($course_ids as $course_id) {
|
|
|
263 |
* $course = get_course($course_id);
|
|
|
264 |
*/
|
|
|
265 |
|
|
|
266 |
foreach ($mycourses as $course) {
|
|
|
267 |
|
|
|
268 |
if ($course instanceof stdClass) {
|
|
|
269 |
$coreCourseList = new core_course_list_element($course);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
if (! in_array($course->category, $course_category_ids)) {
|
|
|
273 |
array_push($course_category_ids, $course->category);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
$image = $url_noimage;
|
|
|
277 |
foreach ($coreCourseList->get_course_overviewfiles() as $file) {
|
|
|
278 |
$isimage = $file->is_valid_image();
|
|
|
279 |
$image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), ! $isimage);
|
|
|
280 |
if (! $isimage) {
|
|
|
281 |
$image = $url_noimage;
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
$lastaccess = null;
|
|
|
286 |
$sql = "select timecreated from {logstore_standard_log} where courseid = :courseid and userid = :userid order by id limit {$perpage}";
|
|
|
287 |
$params_a = [
|
|
|
288 |
'courseid' => $course->id,
|
|
|
289 |
'userid' => $userid
|
|
|
290 |
];
|
|
|
291 |
$timecreated = $DB->get_field_sql($sql, $params_a);
|
|
|
292 |
if ($timecreated) {
|
|
|
293 |
|
|
|
294 |
$lastaccess = date('d/m/Y h:i a', $timecreated);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
$first_section = 0;
|
|
|
298 |
$sections = $DB->get_records('course_sections', [
|
|
|
299 |
'course' => $course->id
|
|
|
300 |
], 'section desc', 'id,name,section,sequence,visible');
|
|
|
301 |
|
|
|
302 |
foreach ($sections as $section) {
|
|
|
303 |
if (! empty($section->section)) {
|
|
|
304 |
$first_section = $section->id;
|
|
|
305 |
break;
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
$course_context = context_course::instance($course->id);
|
|
|
312 |
$roles = get_user_roles($course_context, $USER->id, true);
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id)) {
|
|
|
316 |
$editurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id . '¬ifyeditingon=1';
|
|
|
317 |
|
|
|
318 |
//$editurl = $CFG->wwwroot . '/course/edit.php?id=' . $course->id;
|
|
|
319 |
} else {
|
|
|
320 |
$editurl = '';
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
$modules = get_fast_modinfo($course->id)->get_cms();
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
$viewurl = '';
|
|
|
328 |
foreach ($modules as $module) {
|
|
|
329 |
if (!$module->uservisible || $module->is_stealth() || empty($module->url)) {
|
|
|
330 |
continue;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
$viewurl = new moodle_url($module->url, array(
|
|
|
334 |
'forceview' => 1
|
|
|
335 |
));
|
|
|
336 |
break;
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
$summary = trim(strip_tags($course->summary));
|
|
|
340 |
|
|
|
341 |
if (empty($summary)) {
|
|
|
342 |
$summary = ' ';
|
|
|
343 |
} else if (strlen($summary) > 80) {
|
|
|
344 |
|
|
|
345 |
$summary = substr($summary, 0, 80) . '...';
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
if (empty($editurl) && in_array($course->id, $courseAvailableForAutoRol)) {
|
|
|
349 |
$autoenrol = 'yes';
|
|
|
350 |
} else {
|
|
|
351 |
$autoenrol = '';
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
$category = $all_categories[$course->category];
|
|
|
355 |
if ($category->parent) {
|
|
|
356 |
$categoryParent = $all_categories[$category->parent];
|
|
|
357 |
if ($categoryParent) {
|
|
|
358 |
$categoryName = $categoryParent->name . ' / ' . $category->name;
|
|
|
359 |
} else {
|
|
|
360 |
$categoryName = $category->name;
|
|
|
361 |
}
|
|
|
362 |
} else {
|
|
|
363 |
$categoryName = $category->name;
|
|
|
364 |
}
|
|
|
365 |
if (strlen($categoryName) > 40) {
|
|
|
366 |
|
|
|
367 |
$categoryName = substr($categoryName, 0, 40) . '...';
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
// echo '<pre>';
|
|
|
371 |
// echo '$categoryName = ' . $categoryName . ' $course->category = ' . $course->category;
|
|
|
372 |
// echo '</pre>';
|
|
|
373 |
|
|
|
374 |
array_push($courses, [
|
|
|
375 |
'courseid' => $course->id,
|
|
|
376 |
'coursecategory' => $categoryName,
|
|
|
377 |
'courseimage' => $image,
|
|
|
378 |
'enddate' => $course->enddate,
|
|
|
379 |
'fullname' => $course->fullname,
|
|
|
380 |
'fullnamedisplay' => get_course_display_name_for_list($course),
|
|
|
381 |
'hidden' => false,
|
|
|
382 |
'id' => $course->id,
|
|
|
383 |
'idnumber' => $course->idnumber,
|
|
|
384 |
'isfavourite' => false,
|
|
|
385 |
'shortname' => $course->shortname,
|
|
|
386 |
'showshortname' => false,
|
|
|
387 |
'startdate' => $course->startdate,
|
|
|
388 |
'summary' => $summary,
|
|
|
389 |
'summaryformat' => $course->summaryformat,
|
|
|
390 |
'timeaccess' => $lastaccess,
|
|
|
391 |
'viewurl' => $viewurl,
|
|
|
392 |
'editurl' => $editurl,
|
|
|
393 |
'autoenrol' => $autoenrol,
|
|
|
394 |
'visible' => true
|
|
|
395 |
]);
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
$categories = [];
|
|
|
399 |
|
|
|
400 |
foreach ($course_category_ids as $course_category_id) {
|
|
|
401 |
$category = $all_categories[$course_category_id];
|
|
|
402 |
if ($category) {
|
|
|
403 |
array_push($categories, [
|
|
|
404 |
'id' => $category->id,
|
|
|
405 |
'name' => $category->name
|
|
|
406 |
]);
|
|
|
407 |
}
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
// Crear el array de navegación con el rango dinámico
|
|
|
411 |
$pagination = [
|
|
|
412 |
'current_page' => $page,
|
|
|
413 |
'total_pages' => $total_pages,
|
|
|
414 |
'has_previous' => $page > 1,
|
|
|
415 |
'has_next' => $page < $total_pages,
|
|
|
416 |
'previous_page' => $page > 1 ? $page - 1 : null,
|
|
|
417 |
'next_page' => $page < $total_pages ? $page + 1 : null,
|
|
|
418 |
'pages' => array_map(function ($p) use ($page) {
|
|
|
419 |
return [
|
|
|
420 |
'number' => $p,
|
|
|
421 |
'is_current' => $p === $page
|
|
|
422 |
];
|
|
|
423 |
}, range($start_page, $end_page))
|
|
|
424 |
];
|
|
|
425 |
|
|
|
426 |
// Preparar los datos para la plantilla
|
|
|
427 |
$data = [
|
|
|
428 |
'courses' => $courses,
|
|
|
429 |
'pagination' => $pagination
|
|
|
430 |
];
|
|
|
431 |
|
|
|
432 |
$cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);
|
|
|
433 |
|
|
|
434 |
$data = [
|
|
|
435 |
'categories' => $categories
|
|
|
436 |
];
|
|
|
437 |
|
|
|
438 |
$badges = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/badges', $data);
|
|
|
439 |
|
|
|
440 |
return json_encode([
|
|
|
441 |
'success' => true,
|
|
|
442 |
'search_text' => $search_text,
|
|
|
443 |
'category_id' => $category_id,
|
|
|
444 |
'cards' => $cards,
|
|
|
445 |
'badges' => $badges,
|
|
|
446 |
'courses' => $courses
|
|
|
447 |
]);
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
public static function get_cursos_catalogo_returns()
|
|
|
451 |
{
|
|
|
452 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
453 |
}
|
|
|
454 |
}
|