Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Renderer for use with the course section and all the goodness that falls
19
 * within it.
20
 *
21
 * This renderer should contain methods useful to courses, and categories.
22
 *
23
 * @package   moodlecore
24
 * @copyright 2010 Sam Hemelryk
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
 defined('MOODLE_INTERNAL') || die();
29
 
30
/**
31
 * The core course renderer
32
 *
33
 * Can be retrieved with the following:
34
 * $renderer = $PAGE->get_renderer('core','course');
35
 */
36
class core_course_renderer extends plugin_renderer_base {
37
    const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */
38
    const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */
39
    const COURSECAT_SHOW_COURSES_COLLAPSED = 10;
40
    const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */
41
    const COURSECAT_SHOW_COURSES_EXPANDED = 20;
42
    const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
43
 
44
    const COURSECAT_TYPE_CATEGORY = 0;
45
    const COURSECAT_TYPE_COURSE = 1;
46
 
47
    /**
48
     * A cache of strings
49
     * @var stdClass
50
     */
51
    protected $strings;
52
 
53
    /**
54
     * Whether a category content is being initially rendered with children. This is mainly used by the
55
     * core_course_renderer::corsecat_tree() to render the appropriate action for the Expand/Collapse all link on
56
     * page load.
57
     * @var bool
58
     */
59
    protected $categoryexpandedonload = false;
60
 
61
    /**
62
     * Override the constructor so that we can initialise the string cache
63
     *
64
     * @param moodle_page $page
65
     * @param string $target
66
     */
67
    public function __construct(moodle_page $page, $target) {
68
        $this->strings = new stdClass;
69
        $courseid = $page->course->id;
70
        parent::__construct($page, $target);
71
    }
72
 
73
    /**
74
     * @deprecated since 3.2
75
     */
76
    protected function add_modchoosertoggle() {
77
        throw new coding_exception('core_course_renderer::add_modchoosertoggle() can not be used anymore.');
78
    }
79
 
80
    /**
81
     * Renders course info box.
82
     *
83
     * @param stdClass $course
84
     * @return string
85
     */
86
    public function course_info_box(stdClass $course) {
87
        $content = '';
88
        $content .= $this->output->box_start('generalbox info');
89
        $chelper = new coursecat_helper();
90
        $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
91
        $content .= $this->coursecat_coursebox($chelper, $course);
92
        $content .= $this->output->box_end();
93
        return $content;
94
    }
95
 
96
    /**
97
     * Renderers a structured array of courses and categories into a nice XHTML tree structure.
98
     *
99
     * @deprecated since 2.5
100
     *
101
     * @param array $ignored argument ignored
102
     * @return string
103
     */
104
    final public function course_category_tree(array $ignored) {
105
        debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER);
106
        return $this->frontpage_combo_list();
107
    }
108
 
109
    /**
110
     * Renderers a category for use with course_category_tree
111
     *
112
     * @deprecated since 2.5
113
     *
114
     * @param array $category
115
     * @param int $depth
116
     * @return string
117
     */
118
    final protected function course_category_tree_category(stdClass $category, $depth=1) {
119
        debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER);
120
        return '';
121
    }
122
 
123
    /**
124
     * Render a modchooser.
125
     *
126
     * @param renderable $modchooser The chooser.
127
     * @return string
128
     */
129
    public function render_modchooser(renderable $modchooser) {
130
        return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
131
    }
132
 
133
    /**
134
     * @deprecated since 3.9
135
     */
136
    public function course_modchooser() {
137
        throw new coding_exception('course_modchooser() can not be used anymore, please use course_activitychooser() instead.');
138
    }
139
 
140
    /**
141
     * Build the HTML for the module chooser javascript popup.
142
     *
143
     * @param int $courseid The course id to fetch modules for.
144
     * @return string
145
     */
146
    public function course_activitychooser($courseid) {
147
 
148
        if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
149
            return '';
150
        }
151
 
152
        // Build an object of config settings that we can then hook into in the Activity Chooser.
153
        $chooserconfig = (object) [
154
            'tabmode' => get_config('core', 'activitychoosertabmode'),
155
        ];
156
        $this->page->requires->js_call_amd('core_course/activitychooser', 'init', [$courseid, $chooserconfig]);
157
 
158
        return '';
159
    }
160
 
161
    /**
162
     * Build the HTML for a specified set of modules
163
     *
164
     * @param array $modules A set of modules as used by the
165
     * course_modchooser_module function
166
     * @return string The composed HTML for the module
167
     */
168
    protected function course_modchooser_module_types($modules) {
169
        debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
170
            'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
171
        return '';
172
    }
173
 
174
    /**
175
     * Return the HTML for the specified module adding any required classes
176
     *
177
     * @param object $module An object containing the title, and link. An
178
     * icon, and help text may optionally be specified. If the module
179
     * contains subtypes in the types option, then these will also be
180
     * displayed.
181
     * @param array $classes Additional classes to add to the encompassing
182
     * div element
183
     * @return string The composed HTML for the module
184
     */
185
    protected function course_modchooser_module($module, $classes = array('option')) {
186
        debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
187
            'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
188
        return '';
189
    }
190
 
191
    protected function course_modchooser_title($title, $identifier = null) {
192
        debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
193
            'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
194
        return '';
195
    }
196
 
197
    /**
198
     * @deprecated since 4.0 - please do not use this function any more.
199
     */
200
    public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) {
201
 
202
        throw new coding_exception(
203
            'course_section_cm_edit_actions can not be used any more. Please, use ' .
204
            'core_courseformat\\output\\local\\content\\cm\\controlmenu instead.'
205
        );
206
    }
207
 
208
    /**
209
     * Renders HTML for the menus to add activities and resources to the current course
210
     *
211
     * Renders the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback.
212
     *
213
     * @param stdClass $course
214
     * @param int $section relative section number (field course_sections.section)
215
     * @param int $sectionreturn The section to link back to
216
     * @param array $displayoptions additional display options, for example blocks add
217
     *     option 'inblock' => true, suggesting to display controls vertically
218
     * @return string
219
     */
220
    function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
221
        // Check to see if user can add menus.
222
        if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
223
                || !$this->page->user_is_editing()) {
224
            return '';
225
        }
226
 
227
        $data = [
228
            'sectionid' => $section,
229
            'sectionreturn' => $sectionreturn
230
        ];
231
        $ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
232
 
233
        // Load the JS for the modal.
234
        $this->course_activitychooser($course->id);
235
 
236
        return $ajaxcontrol;
237
    }
238
 
239
    /**
240
     * Renders html to display a course search form
241
     *
242
     * @param string $value default value to populate the search field
243
     * @return string
244
     */
245
    public function course_search_form($value = '') {
246
 
247
        $data = [
248
            'action' => \core_search\manager::get_course_search_url(),
249
            'btnclass' => 'btn-primary',
250
            'inputname' => 'q',
251
            'searchstring' => get_string('searchcourses'),
252
            'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
253
            'query' => $value
254
        ];
255
        return $this->render_from_template('core/search_input', $data);
256
    }
257
 
258
    /**
259
     * @deprecated since Moodle 3.11
260
     */
261
    public function course_section_cm_completion() {
262
        throw new coding_exception(__FUNCTION__ . ' is deprecated. Use the activity_completion output component instead.');
263
    }
264
 
265
    /**
266
     * @deprecated since 4.0 - please do not use this function any more.
267
     */
268
    public function is_cm_conditionally_hidden(cm_info $mod) {
269
 
270
        throw new coding_exception(
271
            'is_cm_conditionally_hidden can not be used any more. Please, use ' .
272
            '\core_availability\info_module::is_available_for_all instead'
273
        );
274
    }
275
 
276
    /**
277
     * @deprecated since 4.0 - please do not use this function any more.
278
     */
279
    public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
280
 
281
        throw new coding_exception(
282
            'course_section_cm_name can not be used any more. Please, use ' .
283
            'core_courseformat\\output\\local\\content\\cm\\cmname class instead.'
284
        );
285
    }
286
 
287
    /**
288
     * @deprecated since 4.0 - please do not use this function any more.
289
     */
290
    protected function course_section_cm_classes(cm_info $mod) {
291
 
292
        throw new coding_exception(
293
            'course_section_cm_classes can not be used any more. Now it is part of core_courseformat\\output\\local\\content\\cm'
294
        );
295
    }
296
 
297
    /**
298
     * @deprecated since 4.0 - please do not use this function any more.
299
     */
300
    public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) {
301
 
302
        throw new coding_exception(
303
            'course_section_cm_name_title can not be used any more. Please, use ' .
304
            'core_courseformat\\output\\local\\cm\\title class instead'
305
        );
306
    }
307
 
308
    /**
309
     * @deprecated since 4.0 - please do not use this function any more.
310
     */
311
    public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
312
 
313
        throw new coding_exception(
314
            'course_section_cm_text can not be used any more. Now it is part of core_courseformat\\output\\local\\content\\cm'
315
        );
316
    }
317
 
318
    /**
319
     * @deprecated since 4.0 - please do not use this function any more.
320
     */
321
    public function availability_info($text, $additionalclasses = '') {
322
 
323
        throw new coding_exception(
324
            'availability_info can not be used any more. Please, use ' .
325
            'core_courseformat\\output\\local\\content\\section\\availability instead'
326
        );
327
    }
328
 
329
    /**
330
     * @deprecated since 4.0 - please do not use this function any more.
331
     */
332
    public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
333
 
334
        throw new coding_exception(
335
            'course_section_cm_availability can not be used any more. Please, use ' .
336
            'core_courseformat\\output\\local\\content\\cm\\availability instead'
337
        );
338
    }
339
 
340
    /**
341
     * @deprecated since 4.0 - use core_course output components or course_format::course_section_updated_cm_item instead.
342
     */
343
    public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
344
 
345
        throw new coding_exception(
346
            'course_section_cm_list_item can not be used any more. Please, use renderer course_section_updated_cm_item instead'
347
        );
348
    }
349
 
350
    /**
351
     * @deprecated since 4.0 - use core_course output components instead.
352
     */
353
    public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
354
 
355
        throw new coding_exception(
356
            'course_section_cm can not be used any more. Please, use core_courseformat\\output\\content\\cm output class instead'
357
        );
358
    }
359
 
360
    /**
361
     * Message displayed to the user when they try to access unavailable activity following URL
362
     *
363
     * This method is a very simplified version of {@link course_section_cm()} to be part of the error
364
     * notification only. It also does not check if module is visible on course page or not.
365
     *
366
     * The message will be displayed inside notification!
367
     *
368
     * @param cm_info $cm
369
     * @return string
370
     */
371
    public function course_section_cm_unavailable_error_message(cm_info $cm) {
372
        if ($cm->uservisible) {
373
            return null;
374
        }
375
        if (!$cm->availableinfo) {
376
            return get_string('activityiscurrentlyhidden');
377
        }
378
 
379
        $altname = get_accesshide(' ' . $cm->modfullname);
380
        $name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(),
381
                'class' => 'iconlarge activityicon', 'alt' => '')) .
382
            html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
383
        $formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
384
        return html_writer::div($name, 'activityinstance-error') .
385
        html_writer::div($formattedinfo, 'availabilityinfo-error');
386
    }
387
 
388
    /**
389
     * @deprecated since 4.0 - use core_course output components instead.
390
     */
391
    public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = []) {
392
 
393
        throw new coding_exception(
394
            'course_section_cm_list can not be used any more. Please, use ' .
395
            'core_courseformat\\output\\local\\content\\section\\cmlist class instead'
396
        );
397
    }
398
 
399
    /**
400
     * Displays a custom list of courses with paging bar if necessary
401
     *
402
     * If $paginationurl is specified but $totalcount is not, the link 'View more'
403
     * appears under the list.
404
     *
405
     * If both $paginationurl and $totalcount are specified, and $totalcount is
406
     * bigger than count($courses), a paging bar is displayed above and under the
407
     * courses list.
408
     *
409
     * @param array $courses array of course records (or instances of core_course_list_element) to show on this page
410
     * @param bool $showcategoryname whether to add category name to the course description
411
     * @param string $additionalclasses additional CSS classes to add to the div.courses
412
     * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
413
     * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
414
     * @param int $page current page number (defaults to 0 referring to the first page)
415
     * @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
416
     * @return string
417
     */
418
    public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) {
419
        global $CFG;
420
        // create instance of coursecat_helper to pass display options to function rendering courses list
421
        $chelper = new coursecat_helper();
422
        if ($showcategoryname) {
423
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT);
424
        } else {
425
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
426
        }
427
        if ($totalcount !== null && $paginationurl !== null) {
428
            // add options to display pagination
429
            if ($perpage === null) {
430
                $perpage = $CFG->coursesperpage;
431
            }
432
            $chelper->set_courses_display_options(array(
433
                'limit' => $perpage,
434
                'offset' => ((int)$page) * $perpage,
435
                'paginationurl' => $paginationurl,
436
            ));
437
        } else if ($paginationurl !== null) {
438
            // add options to display 'View more' link
439
            $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl));
440
            $totalcount = count($courses) + 1; // has to be bigger than count($courses) otherwise link will not be displayed
441
        }
442
        $chelper->set_attributes(array('class' => $additionalclasses));
443
        $content = $this->coursecat_courses($chelper, $courses, $totalcount);
444
        return $content;
445
    }
446
 
447
    /**
448
     * Returns HTML to display course name.
449
     *
450
     * @param coursecat_helper $chelper
451
     * @param core_course_list_element $course
452
     * @return string
453
     */
454
    protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string {
455
        $content = '';
456
        if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) {
457
            $nametag = 'h3';
458
        } else {
459
            $nametag = 'div';
460
        }
461
        $coursename = $chelper->get_course_formatted_name($course);
462
        $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]),
463
            $coursename, ['class' => $course->visible ? 'aalink' : 'aalink dimmed']);
464
        $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']);
465
        // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
466
        $content .= html_writer::start_tag('div', ['class' => 'moreinfo']);
467
        if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
468
            if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles()
469
                || $course->has_custom_fields()) {
470
                $url = new moodle_url('/course/info.php', ['id' => $course->id]);
471
                $image = $this->output->pix_icon('i/info', $this->strings->summary);
472
                $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]);
473
                // Make sure JS file to expand course content is included.
474
                $this->coursecat_include_js();
475
            }
476
        }
477
        $content .= html_writer::end_tag('div');
478
        return $content;
479
    }
480
 
481
    /**
482
     * Returns HTML to display course enrolment icons.
483
     *
484
     * @param core_course_list_element $course
485
     * @return string
486
     */
487
    protected function course_enrolment_icons(core_course_list_element $course): string {
488
        $content = '';
489
        if ($icons = enrol_get_course_info_icons($course)) {
490
            $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']);
491
            foreach ($icons as $icon) {
492
                $content .= $this->render($icon);
493
            }
494
            $content .= html_writer::end_tag('div');
495
        }
496
        return $content;
497
    }
498
 
499
    /**
500
     * Displays one course in the list of courses.
501
     *
502
     * This is an internal function, to display an information about just one course
503
     * please use {@link core_course_renderer::course_info_box()}
504
     *
505
     * @param coursecat_helper $chelper various display options
506
     * @param core_course_list_element|stdClass $course
507
     * @param string $additionalclasses additional classes to add to the main <div> tag (usually
508
     *    depend on the course position in list - first/last/even/odd)
509
     * @return string
510
     */
511
    protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') {
512
        if (!isset($this->strings->summary)) {
513
            $this->strings->summary = get_string('summary');
514
        }
515
        if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) {
516
            return '';
517
        }
518
        if ($course instanceof stdClass) {
519
            $course = new core_course_list_element($course);
520
        }
521
        $content = '';
522
        $classes = trim('coursebox clearfix '. $additionalclasses);
523
        if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
524
            $classes .= ' collapsed';
525
        }
526
 
527
        // .coursebox
528
        $content .= html_writer::start_tag('div', array(
529
            'class' => $classes,
530
            'data-courseid' => $course->id,
531
            'data-type' => self::COURSECAT_TYPE_COURSE,
532
        ));
533
 
534
        $content .= html_writer::start_tag('div', array('class' => 'info'));
535
        $content .= $this->course_name($chelper, $course);
536
        $content .= $this->course_enrolment_icons($course);
537
        $content .= html_writer::end_tag('div');
538
 
539
        $content .= html_writer::start_tag('div', array('class' => 'content'));
540
        $content .= $this->coursecat_coursebox_content($chelper, $course);
541
        $content .= html_writer::end_tag('div');
542
 
543
        $content .= html_writer::end_tag('div'); // .coursebox
544
        return $content;
545
    }
546
 
547
    /**
548
     * Returns HTML to display course summary.
549
     *
550
     * @param coursecat_helper $chelper
551
     * @param core_course_list_element $course
552
     * @return string
553
     */
554
    protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string {
555
        $content = '';
556
        if ($course->has_summary()) {
557
            $content .= html_writer::start_tag('div', ['class' => 'summary']);
558
            $content .= $chelper->get_course_formatted_summary($course,
559
                array('overflowdiv' => true, 'noclean' => true, 'para' => false));
560
            $content .= html_writer::end_tag('div');
561
        }
562
        return $content;
563
    }
564
 
565
    /**
566
     * Returns HTML to display course contacts.
567
     *
568
     * @param core_course_list_element $course
569
     * @return string
570
     */
571
    protected function course_contacts(core_course_list_element $course) {
572
        $content = '';
573
        if ($course->has_course_contacts()) {
574
            $content .= html_writer::start_tag('ul', ['class' => 'teachers']);
575
            foreach ($course->get_course_contacts() as $coursecontact) {
576
                $rolenames = array_map(function ($role) {
577
                    return $role->displayname;
578
                }, $coursecontact['roles']);
579
                $name = html_writer::tag('span', implode(", ", $rolenames).': ', ['class' => 'font-weight-bold']);
580
                $name .= html_writer::link(
581
                   \core_user::get_profile_url($coursecontact['user'], context_system::instance()),
582
                   $coursecontact['username']
583
                );
584
                $content .= html_writer::tag('li', $name);
585
            }
586
            $content .= html_writer::end_tag('ul');
587
        }
588
        return $content;
589
    }
590
 
591
    /**
592
     * Returns HTML to display course overview files.
593
     *
594
     * @param core_course_list_element $course
595
     * @return string
596
     */
597
    protected function course_overview_files(core_course_list_element $course): string {
598
        global $CFG;
599
 
600
        $contentimages = $contentfiles = '';
601
        foreach ($course->get_course_overviewfiles() as $file) {
602
            $isimage = $file->is_valid_image();
603
            $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php",
604
                '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
605
                $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
606
            if ($isimage) {
607
                $contentimages .= html_writer::tag('div',
608
                    html_writer::empty_tag('img', ['src' => $url, 'alt' => '']),
609
                    ['class' => 'courseimage']);
610
            } else {
611
                $image = $this->output->pix_icon(file_file_icon($file), $file->get_filename(), 'moodle');
612
                $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']).
613
                    html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
614
                $contentfiles .= html_writer::tag('span',
615
                    html_writer::link($url, $filename),
616
                    ['class' => 'coursefile fp-filename-icon text-break']);
617
            }
618
        }
619
        return $contentimages . $contentfiles;
620
    }
621
 
622
    /**
623
     * Returns HTML to display course category name.
624
     *
625
     * @param coursecat_helper $chelper
626
     * @param core_course_list_element $course
627
     * @return string
628
     */
629
    protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string {
630
        $content = '';
631
        // Display course category if necessary (for example in search results).
632
        if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
633
            if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) {
634
                $content .= html_writer::start_tag('div', ['class' => 'coursecat']);
635
                $content .= html_writer::tag('span', get_string('category').': ', ['class' => 'font-weight-bold']);
636
                $content .= html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]),
637
                        $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']);
638
                $content .= html_writer::end_tag('div');
639
            }
640
        }
641
        return $content;
642
    }
643
 
644
    /**
645
     * Returns HTML to display course custom fields.
646
     *
647
     * @param core_course_list_element $course
648
     * @return string
649
     */
650
    protected function course_custom_fields(core_course_list_element $course): string {
651
        $content = '';
652
        if ($course->has_custom_fields()) {
653
            $handler = core_course\customfield\course_handler::create();
654
            $customfields = $handler->display_custom_fields_data($course->get_custom_fields());
655
            $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']);
656
        }
657
        return $content;
658
    }
659
 
660
    /**
661
     * Returns HTML to display course content (summary, course contacts and optionally category name)
662
     *
663
     * This method is called from coursecat_coursebox() and may be re-used in AJAX
664
     *
665
     * @param coursecat_helper $chelper various display options
666
     * @param stdClass|core_course_list_element $course
667
     * @return string
668
     */
669
    protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
670
        if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
671
            return '';
672
        }
673
        if ($course instanceof stdClass) {
674
            $course = new core_course_list_element($course);
675
        }
676
        $content = \html_writer::start_tag('div', ['class' => 'd-flex']);
677
        $content .= $this->course_overview_files($course);
678
        $content .= \html_writer::start_tag('div', ['class' => 'flex-grow-1']);
679
        $content .= $this->course_summary($chelper, $course);
680
        $content .= $this->course_contacts($course);
681
        $content .= $this->course_category_name($chelper, $course);
682
        $content .= $this->course_custom_fields($course);
683
        $content .= \html_writer::end_tag('div');
684
        $content .= \html_writer::end_tag('div');
685
        return $content;
686
    }
687
 
688
    /**
689
     * Renders the list of courses
690
     *
691
     * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
692
     * method from outside of the class
693
     *
694
     * If list of courses is specified in $courses; the argument $chelper is only used
695
     * to retrieve display options and attributes, only methods get_show_courses(),
696
     * get_courses_display_option() and get_and_erase_attributes() are called.
697
     *
698
     * @param coursecat_helper $chelper various display options
699
     * @param array $courses the list of courses to display
700
     * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
701
     *     defaulted to count($courses)
702
     * @return string
703
     */
704
    protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) {
705
        global $CFG;
706
        if ($totalcount === null) {
707
            $totalcount = count($courses);
708
        }
709
        if (!$totalcount) {
710
            // Courses count is cached during courses retrieval.
711
            return '';
712
        }
713
 
714
        if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) {
715
            // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
716
            if ($totalcount <= $CFG->courseswithsummarieslimit) {
717
                $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
718
            } else {
719
                $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
720
            }
721
        }
722
 
723
        // prepare content of paging bar if it is needed
724
        $paginationurl = $chelper->get_courses_display_option('paginationurl');
725
        $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
726
        if ($totalcount > count($courses)) {
727
            // there are more results that can fit on one page
728
            if ($paginationurl) {
729
                // the option paginationurl was specified, display pagingbar
730
                $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage);
731
                $page = $chelper->get_courses_display_option('offset') / $perpage;
732
                $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
733
                        $paginationurl->out(false, array('perpage' => $perpage)));
734
                if ($paginationallowall) {
735
                    $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
736
                            get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
737
                }
738
            } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
739
                // the option for 'View more' link was specified, display more link
740
                $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
741
                $morelink = html_writer::tag(
742
                    'div',
743
                    html_writer::link($viewmoreurl, $viewmoretext, ['class' => 'btn btn-secondary']),
744
                    ['class' => 'paging paging-morelink']
745
                );
746
            }
747
        } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
748
            // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
749
            $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
750
                get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
751
        }
752
 
753
        // display list of courses
754
        $attributes = $chelper->get_and_erase_attributes('courses');
755
        $content = html_writer::start_tag('div', $attributes);
756
 
757
        if (!empty($pagingbar)) {
758
            $content .= $pagingbar;
759
        }
760
 
761
        $coursecount = 0;
762
        foreach ($courses as $course) {
763
            $coursecount ++;
764
            $classes = ($coursecount%2) ? 'odd' : 'even';
765
            if ($coursecount == 1) {
766
                $classes .= ' first';
767
            }
768
            if ($coursecount >= count($courses)) {
769
                $classes .= ' last';
770
            }
771
            $content .= $this->coursecat_coursebox($chelper, $course, $classes);
772
        }
773
 
774
        if (!empty($pagingbar)) {
775
            $content .= $pagingbar;
776
        }
777
        if (!empty($morelink)) {
778
            $content .= $morelink;
779
        }
780
 
781
        $content .= html_writer::end_tag('div'); // .courses
782
        return $content;
783
    }
784
 
785
    /**
786
     * Renders the list of subcategories in a category
787
     *
788
     * @param coursecat_helper $chelper various display options
789
     * @param core_course_category $coursecat
790
     * @param int $depth depth of the category in the current tree
791
     * @return string
792
     */
793
    protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) {
794
        global $CFG;
795
        $subcategories = array();
796
        if (!$chelper->get_categories_display_option('nodisplay')) {
797
            $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
798
        }
799
        $totalcount = $coursecat->get_children_count();
800
        if (!$totalcount) {
801
            // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children()
802
            // to avoid extra DB requests.
803
            // Categories count is cached during children categories retrieval.
804
            return '';
805
        }
806
 
807
        // prepare content of paging bar or more link if it is needed
808
        $paginationurl = $chelper->get_categories_display_option('paginationurl');
809
        $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
810
        if ($totalcount > count($subcategories)) {
811
            if ($paginationurl) {
812
                // the option 'paginationurl was specified, display pagingbar
813
                $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage);
814
                $page = $chelper->get_categories_display_option('offset') / $perpage;
815
                $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
816
                        $paginationurl->out(false, array('perpage' => $perpage)));
817
                if ($paginationallowall) {
818
                    $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
819
                            get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
820
                }
821
            } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
822
                // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
823
                if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
824
                    $viewmoreurl->param('categoryid', $coursecat->id);
825
                }
826
                $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
827
                $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
828
                        array('class' => 'paging paging-morelink'));
829
            }
830
        } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
831
            // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
832
            $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
833
                get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
834
        }
835
 
836
        // display list of subcategories
837
        $content = html_writer::start_tag('div', array('class' => 'subcategories'));
838
 
839
        if (!empty($pagingbar)) {
840
            $content .= $pagingbar;
841
        }
842
 
843
        foreach ($subcategories as $subcategory) {
844
            $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1);
845
        }
846
 
847
        if (!empty($pagingbar)) {
848
            $content .= $pagingbar;
849
        }
850
        if (!empty($morelink)) {
851
            $content .= $morelink;
852
        }
853
 
854
        $content .= html_writer::end_tag('div');
855
        return $content;
856
    }
857
 
858
    /**
859
     * Make sure that javascript file for AJAX expanding of courses and categories content is included
860
     */
861
    protected function coursecat_include_js() {
862
        if (!$this->page->requires->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) {
863
            return;
864
        }
865
 
866
        // We must only load this module once.
867
        $this->page->requires->yui_module('moodle-course-categoryexpander',
868
                'Y.Moodle.course.categoryexpander.init');
869
    }
870
 
871
    /**
872
     * Returns HTML to display the subcategories and courses in the given category
873
     *
874
     * This method is re-used by AJAX to expand content of not loaded category
875
     *
876
     * @param coursecat_helper $chelper various display options
877
     * @param core_course_category $coursecat
878
     * @param int $depth depth of the category in the current tree
879
     * @return string
880
     */
881
    protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) {
882
        $content = '';
883
        // Subcategories
884
        $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
885
 
886
        // AUTO show courses: Courses will be shown expanded if this is not nested category,
887
        // and number of courses no bigger than $CFG->courseswithsummarieslimit.
888
        $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO;
889
        if ($showcoursesauto && $depth) {
890
            // this is definitely collapsed mode
891
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
892
        }
893
 
894
        // Courses
895
        if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) {
896
            $courses = array();
897
            if (!$chelper->get_courses_display_option('nodisplay')) {
898
                $courses = $coursecat->get_courses($chelper->get_courses_display_options());
899
            }
900
            if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
901
                // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
902
                if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
903
                    $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id)));
904
                }
905
            }
906
            $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
907
        }
908
 
909
        if ($showcoursesauto) {
910
            // restore the show_courses back to AUTO
911
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO);
912
        }
913
 
914
        return $content;
915
    }
916
 
917
    /**
918
     * Returns HTML to display a course category as a part of a tree
919
     *
920
     * This is an internal function, to display a particular category and all its contents
921
     * use {@link core_course_renderer::course_category()}
922
     *
923
     * @param coursecat_helper $chelper various display options
924
     * @param core_course_category $coursecat
925
     * @param int $depth depth of this category in the current tree
926
     * @return string
927
     */
928
    protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) {
929
        // open category tag
930
        $classes = array('category');
931
        if (empty($coursecat->visible)) {
932
            $classes[] = 'dimmed_category';
933
        }
934
        if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
935
            // do not load content
936
            $categorycontent = '';
937
            $classes[] = 'notloaded';
938
            if ($coursecat->get_children_count() ||
939
                    ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) {
940
                $classes[] = 'with_children';
941
                $classes[] = 'collapsed';
942
            }
943
        } else {
944
            // load category content
945
            $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
946
            $classes[] = 'loaded';
947
            if (!empty($categorycontent)) {
948
                $classes[] = 'with_children';
949
                // Category content loaded with children.
950
                $this->categoryexpandedonload = true;
951
            }
952
        }
953
 
954
        // Make sure JS file to expand category content is included.
955
        $this->coursecat_include_js();
956
 
957
        $content = html_writer::start_tag('div', array(
958
            'class' => join(' ', $classes),
959
            'data-categoryid' => $coursecat->id,
960
            'data-depth' => $depth,
961
            'data-showcourses' => $chelper->get_show_courses(),
962
            'data-type' => self::COURSECAT_TYPE_CATEGORY,
963
        ));
964
 
965
        // category name
966
        $categoryname = $coursecat->get_formatted_name();
967
        $categoryname = html_writer::link(new moodle_url('/course/index.php',
968
                array('categoryid' => $coursecat->id)),
969
                $categoryname);
970
        if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT
971
                && ($coursescount = $coursecat->get_courses_count())) {
972
            $categoryname .= html_writer::tag('span', ' ('. $coursescount.')',
973
                    array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
974
        }
975
        $content .= html_writer::start_tag('div', array('class' => 'info'));
976
 
977
        $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn'));
978
        $content .= html_writer::end_tag('div'); // .info
979
 
980
        // add category content to the output
981
        $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
982
 
983
        $content .= html_writer::end_tag('div'); // .category
984
 
985
        // Return the course category tree HTML
986
        return $content;
987
    }
988
 
989
    /**
990
     * Returns HTML to display a tree of subcategories and courses in the given category
991
     *
992
     * @param coursecat_helper $chelper various display options
993
     * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree)
994
     * @return string
995
     */
996
    protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
997
        // Reset the category expanded flag for this course category tree first.
998
        $this->categoryexpandedonload = false;
999
        $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1000
        if (empty($categorycontent)) {
1001
            return '';
1002
        }
1003
 
1004
        // Start content generation
1005
        $content = '';
1006
        $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1007
        $content .= html_writer::start_tag('div', $attributes);
1008
 
1009
        if ($coursecat->get_children_count()) {
1010
            $classes = array(
1011
                'collapseexpand', 'aabtn'
1012
            );
1013
 
1014
            // Check if the category content contains subcategories with children's content loaded.
1015
            if ($this->categoryexpandedonload) {
1016
                $classes[] = 'collapse-all';
1017
                $linkname = get_string('collapseall');
1018
            } else {
1019
                $linkname = get_string('expandall');
1020
            }
1021
 
1022
            // Only show the collapse/expand if there are children to expand.
1023
            $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions'));
1024
            $content .= html_writer::link('#', $linkname, array('class' => implode(' ', $classes)));
1025
            $content .= html_writer::end_tag('div');
1026
            $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
1027
        }
1028
 
1029
        $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1030
 
1031
        $content .= html_writer::end_tag('div'); // .course_category_tree
1032
 
1033
        return $content;
1034
    }
1035
 
1036
    /**
1037
     * Renders HTML to display particular course category - list of it's subcategories and courses
1038
     *
1039
     * Invoked from /course/index.php
1040
     *
1041
     * @param int|stdClass|core_course_category $category
1042
     */
1043
    public function course_category($category) {
1044
        global $CFG;
1045
        $usertop = core_course_category::user_top();
1046
        if (empty($category)) {
1047
            $coursecat = $usertop;
1048
        } else if (is_object($category) && $category instanceof core_course_category) {
1049
            $coursecat = $category;
1050
        } else {
1051
            $coursecat = core_course_category::get(is_object($category) ? $category->id : $category);
1052
        }
1053
        $site = get_site();
1054
        $actionbar = new \core_course\output\category_action_bar($this->page, $coursecat);
1055
        $output = $this->render_from_template('core_course/category_actionbar', $actionbar->export_for_template($this));
1056
 
1057
        if (core_course_category::is_simple_site()) {
1058
            // There is only one category in the system, do not display link to it.
1059
            $strfulllistofcourses = get_string('fulllistofcourses');
1060
            $this->page->set_title($strfulllistofcourses);
1061
        } else if (!$coursecat->id || !$coursecat->is_uservisible()) {
1062
            $strcategories = get_string('categories');
1063
            $this->page->set_title($strcategories);
1064
        } else {
1065
            $strfulllistofcourses = get_string('fulllistofcourses');
1066
            $this->page->set_title($strfulllistofcourses);
1067
        }
1068
 
1069
        // Print current category description
1070
        $chelper = new coursecat_helper();
1071
        if ($description = $chelper->get_category_formatted_description($coursecat)) {
1072
            $output .= $this->box($description, array('class' => 'generalbox info'));
1073
        }
1074
 
1075
        // Prepare parameters for courses and categories lists in the tree
1076
        $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
1077
                ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));
1078
 
1079
        $coursedisplayoptions = array();
1080
        $catdisplayoptions = array();
1081
        $browse = optional_param('browse', null, PARAM_ALPHA);
1082
        $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
1083
        $page = optional_param('page', 0, PARAM_INT);
1084
        $baseurl = new moodle_url('/course/index.php');
1085
        if ($coursecat->id) {
1086
            $baseurl->param('categoryid', $coursecat->id);
1087
        }
1088
        if ($perpage != $CFG->coursesperpage) {
1089
            $baseurl->param('perpage', $perpage);
1090
        }
1091
        $coursedisplayoptions['limit'] = $perpage;
1092
        $catdisplayoptions['limit'] = $perpage;
1093
        if ($browse === 'courses' || !$coursecat->get_children_count()) {
1094
            $coursedisplayoptions['offset'] = $page * $perpage;
1095
            $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1096
            $catdisplayoptions['nodisplay'] = true;
1097
            $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1098
            $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
1099
        } else if ($browse === 'categories' || !$coursecat->get_courses_count()) {
1100
            $coursedisplayoptions['nodisplay'] = true;
1101
            $catdisplayoptions['offset'] = $page * $perpage;
1102
            $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1103
            $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1104
            $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1105
        } else {
1106
            // we have a category that has both subcategories and courses, display pagination separately
1107
            $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1108
            $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1109
        }
1110
        $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1111
 
1112
        // Display course category tree.
1113
        $output .= $this->coursecat_tree($chelper, $coursecat);
1114
 
1115
        return $output;
1116
    }
1117
 
1118
    /**
1119
     * Serves requests to /course/category.ajax.php
1120
     *
1121
     * In this renderer implementation it may expand the category content or
1122
     * course content.
1123
     *
1124
     * @return string
1125
     * @throws coding_exception
1126
     */
1127
    public function coursecat_ajax() {
1128
        global $DB, $CFG;
1129
 
1130
        $type = required_param('type', PARAM_INT);
1131
 
1132
        if ($type === self::COURSECAT_TYPE_CATEGORY) {
1133
            // This is a request for a category list of some kind.
1134
            $categoryid = required_param('categoryid', PARAM_INT);
1135
            $showcourses = required_param('showcourses', PARAM_INT);
1136
            $depth = required_param('depth', PARAM_INT);
1137
 
1138
            $category = core_course_category::get($categoryid);
1139
 
1140
            $chelper = new coursecat_helper();
1141
            $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
1142
            $coursedisplayoptions = array(
1143
                'limit' => $CFG->coursesperpage,
1144
                'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
1145
            );
1146
            $catdisplayoptions = array(
1147
                'limit' => $CFG->coursesperpage,
1148
                'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
1149
            );
1150
            $chelper->set_show_courses($showcourses)->
1151
                    set_courses_display_options($coursedisplayoptions)->
1152
                    set_categories_display_options($catdisplayoptions);
1153
 
1154
            return $this->coursecat_category_content($chelper, $category, $depth);
1155
        } else if ($type === self::COURSECAT_TYPE_COURSE) {
1156
            // This is a request for the course information.
1157
            $courseid = required_param('courseid', PARAM_INT);
1158
 
1159
            $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
1160
 
1161
            $chelper = new coursecat_helper();
1162
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1163
            return $this->coursecat_coursebox_content($chelper, $course);
1164
        } else {
1165
            throw new coding_exception('Invalid request type');
1166
        }
1167
    }
1168
 
1169
    /**
1170
     * Renders html to display search result page
1171
     *
1172
     * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1173
     * @return string
1174
     */
1175
    public function search_courses($searchcriteria) {
1176
        global $CFG;
1177
        $content = '';
1178
 
1179
        $search = '';
1180
        if (!empty($searchcriteria['search'])) {
1181
            $search = $searchcriteria['search'];
1182
        }
1183
        $content .= $this->course_search_form($search);
1184
 
1185
        if (!empty($searchcriteria)) {
1186
            // print search results
1187
 
1188
            $displayoptions = array('sort' => array('displayname' => 1));
1189
            // take the current page and number of results per page from query
1190
            $perpage = optional_param('perpage', 0, PARAM_RAW);
1191
            if ($perpage !== 'all') {
1192
                $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
1193
                $page = optional_param('page', 0, PARAM_INT);
1194
                $displayoptions['offset'] = $displayoptions['limit'] * $page;
1195
            }
1196
            // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1197
            $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1198
            $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1199
 
1200
            $class = 'course-search-result';
1201
            foreach ($searchcriteria as $key => $value) {
1202
                if (!empty($value)) {
1203
                    $class .= ' course-search-result-'. $key;
1204
                }
1205
            }
1206
            $chelper = new coursecat_helper();
1207
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1208
                    set_courses_display_options($displayoptions)->
1209
                    set_search_criteria($searchcriteria)->
1210
                    set_attributes(array('class' => $class));
1211
 
1212
            $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1213
            $totalcount = core_course_category::search_courses_count($searchcriteria);
1214
            $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1215
 
1216
            if (!$totalcount) {
1217
                if (!empty($searchcriteria['search'])) {
1218
                    $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1219
                } else {
1220
                    $content .= $this->heading(get_string('novalidcourses'));
1221
                }
1222
            } else {
1223
                $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1224
                $content .= $courseslist;
1225
            }
1226
        }
1227
        return $content;
1228
    }
1229
 
1230
    /**
1231
     * Renders html to print list of courses tagged with particular tag
1232
     *
1233
     * @param int $tagid id of the tag
1234
     * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1235
     *             are displayed on the page and the per-page limit may be bigger
1236
     * @param int $fromctx context id where the link was displayed, may be used by callbacks
1237
     *            to display items in the same context first
1238
     * @param int $ctx context id where to search for records
1239
     * @param bool $rec search in subcontexts as well
1240
     * @param array $displayoptions
1241
     * @return string empty string if no courses are marked with this tag or rendered list of courses
1242
     */
1243
    public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) {
1244
        global $CFG;
1245
        if (empty($displayoptions)) {
1246
            $displayoptions = array();
1247
        }
1248
        $showcategories = !core_course_category::is_simple_site();
1249
        $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0);
1250
        $chelper = new coursecat_helper();
1251
        $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
1252
        $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT :
1253
                    self::COURSECAT_SHOW_COURSES_EXPANDED)->
1254
                set_search_criteria($searchcriteria)->
1255
                set_courses_display_options($displayoptions)->
1256
                set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1257
                // (we set the same css class as in search results by tagid)
1258
        if ($totalcount = core_course_category::search_courses_count($searchcriteria)) {
1259
            $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1260
            if ($exclusivemode) {
1261
                return $this->coursecat_courses($chelper, $courses, $totalcount);
1262
            } else {
1263
                $tagfeed = new core_tag\output\tagfeed();
1264
                $img = $this->output->pix_icon('i/course', '');
1265
                foreach ($courses as $course) {
1266
                    $url = course_get_url($course);
1267
                    $imgwithlink = html_writer::link($url, $img);
1268
                    $coursename = html_writer::link($url, $course->get_formatted_name());
1269
                    $details = '';
1270
                    if ($showcategories && ($cat = core_course_category::get($course->category, IGNORE_MISSING))) {
1271
                        $details = get_string('category').': '.
1272
                                html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)),
1273
                                        $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
1274
                    }
1275
                    $tagfeed->add($imgwithlink, $coursename, $details);
1276
                }
1277
                return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output));
1278
            }
1279
        }
1280
        return '';
1281
    }
1282
 
1283
    /**
1284
     * Returns HTML to display one remote course
1285
     *
1286
     * @param stdClass $course remote course information, contains properties:
1287
           id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1288
     * @return string
1289
     */
1290
    protected function frontpage_remote_course(stdClass $course) {
1291
        $url = new moodle_url('/auth/mnet/jump.php', array(
1292
            'hostid' => $course->hostid,
1293
            'wantsurl' => '/course/view.php?id='. $course->remoteid
1294
        ));
1295
 
1296
        $output = '';
1297
        $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1298
        $output .= html_writer::start_tag('div', array('class' => 'info'));
1299
        $output .= html_writer::start_tag('h3', array('class' => 'coursename'));
1300
        $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
1301
        $output .= html_writer::end_tag('h3'); // .name
1302
        $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1303
        $output .= html_writer::end_tag('div'); // .info
1304
        $output .= html_writer::start_tag('div', array('class' => 'content'));
1305
        $output .= html_writer::start_tag('div', array('class' => 'summary'));
1306
        $options = new stdClass();
1307
        $options->noclean = true;
1308
        $options->para = false;
1309
        $options->overflowdiv = true;
1310
        $output .= format_text($course->summary, $course->summaryformat, $options);
1311
        $output .= html_writer::end_tag('div'); // .summary
1312
        $addinfo = format_string($course->hostname) . ' : '
1313
            . format_string($course->cat_name) . ' : '
1314
            . format_string($course->shortname);
1315
        $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1316
        $output .= html_writer::end_tag('div'); // .content
1317
        $output .= html_writer::end_tag('div'); // .coursebox
1318
        return $output;
1319
    }
1320
 
1321
    /**
1322
     * Returns HTML to display one remote host
1323
     *
1324
     * @param array $host host information, contains properties: name, url, count
1325
     * @return string
1326
     */
1327
    protected function frontpage_remote_host($host) {
1328
        $output = '';
1329
        $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1330
        $output .= html_writer::start_tag('div', array('class' => 'info'));
1331
        $output .= html_writer::start_tag('h3', array('class' => 'name'));
1332
        $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1333
        $output .= html_writer::end_tag('h3'); // .name
1334
        $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1335
        $output .= html_writer::end_tag('div'); // .info
1336
        $output .= html_writer::start_tag('div', array('class' => 'content'));
1337
        $output .= html_writer::start_tag('div', array('class' => 'summary'));
1338
        $output .= $host['count'] . ' ' . get_string('courses');
1339
        $output .= html_writer::end_tag('div'); // .content
1340
        $output .= html_writer::end_tag('div'); // .coursebox
1341
        return $output;
1342
    }
1343
 
1344
    /**
1345
     * Returns HTML to print list of courses user is enrolled to for the frontpage
1346
     *
1347
     * Also lists remote courses or remote hosts if MNET authorisation is used
1348
     *
1349
     * @return string
1350
     */
1351
    public function frontpage_my_courses() {
1352
        global $USER, $CFG, $DB;
1353
 
1354
        if (!isloggedin() or isguestuser()) {
1355
            return '';
1356
        }
1357
 
1358
        $output = '';
1359
        $courses  = enrol_get_my_courses('summary, summaryformat');
1360
        $rhosts   = array();
1361
        $rcourses = array();
1362
        if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
1363
            $rcourses = get_my_remotecourses($USER->id);
1364
            $rhosts   = get_my_remotehosts();
1365
        }
1366
 
1367
        if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1368
 
1369
            $chelper = new coursecat_helper();
1370
            $totalcount = count($courses);
1371
            if (count($courses) > $CFG->frontpagecourselimit) {
1372
                // There are more enrolled courses than we can display, display link to 'My courses'.
1373
                $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true);
1374
                $chelper->set_courses_display_options(array(
1375
                        'viewmoreurl' => new moodle_url('/my/courses.php'),
1376
                        'viewmoretext' => new lang_string('mycourses')
1377
                    ));
1378
            } else if (core_course_category::top()->is_uservisible()) {
1379
                // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system.
1380
                $chelper->set_courses_display_options(array(
1381
                        'viewmoreurl' => new moodle_url('/course/index.php'),
1382
                        'viewmoretext' => new lang_string('fulllistofcourses')
1383
                    ));
1384
                $totalcount = $DB->count_records('course') - 1;
1385
            }
1386
            $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1387
                    set_attributes(array('class' => 'frontpage-course-list-enrolled'));
1388
            $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
1389
 
1390
            // MNET
1391
            if (!empty($rcourses)) {
1392
                // at the IDP, we know of all the remote courses
1393
                $output .= html_writer::start_tag('div', array('class' => 'courses'));
1394
                foreach ($rcourses as $course) {
1395
                    $output .= $this->frontpage_remote_course($course);
1396
                }
1397
                $output .= html_writer::end_tag('div'); // .courses
1398
            } elseif (!empty($rhosts)) {
1399
                // non-IDP, we know of all the remote servers, but not courses
1400
                $output .= html_writer::start_tag('div', array('class' => 'courses'));
1401
                foreach ($rhosts as $host) {
1402
                    $output .= $this->frontpage_remote_host($host);
1403
                }
1404
                $output .= html_writer::end_tag('div'); // .courses
1405
            }
1406
        }
1407
        return $output;
1408
    }
1409
 
1410
    /**
1411
     * Returns HTML to print list of available courses for the frontpage
1412
     *
1413
     * @return string
1414
     */
1415
    public function frontpage_available_courses() {
1416
        global $CFG;
1417
 
1418
        $chelper = new coursecat_helper();
1419
        $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1420
                set_courses_display_options(array(
1421
                    'recursive' => true,
1422
                    'limit' => $CFG->frontpagecourselimit,
1423
                    'viewmoreurl' => new moodle_url('/course/index.php'),
1424
                    'viewmoretext' => new lang_string('fulllistofcourses')));
1425
 
1426
        $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
1427
        $courses = core_course_category::top()->get_courses($chelper->get_courses_display_options());
1428
        $totalcount = core_course_category::top()->get_courses_count($chelper->get_courses_display_options());
1429
        if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) {
1430
            // Print link to create a new course, for the 1st available category.
1431
            return $this->add_new_course_button();
1432
        }
1433
        return $this->coursecat_courses($chelper, $courses, $totalcount);
1434
    }
1435
 
1436
    /**
1437
     * Returns HTML to the "add new course" button for the page
1438
     *
1439
     * @return string
1440
     */
1441
    public function add_new_course_button() {
1442
        global $CFG;
1443
        // Print link to create a new course, for the 1st available category.
1444
        $output = $this->container_start('buttons');
1445
        $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
1446
        $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
1447
        $output .= $this->container_end('buttons');
1448
        return $output;
1449
    }
1450
 
1451
    /**
1452
     * Returns HTML to print tree with course categories and courses for the frontpage
1453
     *
1454
     * @return string
1455
     */
1456
    public function frontpage_combo_list() {
1457
        global $CFG;
1458
        // TODO MDL-10965 improve.
1459
        $tree = core_course_category::top();
1460
        if (!$tree->get_children_count()) {
1461
            return '';
1462
        }
1463
        $chelper = new coursecat_helper();
1464
        $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1465
            set_categories_display_options(array(
1466
                'limit' => $CFG->coursesperpage,
1467
                'viewmoreurl' => new moodle_url('/course/index.php',
1468
                        array('browse' => 'categories', 'page' => 1))
1469
            ))->
1470
            set_courses_display_options(array(
1471
                'limit' => $CFG->coursesperpage,
1472
                'viewmoreurl' => new moodle_url('/course/index.php',
1473
                        array('browse' => 'courses', 'page' => 1))
1474
            ))->
1475
            set_attributes(array('class' => 'frontpage-category-combo'));
1476
        return $this->coursecat_tree($chelper, $tree);
1477
    }
1478
 
1479
    /**
1480
     * Returns HTML to print tree of course categories (with number of courses) for the frontpage
1481
     *
1482
     * @return string
1483
     */
1484
    public function frontpage_categories_list() {
1485
        global $CFG;
1486
        // TODO MDL-10965 improve.
1487
        $tree = core_course_category::top();
1488
        if (!$tree->get_children_count()) {
1489
            return '';
1490
        }
1491
        $chelper = new coursecat_helper();
1492
        $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1493
                set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
1494
                set_categories_display_options(array(
1495
                    'limit' => $CFG->coursesperpage,
1496
                    'viewmoreurl' => new moodle_url('/course/index.php',
1497
                            array('browse' => 'categories', 'page' => 1))
1498
                ))->
1499
                set_attributes(array('class' => 'frontpage-category-names'));
1500
        return $this->coursecat_tree($chelper, $tree);
1501
    }
1502
 
1503
    /**
1504
     * Renders the activity information.
1505
     *
1506
     * Defer to template.
1507
     *
1508
     * @deprecated since Moodle 4.3 MDL-78744
1509
     * @todo MDL-78926 This method will be deleted in Moodle 4.7
1510
     * @param \core_course\output\activity_information $page
1511
     * @return string html for the page
1512
     */
1513
    public function render_activity_information(\core_course\output\activity_information $page) {
1514
        debugging('render_activity_information method is deprecated.', DEBUG_DEVELOPER);
1515
        $data = $page->export_for_template($this->output);
1516
        return $this->output->render_from_template('core_course/activity_info', $data);
1517
    }
1518
 
1519
    /**
1520
     * Renders the activity navigation.
1521
     *
1522
     * Defer to template.
1523
     *
1524
     * @param \core_course\output\activity_navigation $page
1525
     * @return string html for the page
1526
     */
1527
    public function render_activity_navigation(\core_course\output\activity_navigation $page) {
1528
        $data = $page->export_for_template($this->output);
1529
        return $this->output->render_from_template('core_course/activity_navigation', $data);
1530
    }
1531
 
1532
    /**
1533
     * Display waiting information about backup size during uploading backup process
1534
     * @param object $backupfile the backup stored_file
1535
     * @return $html string
1536
     */
1537
    public function sendingbackupinfo($backupfile) {
1538
        $sizeinfo = new stdClass();
1539
        $sizeinfo->total = number_format($backupfile->get_filesize() / 1000000, 2);
1540
        $html = html_writer::tag('div', get_string('sendingsize', 'hub', $sizeinfo),
1541
            array('class' => 'courseuploadtextinfo'));
1542
        return $html;
1543
    }
1544
 
1545
    /**
1546
     * Hub information (logo - name - description - link)
1547
     * @param object $hubinfo
1548
     * @return string html code
1549
     */
1550
    public function hubinfo($hubinfo) {
1551
        $screenshothtml = html_writer::empty_tag('img',
1552
            array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name']));
1553
        $hubdescription = html_writer::tag('div', $screenshothtml,
1554
            array('class' => 'hubscreenshot'));
1555
 
1556
        $hubdescription .= html_writer::tag('a', $hubinfo['name'],
1557
            array('class' => 'hublink', 'href' => $hubinfo['url'],
1558
                'onclick' => 'this.target="_blank"'));
1559
 
1560
        $hubdescription .= html_writer::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN),
1561
            array('class' => 'hubdescription'));
1562
        $hubdescription = html_writer::tag('div', $hubdescription, array('class' => 'hubinfo clearfix'));
1563
 
1564
        return $hubdescription;
1565
    }
1566
 
1567
    /**
1568
     * Output frontpage summary text and frontpage modules (stored as section 1 in site course)
1569
     *
1570
     * This may be disabled in settings
1571
     *
1572
     * @return string
1573
     */
1574
    public function frontpage_section1() {
1575
        global $SITE, $USER;
1576
 
1577
        $output = '';
1578
        $editing = $this->page->user_is_editing();
1579
 
1580
        if ($editing) {
1581
            // Make sure section with number 1 exists.
1582
            course_create_sections_if_missing($SITE, 1);
1583
        }
1584
 
1585
        $modinfo = get_fast_modinfo($SITE);
1586
        $section = $modinfo->get_section_info(1);
1587
 
1588
 
1589
        if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
1590
 
1591
            $format = course_get_format($SITE);
1592
            $frontpageclass = $format->get_output_classname('content\\frontpagesection');
1593
            $frontpagesection = new $frontpageclass($format, $section);
1594
 
1595
            // The course outputs works with format renderers, not with course renderers.
1596
            $renderer = $format->get_renderer($this->page);
1597
            $output .= $renderer->render($frontpagesection);
1598
        }
1599
 
1600
        return $output;
1601
    }
1602
 
1603
    /**
1604
     * Output news for the frontpage (extract from site-wide news forum)
1605
     *
1606
     * @param stdClass $forum record from db table 'forum' that represents the site news forum
1607
     * @return string
1608
     */
1609
    protected function frontpage_news($forum) {
1610
        global $CFG, $SITE, $SESSION, $USER;
1611
        require_once($CFG->dirroot .'/mod/forum/lib.php');
1612
 
1613
        $output = '';
1614
 
1615
        if (isloggedin()) {
1616
            $SESSION->fromdiscussion = $CFG->wwwroot;
1617
            $subtext = '';
1618
            if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) {
1619
                if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
1620
                    $subtext = get_string('unsubscribe', 'forum');
1621
                }
1622
            } else {
1623
                $subtext = get_string('subscribe', 'forum');
1624
            }
1625
            $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey()));
1626
            $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
1627
        }
1628
 
1629
        $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
1630
        $context = context_module::instance($coursemodule->id);
1631
 
1632
        $entityfactory = mod_forum\local\container::get_entity_factory();
1633
        $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE);
1634
 
1635
        $rendererfactory = mod_forum\local\container::get_renderer_factory();
1636
        $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity);
1637
        $cm = \cm_info::create($coursemodule);
1638
        return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems);
1639
    }
1640
 
1641
    /**
1642
     * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
1643
     *
1644
     * @param string $skipdivid
1645
     * @param string $contentsdivid
1646
     * @param string $header Header of the part
1647
     * @param string $contents Contents of the part
1648
     * @return string
1649
     */
1650
    protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
1651
        if (strval($contents) === '') {
1652
            return '';
1653
        }
1654
        $output = html_writer::link('#' . $skipdivid,
1655
            get_string('skipa', 'access', core_text::strtolower(strip_tags($header))),
1656
            array('class' => 'skip-block skip aabtn'));
1657
 
1658
        // Wrap frontpage part in div container.
1659
        $output .= html_writer::start_tag('div', array('id' => $contentsdivid));
1660
        $output .= $this->heading($header);
1661
 
1662
        $output .= $contents;
1663
 
1664
        // End frontpage part div container.
1665
        $output .= html_writer::end_tag('div');
1666
 
1667
        $output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
1668
        return $output;
1669
    }
1670
 
1671
    /**
1672
     * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
1673
     *
1674
     * @return string
1675
     */
1676
    public function frontpage() {
1677
        global $CFG, $SITE;
1678
 
1679
        $output = '';
1680
 
1681
        if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
1682
            $frontpagelayout = $CFG->frontpageloggedin;
1683
        } else {
1684
            $frontpagelayout = $CFG->frontpage;
1685
        }
1686
 
1687
        foreach (explode(',', $frontpagelayout) as $v) {
1688
            switch ($v) {
1689
                // Display the main part of the front page.
1690
                case FRONTPAGENEWS:
1691
                    if ($SITE->newsitems) {
1692
                        // Print forums only when needed.
1693
                        require_once($CFG->dirroot .'/mod/forum/lib.php');
1694
                        if (($newsforum = forum_get_course_forum($SITE->id, 'news')) &&
1695
                                ($forumcontents = $this->frontpage_news($newsforum))) {
1696
                            $newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id];
1697
                            $output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
1698
                                $newsforumcm->get_formatted_name(), $forumcontents);
1699
                        }
1700
                    }
1701
                    break;
1702
 
1703
                case FRONTPAGEENROLLEDCOURSELIST:
1704
                    $mycourseshtml = $this->frontpage_my_courses();
1705
                    if (!empty($mycourseshtml)) {
1706
                        $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
1707
                            get_string('mycourses'), $mycourseshtml);
1708
                    }
1709
                    break;
1710
 
1711
                case FRONTPAGEALLCOURSELIST:
1712
                    $availablecourseshtml = $this->frontpage_available_courses();
1713
                    $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
1714
                        get_string('availablecourses'), $availablecourseshtml);
1715
                    break;
1716
 
1717
                case FRONTPAGECATEGORYNAMES:
1718
                    $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
1719
                        get_string('categories'), $this->frontpage_categories_list());
1720
                    break;
1721
 
1722
                case FRONTPAGECATEGORYCOMBO:
1723
                    $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
1724
                        get_string('courses'), $this->frontpage_combo_list());
1725
                    break;
1726
 
1727
                case FRONTPAGECOURSESEARCH:
1728
                    $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
1729
                    break;
1730
 
1731
            }
1732
            $output .= '<br />';
1733
        }
1734
 
1735
        return $output;
1736
    }
1737
}
1738
 
1739
/**
1740
 * Class storing display options and functions to help display course category and/or courses lists
1741
 *
1742
 * This is a wrapper for core_course_category objects that also stores display options
1743
 * and functions to retrieve sorted and paginated lists of categories/courses.
1744
 *
1745
 * If theme overrides methods in core_course_renderers that access this class
1746
 * it may as well not use this class at all or extend it.
1747
 *
1748
 * @package   core
1749
 * @copyright 2013 Marina Glancy
1750
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1751
 */
1752
class coursecat_helper {
1753
    /** @var string [none, collapsed, expanded] how (if) display courses list */
1754
    protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
1755
    /** @var int depth to expand subcategories in the tree (deeper subcategories will be loaded by AJAX or proceed to category page by clicking on category name) */
1756
    protected $subcatdepth = 1;
1757
    /** @var array options to display courses list */
1758
    protected $coursesdisplayoptions = array();
1759
    /** @var array options to display subcategories list */
1760
    protected $categoriesdisplayoptions = array();
1761
    /** @var array additional HTML attributes */
1762
    protected $attributes = array();
1763
    /** @var array search criteria if the list is a search result */
1764
    protected $searchcriteria = null;
1765
 
1766
    /**
1767
     * Sets how (if) to show the courses - none, collapsed, expanded, etc.
1768
     *
1769
     * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
1770
     * @return coursecat_helper
1771
     */
1772
    public function set_show_courses($showcourses) {
1773
        $this->showcourses = $showcourses;
1774
        // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses()
1775
        // and core_course_category::search_courses().
1776
        $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
1777
        $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
1778
        $this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED;
1779
        return $this;
1780
    }
1781
 
1782
    /**
1783
     * Returns how (if) to show the courses - none, collapsed, expanded, etc.
1784
     *
1785
     * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
1786
     */
1787
    public function get_show_courses() {
1788
        return $this->showcourses;
1789
    }
1790
 
1791
    /**
1792
     * Sets the maximum depth to expand subcategories in the tree
1793
     *
1794
     * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
1795
     *
1796
     * @param int $subcatdepth
1797
     * @return coursecat_helper
1798
     */
1799
    public function set_subcat_depth($subcatdepth) {
1800
        $this->subcatdepth = $subcatdepth;
1801
        return $this;
1802
    }
1803
 
1804
    /**
1805
     * Returns the maximum depth to expand subcategories in the tree
1806
     *
1807
     * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
1808
     *
1809
     * @return int
1810
     */
1811
    public function get_subcat_depth() {
1812
        return $this->subcatdepth;
1813
    }
1814
 
1815
    /**
1816
     * Sets options to display list of courses
1817
     *
1818
     * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses()
1819
     *
1820
     * Options that core_course_category::get_courses() accept:
1821
     *    - recursive - return courses from subcategories as well. Use with care,
1822
     *      this may be a huge list!
1823
     *    - summary - preloads fields 'summary' and 'summaryformat'
1824
     *    - coursecontacts - preloads course contacts
1825
     *    - customfields - preloads custom fields data
1826
     *    - isenrolled - preloads indication whether this user is enrolled in the course
1827
     *    - sort - list of fields to sort. Example
1828
     *             array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
1829
     *             will sort by idnumber asc, shortname asc and id desc.
1830
     *             Default: array('sortorder' => 1)
1831
     *             Only cached fields may be used for sorting!
1832
     *    - offset
1833
     *    - limit - maximum number of children to return, 0 or null for no limit
1834
     *
1835
     * Options summary and coursecontacts are filled automatically in the set_show_courses()
1836
     *
1837
     * Also renderer can set here any additional options it wants to pass between renderer functions.
1838
     *
1839
     * @param array $options
1840
     * @return coursecat_helper
1841
     */
1842
    public function set_courses_display_options($options) {
1843
        $this->coursesdisplayoptions = $options;
1844
        $this->set_show_courses($this->showcourses); // this will calculate special display options
1845
        return $this;
1846
    }
1847
 
1848
    /**
1849
     * Sets one option to display list of courses
1850
     *
1851
     * @see coursecat_helper::set_courses_display_options()
1852
     *
1853
     * @param string $key
1854
     * @param mixed $value
1855
     * @return coursecat_helper
1856
     */
1857
    public function set_courses_display_option($key, $value) {
1858
        $this->coursesdisplayoptions[$key] = $value;
1859
        return $this;
1860
    }
1861
 
1862
    /**
1863
     * Return the specified option to display list of courses
1864
     *
1865
     * @param string $optionname option name
1866
     * @param mixed $defaultvalue default value for option if it is not specified
1867
     * @return mixed
1868
     */
1869
    public function get_courses_display_option($optionname, $defaultvalue = null) {
1870
        if (array_key_exists($optionname, $this->coursesdisplayoptions)) {
1871
            return $this->coursesdisplayoptions[$optionname];
1872
        } else {
1873
            return $defaultvalue;
1874
        }
1875
    }
1876
 
1877
    /**
1878
     * Returns all options to display the courses
1879
     *
1880
     * This array is usually passed to {@link core_course_category::get_courses()} or
1881
     * {@link core_course_category::search_courses()}
1882
     *
1883
     * @return array
1884
     */
1885
    public function get_courses_display_options() {
1886
        return $this->coursesdisplayoptions;
1887
    }
1888
 
1889
    /**
1890
     * Sets options to display list of subcategories
1891
     *
1892
     * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children().
1893
     * Any other options may be used by renderer functions
1894
     *
1895
     * @param array $options
1896
     * @return coursecat_helper
1897
     */
1898
    public function set_categories_display_options($options) {
1899
        $this->categoriesdisplayoptions = $options;
1900
        return $this;
1901
    }
1902
 
1903
    /**
1904
     * Return the specified option to display list of subcategories
1905
     *
1906
     * @param string $optionname option name
1907
     * @param mixed $defaultvalue default value for option if it is not specified
1908
     * @return mixed
1909
     */
1910
    public function get_categories_display_option($optionname, $defaultvalue = null) {
1911
        if (array_key_exists($optionname, $this->categoriesdisplayoptions)) {
1912
            return $this->categoriesdisplayoptions[$optionname];
1913
        } else {
1914
            return $defaultvalue;
1915
        }
1916
    }
1917
 
1918
    /**
1919
     * Returns all options to display list of subcategories
1920
     *
1921
     * This array is usually passed to {@link core_course_category::get_children()}
1922
     *
1923
     * @return array
1924
     */
1925
    public function get_categories_display_options() {
1926
        return $this->categoriesdisplayoptions;
1927
    }
1928
 
1929
    /**
1930
     * Sets additional general options to pass between renderer functions, usually HTML attributes
1931
     *
1932
     * @param array $attributes
1933
     * @return coursecat_helper
1934
     */
1935
    public function set_attributes($attributes) {
1936
        $this->attributes = $attributes;
1937
        return $this;
1938
    }
1939
 
1940
    /**
1941
     * Return all attributes and erases them so they are not applied again
1942
     *
1943
     * @param string $classname adds additional class name to the beginning of $attributes['class']
1944
     * @return array
1945
     */
1946
    public function get_and_erase_attributes($classname) {
1947
        $attributes = $this->attributes;
1948
        $this->attributes = array();
1949
        if (empty($attributes['class'])) {
1950
            $attributes['class'] = '';
1951
        }
1952
        $attributes['class'] = $classname . ' '. $attributes['class'];
1953
        return $attributes;
1954
    }
1955
 
1956
    /**
1957
     * Sets the search criteria if the course is a search result
1958
     *
1959
     * Search string will be used to highlight terms in course name and description
1960
     *
1961
     * @param array $searchcriteria
1962
     * @return coursecat_helper
1963
     */
1964
    public function set_search_criteria($searchcriteria) {
1965
        $this->searchcriteria = $searchcriteria;
1966
        return $this;
1967
    }
1968
 
1969
    /**
1970
     * Returns formatted and filtered description of the given category
1971
     *
1972
     * @param core_course_category $coursecat category
1973
     * @param stdClass|array $options format options, by default [noclean,overflowdiv],
1974
     *     if context is not specified it will be added automatically
1975
     * @return string|null
1976
     */
1977
    public function get_category_formatted_description($coursecat, $options = null) {
1978
        if ($coursecat->id && $coursecat->is_uservisible() && !empty($coursecat->description)) {
1979
            if (!isset($coursecat->descriptionformat)) {
1980
                $descriptionformat = FORMAT_MOODLE;
1981
            } else {
1982
                $descriptionformat = $coursecat->descriptionformat;
1983
            }
1984
            if ($options === null) {
1985
                $options = array('noclean' => true, 'overflowdiv' => true);
1986
            } else {
1987
                $options = (array)$options;
1988
            }
1989
            $context = context_coursecat::instance($coursecat->id);
1990
            if (!isset($options['context'])) {
1991
                $options['context'] = $context;
1992
            }
1993
            $text = file_rewrite_pluginfile_urls($coursecat->description,
1994
                    'pluginfile.php', $context->id, 'coursecat', 'description', null);
1995
            return format_text($text, $descriptionformat, $options);
1996
        }
1997
        return null;
1998
    }
1999
 
2000
    /**
2001
     * Returns given course's summary with proper embedded files urls and formatted
2002
     *
2003
     * @param core_course_list_element $course
2004
     * @param array|stdClass $options additional formatting options
2005
     * @return string
2006
     */
2007
    public function get_course_formatted_summary($course, $options = array()) {
2008
        global $CFG;
2009
        require_once($CFG->libdir. '/filelib.php');
2010
        if (!$course->has_summary()) {
2011
            return '';
2012
        }
2013
        $options = (array)$options;
2014
        $context = context_course::instance($course->id);
2015
        if (!isset($options['context'])) {
2016
            $options['context'] = $context;
2017
        }
2018
        $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null);
2019
        $summary = format_text($summary, $course->summaryformat, $options);
2020
        if (!empty($this->searchcriteria['search'])) {
2021
            $summary = highlight($this->searchcriteria['search'], $summary);
2022
        }
2023
        return $summary;
2024
    }
2025
 
2026
    /**
2027
     * Returns course name as it is configured to appear in courses lists formatted to course context
2028
     *
2029
     * @param core_course_list_element $course
2030
     * @param array|stdClass $options additional formatting options
2031
     * @return string
2032
     */
2033
    public function get_course_formatted_name($course, $options = array()) {
2034
        $options = (array)$options;
2035
        if (!isset($options['context'])) {
2036
            $options['context'] = context_course::instance($course->id);
2037
        }
2038
        $name = format_string(get_course_display_name_for_list($course), true, $options);
2039
        if (!empty($this->searchcriteria['search'])) {
2040
            $name = highlight($this->searchcriteria['search'], $name);
2041
        }
2042
        return $name;
2043
    }
2044
}