Merge branch 'MDL-73493' of git://github.com/paulholden/moodle
[moodle.git] / course / renderer.php
blob699eb94764cdfed429d6b103aef76df442460ce3
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Renderer for use with the course section and all the goodness that falls
20 * within it.
22 * This renderer should contain methods useful to courses, and categories.
24 * @package moodlecore
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 /**
30 * The core course renderer
32 * Can be retrieved with the following:
33 * $renderer = $PAGE->get_renderer('core','course');
35 class core_course_renderer extends plugin_renderer_base {
36 const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */
37 const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */
38 const COURSECAT_SHOW_COURSES_COLLAPSED = 10;
39 const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */
40 const COURSECAT_SHOW_COURSES_EXPANDED = 20;
41 const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
43 const COURSECAT_TYPE_CATEGORY = 0;
44 const COURSECAT_TYPE_COURSE = 1;
46 /**
47 * A cache of strings
48 * @var stdClass
50 protected $strings;
52 /**
53 * Whether a category content is being initially rendered with children. This is mainly used by the
54 * core_course_renderer::corsecat_tree() to render the appropriate action for the Expand/Collapse all link on
55 * page load.
56 * @var bool
58 protected $categoryexpandedonload = false;
60 /**
61 * Override the constructor so that we can initialise the string cache
63 * @param moodle_page $page
64 * @param string $target
66 public function __construct(moodle_page $page, $target) {
67 $this->strings = new stdClass;
68 $courseid = $page->course->id;
69 user_preference_allow_ajax_update('coursesectionspreferences_' . $courseid, PARAM_RAW);
70 parent::__construct($page, $target);
73 /**
74 * @deprecated since 3.2
76 protected function add_modchoosertoggle() {
77 throw new coding_exception('core_course_renderer::add_modchoosertoggle() can not be used anymore.');
80 /**
81 * Renders course info box.
83 * @param stdClass $course
84 * @return string
86 public function course_info_box(stdClass $course) {
87 $content = '';
88 $content .= $this->output->box_start('generalbox info');
89 $chelper = new coursecat_helper();
90 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
91 $content .= $this->coursecat_coursebox($chelper, $course);
92 $content .= $this->output->box_end();
93 return $content;
96 /**
97 * Renderers a structured array of courses and categories into a nice XHTML tree structure.
99 * @deprecated since 2.5
101 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
103 * @param array $ignored argument ignored
104 * @return string
106 public final function course_category_tree(array $ignored) {
107 debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER);
108 return $this->frontpage_combo_list();
112 * Renderers a category for use with course_category_tree
114 * @deprecated since 2.5
116 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
118 * @param array $category
119 * @param int $depth
120 * @return string
122 protected final function course_category_tree_category(stdClass $category, $depth=1) {
123 debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER);
124 return '';
128 * Render a modchooser.
130 * @param renderable $modchooser The chooser.
131 * @return string
133 public function render_modchooser(renderable $modchooser) {
134 return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this));
138 * Build the HTML for the module chooser javascript popup
140 * @param array $modules A set of modules as returned form @see
141 * get_module_metadata
142 * @param object $course The course that will be displayed
143 * @return string The composed HTML for the module
145 public function course_modchooser($modules, $course) {
146 debugging('course_modchooser() is deprecated. Please use course_activitychooser() instead.', DEBUG_DEVELOPER);
148 return $this->course_activitychooser($course->id);
152 * Build the HTML for the module chooser javascript popup.
154 * @param int $courseid The course id to fetch modules for.
155 * @return string
157 public function course_activitychooser($courseid) {
159 if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) {
160 return '';
163 // Build an object of config settings that we can then hook into in the Activity Chooser.
164 $chooserconfig = (object) [
165 'tabmode' => get_config('core', 'activitychoosertabmode'),
167 $this->page->requires->js_call_amd('core_course/activitychooser', 'init', [$courseid, $chooserconfig]);
169 return '';
173 * Build the HTML for a specified set of modules
175 * @param array $modules A set of modules as used by the
176 * course_modchooser_module function
177 * @return string The composed HTML for the module
179 protected function course_modchooser_module_types($modules) {
180 debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' .
181 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
182 return '';
186 * Return the HTML for the specified module adding any required classes
188 * @param object $module An object containing the title, and link. An
189 * icon, and help text may optionally be specified. If the module
190 * contains subtypes in the types option, then these will also be
191 * displayed.
192 * @param array $classes Additional classes to add to the encompassing
193 * div element
194 * @return string The composed HTML for the module
196 protected function course_modchooser_module($module, $classes = array('option')) {
197 debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' .
198 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
199 return '';
202 protected function course_modchooser_title($title, $identifier = null) {
203 debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' .
204 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER);
205 return '';
209 * Renders HTML for displaying the sequence of course module editing buttons
211 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
213 * @see course_get_cm_edit_actions()
215 * @param action_link[] $actions Array of action_link objects
216 * @param cm_info $mod The module we are displaying actions for.
217 * @param array $displayoptions additional display options:
218 * ownerselector => A JS/CSS selector that can be used to find an cm node.
219 * If specified the owning node will be given the class 'action-menu-shown' when the action
220 * menu is being displayed.
221 * constraintselector => A JS/CSS selector that can be used to find the parent node for which to constrain
222 * the action menu to when it is being displayed.
223 * donotenhance => If set to true the action menu that gets displayed won't be enhanced by JS.
224 * @return string
226 public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) {
227 global $CFG;
229 debugging(
230 'course_section_cm_edit_actions is deprecated. Use core_courseformat\\output\\local\\content\\cm\\controlmenu instead.',
231 DEBUG_DEVELOPER
234 if (empty($actions)) {
235 return '';
238 if (isset($displayoptions['ownerselector'])) {
239 $ownerselector = $displayoptions['ownerselector'];
240 } else if ($mod) {
241 $ownerselector = '#module-'.$mod->id;
242 } else {
243 debugging('You should upgrade your call to '.__FUNCTION__.' and provide $mod', DEBUG_DEVELOPER);
244 $ownerselector = 'li.activity';
247 if (isset($displayoptions['constraintselector'])) {
248 $constraint = $displayoptions['constraintselector'];
249 } else {
250 $constraint = '.course-content';
253 $menu = new action_menu();
254 $menu->set_owner_selector($ownerselector);
255 $menu->set_constraint($constraint);
256 $menu->set_alignment(action_menu::TR, action_menu::BR);
257 $menu->set_menu_trigger(get_string('edit'));
259 foreach ($actions as $action) {
260 if ($action instanceof action_menu_link) {
261 $action->add_class('cm-edit-action');
263 $menu->add($action);
265 $menu->attributes['class'] .= ' section-cm-edit-actions commands';
267 // Prioritise the menu ahead of all other actions.
268 $menu->prioritise = true;
270 return $this->render($menu);
274 * Renders HTML for the menus to add activities and resources to the current course
276 * Renders the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback.
278 * @param stdClass $course
279 * @param int $section relative section number (field course_sections.section)
280 * @param int $sectionreturn The section to link back to
281 * @param array $displayoptions additional display options, for example blocks add
282 * option 'inblock' => true, suggesting to display controls vertically
283 * @return string
285 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
286 // Check to see if user can add menus.
287 if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
288 || !$this->page->user_is_editing()) {
289 return '';
292 $data = [
293 'sectionid' => $section,
294 'sectionreturn' => $sectionreturn
296 $ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
298 // Load the JS for the modal.
299 $this->course_activitychooser($course->id);
301 return $ajaxcontrol;
305 * Render the deprecated nonajax activity chooser.
307 * @deprecated since Moodle 3.11
309 * @todo MDL-71331 deprecate this function
310 * @param stdClass $course the course object
311 * @param int $section relative section number (field course_sections.section)
312 * @param int $sectionreturn The section to link back to
313 * @param array $displayoptions additional display options, for example blocks add
314 * option 'inblock' => true, suggesting to display controls vertically
315 * @return string
317 private function course_section_add_cm_control_nonajax($course, $section, $sectionreturn = null,
318 $displayoptions = array()): string {
319 global $USER;
321 $vertical = !empty($displayoptions['inblock']);
323 // Check to see if user can add menus.
324 if (
325 !has_capability('moodle/course:manageactivities', context_course::instance($course->id))
326 || !$this->page->user_is_editing()
328 return '';
331 debugging('non-js dropdowns are deprecated.', DEBUG_DEVELOPER);
332 // Retrieve all modules with associated metadata.
333 $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
334 $urlparams = ['section' => $section];
335 if (!is_null($sectionreturn)) {
336 $urlparams['sr'] = $sectionreturn;
338 $modules = $contentitemservice->get_content_items_for_user_in_course($USER, $course, $urlparams);
340 // Return if there are no content items to add.
341 if (empty($modules)) {
342 return '';
345 // We'll sort resources and activities into two lists.
346 $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
348 foreach ($modules as $module) {
349 $activityclass = MOD_CLASS_ACTIVITY;
350 if ($module->archetype == MOD_ARCHETYPE_RESOURCE) {
351 $activityclass = MOD_CLASS_RESOURCE;
352 } else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) {
353 // System modules cannot be added by user, do not add to dropdown.
354 continue;
356 $link = $module->link;
357 $activities[$activityclass][$link] = $module->title;
360 $straddactivity = get_string('addactivity');
361 $straddresource = get_string('addresource');
362 $sectionname = get_section_name($course, $section);
363 $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
364 $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
366 $nonajaxcontrol = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-'
367 . $section));
369 if (!$vertical) {
370 $nonajaxcontrol .= html_writer::start_tag('div', array('class' => 'horizontal'));
373 if (!empty($activities[MOD_CLASS_RESOURCE])) {
374 $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array('' => $straddresource), "ressection$section");
375 $select->set_help_icon('resources');
376 $select->set_label($strresourcelabel, array('class' => 'accesshide'));
377 $nonajaxcontrol .= $this->output->render($select);
380 if (!empty($activities[MOD_CLASS_ACTIVITY])) {
381 $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array('' => $straddactivity), "section$section");
382 $select->set_help_icon('activities');
383 $select->set_label($stractivitylabel, array('class' => 'accesshide'));
384 $nonajaxcontrol .= $this->output->render($select);
387 if (!$vertical) {
388 $nonajaxcontrol .= html_writer::end_tag('div');
391 $nonajaxcontrol .= html_writer::end_tag('div');
393 return $nonajaxcontrol;
397 * Renders html to display a course search form
399 * @param string $value default value to populate the search field
400 * @return string
402 public function course_search_form($value = '') {
404 $data = [
405 'action' => \core_search\manager::get_course_search_url(),
406 'btnclass' => 'btn-primary',
407 'inputname' => 'q',
408 'searchstring' => get_string('searchcourses'),
409 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
410 'query' => $value
412 return $this->render_from_template('core/search_input', $data);
416 * Renders html for completion box on course page
418 * If completion is disabled, returns empty string
419 * If completion is automatic, returns an icon of the current completion state
420 * If completion is manual, returns a form (with an icon inside) that allows user to
421 * toggle completion
423 * @deprecated since Moodle 3.11
424 * @todo MDL-71183 Final deprecation in Moodle 4.3.
425 * @see \core_renderer::activity_information
427 * @param stdClass $course course object
428 * @param completion_info $completioninfo completion info for the course, it is recommended
429 * to fetch once for all modules in course/section for performance
430 * @param cm_info $mod module to show completion for
431 * @param array $displayoptions display options, not used in core
432 * @return string
434 public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) {
435 global $CFG, $DB, $USER;
437 debugging(__FUNCTION__ . ' is deprecated and is being replaced by the activity_information output component.',
438 DEBUG_DEVELOPER);
440 $output = '';
442 $istrackeduser = $completioninfo->is_tracked_user($USER->id);
443 $isediting = $this->page->user_is_editing();
445 if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
446 return $output;
448 if ($completioninfo === null) {
449 $completioninfo = new completion_info($course);
451 $completion = $completioninfo->is_enabled($mod);
453 if ($completion == COMPLETION_TRACKING_NONE) {
454 if ($isediting) {
455 $output .= html_writer::span('&nbsp;', 'filler');
457 return $output;
460 $completionicon = '';
462 if ($isediting || !$istrackeduser) {
463 switch ($completion) {
464 case COMPLETION_TRACKING_MANUAL :
465 $completionicon = 'manual-enabled'; break;
466 case COMPLETION_TRACKING_AUTOMATIC :
467 $completionicon = 'auto-enabled'; break;
469 } else {
470 $completiondata = $completioninfo->get_data($mod, true);
471 if ($completion == COMPLETION_TRACKING_MANUAL) {
472 switch($completiondata->completionstate) {
473 case COMPLETION_INCOMPLETE:
474 $completionicon = 'manual-n' . ($completiondata->overrideby ? '-override' : '');
475 break;
476 case COMPLETION_COMPLETE:
477 $completionicon = 'manual-y' . ($completiondata->overrideby ? '-override' : '');
478 break;
480 } else { // Automatic
481 switch($completiondata->completionstate) {
482 case COMPLETION_INCOMPLETE:
483 $completionicon = 'auto-n' . ($completiondata->overrideby ? '-override' : '');
484 break;
485 case COMPLETION_COMPLETE:
486 $completionicon = 'auto-y' . ($completiondata->overrideby ? '-override' : '');
487 break;
488 case COMPLETION_COMPLETE_PASS:
489 $completionicon = 'auto-pass'; break;
490 case COMPLETION_COMPLETE_FAIL:
491 $completionicon = 'auto-fail'; break;
495 if ($completionicon) {
496 $formattedname = html_entity_decode($mod->get_formatted_name(), ENT_QUOTES, 'UTF-8');
497 if (!$isediting && $istrackeduser && $completiondata->overrideby) {
498 $args = new stdClass();
499 $args->modname = $formattedname;
500 $overridebyuser = \core_user::get_user($completiondata->overrideby, '*', MUST_EXIST);
501 $args->overrideuser = fullname($overridebyuser);
502 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $args);
503 } else {
504 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
507 if ($isediting || !$istrackeduser || !has_capability('moodle/course:togglecompletion', $mod->context)) {
508 // When editing, the icon is just an image.
509 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
510 array('title' => $imgalt, 'class' => 'iconsmall'));
511 $output .= html_writer::tag('span', $this->output->render($completionpixicon),
512 array('class' => 'autocompletion'));
513 } else if ($completion == COMPLETION_TRACKING_MANUAL) {
514 $newstate =
515 $completiondata->completionstate == COMPLETION_COMPLETE
516 ? COMPLETION_INCOMPLETE
517 : COMPLETION_COMPLETE;
518 // In manual mode the icon is a toggle form...
520 // If this completion state is used by the
521 // conditional activities system, we need to turn
522 // off the JS.
523 $extraclass = '';
524 if (!empty($CFG->enableavailability) &&
525 core_availability\info::completion_value_used($course, $mod->id)) {
526 $extraclass = ' preventjs';
528 $output .= html_writer::start_tag('form', array('method' => 'post',
529 'action' => new moodle_url('/course/togglecompletion.php'),
530 'class' => 'togglecompletion'. $extraclass));
531 $output .= html_writer::start_tag('div');
532 $output .= html_writer::empty_tag('input', array(
533 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
534 $output .= html_writer::empty_tag('input', array(
535 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
536 $output .= html_writer::empty_tag('input', array(
537 'type' => 'hidden', 'name' => 'modulename', 'value' => $formattedname));
538 $output .= html_writer::empty_tag('input', array(
539 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
540 $output .= html_writer::tag('button',
541 $this->output->pix_icon('i/completion-' . $completionicon, $imgalt),
542 array('class' => 'btn btn-link', 'aria-live' => 'assertive'));
543 $output .= html_writer::end_tag('div');
544 $output .= html_writer::end_tag('form');
545 } else {
546 // In auto mode, the icon is just an image.
547 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
548 array('title' => $imgalt));
549 $output .= html_writer::tag('span', $this->output->render($completionpixicon),
550 array('class' => 'autocompletion'));
553 return $output;
557 * Checks if course module has any conditions that may make it unavailable for
558 * all or some of the students
560 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
562 * @param cm_info $mod
563 * @return bool
565 public function is_cm_conditionally_hidden(cm_info $mod) {
566 global $CFG;
568 debugging(
569 'is_cm_conditionally_hidden is deprecated. Use \core_availability\info_module::is_available_for_all instead',
570 DEBUG_DEVELOPER
573 $conditionalhidden = false;
574 if (!empty($CFG->enableavailability)) {
575 $info = new \core_availability\info_module($mod);
576 $conditionalhidden = !$info->is_available_for_all();
578 return $conditionalhidden;
582 * Renders html to display a name with the link to the course module on a course page
584 * If module is unavailable for user but still needs to be displayed
585 * in the list, just the name is returned without a link
587 * Note, that for course modules that never have separate pages (i.e. labels)
588 * this function return an empty string
590 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
592 * @param cm_info $mod
593 * @param array $displayoptions
594 * @return string
596 public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
597 debugging(
598 'course_section_cm_name is deprecated. Use core_courseformat\\output\\local\\content\\cm\\cmname class instead.',
599 DEBUG_DEVELOPER
602 if (!$mod->is_visible_on_course_page() || !$mod->url) {
603 // Nothing to be displayed to the user.
604 return '';
607 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
608 $groupinglabel = $mod->get_grouping_label($textclasses);
610 // Render element that allows to edit activity name inline.
611 $format = course_get_format($mod->course);
612 $cmnameclass = $format->get_output_classname('content\\cm\\cmname');
613 // Mod inplace name editable.
614 $cmname = new $cmnameclass(
615 $format,
616 $mod->get_section_info(),
617 $mod,
618 $this->page->user_is_editing(),
619 $displayoptions
622 $data = $cmname->export_for_template($this->output);
624 return $this->output->render_from_template('core/inplace_editable', $data) .
625 $groupinglabel;
629 * Returns the CSS classes for the activity name/content
631 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
633 * For items which are hidden, unavailable or stealth but should be displayed
634 * to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
635 * Students will also see as dimmed activities names that are not yet available
636 * but should still be displayed (without link) with availability info.
638 * @param cm_info $mod
639 * @return array array of two elements ($linkclasses, $textclasses)
641 protected function course_section_cm_classes(cm_info $mod) {
643 debugging(
644 'course_section_cm_classes is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
645 DEBUG_DEVELOPER
648 $format = course_get_format($mod->course);
650 $cmclass = $format->get_output_classname('content\\cm');
651 $cmoutput = new $cmclass(
652 $format,
653 $mod->get_section_info(),
654 $mod,
656 return [
657 $cmoutput->get_link_classes(),
658 $cmoutput->get_text_classes(),
663 * Renders html to display a name with the link to the course module on a course page
665 * If module is unavailable for user but still needs to be displayed
666 * in the list, just the name is returned without a link
668 * Note, that for course modules that never have separate pages (i.e. labels)
669 * this function return an empty string
671 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
673 * @param cm_info $mod
674 * @param array $displayoptions
675 * @return string
677 public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) {
679 debugging(
680 'course_section_cm_name_title is deprecated. Use core_courseformat\\output\\local\\cm\\title class instead.',
681 DEBUG_DEVELOPER
684 $output = '';
685 $url = $mod->url;
686 if (!$mod->is_visible_on_course_page() || !$url) {
687 // Nothing to be displayed to the user.
688 return $output;
691 //Accessibility: for files get description via icon, this is very ugly hack!
692 $instancename = $mod->get_formatted_name();
693 $altname = $mod->modfullname;
694 // Avoid unnecessary duplication: if e.g. a forum name already
695 // includes the word forum (or Forum, etc) then it is unhelpful
696 // to include that in the accessible description that is added.
697 if (false !== strpos(core_text::strtolower($instancename),
698 core_text::strtolower($altname))) {
699 $altname = '';
701 // File type after name, for alphabetic lists (screen reader).
702 if ($altname) {
703 $altname = get_accesshide(' '.$altname);
706 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
708 // Get on-click attribute value if specified and decode the onclick - it
709 // has already been encoded for display (puke).
710 $onclick = htmlspecialchars_decode($mod->onclick, ENT_QUOTES);
712 // Display link itself.
713 $instancename = html_writer::tag('span', $instancename . $altname, ['class' => 'instancename ml-1']);
715 $imageicon = html_writer::empty_tag('img', ['src' => $mod->get_icon_url(),
716 'class' => 'activityicon', 'alt' => '', 'role' => 'presentation', 'aria-hidden' => 'true']);
717 $imageicon = html_writer::tag('span', $imageicon, ['class' => 'activityiconcontainer courseicon']);
718 $activitylink = $imageicon . $instancename;
720 if ($mod->uservisible) {
721 $output .= html_writer::link($url, $activitylink, array('class' => 'aalink' . $linkclasses, 'onclick' => $onclick));
722 } else {
723 // We may be displaying this just in order to show information
724 // about visibility, without the actual link ($mod->is_visible_on_course_page()).
725 $output .= html_writer::tag('div', $activitylink, array('class' => $textclasses));
727 return $output;
731 * Renders html to display the module content on the course page (i.e. text of the labels)
733 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
735 * @param cm_info $mod
736 * @param array $displayoptions
737 * @return string
739 public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
741 debugging(
742 'course_section_cm_text is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
743 DEBUG_DEVELOPER
746 $output = '';
747 if (!$mod->is_visible_on_course_page()) {
748 // nothing to be displayed to the user
749 return $output;
751 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
752 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
753 if ($mod->url && $mod->uservisible) {
754 if ($content) {
755 // If specified, display extra content after link.
756 $output = html_writer::tag('div', $content, array('class' =>
757 trim('contentafterlink ' . $textclasses)));
759 } else {
760 $groupinglabel = $mod->get_grouping_label($textclasses);
762 // No link, so display only content.
763 $output = html_writer::tag('div', $content . $groupinglabel,
764 array('class' => 'contentwithoutlink ' . $textclasses));
766 return $output;
770 * Displays availability info for a course section or course module
772 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
773 * @param string $text
774 * @param string $additionalclasses
775 * @return string
777 public function availability_info($text, $additionalclasses = '') {
779 debugging(
780 'availability_info is deprecated. Use core_courseformat\\output\\local\\content\\section\\availability instead',
781 DEBUG_DEVELOPER
784 $data = ['text' => $text, 'classes' => $additionalclasses];
785 $additionalclasses = array_filter(explode(' ', $additionalclasses));
787 if (in_array('ishidden', $additionalclasses)) {
788 $data['ishidden'] = 1;
790 } else if (in_array('isstealth', $additionalclasses)) {
791 $data['isstealth'] = 1;
793 } else if (in_array('isrestricted', $additionalclasses)) {
794 $data['isrestricted'] = 1;
796 if (in_array('isfullinfo', $additionalclasses)) {
797 $data['isfullinfo'] = 1;
801 return $this->render_from_template('core/availability_info', $data);
805 * Renders HTML to show course module availability information (for someone who isn't allowed
806 * to see the activity itself, or for staff)
808 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
809 * @param cm_info $mod
810 * @param array $displayoptions
811 * @return string
813 public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
815 debugging(
816 'course_section_cm_availability is deprecated. Use core_courseformat\\output\\local\\content\\cm\\availability instead',
817 DEBUG_DEVELOPER
820 $format = course_get_format($mod->course);
822 $availabilityclass = $format->get_output_classname('content\\cm\\availability');
823 $availability = new $availabilityclass(
824 $format,
825 $mod->get_section_info(),
826 $mod,
829 $renderer = $format->get_renderer($this->page);
830 $data = $availability->export_for_template($renderer);
831 return $data->info ?? '';
835 * Renders HTML to display one course module for display within a section.
837 * @deprecated since 4.0 - use core_course output components or course_format::course_section_updated_cm_item instead.
839 * This function calls:
840 * {@link core_course_renderer::course_section_cm()}
842 * @param stdClass $course
843 * @param completion_info $completioninfo
844 * @param cm_info $mod
845 * @param int|null $sectionreturn
846 * @param array $displayoptions
847 * @return String
849 public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
851 debugging(
852 'course_section_cm_list_item is deprecated. Use renderer course_section_updated_cm_item instead',
853 DEBUG_DEVELOPER
856 $output = '';
857 if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
858 $modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses;
859 $output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id));
861 return $output;
865 * Renders HTML to display one course module in a course section
867 * This includes link, content, availability, completion info and additional information
868 * that module type wants to display (i.e. number of unread forum posts)
870 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
872 * @param stdClass $course
873 * @param completion_info $completioninfo
874 * @param cm_info $mod
875 * @param int|null $sectionreturn
876 * @param array $displayoptions
877 * @return string
879 public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = []) {
881 debugging(
882 'course_section_cm is deprecated. Use core_courseformat\\output\\content\\cm output class instead.',
883 DEBUG_DEVELOPER
886 if (!$mod->is_visible_on_course_page()) {
887 return '';
890 $format = course_get_format($course);
891 $modinfo = $format->get_modinfo();
892 // Output renderers works only with real section_info objects.
893 if ($sectionreturn) {
894 $format->set_section_number($sectionreturn);
896 $section = $modinfo->get_section_info($format->get_section_number());
898 $cmclass = $format->get_output_classname('content\\cm');
899 $cm = new $cmclass($format, $section, $mod, $displayoptions);
900 // The course outputs works with format renderers, not with course renderers.
901 $renderer = $format->get_renderer($this->page);
902 $data = $cm->export_for_template($renderer);
903 return $this->output->render_from_template('core_courseformat/local/content/cm', $data);
907 * Message displayed to the user when they try to access unavailable activity following URL
909 * This method is a very simplified version of {@link course_section_cm()} to be part of the error
910 * notification only. It also does not check if module is visible on course page or not.
912 * The message will be displayed inside notification!
914 * @param cm_info $cm
915 * @return string
917 public function course_section_cm_unavailable_error_message(cm_info $cm) {
918 if ($cm->uservisible) {
919 return null;
921 if (!$cm->availableinfo) {
922 return get_string('activityiscurrentlyhidden');
925 $altname = get_accesshide(' ' . $cm->modfullname);
926 $name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(),
927 'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) .
928 html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
929 $formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course());
930 return html_writer::div($name, 'activityinstance-error') .
931 html_writer::div($formattedinfo, 'availabilityinfo-error');
935 * Renders HTML to display a list of course modules in a course section
936 * Also displays "move here" controls in Javascript-disabled mode.
938 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
940 * This function calls {@link core_course_renderer::course_section_cm()}
942 * @param stdClass $course course object
943 * @param int|stdClass|section_info $section relative section number or section object
944 * @param int $sectionreturn section number to return to
945 * @param int $displayoptions
946 * @return void
948 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = []) {
949 global $USER;
951 debugging('course_section_cm_list is deprecated. Use core_courseformat\\output\\local\\content\\section\\cmlist '.
952 'classes instead.', DEBUG_DEVELOPER);
954 $output = '';
956 $format = course_get_format($course);
957 $modinfo = $format->get_modinfo();
959 if (is_object($section)) {
960 $section = $modinfo->get_section_info($section->section);
961 } else {
962 $section = $modinfo->get_section_info($section);
964 $completioninfo = new completion_info($course);
966 // check if we are currently in the process of moving a module with JavaScript disabled
967 $ismoving = $format->show_editor() && ismoving($course->id);
969 if ($ismoving) {
970 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
973 // Get the list of modules visible to user (excluding the module being moved if there is one)
974 $moduleshtml = [];
975 if (!empty($modinfo->sections[$section->section])) {
976 foreach ($modinfo->sections[$section->section] as $modnumber) {
977 $mod = $modinfo->cms[$modnumber];
979 if ($ismoving and $mod->id == $USER->activitycopy) {
980 // do not display moving mod
981 continue;
984 if ($modulehtml = $this->course_section_cm_list_item($course,
985 $completioninfo, $mod, $sectionreturn, $displayoptions)) {
986 $moduleshtml[$modnumber] = $modulehtml;
991 $sectionoutput = '';
992 if (!empty($moduleshtml) || $ismoving) {
993 foreach ($moduleshtml as $modnumber => $modulehtml) {
994 if ($ismoving) {
995 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey()));
996 $sectionoutput .= html_writer::tag('li',
997 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
998 array('class' => 'movehere'));
1001 $sectionoutput .= $modulehtml;
1004 if ($ismoving) {
1005 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
1006 $sectionoutput .= html_writer::tag('li',
1007 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
1008 array('class' => 'movehere'));
1012 // Always output the section module list.
1013 $output .= html_writer::tag('ul', $sectionoutput, array('class' => 'section img-text'));
1015 return $output;
1019 * Displays a custom list of courses with paging bar if necessary
1021 * If $paginationurl is specified but $totalcount is not, the link 'View more'
1022 * appears under the list.
1024 * If both $paginationurl and $totalcount are specified, and $totalcount is
1025 * bigger than count($courses), a paging bar is displayed above and under the
1026 * courses list.
1028 * @param array $courses array of course records (or instances of core_course_list_element) to show on this page
1029 * @param bool $showcategoryname whether to add category name to the course description
1030 * @param string $additionalclasses additional CSS classes to add to the div.courses
1031 * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
1032 * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
1033 * @param int $page current page number (defaults to 0 referring to the first page)
1034 * @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
1035 * @return string
1037 public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) {
1038 global $CFG;
1039 // create instance of coursecat_helper to pass display options to function rendering courses list
1040 $chelper = new coursecat_helper();
1041 if ($showcategoryname) {
1042 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT);
1043 } else {
1044 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1046 if ($totalcount !== null && $paginationurl !== null) {
1047 // add options to display pagination
1048 if ($perpage === null) {
1049 $perpage = $CFG->coursesperpage;
1051 $chelper->set_courses_display_options(array(
1052 'limit' => $perpage,
1053 'offset' => ((int)$page) * $perpage,
1054 'paginationurl' => $paginationurl,
1056 } else if ($paginationurl !== null) {
1057 // add options to display 'View more' link
1058 $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl));
1059 $totalcount = count($courses) + 1; // has to be bigger than count($courses) otherwise link will not be displayed
1061 $chelper->set_attributes(array('class' => $additionalclasses));
1062 $content = $this->coursecat_courses($chelper, $courses, $totalcount);
1063 return $content;
1067 * Returns HTML to display course name.
1069 * @param coursecat_helper $chelper
1070 * @param core_course_list_element $course
1071 * @return string
1073 protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string {
1074 $content = '';
1075 if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) {
1076 $nametag = 'h3';
1077 } else {
1078 $nametag = 'div';
1080 $coursename = $chelper->get_course_formatted_name($course);
1081 $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]),
1082 $coursename, ['class' => $course->visible ? 'aalink' : 'aalink dimmed']);
1083 $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']);
1084 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
1085 $content .= html_writer::start_tag('div', ['class' => 'moreinfo']);
1086 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1087 if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles()
1088 || $course->has_custom_fields()) {
1089 $url = new moodle_url('/course/info.php', ['id' => $course->id]);
1090 $image = $this->output->pix_icon('i/info', $this->strings->summary);
1091 $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]);
1092 // Make sure JS file to expand course content is included.
1093 $this->coursecat_include_js();
1096 $content .= html_writer::end_tag('div');
1097 return $content;
1101 * Returns HTML to display course enrolment icons.
1103 * @param core_course_list_element $course
1104 * @return string
1106 protected function course_enrolment_icons(core_course_list_element $course): string {
1107 $content = '';
1108 if ($icons = enrol_get_course_info_icons($course)) {
1109 $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']);
1110 foreach ($icons as $icon) {
1111 $content .= $this->render($icon);
1113 $content .= html_writer::end_tag('div');
1115 return $content;
1119 * Displays one course in the list of courses.
1121 * This is an internal function, to display an information about just one course
1122 * please use {@link core_course_renderer::course_info_box()}
1124 * @param coursecat_helper $chelper various display options
1125 * @param core_course_list_element|stdClass $course
1126 * @param string $additionalclasses additional classes to add to the main <div> tag (usually
1127 * depend on the course position in list - first/last/even/odd)
1128 * @return string
1130 protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') {
1131 if (!isset($this->strings->summary)) {
1132 $this->strings->summary = get_string('summary');
1134 if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) {
1135 return '';
1137 if ($course instanceof stdClass) {
1138 $course = new core_course_list_element($course);
1140 $content = '';
1141 $classes = trim('coursebox clearfix '. $additionalclasses);
1142 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1143 $classes .= ' collapsed';
1146 // .coursebox
1147 $content .= html_writer::start_tag('div', array(
1148 'class' => $classes,
1149 'data-courseid' => $course->id,
1150 'data-type' => self::COURSECAT_TYPE_COURSE,
1153 $content .= html_writer::start_tag('div', array('class' => 'info'));
1154 $content .= $this->course_name($chelper, $course);
1155 $content .= $this->course_enrolment_icons($course);
1156 $content .= html_writer::end_tag('div');
1158 $content .= html_writer::start_tag('div', array('class' => 'content'));
1159 $content .= $this->coursecat_coursebox_content($chelper, $course);
1160 $content .= html_writer::end_tag('div');
1162 $content .= html_writer::end_tag('div'); // .coursebox
1163 return $content;
1167 * Returns HTML to display course summary.
1169 * @param coursecat_helper $chelper
1170 * @param core_course_list_element $course
1171 * @return string
1173 protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string {
1174 $content = '';
1175 if ($course->has_summary()) {
1176 $content .= html_writer::start_tag('div', ['class' => 'summary']);
1177 $content .= $chelper->get_course_formatted_summary($course,
1178 array('overflowdiv' => true, 'noclean' => true, 'para' => false));
1179 $content .= html_writer::end_tag('div');
1181 return $content;
1185 * Returns HTML to display course contacts.
1187 * @param core_course_list_element $course
1188 * @return string
1190 protected function course_contacts(core_course_list_element $course) {
1191 $content = '';
1192 if ($course->has_course_contacts()) {
1193 $content .= html_writer::start_tag('ul', ['class' => 'teachers']);
1194 foreach ($course->get_course_contacts() as $coursecontact) {
1195 $rolenames = array_map(function ($role) {
1196 return $role->displayname;
1197 }, $coursecontact['roles']);
1198 $name = html_writer::tag('span', implode(", ", $rolenames).': ', ['class' => 'font-weight-bold']);
1199 $name .= html_writer::link(new moodle_url('/user/view.php',
1200 ['id' => $coursecontact['user']->id, 'course' => SITEID]),
1201 $coursecontact['username']);
1202 $content .= html_writer::tag('li', $name);
1204 $content .= html_writer::end_tag('ul');
1206 return $content;
1210 * Returns HTML to display course overview files.
1212 * @param core_course_list_element $course
1213 * @return string
1215 protected function course_overview_files(core_course_list_element $course): string {
1216 global $CFG;
1218 $contentimages = $contentfiles = '';
1219 foreach ($course->get_course_overviewfiles() as $file) {
1220 $isimage = $file->is_valid_image();
1221 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php",
1222 '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
1223 $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
1224 if ($isimage) {
1225 $contentimages .= html_writer::tag('div',
1226 html_writer::empty_tag('img', ['src' => $url]),
1227 ['class' => 'courseimage']);
1228 } else {
1229 $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
1230 $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']).
1231 html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
1232 $contentfiles .= html_writer::tag('span',
1233 html_writer::link($url, $filename),
1234 ['class' => 'coursefile fp-filename-icon']);
1237 return $contentimages . $contentfiles;
1241 * Returns HTML to display course category name.
1243 * @param coursecat_helper $chelper
1244 * @param core_course_list_element $course
1245 * @return string
1247 protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string {
1248 $content = '';
1249 // Display course category if necessary (for example in search results).
1250 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
1251 if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) {
1252 $content .= html_writer::start_tag('div', ['class' => 'coursecat']);
1253 $content .= html_writer::tag('span', get_string('category').': ', ['class' => 'font-weight-bold']);
1254 $content .= html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]),
1255 $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']);
1256 $content .= html_writer::end_tag('div');
1259 return $content;
1263 * Returns HTML to display course custom fields.
1265 * @param core_course_list_element $course
1266 * @return string
1268 protected function course_custom_fields(core_course_list_element $course): string {
1269 $content = '';
1270 if ($course->has_custom_fields()) {
1271 $handler = core_course\customfield\course_handler::create();
1272 $customfields = $handler->display_custom_fields_data($course->get_custom_fields());
1273 $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']);
1275 return $content;
1279 * Returns HTML to display course content (summary, course contacts and optionally category name)
1281 * This method is called from coursecat_coursebox() and may be re-used in AJAX
1283 * @param coursecat_helper $chelper various display options
1284 * @param stdClass|core_course_list_element $course
1285 * @return string
1287 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
1288 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1289 return '';
1291 if ($course instanceof stdClass) {
1292 $course = new core_course_list_element($course);
1294 $content = \html_writer::start_tag('div', ['class' => 'd-flex']);
1295 $content .= $this->course_overview_files($course);
1296 $content .= \html_writer::start_tag('div', ['class' => 'flex-grow-1']);
1297 $content .= $this->course_summary($chelper, $course);
1298 $content .= $this->course_contacts($course);
1299 $content .= $this->course_category_name($chelper, $course);
1300 $content .= $this->course_custom_fields($course);
1301 $content .= \html_writer::end_tag('div');
1302 $content .= \html_writer::end_tag('div');
1303 return $content;
1307 * Renders the list of courses
1309 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
1310 * method from outside of the class
1312 * If list of courses is specified in $courses; the argument $chelper is only used
1313 * to retrieve display options and attributes, only methods get_show_courses(),
1314 * get_courses_display_option() and get_and_erase_attributes() are called.
1316 * @param coursecat_helper $chelper various display options
1317 * @param array $courses the list of courses to display
1318 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
1319 * defaulted to count($courses)
1320 * @return string
1322 protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) {
1323 global $CFG;
1324 if ($totalcount === null) {
1325 $totalcount = count($courses);
1327 if (!$totalcount) {
1328 // Courses count is cached during courses retrieval.
1329 return '';
1332 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) {
1333 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
1334 if ($totalcount <= $CFG->courseswithsummarieslimit) {
1335 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1336 } else {
1337 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1341 // prepare content of paging bar if it is needed
1342 $paginationurl = $chelper->get_courses_display_option('paginationurl');
1343 $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
1344 if ($totalcount > count($courses)) {
1345 // there are more results that can fit on one page
1346 if ($paginationurl) {
1347 // the option paginationurl was specified, display pagingbar
1348 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage);
1349 $page = $chelper->get_courses_display_option('offset') / $perpage;
1350 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1351 $paginationurl->out(false, array('perpage' => $perpage)));
1352 if ($paginationallowall) {
1353 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1354 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1356 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1357 // the option for 'View more' link was specified, display more link
1358 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
1359 $morelink = html_writer::tag(
1360 'div',
1361 html_writer::link($viewmoreurl, $viewmoretext, ['class' => 'btn btn-secondary']),
1362 ['class' => 'paging paging-morelink']
1365 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1366 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1367 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1368 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1371 // display list of courses
1372 $attributes = $chelper->get_and_erase_attributes('courses');
1373 $content = html_writer::start_tag('div', $attributes);
1375 if (!empty($pagingbar)) {
1376 $content .= $pagingbar;
1379 $coursecount = 0;
1380 foreach ($courses as $course) {
1381 $coursecount ++;
1382 $classes = ($coursecount%2) ? 'odd' : 'even';
1383 if ($coursecount == 1) {
1384 $classes .= ' first';
1386 if ($coursecount >= count($courses)) {
1387 $classes .= ' last';
1389 $content .= $this->coursecat_coursebox($chelper, $course, $classes);
1392 if (!empty($pagingbar)) {
1393 $content .= $pagingbar;
1395 if (!empty($morelink)) {
1396 $content .= $morelink;
1399 $content .= html_writer::end_tag('div'); // .courses
1400 return $content;
1404 * Renders the list of subcategories in a category
1406 * @param coursecat_helper $chelper various display options
1407 * @param core_course_category $coursecat
1408 * @param int $depth depth of the category in the current tree
1409 * @return string
1411 protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) {
1412 global $CFG;
1413 $subcategories = array();
1414 if (!$chelper->get_categories_display_option('nodisplay')) {
1415 $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
1417 $totalcount = $coursecat->get_children_count();
1418 if (!$totalcount) {
1419 // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children()
1420 // to avoid extra DB requests.
1421 // Categories count is cached during children categories retrieval.
1422 return '';
1425 // prepare content of paging bar or more link if it is needed
1426 $paginationurl = $chelper->get_categories_display_option('paginationurl');
1427 $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
1428 if ($totalcount > count($subcategories)) {
1429 if ($paginationurl) {
1430 // the option 'paginationurl was specified, display pagingbar
1431 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage);
1432 $page = $chelper->get_categories_display_option('offset') / $perpage;
1433 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1434 $paginationurl->out(false, array('perpage' => $perpage)));
1435 if ($paginationallowall) {
1436 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1437 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1439 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
1440 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
1441 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
1442 $viewmoreurl->param('categoryid', $coursecat->id);
1444 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
1445 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
1446 array('class' => 'paging paging-morelink'));
1448 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1449 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1450 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1451 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1454 // display list of subcategories
1455 $content = html_writer::start_tag('div', array('class' => 'subcategories'));
1457 if (!empty($pagingbar)) {
1458 $content .= $pagingbar;
1461 foreach ($subcategories as $subcategory) {
1462 $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1);
1465 if (!empty($pagingbar)) {
1466 $content .= $pagingbar;
1468 if (!empty($morelink)) {
1469 $content .= $morelink;
1472 $content .= html_writer::end_tag('div');
1473 return $content;
1477 * Make sure that javascript file for AJAX expanding of courses and categories content is included
1479 protected function coursecat_include_js() {
1480 if (!$this->page->requires->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) {
1481 return;
1484 // We must only load this module once.
1485 $this->page->requires->yui_module('moodle-course-categoryexpander',
1486 'Y.Moodle.course.categoryexpander.init');
1490 * Returns HTML to display the subcategories and courses in the given category
1492 * This method is re-used by AJAX to expand content of not loaded category
1494 * @param coursecat_helper $chelper various display options
1495 * @param core_course_category $coursecat
1496 * @param int $depth depth of the category in the current tree
1497 * @return string
1499 protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) {
1500 $content = '';
1501 // Subcategories
1502 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
1504 // AUTO show courses: Courses will be shown expanded if this is not nested category,
1505 // and number of courses no bigger than $CFG->courseswithsummarieslimit.
1506 $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO;
1507 if ($showcoursesauto && $depth) {
1508 // this is definitely collapsed mode
1509 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1512 // Courses
1513 if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) {
1514 $courses = array();
1515 if (!$chelper->get_courses_display_option('nodisplay')) {
1516 $courses = $coursecat->get_courses($chelper->get_courses_display_options());
1518 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1519 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
1520 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) {
1521 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id)));
1524 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
1527 if ($showcoursesauto) {
1528 // restore the show_courses back to AUTO
1529 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO);
1532 return $content;
1536 * Returns HTML to display a course category as a part of a tree
1538 * This is an internal function, to display a particular category and all its contents
1539 * use {@link core_course_renderer::course_category()}
1541 * @param coursecat_helper $chelper various display options
1542 * @param core_course_category $coursecat
1543 * @param int $depth depth of this category in the current tree
1544 * @return string
1546 protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) {
1547 // open category tag
1548 $classes = array('category');
1549 if (empty($coursecat->visible)) {
1550 $classes[] = 'dimmed_category';
1552 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
1553 // do not load content
1554 $categorycontent = '';
1555 $classes[] = 'notloaded';
1556 if ($coursecat->get_children_count() ||
1557 ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) {
1558 $classes[] = 'with_children';
1559 $classes[] = 'collapsed';
1561 } else {
1562 // load category content
1563 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
1564 $classes[] = 'loaded';
1565 if (!empty($categorycontent)) {
1566 $classes[] = 'with_children';
1567 // Category content loaded with children.
1568 $this->categoryexpandedonload = true;
1572 // Make sure JS file to expand category content is included.
1573 $this->coursecat_include_js();
1575 $content = html_writer::start_tag('div', array(
1576 'class' => join(' ', $classes),
1577 'data-categoryid' => $coursecat->id,
1578 'data-depth' => $depth,
1579 'data-showcourses' => $chelper->get_show_courses(),
1580 'data-type' => self::COURSECAT_TYPE_CATEGORY,
1583 // category name
1584 $categoryname = $coursecat->get_formatted_name();
1585 $categoryname = html_writer::link(new moodle_url('/course/index.php',
1586 array('categoryid' => $coursecat->id)),
1587 $categoryname);
1588 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT
1589 && ($coursescount = $coursecat->get_courses_count())) {
1590 $categoryname .= html_writer::tag('span', ' ('. $coursescount.')',
1591 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
1593 $content .= html_writer::start_tag('div', array('class' => 'info'));
1595 $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn'));
1596 $content .= html_writer::end_tag('div'); // .info
1598 // add category content to the output
1599 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1601 $content .= html_writer::end_tag('div'); // .category
1603 // Return the course category tree HTML
1604 return $content;
1608 * Returns HTML to display a tree of subcategories and courses in the given category
1610 * @param coursecat_helper $chelper various display options
1611 * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree)
1612 * @return string
1614 protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
1615 // Reset the category expanded flag for this course category tree first.
1616 $this->categoryexpandedonload = false;
1617 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1618 if (empty($categorycontent)) {
1619 return '';
1622 // Start content generation
1623 $content = '';
1624 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1625 $content .= html_writer::start_tag('div', $attributes);
1627 if ($coursecat->get_children_count()) {
1628 $classes = array(
1629 'collapseexpand', 'aabtn'
1632 // Check if the category content contains subcategories with children's content loaded.
1633 if ($this->categoryexpandedonload) {
1634 $classes[] = 'collapse-all';
1635 $linkname = get_string('collapseall');
1636 } else {
1637 $linkname = get_string('expandall');
1640 // Only show the collapse/expand if there are children to expand.
1641 $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions'));
1642 $content .= html_writer::link('#', $linkname, array('class' => implode(' ', $classes)));
1643 $content .= html_writer::end_tag('div');
1644 $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle');
1647 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1649 $content .= html_writer::end_tag('div'); // .course_category_tree
1651 return $content;
1655 * Renders HTML to display particular course category - list of it's subcategories and courses
1657 * Invoked from /course/index.php
1659 * @param int|stdClass|core_course_category $category
1661 public function course_category($category) {
1662 global $CFG;
1663 $usertop = core_course_category::user_top();
1664 if (empty($category)) {
1665 $coursecat = $usertop;
1666 } else if (is_object($category) && $category instanceof core_course_category) {
1667 $coursecat = $category;
1668 } else {
1669 $coursecat = core_course_category::get(is_object($category) ? $category->id : $category);
1671 $site = get_site();
1672 $output = '';
1674 if ($coursecat->can_create_course() || $coursecat->has_manage_capability()) {
1675 // Add 'Manage' button if user has permissions to edit this category.
1676 $managebutton = $this->single_button(new moodle_url('/course/management.php',
1677 array('categoryid' => $coursecat->id)), get_string('managecourses'), 'get');
1678 $this->page->set_button($managebutton);
1681 if (core_course_category::is_simple_site()) {
1682 // There is only one category in the system, do not display link to it.
1683 $strfulllistofcourses = get_string('fulllistofcourses');
1684 $this->page->set_title("$site->shortname: $strfulllistofcourses");
1685 } else if (!$coursecat->id || !$coursecat->is_uservisible()) {
1686 $strcategories = get_string('categories');
1687 $this->page->set_title("$site->shortname: $strcategories");
1688 } else {
1689 $strfulllistofcourses = get_string('fulllistofcourses');
1690 $this->page->set_title("$site->shortname: $strfulllistofcourses");
1692 // Print the category selector
1693 $categorieslist = core_course_category::make_categories_list();
1694 if (count($categorieslist) > 1) {
1695 $output .= html_writer::start_tag('div', array('class' => 'categorypicker'));
1696 $select = new single_select(new moodle_url('/course/index.php'), 'categoryid',
1697 core_course_category::make_categories_list(), $coursecat->id, null, 'switchcategory');
1698 $select->set_label(get_string('categories').':', ['class' => 'sr-only']);
1699 $output .= $this->render($select);
1700 $output .= html_writer::end_tag('div'); // .categorypicker
1704 // Print current category description
1705 $chelper = new coursecat_helper();
1706 if ($description = $chelper->get_category_formatted_description($coursecat)) {
1707 $output .= $this->box($description, array('class' => 'generalbox info'));
1710 // Prepare parameters for courses and categories lists in the tree
1711 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
1712 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));
1714 $coursedisplayoptions = array();
1715 $catdisplayoptions = array();
1716 $browse = optional_param('browse', null, PARAM_ALPHA);
1717 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
1718 $page = optional_param('page', 0, PARAM_INT);
1719 $baseurl = new moodle_url('/course/index.php');
1720 if ($coursecat->id) {
1721 $baseurl->param('categoryid', $coursecat->id);
1723 if ($perpage != $CFG->coursesperpage) {
1724 $baseurl->param('perpage', $perpage);
1726 $coursedisplayoptions['limit'] = $perpage;
1727 $catdisplayoptions['limit'] = $perpage;
1728 if ($browse === 'courses' || !$coursecat->get_children_count()) {
1729 $coursedisplayoptions['offset'] = $page * $perpage;
1730 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1731 $catdisplayoptions['nodisplay'] = true;
1732 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1733 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
1734 } else if ($browse === 'categories' || !$coursecat->get_courses_count()) {
1735 $coursedisplayoptions['nodisplay'] = true;
1736 $catdisplayoptions['offset'] = $page * $perpage;
1737 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1738 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1739 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1740 } else {
1741 // we have a category that has both subcategories and courses, display pagination separately
1742 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1743 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1745 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1746 // Add course search form.
1747 $output .= $this->course_search_form();
1749 // Display course category tree.
1750 $output .= $this->coursecat_tree($chelper, $coursecat);
1752 // Add action buttons
1753 $output .= $this->container_start('buttons mt-3');
1754 if ($coursecat->is_uservisible()) {
1755 $context = get_category_or_system_context($coursecat->id);
1756 if (has_capability('moodle/course:create', $context)) {
1757 // Print link to create a new course, for the 1st available category.
1758 if ($coursecat->id) {
1759 $url = new moodle_url('/course/edit.php', array('category' => $coursecat->id, 'returnto' => 'category'));
1760 } else {
1761 $url = new moodle_url('/course/edit.php',
1762 array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
1764 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
1766 ob_start();
1767 print_course_request_buttons($context);
1768 $output .= ob_get_contents();
1769 ob_end_clean();
1771 $output .= $this->container_end();
1773 return $output;
1777 * Serves requests to /course/category.ajax.php
1779 * In this renderer implementation it may expand the category content or
1780 * course content.
1782 * @return string
1783 * @throws coding_exception
1785 public function coursecat_ajax() {
1786 global $DB, $CFG;
1788 $type = required_param('type', PARAM_INT);
1790 if ($type === self::COURSECAT_TYPE_CATEGORY) {
1791 // This is a request for a category list of some kind.
1792 $categoryid = required_param('categoryid', PARAM_INT);
1793 $showcourses = required_param('showcourses', PARAM_INT);
1794 $depth = required_param('depth', PARAM_INT);
1796 $category = core_course_category::get($categoryid);
1798 $chelper = new coursecat_helper();
1799 $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
1800 $coursedisplayoptions = array(
1801 'limit' => $CFG->coursesperpage,
1802 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
1804 $catdisplayoptions = array(
1805 'limit' => $CFG->coursesperpage,
1806 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
1808 $chelper->set_show_courses($showcourses)->
1809 set_courses_display_options($coursedisplayoptions)->
1810 set_categories_display_options($catdisplayoptions);
1812 return $this->coursecat_category_content($chelper, $category, $depth);
1813 } else if ($type === self::COURSECAT_TYPE_COURSE) {
1814 // This is a request for the course information.
1815 $courseid = required_param('courseid', PARAM_INT);
1817 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
1819 $chelper = new coursecat_helper();
1820 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1821 return $this->coursecat_coursebox_content($chelper, $course);
1822 } else {
1823 throw new coding_exception('Invalid request type');
1828 * Renders html to display search result page
1830 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1831 * @return string
1833 public function search_courses($searchcriteria) {
1834 global $CFG;
1835 $content = '';
1837 $search = '';
1838 if (!empty($searchcriteria['search'])) {
1839 $search = $searchcriteria['search'];
1841 $content .= $this->course_search_form($search);
1843 if (!empty($searchcriteria)) {
1844 // print search results
1846 $displayoptions = array('sort' => array('displayname' => 1));
1847 // take the current page and number of results per page from query
1848 $perpage = optional_param('perpage', 0, PARAM_RAW);
1849 if ($perpage !== 'all') {
1850 $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
1851 $page = optional_param('page', 0, PARAM_INT);
1852 $displayoptions['offset'] = $displayoptions['limit'] * $page;
1854 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1855 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1856 $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1858 $class = 'course-search-result';
1859 foreach ($searchcriteria as $key => $value) {
1860 if (!empty($value)) {
1861 $class .= ' course-search-result-'. $key;
1864 $chelper = new coursecat_helper();
1865 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1866 set_courses_display_options($displayoptions)->
1867 set_search_criteria($searchcriteria)->
1868 set_attributes(array('class' => $class));
1870 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1871 $totalcount = core_course_category::search_courses_count($searchcriteria);
1872 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1874 if (!$totalcount) {
1875 if (!empty($searchcriteria['search'])) {
1876 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1877 } else {
1878 $content .= $this->heading(get_string('novalidcourses'));
1880 } else {
1881 $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1882 $content .= $courseslist;
1885 return $content;
1889 * Renders html to print list of courses tagged with particular tag
1891 * @param int $tagid id of the tag
1892 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1893 * are displayed on the page and the per-page limit may be bigger
1894 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1895 * to display items in the same context first
1896 * @param int $ctx context id where to search for records
1897 * @param bool $rec search in subcontexts as well
1898 * @param array $displayoptions
1899 * @return string empty string if no courses are marked with this tag or rendered list of courses
1901 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) {
1902 global $CFG;
1903 if (empty($displayoptions)) {
1904 $displayoptions = array();
1906 $showcategories = !core_course_category::is_simple_site();
1907 $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0);
1908 $chelper = new coursecat_helper();
1909 $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
1910 $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT :
1911 self::COURSECAT_SHOW_COURSES_EXPANDED)->
1912 set_search_criteria($searchcriteria)->
1913 set_courses_display_options($displayoptions)->
1914 set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1915 // (we set the same css class as in search results by tagid)
1916 if ($totalcount = core_course_category::search_courses_count($searchcriteria)) {
1917 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options());
1918 if ($exclusivemode) {
1919 return $this->coursecat_courses($chelper, $courses, $totalcount);
1920 } else {
1921 $tagfeed = new core_tag\output\tagfeed();
1922 $img = $this->output->pix_icon('i/course', '');
1923 foreach ($courses as $course) {
1924 $url = course_get_url($course);
1925 $imgwithlink = html_writer::link($url, $img);
1926 $coursename = html_writer::link($url, $course->get_formatted_name());
1927 $details = '';
1928 if ($showcategories && ($cat = core_course_category::get($course->category, IGNORE_MISSING))) {
1929 $details = get_string('category').': '.
1930 html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)),
1931 $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
1933 $tagfeed->add($imgwithlink, $coursename, $details);
1935 return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output));
1938 return '';
1942 * Returns HTML to display one remote course
1944 * @param stdClass $course remote course information, contains properties:
1945 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1946 * @return string
1948 protected function frontpage_remote_course(stdClass $course) {
1949 $url = new moodle_url('/auth/mnet/jump.php', array(
1950 'hostid' => $course->hostid,
1951 'wantsurl' => '/course/view.php?id='. $course->remoteid
1954 $output = '';
1955 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1956 $output .= html_writer::start_tag('div', array('class' => 'info'));
1957 $output .= html_writer::start_tag('h3', array('class' => 'coursename'));
1958 $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
1959 $output .= html_writer::end_tag('h3'); // .name
1960 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1961 $output .= html_writer::end_tag('div'); // .info
1962 $output .= html_writer::start_tag('div', array('class' => 'content'));
1963 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1964 $options = new stdClass();
1965 $options->noclean = true;
1966 $options->para = false;
1967 $options->overflowdiv = true;
1968 $output .= format_text($course->summary, $course->summaryformat, $options);
1969 $output .= html_writer::end_tag('div'); // .summary
1970 $addinfo = format_string($course->hostname) . ' : '
1971 . format_string($course->cat_name) . ' : '
1972 . format_string($course->shortname);
1973 $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1974 $output .= html_writer::end_tag('div'); // .content
1975 $output .= html_writer::end_tag('div'); // .coursebox
1976 return $output;
1980 * Returns HTML to display one remote host
1982 * @param array $host host information, contains properties: name, url, count
1983 * @return string
1985 protected function frontpage_remote_host($host) {
1986 $output = '';
1987 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1988 $output .= html_writer::start_tag('div', array('class' => 'info'));
1989 $output .= html_writer::start_tag('h3', array('class' => 'name'));
1990 $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1991 $output .= html_writer::end_tag('h3'); // .name
1992 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1993 $output .= html_writer::end_tag('div'); // .info
1994 $output .= html_writer::start_tag('div', array('class' => 'content'));
1995 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1996 $output .= $host['count'] . ' ' . get_string('courses');
1997 $output .= html_writer::end_tag('div'); // .content
1998 $output .= html_writer::end_tag('div'); // .coursebox
1999 return $output;
2003 * Returns HTML to print list of courses user is enrolled to for the frontpage
2005 * Also lists remote courses or remote hosts if MNET authorisation is used
2007 * @return string
2009 public function frontpage_my_courses() {
2010 global $USER, $CFG, $DB;
2012 if (!isloggedin() or isguestuser()) {
2013 return '';
2016 $output = '';
2017 $courses = enrol_get_my_courses('summary, summaryformat');
2018 $rhosts = array();
2019 $rcourses = array();
2020 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
2021 $rcourses = get_my_remotecourses($USER->id);
2022 $rhosts = get_my_remotehosts();
2025 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
2027 $chelper = new coursecat_helper();
2028 $totalcount = count($courses);
2029 if (count($courses) > $CFG->frontpagecourselimit) {
2030 // There are more enrolled courses than we can display, display link to 'My courses'.
2031 $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true);
2032 $chelper->set_courses_display_options(array(
2033 'viewmoreurl' => new moodle_url('/my/'),
2034 'viewmoretext' => new lang_string('mycourses')
2036 } else if (core_course_category::top()->is_uservisible()) {
2037 // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system.
2038 $chelper->set_courses_display_options(array(
2039 'viewmoreurl' => new moodle_url('/course/index.php'),
2040 'viewmoretext' => new lang_string('fulllistofcourses')
2042 $totalcount = $DB->count_records('course') - 1;
2044 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
2045 set_attributes(array('class' => 'frontpage-course-list-enrolled'));
2046 $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
2048 // MNET
2049 if (!empty($rcourses)) {
2050 // at the IDP, we know of all the remote courses
2051 $output .= html_writer::start_tag('div', array('class' => 'courses'));
2052 foreach ($rcourses as $course) {
2053 $output .= $this->frontpage_remote_course($course);
2055 $output .= html_writer::end_tag('div'); // .courses
2056 } elseif (!empty($rhosts)) {
2057 // non-IDP, we know of all the remote servers, but not courses
2058 $output .= html_writer::start_tag('div', array('class' => 'courses'));
2059 foreach ($rhosts as $host) {
2060 $output .= $this->frontpage_remote_host($host);
2062 $output .= html_writer::end_tag('div'); // .courses
2065 return $output;
2069 * Returns HTML to print list of available courses for the frontpage
2071 * @return string
2073 public function frontpage_available_courses() {
2074 global $CFG;
2076 $chelper = new coursecat_helper();
2077 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
2078 set_courses_display_options(array(
2079 'recursive' => true,
2080 'limit' => $CFG->frontpagecourselimit,
2081 'viewmoreurl' => new moodle_url('/course/index.php'),
2082 'viewmoretext' => new lang_string('fulllistofcourses')));
2084 $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
2085 $courses = core_course_category::top()->get_courses($chelper->get_courses_display_options());
2086 $totalcount = core_course_category::top()->get_courses_count($chelper->get_courses_display_options());
2087 if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) {
2088 // Print link to create a new course, for the 1st available category.
2089 return $this->add_new_course_button();
2091 return $this->coursecat_courses($chelper, $courses, $totalcount);
2095 * Returns HTML to the "add new course" button for the page
2097 * @return string
2099 public function add_new_course_button() {
2100 global $CFG;
2101 // Print link to create a new course, for the 1st available category.
2102 $output = $this->container_start('buttons');
2103 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
2104 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
2105 $output .= $this->container_end('buttons');
2106 return $output;
2110 * Returns HTML to print tree with course categories and courses for the frontpage
2112 * @return string
2114 public function frontpage_combo_list() {
2115 global $CFG;
2116 // TODO MDL-10965 improve.
2117 $tree = core_course_category::top();
2118 if (!$tree->get_children_count()) {
2119 return '';
2121 $chelper = new coursecat_helper();
2122 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
2123 set_categories_display_options(array(
2124 'limit' => $CFG->coursesperpage,
2125 'viewmoreurl' => new moodle_url('/course/index.php',
2126 array('browse' => 'categories', 'page' => 1))
2127 ))->
2128 set_courses_display_options(array(
2129 'limit' => $CFG->coursesperpage,
2130 'viewmoreurl' => new moodle_url('/course/index.php',
2131 array('browse' => 'courses', 'page' => 1))
2132 ))->
2133 set_attributes(array('class' => 'frontpage-category-combo'));
2134 return $this->coursecat_tree($chelper, $tree);
2138 * Returns HTML to print tree of course categories (with number of courses) for the frontpage
2140 * @return string
2142 public function frontpage_categories_list() {
2143 global $CFG;
2144 // TODO MDL-10965 improve.
2145 $tree = core_course_category::top();
2146 if (!$tree->get_children_count()) {
2147 return '';
2149 $chelper = new coursecat_helper();
2150 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
2151 set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
2152 set_categories_display_options(array(
2153 'limit' => $CFG->coursesperpage,
2154 'viewmoreurl' => new moodle_url('/course/index.php',
2155 array('browse' => 'categories', 'page' => 1))
2156 ))->
2157 set_attributes(array('class' => 'frontpage-category-names'));
2158 return $this->coursecat_tree($chelper, $tree);
2162 * Renders the activity information.
2164 * Defer to template.
2166 * @param \core_course\output\activity_information $page
2167 * @return string html for the page
2169 public function render_activity_information(\core_course\output\activity_information $page) {
2170 $data = $page->export_for_template($this->output);
2171 return $this->output->render_from_template('core_course/activity_info', $data);
2175 * Renders the activity navigation.
2177 * Defer to template.
2179 * @param \core_course\output\activity_navigation $page
2180 * @return string html for the page
2182 public function render_activity_navigation(\core_course\output\activity_navigation $page) {
2183 $data = $page->export_for_template($this->output);
2184 return $this->output->render_from_template('core_course/activity_navigation', $data);
2188 * Display waiting information about backup size during uploading backup process
2189 * @param object $backupfile the backup stored_file
2190 * @return $html string
2192 public function sendingbackupinfo($backupfile) {
2193 $sizeinfo = new stdClass();
2194 $sizeinfo->total = number_format($backupfile->get_filesize() / 1000000, 2);
2195 $html = html_writer::tag('div', get_string('sendingsize', 'hub', $sizeinfo),
2196 array('class' => 'courseuploadtextinfo'));
2197 return $html;
2201 * Hub information (logo - name - description - link)
2202 * @param object $hubinfo
2203 * @return string html code
2205 public function hubinfo($hubinfo) {
2206 $screenshothtml = html_writer::empty_tag('img',
2207 array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name']));
2208 $hubdescription = html_writer::tag('div', $screenshothtml,
2209 array('class' => 'hubscreenshot'));
2211 $hubdescription .= html_writer::tag('a', $hubinfo['name'],
2212 array('class' => 'hublink', 'href' => $hubinfo['url'],
2213 'onclick' => 'this.target="_blank"'));
2215 $hubdescription .= html_writer::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN),
2216 array('class' => 'hubdescription'));
2217 $hubdescription = html_writer::tag('div', $hubdescription, array('class' => 'hubinfo clearfix'));
2219 return $hubdescription;
2223 * Output frontpage summary text and frontpage modules (stored as section 1 in site course)
2225 * This may be disabled in settings
2227 * @return string
2229 public function frontpage_section1() {
2230 global $SITE, $USER;
2232 $output = '';
2233 $editing = $this->page->user_is_editing();
2235 if ($editing) {
2236 // Make sure section with number 1 exists.
2237 course_create_sections_if_missing($SITE, 1);
2240 $modinfo = get_fast_modinfo($SITE);
2241 $section = $modinfo->get_section_info(1);
2244 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) {
2246 $format = course_get_format($SITE);
2247 $frontpageclass = $format->get_output_classname('content\\frontpagesection');
2248 $frontpagesection = new $frontpageclass($format, $section);
2250 // The course outputs works with format renderers, not with course renderers.
2251 $renderer = $format->get_renderer($this->page);
2252 $output .= $renderer->render($frontpagesection);
2255 return $output;
2259 * Output news for the frontpage (extract from site-wide news forum)
2261 * @param stdClass $forum record from db table 'forum' that represents the site news forum
2262 * @return string
2264 protected function frontpage_news($forum) {
2265 global $CFG, $SITE, $SESSION, $USER;
2266 require_once($CFG->dirroot .'/mod/forum/lib.php');
2268 $output = '';
2270 if (isloggedin()) {
2271 $SESSION->fromdiscussion = $CFG->wwwroot;
2272 $subtext = '';
2273 if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) {
2274 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
2275 $subtext = get_string('unsubscribe', 'forum');
2277 } else {
2278 $subtext = get_string('subscribe', 'forum');
2280 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey()));
2281 $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
2284 $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
2285 $context = context_module::instance($coursemodule->id);
2287 $entityfactory = mod_forum\local\container::get_entity_factory();
2288 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE);
2290 $rendererfactory = mod_forum\local\container::get_renderer_factory();
2291 $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity);
2292 $cm = \cm_info::create($coursemodule);
2293 return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems);
2297 * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
2299 * @param string $skipdivid
2300 * @param string $contentsdivid
2301 * @param string $header Header of the part
2302 * @param string $contents Contents of the part
2303 * @return string
2305 protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
2306 if (strval($contents) === '') {
2307 return '';
2309 $output = html_writer::link('#' . $skipdivid,
2310 get_string('skipa', 'access', core_text::strtolower(strip_tags($header))),
2311 array('class' => 'skip-block skip aabtn'));
2313 // Wrap frontpage part in div container.
2314 $output .= html_writer::start_tag('div', array('id' => $contentsdivid));
2315 $output .= $this->heading($header);
2317 $output .= $contents;
2319 // End frontpage part div container.
2320 $output .= html_writer::end_tag('div');
2322 $output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
2323 return $output;
2327 * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
2329 * @return string
2331 public function frontpage() {
2332 global $CFG, $SITE;
2334 $output = '';
2336 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
2337 $frontpagelayout = $CFG->frontpageloggedin;
2338 } else {
2339 $frontpagelayout = $CFG->frontpage;
2342 foreach (explode(',', $frontpagelayout) as $v) {
2343 switch ($v) {
2344 // Display the main part of the front page.
2345 case FRONTPAGENEWS:
2346 if ($SITE->newsitems) {
2347 // Print forums only when needed.
2348 require_once($CFG->dirroot .'/mod/forum/lib.php');
2349 if (($newsforum = forum_get_course_forum($SITE->id, 'news')) &&
2350 ($forumcontents = $this->frontpage_news($newsforum))) {
2351 $newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id];
2352 $output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
2353 $newsforumcm->get_formatted_name(), $forumcontents);
2356 break;
2358 case FRONTPAGEENROLLEDCOURSELIST:
2359 $mycourseshtml = $this->frontpage_my_courses();
2360 if (!empty($mycourseshtml)) {
2361 $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
2362 get_string('mycourses'), $mycourseshtml);
2364 break;
2366 case FRONTPAGEALLCOURSELIST:
2367 $availablecourseshtml = $this->frontpage_available_courses();
2368 $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
2369 get_string('availablecourses'), $availablecourseshtml);
2370 break;
2372 case FRONTPAGECATEGORYNAMES:
2373 $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
2374 get_string('categories'), $this->frontpage_categories_list());
2375 break;
2377 case FRONTPAGECATEGORYCOMBO:
2378 $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
2379 get_string('courses'), $this->frontpage_combo_list());
2380 break;
2382 case FRONTPAGECOURSESEARCH:
2383 $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
2384 break;
2387 $output .= '<br />';
2390 return $output;
2395 * Class storing display options and functions to help display course category and/or courses lists
2397 * This is a wrapper for core_course_category objects that also stores display options
2398 * and functions to retrieve sorted and paginated lists of categories/courses.
2400 * If theme overrides methods in core_course_renderers that access this class
2401 * it may as well not use this class at all or extend it.
2403 * @package core
2404 * @copyright 2013 Marina Glancy
2405 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2407 class coursecat_helper {
2408 /** @var string [none, collapsed, expanded] how (if) display courses list */
2409 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
2410 /** @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) */
2411 protected $subcatdepth = 1;
2412 /** @var array options to display courses list */
2413 protected $coursesdisplayoptions = array();
2414 /** @var array options to display subcategories list */
2415 protected $categoriesdisplayoptions = array();
2416 /** @var array additional HTML attributes */
2417 protected $attributes = array();
2418 /** @var array search criteria if the list is a search result */
2419 protected $searchcriteria = null;
2422 * Sets how (if) to show the courses - none, collapsed, expanded, etc.
2424 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
2425 * @return coursecat_helper
2427 public function set_show_courses($showcourses) {
2428 $this->showcourses = $showcourses;
2429 // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses()
2430 // and core_course_category::search_courses().
2431 $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
2432 $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
2433 $this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED;
2434 return $this;
2438 * Returns how (if) to show the courses - none, collapsed, expanded, etc.
2440 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
2442 public function get_show_courses() {
2443 return $this->showcourses;
2447 * Sets the maximum depth to expand subcategories in the tree
2449 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2451 * @param int $subcatdepth
2452 * @return coursecat_helper
2454 public function set_subcat_depth($subcatdepth) {
2455 $this->subcatdepth = $subcatdepth;
2456 return $this;
2460 * Returns the maximum depth to expand subcategories in the tree
2462 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2464 * @return int
2466 public function get_subcat_depth() {
2467 return $this->subcatdepth;
2471 * Sets options to display list of courses
2473 * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses()
2475 * Options that core_course_category::get_courses() accept:
2476 * - recursive - return courses from subcategories as well. Use with care,
2477 * this may be a huge list!
2478 * - summary - preloads fields 'summary' and 'summaryformat'
2479 * - coursecontacts - preloads course contacts
2480 * - customfields - preloads custom fields data
2481 * - isenrolled - preloads indication whether this user is enrolled in the course
2482 * - sort - list of fields to sort. Example
2483 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
2484 * will sort by idnumber asc, shortname asc and id desc.
2485 * Default: array('sortorder' => 1)
2486 * Only cached fields may be used for sorting!
2487 * - offset
2488 * - limit - maximum number of children to return, 0 or null for no limit
2490 * Options summary and coursecontacts are filled automatically in the set_show_courses()
2492 * Also renderer can set here any additional options it wants to pass between renderer functions.
2494 * @param array $options
2495 * @return coursecat_helper
2497 public function set_courses_display_options($options) {
2498 $this->coursesdisplayoptions = $options;
2499 $this->set_show_courses($this->showcourses); // this will calculate special display options
2500 return $this;
2504 * Sets one option to display list of courses
2506 * @see coursecat_helper::set_courses_display_options()
2508 * @param string $key
2509 * @param mixed $value
2510 * @return coursecat_helper
2512 public function set_courses_display_option($key, $value) {
2513 $this->coursesdisplayoptions[$key] = $value;
2514 return $this;
2518 * Return the specified option to display list of courses
2520 * @param string $optionname option name
2521 * @param mixed $defaultvalue default value for option if it is not specified
2522 * @return mixed
2524 public function get_courses_display_option($optionname, $defaultvalue = null) {
2525 if (array_key_exists($optionname, $this->coursesdisplayoptions)) {
2526 return $this->coursesdisplayoptions[$optionname];
2527 } else {
2528 return $defaultvalue;
2533 * Returns all options to display the courses
2535 * This array is usually passed to {@link core_course_category::get_courses()} or
2536 * {@link core_course_category::search_courses()}
2538 * @return array
2540 public function get_courses_display_options() {
2541 return $this->coursesdisplayoptions;
2545 * Sets options to display list of subcategories
2547 * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children().
2548 * Any other options may be used by renderer functions
2550 * @param array $options
2551 * @return coursecat_helper
2553 public function set_categories_display_options($options) {
2554 $this->categoriesdisplayoptions = $options;
2555 return $this;
2559 * Return the specified option to display list of subcategories
2561 * @param string $optionname option name
2562 * @param mixed $defaultvalue default value for option if it is not specified
2563 * @return mixed
2565 public function get_categories_display_option($optionname, $defaultvalue = null) {
2566 if (array_key_exists($optionname, $this->categoriesdisplayoptions)) {
2567 return $this->categoriesdisplayoptions[$optionname];
2568 } else {
2569 return $defaultvalue;
2574 * Returns all options to display list of subcategories
2576 * This array is usually passed to {@link core_course_category::get_children()}
2578 * @return array
2580 public function get_categories_display_options() {
2581 return $this->categoriesdisplayoptions;
2585 * Sets additional general options to pass between renderer functions, usually HTML attributes
2587 * @param array $attributes
2588 * @return coursecat_helper
2590 public function set_attributes($attributes) {
2591 $this->attributes = $attributes;
2592 return $this;
2596 * Return all attributes and erases them so they are not applied again
2598 * @param string $classname adds additional class name to the beginning of $attributes['class']
2599 * @return array
2601 public function get_and_erase_attributes($classname) {
2602 $attributes = $this->attributes;
2603 $this->attributes = array();
2604 if (empty($attributes['class'])) {
2605 $attributes['class'] = '';
2607 $attributes['class'] = $classname . ' '. $attributes['class'];
2608 return $attributes;
2612 * Sets the search criteria if the course is a search result
2614 * Search string will be used to highlight terms in course name and description
2616 * @param array $searchcriteria
2617 * @return coursecat_helper
2619 public function set_search_criteria($searchcriteria) {
2620 $this->searchcriteria = $searchcriteria;
2621 return $this;
2625 * Returns formatted and filtered description of the given category
2627 * @param core_course_category $coursecat category
2628 * @param stdClass|array $options format options, by default [noclean,overflowdiv],
2629 * if context is not specified it will be added automatically
2630 * @return string|null
2632 public function get_category_formatted_description($coursecat, $options = null) {
2633 if ($coursecat->id && $coursecat->is_uservisible() && !empty($coursecat->description)) {
2634 if (!isset($coursecat->descriptionformat)) {
2635 $descriptionformat = FORMAT_MOODLE;
2636 } else {
2637 $descriptionformat = $coursecat->descriptionformat;
2639 if ($options === null) {
2640 $options = array('noclean' => true, 'overflowdiv' => true);
2641 } else {
2642 $options = (array)$options;
2644 $context = context_coursecat::instance($coursecat->id);
2645 if (!isset($options['context'])) {
2646 $options['context'] = $context;
2648 $text = file_rewrite_pluginfile_urls($coursecat->description,
2649 'pluginfile.php', $context->id, 'coursecat', 'description', null);
2650 return format_text($text, $descriptionformat, $options);
2652 return null;
2656 * Returns given course's summary with proper embedded files urls and formatted
2658 * @param core_course_list_element $course
2659 * @param array|stdClass $options additional formatting options
2660 * @return string
2662 public function get_course_formatted_summary($course, $options = array()) {
2663 global $CFG;
2664 require_once($CFG->libdir. '/filelib.php');
2665 if (!$course->has_summary()) {
2666 return '';
2668 $options = (array)$options;
2669 $context = context_course::instance($course->id);
2670 if (!isset($options['context'])) {
2671 // TODO see MDL-38521
2672 // option 1 (current), page context - no code required
2673 // option 2, system context
2674 // $options['context'] = context_system::instance();
2675 // option 3, course context:
2676 // $options['context'] = $context;
2677 // option 4, course category context:
2678 // $options['context'] = $context->get_parent_context();
2680 $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null);
2681 $summary = format_text($summary, $course->summaryformat, $options, $course->id);
2682 if (!empty($this->searchcriteria['search'])) {
2683 $summary = highlight($this->searchcriteria['search'], $summary);
2685 return $summary;
2689 * Returns course name as it is configured to appear in courses lists formatted to course context
2691 * @param core_course_list_element $course
2692 * @param array|stdClass $options additional formatting options
2693 * @return string
2695 public function get_course_formatted_name($course, $options = array()) {
2696 $options = (array)$options;
2697 if (!isset($options['context'])) {
2698 $options['context'] = context_course::instance($course->id);
2700 $name = format_string(get_course_display_name_for_list($course), true, $options);
2701 if (!empty($this->searchcriteria['search'])) {
2702 $name = highlight($this->searchcriteria['search'], $name);
2704 return $name;