Merge branch 'MDL-74286-master' of https://github.com/ferranrecio/moodle
[moodle.git] / course / renderer.php
blob27c53644075e405251e7fdf0e5ba34c6d21c9500
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Renderer for use with the course section and all the goodness that falls
20 * within it.
22 * This renderer should contain methods useful to courses, and categories.
24 * @package moodlecore
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 /**
30 * The core course renderer
32 * Can be retrieved with the following:
33 * $renderer = $PAGE->get_renderer('core','course');
35 class core_course_renderer extends plugin_renderer_base {
36 const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */
37 const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */
38 const COURSECAT_SHOW_COURSES_COLLAPSED = 10;
39 const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */
40 const COURSECAT_SHOW_COURSES_EXPANDED = 20;
41 const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
43 const COURSECAT_TYPE_CATEGORY = 0;
44 const COURSECAT_TYPE_COURSE = 1;
46 /**
47 * A cache of strings
48 * @var stdClass
50 protected $strings;
52 /**
53 * Whether a category content is being initially rendered with children. This is mainly used by the
54 * core_course_renderer::corsecat_tree() to render the appropriate action for the Expand/Collapse all link on
55 * page load.
56 * @var bool
58 protected $categoryexpandedonload = false;
60 /**
61 * Override the constructor so that we can initialise the string cache
63 * @param moodle_page $page
64 * @param string $target
66 public function __construct(moodle_page $page, $target) {
67 $this->strings = new stdClass;
68 $courseid = $page->course->id;
69 parent::__construct($page, $target);
72 /**
73 * @deprecated since 3.2
75 protected function add_modchoosertoggle() {
76 throw new coding_exception('core_course_renderer::add_modchoosertoggle() can not be used anymore.');
79 /**
80 * Renders course info box.
82 * @param stdClass $course
83 * @return string
85 public function course_info_box(stdClass $course) {
86 $content = '';
87 $content .= $this->output->box_start('generalbox info');
88 $chelper = new coursecat_helper();
89 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
90 $content .= $this->coursecat_coursebox($chelper, $course);
91 $content .= $this->output->box_end();
92 return $content;
95 /**
96 * Renderers a structured array of courses and categories into a nice XHTML tree structure.
98 * @deprecated since 2.5
100 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
102 * @param array $ignored argument ignored
103 * @return string
105 public final function course_category_tree(array $ignored) {
106 debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER);
107 return $this->frontpage_combo_list();
111 * Renderers a category for use with course_category_tree
113 * @deprecated since 2.5
115 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
117 * @param array $category
118 * @param int $depth
119 * @return string
121 protected final function course_category_tree_category(stdClass $category, $depth=1) {
122 debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER);
123 return '';
127 * Render a modchooser.
129 * @param renderable $modchooser The chooser.
130 * @return string
132 public function render_modchooser(renderable $modchooser) {
133 return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
137 * Build the HTML for the module chooser javascript popup
139 * @param array $modules A set of modules as returned form @see
140 * get_module_metadata
141 * @param object $course The course that will be displayed
142 * @return string The composed HTML for the module
144 public function course_modchooser($modules, $course) {
145 debugging('course_modchooser() is deprecated. Please use course_activitychooser() instead.', DEBUG_DEVELOPER);
147 return $this->course_activitychooser($course->id);
151 * Build the HTML for the module chooser javascript popup.
153 * @param int $courseid The course id to fetch modules for.
154 * @return string
156 public function course_activitychooser($courseid) {
158 if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
159 return '';
162 // Build an object of config settings that we can then hook into in the Activity Chooser.
163 $chooserconfig = (object) [
164 'tabmode' => get_config('core', 'activitychoosertabmode'),
166 $this->page->requires->js_call_amd('core_course/activitychooser', 'init', [$courseid, $chooserconfig]);
168 return '';
172 * Build the HTML for a specified set of modules
174 * @param array $modules A set of modules as used by the
175 * course_modchooser_module function
176 * @return string The composed HTML for the module
178 protected function course_modchooser_module_types($modules) {
179 debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
180 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
181 return '';
185 * Return the HTML for the specified module adding any required classes
187 * @param object $module An object containing the title, and link. An
188 * icon, and help text may optionally be specified. If the module
189 * contains subtypes in the types option, then these will also be
190 * displayed.
191 * @param array $classes Additional classes to add to the encompassing
192 * div element
193 * @return string The composed HTML for the module
195 protected function course_modchooser_module($module, $classes = array('option')) {
196 debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
197 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
198 return '';
201 protected function course_modchooser_title($title, $identifier = null) {
202 debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
203 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
204 return '';
208 * Renders HTML for displaying the sequence of course module editing buttons
210 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
212 * @see course_get_cm_edit_actions()
214 * @param action_link[] $actions Array of action_link objects
215 * @param cm_info $mod The module we are displaying actions for.
216 * @param array $displayoptions additional display options:
217 * ownerselector => A JS/CSS selector that can be used to find an cm node.
218 * If specified the owning node will be given the class 'action-menu-shown' when the action
219 * menu is being displayed.
220 * constraintselector => A JS/CSS selector that can be used to find the parent node for which to constrain
221 * the action menu to when it is being displayed.
222 * donotenhance => If set to true the action menu that gets displayed won't be enhanced by JS.
223 * @return string
225 public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) {
226 global $CFG;
228 debugging(
229 'course_section_cm_edit_actions is deprecated. Use core_courseformat\\output\\local\\content\\cm\\controlmenu instead.',
230 DEBUG_DEVELOPER
233 if (empty($actions)) {
234 return '';
237 if (isset($displayoptions['ownerselector'])) {
238 $ownerselector = $displayoptions['ownerselector'];
239 } else if ($mod) {
240 $ownerselector = '#module-'.$mod->id;
241 } else {
242 debugging('You should upgrade your call to '.__FUNCTION__.' and provide $mod', DEBUG_DEVELOPER);
243 $ownerselector = 'li.activity';
246 if (isset($displayoptions['constraintselector'])) {
247 $constraint = $displayoptions['constraintselector'];
248 } else {
249 $constraint = '.course-content';
252 $menu = new action_menu();
253 $menu->set_owner_selector($ownerselector);
254 $menu->set_constraint($constraint);
255 $menu->set_menu_trigger(get_string('edit'));
257 foreach ($actions as $action) {
258 if ($action instanceof action_menu_link) {
259 $action->add_class('cm-edit-action');
261 $menu->add($action);
263 $menu->attributes['class'] .= ' section-cm-edit-actions commands';
265 // Prioritise the menu ahead of all other actions.
266 $menu->prioritise = true;
268 return $this->render($menu);
272 * Renders HTML for the menus to add activities and resources to the current course
274 * Renders the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback.
276 * @param stdClass $course
277 * @param int $section relative section number (field course_sections.section)
278 * @param int $sectionreturn The section to link back to
279 * @param array $displayoptions additional display options, for example blocks add
280 * option 'inblock' => true, suggesting to display controls vertically
281 * @return string
283 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
284 // Check to see if user can add menus.
285 if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
286 || !$this->page->user_is_editing()) {
287 return '';
290 $data = [
291 'sectionid' => $section,
292 'sectionreturn' => $sectionreturn
294 $ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
296 // Load the JS for the modal.
297 $this->course_activitychooser($course->id);
299 return $ajaxcontrol;
303 * Render the deprecated nonajax activity chooser.
305 * @deprecated since Moodle 3.11
307 * @todo MDL-71331 deprecate this function
308 * @param stdClass $course the course object
309 * @param int $section relative section number (field course_sections.section)
310 * @param int $sectionreturn The section to link back to
311 * @param array $displayoptions additional display options, for example blocks add
312 * option 'inblock' => true, suggesting to display controls vertically
313 * @return string
315 private function course_section_add_cm_control_nonajax($course, $section, $sectionreturn = null,
316 $displayoptions = array()): string {
317 global $USER;
319 $vertical = !empty($displayoptions['inblock']);
321 // Check to see if user can add menus.
322 if (
323 !has_capability('moodle/course:manageactivities', context_course::instance($course->id))
324 || !$this->page->user_is_editing()
326 return '';
329 debugging('non-js dropdowns are deprecated.', DEBUG_DEVELOPER);
330 // Retrieve all modules with associated metadata.
331 $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
332 $urlparams = ['section' => $section];
333 if (!is_null($sectionreturn)) {
334 $urlparams['sr'] = $sectionreturn;
336 $modules = $contentitemservice->get_content_items_for_user_in_course($USER, $course, $urlparams);
338 // Return if there are no content items to add.
339 if (empty($modules)) {
340 return '';
343 // We'll sort resources and activities into two lists.
344 $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
346 foreach ($modules as $module) {
347 $activityclass = MOD_CLASS_ACTIVITY;
348 if ($module->archetype == MOD_ARCHETYPE_RESOURCE) {
349 $activityclass = MOD_CLASS_RESOURCE;
350 } else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) {
351 // System modules cannot be added by user, do not add to dropdown.
352 continue;
354 $link = $module->link;
355 $activities[$activityclass][$link] = $module->title;
358 $straddactivity = get_string('addactivity');
359 $straddresource = get_string('addresource');
360 $sectionname = get_section_name($course, $section);
361 $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
362 $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
364 $nonajaxcontrol = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-'
365 . $section));
367 if (!$vertical) {
368 $nonajaxcontrol .= html_writer::start_tag('div', array('class' => 'horizontal'));
371 if (!empty($activities[MOD_CLASS_RESOURCE])) {
372 $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array('' => $straddresource), "ressection$section");
373 $select->set_help_icon('resources');
374 $select->set_label($strresourcelabel, array('class' => 'accesshide'));
375 $nonajaxcontrol .= $this->output->render($select);
378 if (!empty($activities[MOD_CLASS_ACTIVITY])) {
379 $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array('' => $straddactivity), "section$section");
380 $select->set_help_icon('activities');
381 $select->set_label($stractivitylabel, array('class' => 'accesshide'));
382 $nonajaxcontrol .= $this->output->render($select);
385 if (!$vertical) {
386 $nonajaxcontrol .= html_writer::end_tag('div');
389 $nonajaxcontrol .= html_writer::end_tag('div');
391 return $nonajaxcontrol;
395 * Renders html to display a course search form
397 * @param string $value default value to populate the search field
398 * @return string
400 public function course_search_form($value = '') {
402 $data = [
403 'action' => \core_search\manager::get_course_search_url(),
404 'btnclass' => 'btn-primary',
405 'inputname' => 'q',
406 'searchstring' => get_string('searchcourses'),
407 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
408 'query' => $value
410 return $this->render_from_template('core/search_input', $data);
414 * Renders html for completion box on course page
416 * If completion is disabled, returns empty string
417 * If completion is automatic, returns an icon of the current completion state
418 * If completion is manual, returns a form (with an icon inside) that allows user to
419 * toggle completion
421 * @deprecated since Moodle 3.11
422 * @todo MDL-71183 Final deprecation in Moodle 4.3.
423 * @see \core_renderer::activity_information
425 * @param stdClass $course course object
426 * @param completion_info $completioninfo completion info for the course, it is recommended
427 * to fetch once for all modules in course/section for performance
428 * @param cm_info $mod module to show completion for
429 * @param array $displayoptions display options, not used in core
430 * @return string
432 public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) {
433 global $CFG, $DB, $USER;
435 debugging(__FUNCTION__ . ' is deprecated and is being replaced by the activity_information output component.',
436 DEBUG_DEVELOPER);
438 $output = '';
440 $istrackeduser = $completioninfo->is_tracked_user($USER->id);
441 $isediting = $this->page->user_is_editing();
443 if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
444 return $output;
446 if ($completioninfo === null) {
447 $completioninfo = new completion_info($course);
449 $completion = $completioninfo->is_enabled($mod);
451 if ($completion == COMPLETION_TRACKING_NONE) {
452 if ($isediting) {
453 $output .= html_writer::span('&nbsp;', 'filler');
455 return $output;
458 $completionicon = '';
460 if ($isediting || !$istrackeduser) {
461 switch ($completion) {
462 case COMPLETION_TRACKING_MANUAL :
463 $completionicon = 'manual-enabled'; break;
464 case COMPLETION_TRACKING_AUTOMATIC :
465 $completionicon = 'auto-enabled'; break;
467 } else {
468 $completiondata = $completioninfo->get_data($mod, true);
469 if ($completion == COMPLETION_TRACKING_MANUAL) {
470 switch($completiondata->completionstate) {
471 case COMPLETION_INCOMPLETE:
472 $completionicon = 'manual-n' . ($completiondata->overrideby ? '-override' : '');
473 break;
474 case COMPLETION_COMPLETE:
475 $completionicon = 'manual-y' . ($completiondata->overrideby ? '-override' : '');
476 break;
478 } else { // Automatic
479 switch($completiondata->completionstate) {
480 case COMPLETION_INCOMPLETE:
481 $completionicon = 'auto-n' . ($completiondata->overrideby ? '-override' : '');
482 break;
483 case COMPLETION_COMPLETE:
484 $completionicon = 'auto-y' . ($completiondata->overrideby ? '-override' : '');
485 break;
486 case COMPLETION_COMPLETE_PASS:
487 $completionicon = 'auto-pass'; break;
488 case COMPLETION_COMPLETE_FAIL:
489 $completionicon = 'auto-fail'; break;
493 if ($completionicon) {
494 $formattedname = html_entity_decode($mod->get_formatted_name(), ENT_QUOTES, 'UTF-8');
495 if (!$isediting && $istrackeduser && $completiondata->overrideby) {
496 $args = new stdClass();
497 $args->modname = $formattedname;
498 $overridebyuser = \core_user::get_user($completiondata->overrideby, '*', MUST_EXIST);
499 $args->overrideuser = fullname($overridebyuser);
500 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $args);
501 } else {
502 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
505 if ($isediting || !$istrackeduser || !has_capability('moodle/course:togglecompletion', $mod->context)) {
506 // When editing, the icon is just an image.
507 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
508 array('title' => $imgalt, 'class' => 'iconsmall'));
509 $output .= html_writer::tag('span', $this->output->render($completionpixicon),
510 array('class' => 'autocompletion'));
511 } else if ($completion == COMPLETION_TRACKING_MANUAL) {
512 $newstate =
513 $completiondata->completionstate == COMPLETION_COMPLETE
514 ? COMPLETION_INCOMPLETE
515 : COMPLETION_COMPLETE;
516 // In manual mode the icon is a toggle form...
518 // If this completion state is used by the
519 // conditional activities system, we need to turn
520 // off the JS.
521 $extraclass = '';
522 if (!empty($CFG->enableavailability) &&
523 core_availability\info::completion_value_used($course, $mod->id)) {
524 $extraclass = ' preventjs';
526 $output .= html_writer::start_tag('form', array('method' => 'post',
527 'action' => new moodle_url('/course/togglecompletion.php'),
528 'class' => 'togglecompletion'. $extraclass));
529 $output .= html_writer::start_tag('div');
530 $output .= html_writer::empty_tag('input', array(
531 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
532 $output .= html_writer::empty_tag('input', array(
533 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
534 $output .= html_writer::empty_tag('input', array(
535 'type' => 'hidden', 'name' => 'modulename', 'value' => $formattedname));
536 $output .= html_writer::empty_tag('input', array(
537 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
538 $output .= html_writer::tag('button',
539 $this->output->pix_icon('i/completion-' . $completionicon, $imgalt),
540 array('class' => 'btn btn-link', 'aria-live' => 'assertive'));
541 $output .= html_writer::end_tag('div');
542 $output .= html_writer::end_tag('form');
543 } else {
544 // In auto mode, the icon is just an image.
545 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
546 array('title' => $imgalt));
547 $output .= html_writer::tag('span', $this->output->render($completionpixicon),
548 array('class' => 'autocompletion'));
551 return $output;
555 * Checks if course module has any conditions that may make it unavailable for
556 * all or some of the students
558 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
560 * @param cm_info $mod
561 * @return bool
563 public function is_cm_conditionally_hidden(cm_info $mod) {
564 global $CFG;
566 debugging(
567 'is_cm_conditionally_hidden is deprecated. Use \core_availability\info_module::is_available_for_all instead',
568 DEBUG_DEVELOPER
571 $conditionalhidden = false;
572 if (!empty($CFG->enableavailability)) {
573 $info = new \core_availability\info_module($mod);
574 $conditionalhidden = !$info->is_available_for_all();
576 return $conditionalhidden;
580 * Renders html to display a name with the link to the course module on a course page
582 * If module is unavailable for user but still needs to be displayed
583 * in the list, just the name is returned without a link
585 * Note, that for course modules that never have separate pages (i.e. labels)
586 * this function return an empty string
588 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
590 * @param cm_info $mod
591 * @param array $displayoptions
592 * @return string
594 public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
595 debugging(
596 'course_section_cm_name is deprecated. Use core_courseformat\\output\\local\\content\\cm\\cmname class instead.',
597 DEBUG_DEVELOPER
600 if (!$mod->is_visible_on_course_page() || !$mod->url) {
601 // Nothing to be displayed to the user.
602 return '';
605 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
606 $groupinglabel = $mod->get_grouping_label($textclasses);
608 // Render element that allows to edit activity name inline.
609 $format = course_get_format($mod->course);
610 $cmnameclass = $format->get_output_classname('content\\cm\\cmname');
611 // Mod inplace name editable.
612 $cmname = new $cmnameclass(
613 $format,
614 $mod->get_section_info(),
615 $mod,
616 $this->page->user_is_editing(),
617 $displayoptions
620 $data = $cmname->export_for_template($this->output);
622 return $this->output->render_from_template('core/inplace_editable', $data) .
623 $groupinglabel;
627 * Returns the CSS classes for the activity name/content
629 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
631 * For items which are hidden, unavailable or stealth but should be displayed
632 * to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
633 * Students will also see as dimmed activities names that are not yet available
634 * but should still be displayed (without link) with availability info.
636 * @param cm_info $mod
637 * @return array array of two elements ($linkclasses, $textclasses)
639 protected function course_section_cm_classes(cm_info $mod) {
641 debugging(
642 'course_section_cm_classes is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
643 DEBUG_DEVELOPER
646 $format = course_get_format($mod->course);
648 $cmclass = $format->get_output_classname('content\\cm');
649 $cmoutput = new $cmclass(
650 $format,
651 $mod->get_section_info(),
652 $mod,
654 return [
655 $cmoutput->get_link_classes(),
656 $cmoutput->get_text_classes(),
661 * Renders html to display a name with the link to the course module on a course page
663 * If module is unavailable for user but still needs to be displayed
664 * in the list, just the name is returned without a link
666 * Note, that for course modules that never have separate pages (i.e. labels)
667 * this function return an empty string
669 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
671 * @param cm_info $mod
672 * @param array $displayoptions
673 * @return string
675 public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) {
677 debugging(
678 'course_section_cm_name_title is deprecated. Use core_courseformat\\output\\local\\cm\\title class instead.',
679 DEBUG_DEVELOPER
682 $output = '';
683 $url = $mod->url;
684 if (!$mod->is_visible_on_course_page() || !$url) {
685 // Nothing to be displayed to the user.
686 return $output;
689 //Accessibility: for files get description via icon, this is very ugly hack!
690 $instancename = $mod->get_formatted_name();
691 $altname = $mod->modfullname;
692 // Avoid unnecessary duplication: if e.g. a forum name already
693 // includes the word forum (or Forum, etc) then it is unhelpful
694 // to include that in the accessible description that is added.
695 if (false !== strpos(core_text::strtolower($instancename),
696 core_text::strtolower($altname))) {
697 $altname = '';
699 // File type after name, for alphabetic lists (screen reader).
700 if ($altname) {
701 $altname = get_accesshide(' '.$altname);
704 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
706 // Get on-click attribute value if specified and decode the onclick - it
707 // has already been encoded for display (puke).
708 $onclick = htmlspecialchars_decode($mod->onclick, ENT_QUOTES);
710 // Display link itself.
711 $instancename = html_writer::tag('span', $instancename . $altname, ['class' => 'instancename ml-1']);
713 $imageicon = html_writer::empty_tag('img', ['src' => $mod->get_icon_url(),
714 'class' => 'activityicon', 'alt' => '', 'role' => 'presentation', 'aria-hidden' => 'true']);
715 $imageicon = html_writer::tag('span', $imageicon, ['class' => 'activityiconcontainer courseicon']);
716 $activitylink = $imageicon . $instancename;
718 if ($mod->uservisible) {
719 $output .= html_writer::link($url, $activitylink, array('class' => 'aalink' . $linkclasses, 'onclick' => $onclick));
720 } else {
721 // We may be displaying this just in order to show information
722 // about visibility, without the actual link ($mod->is_visible_on_course_page()).
723 $output .= html_writer::tag('div', $activitylink, array('class' => $textclasses));
725 return $output;
729 * Renders html to display the module content on the course page (i.e. text of the labels)
731 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
733 * @param cm_info $mod
734 * @param array $displayoptions
735 * @return string
737 public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
739 debugging(
740 'course_section_cm_text is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
741 DEBUG_DEVELOPER
744 $output = '';
745 if (!$mod->is_visible_on_course_page()) {
746 // nothing to be displayed to the user
747 return $output;
749 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
750 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
751 if ($mod->url && $mod->uservisible) {
752 if ($content) {
753 // If specified, display extra content after link.
754 $output = html_writer::tag('div', $content, array('class' =>
755 trim('contentafterlink ' . $textclasses)));
757 } else {
758 $groupinglabel = $mod->get_grouping_label($textclasses);
760 // No link, so display only content.
761 $output = html_writer::tag('div', $content . $groupinglabel,
762 array('class' => 'contentwithoutlink ' . $textclasses));
764 return $output;
768 * Displays availability info for a course section or course module
770 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
771 * @param string $text
772 * @param string $additionalclasses
773 * @return string
775 public function availability_info($text, $additionalclasses = '') {
777 debugging(
778 'availability_info is deprecated. Use core_courseformat\\output\\local\\content\\section\\availability instead',
779 DEBUG_DEVELOPER
782 $data = ['text' => $text, 'classes' => $additionalclasses];
783 $additionalclasses = array_filter(explode(' ', $additionalclasses));
785 if (in_array('ishidden', $additionalclasses)) {
786 $data['ishidden'] = 1;
788 } else if (in_array('isstealth', $additionalclasses)) {
789 $data['isstealth'] = 1;
791 } else if (in_array('isrestricted', $additionalclasses)) {
792 $data['isrestricted'] = 1;
794 if (in_array('isfullinfo', $additionalclasses)) {
795 $data['isfullinfo'] = 1;
799 return $this->render_from_template('core/availability_info', $data);
803 * Renders HTML to show course module availability information (for someone who isn't allowed
804 * to see the activity itself, or for staff)
806 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
807 * @param cm_info $mod
808 * @param array $displayoptions
809 * @return string
811 public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
813 debugging(
814 'course_section_cm_availability is deprecated. Use core_courseformat\\output\\local\\content\\cm\\availability instead',
815 DEBUG_DEVELOPER
818 $format = course_get_format($mod->course);
820 $availabilityclass = $format->get_output_classname('content\\cm\\availability');
821 $availability = new $availabilityclass(
822 $format,
823 $mod->get_section_info(),
824 $mod,
826 $renderer = $format->get_renderer($this->page);
827 return $renderer->render($availability);
831 * Renders HTML to display one course module for display within a section.
833 * @deprecated since 4.0 - use core_course output components or course_format::course_section_updated_cm_item instead.
835 * This function calls:
836 * {@link core_course_renderer::course_section_cm()}
838 * @param stdClass $course
839 * @param completion_info $completioninfo
840 * @param cm_info $mod
841 * @param int|null $sectionreturn
842 * @param array $displayoptions
843 * @return String
845 public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
847 debugging(
848 'course_section_cm_list_item is deprecated. Use renderer course_section_updated_cm_item instead',
849 DEBUG_DEVELOPER
852 $output = '';
853 if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
854 $modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses;
855 $output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id));
857 return $output;
861 * Renders HTML to display one course module in a course section
863 * This includes link, content, availability, completion info and additional information
864 * that module type wants to display (i.e. number of unread forum posts)
866 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
868 * @param stdClass $course
869 * @param completion_info $completioninfo
870 * @param cm_info $mod
871 * @param int|null $sectionreturn
872 * @param array $displayoptions
873 * @return string
875 public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
877 debugging(
878 'course_section_cm is deprecated. Use core_courseformat\\output\\content\\cm output class instead.',
879 DEBUG_DEVELOPER
882 if (!$mod->is_visible_on_course_page()) {
883 return '';
886 $format = course_get_format($course);
887 $modinfo = $format->get_modinfo();
888 // Output renderers works only with real section_info objects.
889 if ($sectionreturn) {
890 $format->set_section_number($sectionreturn);
892 $section = $modinfo->get_section_info($format->get_section_number());
894 $cmclass = $format->get_output_classname('content\\cm');
895 $cm = new $cmclass($format, $section, $mod, $displayoptions);
896 // The course outputs works with format renderers, not with course renderers.
897 $renderer = $format->get_renderer($this->page);
898 $data = $cm->export_for_template($renderer);
899 return $this->output->render_from_template('core_courseformat/local/content/cm', $data);
903 * Message displayed to the user when they try to access unavailable activity following URL
905 * This method is a very simplified version of {@link course_section_cm()} to be part of the error
906 * notification only. It also does not check if module is visible on course page or not.
908 * The message will be displayed inside notification!
910 * @param cm_info $cm
911 * @return string
913 public function course_section_cm_unavailable_error_message(cm_info $cm) {
914 if ($cm->uservisible) {
915 return null;
917 if (!$cm->availableinfo) {
918 return get_string('activityiscurrentlyhidden');
921 $altname = get_accesshide(' ' . $cm->modfullname);
922 $name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(),
923 'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) .
924 html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
925 $formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
926 return html_writer::div($name, 'activityinstance-error') .
927 html_writer::div($formattedinfo, 'availabilityinfo-error');
931 * Renders HTML to display a list of course modules in a course section
932 * Also displays "move here" controls in Javascript-disabled mode.
934 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
936 * This function calls {@link core_course_renderer::course_section_cm()}
938 * @param stdClass $course course object
939 * @param int|stdClass|section_info $section relative section number or section object
940 * @param int $sectionreturn section number to return to
941 * @param int $displayoptions
942 * @return void
944 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = []) {
945 global $USER;
947 debugging('course_section_cm_list is deprecated. Use core_courseformat\\output\\local\\content\\section\\cmlist '.
948 'classes instead.', DEBUG_DEVELOPER);
950 $output = '';
952 $format = course_get_format($course);
953 $modinfo = $format->get_modinfo();
955 if (is_object($section)) {
956 $section = $modinfo->get_section_info($section->section);
957 } else {
958 $section = $modinfo->get_section_info($section);
960 $completioninfo = new completion_info($course);
962 // check if we are currently in the process of moving a module with JavaScript disabled
963 $ismoving = $format->show_editor() && ismoving($course->id);
965 if ($ismoving) {
966 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
969 // Get the list of modules visible to user (excluding the module being moved if there is one)
970 $moduleshtml = [];
971 if (!empty($modinfo->sections[$section->section])) {
972 foreach ($modinfo->sections[$section->section] as $modnumber) {
973 $mod = $modinfo->cms[$modnumber];
975 if ($ismoving and $mod->id == $USER->activitycopy) {
976 // do not display moving mod
977 continue;
980 if ($modulehtml = $this->course_section_cm_list_item($course,
981 $completioninfo, $mod, $sectionreturn, $displayoptions)) {
982 $moduleshtml[$modnumber] = $modulehtml;
987 $sectionoutput = '';
988 if (!empty($moduleshtml) || $ismoving) {
989 foreach ($moduleshtml as $modnumber => $modulehtml) {
990 if ($ismoving) {
991 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey()));
992 $sectionoutput .= html_writer::tag('li',
993 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
994 array('class' => 'movehere'));
997 $sectionoutput .= $modulehtml;
1000 if ($ismoving) {
1001 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
1002 $sectionoutput .= html_writer::tag('li',
1003 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
1004 array('class' => 'movehere'));
1008 // Always output the section module list.
1009 $output .= html_writer::tag('ul', $sectionoutput, array('class' => 'section img-text'));
1011 return $output;
1015 * Displays a custom list of courses with paging bar if necessary
1017 * If $paginationurl is specified but $totalcount is not, the link 'View more'
1018 * appears under the list.
1020 * If both $paginationurl and $totalcount are specified, and $totalcount is
1021 * bigger than count($courses), a paging bar is displayed above and under the
1022 * courses list.
1024 * @param array $courses array of course records (or instances of core_course_list_element) to show on this page
1025 * @param bool $showcategoryname whether to add category name to the course description
1026 * @param string $additionalclasses additional CSS classes to add to the div.courses
1027 * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
1028 * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
1029 * @param int $page current page number (defaults to 0 referring to the first page)
1030 * @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
1031 * @return string
1033 public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) {
1034 global $CFG;
1035 // create instance of coursecat_helper to pass display options to function rendering courses list
1036 $chelper = new coursecat_helper();
1037 if ($showcategoryname) {
1038 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT);
1039 } else {
1040 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1042 if ($totalcount !== null && $paginationurl !== null) {
1043 // add options to display pagination
1044 if ($perpage === null) {
1045 $perpage = $CFG->coursesperpage;
1047 $chelper->set_courses_display_options(array(
1048 'limit' => $perpage,
1049 'offset' => ((int)$page) * $perpage,
1050 'paginationurl' => $paginationurl,
1052 } else if ($paginationurl !== null) {
1053 // add options to display 'View more' link
1054 $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl));
1055 $totalcount = count($courses) + 1; // has to be bigger than count($courses) otherwise link will not be displayed
1057 $chelper->set_attributes(array('class' => $additionalclasses));
1058 $content = $this->coursecat_courses($chelper, $courses, $totalcount);
1059 return $content;
1063 * Returns HTML to display course name.
1065 * @param coursecat_helper $chelper
1066 * @param core_course_list_element $course
1067 * @return string
1069 protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string {
1070 $content = '';
1071 if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) {
1072 $nametag = 'h3';
1073 } else {
1074 $nametag = 'div';
1076 $coursename = $chelper->get_course_formatted_name($course);
1077 $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]),
1078 $coursename, ['class' => $course->visible ? 'aalink' : 'aalink dimmed']);
1079 $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']);
1080 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
1081 $content .= html_writer::start_tag('div', ['class' => 'moreinfo']);
1082 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1083 if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles()
1084 || $course->has_custom_fields()) {
1085 $url = new moodle_url('/course/info.php', ['id' => $course->id]);
1086 $image = $this->output->pix_icon('i/info', $this->strings->summary);
1087 $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]);
1088 // Make sure JS file to expand course content is included.
1089 $this->coursecat_include_js();
1092 $content .= html_writer::end_tag('div');
1093 return $content;
1097 * Returns HTML to display course enrolment icons.
1099 * @param core_course_list_element $course
1100 * @return string
1102 protected function course_enrolment_icons(core_course_list_element $course): string {
1103 $content = '';
1104 if ($icons = enrol_get_course_info_icons($course)) {
1105 $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']);
1106 foreach ($icons as $icon) {
1107 $content .= $this->render($icon);
1109 $content .= html_writer::end_tag('div');
1111 return $content;
1115 * Displays one course in the list of courses.
1117 * This is an internal function, to display an information about just one course
1118 * please use {@link core_course_renderer::course_info_box()}
1120 * @param coursecat_helper $chelper various display options
1121 * @param core_course_list_element|stdClass $course
1122 * @param string $additionalclasses additional classes to add to the main <div> tag (usually
1123 * depend on the course position in list - first/last/even/odd)
1124 * @return string
1126 protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') {
1127 if (!isset($this->strings->summary)) {
1128 $this->strings->summary = get_string('summary');
1130 if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) {
1131 return '';
1133 if ($course instanceof stdClass) {
1134 $course = new core_course_list_element($course);
1136 $content = '';
1137 $classes = trim('coursebox clearfix '. $additionalclasses);
1138 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1139 $classes .= ' collapsed';
1142 // .coursebox
1143 $content .= html_writer::start_tag('div', array(
1144 'class' => $classes,
1145 'data-courseid' => $course->id,
1146 'data-type' => self::COURSECAT_TYPE_COURSE,
1149 $content .= html_writer::start_tag('div', array('class' => 'info'));
1150 $content .= $this->course_name($chelper, $course);
1151 $content .= $this->course_enrolment_icons($course);
1152 $content .= html_writer::end_tag('div');
1154 $content .= html_writer::start_tag('div', array('class' => 'content'));
1155 $content .= $this->coursecat_coursebox_content($chelper, $course);
1156 $content .= html_writer::end_tag('div');
1158 $content .= html_writer::end_tag('div'); // .coursebox
1159 return $content;
1163 * Returns HTML to display course summary.
1165 * @param coursecat_helper $chelper
1166 * @param core_course_list_element $course
1167 * @return string
1169 protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string {
1170 $content = '';
1171 if ($course->has_summary()) {
1172 $content .= html_writer::start_tag('div', ['class' => 'summary']);
1173 $content .= $chelper->get_course_formatted_summary($course,
1174 array('overflowdiv' => true, 'noclean' => true, 'para' => false));
1175 $content .= html_writer::end_tag('div');
1177 return $content;
1181 * Returns HTML to display course contacts.
1183 * @param core_course_list_element $course
1184 * @return string
1186 protected function course_contacts(core_course_list_element $course) {
1187 $content = '';
1188 if ($course->has_course_contacts()) {
1189 $content .= html_writer::start_tag('ul', ['class' => 'teachers']);
1190 foreach ($course->get_course_contacts() as $coursecontact) {
1191 $rolenames = array_map(function ($role) {
1192 return $role->displayname;
1193 }, $coursecontact['roles']);
1194 $name = html_writer::tag('span', implode(", ", $rolenames).': ', ['class' => 'font-weight-bold']);
1195 $name .= html_writer::link(new moodle_url('/user/view.php',
1196 ['id' => $coursecontact['user']->id, 'course' => SITEID]),
1197 $coursecontact['username']);
1198 $content .= html_writer::tag('li', $name);
1200 $content .= html_writer::end_tag('ul');
1202 return $content;
1206 * Returns HTML to display course overview files.
1208 * @param core_course_list_element $course
1209 * @return string
1211 protected function course_overview_files(core_course_list_element $course): string {
1212 global $CFG;
1214 $contentimages = $contentfiles = '';
1215 foreach ($course->get_course_overviewfiles() as $file) {
1216 $isimage = $file->is_valid_image();
1217 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php",
1218 '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
1219 $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
1220 if ($isimage) {
1221 $contentimages .= html_writer::tag('div',
1222 html_writer::empty_tag('img', ['src' => $url]),
1223 ['class' => 'courseimage']);
1224 } else {
1225 $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
1226 $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']).
1227 html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
1228 $contentfiles .= html_writer::tag('span',
1229 html_writer::link($url, $filename),
1230 ['class' => 'coursefile fp-filename-icon']);
1233 return $contentimages . $contentfiles;
1237 * Returns HTML to display course category name.
1239 * @param coursecat_helper $chelper
1240 * @param core_course_list_element $course
1241 * @return string
1243 protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string {
1244 $content = '';
1245 // Display course category if necessary (for example in search results).
1246 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
1247 if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) {
1248 $content .= html_writer::start_tag('div', ['class' => 'coursecat']);
1249 $content .= html_writer::tag('span', get_string('category').': ', ['class' => 'font-weight-bold']);
1250 $content .= html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]),
1251 $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']);
1252 $content .= html_writer::end_tag('div');
1255 return $content;
1259 * Returns HTML to display course custom fields.
1261 * @param core_course_list_element $course
1262 * @return string
1264 protected function course_custom_fields(core_course_list_element $course): string {
1265 $content = '';
1266 if ($course->has_custom_fields()) {
1267 $handler = core_course\customfield\course_handler::create();
1268 $customfields = $handler->display_custom_fields_data($course->get_custom_fields());
1269 $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']);
1271 return $content;
1275 * Returns HTML to display course content (summary, course contacts and optionally category name)
1277 * This method is called from coursecat_coursebox() and may be re-used in AJAX
1279 * @param coursecat_helper $chelper various display options
1280 * @param stdClass|core_course_list_element $course
1281 * @return string
1283 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
1284 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1285 return '';
1287 if ($course instanceof stdClass) {
1288 $course = new core_course_list_element($course);
1290 $content = \html_writer::start_tag('div', ['class' => 'd-flex']);
1291 $content .= $this->course_overview_files($course);
1292 $content .= \html_writer::start_tag('div', ['class' => 'flex-grow-1']);
1293 $content .= $this->course_summary($chelper, $course);
1294 $content .= $this->course_contacts($course);
1295 $content .= $this->course_category_name($chelper, $course);
1296 $content .= $this->course_custom_fields($course);
1297 $content .= \html_writer::end_tag('div');
1298 $content .= \html_writer::end_tag('div');
1299 return $content;
1303 * Renders the list of courses
1305 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
1306 * method from outside of the class
1308 * If list of courses is specified in $courses; the argument $chelper is only used
1309 * to retrieve display options and attributes, only methods get_show_courses(),
1310 * get_courses_display_option() and get_and_erase_attributes() are called.
1312 * @param coursecat_helper $chelper various display options
1313 * @param array $courses the list of courses to display
1314 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
1315 * defaulted to count($courses)
1316 * @return string
1318 protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) {
1319 global $CFG;
1320 if ($totalcount === null) {
1321 $totalcount = count($courses);
1323 if (!$totalcount) {
1324 // Courses count is cached during courses retrieval.
1325 return '';
1328 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) {
1329 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
1330 if ($totalcount <= $CFG->courseswithsummarieslimit) {
1331 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1332 } else {
1333 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1337 // prepare content of paging bar if it is needed
1338 $paginationurl = $chelper->get_courses_display_option('paginationurl');
1339 $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
1340 if ($totalcount > count($courses)) {
1341 // there are more results that can fit on one page
1342 if ($paginationurl) {
1343 // the option paginationurl was specified, display pagingbar
1344 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage);
1345 $page = $chelper->get_courses_display_option('offset') / $perpage;
1346 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1347 $paginationurl->out(false, array('perpage' => $perpage)));
1348 if ($paginationallowall) {
1349 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1350 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1352 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1353 // the option for 'View more' link was specified, display more link
1354 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
1355 $morelink = html_writer::tag(
1356 'div',
1357 html_writer::link($viewmoreurl, $viewmoretext, ['class' => 'btn btn-secondary']),
1358 ['class' => 'paging paging-morelink']
1361 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1362 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1363 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1364 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1367 // display list of courses
1368 $attributes = $chelper->get_and_erase_attributes('courses');
1369 $content = html_writer::start_tag('div', $attributes);
1371 if (!empty($pagingbar)) {
1372 $content .= $pagingbar;
1375 $coursecount = 0;
1376 foreach ($courses as $course) {
1377 $coursecount ++;
1378 $classes = ($coursecount%2) ? 'odd' : 'even';
1379 if ($coursecount == 1) {
1380 $classes .= ' first';
1382 if ($coursecount >= count($courses)) {
1383 $classes .= ' last';
1385 $content .= $this->coursecat_coursebox($chelper, $course, $classes);
1388 if (!empty($pagingbar)) {
1389 $content .= $pagingbar;
1391 if (!empty($morelink)) {
1392 $content .= $morelink;
1395 $content .= html_writer::end_tag('div'); // .courses
1396 return $content;
1400 * Renders the list of subcategories in a category
1402 * @param coursecat_helper $chelper various display options
1403 * @param core_course_category $coursecat
1404 * @param int $depth depth of the category in the current tree
1405 * @return string
1407 protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) {
1408 global $CFG;
1409 $subcategories = array();
1410 if (!$chelper->get_categories_display_option('nodisplay')) {
1411 $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
1413 $totalcount = $coursecat->get_children_count();
1414 if (!$totalcount) {
1415 // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children()
1416 // to avoid extra DB requests.
1417 // Categories count is cached during children categories retrieval.
1418 return '';
1421 // prepare content of paging bar or more link if it is needed
1422 $paginationurl = $chelper->get_categories_display_option('paginationurl');
1423 $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
1424 if ($totalcount > count($subcategories)) {
1425 if ($paginationurl) {
1426 // the option 'paginationurl was specified, display pagingbar
1427 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage);
1428 $page = $chelper->get_categories_display_option('offset') / $perpage;
1429 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1430 $paginationurl->out(false, array('perpage' => $perpage)));
1431 if ($paginationallowall) {
1432 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1433 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1435 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
1436 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
1437 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
1438 $viewmoreurl->param('categoryid', $coursecat->id);
1440 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
1441 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
1442 array('class' => 'paging paging-morelink'));
1444 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1445 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1446 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1447 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1450 // display list of subcategories
1451 $content = html_writer::start_tag('div', array('class' => 'subcategories'));
1453 if (!empty($pagingbar)) {
1454 $content .= $pagingbar;
1457 foreach ($subcategories as $subcategory) {
1458 $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1);
1461 if (!empty($pagingbar)) {
1462 $content .= $pagingbar;
1464 if (!empty($morelink)) {
1465 $content .= $morelink;
1468 $content .= html_writer::end_tag('div');
1469 return $content;
1473 * Make sure that javascript file for AJAX expanding of courses and categories content is included
1475 protected function coursecat_include_js() {
1476 if (!$this->page->requires->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) {
1477 return;
1480 // We must only load this module once.
1481 $this->page->requires->yui_module('moodle-course-categoryexpander',
1482 'Y.Moodle.course.categoryexpander.init');
1486 * Returns HTML to display the subcategories and courses in the given category
1488 * This method is re-used by AJAX to expand content of not loaded category
1490 * @param coursecat_helper $chelper various display options
1491 * @param core_course_category $coursecat
1492 * @param int $depth depth of the category in the current tree
1493 * @return string
1495 protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) {
1496 $content = '';
1497 // Subcategories
1498 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
1500 // AUTO show courses: Courses will be shown expanded if this is not nested category,
1501 // and number of courses no bigger than $CFG->courseswithsummarieslimit.
1502 $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO;
1503 if ($showcoursesauto && $depth) {
1504 // this is definitely collapsed mode
1505 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1508 // Courses
1509 if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) {
1510 $courses = array();
1511 if (!$chelper->get_courses_display_option('nodisplay')) {
1512 $courses = $coursecat->get_courses($chelper->get_courses_display_options());
1514 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1515 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
1516 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
1517 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id)));
1520 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
1523 if ($showcoursesauto) {
1524 // restore the show_courses back to AUTO
1525 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO);
1528 return $content;
1532 * Returns HTML to display a course category as a part of a tree
1534 * This is an internal function, to display a particular category and all its contents
1535 * use {@link core_course_renderer::course_category()}
1537 * @param coursecat_helper $chelper various display options
1538 * @param core_course_category $coursecat
1539 * @param int $depth depth of this category in the current tree
1540 * @return string
1542 protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) {
1543 // open category tag
1544 $classes = array('category');
1545 if (empty($coursecat->visible)) {
1546 $classes[] = 'dimmed_category';
1548 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
1549 // do not load content
1550 $categorycontent = '';
1551 $classes[] = 'notloaded';
1552 if ($coursecat->get_children_count() ||
1553 ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) {
1554 $classes[] = 'with_children';
1555 $classes[] = 'collapsed';
1557 } else {
1558 // load category content
1559 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
1560 $classes[] = 'loaded';
1561 if (!empty($categorycontent)) {
1562 $classes[] = 'with_children';
1563 // Category content loaded with children.
1564 $this->categoryexpandedonload = true;
1568 // Make sure JS file to expand category content is included.
1569 $this->coursecat_include_js();
1571 $content = html_writer::start_tag('div', array(
1572 'class' => join(' ', $classes),
1573 'data-categoryid' => $coursecat->id,
1574 'data-depth' => $depth,
1575 'data-showcourses' => $chelper->get_show_courses(),
1576 'data-type' => self::COURSECAT_TYPE_CATEGORY,
1579 // category name
1580 $categoryname = $coursecat->get_formatted_name();
1581 $categoryname = html_writer::link(new moodle_url('/course/index.php',
1582 array('categoryid' => $coursecat->id)),
1583 $categoryname);
1584 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT
1585 && ($coursescount = $coursecat->get_courses_count())) {
1586 $categoryname .= html_writer::tag('span', ' ('. $coursescount.')',
1587 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
1589 $content .= html_writer::start_tag('div', array('class' => 'info'));
1591 $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn'));
1592 $content .= html_writer::end_tag('div'); // .info
1594 // add category content to the output
1595 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1597 $content .= html_writer::end_tag('div'); // .category
1599 // Return the course category tree HTML
1600 return $content;
1604 * Returns HTML to display a tree of subcategories and courses in the given category
1606 * @param coursecat_helper $chelper various display options
1607 * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree)
1608 * @return string
1610 protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
1611 // Reset the category expanded flag for this course category tree first.
1612 $this->categoryexpandedonload = false;
1613 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1614 if (empty($categorycontent)) {
1615 return '';
1618 // Start content generation
1619 $content = '';
1620 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1621 $content .= html_writer::start_tag('div', $attributes);
1623 if ($coursecat->get_children_count()) {
1624 $classes = array(
1625 'collapseexpand', 'aabtn'
1628 // Check if the category content contains subcategories with children's content loaded.
1629 if ($this->categoryexpandedonload) {
1630 $classes[] = 'collapse-all';
1631 $linkname = get_string('collapseall');
1632 } else {
1633 $linkname = get_string('expandall');
1636 // Only show the collapse/expand if there are children to expand.
1637 $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions'));
1638 $content .= html_writer::link('#', $linkname, array('class' => implode(' ', $classes)));
1639 $content .= html_writer::end_tag('div');
1640 $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
1643 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1645 $content .= html_writer::end_tag('div'); // .course_category_tree
1647 return $content;
1651 * Renders HTML to display particular course category - list of it's subcategories and courses
1653 * Invoked from /course/index.php
1655 * @param int|stdClass|core_course_category $category
1657 public function course_category($category) {
1658 global $CFG;
1659 $usertop = core_course_category::user_top();
1660 if (empty($category)) {
1661 $coursecat = $usertop;
1662 } else if (is_object($category) && $category instanceof core_course_category) {
1663 $coursecat = $category;
1664 } else {
1665 $coursecat = core_course_category::get(is_object($category) ? $category->id : $category);
1667 $site = get_site();
1668 $actionbar = new \core_course\output\category_action_bar($this->page, $coursecat);
1669 $output = $this->render_from_template('core_course/category_actionbar', $actionbar->export_for_template($this));
1671 if (core_course_category::is_simple_site()) {
1672 // There is only one category in the system, do not display link to it.
1673 $strfulllistofcourses = get_string('fulllistofcourses');
1674 $this->page->set_title("$site->shortname: $strfulllistofcourses");
1675 } else if (!$coursecat->id || !$coursecat->is_uservisible()) {
1676 $strcategories = get_string('categories');
1677 $this->page->set_title("$site->shortname: $strcategories");
1678 } else {
1679 $strfulllistofcourses = get_string('fulllistofcourses');
1680 $this->page->set_title("$site->shortname: $strfulllistofcourses");
1683 // Print current category description
1684 $chelper = new coursecat_helper();
1685 if ($description = $chelper->get_category_formatted_description($coursecat)) {
1686 $output .= $this->box($description, array('class' => 'generalbox info'));
1689 // Prepare parameters for courses and categories lists in the tree
1690 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
1691 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));
1693 $coursedisplayoptions = array();
1694 $catdisplayoptions = array();
1695 $browse = optional_param('browse', null, PARAM_ALPHA);
1696 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
1697 $page = optional_param('page', 0, PARAM_INT);
1698 $baseurl = new moodle_url('/course/index.php');
1699 if ($coursecat->id) {
1700 $baseurl->param('categoryid', $coursecat->id);
1702 if ($perpage != $CFG->coursesperpage) {
1703 $baseurl->param('perpage', $perpage);
1705 $coursedisplayoptions['limit'] = $perpage;
1706 $catdisplayoptions['limit'] = $perpage;
1707 if ($browse === 'courses' || !$coursecat->get_children_count()) {
1708 $coursedisplayoptions['offset'] = $page * $perpage;
1709 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1710 $catdisplayoptions['nodisplay'] = true;
1711 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1712 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
1713 } else if ($browse === 'categories' || !$coursecat->get_courses_count()) {
1714 $coursedisplayoptions['nodisplay'] = true;
1715 $catdisplayoptions['offset'] = $page * $perpage;
1716 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1717 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1718 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1719 } else {
1720 // we have a category that has both subcategories and courses, display pagination separately
1721 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1722 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1724 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1726 // Display course category tree.
1727 $output .= $this->coursecat_tree($chelper, $coursecat);
1729 return $output;
1733 * Serves requests to /course/category.ajax.php
1735 * In this renderer implementation it may expand the category content or
1736 * course content.
1738 * @return string
1739 * @throws coding_exception
1741 public function coursecat_ajax() {
1742 global $DB, $CFG;
1744 $type = required_param('type', PARAM_INT);
1746 if ($type === self::COURSECAT_TYPE_CATEGORY) {
1747 // This is a request for a category list of some kind.
1748 $categoryid = required_param('categoryid', PARAM_INT);
1749 $showcourses = required_param('showcourses', PARAM_INT);
1750 $depth = required_param('depth', PARAM_INT);
1752 $category = core_course_category::get($categoryid);
1754 $chelper = new coursecat_helper();
1755 $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
1756 $coursedisplayoptions = array(
1757 'limit' => $CFG->coursesperpage,
1758 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
1760 $catdisplayoptions = array(
1761 'limit' => $CFG->coursesperpage,
1762 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
1764 $chelper->set_show_courses($showcourses)->
1765 set_courses_display_options($coursedisplayoptions)->
1766 set_categories_display_options($catdisplayoptions);
1768 return $this->coursecat_category_content($chelper, $category, $depth);
1769 } else if ($type === self::COURSECAT_TYPE_COURSE) {
1770 // This is a request for the course information.
1771 $courseid = required_param('courseid', PARAM_INT);
1773 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
1775 $chelper = new coursecat_helper();
1776 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1777 return $this->coursecat_coursebox_content($chelper, $course);
1778 } else {
1779 throw new coding_exception('Invalid request type');
1784 * Renders html to display search result page
1786 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1787 * @return string
1789 public function search_courses($searchcriteria) {
1790 global $CFG;
1791 $content = '';
1793 $search = '';
1794 if (!empty($searchcriteria['search'])) {
1795 $search = $searchcriteria['search'];
1797 $content .= $this->course_search_form($search);
1799 if (!empty($searchcriteria)) {
1800 // print search results
1802 $displayoptions = array('sort' => array('displayname' => 1));
1803 // take the current page and number of results per page from query
1804 $perpage = optional_param('perpage', 0, PARAM_RAW);
1805 if ($perpage !== 'all') {
1806 $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
1807 $page = optional_param('page', 0, PARAM_INT);
1808 $displayoptions['offset'] = $displayoptions['limit'] * $page;
1810 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1811 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1812 $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1814 $class = 'course-search-result';
1815 foreach ($searchcriteria as $key => $value) {
1816 if (!empty($value)) {
1817 $class .= ' course-search-result-'. $key;
1820 $chelper = new coursecat_helper();
1821 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1822 set_courses_display_options($displayoptions)->
1823 set_search_criteria($searchcriteria)->
1824 set_attributes(array('class' => $class));
1826 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1827 $totalcount = core_course_category::search_courses_count($searchcriteria);
1828 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1830 if (!$totalcount) {
1831 if (!empty($searchcriteria['search'])) {
1832 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1833 } else {
1834 $content .= $this->heading(get_string('novalidcourses'));
1836 } else {
1837 $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1838 $content .= $courseslist;
1841 return $content;
1845 * Renders html to print list of courses tagged with particular tag
1847 * @param int $tagid id of the tag
1848 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1849 * are displayed on the page and the per-page limit may be bigger
1850 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1851 * to display items in the same context first
1852 * @param int $ctx context id where to search for records
1853 * @param bool $rec search in subcontexts as well
1854 * @param array $displayoptions
1855 * @return string empty string if no courses are marked with this tag or rendered list of courses
1857 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) {
1858 global $CFG;
1859 if (empty($displayoptions)) {
1860 $displayoptions = array();
1862 $showcategories = !core_course_category::is_simple_site();
1863 $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0);
1864 $chelper = new coursecat_helper();
1865 $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
1866 $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT :
1867 self::COURSECAT_SHOW_COURSES_EXPANDED)->
1868 set_search_criteria($searchcriteria)->
1869 set_courses_display_options($displayoptions)->
1870 set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1871 // (we set the same css class as in search results by tagid)
1872 if ($totalcount = core_course_category::search_courses_count($searchcriteria)) {
1873 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1874 if ($exclusivemode) {
1875 return $this->coursecat_courses($chelper, $courses, $totalcount);
1876 } else {
1877 $tagfeed = new core_tag\output\tagfeed();
1878 $img = $this->output->pix_icon('i/course', '');
1879 foreach ($courses as $course) {
1880 $url = course_get_url($course);
1881 $imgwithlink = html_writer::link($url, $img);
1882 $coursename = html_writer::link($url, $course->get_formatted_name());
1883 $details = '';
1884 if ($showcategories && ($cat = core_course_category::get($course->category, IGNORE_MISSING))) {
1885 $details = get_string('category').': '.
1886 html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)),
1887 $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
1889 $tagfeed->add($imgwithlink, $coursename, $details);
1891 return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output));
1894 return '';
1898 * Returns HTML to display one remote course
1900 * @param stdClass $course remote course information, contains properties:
1901 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1902 * @return string
1904 protected function frontpage_remote_course(stdClass $course) {
1905 $url = new moodle_url('/auth/mnet/jump.php', array(
1906 'hostid' => $course->hostid,
1907 'wantsurl' => '/course/view.php?id='. $course->remoteid
1910 $output = '';
1911 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1912 $output .= html_writer::start_tag('div', array('class' => 'info'));
1913 $output .= html_writer::start_tag('h3', array('class' => 'coursename'));
1914 $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
1915 $output .= html_writer::end_tag('h3'); // .name
1916 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1917 $output .= html_writer::end_tag('div'); // .info
1918 $output .= html_writer::start_tag('div', array('class' => 'content'));
1919 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1920 $options = new stdClass();
1921 $options->noclean = true;
1922 $options->para = false;
1923 $options->overflowdiv = true;
1924 $output .= format_text($course->summary, $course->summaryformat, $options);
1925 $output .= html_writer::end_tag('div'); // .summary
1926 $addinfo = format_string($course->hostname) . ' : '
1927 . format_string($course->cat_name) . ' : '
1928 . format_string($course->shortname);
1929 $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1930 $output .= html_writer::end_tag('div'); // .content
1931 $output .= html_writer::end_tag('div'); // .coursebox
1932 return $output;
1936 * Returns HTML to display one remote host
1938 * @param array $host host information, contains properties: name, url, count
1939 * @return string
1941 protected function frontpage_remote_host($host) {
1942 $output = '';
1943 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1944 $output .= html_writer::start_tag('div', array('class' => 'info'));
1945 $output .= html_writer::start_tag('h3', array('class' => 'name'));
1946 $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1947 $output .= html_writer::end_tag('h3'); // .name
1948 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1949 $output .= html_writer::end_tag('div'); // .info
1950 $output .= html_writer::start_tag('div', array('class' => 'content'));
1951 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1952 $output .= $host['count'] . ' ' . get_string('courses');
1953 $output .= html_writer::end_tag('div'); // .content
1954 $output .= html_writer::end_tag('div'); // .coursebox
1955 return $output;
1959 * Returns HTML to print list of courses user is enrolled to for the frontpage
1961 * Also lists remote courses or remote hosts if MNET authorisation is used
1963 * @return string
1965 public function frontpage_my_courses() {
1966 global $USER, $CFG, $DB;
1968 if (!isloggedin() or isguestuser()) {
1969 return '';
1972 $output = '';
1973 $courses = enrol_get_my_courses('summary, summaryformat');
1974 $rhosts = array();
1975 $rcourses = array();
1976 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
1977 $rcourses = get_my_remotecourses($USER->id);
1978 $rhosts = get_my_remotehosts();
1981 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1983 $chelper = new coursecat_helper();
1984 $totalcount = count($courses);
1985 if (count($courses) > $CFG->frontpagecourselimit) {
1986 // There are more enrolled courses than we can display, display link to 'My courses'.
1987 $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true);
1988 $chelper->set_courses_display_options(array(
1989 'viewmoreurl' => new moodle_url('/my/courses.php'),
1990 'viewmoretext' => new lang_string('mycourses')
1992 } else if (core_course_category::top()->is_uservisible()) {
1993 // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system.
1994 $chelper->set_courses_display_options(array(
1995 'viewmoreurl' => new moodle_url('/course/index.php'),
1996 'viewmoretext' => new lang_string('fulllistofcourses')
1998 $totalcount = $DB->count_records('course') - 1;
2000 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
2001 set_attributes(array('class' => 'frontpage-course-list-enrolled'));
2002 $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
2004 // MNET
2005 if (!empty($rcourses)) {
2006 // at the IDP, we know of all the remote courses
2007 $output .= html_writer::start_tag('div', array('class' => 'courses'));
2008 foreach ($rcourses as $course) {
2009 $output .= $this->frontpage_remote_course($course);
2011 $output .= html_writer::end_tag('div'); // .courses
2012 } elseif (!empty($rhosts)) {
2013 // non-IDP, we know of all the remote servers, but not courses
2014 $output .= html_writer::start_tag('div', array('class' => 'courses'));
2015 foreach ($rhosts as $host) {
2016 $output .= $this->frontpage_remote_host($host);
2018 $output .= html_writer::end_tag('div'); // .courses
2021 return $output;
2025 * Returns HTML to print list of available courses for the frontpage
2027 * @return string
2029 public function frontpage_available_courses() {
2030 global $CFG;
2032 $chelper = new coursecat_helper();
2033 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
2034 set_courses_display_options(array(
2035 'recursive' => true,
2036 'limit' => $CFG->frontpagecourselimit,
2037 'viewmoreurl' => new moodle_url('/course/index.php'),
2038 'viewmoretext' => new lang_string('fulllistofcourses')));
2040 $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
2041 $courses = core_course_category::top()->get_courses($chelper->get_courses_display_options());
2042 $totalcount = core_course_category::top()->get_courses_count($chelper->get_courses_display_options());
2043 if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) {
2044 // Print link to create a new course, for the 1st available category.
2045 return $this->add_new_course_button();
2047 return $this->coursecat_courses($chelper, $courses, $totalcount);
2051 * Returns HTML to the "add new course" button for the page
2053 * @return string
2055 public function add_new_course_button() {
2056 global $CFG;
2057 // Print link to create a new course, for the 1st available category.
2058 $output = $this->container_start('buttons');
2059 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
2060 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
2061 $output .= $this->container_end('buttons');
2062 return $output;
2066 * Returns HTML to print tree with course categories and courses for the frontpage
2068 * @return string
2070 public function frontpage_combo_list() {
2071 global $CFG;
2072 // TODO MDL-10965 improve.
2073 $tree = core_course_category::top();
2074 if (!$tree->get_children_count()) {
2075 return '';
2077 $chelper = new coursecat_helper();
2078 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
2079 set_categories_display_options(array(
2080 'limit' => $CFG->coursesperpage,
2081 'viewmoreurl' => new moodle_url('/course/index.php',
2082 array('browse' => 'categories', 'page' => 1))
2083 ))->
2084 set_courses_display_options(array(
2085 'limit' => $CFG->coursesperpage,
2086 'viewmoreurl' => new moodle_url('/course/index.php',
2087 array('browse' => 'courses', 'page' => 1))
2088 ))->
2089 set_attributes(array('class' => 'frontpage-category-combo'));
2090 return $this->coursecat_tree($chelper, $tree);
2094 * Returns HTML to print tree of course categories (with number of courses) for the frontpage
2096 * @return string
2098 public function frontpage_categories_list() {
2099 global $CFG;
2100 // TODO MDL-10965 improve.
2101 $tree = core_course_category::top();
2102 if (!$tree->get_children_count()) {
2103 return '';
2105 $chelper = new coursecat_helper();
2106 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
2107 set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
2108 set_categories_display_options(array(
2109 'limit' => $CFG->coursesperpage,
2110 'viewmoreurl' => new moodle_url('/course/index.php',
2111 array('browse' => 'categories', 'page' => 1))
2112 ))->
2113 set_attributes(array('class' => 'frontpage-category-names'));
2114 return $this->coursecat_tree($chelper, $tree);
2118 * Renders the activity information.
2120 * Defer to template.
2122 * @param \core_course\output\activity_information $page
2123 * @return string html for the page
2125 public function render_activity_information(\core_course\output\activity_information $page) {
2126 $data = $page->export_for_template($this->output);
2127 return $this->output->render_from_template('core_course/activity_info', $data);
2131 * Renders the activity navigation.
2133 * Defer to template.
2135 * @param \core_course\output\activity_navigation $page
2136 * @return string html for the page
2138 public function render_activity_navigation(\core_course\output\activity_navigation $page) {
2139 $data = $page->export_for_template($this->output);
2140 return $this->output->render_from_template('core_course/activity_navigation', $data);
2144 * Display waiting information about backup size during uploading backup process
2145 * @param object $backupfile the backup stored_file
2146 * @return $html string
2148 public function sendingbackupinfo($backupfile) {
2149 $sizeinfo = new stdClass();
2150 $sizeinfo->total = number_format($backupfile->get_filesize() / 1000000, 2);
2151 $html = html_writer::tag('div', get_string('sendingsize', 'hub', $sizeinfo),
2152 array('class' => 'courseuploadtextinfo'));
2153 return $html;
2157 * Hub information (logo - name - description - link)
2158 * @param object $hubinfo
2159 * @return string html code
2161 public function hubinfo($hubinfo) {
2162 $screenshothtml = html_writer::empty_tag('img',
2163 array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name']));
2164 $hubdescription = html_writer::tag('div', $screenshothtml,
2165 array('class' => 'hubscreenshot'));
2167 $hubdescription .= html_writer::tag('a', $hubinfo['name'],
2168 array('class' => 'hublink', 'href' => $hubinfo['url'],
2169 'onclick' => 'this.target="_blank"'));
2171 $hubdescription .= html_writer::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN),
2172 array('class' => 'hubdescription'));
2173 $hubdescription = html_writer::tag('div', $hubdescription, array('class' => 'hubinfo clearfix'));
2175 return $hubdescription;
2179 * Output frontpage summary text and frontpage modules (stored as section 1 in site course)
2181 * This may be disabled in settings
2183 * @return string
2185 public function frontpage_section1() {
2186 global $SITE, $USER;
2188 $output = '';
2189 $editing = $this->page->user_is_editing();
2191 if ($editing) {
2192 // Make sure section with number 1 exists.
2193 course_create_sections_if_missing($SITE, 1);
2196 $modinfo = get_fast_modinfo($SITE);
2197 $section = $modinfo->get_section_info(1);
2200 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
2202 $format = course_get_format($SITE);
2203 $frontpageclass = $format->get_output_classname('content\\frontpagesection');
2204 $frontpagesection = new $frontpageclass($format, $section);
2206 // The course outputs works with format renderers, not with course renderers.
2207 $renderer = $format->get_renderer($this->page);
2208 $output .= $renderer->render($frontpagesection);
2211 return $output;
2215 * Output news for the frontpage (extract from site-wide news forum)
2217 * @param stdClass $forum record from db table 'forum' that represents the site news forum
2218 * @return string
2220 protected function frontpage_news($forum) {
2221 global $CFG, $SITE, $SESSION, $USER;
2222 require_once($CFG->dirroot .'/mod/forum/lib.php');
2224 $output = '';
2226 if (isloggedin()) {
2227 $SESSION->fromdiscussion = $CFG->wwwroot;
2228 $subtext = '';
2229 if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) {
2230 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
2231 $subtext = get_string('unsubscribe', 'forum');
2233 } else {
2234 $subtext = get_string('subscribe', 'forum');
2236 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey()));
2237 $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
2240 $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
2241 $context = context_module::instance($coursemodule->id);
2243 $entityfactory = mod_forum\local\container::get_entity_factory();
2244 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE);
2246 $rendererfactory = mod_forum\local\container::get_renderer_factory();
2247 $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity);
2248 $cm = \cm_info::create($coursemodule);
2249 return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems);
2253 * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
2255 * @param string $skipdivid
2256 * @param string $contentsdivid
2257 * @param string $header Header of the part
2258 * @param string $contents Contents of the part
2259 * @return string
2261 protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
2262 if (strval($contents) === '') {
2263 return '';
2265 $output = html_writer::link('#' . $skipdivid,
2266 get_string('skipa', 'access', core_text::strtolower(strip_tags($header))),
2267 array('class' => 'skip-block skip aabtn'));
2269 // Wrap frontpage part in div container.
2270 $output .= html_writer::start_tag('div', array('id' => $contentsdivid));
2271 $output .= $this->heading($header);
2273 $output .= $contents;
2275 // End frontpage part div container.
2276 $output .= html_writer::end_tag('div');
2278 $output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
2279 return $output;
2283 * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
2285 * @return string
2287 public function frontpage() {
2288 global $CFG, $SITE;
2290 $output = '';
2292 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
2293 $frontpagelayout = $CFG->frontpageloggedin;
2294 } else {
2295 $frontpagelayout = $CFG->frontpage;
2298 foreach (explode(',', $frontpagelayout) as $v) {
2299 switch ($v) {
2300 // Display the main part of the front page.
2301 case FRONTPAGENEWS:
2302 if ($SITE->newsitems) {
2303 // Print forums only when needed.
2304 require_once($CFG->dirroot .'/mod/forum/lib.php');
2305 if (($newsforum = forum_get_course_forum($SITE->id, 'news')) &&
2306 ($forumcontents = $this->frontpage_news($newsforum))) {
2307 $newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id];
2308 $output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
2309 $newsforumcm->get_formatted_name(), $forumcontents);
2312 break;
2314 case FRONTPAGEENROLLEDCOURSELIST:
2315 $mycourseshtml = $this->frontpage_my_courses();
2316 if (!empty($mycourseshtml)) {
2317 $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
2318 get_string('mycourses'), $mycourseshtml);
2320 break;
2322 case FRONTPAGEALLCOURSELIST:
2323 $availablecourseshtml = $this->frontpage_available_courses();
2324 $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
2325 get_string('availablecourses'), $availablecourseshtml);
2326 break;
2328 case FRONTPAGECATEGORYNAMES:
2329 $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
2330 get_string('categories'), $this->frontpage_categories_list());
2331 break;
2333 case FRONTPAGECATEGORYCOMBO:
2334 $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
2335 get_string('courses'), $this->frontpage_combo_list());
2336 break;
2338 case FRONTPAGECOURSESEARCH:
2339 $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
2340 break;
2343 $output .= '<br />';
2346 return $output;
2351 * Class storing display options and functions to help display course category and/or courses lists
2353 * This is a wrapper for core_course_category objects that also stores display options
2354 * and functions to retrieve sorted and paginated lists of categories/courses.
2356 * If theme overrides methods in core_course_renderers that access this class
2357 * it may as well not use this class at all or extend it.
2359 * @package core
2360 * @copyright 2013 Marina Glancy
2361 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2363 class coursecat_helper {
2364 /** @var string [none, collapsed, expanded] how (if) display courses list */
2365 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
2366 /** @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) */
2367 protected $subcatdepth = 1;
2368 /** @var array options to display courses list */
2369 protected $coursesdisplayoptions = array();
2370 /** @var array options to display subcategories list */
2371 protected $categoriesdisplayoptions = array();
2372 /** @var array additional HTML attributes */
2373 protected $attributes = array();
2374 /** @var array search criteria if the list is a search result */
2375 protected $searchcriteria = null;
2378 * Sets how (if) to show the courses - none, collapsed, expanded, etc.
2380 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
2381 * @return coursecat_helper
2383 public function set_show_courses($showcourses) {
2384 $this->showcourses = $showcourses;
2385 // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses()
2386 // and core_course_category::search_courses().
2387 $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
2388 $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
2389 $this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED;
2390 return $this;
2394 * Returns how (if) to show the courses - none, collapsed, expanded, etc.
2396 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
2398 public function get_show_courses() {
2399 return $this->showcourses;
2403 * Sets the maximum depth to expand subcategories in the tree
2405 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2407 * @param int $subcatdepth
2408 * @return coursecat_helper
2410 public function set_subcat_depth($subcatdepth) {
2411 $this->subcatdepth = $subcatdepth;
2412 return $this;
2416 * Returns the maximum depth to expand subcategories in the tree
2418 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2420 * @return int
2422 public function get_subcat_depth() {
2423 return $this->subcatdepth;
2427 * Sets options to display list of courses
2429 * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses()
2431 * Options that core_course_category::get_courses() accept:
2432 * - recursive - return courses from subcategories as well. Use with care,
2433 * this may be a huge list!
2434 * - summary - preloads fields 'summary' and 'summaryformat'
2435 * - coursecontacts - preloads course contacts
2436 * - customfields - preloads custom fields data
2437 * - isenrolled - preloads indication whether this user is enrolled in the course
2438 * - sort - list of fields to sort. Example
2439 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
2440 * will sort by idnumber asc, shortname asc and id desc.
2441 * Default: array('sortorder' => 1)
2442 * Only cached fields may be used for sorting!
2443 * - offset
2444 * - limit - maximum number of children to return, 0 or null for no limit
2446 * Options summary and coursecontacts are filled automatically in the set_show_courses()
2448 * Also renderer can set here any additional options it wants to pass between renderer functions.
2450 * @param array $options
2451 * @return coursecat_helper
2453 public function set_courses_display_options($options) {
2454 $this->coursesdisplayoptions = $options;
2455 $this->set_show_courses($this->showcourses); // this will calculate special display options
2456 return $this;
2460 * Sets one option to display list of courses
2462 * @see coursecat_helper::set_courses_display_options()
2464 * @param string $key
2465 * @param mixed $value
2466 * @return coursecat_helper
2468 public function set_courses_display_option($key, $value) {
2469 $this->coursesdisplayoptions[$key] = $value;
2470 return $this;
2474 * Return the specified option to display list of courses
2476 * @param string $optionname option name
2477 * @param mixed $defaultvalue default value for option if it is not specified
2478 * @return mixed
2480 public function get_courses_display_option($optionname, $defaultvalue = null) {
2481 if (array_key_exists($optionname, $this->coursesdisplayoptions)) {
2482 return $this->coursesdisplayoptions[$optionname];
2483 } else {
2484 return $defaultvalue;
2489 * Returns all options to display the courses
2491 * This array is usually passed to {@link core_course_category::get_courses()} or
2492 * {@link core_course_category::search_courses()}
2494 * @return array
2496 public function get_courses_display_options() {
2497 return $this->coursesdisplayoptions;
2501 * Sets options to display list of subcategories
2503 * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children().
2504 * Any other options may be used by renderer functions
2506 * @param array $options
2507 * @return coursecat_helper
2509 public function set_categories_display_options($options) {
2510 $this->categoriesdisplayoptions = $options;
2511 return $this;
2515 * Return the specified option to display list of subcategories
2517 * @param string $optionname option name
2518 * @param mixed $defaultvalue default value for option if it is not specified
2519 * @return mixed
2521 public function get_categories_display_option($optionname, $defaultvalue = null) {
2522 if (array_key_exists($optionname, $this->categoriesdisplayoptions)) {
2523 return $this->categoriesdisplayoptions[$optionname];
2524 } else {
2525 return $defaultvalue;
2530 * Returns all options to display list of subcategories
2532 * This array is usually passed to {@link core_course_category::get_children()}
2534 * @return array
2536 public function get_categories_display_options() {
2537 return $this->categoriesdisplayoptions;
2541 * Sets additional general options to pass between renderer functions, usually HTML attributes
2543 * @param array $attributes
2544 * @return coursecat_helper
2546 public function set_attributes($attributes) {
2547 $this->attributes = $attributes;
2548 return $this;
2552 * Return all attributes and erases them so they are not applied again
2554 * @param string $classname adds additional class name to the beginning of $attributes['class']
2555 * @return array
2557 public function get_and_erase_attributes($classname) {
2558 $attributes = $this->attributes;
2559 $this->attributes = array();
2560 if (empty($attributes['class'])) {
2561 $attributes['class'] = '';
2563 $attributes['class'] = $classname . ' '. $attributes['class'];
2564 return $attributes;
2568 * Sets the search criteria if the course is a search result
2570 * Search string will be used to highlight terms in course name and description
2572 * @param array $searchcriteria
2573 * @return coursecat_helper
2575 public function set_search_criteria($searchcriteria) {
2576 $this->searchcriteria = $searchcriteria;
2577 return $this;
2581 * Returns formatted and filtered description of the given category
2583 * @param core_course_category $coursecat category
2584 * @param stdClass|array $options format options, by default [noclean,overflowdiv],
2585 * if context is not specified it will be added automatically
2586 * @return string|null
2588 public function get_category_formatted_description($coursecat, $options = null) {
2589 if ($coursecat->id && $coursecat->is_uservisible() && !empty($coursecat->description)) {
2590 if (!isset($coursecat->descriptionformat)) {
2591 $descriptionformat = FORMAT_MOODLE;
2592 } else {
2593 $descriptionformat = $coursecat->descriptionformat;
2595 if ($options === null) {
2596 $options = array('noclean' => true, 'overflowdiv' => true);
2597 } else {
2598 $options = (array)$options;
2600 $context = context_coursecat::instance($coursecat->id);
2601 if (!isset($options['context'])) {
2602 $options['context'] = $context;
2604 $text = file_rewrite_pluginfile_urls($coursecat->description,
2605 'pluginfile.php', $context->id, 'coursecat', 'description', null);
2606 return format_text($text, $descriptionformat, $options);
2608 return null;
2612 * Returns given course's summary with proper embedded files urls and formatted
2614 * @param core_course_list_element $course
2615 * @param array|stdClass $options additional formatting options
2616 * @return string
2618 public function get_course_formatted_summary($course, $options = array()) {
2619 global $CFG;
2620 require_once($CFG->libdir. '/filelib.php');
2621 if (!$course->has_summary()) {
2622 return '';
2624 $options = (array)$options;
2625 $context = context_course::instance($course->id);
2626 if (!isset($options['context'])) {
2627 // TODO see MDL-38521
2628 // option 1 (current), page context - no code required
2629 // option 2, system context
2630 // $options['context'] = context_system::instance();
2631 // option 3, course context:
2632 // $options['context'] = $context;
2633 // option 4, course category context:
2634 // $options['context'] = $context->get_parent_context();
2636 $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null);
2637 $summary = format_text($summary, $course->summaryformat, $options, $course->id);
2638 if (!empty($this->searchcriteria['search'])) {
2639 $summary = highlight($this->searchcriteria['search'], $summary);
2641 return $summary;
2645 * Returns course name as it is configured to appear in courses lists formatted to course context
2647 * @param core_course_list_element $course
2648 * @param array|stdClass $options additional formatting options
2649 * @return string
2651 public function get_course_formatted_name($course, $options = array()) {
2652 $options = (array)$options;
2653 if (!isset($options['context'])) {
2654 $options['context'] = context_course::instance($course->id);
2656 $name = format_string(get_course_display_name_for_list($course), true, $options);
2657 if (!empty($this->searchcriteria['search'])) {
2658 $name = highlight($this->searchcriteria['search'], $name);
2660 return $name;