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