Proyectos de Subversion Moodle

Rev

Rev 1057 | Rev 1059 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1057 Rev 1058
Línea 95... Línea 95...
95
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
95
            'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
96
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
96
            'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' '),
97
        ]);
97
        ]);
98
    }
98
    }
Línea 99... Línea 99...
99
 
99
 
100
    public static function get_cursos_catalogo($category_id, $search_text, $page = 1, $courses_per_page = 10)
100
    public static function get_cursos_catalogo($category_id, $search_text)
Línea -... Línea 101...
-
 
101
    {
101
    {
102
 
Línea -... Línea 103...
-
 
103
 
-
 
104
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
102
 
105
 
103
        global $USER, $DB, $CFG, $OUTPUT, $PAGE;
106
 
Línea 104... Línea 107...
104
 
107
 
105
        $userid = $USER->id;
108
        $userid = $USER->id;
Línea 118... Línea 121...
118
        $records = $DB->get_records('course_categories', ['visible' => 1], 'sortorder, name');
121
        $records = $DB->get_records('course_categories', ['visible' => 1], 'sortorder, name');
119
        foreach ($records as $record) {
122
        foreach ($records as $record) {
120
            $all_categories[$record->id] = $record;
123
            $all_categories[$record->id] = $record;
121
        }
124
        }
Línea -... Línea 125...
-
 
125
 
122
 
126
 
123
        $categories_ids = [];
127
        $categories_ids = [];
124
        $category_filter_active = false;
128
        $category_filter_active = false;
125
        if ($category_id) {
129
        if ($category_id) {
Línea 126... Línea 130...
126
            $category_filter_active = true;
130
            $category_filter_active = true;
Línea -... Línea 131...
-
 
131
 
127
 
132
            $recordLevel0 = $DB->get_record('course_categories', ['id' => $category_id]);
128
            $recordLevel0 = $DB->get_record('course_categories', ['id' => $category_id]);
133
 
Línea -... Línea 134...
-
 
134
 
-
 
135
            if ($recordLevel0) {
129
 
136
                array_push($categories_ids, $recordLevel0->id);
-
 
137
 
130
            if ($recordLevel0) {
138
 
-
 
139
 
131
                array_push($categories_ids, $recordLevel0->id);
140
                $recordsLevel1 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel0->id]);
Línea 132... Línea 141...
132
 
141
 
133
                $recordsLevel1 = $DB->get_records('course_categories', ['visible' => 1, 'parent' => $recordLevel0->id]);
142
                foreach ($recordsLevel1 as $recordLevel1) {
134
                foreach ($recordsLevel1 as $recordLevel1) {
143
 
Línea 145... Línea 154...
145
                    }
154
                    }
146
                }
155
                }
147
            }
156
            }
148
        }
157
        }
Línea -... Línea 158...
-
 
158
 
-
 
159
 
149
 
160
 
150
        $course_ids = [];
161
        $course_ids                 = [];
151
        $course_category_ids = [];
162
        $course_category_ids        = [];
-
 
163
        $courseAvailableForAutoRol  = [];
Línea 152... Línea 164...
152
        $courseAvailableForAutoRol = [];
164
 
153
 
165
 
154
        if ($is_admin) {
166
        if ($is_admin) {
155
            $mycourses = get_courses();
167
            $mycourses = get_courses();
156
        } else {
168
        } else {
Línea -... Línea 169...
-
 
169
            $mycourses  = enrol_get_users_courses($USER->id);
-
 
170
        }
157
            $mycourses = enrol_get_users_courses($USER->id);
171
 
158
        }
172
 
159
 
173
 
160
        foreach ($mycourses as $course) {
174
        foreach ($mycourses as $course) {
Línea 166... Línea 180...
166
                if (stripos($course->fullname, $search_text) === false) {
180
                if (stripos($course->fullname, $search_text) === false) {
167
                    continue;
181
                    continue;
168
                }
182
                }
169
            }
183
            }
Línea -... Línea 184...
-
 
184
 
170
 
185
 
171
            if ($category_filter_active) {
186
            if ($category_filter_active) {
172
                if (!in_array($course->category, $categories_ids)) {
187
                if (!in_array($course->category, $categories_ids)) {
173
                    continue;
188
                    continue;
174
                }
189
                }
Línea -... Línea 190...
-
 
190
            }
175
            }
191
 
176
 
192
 
Línea 177... Línea 193...
177
            array_push($course_ids, $course->id);
193
            array_push($course_ids, $course->id);
178
        }
194
        }
179
 
195
 
180
        $courses_with_autoenrol = $DB->get_records('enrol', ['enrol' => 'self', 'status' => 0]);
196
        $courses_with_autoenrol  = $DB->get_records('enrol', ['enrol' => 'self', 'status' => 0]);
181
        foreach ($courses_with_autoenrol as $course_with_autoenrol) {
197
        foreach ($courses_with_autoenrol as $course_with_autoenrol) {
182
            $course = get_course($course_with_autoenrol->courseid);
198
            $course = get_course($course_with_autoenrol->courseid);
Línea 198... Línea 214...
198
 
214
 
199
            array_push($course_ids, $course->id);
215
            array_push($course_ids, $course->id);
200
            array_push($courseAvailableForAutoRol, $course->id);
216
            array_push($courseAvailableForAutoRol, $course->id);
Línea 201... Línea -...
201
        }
-
 
202
 
-
 
203
        // Calculate pagination
-
 
204
        $total_courses = count($course_ids);
-
 
Línea 205... Línea -...
205
        $total_pages = ceil($total_courses / $courses_per_page);
-
 
-
 
217
        }
-
 
218
 
-
 
219
 
Línea 206... Línea 220...
206
        $offset = ($page - 1) * $courses_per_page;
220
 
207
 
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
 
208
        $course_ids_paginated = array_slice($course_ids, $offset, $courses_per_page);
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];
209
 
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,
-
 
346
                'id' => $course->id,
-
 
347
                'idnumber' => $course->idnumber,
-
 
348
                'isfavourite' => false,
-
 
349
                'shortname' => $course->shortname,
-
 
350
                'showshortname' => false,
-
 
351
                'startdate' => $course->startdate,
-
 
352
                'summary' =>  $summary,
-
 
353
                'summaryformat' => $course->summaryformat,
-
 
354
                'timeaccess' => $lastaccess,
-
 
355
                'viewurl' => $linkurl,
210
        $courses = [];
356
                'editurl' => $editurl,
Línea 211... Línea 357...
211
        foreach ($course_ids_paginated as $course_id) {
357
                'autoenrol' => $autoenrol,
212
            // Rest of your code remains the same
358
                'visible' => true,
213
            // ...
359
            ]);
Línea -... Línea 360...
-
 
360
        }
-
 
361
 
-
 
362
        usort($courses, function ($a, $b) {
214
        }
363
            return $a['fullname'] <=> $b['fullname'];
215
 
364
        });
216
        usort($courses, function ($a, $b) {
365
 
217
            return $a['fullname'] <=> $b['fullname'];
366
 
218
        });
367
 
219
 
368
 
220
        $categories = [];
369
        $categories = [];
Línea -... Línea 370...
-
 
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
            }
221
        foreach ($course_category_ids as $course_category_id) {
375
        }
-
 
376
 
-
 
377
 
222
            $category = $all_categories[$course_category_id];
378
 
Línea -... Línea 379...
-
 
379
 
223
            if ($category) {
380
 
-
 
381
        $data = [
-
 
382
            'courses' => $courses
224
                array_push($categories, ['id' => $category->id, 'name' => $category->name]);
383
        ];
Línea 225... Línea -...
225
            }
-
 
226
        }
-
 
227
 
-
 
-
 
384
 
228
        $data = ['courses' => $courses];
385
        $cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);
229
        $cards = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/cards', $data);
-
 
230
 
-
 
231
        $data = ['categories' => $categories];
-
 
232
        $badges = $OUTPUT->render_from_template('block_cursos_catalogo_ajax/badges', $data);
-
 
233
 
-
 
234
        return json_encode([
-
 
235
            'success' => true,
-
 
236
            'search_text' => $search_text,
386
 
Línea 237... Línea 387...
237
            'category_id' => $category_id,
387
        $data = [
238
            'cards' => $cards,
388
            'categories' => $categories