| 1453 |
ariadna |
1 |
<?php
|
| 1482 |
ariadna |
2 |
defined('MOODLE_INTERNAL') || die();
|
|
|
3 |
|
| 1453 |
ariadna |
4 |
class block_cursos_catalogo_ajax_renderer extends plugin_renderer_base
|
|
|
5 |
{
|
|
|
6 |
|
|
|
7 |
public function procesar()
|
|
|
8 |
{
|
|
|
9 |
|
|
|
10 |
global $USER, $DB, $CFG;
|
|
|
11 |
require_once $CFG->dirroot . "/blocks/course_list/block_course_list.php";
|
|
|
12 |
|
|
|
13 |
$is_admin = false;
|
|
|
14 |
$admins = get_admins();
|
|
|
15 |
foreach ($admins as $admin) {
|
|
|
16 |
if ($USER->id == $admin->id) {
|
|
|
17 |
$is_admin = true;
|
|
|
18 |
break;
|
|
|
19 |
}
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
$category_ids = [];
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
if ($is_admin) {
|
|
|
27 |
$mycourses = get_courses();
|
|
|
28 |
foreach ($mycourses as $course) {
|
|
|
29 |
if (!$course->visible) {
|
|
|
30 |
continue;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
if (!in_array($course->category, $category_ids)) {
|
|
|
35 |
array_push($category_ids, $course->category);
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
} else {
|
|
|
39 |
$mycourses = enrol_get_users_courses($USER->id);
|
|
|
40 |
foreach ($mycourses as $course) {
|
|
|
41 |
if (!$course->visible) {
|
|
|
42 |
continue;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if (!in_array($course->category, $category_ids)) {
|
|
|
46 |
array_push($category_ids, $course->category);
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
$courses_with_autoenrol = $DB->get_records('enrol', ['enrol' => 'self']);
|
|
|
52 |
foreach ($courses_with_autoenrol as $course_with_autoenrol) {
|
|
|
53 |
$course = get_course($course_with_autoenrol->courseid);
|
|
|
54 |
if (!$course->visible) {
|
|
|
55 |
continue;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (!in_array($course->category, $category_ids)) {
|
|
|
59 |
array_push($category_ids, $course->category);
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/*
|
|
|
65 |
public function get_records($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
|
|
|
66 |
list($select, $params) = $this->where_clause($table, $conditions);
|
|
|
67 |
return $this->get_records_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
|
|
|
68 |
}
|
|
|
69 |
*/
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
$categories = [];
|
|
|
73 |
$recordsLevel0 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => 0], 'sortorder, name');
|
|
|
74 |
foreach ($recordsLevel0 as $recordLevel0) {
|
|
|
75 |
|
|
|
76 |
$categoryLevel0 = [
|
|
|
77 |
'id' => $recordLevel0->id,
|
|
|
78 |
'name' => $recordLevel0->name,
|
|
|
79 |
'coursecount' => $recordLevel0->coursecount,
|
|
|
80 |
'childsLevel0' => [],
|
|
|
81 |
|
|
|
82 |
];
|
|
|
83 |
|
|
|
84 |
$recordsLevel1 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel0->id], 'sortorder, name');
|
|
|
85 |
foreach ($recordsLevel1 as $recordLevel1) {
|
|
|
86 |
$categoryLevel1 = [
|
|
|
87 |
'id' => $recordLevel1->id,
|
|
|
88 |
'name' => $recordLevel1->name,
|
|
|
89 |
'coursecount' => $recordLevel1->coursecount,
|
|
|
90 |
'childsLevel1' => [],
|
|
|
91 |
];
|
|
|
92 |
|
|
|
93 |
$recordsLevel2 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel1->id], 'sortorder, name');
|
|
|
94 |
foreach ($recordsLevel2 as $recordLevel2) {
|
|
|
95 |
$categoryLevel2 = [
|
|
|
96 |
'id' => $recordLevel2->id,
|
|
|
97 |
'name' => $recordLevel2->name,
|
|
|
98 |
'coursecount' => $recordLevel2->coursecount,
|
|
|
99 |
'childsLevel2' => [],
|
|
|
100 |
];
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
| 1482 |
ariadna |
104 |
$recordsLevel3 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel2->id], 'sortorder, name');
|
| 1453 |
ariadna |
105 |
foreach ($recordsLevel3 as $recordLevel3) {
|
|
|
106 |
$categoryLevel3 = [
|
|
|
107 |
'id' => $recordLevel3->id,
|
|
|
108 |
'name' => $recordLevel3->name,
|
|
|
109 |
];
|
|
|
110 |
array_push($categoryLevel2['childsLevel2'], $categoryLevel3);
|
|
|
111 |
}
|
|
|
112 |
|
| 1482 |
ariadna |
113 |
if (count($categoryLevel2['childsLevel2']) > 0 || $categoryLevel2['coursecount'] > 0) {
|
| 1453 |
ariadna |
114 |
array_push($categoryLevel1['childsLevel1'], $categoryLevel2);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
if (count($categoryLevel1['childsLevel1']) > 0 || $categoryLevel1['coursecount'] > 0) {
|
|
|
119 |
array_push($categoryLevel0['childsLevel0'], $categoryLevel1);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
if (count($categoryLevel0['childsLevel0']) > 0 || $categoryLevel0['coursecount'] > 0) {
|
|
|
124 |
array_push($categories, $categoryLevel0);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
$data = [
|
|
|
129 |
'categories' => $categories
|
|
|
130 |
];
|
|
|
131 |
|
|
|
132 |
return $this->render_from_template('block_cursos_catalogo_ajax/form', $data);
|
|
|
133 |
}
|
|
|
134 |
}
|