Proyectos de Subversion Moodle

Rev

Rev 509 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
class block_cursos_recientes_ajax_external extends \external_api
13
{
14
    public static function get_listado_de_cursos_recientes_parameters()
15
    {
16
        return new \external_function_parameters([
17
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
446 ariadna 18
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
1 efrain 19
        ]);
20
    }
21
 
22
    public static function get_listado_de_cursos_recientes($category_id, $search_text)
23
    {
24
 
25
        /*
26
        [
27
        'category_id' => $category_id,
28
        'search_text' => $search_text,
29
        ] = self::validate_parameters(self::execute_parameters(), [
30
            'category_id' => $category_id,
31
            'search_text' => $search_text
32
        ]);
33
        */
446 ariadna 34
 
1 efrain 35
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
446 ariadna 36
 
37
 
1 efrain 38
        $userid = $USER->id;
39
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
40
 
41
        $courses = [];
509 ariadna 42
        $courselist = array();
446 ariadna 43
 
509 ariadna 44
 
1 efrain 45
        $mycourses = enrol_get_users_courses($userid);
446 ariadna 46
 
509 ariadna 47
        $courselist = array();
446 ariadna 48
        foreach ($mycourses as $course) {
49
            if (!$course->visible) {
1 efrain 50
                continue;
51
            }
446 ariadna 52
 
53
 
54
            if ($search_text) {
55
                if (stripos($course->fullname, $search_text) === false) {
1 efrain 56
                    continue;
57
                }
58
            }
446 ariadna 59
 
60
            if ($category_id) {
61
                if ($category_id != $course->category) {
1 efrain 62
                    continue;
63
                }
64
            }
446 ariadna 65
 
66
            $category = $DB->get_record('course_categories', array('id' => $course->category, 'visible' => 1));
67
            if (!$category) {
1 efrain 68
                continue;
69
            }
70
 
446 ariadna 71
 
1 efrain 72
            if ($course instanceof stdClass) {
73
                $courseInList = new core_course_list_element($course);
74
            }
75
            $image = $url_noimage;
446 ariadna 76
            foreach ($courseInList->get_course_overviewfiles() as $file) {
1 efrain 77
                $isimage = $file->is_valid_image();
78
                $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
79
                if (!$isimage) {
80
                    $image = $url_noimage;
81
                }
82
            }
446 ariadna 83
 
1 efrain 84
            $lastaccess = null;
85
            $sql = "select timecreated  from {logstore_standard_log} where courseid  = :courseid and userid  = :userid " .
86
                "  and eventname  = '\\\\core\\\\event\\\\course_viewed' order by id desc limit 1 ";
446 ariadna 87
 
1 efrain 88
            $timecreated = $DB->get_field_sql($sql,  array('courseid' => $course->id, 'userid' => $userid));
446 ariadna 89
            if ($timecreated) {
90
 
1 efrain 91
                $lastaccess = date('d/m/Y h:i a', $timecreated);
92
            }
93
 
446 ariadna 94
 
95
 
96
            $miProgreso = number_format(progress::get_course_progress_percentage($course), 2); // Progreso por curso
97
 
98
 
1 efrain 99
            $first_section = 0;
100
            $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
446 ariadna 101
 
102
            foreach ($sections as $section) {
103
                if (!empty($section->section)) {
1 efrain 104
                    $first_section = $section->id;
105
                    break;
106
                }
107
            }
108
            $course_context = context_course::instance($course->id);
109
            $roles = get_user_roles($course_context, $USER->id, true);
446 ariadna 110
 
1 efrain 111
            $completion_edit_curso = false;
112
            foreach ($roles as $role) {
113
                if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
114
                    $completion_edit_curso  = true;
115
                    break;
116
                }
117
            }
446 ariadna 118
 
1 efrain 119
            if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id) || $completion_edit_curso) {
446 ariadna 120
                $editurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id . '&notifyeditingon=1';
121
 
1 efrain 122
                //$editurl = $CFG->wwwroot . '/course/edit.php?id=' . $course->id;
123
            } else {
124
                $editurl = '';
125
            }
446 ariadna 126
 
127
 
128
 
1 efrain 129
            $modules = get_fast_modinfo($course->id)->get_cms();
446 ariadna 130
 
131
 
132
 
1 efrain 133
            $linkurl = '';
446 ariadna 134
            foreach ($modules as $module) {
135
 
136
 
137
 
138
 
1 efrain 139
                //print "!".$module->uservisible ."||". $module->is_stealth() ."||". $module->url ."||". $module->section."<br>";
140
                if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
141
                    continue;
142
                }
446 ariadna 143
 
144
                if ($module->section == $first_section || $completion_edit_curso) {
145
 
146
 
147
 
148
 
149
 
1 efrain 150
                    $linkurl =  new moodle_url($module->url, array('forceview' => 1));
151
                    //print_r($linkurl);
152
                    //var_dump($linkurl);
153
                    break;
154
                }
155
            }
156
            /*
157
            if( $completion_edit_curso){
158
                $linkurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
159
            }*/
446 ariadna 160
 
1 efrain 161
            $summary = trim(strip_tags($course->summary));
446 ariadna 162
 
163
            if (empty($summary)) {
1 efrain 164
                $summary = `<p></p>`;
446 ariadna 165
            } else if (strlen($summary) > 80) {
166
 
1 efrain 167
                $summary =  substr($summary, 0, 80) . '...';
446 ariadna 168
            }
169
 
509 ariadna 170
            $courses[] = [
446 ariadna 171
 
1 efrain 172
                'coursecategory' => $category->name,
173
                'courseimage' => $image,
174
                'enddate' => $course->enddate,
175
                'fullname' => $course->fullname,
176
                'fullnamedisplay' => get_course_display_name_for_list($course),
177
                'hasprogress' => true,
178
                'hidden' => false,
179
                'id' => $course->id,
180
                'idnumber' => $course->idnumber,
181
                'isfavourite' => false,
182
                'progress' => $miProgreso,
183
                'shortname' => $course->shortname,
184
                'showshortname' => false,
185
                'startdate' => $course->startdate,
186
                'summary' =>  $summary,
187
                'summaryformat' => $course->summaryformat,
188
                'timeaccess' => $lastaccess,
189
                'viewurl' => $linkurl,
190
                'editurl' => $editurl,
191
                'visible' => true,
446 ariadna 192
 
509 ariadna 193
            ];
1 efrain 194
        }
446 ariadna 195
 
1 efrain 196
        $data = [
446 ariadna 197
            'courses' => $courses
198
        ];
199
 
1 efrain 200
        $cards = $OUTPUT->render_from_template('block_cursos_recientes_ajax/cards', $data);
201
 
446 ariadna 202
 
1 efrain 203
        return json_encode(['success' => true, 'search_text' => $search_text, 'category_id' => $category_id,  'cards' => $cards]);
204
    }
205
 
206
    public static function get_listado_de_cursos_recientes_returns()
207
    {
208
        return new \external_value(PARAM_RAW, 'The updated JSON output');
209
    }
510 ariadna 210
 
211
    public static function get_cursos_catalogo_parameters()
212
    {
213
        return new \external_function_parameters([
214
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
215
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
216
        ]);
217
    }
218
 
219
    public static function get_cursos_catalogo($category_id, $search_text)
220
    {
221
 
222
 
223
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
224
 
225
 
226
 
227
        $userid = $USER->id;
228
        $url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
229
 
230
        $is_admin = false;
231
        $admins = get_admins();
232
        foreach ($admins as $admin) {
233
            if ($USER->id == $admin->id) {
234
                $is_admin = true;
235
                break;
236
            }
237
        }
238
 
239
        $all_categories = [];
240
        $records = $DB->get_records('course_categories', ['visible' => 1], 'sortorder, name');
241
        foreach ($records as $record) {
242
            $all_categories[$record->id] = $record;
243
        }
244
 
245
 
246
        $categories_ids = [];
247
        $category_filter_active = false;
248
        if ($category_id) {
249
            $category_filter_active = true;
250
 
251
            $recordLevel0 = $DB->get_record('course_categories', ['id' => $category_id]);
252
 
253
 
254
            if ($recordLevel0) {
255
                array_push($categories_ids, $recordLevel0->id);
256
 
257
 
258
 
259
                $recordsLevel1 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel0->id]);
260
 
261
                foreach ($recordsLevel1 as $recordLevel1) {
262
 
263
                    array_push($categories_ids, $recordLevel1->id);
264
 
265
                    $recordsLevel2 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel1->id]);
266
                    foreach ($recordsLevel2 as $recordLevel2) {
267
                        array_push($categories_ids, $recordLevel2->id);
268
 
269
                        $recordsLevel3 = $DB->get_records('course_categories', ['visible' => 2, 'parent' => $recordLevel2->id]);
270
                        foreach ($recordsLevel3 as $recordLevel3) {
271
                            array_push($categories_ids, $recordLevel3->id);
272
                        }
273
                    }
274
                }
275
            }
276
        }
277
 
278
 
279
 
280
        $course_ids                 = [];
281
        $course_category_ids        = [];
282
        $courseAvailableForAutoRol  = [];
283
 
284
 
285
        if ($is_admin) {
286
            $mycourses = get_courses();
287
        } else {
288
            $mycourses  = enrol_get_users_courses($USER->id);
289
        }
290
 
291
 
292
 
293
        foreach ($mycourses as $course) {
294
            if (!$course->visible) {
295
                continue;
296
            }
297
 
298
            if ($search_text) {
299
                if (stripos($course->fullname, $search_text) === false) {
300
                    continue;
301
                }
302
            }
303
 
304
 
305
            if ($category_filter_active) {
306
                if (!in_array($course->category, $categories_ids)) {
307
                    continue;
308
                }
309
            }
310
 
311
 
312
            array_push($course_ids, $course->id);
313
        }
314
 
315
        $courses_with_autoenrol  = $DB->get_records('enrol', ['enrol' => 'self', 'status' => 0]);
316
        foreach ($courses_with_autoenrol as $course_with_autoenrol) {
317
            $course = get_course($course_with_autoenrol->courseid);
318
            if (!$course->visible) {
319
                continue;
320
            }
321
 
322
            if ($search_text) {
323
                if (stripos($course_with_autoenrol->fullname, $search_text) === false) {
324
                    continue;
325
                }
326
            }
327
 
328
            if ($category_filter_active) {
329
                if (!in_array($course->category, $categories_ids)) {
330
                    continue;
331
                }
332
            }
333
 
334
            array_push($course_ids, $course->id);
335
            array_push($courseAvailableForAutoRol, $course->id);
336
        }
337
 
338
 
339
 
340
 
341
 
342
 
343
        $courses = [];
344
        foreach ($course_ids as $course_id) {
345
            $course = get_course($course_id);
346
 
347
            if ($course instanceof stdClass) {
348
                $coreCourseList = new core_course_list_element($course);
349
            }
350
 
351
            if (!in_array($course->category, $course_category_ids)) {
352
 
353
 
354
                array_push($course_category_ids, $course->category);
355
            }
356
 
357
 
358
            $image = $url_noimage;
359
            foreach ($coreCourseList->get_course_overviewfiles() as $file) {
360
                $isimage = $file->is_valid_image();
361
                $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
362
                if (!$isimage) {
363
                    $image = $url_noimage;
364
                }
365
            }
366
 
367
            $lastaccess = null;
368
            $sql = "select timecreated  from {logstore_standard_log} where courseid  = :courseid and userid  = :userid " .
369
                "  and eventname  = '\\\\core\\\\event\\\\course_viewed' order by id desc limit 1 ";
370
 
371
            $timecreated = $DB->get_field_sql($sql,  array('courseid' => $course->id, 'userid' => $userid));
372
            if ($timecreated) {
373
 
374
                $lastaccess = date('d/m/Y h:i a', $timecreated);
375
            }
376
 
377
            $first_section = 0;
378
            $sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
379
 
380
            foreach ($sections as $section) {
381
                if (!empty($section->section)) {
382
                    $first_section = $section->id;
383
                    break;
384
                }
385
            }
386
 
387
            $course_context = context_course::instance($course->id);
388
            $roles = get_user_roles($course_context, $USER->id, true);
389
 
390
            $completion_edit_curso = false;
391
            foreach ($roles as $role) {
392
                if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
393
                    $completion_edit_curso  = true;
394
                    break;
395
                }
396
            }
397
 
398
            if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id) || $completion_edit_curso) {
399
                $editurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id . '&notifyeditingon=1';
400
            } else {
401
                $editurl = '';
402
            }
403
 
404
 
405
            $modules = get_fast_modinfo($course->id)->get_cms();
406
 
407
            $linkurl = '';
408
            foreach ($modules as $module) {
409
                if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
410
                    continue;
411
                }
412
 
413
                if ($module->section == $first_section || $completion_edit_curso) {
414
                    $linkurl =  new moodle_url($module->url, array('forceview' => 1));
415
                    break;
416
                }
417
            }
418
 
419
            $summary = trim(strip_tags($course->summary));
420
 
421
            if (empty($summary)) {
422
                $summary = '<p></p>';
423
            } else if (strlen($summary) > 80) {
424
 
425
                $summary =  substr($summary, 0, 80) . '...';
426
            }
427
 
428
            if (empty($editurl) && in_array($course->id, $courseAvailableForAutoRol)) {
429
                $autoenrol = 'yes';
430
            } else {
431
                $autoenrol = '';
432
            }
433
 
434
 
435
            $category = $all_categories[$course->category];
436
            if ($category->parent) {
437
                $categoryParent = $all_categories[$category->parent];
438
                if ($categoryParent) {
439
                    $categoryName = $categoryParent->name . ' / ' . $category->name;
440
                } else {
441
                    $categoryName = $category->name;
442
                }
443
            } else {
444
                $categoryName =  $category->name;
445
            }
446
            if (strlen($categoryName) > 40) {
447
 
448
                $categoryName =  substr($categoryName, 0, 40) . '...';
449
            }
450
 
451
 
452
            // echo '<pre>';
453
            // echo '$categoryName = ' . $categoryName . ' $course->category = ' . $course->category;
454
            // echo '</pre>';
455
 
456
 
457
            array_push($courses, [
458
                'courseid' => $course->id,
459
                'coursecategory' => $categoryName,
460
                'courseimage' => $image,
461
                'enddate' => $course->enddate,
462
                'fullname' => $course->fullname,
463
                'fullnamedisplay' => get_course_display_name_for_list($course),
464
                'hidden' => false,
465
                'id' => $course->id,
466
                'idnumber' => $course->idnumber,
467
                'isfavourite' => false,
468
                'shortname' => $course->shortname,
469
                'showshortname' => false,
470
                'startdate' => $course->startdate,
471
                'summary' =>  $summary,
472
                'summaryformat' => $course->summaryformat,
473
                'timeaccess' => $lastaccess,
474
                'viewurl' => $linkurl,
475
                'editurl' => $editurl,
476
                'autoenrol' => $autoenrol,
477
                'visible' => true,
478
            ]);
479
        }
480
 
481
        usort($courses, function ($a, $b) {
482
            return $a['fullname'] <=> $b['fullname'];
483
        });
484
 
485
 
486
 
487
 
488
        $categories = [];
489
        foreach ($course_category_ids as $course_category_id) {
490
            $category = $all_categories[$course_category_id];
491
            if ($category) {
492
                array_push($categories, ['id' => $category->id, 'name' => $category->name]);
493
            }
494
        }
495
 
496
 
497
 
498
 
499
 
500
        $data = [
501
            'courses' => $courses
502
        ];
503
 
504
        $cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);
505
 
506
        $data = [
507
            'categories' => $categories
508
        ];
509
 
510
        $badges = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/badges', $data);
511
 
512
 
513
        return json_encode(['success' => true, 'search_text' => $search_text, 'category_id' => $category_id,  'cards' => $cards, 'badges' => $badges]);
514
    }
515
 
516
 
517
    public static function get_cursos_catalogo_returns()
518
    {
519
        return new \external_value(PARAM_RAW, 'The updated JSON output');
520
    }
446 ariadna 521
}