Proyectos de Subversion Moodle

Rev

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