Merge branch 'MDL-81111-master' of https://github.com/tailetan/moodle
[moodle.git] / course / renderer.php
blob4bfd1a5b1c607c31fb9dc07eeff2a88a0906cc6b
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.
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/>.
17 /**
18 * Renderer for use with the course section and all the goodness that falls
19 * within it.
21 * This renderer should contain methods useful to courses, and categories.
23 * @package moodlecore
24 * @copyright 2010 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * The core course renderer
33 * Can be retrieved with the following:
34 * $renderer = $PAGE->get_renderer('core','course');
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;
44 const COURSECAT_TYPE_CATEGORY = 0;
45 const COURSECAT_TYPE_COURSE = 1;
47 /**
48 * A cache of strings
49 * @var stdClass
51 protected $strings;
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
59 protected $categoryexpandedonload = false;
61 /**
62 * Override the constructor so that we can initialise the string cache
64 * @param moodle_page $page
65 * @param string $target
67 public function __construct(moodle_page $page, $target) {
68 $this->strings = new stdClass;
69 $courseid = $page->course->id;
70 parent::__construct($page, $target);
73 /**
74 * @deprecated since 3.2
76 protected function add_modchoosertoggle() {
77 throw new coding_exception('core_course_renderer::add_modchoosertoggle() can not be used anymore.');
80 /**
81 * Renders course info box.
83 * @param stdClass $course
84 * @return string
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;
96 /**
97 * Renderers a structured array of courses and categories into a nice XHTML tree structure.
99 * @deprecated since 2.5
101 * @param array $ignored argument ignored
102 * @return string
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();
110 * Renderers a category for use with course_category_tree
112 * @deprecated since 2.5
114 * @param array $category
115 * @param int $depth
116 * @return string
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 '';
124 * Render a modchooser.
126 * @param renderable $modchooser The chooser.
127 * @return string
129 public function render_modchooser(renderable $modchooser) {
130 return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
134 * @deprecated since 3.9
136 public function course_modchooser() {
137 throw new coding_exception('course_modchooser() can not be used anymore, please use course_activitychooser() instead.');
141 * Build the HTML for the module chooser javascript popup.
143 * @param int $courseid The course id to fetch modules for.
144 * @param int|null $sectionnum The section number to fetch modules for.
145 * @return string
147 public function course_activitychooser($courseid, ?int $sectionnum = null) {
149 if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
150 return '';
153 // Build an object of config settings that we can then hook into in the Activity Chooser.
154 $chooserconfig = (object) [
155 'tabmode' => get_config('core', 'activitychoosertabmode'),
157 $this->page->requires->js_call_amd('core_course/activitychooser', 'init', [$courseid, $chooserconfig, $sectionnum]);
159 return '';
163 * Build the HTML for a specified set of modules
165 * @param array $modules A set of modules as used by the
166 * course_modchooser_module function
167 * @return string The composed HTML for the module
169 protected function course_modchooser_module_types($modules) {
170 debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
171 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
172 return '';
176 * Return the HTML for the specified module adding any required classes
178 * @param object $module An object containing the title, and link. An
179 * icon, and help text may optionally be specified. If the module
180 * contains subtypes in the types option, then these will also be
181 * displayed.
182 * @param array $classes Additional classes to add to the encompassing
183 * div element
184 * @return string The composed HTML for the module
186 protected function course_modchooser_module($module, $classes = array('option')) {
187 debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
188 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
189 return '';
192 protected function course_modchooser_title($title, $identifier = null) {
193 debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
194 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
195 return '';
199 * @deprecated since 4.0 - please do not use this function any more.
201 public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) {
203 throw new coding_exception(
204 'course_section_cm_edit_actions can not be used any more. Please, use ' .
205 'core_courseformat\\output\\local\\content\\cm\\controlmenu instead.'
210 * Renders HTML for the menus to add activities and resources to the current course
212 * Renders the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback.
214 * @param stdClass $course
215 * @param int $section relative section number (field course_sections.section)
216 * @param int $sectionreturn The section to link back to
217 * @param array $displayoptions additional display options, for example blocks add
218 * option 'inblock' => true, suggesting to display controls vertically
219 * @return string
221 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
222 // Check to see if user can add menus.
223 if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
224 || !$this->page->user_is_editing()) {
225 return '';
228 $data = [
229 'sectionnum' => $section,
230 'sectionreturn' => $sectionreturn
232 $ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
234 // Load the JS for the modal.
235 $this->course_activitychooser($course->id, $section);
237 return $ajaxcontrol;
241 * Renders html to display a course search form
243 * @param string $value default value to populate the search field
244 * @return string
246 public function course_search_form($value = '') {
248 $data = [
249 'action' => \core_search\manager::get_course_search_url(),
250 'btnclass' => 'btn-primary',
251 'inputname' => 'q',
252 'searchstring' => get_string('searchcourses'),
253 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
254 'query' => $value
256 return $this->render_from_template('core/search_input', $data);
260 * @deprecated since Moodle 3.11
262 public function course_section_cm_completion() {
263 throw new coding_exception(__FUNCTION__ . ' is deprecated. Use the activity_completion output component instead.');
267 * @deprecated since 4.0 - please do not use this function any more.
269 public function is_cm_conditionally_hidden(cm_info $mod) {
271 throw new coding_exception(
272 'is_cm_conditionally_hidden can not be used any more. Please, use ' .
273 '\core_availability\info_module::is_available_for_all instead'
278 * @deprecated since 4.0 - please do not use this function any more.
280 public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
282 throw new coding_exception(
283 'course_section_cm_name can not be used any more. Please, use ' .
284 'core_courseformat\\output\\local\\content\\cm\\cmname class instead.'
289 * @deprecated since 4.0 - please do not use this function any more.
291 protected function course_section_cm_classes(cm_info $mod) {
293 throw new coding_exception(
294 'course_section_cm_classes can not be used any more. Now it is part of core_courseformat\\output\\local\\content\\cm'
299 * @deprecated since 4.0 - please do not use this function any more.
301 public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) {
303 throw new coding_exception(
304 'course_section_cm_name_title can not be used any more. Please, use ' .
305 'core_courseformat\\output\\local\\cm\\title class instead'
310 * @deprecated since 4.0 - please do not use this function any more.
312 public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
314 throw new coding_exception(
315 'course_section_cm_text can not be used any more. Now it is part of core_courseformat\\output\\local\\content\\cm'
320 * @deprecated since 4.0 - please do not use this function any more.
322 public function availability_info($text, $additionalclasses = '') {
324 throw new coding_exception(
325 'availability_info can not be used any more. Please, use ' .
326 'core_courseformat\\output\\local\\content\\section\\availability instead'
331 * @deprecated since 4.0 - please do not use this function any more.
333 public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
335 throw new coding_exception(
336 'course_section_cm_availability can not be used any more. Please, use ' .
337 'core_courseformat\\output\\local\\content\\cm\\availability instead'
342 * @deprecated since 4.0 - use core_course output components or course_format::course_section_updated_cm_item instead.
344 public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
346 throw new coding_exception(
347 'course_section_cm_list_item can not be used any more. Please, use renderer course_section_updated_cm_item instead'
352 * @deprecated since 4.0 - use core_course output components instead.
354 public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
356 throw new coding_exception(
357 'course_section_cm can not be used any more. Please, use core_courseformat\\output\\content\\cm output class instead'
362 * Message displayed to the user when they try to access unavailable activity following URL
364 * This method is a very simplified version of {@link course_section_cm()} to be part of the error
365 * notification only. It also does not check if module is visible on course page or not.
367 * The message will be displayed inside notification!
369 * @param cm_info $cm
370 * @return string
372 public function course_section_cm_unavailable_error_message(cm_info $cm) {
373 if ($cm->uservisible) {
374 return null;
376 if (!$cm->availableinfo) {
377 return get_string('activityiscurrentlyhidden');
380 $altname = get_accesshide(' ' . $cm->modfullname);
381 $name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(),
382 'class' => 'iconlarge activityicon', 'alt' => '')) .
383 html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
384 $formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
385 return html_writer::div($name, 'activityinstance-error') .
386 html_writer::div($formattedinfo, 'availabilityinfo-error');
390 * @deprecated since 4.0 - use core_course output components instead.
392 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = []) {
394 throw new coding_exception(
395 'course_section_cm_list can not be used any more. Please, use ' .
396 'core_courseformat\\output\\local\\content\\section\\cmlist class instead'
401 * Displays a custom list of courses with paging bar if necessary
403 * If $paginationurl is specified but $totalcount is not, the link 'View more'
404 * appears under the list.
406 * If both $paginationurl and $totalcount are specified, and $totalcount is
407 * bigger than count($courses), a paging bar is displayed above and under the
408 * courses list.
410 * @param array $courses array of course records (or instances of core_course_list_element) to show on this page
411 * @param bool $showcategoryname whether to add category name to the course description
412 * @param string $additionalclasses additional CSS classes to add to the div.courses
413 * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
414 * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
415 * @param int $page current page number (defaults to 0 referring to the first page)
416 * @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
417 * @return string
419 public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) {
420 global $CFG;
421 // create instance of coursecat_helper to pass display options to function rendering courses list
422 $chelper = new coursecat_helper();
423 if ($showcategoryname) {
424 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT);
425 } else {
426 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
428 if ($totalcount !== null && $paginationurl !== null) {
429 // add options to display pagination
430 if ($perpage === null) {
431 $perpage = $CFG->coursesperpage;
433 $chelper->set_courses_display_options(array(
434 'limit' => $perpage,
435 'offset' => ((int)$page) * $perpage,
436 'paginationurl' => $paginationurl,
438 } else if ($paginationurl !== null) {
439 // add options to display 'View more' link
440 $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl));
441 $totalcount = count($courses) + 1; // has to be bigger than count($courses) otherwise link will not be displayed
443 $chelper->set_attributes(array('class' => $additionalclasses));
444 $content = $this->coursecat_courses($chelper, $courses, $totalcount);
445 return $content;
449 * Returns HTML to display course name.
451 * @param coursecat_helper $chelper
452 * @param core_course_list_element $course
453 * @return string
455 protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string {
456 $content = '';
457 if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) {
458 $nametag = 'h3';
459 } else {
460 $nametag = 'div';
462 $coursename = $chelper->get_course_formatted_name($course);
463 $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]),
464 $coursename, ['class' => $course->visible ? 'aalink' : 'aalink dimmed']);
465 $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']);
466 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
467 $content .= html_writer::start_tag('div', ['class' => 'moreinfo']);
468 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
469 if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles()
470 || $course->has_custom_fields()) {
471 $url = new moodle_url('/course/info.php', ['id' => $course->id]);
472 $image = $this->output->pix_icon('i/info', $this->strings->summary);
473 $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]);
474 // Make sure JS file to expand course content is included.
475 $this->coursecat_include_js();
478 $content .= html_writer::end_tag('div');
479 return $content;
483 * Returns HTML to display course enrolment icons.
485 * @param core_course_list_element $course
486 * @return string
488 protected function course_enrolment_icons(core_course_list_element $course): string {
489 $content = '';
490 if ($icons = enrol_get_course_info_icons($course)) {
491 $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']);
492 foreach ($icons as $icon) {
493 $content .= $this->render($icon);
495 $content .= html_writer::end_tag('div');
497 return $content;
501 * Displays one course in the list of courses.
503 * This is an internal function, to display an information about just one course
504 * please use {@link core_course_renderer::course_info_box()}
506 * @param coursecat_helper $chelper various display options
507 * @param core_course_list_element|stdClass $course
508 * @param string $additionalclasses additional classes to add to the main <div> tag (usually
509 * depend on the course position in list - first/last/even/odd)
510 * @return string
512 protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') {
513 if (!isset($this->strings->summary)) {
514 $this->strings->summary = get_string('summary');
516 if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) {
517 return '';
519 if ($course instanceof stdClass) {
520 $course = new core_course_list_element($course);
522 $content = '';
523 $classes = trim('coursebox clearfix '. $additionalclasses);
524 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
525 $classes .= ' collapsed';
528 // .coursebox
529 $content .= html_writer::start_tag('div', array(
530 'class' => $classes,
531 'data-courseid' => $course->id,
532 'data-type' => self::COURSECAT_TYPE_COURSE,
535 $content .= html_writer::start_tag('div', array('class' => 'info'));
536 $content .= $this->course_name($chelper, $course);
537 $content .= $this->course_enrolment_icons($course);
538 $content .= html_writer::end_tag('div');
540 $content .= html_writer::start_tag('div', array('class' => 'content'));
541 $content .= $this->coursecat_coursebox_content($chelper, $course);
542 $content .= html_writer::end_tag('div');
544 $content .= html_writer::end_tag('div'); // .coursebox
545 return $content;
549 * Returns HTML to display course summary.
551 * @param coursecat_helper $chelper
552 * @param core_course_list_element $course
553 * @return string
555 protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string {
556 $content = '';
557 if ($course->has_summary()) {
558 $content .= html_writer::start_tag('div', ['class' => 'summary']);
559 $content .= $chelper->get_course_formatted_summary($course,
560 array('overflowdiv' => true, 'noclean' => true, 'para' => false));
561 $content .= html_writer::end_tag('div');
563 return $content;
567 * Returns HTML to display course contacts.
569 * @param core_course_list_element $course
570 * @return string
572 protected function course_contacts(core_course_list_element $course) {
573 $content = '';
574 if ($course->has_course_contacts()) {
575 $content .= html_writer::start_tag('ul', ['class' => 'teachers']);
576 foreach ($course->get_course_contacts() as $coursecontact) {
577 $rolenames = array_map(function ($role) {
578 return $role->displayname;
579 }, $coursecontact['roles']);
580 $name = html_writer::tag('span', implode(", ", $rolenames).': ', ['class' => 'font-weight-bold']);
581 $name .= html_writer::link(
582 \core_user::get_profile_url($coursecontact['user'], context_system::instance()),
583 $coursecontact['username']
585 $content .= html_writer::tag('li', $name);
587 $content .= html_writer::end_tag('ul');
589 return $content;
593 * Returns HTML to display course overview files.
595 * @param core_course_list_element $course
596 * @return string
598 protected function course_overview_files(core_course_list_element $course): string {
599 global $CFG;
601 $contentimages = $contentfiles = '';
602 foreach ($course->get_course_overviewfiles() as $file) {
603 $isimage = $file->is_valid_image();
604 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php",
605 '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
606 $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
607 if ($isimage) {
608 $contentimages .= html_writer::tag('div',
609 html_writer::empty_tag('img', ['src' => $url, 'alt' => '']),
610 ['class' => 'courseimage']);
611 } else {
612 $image = $this->output->pix_icon(file_file_icon($file), $file->get_filename(), 'moodle');
613 $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']).
614 html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
615 $contentfiles .= html_writer::tag('span',
616 html_writer::link($url, $filename),
617 ['class' => 'coursefile fp-filename-icon text-break']);
620 return $contentimages . $contentfiles;
624 * Returns HTML to display course category name.
626 * @param coursecat_helper $chelper
627 * @param core_course_list_element $course
628 * @return string
630 protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string {
631 $content = '';
632 // Display course category if necessary (for example in search results).
633 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
634 if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) {
635 $content .= html_writer::start_tag('div', ['class' => 'coursecat']);
636 $content .= html_writer::tag('span', get_string('category').': ', ['class' => 'font-weight-bold']);
637 $content .= html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]),
638 $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']);
639 $content .= html_writer::end_tag('div');
642 return $content;
646 * Returns HTML to display course custom fields.
648 * @param core_course_list_element $course
649 * @return string
651 protected function course_custom_fields(core_course_list_element $course): string {
652 $content = '';
653 if ($course->has_custom_fields()) {
654 $handler = core_course\customfield\course_handler::create();
655 $customfields = $handler->display_custom_fields_data($course->get_custom_fields());
656 $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']);
658 return $content;
662 * Returns HTML to display course content (summary, course contacts and optionally category name)
664 * This method is called from coursecat_coursebox() and may be re-used in AJAX
666 * @param coursecat_helper $chelper various display options
667 * @param stdClass|core_course_list_element $course
668 * @return string
670 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
671 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
672 return '';
674 if ($course instanceof stdClass) {
675 $course = new core_course_list_element($course);
677 $content = \html_writer::start_tag('div', ['class' => 'd-flex']);
678 $content .= $this->course_overview_files($course);
679 $content .= \html_writer::start_tag('div', ['class' => 'flex-grow-1']);
680 $content .= $this->course_summary($chelper, $course);
681 $content .= $this->course_contacts($course);
682 $content .= $this->course_category_name($chelper, $course);
683 $content .= $this->course_custom_fields($course);
684 $content .= \html_writer::end_tag('div');
685 $content .= \html_writer::end_tag('div');
686 return $content;
690 * Renders the list of courses
692 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
693 * method from outside of the class
695 * If list of courses is specified in $courses; the argument $chelper is only used
696 * to retrieve display options and attributes, only methods get_show_courses(),
697 * get_courses_display_option() and get_and_erase_attributes() are called.
699 * @param coursecat_helper $chelper various display options
700 * @param array $courses the list of courses to display
701 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
702 * defaulted to count($courses)
703 * @return string
705 protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) {
706 global $CFG;
707 if ($totalcount === null) {
708 $totalcount = count($courses);
710 if (!$totalcount) {
711 // Courses count is cached during courses retrieval.
712 return '';
715 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) {
716 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
717 if ($totalcount <= $CFG->courseswithsummarieslimit) {
718 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
719 } else {
720 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
724 // prepare content of paging bar if it is needed
725 $paginationurl = $chelper->get_courses_display_option('paginationurl');
726 $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
727 if ($totalcount > count($courses)) {
728 // there are more results that can fit on one page
729 if ($paginationurl) {
730 // the option paginationurl was specified, display pagingbar
731 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage);
732 $page = $chelper->get_courses_display_option('offset') / $perpage;
733 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
734 $paginationurl->out(false, array('perpage' => $perpage)));
735 if ($paginationallowall) {
736 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
737 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
739 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
740 // the option for 'View more' link was specified, display more link
741 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
742 $morelink = html_writer::tag(
743 'div',
744 html_writer::link($viewmoreurl, $viewmoretext, ['class' => 'btn btn-secondary']),
745 ['class' => 'paging paging-morelink']
748 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
749 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
750 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
751 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
754 // display list of courses
755 $attributes = $chelper->get_and_erase_attributes('courses');
756 $content = html_writer::start_tag('div', $attributes);
758 if (!empty($pagingbar)) {
759 $content .= $pagingbar;
762 $coursecount = 0;
763 foreach ($courses as $course) {
764 $coursecount ++;
765 $classes = ($coursecount%2) ? 'odd' : 'even';
766 if ($coursecount == 1) {
767 $classes .= ' first';
769 if ($coursecount >= count($courses)) {
770 $classes .= ' last';
772 $content .= $this->coursecat_coursebox($chelper, $course, $classes);
775 if (!empty($pagingbar)) {
776 $content .= $pagingbar;
778 if (!empty($morelink)) {
779 $content .= $morelink;
782 $content .= html_writer::end_tag('div'); // .courses
783 return $content;
787 * Renders the list of subcategories in a category
789 * @param coursecat_helper $chelper various display options
790 * @param core_course_category $coursecat
791 * @param int $depth depth of the category in the current tree
792 * @return string
794 protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) {
795 global $CFG;
796 $subcategories = array();
797 if (!$chelper->get_categories_display_option('nodisplay')) {
798 $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
800 $totalcount = $coursecat->get_children_count();
801 if (!$totalcount) {
802 // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children()
803 // to avoid extra DB requests.
804 // Categories count is cached during children categories retrieval.
805 return '';
808 // prepare content of paging bar or more link if it is needed
809 $paginationurl = $chelper->get_categories_display_option('paginationurl');
810 $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
811 if ($totalcount > count($subcategories)) {
812 if ($paginationurl) {
813 // the option 'paginationurl was specified, display pagingbar
814 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage);
815 $page = $chelper->get_categories_display_option('offset') / $perpage;
816 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
817 $paginationurl->out(false, array('perpage' => $perpage)));
818 if ($paginationallowall) {
819 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
820 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
822 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
823 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
824 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
825 $viewmoreurl->param('categoryid', $coursecat->id);
827 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
828 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
829 array('class' => 'paging paging-morelink'));
831 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
832 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
833 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
834 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
837 // display list of subcategories
838 $content = html_writer::start_tag('div', array('class' => 'subcategories'));
840 if (!empty($pagingbar)) {
841 $content .= $pagingbar;
844 foreach ($subcategories as $subcategory) {
845 $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1);
848 if (!empty($pagingbar)) {
849 $content .= $pagingbar;
851 if (!empty($morelink)) {
852 $content .= $morelink;
855 $content .= html_writer::end_tag('div');
856 return $content;
860 * Make sure that javascript file for AJAX expanding of courses and categories content is included
862 protected function coursecat_include_js() {
863 if (!$this->page->requires->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) {
864 return;
867 // We must only load this module once.
868 $this->page->requires->yui_module('moodle-course-categoryexpander',
869 'Y.Moodle.course.categoryexpander.init');
873 * Returns HTML to display the subcategories and courses in the given category
875 * This method is re-used by AJAX to expand content of not loaded category
877 * @param coursecat_helper $chelper various display options
878 * @param core_course_category $coursecat
879 * @param int $depth depth of the category in the current tree
880 * @return string
882 protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) {
883 $content = '';
884 // Subcategories
885 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
887 // AUTO show courses: Courses will be shown expanded if this is not nested category,
888 // and number of courses no bigger than $CFG->courseswithsummarieslimit.
889 $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO;
890 if ($showcoursesauto && $depth) {
891 // this is definitely collapsed mode
892 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
895 // Courses
896 if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) {
897 $courses = array();
898 if (!$chelper->get_courses_display_option('nodisplay')) {
899 $courses = $coursecat->get_courses($chelper->get_courses_display_options());
901 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
902 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
903 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
904 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id)));
907 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
910 if ($showcoursesauto) {
911 // restore the show_courses back to AUTO
912 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO);
915 return $content;
919 * Returns HTML to display a course category as a part of a tree
921 * This is an internal function, to display a particular category and all its contents
922 * use {@link core_course_renderer::course_category()}
924 * @param coursecat_helper $chelper various display options
925 * @param core_course_category $coursecat
926 * @param int $depth depth of this category in the current tree
927 * @return string
929 protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) {
930 // open category tag
931 $classes = array('category');
932 if (empty($coursecat->visible)) {
933 $classes[] = 'dimmed_category';
935 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
936 // do not load content
937 $categorycontent = '';
938 $classes[] = 'notloaded';
939 if ($coursecat->get_children_count() ||
940 ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) {
941 $classes[] = 'with_children';
942 $classes[] = 'collapsed';
944 } else {
945 // load category content
946 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
947 $classes[] = 'loaded';
948 if (!empty($categorycontent)) {
949 $classes[] = 'with_children';
950 // Category content loaded with children.
951 $this->categoryexpandedonload = true;
955 // Make sure JS file to expand category content is included.
956 $this->coursecat_include_js();
958 $content = html_writer::start_tag('div', array(
959 'class' => join(' ', $classes),
960 'data-categoryid' => $coursecat->id,
961 'data-depth' => $depth,
962 'data-showcourses' => $chelper->get_show_courses(),
963 'data-type' => self::COURSECAT_TYPE_CATEGORY,
966 // category name
967 $categoryname = $coursecat->get_formatted_name();
968 $categoryname = html_writer::link(new moodle_url('/course/index.php',
969 array('categoryid' => $coursecat->id)),
970 $categoryname);
971 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT
972 && ($coursescount = $coursecat->get_courses_count())) {
973 $categoryname .= html_writer::tag('span', ' ('. $coursescount.')',
974 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
976 $content .= html_writer::start_tag('div', array('class' => 'info'));
978 $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn'));
979 $content .= html_writer::end_tag('div'); // .info
981 // add category content to the output
982 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
984 $content .= html_writer::end_tag('div'); // .category
986 // Return the course category tree HTML
987 return $content;
991 * Returns HTML to display a tree of subcategories and courses in the given category
993 * @param coursecat_helper $chelper various display options
994 * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree)
995 * @return string
997 protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
998 // Reset the category expanded flag for this course category tree first.
999 $this->categoryexpandedonload = false;
1000 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1001 if (empty($categorycontent)) {
1002 return '';
1005 // Start content generation
1006 $content = '';
1007 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1008 $content .= html_writer::start_tag('div', $attributes);
1010 if ($coursecat->get_children_count()) {
1011 $classes = array(
1012 'collapseexpand', 'aabtn'
1015 // Check if the category content contains subcategories with children's content loaded.
1016 if ($this->categoryexpandedonload) {
1017 $classes[] = 'collapse-all';
1018 $linkname = get_string('collapseall');
1019 } else {
1020 $linkname = get_string('expandall');
1023 // Only show the collapse/expand if there are children to expand.
1024 $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions'));
1025 $content .= html_writer::link('#', $linkname, array('class' => implode(' ', $classes)));
1026 $content .= html_writer::end_tag('div');
1027 $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
1030 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1032 $content .= html_writer::end_tag('div'); // .course_category_tree
1034 return $content;
1038 * Renders HTML to display particular course category - list of it's subcategories and courses
1040 * Invoked from /course/index.php
1042 * @param int|stdClass|core_course_category $category
1044 public function course_category($category) {
1045 global $CFG;
1046 $usertop = core_course_category::user_top();
1047 if (empty($category)) {
1048 $coursecat = $usertop;
1049 } else if (is_object($category) && $category instanceof core_course_category) {
1050 $coursecat = $category;
1051 } else {
1052 $coursecat = core_course_category::get(is_object($category) ? $category->id : $category);
1054 $site = get_site();
1055 $actionbar = new \core_course\output\category_action_bar($this->page, $coursecat);
1056 $output = $this->render_from_template('core_course/category_actionbar', $actionbar->export_for_template($this));
1058 if (core_course_category::is_simple_site()) {
1059 // There is only one category in the system, do not display link to it.
1060 $strfulllistofcourses = get_string('fulllistofcourses');
1061 $this->page->set_title($strfulllistofcourses);
1062 } else if (!$coursecat->id || !$coursecat->is_uservisible()) {
1063 $strcategories = get_string('categories');
1064 $this->page->set_title($strcategories);
1065 } else {
1066 $strfulllistofcourses = get_string('fulllistofcourses');
1067 $this->page->set_title($strfulllistofcourses);
1070 // Print current category description
1071 $chelper = new coursecat_helper();
1072 if ($description = $chelper->get_category_formatted_description($coursecat)) {
1073 $output .= $this->box($description, array('class' => 'generalbox info'));
1076 // Prepare parameters for courses and categories lists in the tree
1077 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
1078 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));
1080 $coursedisplayoptions = array();
1081 $catdisplayoptions = array();
1082 $browse = optional_param('browse', null, PARAM_ALPHA);
1083 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
1084 $page = optional_param('page', 0, PARAM_INT);
1085 $baseurl = new moodle_url('/course/index.php');
1086 if ($coursecat->id) {
1087 $baseurl->param('categoryid', $coursecat->id);
1089 if ($perpage != $CFG->coursesperpage) {
1090 $baseurl->param('perpage', $perpage);
1092 $coursedisplayoptions['limit'] = $perpage;
1093 $catdisplayoptions['limit'] = $perpage;
1094 if ($browse === 'courses' || !$coursecat->get_children_count()) {
1095 $coursedisplayoptions['offset'] = $page * $perpage;
1096 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1097 $catdisplayoptions['nodisplay'] = true;
1098 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1099 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
1100 } else if ($browse === 'categories' || !$coursecat->get_courses_count()) {
1101 $coursedisplayoptions['nodisplay'] = true;
1102 $catdisplayoptions['offset'] = $page * $perpage;
1103 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1104 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1105 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1106 } else {
1107 // we have a category that has both subcategories and courses, display pagination separately
1108 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1109 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1111 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1113 // Display course category tree.
1114 $output .= $this->coursecat_tree($chelper, $coursecat);
1116 return $output;
1120 * Serves requests to /course/category.ajax.php
1122 * In this renderer implementation it may expand the category content or
1123 * course content.
1125 * @return string
1126 * @throws coding_exception
1128 public function coursecat_ajax() {
1129 global $DB, $CFG;
1131 $type = required_param('type', PARAM_INT);
1133 if ($type === self::COURSECAT_TYPE_CATEGORY) {
1134 // This is a request for a category list of some kind.
1135 $categoryid = required_param('categoryid', PARAM_INT);
1136 $showcourses = required_param('showcourses', PARAM_INT);
1137 $depth = required_param('depth', PARAM_INT);
1139 $category = core_course_category::get($categoryid);
1141 $chelper = new coursecat_helper();
1142 $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
1143 $coursedisplayoptions = array(
1144 'limit' => $CFG->coursesperpage,
1145 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
1147 $catdisplayoptions = array(
1148 'limit' => $CFG->coursesperpage,
1149 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
1151 $chelper->set_show_courses($showcourses)->
1152 set_courses_display_options($coursedisplayoptions)->
1153 set_categories_display_options($catdisplayoptions);
1155 return $this->coursecat_category_content($chelper, $category, $depth);
1156 } else if ($type === self::COURSECAT_TYPE_COURSE) {
1157 // This is a request for the course information.
1158 $courseid = required_param('courseid', PARAM_INT);
1160 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
1162 $chelper = new coursecat_helper();
1163 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1164 return $this->coursecat_coursebox_content($chelper, $course);
1165 } else {
1166 throw new coding_exception('Invalid request type');
1171 * Renders html to display search result page
1173 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1174 * @return string
1176 public function search_courses($searchcriteria) {
1177 global $CFG;
1178 $content = '';
1180 $search = '';
1181 if (!empty($searchcriteria['search'])) {
1182 $search = $searchcriteria['search'];
1184 $content .= $this->course_search_form($search);
1186 if (!empty($searchcriteria)) {
1187 // print search results
1189 $displayoptions = array('sort' => array('displayname' => 1));
1190 // take the current page and number of results per page from query
1191 $perpage = optional_param('perpage', 0, PARAM_RAW);
1192 if ($perpage !== 'all') {
1193 $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
1194 $page = optional_param('page', 0, PARAM_INT);
1195 $displayoptions['offset'] = $displayoptions['limit'] * $page;
1197 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1198 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1199 $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1201 $class = 'course-search-result';
1202 foreach ($searchcriteria as $key => $value) {
1203 if (!empty($value)) {
1204 $class .= ' course-search-result-'. $key;
1207 $chelper = new coursecat_helper();
1208 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1209 set_courses_display_options($displayoptions)->
1210 set_search_criteria($searchcriteria)->
1211 set_attributes(array('class' => $class));
1213 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1214 $totalcount = core_course_category::search_courses_count($searchcriteria);
1215 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1217 if (!$totalcount) {
1218 if (!empty($searchcriteria['search'])) {
1219 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1220 } else {
1221 $content .= $this->heading(get_string('novalidcourses'));
1223 } else {
1224 $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1225 $content .= $courseslist;
1228 return $content;
1232 * Renders html to print list of courses tagged with particular tag
1234 * @param int $tagid id of the tag
1235 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1236 * are displayed on the page and the per-page limit may be bigger
1237 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1238 * to display items in the same context first
1239 * @param int $ctx context id where to search for records
1240 * @param bool $rec search in subcontexts as well
1241 * @param array $displayoptions
1242 * @return string empty string if no courses are marked with this tag or rendered list of courses
1244 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) {
1245 global $CFG;
1246 if (empty($displayoptions)) {
1247 $displayoptions = array();
1249 $showcategories = !core_course_category::is_simple_site();
1250 $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0);
1251 $chelper = new coursecat_helper();
1252 $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
1253 $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT :
1254 self::COURSECAT_SHOW_COURSES_EXPANDED)->
1255 set_search_criteria($searchcriteria)->
1256 set_courses_display_options($displayoptions)->
1257 set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1258 // (we set the same css class as in search results by tagid)
1259 if ($totalcount = core_course_category::search_courses_count($searchcriteria)) {
1260 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1261 if ($exclusivemode) {
1262 return $this->coursecat_courses($chelper, $courses, $totalcount);
1263 } else {
1264 $tagfeed = new core_tag\output\tagfeed();
1265 $img = $this->output->pix_icon('i/course', '');
1266 foreach ($courses as $course) {
1267 $url = course_get_url($course);
1268 $imgwithlink = html_writer::link($url, $img);
1269 $coursename = html_writer::link($url, $course->get_formatted_name());
1270 $details = '';
1271 if ($showcategories && ($cat = core_course_category::get($course->category, IGNORE_MISSING))) {
1272 $details = get_string('category').': '.
1273 html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)),
1274 $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
1276 $tagfeed->add($imgwithlink, $coursename, $details);
1278 return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output));
1281 return '';
1285 * Returns HTML to display one remote course
1287 * @param stdClass $course remote course information, contains properties:
1288 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1289 * @return string
1291 protected function frontpage_remote_course(stdClass $course) {
1292 $url = new moodle_url('/auth/mnet/jump.php', array(
1293 'hostid' => $course->hostid,
1294 'wantsurl' => '/course/view.php?id='. $course->remoteid
1297 $output = '';
1298 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1299 $output .= html_writer::start_tag('div', array('class' => 'info'));
1300 $output .= html_writer::start_tag('h3', array('class' => 'coursename'));
1301 $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
1302 $output .= html_writer::end_tag('h3'); // .name
1303 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1304 $output .= html_writer::end_tag('div'); // .info
1305 $output .= html_writer::start_tag('div', array('class' => 'content'));
1306 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1307 $options = new stdClass();
1308 $options->noclean = true;
1309 $options->para = false;
1310 $options->overflowdiv = true;
1311 $output .= format_text($course->summary, $course->summaryformat, $options);
1312 $output .= html_writer::end_tag('div'); // .summary
1313 $addinfo = format_string($course->hostname) . ' : '
1314 . format_string($course->cat_name) . ' : '
1315 . format_string($course->shortname);
1316 $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1317 $output .= html_writer::end_tag('div'); // .content
1318 $output .= html_writer::end_tag('div'); // .coursebox
1319 return $output;
1323 * Returns HTML to display one remote host
1325 * @param array $host host information, contains properties: name, url, count
1326 * @return string
1328 protected function frontpage_remote_host($host) {
1329 $output = '';
1330 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1331 $output .= html_writer::start_tag('div', array('class' => 'info'));
1332 $output .= html_writer::start_tag('h3', array('class' => 'name'));
1333 $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1334 $output .= html_writer::end_tag('h3'); // .name
1335 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1336 $output .= html_writer::end_tag('div'); // .info
1337 $output .= html_writer::start_tag('div', array('class' => 'content'));
1338 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1339 $output .= $host['count'] . ' ' . get_string('courses');
1340 $output .= html_writer::end_tag('div'); // .content
1341 $output .= html_writer::end_tag('div'); // .coursebox
1342 return $output;
1346 * Returns HTML to print list of courses user is enrolled to for the frontpage
1348 * Also lists remote courses or remote hosts if MNET authorisation is used
1350 * @return string
1352 public function frontpage_my_courses() {
1353 global $USER, $CFG, $DB;
1355 if (!isloggedin() or isguestuser()) {
1356 return '';
1359 $output = '';
1360 $courses = enrol_get_my_courses('summary, summaryformat');
1361 $rhosts = array();
1362 $rcourses = array();
1363 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
1364 $rcourses = get_my_remotecourses($USER->id);
1365 $rhosts = get_my_remotehosts();
1368 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1370 $chelper = new coursecat_helper();
1371 $totalcount = count($courses);
1372 if (count($courses) > $CFG->frontpagecourselimit) {
1373 // There are more enrolled courses than we can display, display link to 'My courses'.
1374 $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true);
1375 $chelper->set_courses_display_options(array(
1376 'viewmoreurl' => new moodle_url('/my/courses.php'),
1377 'viewmoretext' => new lang_string('mycourses')
1379 } else if (core_course_category::top()->is_uservisible()) {
1380 // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system.
1381 $chelper->set_courses_display_options(array(
1382 'viewmoreurl' => new moodle_url('/course/index.php'),
1383 'viewmoretext' => new lang_string('fulllistofcourses')
1385 $totalcount = $DB->count_records('course') - 1;
1387 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1388 set_attributes(array('class' => 'frontpage-course-list-enrolled'));
1389 $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
1391 // MNET
1392 if (!empty($rcourses)) {
1393 // at the IDP, we know of all the remote courses
1394 $output .= html_writer::start_tag('div', array('class' => 'courses'));
1395 foreach ($rcourses as $course) {
1396 $output .= $this->frontpage_remote_course($course);
1398 $output .= html_writer::end_tag('div'); // .courses
1399 } elseif (!empty($rhosts)) {
1400 // non-IDP, we know of all the remote servers, but not courses
1401 $output .= html_writer::start_tag('div', array('class' => 'courses'));
1402 foreach ($rhosts as $host) {
1403 $output .= $this->frontpage_remote_host($host);
1405 $output .= html_writer::end_tag('div'); // .courses
1408 return $output;
1412 * Returns HTML to print list of available courses for the frontpage
1414 * @return string
1416 public function frontpage_available_courses() {
1417 global $CFG;
1419 $chelper = new coursecat_helper();
1420 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1421 set_courses_display_options(array(
1422 'recursive' => true,
1423 'limit' => $CFG->frontpagecourselimit,
1424 'viewmoreurl' => new moodle_url('/course/index.php'),
1425 'viewmoretext' => new lang_string('fulllistofcourses')));
1427 $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
1428 $courses = core_course_category::top()->get_courses($chelper->get_courses_display_options());
1429 $totalcount = core_course_category::top()->get_courses_count($chelper->get_courses_display_options());
1430 if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) {
1431 // Print link to create a new course, for the 1st available category.
1432 return $this->add_new_course_button();
1434 return $this->coursecat_courses($chelper, $courses, $totalcount);
1438 * Returns HTML to the "add new course" button for the page
1440 * @return string
1442 public function add_new_course_button() {
1443 global $CFG;
1444 // Print link to create a new course, for the 1st available category.
1445 $output = $this->container_start('buttons');
1446 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
1447 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
1448 $output .= $this->container_end('buttons');
1449 return $output;
1453 * Returns HTML to print tree with course categories and courses for the frontpage
1455 * @return string
1457 public function frontpage_combo_list() {
1458 global $CFG;
1459 // TODO MDL-10965 improve.
1460 $tree = core_course_category::top();
1461 if (!$tree->get_children_count()) {
1462 return '';
1464 $chelper = new coursecat_helper();
1465 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1466 set_categories_display_options(array(
1467 'limit' => $CFG->coursesperpage,
1468 'viewmoreurl' => new moodle_url('/course/index.php',
1469 array('browse' => 'categories', 'page' => 1))
1470 ))->
1471 set_courses_display_options(array(
1472 'limit' => $CFG->coursesperpage,
1473 'viewmoreurl' => new moodle_url('/course/index.php',
1474 array('browse' => 'courses', 'page' => 1))
1475 ))->
1476 set_attributes(array('class' => 'frontpage-category-combo'));
1477 return $this->coursecat_tree($chelper, $tree);
1481 * Returns HTML to print tree of course categories (with number of courses) for the frontpage
1483 * @return string
1485 public function frontpage_categories_list() {
1486 global $CFG;
1487 // TODO MDL-10965 improve.
1488 $tree = core_course_category::top();
1489 if (!$tree->get_children_count()) {
1490 return '';
1492 $chelper = new coursecat_helper();
1493 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1494 set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
1495 set_categories_display_options(array(
1496 'limit' => $CFG->coursesperpage,
1497 'viewmoreurl' => new moodle_url('/course/index.php',
1498 array('browse' => 'categories', 'page' => 1))
1499 ))->
1500 set_attributes(array('class' => 'frontpage-category-names'));
1501 return $this->coursecat_tree($chelper, $tree);
1505 * Renders the activity information.
1507 * Defer to template.
1509 * @deprecated since Moodle 4.3 MDL-78744
1510 * @todo MDL-78926 This method will be deleted in Moodle 4.7
1511 * @param \core_course\output\activity_information $page
1512 * @return string html for the page
1514 public function render_activity_information(\core_course\output\activity_information $page) {
1515 debugging('render_activity_information method is deprecated.', DEBUG_DEVELOPER);
1516 $data = $page->export_for_template($this->output);
1517 return $this->output->render_from_template('core_course/activity_info', $data);
1521 * Renders the activity navigation.
1523 * Defer to template.
1525 * @param \core_course\output\activity_navigation $page
1526 * @return string html for the page
1528 public function render_activity_navigation(\core_course\output\activity_navigation $page) {
1529 $data = $page->export_for_template($this->output);
1530 return $this->output->render_from_template('core_course/activity_navigation', $data);
1534 * Display waiting information about backup size during uploading backup process
1535 * @param object $backupfile the backup stored_file
1536 * @return $html string
1538 public function sendingbackupinfo($backupfile) {
1539 $sizeinfo = new stdClass();
1540 $sizeinfo->total = number_format($backupfile->get_filesize() / 1000000, 2);
1541 $html = html_writer::tag('div', get_string('sendingsize', 'hub', $sizeinfo),
1542 array('class' => 'courseuploadtextinfo'));
1543 return $html;
1547 * Hub information (logo - name - description - link)
1548 * @param object $hubinfo
1549 * @return string html code
1551 public function hubinfo($hubinfo) {
1552 $screenshothtml = html_writer::empty_tag('img',
1553 array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name']));
1554 $hubdescription = html_writer::tag('div', $screenshothtml,
1555 array('class' => 'hubscreenshot'));
1557 $hubdescription .= html_writer::tag('a', $hubinfo['name'],
1558 array('class' => 'hublink', 'href' => $hubinfo['url'],
1559 'onclick' => 'this.target="_blank"'));
1561 $hubdescription .= html_writer::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN),
1562 array('class' => 'hubdescription'));
1563 $hubdescription = html_writer::tag('div', $hubdescription, array('class' => 'hubinfo clearfix'));
1565 return $hubdescription;
1569 * Output frontpage summary text and frontpage modules (stored as section 1 in site course)
1571 * This may be disabled in settings
1573 * @return string
1575 public function frontpage_section1() {
1576 global $SITE, $USER;
1578 $output = '';
1579 $editing = $this->page->user_is_editing();
1581 if ($editing) {
1582 // Make sure section with number 1 exists.
1583 course_create_sections_if_missing($SITE, 1);
1586 $modinfo = get_fast_modinfo($SITE);
1587 $section = $modinfo->get_section_info(1);
1590 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
1592 $format = course_get_format($SITE);
1593 $frontpageclass = $format->get_output_classname('content\\frontpagesection');
1594 $frontpagesection = new $frontpageclass($format, $section);
1596 // The course outputs works with format renderers, not with course renderers.
1597 $renderer = $format->get_renderer($this->page);
1598 $output .= $renderer->render($frontpagesection);
1601 return $output;
1605 * Output news for the frontpage (extract from site-wide news forum)
1607 * @param stdClass $forum record from db table 'forum' that represents the site news forum
1608 * @return string
1610 protected function frontpage_news($forum) {
1611 global $CFG, $SITE, $SESSION, $USER;
1612 require_once($CFG->dirroot .'/mod/forum/lib.php');
1614 $output = '';
1616 if (isloggedin()) {
1617 $SESSION->fromdiscussion = $CFG->wwwroot;
1618 $subtext = '';
1619 if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) {
1620 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
1621 $subtext = get_string('unsubscribe', 'forum');
1623 } else {
1624 $subtext = get_string('subscribe', 'forum');
1626 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey()));
1627 $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
1630 $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
1631 $context = context_module::instance($coursemodule->id);
1633 $entityfactory = mod_forum\local\container::get_entity_factory();
1634 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE);
1636 $rendererfactory = mod_forum\local\container::get_renderer_factory();
1637 $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity);
1638 $cm = \cm_info::create($coursemodule);
1639 return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems);
1643 * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
1645 * @param string $skipdivid
1646 * @param string $contentsdivid
1647 * @param string $header Header of the part
1648 * @param string $contents Contents of the part
1649 * @return string
1651 protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
1652 if (strval($contents) === '') {
1653 return '';
1655 $output = html_writer::link('#' . $skipdivid,
1656 get_string('skipa', 'access', core_text::strtolower(strip_tags($header))),
1657 array('class' => 'skip-block skip aabtn'));
1659 // Wrap frontpage part in div container.
1660 $output .= html_writer::start_tag('div', array('id' => $contentsdivid));
1661 $output .= $this->heading($header);
1663 $output .= $contents;
1665 // End frontpage part div container.
1666 $output .= html_writer::end_tag('div');
1668 $output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
1669 return $output;
1673 * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
1675 * @return string
1677 public function frontpage() {
1678 global $CFG, $SITE;
1680 $output = '';
1682 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
1683 $frontpagelayout = $CFG->frontpageloggedin;
1684 } else {
1685 $frontpagelayout = $CFG->frontpage;
1688 foreach (explode(',', $frontpagelayout) as $v) {
1689 switch ($v) {
1690 // Display the main part of the front page.
1691 case FRONTPAGENEWS:
1692 if ($SITE->newsitems) {
1693 // Print forums only when needed.
1694 require_once($CFG->dirroot .'/mod/forum/lib.php');
1695 if (($newsforum = forum_get_course_forum($SITE->id, 'news')) &&
1696 ($forumcontents = $this->frontpage_news($newsforum))) {
1697 $newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id];
1698 $output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
1699 $newsforumcm->get_formatted_name(), $forumcontents);
1702 break;
1704 case FRONTPAGEENROLLEDCOURSELIST:
1705 $mycourseshtml = $this->frontpage_my_courses();
1706 if (!empty($mycourseshtml)) {
1707 $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
1708 get_string('mycourses'), $mycourseshtml);
1710 break;
1712 case FRONTPAGEALLCOURSELIST:
1713 $availablecourseshtml = $this->frontpage_available_courses();
1714 $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
1715 get_string('availablecourses'), $availablecourseshtml);
1716 break;
1718 case FRONTPAGECATEGORYNAMES:
1719 $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
1720 get_string('categories'), $this->frontpage_categories_list());
1721 break;
1723 case FRONTPAGECATEGORYCOMBO:
1724 $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
1725 get_string('courses'), $this->frontpage_combo_list());
1726 break;
1728 case FRONTPAGECOURSESEARCH:
1729 $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
1730 break;
1733 $output .= '<br />';
1736 return $output;
1741 * Class storing display options and functions to help display course category and/or courses lists
1743 * This is a wrapper for core_course_category objects that also stores display options
1744 * and functions to retrieve sorted and paginated lists of categories/courses.
1746 * If theme overrides methods in core_course_renderers that access this class
1747 * it may as well not use this class at all or extend it.
1749 * @package core
1750 * @copyright 2013 Marina Glancy
1751 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1753 class coursecat_helper {
1754 /** @var string [none, collapsed, expanded] how (if) display courses list */
1755 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
1756 /** @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) */
1757 protected $subcatdepth = 1;
1758 /** @var array options to display courses list */
1759 protected $coursesdisplayoptions = array();
1760 /** @var array options to display subcategories list */
1761 protected $categoriesdisplayoptions = array();
1762 /** @var array additional HTML attributes */
1763 protected $attributes = array();
1764 /** @var array search criteria if the list is a search result */
1765 protected $searchcriteria = null;
1768 * Sets how (if) to show the courses - none, collapsed, expanded, etc.
1770 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
1771 * @return coursecat_helper
1773 public function set_show_courses($showcourses) {
1774 $this->showcourses = $showcourses;
1775 // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses()
1776 // and core_course_category::search_courses().
1777 $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
1778 $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
1779 $this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED;
1780 return $this;
1784 * Returns how (if) to show the courses - none, collapsed, expanded, etc.
1786 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
1788 public function get_show_courses() {
1789 return $this->showcourses;
1793 * Sets the maximum depth to expand subcategories in the tree
1795 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
1797 * @param int $subcatdepth
1798 * @return coursecat_helper
1800 public function set_subcat_depth($subcatdepth) {
1801 $this->subcatdepth = $subcatdepth;
1802 return $this;
1806 * Returns the maximum depth to expand subcategories in the tree
1808 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
1810 * @return int
1812 public function get_subcat_depth() {
1813 return $this->subcatdepth;
1817 * Sets options to display list of courses
1819 * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses()
1821 * Options that core_course_category::get_courses() accept:
1822 * - recursive - return courses from subcategories as well. Use with care,
1823 * this may be a huge list!
1824 * - summary - preloads fields 'summary' and 'summaryformat'
1825 * - coursecontacts - preloads course contacts
1826 * - customfields - preloads custom fields data
1827 * - isenrolled - preloads indication whether this user is enrolled in the course
1828 * - sort - list of fields to sort. Example
1829 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
1830 * will sort by idnumber asc, shortname asc and id desc.
1831 * Default: array('sortorder' => 1)
1832 * Only cached fields may be used for sorting!
1833 * - offset
1834 * - limit - maximum number of children to return, 0 or null for no limit
1836 * Options summary and coursecontacts are filled automatically in the set_show_courses()
1838 * Also renderer can set here any additional options it wants to pass between renderer functions.
1840 * @param array $options
1841 * @return coursecat_helper
1843 public function set_courses_display_options($options) {
1844 $this->coursesdisplayoptions = $options;
1845 $this->set_show_courses($this->showcourses); // this will calculate special display options
1846 return $this;
1850 * Sets one option to display list of courses
1852 * @see coursecat_helper::set_courses_display_options()
1854 * @param string $key
1855 * @param mixed $value
1856 * @return coursecat_helper
1858 public function set_courses_display_option($key, $value) {
1859 $this->coursesdisplayoptions[$key] = $value;
1860 return $this;
1864 * Return the specified option to display list of courses
1866 * @param string $optionname option name
1867 * @param mixed $defaultvalue default value for option if it is not specified
1868 * @return mixed
1870 public function get_courses_display_option($optionname, $defaultvalue = null) {
1871 if (array_key_exists($optionname, $this->coursesdisplayoptions)) {
1872 return $this->coursesdisplayoptions[$optionname];
1873 } else {
1874 return $defaultvalue;
1879 * Returns all options to display the courses
1881 * This array is usually passed to {@link core_course_category::get_courses()} or
1882 * {@link core_course_category::search_courses()}
1884 * @return array
1886 public function get_courses_display_options() {
1887 return $this->coursesdisplayoptions;
1891 * Sets options to display list of subcategories
1893 * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children().
1894 * Any other options may be used by renderer functions
1896 * @param array $options
1897 * @return coursecat_helper
1899 public function set_categories_display_options($options) {
1900 $this->categoriesdisplayoptions = $options;
1901 return $this;
1905 * Return the specified option to display list of subcategories
1907 * @param string $optionname option name
1908 * @param mixed $defaultvalue default value for option if it is not specified
1909 * @return mixed
1911 public function get_categories_display_option($optionname, $defaultvalue = null) {
1912 if (array_key_exists($optionname, $this->categoriesdisplayoptions)) {
1913 return $this->categoriesdisplayoptions[$optionname];
1914 } else {
1915 return $defaultvalue;
1920 * Returns all options to display list of subcategories
1922 * This array is usually passed to {@link core_course_category::get_children()}
1924 * @return array
1926 public function get_categories_display_options() {
1927 return $this->categoriesdisplayoptions;
1931 * Sets additional general options to pass between renderer functions, usually HTML attributes
1933 * @param array $attributes
1934 * @return coursecat_helper
1936 public function set_attributes($attributes) {
1937 $this->attributes = $attributes;
1938 return $this;
1942 * Return all attributes and erases them so they are not applied again
1944 * @param string $classname adds additional class name to the beginning of $attributes['class']
1945 * @return array
1947 public function get_and_erase_attributes($classname) {
1948 $attributes = $this->attributes;
1949 $this->attributes = array();
1950 if (empty($attributes['class'])) {
1951 $attributes['class'] = '';
1953 $attributes['class'] = $classname . ' '. $attributes['class'];
1954 return $attributes;
1958 * Sets the search criteria if the course is a search result
1960 * Search string will be used to highlight terms in course name and description
1962 * @param array $searchcriteria
1963 * @return coursecat_helper
1965 public function set_search_criteria($searchcriteria) {
1966 $this->searchcriteria = $searchcriteria;
1967 return $this;
1971 * Returns formatted and filtered description of the given category
1973 * @param core_course_category $coursecat category
1974 * @param stdClass|array $options format options, by default [noclean,overflowdiv],
1975 * if context is not specified it will be added automatically
1976 * @return string|null
1978 public function get_category_formatted_description($coursecat, $options = null) {
1979 if ($coursecat->id && $coursecat->is_uservisible() && !empty($coursecat->description)) {
1980 if (!isset($coursecat->descriptionformat)) {
1981 $descriptionformat = FORMAT_MOODLE;
1982 } else {
1983 $descriptionformat = $coursecat->descriptionformat;
1985 if ($options === null) {
1986 $options = array('noclean' => true, 'overflowdiv' => true);
1987 } else {
1988 $options = (array)$options;
1990 $context = context_coursecat::instance($coursecat->id);
1991 if (!isset($options['context'])) {
1992 $options['context'] = $context;
1994 $text = file_rewrite_pluginfile_urls($coursecat->description,
1995 'pluginfile.php', $context->id, 'coursecat', 'description', null);
1996 return format_text($text, $descriptionformat, $options);
1998 return null;
2002 * Returns given course's summary with proper embedded files urls and formatted
2004 * @param core_course_list_element $course
2005 * @param array|stdClass $options additional formatting options
2006 * @return string
2008 public function get_course_formatted_summary($course, $options = array()) {
2009 global $CFG;
2010 require_once($CFG->libdir. '/filelib.php');
2011 if (!$course->has_summary()) {
2012 return '';
2014 $options = (array)$options;
2015 $context = context_course::instance($course->id);
2016 if (!isset($options['context'])) {
2017 $options['context'] = $context;
2019 $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null);
2020 $summary = format_text($summary, $course->summaryformat, $options);
2021 if (!empty($this->searchcriteria['search'])) {
2022 $summary = highlight($this->searchcriteria['search'], $summary);
2024 return $summary;
2028 * Returns course name as it is configured to appear in courses lists formatted to course context
2030 * @param core_course_list_element $course
2031 * @param array|stdClass $options additional formatting options
2032 * @return string
2034 public function get_course_formatted_name($course, $options = array()) {
2035 $options = (array)$options;
2036 if (!isset($options['context'])) {
2037 $options['context'] = context_course::instance($course->id);
2039 $name = format_string(get_course_display_name_for_list($course), true, $options);
2040 if (!empty($this->searchcriteria['search'])) {
2041 $name = highlight($this->searchcriteria['search'], $name);
2043 return $name;