Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

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