Proyectos de Subversion Moodle

Rev

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