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