3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Renderer for use with the course section and all the goodness that falls
22 * This renderer should contain methods useful to courses, and categories.
25 * @copyright 2010 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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;
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
58 protected $categoryexpandedonload = false;
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);
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.');
81 * Renders course info box.
83 * @param stdClass $course
86 public function course_info_box(stdClass
$course) {
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();
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
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
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
);
128 * Render a modchooser.
130 * @param renderable $modchooser The chooser.
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.
157 public function course_activitychooser($courseid) {
159 if (!$this->page
->requires
->should_create_one_time_item_now('core_course_modchooser')) {
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]);
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
);
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
192 * @param array $classes Additional classes to add to the encompassing
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
);
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
);
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.
226 public function course_section_cm_edit_actions($actions, cm_info
$mod = null, $displayoptions = array()) {
230 'course_section_cm_edit_actions is deprecated. Use core_courseformat\\output\\local\\content\\cm\\controlmenu instead.',
234 if (empty($actions)) {
238 if (isset($displayoptions['ownerselector'])) {
239 $ownerselector = $displayoptions['ownerselector'];
241 $ownerselector = '#module-'.$mod->id
;
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'];
250 $constraint = '.course-content';
253 $menu = new action_menu();
254 $menu->set_owner_selector($ownerselector);
255 $menu->set_constraint($constraint);
256 $menu->set_menu_trigger(get_string('edit'));
258 foreach ($actions as $action) {
259 if ($action instanceof action_menu_link
) {
260 $action->add_class('cm-edit-action');
264 $menu->attributes
['class'] .= ' section-cm-edit-actions commands';
266 // Prioritise the menu ahead of all other actions.
267 $menu->prioritise
= true;
269 return $this->render($menu);
273 * Renders HTML for the menus to add activities and resources to the current course
275 * Renders the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback.
277 * @param stdClass $course
278 * @param int $section relative section number (field course_sections.section)
279 * @param int $sectionreturn The section to link back to
280 * @param array $displayoptions additional display options, for example blocks add
281 * option 'inblock' => true, suggesting to display controls vertically
284 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
285 // Check to see if user can add menus.
286 if (!has_capability('moodle/course:manageactivities', context_course
::instance($course->id
))
287 ||
!$this->page
->user_is_editing()) {
292 'sectionid' => $section,
293 'sectionreturn' => $sectionreturn
295 $ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
297 // Load the JS for the modal.
298 $this->course_activitychooser($course->id
);
304 * Render the deprecated nonajax activity chooser.
306 * @deprecated since Moodle 3.11
308 * @todo MDL-71331 deprecate this function
309 * @param stdClass $course the course object
310 * @param int $section relative section number (field course_sections.section)
311 * @param int $sectionreturn The section to link back to
312 * @param array $displayoptions additional display options, for example blocks add
313 * option 'inblock' => true, suggesting to display controls vertically
316 private function course_section_add_cm_control_nonajax($course, $section, $sectionreturn = null,
317 $displayoptions = array()): string {
320 $vertical = !empty($displayoptions['inblock']);
322 // Check to see if user can add menus.
324 !has_capability('moodle/course:manageactivities', context_course
::instance($course->id
))
325 ||
!$this->page
->user_is_editing()
330 debugging('non-js dropdowns are deprecated.', DEBUG_DEVELOPER
);
331 // Retrieve all modules with associated metadata.
332 $contentitemservice = \core_course\local\factory\content_item_service_factory
::get_content_item_service();
333 $urlparams = ['section' => $section];
334 if (!is_null($sectionreturn)) {
335 $urlparams['sr'] = $sectionreturn;
337 $modules = $contentitemservice->get_content_items_for_user_in_course($USER, $course, $urlparams);
339 // Return if there are no content items to add.
340 if (empty($modules)) {
344 // We'll sort resources and activities into two lists.
345 $activities = array(MOD_CLASS_ACTIVITY
=> array(), MOD_CLASS_RESOURCE
=> array());
347 foreach ($modules as $module) {
348 $activityclass = MOD_CLASS_ACTIVITY
;
349 if ($module->archetype
== MOD_ARCHETYPE_RESOURCE
) {
350 $activityclass = MOD_CLASS_RESOURCE
;
351 } else if ($module->archetype
=== MOD_ARCHETYPE_SYSTEM
) {
352 // System modules cannot be added by user, do not add to dropdown.
355 $link = $module->link
;
356 $activities[$activityclass][$link] = $module->title
;
359 $straddactivity = get_string('addactivity');
360 $straddresource = get_string('addresource');
361 $sectionname = get_section_name($course, $section);
362 $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
363 $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
365 $nonajaxcontrol = html_writer
::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-'
369 $nonajaxcontrol .= html_writer
::start_tag('div', array('class' => 'horizontal'));
372 if (!empty($activities[MOD_CLASS_RESOURCE
])) {
373 $select = new url_select($activities[MOD_CLASS_RESOURCE
], '', array('' => $straddresource), "ressection$section");
374 $select->set_help_icon('resources');
375 $select->set_label($strresourcelabel, array('class' => 'accesshide'));
376 $nonajaxcontrol .= $this->output
->render($select);
379 if (!empty($activities[MOD_CLASS_ACTIVITY
])) {
380 $select = new url_select($activities[MOD_CLASS_ACTIVITY
], '', array('' => $straddactivity), "section$section");
381 $select->set_help_icon('activities');
382 $select->set_label($stractivitylabel, array('class' => 'accesshide'));
383 $nonajaxcontrol .= $this->output
->render($select);
387 $nonajaxcontrol .= html_writer
::end_tag('div');
390 $nonajaxcontrol .= html_writer
::end_tag('div');
392 return $nonajaxcontrol;
396 * Renders html to display a course search form
398 * @param string $value default value to populate the search field
401 public function course_search_form($value = '') {
404 'action' => \core_search\manager
::get_course_search_url(),
405 'btnclass' => 'btn-primary',
407 'searchstring' => get_string('searchcourses'),
408 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
411 return $this->render_from_template('core/search_input', $data);
415 * Renders html for completion box on course page
417 * If completion is disabled, returns empty string
418 * If completion is automatic, returns an icon of the current completion state
419 * If completion is manual, returns a form (with an icon inside) that allows user to
422 * @deprecated since Moodle 3.11
423 * @todo MDL-71183 Final deprecation in Moodle 4.3.
424 * @see \core_renderer::activity_information
426 * @param stdClass $course course object
427 * @param completion_info $completioninfo completion info for the course, it is recommended
428 * to fetch once for all modules in course/section for performance
429 * @param cm_info $mod module to show completion for
430 * @param array $displayoptions display options, not used in core
433 public function course_section_cm_completion($course, &$completioninfo, cm_info
$mod, $displayoptions = array()) {
434 global $CFG, $DB, $USER;
436 debugging(__FUNCTION__
. ' is deprecated and is being replaced by the activity_information output component.',
441 $istrackeduser = $completioninfo->is_tracked_user($USER->id
);
442 $isediting = $this->page
->user_is_editing();
444 if (!empty($displayoptions['hidecompletion']) ||
!isloggedin() ||
isguestuser() ||
!$mod->uservisible
) {
447 if ($completioninfo === null) {
448 $completioninfo = new completion_info($course);
450 $completion = $completioninfo->is_enabled($mod);
452 if ($completion == COMPLETION_TRACKING_NONE
) {
454 $output .= html_writer
::span(' ', 'filler');
459 $completionicon = '';
461 if ($isediting ||
!$istrackeduser) {
462 switch ($completion) {
463 case COMPLETION_TRACKING_MANUAL
:
464 $completionicon = 'manual-enabled'; break;
465 case COMPLETION_TRACKING_AUTOMATIC
:
466 $completionicon = 'auto-enabled'; break;
469 $completiondata = $completioninfo->get_data($mod, true);
470 if ($completion == COMPLETION_TRACKING_MANUAL
) {
471 switch($completiondata->completionstate
) {
472 case COMPLETION_INCOMPLETE
:
473 $completionicon = 'manual-n' . ($completiondata->overrideby ?
'-override' : '');
475 case COMPLETION_COMPLETE
:
476 $completionicon = 'manual-y' . ($completiondata->overrideby ?
'-override' : '');
479 } else { // Automatic
480 switch($completiondata->completionstate
) {
481 case COMPLETION_INCOMPLETE
:
482 $completionicon = 'auto-n' . ($completiondata->overrideby ?
'-override' : '');
484 case COMPLETION_COMPLETE
:
485 $completionicon = 'auto-y' . ($completiondata->overrideby ?
'-override' : '');
487 case COMPLETION_COMPLETE_PASS
:
488 $completionicon = 'auto-pass'; break;
489 case COMPLETION_COMPLETE_FAIL
:
490 $completionicon = 'auto-fail'; break;
494 if ($completionicon) {
495 $formattedname = html_entity_decode($mod->get_formatted_name(), ENT_QUOTES
, 'UTF-8');
496 if (!$isediting && $istrackeduser && $completiondata->overrideby
) {
497 $args = new stdClass();
498 $args->modname
= $formattedname;
499 $overridebyuser = \core_user
::get_user($completiondata->overrideby
, '*', MUST_EXIST
);
500 $args->overrideuser
= fullname($overridebyuser);
501 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $args);
503 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
506 if ($isediting ||
!$istrackeduser ||
!has_capability('moodle/course:togglecompletion', $mod->context
)) {
507 // When editing, the icon is just an image.
508 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
509 array('title' => $imgalt, 'class' => 'iconsmall'));
510 $output .= html_writer
::tag('span', $this->output
->render($completionpixicon),
511 array('class' => 'autocompletion'));
512 } else if ($completion == COMPLETION_TRACKING_MANUAL
) {
514 $completiondata->completionstate
== COMPLETION_COMPLETE
515 ? COMPLETION_INCOMPLETE
516 : COMPLETION_COMPLETE
;
517 // In manual mode the icon is a toggle form...
519 // If this completion state is used by the
520 // conditional activities system, we need to turn
523 if (!empty($CFG->enableavailability
) &&
524 core_availability\info
::completion_value_used($course, $mod->id
)) {
525 $extraclass = ' preventjs';
527 $output .= html_writer
::start_tag('form', array('method' => 'post',
528 'action' => new moodle_url('/course/togglecompletion.php'),
529 'class' => 'togglecompletion'. $extraclass));
530 $output .= html_writer
::start_tag('div');
531 $output .= html_writer
::empty_tag('input', array(
532 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id
));
533 $output .= html_writer
::empty_tag('input', array(
534 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
535 $output .= html_writer
::empty_tag('input', array(
536 'type' => 'hidden', 'name' => 'modulename', 'value' => $formattedname));
537 $output .= html_writer
::empty_tag('input', array(
538 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
539 $output .= html_writer
::tag('button',
540 $this->output
->pix_icon('i/completion-' . $completionicon, $imgalt),
541 array('class' => 'btn btn-link', 'aria-live' => 'assertive'));
542 $output .= html_writer
::end_tag('div');
543 $output .= html_writer
::end_tag('form');
545 // In auto mode, the icon is just an image.
546 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
547 array('title' => $imgalt));
548 $output .= html_writer
::tag('span', $this->output
->render($completionpixicon),
549 array('class' => 'autocompletion'));
556 * Checks if course module has any conditions that may make it unavailable for
557 * all or some of the students
559 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
561 * @param cm_info $mod
564 public function is_cm_conditionally_hidden(cm_info
$mod) {
568 'is_cm_conditionally_hidden is deprecated. Use \core_availability\info_module::is_available_for_all instead',
572 $conditionalhidden = false;
573 if (!empty($CFG->enableavailability
)) {
574 $info = new \core_availability\
info_module($mod);
575 $conditionalhidden = !$info->is_available_for_all();
577 return $conditionalhidden;
581 * Renders html to display a name with the link to the course module on a course page
583 * If module is unavailable for user but still needs to be displayed
584 * in the list, just the name is returned without a link
586 * Note, that for course modules that never have separate pages (i.e. labels)
587 * this function return an empty string
589 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
591 * @param cm_info $mod
592 * @param array $displayoptions
595 public function course_section_cm_name(cm_info
$mod, $displayoptions = array()) {
597 'course_section_cm_name is deprecated. Use core_courseformat\\output\\local\\content\\cm\\cmname class instead.',
601 if (!$mod->is_visible_on_course_page() ||
!$mod->url
) {
602 // Nothing to be displayed to the user.
606 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
607 $groupinglabel = $mod->get_grouping_label($textclasses);
609 // Render element that allows to edit activity name inline.
610 $format = course_get_format($mod->course
);
611 $cmnameclass = $format->get_output_classname('content\\cm\\cmname');
612 // Mod inplace name editable.
613 $cmname = new $cmnameclass(
615 $mod->get_section_info(),
617 $this->page
->user_is_editing(),
621 $data = $cmname->export_for_template($this->output
);
623 return $this->output
->render_from_template('core/inplace_editable', $data) .
628 * Returns the CSS classes for the activity name/content
630 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
632 * For items which are hidden, unavailable or stealth but should be displayed
633 * to current user ($mod->is_visible_on_course_page()), we show those as dimmed.
634 * Students will also see as dimmed activities names that are not yet available
635 * but should still be displayed (without link) with availability info.
637 * @param cm_info $mod
638 * @return array array of two elements ($linkclasses, $textclasses)
640 protected function course_section_cm_classes(cm_info
$mod) {
643 'course_section_cm_classes is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
647 $format = course_get_format($mod->course
);
649 $cmclass = $format->get_output_classname('content\\cm');
650 $cmoutput = new $cmclass(
652 $mod->get_section_info(),
656 $cmoutput->get_link_classes(),
657 $cmoutput->get_text_classes(),
662 * Renders html to display a name with the link to the course module on a course page
664 * If module is unavailable for user but still needs to be displayed
665 * in the list, just the name is returned without a link
667 * Note, that for course modules that never have separate pages (i.e. labels)
668 * this function return an empty string
670 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
672 * @param cm_info $mod
673 * @param array $displayoptions
676 public function course_section_cm_name_title(cm_info
$mod, $displayoptions = array()) {
679 'course_section_cm_name_title is deprecated. Use core_courseformat\\output\\local\\cm\\title class instead.',
685 if (!$mod->is_visible_on_course_page() ||
!$url) {
686 // Nothing to be displayed to the user.
690 //Accessibility: for files get description via icon, this is very ugly hack!
691 $instancename = $mod->get_formatted_name();
692 $altname = $mod->modfullname
;
693 // Avoid unnecessary duplication: if e.g. a forum name already
694 // includes the word forum (or Forum, etc) then it is unhelpful
695 // to include that in the accessible description that is added.
696 if (false !== strpos(core_text
::strtolower($instancename),
697 core_text
::strtolower($altname))) {
700 // File type after name, for alphabetic lists (screen reader).
702 $altname = get_accesshide(' '.$altname);
705 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
707 // Get on-click attribute value if specified and decode the onclick - it
708 // has already been encoded for display (puke).
709 $onclick = htmlspecialchars_decode($mod->onclick
, ENT_QUOTES
);
711 // Display link itself.
712 $instancename = html_writer
::tag('span', $instancename . $altname, ['class' => 'instancename ml-1']);
714 $imageicon = html_writer
::empty_tag('img', ['src' => $mod->get_icon_url(),
715 'class' => 'activityicon', 'alt' => '', 'role' => 'presentation', 'aria-hidden' => 'true']);
716 $imageicon = html_writer
::tag('span', $imageicon, ['class' => 'activityiconcontainer courseicon']);
717 $activitylink = $imageicon . $instancename;
719 if ($mod->uservisible
) {
720 $output .= html_writer
::link($url, $activitylink, array('class' => 'aalink' . $linkclasses, 'onclick' => $onclick));
722 // We may be displaying this just in order to show information
723 // about visibility, without the actual link ($mod->is_visible_on_course_page()).
724 $output .= html_writer
::tag('div', $activitylink, array('class' => $textclasses));
730 * Renders html to display the module content on the course page (i.e. text of the labels)
732 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
734 * @param cm_info $mod
735 * @param array $displayoptions
738 public function course_section_cm_text(cm_info
$mod, $displayoptions = array()) {
741 'course_section_cm_text is deprecated. Now it is part of core_courseformat\\output\\local\\content\\cm ',
746 if (!$mod->is_visible_on_course_page()) {
747 // nothing to be displayed to the user
750 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
751 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod);
752 if ($mod->url
&& $mod->uservisible
) {
754 // If specified, display extra content after link.
755 $output = html_writer
::tag('div', $content, array('class' =>
756 trim('contentafterlink ' . $textclasses)));
759 $groupinglabel = $mod->get_grouping_label($textclasses);
761 // No link, so display only content.
762 $output = html_writer
::tag('div', $content . $groupinglabel,
763 array('class' => 'contentwithoutlink ' . $textclasses));
769 * Displays availability info for a course section or course module
771 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
772 * @param string $text
773 * @param string $additionalclasses
776 public function availability_info($text, $additionalclasses = '') {
779 'availability_info is deprecated. Use core_courseformat\\output\\local\\content\\section\\availability instead',
783 $data = ['text' => $text, 'classes' => $additionalclasses];
784 $additionalclasses = array_filter(explode(' ', $additionalclasses));
786 if (in_array('ishidden', $additionalclasses)) {
787 $data['ishidden'] = 1;
789 } else if (in_array('isstealth', $additionalclasses)) {
790 $data['isstealth'] = 1;
792 } else if (in_array('isrestricted', $additionalclasses)) {
793 $data['isrestricted'] = 1;
795 if (in_array('isfullinfo', $additionalclasses)) {
796 $data['isfullinfo'] = 1;
800 return $this->render_from_template('core/availability_info', $data);
804 * Renders HTML to show course module availability information (for someone who isn't allowed
805 * to see the activity itself, or for staff)
807 * @deprecated since Moodle 4.0 MDL-72656 - please do not use this function any more.
808 * @param cm_info $mod
809 * @param array $displayoptions
812 public function course_section_cm_availability(cm_info
$mod, $displayoptions = array()) {
815 'course_section_cm_availability is deprecated. Use core_courseformat\\output\\local\\content\\cm\\availability instead',
819 $format = course_get_format($mod->course
);
821 $availabilityclass = $format->get_output_classname('content\\cm\\availability');
822 $availability = new $availabilityclass(
824 $mod->get_section_info(),
828 $renderer = $format->get_renderer($this->page
);
829 $data = $availability->export_for_template($renderer);
830 return $data->info ??
'';
834 * Renders HTML to display one course module for display within a section.
836 * @deprecated since 4.0 - use core_course output components or course_format::course_section_updated_cm_item instead.
838 * This function calls:
839 * {@link core_course_renderer::course_section_cm()}
841 * @param stdClass $course
842 * @param completion_info $completioninfo
843 * @param cm_info $mod
844 * @param int|null $sectionreturn
845 * @param array $displayoptions
848 public function course_section_cm_list_item($course, &$completioninfo, cm_info
$mod, $sectionreturn, $displayoptions = []) {
851 'course_section_cm_list_item is deprecated. Use renderer course_section_updated_cm_item instead',
856 if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) {
857 $modclasses = 'activity ' . $mod->modname
. ' modtype_' . $mod->modname
. ' ' . $mod->extraclasses
;
858 $output .= html_writer
::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id
));
864 * Renders HTML to display one course module in a course section
866 * This includes link, content, availability, completion info and additional information
867 * that module type wants to display (i.e. number of unread forum posts)
869 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
871 * @param stdClass $course
872 * @param completion_info $completioninfo
873 * @param cm_info $mod
874 * @param int|null $sectionreturn
875 * @param array $displayoptions
878 public function course_section_cm($course, &$completioninfo, cm_info
$mod, $sectionreturn, $displayoptions = []) {
881 'course_section_cm is deprecated. Use core_courseformat\\output\\content\\cm output class instead.',
885 if (!$mod->is_visible_on_course_page()) {
889 $format = course_get_format($course);
890 $modinfo = $format->get_modinfo();
891 // Output renderers works only with real section_info objects.
892 if ($sectionreturn) {
893 $format->set_section_number($sectionreturn);
895 $section = $modinfo->get_section_info($format->get_section_number());
897 $cmclass = $format->get_output_classname('content\\cm');
898 $cm = new $cmclass($format, $section, $mod, $displayoptions);
899 // The course outputs works with format renderers, not with course renderers.
900 $renderer = $format->get_renderer($this->page
);
901 $data = $cm->export_for_template($renderer);
902 return $this->output
->render_from_template('core_courseformat/local/content/cm', $data);
906 * Message displayed to the user when they try to access unavailable activity following URL
908 * This method is a very simplified version of {@link course_section_cm()} to be part of the error
909 * notification only. It also does not check if module is visible on course page or not.
911 * The message will be displayed inside notification!
916 public function course_section_cm_unavailable_error_message(cm_info
$cm) {
917 if ($cm->uservisible
) {
920 if (!$cm->availableinfo
) {
921 return get_string('activityiscurrentlyhidden');
924 $altname = get_accesshide(' ' . $cm->modfullname
);
925 $name = html_writer
::empty_tag('img', array('src' => $cm->get_icon_url(),
926 'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) .
927 html_writer
::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename'));
928 $formattedinfo = \core_availability\info
::format_info($cm->availableinfo
, $cm->get_course());
929 return html_writer
::div($name, 'activityinstance-error') .
930 html_writer
::div($formattedinfo, 'availabilityinfo-error');
934 * Renders HTML to display a list of course modules in a course section
935 * Also displays "move here" controls in Javascript-disabled mode.
937 * @deprecated since 4.0 MDL-72656 - use core_course output components instead.
939 * This function calls {@link core_course_renderer::course_section_cm()}
941 * @param stdClass $course course object
942 * @param int|stdClass|section_info $section relative section number or section object
943 * @param int $sectionreturn section number to return to
944 * @param int $displayoptions
947 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = []) {
950 debugging('course_section_cm_list is deprecated. Use core_courseformat\\output\\local\\content\\section\\cmlist '.
951 'classes instead.', DEBUG_DEVELOPER
);
955 $format = course_get_format($course);
956 $modinfo = $format->get_modinfo();
958 if (is_object($section)) {
959 $section = $modinfo->get_section_info($section->section
);
961 $section = $modinfo->get_section_info($section);
963 $completioninfo = new completion_info($course);
965 // check if we are currently in the process of moving a module with JavaScript disabled
966 $ismoving = $format->show_editor() && ismoving($course->id
);
969 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
972 // Get the list of modules visible to user (excluding the module being moved if there is one)
974 if (!empty($modinfo->sections
[$section->section
])) {
975 foreach ($modinfo->sections
[$section->section
] as $modnumber) {
976 $mod = $modinfo->cms
[$modnumber];
978 if ($ismoving and $mod->id
== $USER->activitycopy
) {
979 // do not display moving mod
983 if ($modulehtml = $this->course_section_cm_list_item($course,
984 $completioninfo, $mod, $sectionreturn, $displayoptions)) {
985 $moduleshtml[$modnumber] = $modulehtml;
991 if (!empty($moduleshtml) ||
$ismoving) {
992 foreach ($moduleshtml as $modnumber => $modulehtml) {
994 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey()));
995 $sectionoutput .= html_writer
::tag('li',
996 html_writer
::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
997 array('class' => 'movehere'));
1000 $sectionoutput .= $modulehtml;
1004 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id
, 'sesskey' => sesskey()));
1005 $sectionoutput .= html_writer
::tag('li',
1006 html_writer
::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')),
1007 array('class' => 'movehere'));
1011 // Always output the section module list.
1012 $output .= html_writer
::tag('ul', $sectionoutput, array('class' => 'section img-text'));
1018 * Displays a custom list of courses with paging bar if necessary
1020 * If $paginationurl is specified but $totalcount is not, the link 'View more'
1021 * appears under the list.
1023 * If both $paginationurl and $totalcount are specified, and $totalcount is
1024 * bigger than count($courses), a paging bar is displayed above and under the
1027 * @param array $courses array of course records (or instances of core_course_list_element) to show on this page
1028 * @param bool $showcategoryname whether to add category name to the course description
1029 * @param string $additionalclasses additional CSS classes to add to the div.courses
1030 * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
1031 * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
1032 * @param int $page current page number (defaults to 0 referring to the first page)
1033 * @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
1036 public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) {
1038 // create instance of coursecat_helper to pass display options to function rendering courses list
1039 $chelper = new coursecat_helper();
1040 if ($showcategoryname) {
1041 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT
);
1043 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED
);
1045 if ($totalcount !== null && $paginationurl !== null) {
1046 // add options to display pagination
1047 if ($perpage === null) {
1048 $perpage = $CFG->coursesperpage
;
1050 $chelper->set_courses_display_options(array(
1051 'limit' => $perpage,
1052 'offset' => ((int)$page) * $perpage,
1053 'paginationurl' => $paginationurl,
1055 } else if ($paginationurl !== null) {
1056 // add options to display 'View more' link
1057 $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl));
1058 $totalcount = count($courses) +
1; // has to be bigger than count($courses) otherwise link will not be displayed
1060 $chelper->set_attributes(array('class' => $additionalclasses));
1061 $content = $this->coursecat_courses($chelper, $courses, $totalcount);
1066 * Returns HTML to display course name.
1068 * @param coursecat_helper $chelper
1069 * @param core_course_list_element $course
1072 protected function course_name(coursecat_helper
$chelper, core_course_list_element
$course): string {
1074 if ($chelper->get_show_courses() >= self
::COURSECAT_SHOW_COURSES_EXPANDED
) {
1079 $coursename = $chelper->get_course_formatted_name($course);
1080 $coursenamelink = html_writer
::link(new moodle_url('/course/view.php', ['id' => $course->id
]),
1081 $coursename, ['class' => $course->visible ?
'aalink' : 'aalink dimmed']);
1082 $content .= html_writer
::tag($nametag, $coursenamelink, ['class' => 'coursename']);
1083 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
1084 $content .= html_writer
::start_tag('div', ['class' => 'moreinfo']);
1085 if ($chelper->get_show_courses() < self
::COURSECAT_SHOW_COURSES_EXPANDED
) {
1086 if ($course->has_summary() ||
$course->has_course_contacts() ||
$course->has_course_overviewfiles()
1087 ||
$course->has_custom_fields()) {
1088 $url = new moodle_url('/course/info.php', ['id' => $course->id
]);
1089 $image = $this->output
->pix_icon('i/info', $this->strings
->summary
);
1090 $content .= html_writer
::link($url, $image, ['title' => $this->strings
->summary
]);
1091 // Make sure JS file to expand course content is included.
1092 $this->coursecat_include_js();
1095 $content .= html_writer
::end_tag('div');
1100 * Returns HTML to display course enrolment icons.
1102 * @param core_course_list_element $course
1105 protected function course_enrolment_icons(core_course_list_element
$course): string {
1107 if ($icons = enrol_get_course_info_icons($course)) {
1108 $content .= html_writer
::start_tag('div', ['class' => 'enrolmenticons']);
1109 foreach ($icons as $icon) {
1110 $content .= $this->render($icon);
1112 $content .= html_writer
::end_tag('div');
1118 * Displays one course in the list of courses.
1120 * This is an internal function, to display an information about just one course
1121 * please use {@link core_course_renderer::course_info_box()}
1123 * @param coursecat_helper $chelper various display options
1124 * @param core_course_list_element|stdClass $course
1125 * @param string $additionalclasses additional classes to add to the main <div> tag (usually
1126 * depend on the course position in list - first/last/even/odd)
1129 protected function coursecat_coursebox(coursecat_helper
$chelper, $course, $additionalclasses = '') {
1130 if (!isset($this->strings
->summary
)) {
1131 $this->strings
->summary
= get_string('summary');
1133 if ($chelper->get_show_courses() <= self
::COURSECAT_SHOW_COURSES_COUNT
) {
1136 if ($course instanceof stdClass
) {
1137 $course = new core_course_list_element($course);
1140 $classes = trim('coursebox clearfix '. $additionalclasses);
1141 if ($chelper->get_show_courses() < self
::COURSECAT_SHOW_COURSES_EXPANDED
) {
1142 $classes .= ' collapsed';
1146 $content .= html_writer
::start_tag('div', array(
1147 'class' => $classes,
1148 'data-courseid' => $course->id
,
1149 'data-type' => self
::COURSECAT_TYPE_COURSE
,
1152 $content .= html_writer
::start_tag('div', array('class' => 'info'));
1153 $content .= $this->course_name($chelper, $course);
1154 $content .= $this->course_enrolment_icons($course);
1155 $content .= html_writer
::end_tag('div');
1157 $content .= html_writer
::start_tag('div', array('class' => 'content'));
1158 $content .= $this->coursecat_coursebox_content($chelper, $course);
1159 $content .= html_writer
::end_tag('div');
1161 $content .= html_writer
::end_tag('div'); // .coursebox
1166 * Returns HTML to display course summary.
1168 * @param coursecat_helper $chelper
1169 * @param core_course_list_element $course
1172 protected function course_summary(coursecat_helper
$chelper, core_course_list_element
$course): string {
1174 if ($course->has_summary()) {
1175 $content .= html_writer
::start_tag('div', ['class' => 'summary']);
1176 $content .= $chelper->get_course_formatted_summary($course,
1177 array('overflowdiv' => true, 'noclean' => true, 'para' => false));
1178 $content .= html_writer
::end_tag('div');
1184 * Returns HTML to display course contacts.
1186 * @param core_course_list_element $course
1189 protected function course_contacts(core_course_list_element
$course) {
1191 if ($course->has_course_contacts()) {
1192 $content .= html_writer
::start_tag('ul', ['class' => 'teachers']);
1193 foreach ($course->get_course_contacts() as $coursecontact) {
1194 $rolenames = array_map(function ($role) {
1195 return $role->displayname
;
1196 }, $coursecontact['roles']);
1197 $name = html_writer
::tag('span', implode(", ", $rolenames).': ', ['class' => 'font-weight-bold']);
1198 $name .= html_writer
::link(new moodle_url('/user/view.php',
1199 ['id' => $coursecontact['user']->id
, 'course' => SITEID
]),
1200 $coursecontact['username']);
1201 $content .= html_writer
::tag('li', $name);
1203 $content .= html_writer
::end_tag('ul');
1209 * Returns HTML to display course overview files.
1211 * @param core_course_list_element $course
1214 protected function course_overview_files(core_course_list_element
$course): string {
1217 $contentimages = $contentfiles = '';
1218 foreach ($course->get_course_overviewfiles() as $file) {
1219 $isimage = $file->is_valid_image();
1220 $url = moodle_url
::make_file_url("$CFG->wwwroot/pluginfile.php",
1221 '/' . $file->get_contextid() . '/' . $file->get_component() . '/' .
1222 $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
1224 $contentimages .= html_writer
::tag('div',
1225 html_writer
::empty_tag('img', ['src' => $url]),
1226 ['class' => 'courseimage']);
1228 $image = $this->output
->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
1229 $filename = html_writer
::tag('span', $image, ['class' => 'fp-icon']).
1230 html_writer
::tag('span', $file->get_filename(), ['class' => 'fp-filename']);
1231 $contentfiles .= html_writer
::tag('span',
1232 html_writer
::link($url, $filename),
1233 ['class' => 'coursefile fp-filename-icon']);
1236 return $contentimages . $contentfiles;
1240 * Returns HTML to display course category name.
1242 * @param coursecat_helper $chelper
1243 * @param core_course_list_element $course
1246 protected function course_category_name(coursecat_helper
$chelper, core_course_list_element
$course): string {
1248 // Display course category if necessary (for example in search results).
1249 if ($chelper->get_show_courses() == self
::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT
) {
1250 if ($cat = core_course_category
::get($course->category
, IGNORE_MISSING
)) {
1251 $content .= html_writer
::start_tag('div', ['class' => 'coursecat']);
1252 $content .= html_writer
::tag('span', get_string('category').': ', ['class' => 'font-weight-bold']);
1253 $content .= html_writer
::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id
]),
1254 $cat->get_formatted_name(), ['class' => $cat->visible ?
'' : 'dimmed']);
1255 $content .= html_writer
::end_tag('div');
1262 * Returns HTML to display course custom fields.
1264 * @param core_course_list_element $course
1267 protected function course_custom_fields(core_course_list_element
$course): string {
1269 if ($course->has_custom_fields()) {
1270 $handler = core_course\customfield\course_handler
::create();
1271 $customfields = $handler->display_custom_fields_data($course->get_custom_fields());
1272 $content .= \html_writer
::tag('div', $customfields, ['class' => 'customfields-container']);
1278 * Returns HTML to display course content (summary, course contacts and optionally category name)
1280 * This method is called from coursecat_coursebox() and may be re-used in AJAX
1282 * @param coursecat_helper $chelper various display options
1283 * @param stdClass|core_course_list_element $course
1286 protected function coursecat_coursebox_content(coursecat_helper
$chelper, $course) {
1287 if ($chelper->get_show_courses() < self
::COURSECAT_SHOW_COURSES_EXPANDED
) {
1290 if ($course instanceof stdClass
) {
1291 $course = new core_course_list_element($course);
1293 $content = \html_writer
::start_tag('div', ['class' => 'd-flex']);
1294 $content .= $this->course_overview_files($course);
1295 $content .= \html_writer
::start_tag('div', ['class' => 'flex-grow-1']);
1296 $content .= $this->course_summary($chelper, $course);
1297 $content .= $this->course_contacts($course);
1298 $content .= $this->course_category_name($chelper, $course);
1299 $content .= $this->course_custom_fields($course);
1300 $content .= \html_writer
::end_tag('div');
1301 $content .= \html_writer
::end_tag('div');
1306 * Renders the list of courses
1308 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
1309 * method from outside of the class
1311 * If list of courses is specified in $courses; the argument $chelper is only used
1312 * to retrieve display options and attributes, only methods get_show_courses(),
1313 * get_courses_display_option() and get_and_erase_attributes() are called.
1315 * @param coursecat_helper $chelper various display options
1316 * @param array $courses the list of courses to display
1317 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
1318 * defaulted to count($courses)
1321 protected function coursecat_courses(coursecat_helper
$chelper, $courses, $totalcount = null) {
1323 if ($totalcount === null) {
1324 $totalcount = count($courses);
1327 // Courses count is cached during courses retrieval.
1331 if ($chelper->get_show_courses() == self
::COURSECAT_SHOW_COURSES_AUTO
) {
1332 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
1333 if ($totalcount <= $CFG->courseswithsummarieslimit
) {
1334 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED
);
1336 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_COLLAPSED
);
1340 // prepare content of paging bar if it is needed
1341 $paginationurl = $chelper->get_courses_display_option('paginationurl');
1342 $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
1343 if ($totalcount > count($courses)) {
1344 // there are more results that can fit on one page
1345 if ($paginationurl) {
1346 // the option paginationurl was specified, display pagingbar
1347 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage
);
1348 $page = $chelper->get_courses_display_option('offset') / $perpage;
1349 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1350 $paginationurl->out(false, array('perpage' => $perpage)));
1351 if ($paginationallowall) {
1352 $pagingbar .= html_writer
::tag('div', html_writer
::link($paginationurl->out(false, array('perpage' => 'all')),
1353 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1355 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1356 // the option for 'View more' link was specified, display more link
1357 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
1358 $morelink = html_writer
::tag(
1360 html_writer
::link($viewmoreurl, $viewmoretext, ['class' => 'btn btn-secondary']),
1361 ['class' => 'paging paging-morelink']
1364 } else if (($totalcount > $CFG->coursesperpage
) && $paginationurl && $paginationallowall) {
1365 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1366 $pagingbar = html_writer
::tag('div', html_writer
::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage
)),
1367 get_string('showperpage', '', $CFG->coursesperpage
)), array('class' => 'paging paging-showperpage'));
1370 // display list of courses
1371 $attributes = $chelper->get_and_erase_attributes('courses');
1372 $content = html_writer
::start_tag('div', $attributes);
1374 if (!empty($pagingbar)) {
1375 $content .= $pagingbar;
1379 foreach ($courses as $course) {
1381 $classes = ($coursecount%2
) ?
'odd' : 'even';
1382 if ($coursecount == 1) {
1383 $classes .= ' first';
1385 if ($coursecount >= count($courses)) {
1386 $classes .= ' last';
1388 $content .= $this->coursecat_coursebox($chelper, $course, $classes);
1391 if (!empty($pagingbar)) {
1392 $content .= $pagingbar;
1394 if (!empty($morelink)) {
1395 $content .= $morelink;
1398 $content .= html_writer
::end_tag('div'); // .courses
1403 * Renders the list of subcategories in a category
1405 * @param coursecat_helper $chelper various display options
1406 * @param core_course_category $coursecat
1407 * @param int $depth depth of the category in the current tree
1410 protected function coursecat_subcategories(coursecat_helper
$chelper, $coursecat, $depth) {
1412 $subcategories = array();
1413 if (!$chelper->get_categories_display_option('nodisplay')) {
1414 $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
1416 $totalcount = $coursecat->get_children_count();
1418 // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children()
1419 // to avoid extra DB requests.
1420 // Categories count is cached during children categories retrieval.
1424 // prepare content of paging bar or more link if it is needed
1425 $paginationurl = $chelper->get_categories_display_option('paginationurl');
1426 $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
1427 if ($totalcount > count($subcategories)) {
1428 if ($paginationurl) {
1429 // the option 'paginationurl was specified, display pagingbar
1430 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage
);
1431 $page = $chelper->get_categories_display_option('offset') / $perpage;
1432 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1433 $paginationurl->out(false, array('perpage' => $perpage)));
1434 if ($paginationallowall) {
1435 $pagingbar .= html_writer
::tag('div', html_writer
::link($paginationurl->out(false, array('perpage' => 'all')),
1436 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1438 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
1439 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
1440 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE
)) {
1441 $viewmoreurl->param('categoryid', $coursecat->id
);
1443 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
1444 $morelink = html_writer
::tag('div', html_writer
::link($viewmoreurl, $viewmoretext),
1445 array('class' => 'paging paging-morelink'));
1447 } else if (($totalcount > $CFG->coursesperpage
) && $paginationurl && $paginationallowall) {
1448 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1449 $pagingbar = html_writer
::tag('div', html_writer
::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage
)),
1450 get_string('showperpage', '', $CFG->coursesperpage
)), array('class' => 'paging paging-showperpage'));
1453 // display list of subcategories
1454 $content = html_writer
::start_tag('div', array('class' => 'subcategories'));
1456 if (!empty($pagingbar)) {
1457 $content .= $pagingbar;
1460 foreach ($subcategories as $subcategory) {
1461 $content .= $this->coursecat_category($chelper, $subcategory, $depth +
1);
1464 if (!empty($pagingbar)) {
1465 $content .= $pagingbar;
1467 if (!empty($morelink)) {
1468 $content .= $morelink;
1471 $content .= html_writer
::end_tag('div');
1476 * Make sure that javascript file for AJAX expanding of courses and categories content is included
1478 protected function coursecat_include_js() {
1479 if (!$this->page
->requires
->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) {
1483 // We must only load this module once.
1484 $this->page
->requires
->yui_module('moodle-course-categoryexpander',
1485 'Y.Moodle.course.categoryexpander.init');
1489 * Returns HTML to display the subcategories and courses in the given category
1491 * This method is re-used by AJAX to expand content of not loaded category
1493 * @param coursecat_helper $chelper various display options
1494 * @param core_course_category $coursecat
1495 * @param int $depth depth of the category in the current tree
1498 protected function coursecat_category_content(coursecat_helper
$chelper, $coursecat, $depth) {
1501 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
1503 // AUTO show courses: Courses will be shown expanded if this is not nested category,
1504 // and number of courses no bigger than $CFG->courseswithsummarieslimit.
1505 $showcoursesauto = $chelper->get_show_courses() == self
::COURSECAT_SHOW_COURSES_AUTO
;
1506 if ($showcoursesauto && $depth) {
1507 // this is definitely collapsed mode
1508 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_COLLAPSED
);
1512 if ($chelper->get_show_courses() > core_course_renderer
::COURSECAT_SHOW_COURSES_COUNT
) {
1514 if (!$chelper->get_courses_display_option('nodisplay')) {
1515 $courses = $coursecat->get_courses($chelper->get_courses_display_options());
1517 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1518 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
1519 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE
)) {
1520 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id
)));
1523 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
1526 if ($showcoursesauto) {
1527 // restore the show_courses back to AUTO
1528 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_AUTO
);
1535 * Returns HTML to display a course category as a part of a tree
1537 * This is an internal function, to display a particular category and all its contents
1538 * use {@link core_course_renderer::course_category()}
1540 * @param coursecat_helper $chelper various display options
1541 * @param core_course_category $coursecat
1542 * @param int $depth depth of this category in the current tree
1545 protected function coursecat_category(coursecat_helper
$chelper, $coursecat, $depth) {
1546 // open category tag
1547 $classes = array('category');
1548 if (empty($coursecat->visible
)) {
1549 $classes[] = 'dimmed_category';
1551 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
1552 // do not load content
1553 $categorycontent = '';
1554 $classes[] = 'notloaded';
1555 if ($coursecat->get_children_count() ||
1556 ($chelper->get_show_courses() >= self
::COURSECAT_SHOW_COURSES_COLLAPSED
&& $coursecat->get_courses_count())) {
1557 $classes[] = 'with_children';
1558 $classes[] = 'collapsed';
1561 // load category content
1562 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
1563 $classes[] = 'loaded';
1564 if (!empty($categorycontent)) {
1565 $classes[] = 'with_children';
1566 // Category content loaded with children.
1567 $this->categoryexpandedonload
= true;
1571 // Make sure JS file to expand category content is included.
1572 $this->coursecat_include_js();
1574 $content = html_writer
::start_tag('div', array(
1575 'class' => join(' ', $classes),
1576 'data-categoryid' => $coursecat->id
,
1577 'data-depth' => $depth,
1578 'data-showcourses' => $chelper->get_show_courses(),
1579 'data-type' => self
::COURSECAT_TYPE_CATEGORY
,
1583 $categoryname = $coursecat->get_formatted_name();
1584 $categoryname = html_writer
::link(new moodle_url('/course/index.php',
1585 array('categoryid' => $coursecat->id
)),
1587 if ($chelper->get_show_courses() == self
::COURSECAT_SHOW_COURSES_COUNT
1588 && ($coursescount = $coursecat->get_courses_count())) {
1589 $categoryname .= html_writer
::tag('span', ' ('. $coursescount.')',
1590 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
1592 $content .= html_writer
::start_tag('div', array('class' => 'info'));
1594 $content .= html_writer
::tag(($depth > 1) ?
'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn'));
1595 $content .= html_writer
::end_tag('div'); // .info
1597 // add category content to the output
1598 $content .= html_writer
::tag('div', $categorycontent, array('class' => 'content'));
1600 $content .= html_writer
::end_tag('div'); // .category
1602 // Return the course category tree HTML
1607 * Returns HTML to display a tree of subcategories and courses in the given category
1609 * @param coursecat_helper $chelper various display options
1610 * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree)
1613 protected function coursecat_tree(coursecat_helper
$chelper, $coursecat) {
1614 // Reset the category expanded flag for this course category tree first.
1615 $this->categoryexpandedonload
= false;
1616 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1617 if (empty($categorycontent)) {
1621 // Start content generation
1623 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1624 $content .= html_writer
::start_tag('div', $attributes);
1626 if ($coursecat->get_children_count()) {
1628 'collapseexpand', 'aabtn'
1631 // Check if the category content contains subcategories with children's content loaded.
1632 if ($this->categoryexpandedonload
) {
1633 $classes[] = 'collapse-all';
1634 $linkname = get_string('collapseall');
1636 $linkname = get_string('expandall');
1639 // Only show the collapse/expand if there are children to expand.
1640 $content .= html_writer
::start_tag('div', array('class' => 'collapsible-actions'));
1641 $content .= html_writer
::link('#', $linkname, array('class' => implode(' ', $classes)));
1642 $content .= html_writer
::end_tag('div');
1643 $this->page
->requires
->strings_for_js(array('collapseall', 'expandall'), 'moodle');
1646 $content .= html_writer
::tag('div', $categorycontent, array('class' => 'content'));
1648 $content .= html_writer
::end_tag('div'); // .course_category_tree
1654 * Renders HTML to display particular course category - list of it's subcategories and courses
1656 * Invoked from /course/index.php
1658 * @param int|stdClass|core_course_category $category
1660 public function course_category($category) {
1662 $usertop = core_course_category
::user_top();
1663 if (empty($category)) {
1664 $coursecat = $usertop;
1665 } else if (is_object($category) && $category instanceof core_course_category
) {
1666 $coursecat = $category;
1668 $coursecat = core_course_category
::get(is_object($category) ?
$category->id
: $category);
1673 if ($coursecat->can_create_course() ||
$coursecat->has_manage_capability()) {
1674 // Add 'Manage' button if user has permissions to edit this category.
1675 $managebutton = $this->single_button(new moodle_url('/course/management.php',
1676 array('categoryid' => $coursecat->id
)), get_string('managecourses'), 'get');
1677 $this->page
->set_button($managebutton);
1680 if (core_course_category
::is_simple_site()) {
1681 // There is only one category in the system, do not display link to it.
1682 $strfulllistofcourses = get_string('fulllistofcourses');
1683 $this->page
->set_title("$site->shortname: $strfulllistofcourses");
1684 } else if (!$coursecat->id ||
!$coursecat->is_uservisible()) {
1685 $strcategories = get_string('categories');
1686 $this->page
->set_title("$site->shortname: $strcategories");
1688 $strfulllistofcourses = get_string('fulllistofcourses');
1689 $this->page
->set_title("$site->shortname: $strfulllistofcourses");
1691 // Print the category selector
1692 $categorieslist = core_course_category
::make_categories_list();
1693 if (count($categorieslist) > 1) {
1694 $output .= html_writer
::start_tag('div', array('class' => 'categorypicker'));
1695 $select = new single_select(new moodle_url('/course/index.php'), 'categoryid',
1696 core_course_category
::make_categories_list(), $coursecat->id
, null, 'switchcategory');
1697 $select->set_label(get_string('categories').':', ['class' => 'sr-only']);
1698 $output .= $this->render($select);
1699 $output .= html_writer
::end_tag('div'); // .categorypicker
1703 // Print current category description
1704 $chelper = new coursecat_helper();
1705 if ($description = $chelper->get_category_formatted_description($coursecat)) {
1706 $output .= $this->box($description, array('class' => 'generalbox info'));
1709 // Prepare parameters for courses and categories lists in the tree
1710 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_AUTO
)
1711 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id
));
1713 $coursedisplayoptions = array();
1714 $catdisplayoptions = array();
1715 $browse = optional_param('browse', null, PARAM_ALPHA
);
1716 $perpage = optional_param('perpage', $CFG->coursesperpage
, PARAM_INT
);
1717 $page = optional_param('page', 0, PARAM_INT
);
1718 $baseurl = new moodle_url('/course/index.php');
1719 if ($coursecat->id
) {
1720 $baseurl->param('categoryid', $coursecat->id
);
1722 if ($perpage != $CFG->coursesperpage
) {
1723 $baseurl->param('perpage', $perpage);
1725 $coursedisplayoptions['limit'] = $perpage;
1726 $catdisplayoptions['limit'] = $perpage;
1727 if ($browse === 'courses' ||
!$coursecat->get_children_count()) {
1728 $coursedisplayoptions['offset'] = $page * $perpage;
1729 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1730 $catdisplayoptions['nodisplay'] = true;
1731 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1732 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories');
1733 } else if ($browse === 'categories' ||
!$coursecat->get_courses_count()) {
1734 $coursedisplayoptions['nodisplay'] = true;
1735 $catdisplayoptions['offset'] = $page * $perpage;
1736 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories'));
1737 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses'));
1738 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1740 // we have a category that has both subcategories and courses, display pagination separately
1741 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1742 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1744 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1745 // Add course search form.
1746 $output .= $this->course_search_form();
1748 // Display course category tree.
1749 $output .= $this->coursecat_tree($chelper, $coursecat);
1751 // Add action buttons
1752 $output .= $this->container_start('buttons mt-3');
1753 if ($coursecat->is_uservisible()) {
1754 $context = get_category_or_system_context($coursecat->id
);
1755 if (has_capability('moodle/course:create', $context)) {
1756 // Print link to create a new course, for the 1st available category.
1757 if ($coursecat->id
) {
1758 $url = new moodle_url('/course/edit.php', array('category' => $coursecat->id
, 'returnto' => 'category'));
1760 $url = new moodle_url('/course/edit.php',
1761 array('category' => $CFG->defaultrequestcategory
, 'returnto' => 'topcat'));
1763 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
1766 print_course_request_buttons($context);
1767 $output .= ob_get_contents();
1770 $output .= $this->container_end();
1776 * Serves requests to /course/category.ajax.php
1778 * In this renderer implementation it may expand the category content or
1782 * @throws coding_exception
1784 public function coursecat_ajax() {
1787 $type = required_param('type', PARAM_INT
);
1789 if ($type === self
::COURSECAT_TYPE_CATEGORY
) {
1790 // This is a request for a category list of some kind.
1791 $categoryid = required_param('categoryid', PARAM_INT
);
1792 $showcourses = required_param('showcourses', PARAM_INT
);
1793 $depth = required_param('depth', PARAM_INT
);
1795 $category = core_course_category
::get($categoryid);
1797 $chelper = new coursecat_helper();
1798 $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid));
1799 $coursedisplayoptions = array(
1800 'limit' => $CFG->coursesperpage
,
1801 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1))
1803 $catdisplayoptions = array(
1804 'limit' => $CFG->coursesperpage
,
1805 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1))
1807 $chelper->set_show_courses($showcourses)->
1808 set_courses_display_options($coursedisplayoptions)->
1809 set_categories_display_options($catdisplayoptions);
1811 return $this->coursecat_category_content($chelper, $category, $depth);
1812 } else if ($type === self
::COURSECAT_TYPE_COURSE
) {
1813 // This is a request for the course information.
1814 $courseid = required_param('courseid', PARAM_INT
);
1816 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
1818 $chelper = new coursecat_helper();
1819 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED
);
1820 return $this->coursecat_coursebox_content($chelper, $course);
1822 throw new coding_exception('Invalid request type');
1827 * Renders html to display search result page
1829 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1832 public function search_courses($searchcriteria) {
1837 if (!empty($searchcriteria['search'])) {
1838 $search = $searchcriteria['search'];
1840 $content .= $this->course_search_form($search);
1842 if (!empty($searchcriteria)) {
1843 // print search results
1845 $displayoptions = array('sort' => array('displayname' => 1));
1846 // take the current page and number of results per page from query
1847 $perpage = optional_param('perpage', 0, PARAM_RAW
);
1848 if ($perpage !== 'all') {
1849 $displayoptions['limit'] = ((int)$perpage <= 0) ?
$CFG->coursesperpage
: (int)$perpage;
1850 $page = optional_param('page', 0, PARAM_INT
);
1851 $displayoptions['offset'] = $displayoptions['limit'] * $page;
1853 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1854 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1855 $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1857 $class = 'course-search-result';
1858 foreach ($searchcriteria as $key => $value) {
1859 if (!empty($value)) {
1860 $class .= ' course-search-result-'. $key;
1863 $chelper = new coursecat_helper();
1864 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT
)->
1865 set_courses_display_options($displayoptions)->
1866 set_search_criteria($searchcriteria)->
1867 set_attributes(array('class' => $class));
1869 $courses = core_course_category
::search_courses($searchcriteria, $chelper->get_courses_display_options());
1870 $totalcount = core_course_category
::search_courses_count($searchcriteria);
1871 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1874 if (!empty($searchcriteria['search'])) {
1875 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1877 $content .= $this->heading(get_string('novalidcourses'));
1880 $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1881 $content .= $courseslist;
1888 * Renders html to print list of courses tagged with particular tag
1890 * @param int $tagid id of the tag
1891 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1892 * are displayed on the page and the per-page limit may be bigger
1893 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1894 * to display items in the same context first
1895 * @param int $ctx context id where to search for records
1896 * @param bool $rec search in subcontexts as well
1897 * @param array $displayoptions
1898 * @return string empty string if no courses are marked with this tag or rendered list of courses
1900 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) {
1902 if (empty($displayoptions)) {
1903 $displayoptions = array();
1905 $showcategories = !core_course_category
::is_simple_site();
1906 $displayoptions +
= array('limit' => $CFG->coursesperpage
, 'offset' => 0);
1907 $chelper = new coursecat_helper();
1908 $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec);
1909 $chelper->set_show_courses($showcategories ? self
::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT
:
1910 self
::COURSECAT_SHOW_COURSES_EXPANDED
)->
1911 set_search_criteria($searchcriteria)->
1912 set_courses_display_options($displayoptions)->
1913 set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1914 // (we set the same css class as in search results by tagid)
1915 if ($totalcount = core_course_category
::search_courses_count($searchcriteria)) {
1916 $courses = core_course_category
::search_courses($searchcriteria, $chelper->get_courses_display_options());
1917 if ($exclusivemode) {
1918 return $this->coursecat_courses($chelper, $courses, $totalcount);
1920 $tagfeed = new core_tag\output\tagfeed
();
1921 $img = $this->output
->pix_icon('i/course', '');
1922 foreach ($courses as $course) {
1923 $url = course_get_url($course);
1924 $imgwithlink = html_writer
::link($url, $img);
1925 $coursename = html_writer
::link($url, $course->get_formatted_name());
1927 if ($showcategories && ($cat = core_course_category
::get($course->category
, IGNORE_MISSING
))) {
1928 $details = get_string('category').': '.
1929 html_writer
::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id
)),
1930 $cat->get_formatted_name(), array('class' => $cat->visible ?
'' : 'dimmed'));
1932 $tagfeed->add($imgwithlink, $coursename, $details);
1934 return $this->output
->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output
));
1941 * Returns HTML to display one remote course
1943 * @param stdClass $course remote course information, contains properties:
1944 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1947 protected function frontpage_remote_course(stdClass
$course) {
1948 $url = new moodle_url('/auth/mnet/jump.php', array(
1949 'hostid' => $course->hostid
,
1950 'wantsurl' => '/course/view.php?id='. $course->remoteid
1954 $output .= html_writer
::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1955 $output .= html_writer
::start_tag('div', array('class' => 'info'));
1956 $output .= html_writer
::start_tag('h3', array('class' => 'coursename'));
1957 $output .= html_writer
::link($url, format_string($course->fullname
), array('title' => get_string('entercourse')));
1958 $output .= html_writer
::end_tag('h3'); // .name
1959 $output .= html_writer
::tag('div', '', array('class' => 'moreinfo'));
1960 $output .= html_writer
::end_tag('div'); // .info
1961 $output .= html_writer
::start_tag('div', array('class' => 'content'));
1962 $output .= html_writer
::start_tag('div', array('class' => 'summary'));
1963 $options = new stdClass();
1964 $options->noclean
= true;
1965 $options->para
= false;
1966 $options->overflowdiv
= true;
1967 $output .= format_text($course->summary
, $course->summaryformat
, $options);
1968 $output .= html_writer
::end_tag('div'); // .summary
1969 $addinfo = format_string($course->hostname
) . ' : '
1970 . format_string($course->cat_name
) . ' : '
1971 . format_string($course->shortname
);
1972 $output .= html_writer
::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1973 $output .= html_writer
::end_tag('div'); // .content
1974 $output .= html_writer
::end_tag('div'); // .coursebox
1979 * Returns HTML to display one remote host
1981 * @param array $host host information, contains properties: name, url, count
1984 protected function frontpage_remote_host($host) {
1986 $output .= html_writer
::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1987 $output .= html_writer
::start_tag('div', array('class' => 'info'));
1988 $output .= html_writer
::start_tag('h3', array('class' => 'name'));
1989 $output .= html_writer
::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1990 $output .= html_writer
::end_tag('h3'); // .name
1991 $output .= html_writer
::tag('div', '', array('class' => 'moreinfo'));
1992 $output .= html_writer
::end_tag('div'); // .info
1993 $output .= html_writer
::start_tag('div', array('class' => 'content'));
1994 $output .= html_writer
::start_tag('div', array('class' => 'summary'));
1995 $output .= $host['count'] . ' ' . get_string('courses');
1996 $output .= html_writer
::end_tag('div'); // .content
1997 $output .= html_writer
::end_tag('div'); // .coursebox
2002 * Returns HTML to print list of courses user is enrolled to for the frontpage
2004 * Also lists remote courses or remote hosts if MNET authorisation is used
2008 public function frontpage_my_courses() {
2009 global $USER, $CFG, $DB;
2011 if (!isloggedin() or isguestuser()) {
2016 $courses = enrol_get_my_courses('summary, summaryformat');
2018 $rcourses = array();
2019 if (!empty($CFG->mnet_dispatcher_mode
) && $CFG->mnet_dispatcher_mode
==='strict') {
2020 $rcourses = get_my_remotecourses($USER->id
);
2021 $rhosts = get_my_remotehosts();
2024 if (!empty($courses) ||
!empty($rcourses) ||
!empty($rhosts)) {
2026 $chelper = new coursecat_helper();
2027 $totalcount = count($courses);
2028 if (count($courses) > $CFG->frontpagecourselimit
) {
2029 // There are more enrolled courses than we can display, display link to 'My courses'.
2030 $courses = array_slice($courses, 0, $CFG->frontpagecourselimit
, true);
2031 $chelper->set_courses_display_options(array(
2032 'viewmoreurl' => new moodle_url('/my/'),
2033 'viewmoretext' => new lang_string('mycourses')
2035 } else if (core_course_category
::top()->is_uservisible()) {
2036 // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system.
2037 $chelper->set_courses_display_options(array(
2038 'viewmoreurl' => new moodle_url('/course/index.php'),
2039 'viewmoretext' => new lang_string('fulllistofcourses')
2041 $totalcount = $DB->count_records('course') - 1;
2043 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED
)->
2044 set_attributes(array('class' => 'frontpage-course-list-enrolled'));
2045 $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
2048 if (!empty($rcourses)) {
2049 // at the IDP, we know of all the remote courses
2050 $output .= html_writer
::start_tag('div', array('class' => 'courses'));
2051 foreach ($rcourses as $course) {
2052 $output .= $this->frontpage_remote_course($course);
2054 $output .= html_writer
::end_tag('div'); // .courses
2055 } elseif (!empty($rhosts)) {
2056 // non-IDP, we know of all the remote servers, but not courses
2057 $output .= html_writer
::start_tag('div', array('class' => 'courses'));
2058 foreach ($rhosts as $host) {
2059 $output .= $this->frontpage_remote_host($host);
2061 $output .= html_writer
::end_tag('div'); // .courses
2068 * Returns HTML to print list of available courses for the frontpage
2072 public function frontpage_available_courses() {
2075 $chelper = new coursecat_helper();
2076 $chelper->set_show_courses(self
::COURSECAT_SHOW_COURSES_EXPANDED
)->
2077 set_courses_display_options(array(
2078 'recursive' => true,
2079 'limit' => $CFG->frontpagecourselimit
,
2080 'viewmoreurl' => new moodle_url('/course/index.php'),
2081 'viewmoretext' => new lang_string('fulllistofcourses')));
2083 $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
2084 $courses = core_course_category
::top()->get_courses($chelper->get_courses_display_options());
2085 $totalcount = core_course_category
::top()->get_courses_count($chelper->get_courses_display_options());
2086 if (!$totalcount && !$this->page
->user_is_editing() && has_capability('moodle/course:create', context_system
::instance())) {
2087 // Print link to create a new course, for the 1st available category.
2088 return $this->add_new_course_button();
2090 return $this->coursecat_courses($chelper, $courses, $totalcount);
2094 * Returns HTML to the "add new course" button for the page
2098 public function add_new_course_button() {
2100 // Print link to create a new course, for the 1st available category.
2101 $output = $this->container_start('buttons');
2102 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory
, 'returnto' => 'topcat'));
2103 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
2104 $output .= $this->container_end('buttons');
2109 * Returns HTML to print tree with course categories and courses for the frontpage
2113 public function frontpage_combo_list() {
2115 // TODO MDL-10965 improve.
2116 $tree = core_course_category
::top();
2117 if (!$tree->get_children_count()) {
2120 $chelper = new coursecat_helper();
2121 $chelper->set_subcat_depth($CFG->maxcategorydepth
)->
2122 set_categories_display_options(array(
2123 'limit' => $CFG->coursesperpage
,
2124 'viewmoreurl' => new moodle_url('/course/index.php',
2125 array('browse' => 'categories', 'page' => 1))
2127 set_courses_display_options(array(
2128 'limit' => $CFG->coursesperpage
,
2129 'viewmoreurl' => new moodle_url('/course/index.php',
2130 array('browse' => 'courses', 'page' => 1))
2132 set_attributes(array('class' => 'frontpage-category-combo'));
2133 return $this->coursecat_tree($chelper, $tree);
2137 * Returns HTML to print tree of course categories (with number of courses) for the frontpage
2141 public function frontpage_categories_list() {
2143 // TODO MDL-10965 improve.
2144 $tree = core_course_category
::top();
2145 if (!$tree->get_children_count()) {
2148 $chelper = new coursecat_helper();
2149 $chelper->set_subcat_depth($CFG->maxcategorydepth
)->
2150 set_show_courses(self
::COURSECAT_SHOW_COURSES_COUNT
)->
2151 set_categories_display_options(array(
2152 'limit' => $CFG->coursesperpage
,
2153 'viewmoreurl' => new moodle_url('/course/index.php',
2154 array('browse' => 'categories', 'page' => 1))
2156 set_attributes(array('class' => 'frontpage-category-names'));
2157 return $this->coursecat_tree($chelper, $tree);
2161 * Renders the activity information.
2163 * Defer to template.
2165 * @param \core_course\output\activity_information $page
2166 * @return string html for the page
2168 public function render_activity_information(\core_course\output\activity_information
$page) {
2169 $data = $page->export_for_template($this->output
);
2170 return $this->output
->render_from_template('core_course/activity_info', $data);
2174 * Renders the activity navigation.
2176 * Defer to template.
2178 * @param \core_course\output\activity_navigation $page
2179 * @return string html for the page
2181 public function render_activity_navigation(\core_course\output\activity_navigation
$page) {
2182 $data = $page->export_for_template($this->output
);
2183 return $this->output
->render_from_template('core_course/activity_navigation', $data);
2187 * Display waiting information about backup size during uploading backup process
2188 * @param object $backupfile the backup stored_file
2189 * @return $html string
2191 public function sendingbackupinfo($backupfile) {
2192 $sizeinfo = new stdClass();
2193 $sizeinfo->total
= number_format($backupfile->get_filesize() / 1000000, 2);
2194 $html = html_writer
::tag('div', get_string('sendingsize', 'hub', $sizeinfo),
2195 array('class' => 'courseuploadtextinfo'));
2200 * Hub information (logo - name - description - link)
2201 * @param object $hubinfo
2202 * @return string html code
2204 public function hubinfo($hubinfo) {
2205 $screenshothtml = html_writer
::empty_tag('img',
2206 array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name']));
2207 $hubdescription = html_writer
::tag('div', $screenshothtml,
2208 array('class' => 'hubscreenshot'));
2210 $hubdescription .= html_writer
::tag('a', $hubinfo['name'],
2211 array('class' => 'hublink', 'href' => $hubinfo['url'],
2212 'onclick' => 'this.target="_blank"'));
2214 $hubdescription .= html_writer
::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN
),
2215 array('class' => 'hubdescription'));
2216 $hubdescription = html_writer
::tag('div', $hubdescription, array('class' => 'hubinfo clearfix'));
2218 return $hubdescription;
2222 * Output frontpage summary text and frontpage modules (stored as section 1 in site course)
2224 * This may be disabled in settings
2228 public function frontpage_section1() {
2229 global $SITE, $USER;
2232 $editing = $this->page
->user_is_editing();
2235 // Make sure section with number 1 exists.
2236 course_create_sections_if_missing($SITE, 1);
2239 $modinfo = get_fast_modinfo($SITE);
2240 $section = $modinfo->get_section_info(1);
2243 if (($section && (!empty($modinfo->sections
[1]) or !empty($section->summary
))) or $editing) {
2245 $format = course_get_format($SITE);
2246 $frontpageclass = $format->get_output_classname('content\\frontpagesection');
2247 $frontpagesection = new $frontpageclass($format, $section);
2249 // The course outputs works with format renderers, not with course renderers.
2250 $renderer = $format->get_renderer($this->page
);
2251 $output .= $renderer->render($frontpagesection);
2258 * Output news for the frontpage (extract from site-wide news forum)
2260 * @param stdClass $forum record from db table 'forum' that represents the site news forum
2263 protected function frontpage_news($forum) {
2264 global $CFG, $SITE, $SESSION, $USER;
2265 require_once($CFG->dirroot
.'/mod/forum/lib.php');
2270 $SESSION->fromdiscussion
= $CFG->wwwroot
;
2272 if (\mod_forum\subscriptions
::is_subscribed($USER->id
, $forum)) {
2273 if (!\mod_forum\subscriptions
::is_forcesubscribed($forum)) {
2274 $subtext = get_string('unsubscribe', 'forum');
2277 $subtext = get_string('subscribe', 'forum');
2279 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id
, 'sesskey' => sesskey()));
2280 $output .= html_writer
::tag('div', html_writer
::link($suburl, $subtext), array('class' => 'subscribelink'));
2283 $coursemodule = get_coursemodule_from_instance('forum', $forum->id
);
2284 $context = context_module
::instance($coursemodule->id
);
2286 $entityfactory = mod_forum\local\container
::get_entity_factory();
2287 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE);
2289 $rendererfactory = mod_forum\local\container
::get_renderer_factory();
2290 $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity);
2291 $cm = \cm_info
::create($coursemodule);
2292 return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems
);
2296 * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
2298 * @param string $skipdivid
2299 * @param string $contentsdivid
2300 * @param string $header Header of the part
2301 * @param string $contents Contents of the part
2304 protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
2305 if (strval($contents) === '') {
2308 $output = html_writer
::link('#' . $skipdivid,
2309 get_string('skipa', 'access', core_text
::strtolower(strip_tags($header))),
2310 array('class' => 'skip-block skip aabtn'));
2312 // Wrap frontpage part in div container.
2313 $output .= html_writer
::start_tag('div', array('id' => $contentsdivid));
2314 $output .= $this->heading($header);
2316 $output .= $contents;
2318 // End frontpage part div container.
2319 $output .= html_writer
::end_tag('div');
2321 $output .= html_writer
::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
2326 * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
2330 public function frontpage() {
2335 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin
)) {
2336 $frontpagelayout = $CFG->frontpageloggedin
;
2338 $frontpagelayout = $CFG->frontpage
;
2341 foreach (explode(',', $frontpagelayout) as $v) {
2343 // Display the main part of the front page.
2345 if ($SITE->newsitems
) {
2346 // Print forums only when needed.
2347 require_once($CFG->dirroot
.'/mod/forum/lib.php');
2348 if (($newsforum = forum_get_course_forum($SITE->id
, 'news')) &&
2349 ($forumcontents = $this->frontpage_news($newsforum))) {
2350 $newsforumcm = get_fast_modinfo($SITE)->instances
['forum'][$newsforum->id
];
2351 $output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
2352 $newsforumcm->get_formatted_name(), $forumcontents);
2357 case FRONTPAGEENROLLEDCOURSELIST
:
2358 $mycourseshtml = $this->frontpage_my_courses();
2359 if (!empty($mycourseshtml)) {
2360 $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
2361 get_string('mycourses'), $mycourseshtml);
2365 case FRONTPAGEALLCOURSELIST
:
2366 $availablecourseshtml = $this->frontpage_available_courses();
2367 $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
2368 get_string('availablecourses'), $availablecourseshtml);
2371 case FRONTPAGECATEGORYNAMES
:
2372 $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
2373 get_string('categories'), $this->frontpage_categories_list());
2376 case FRONTPAGECATEGORYCOMBO
:
2377 $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
2378 get_string('courses'), $this->frontpage_combo_list());
2381 case FRONTPAGECOURSESEARCH
:
2382 $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
2386 $output .= '<br />';
2394 * Class storing display options and functions to help display course category and/or courses lists
2396 * This is a wrapper for core_course_category objects that also stores display options
2397 * and functions to retrieve sorted and paginated lists of categories/courses.
2399 * If theme overrides methods in core_course_renderers that access this class
2400 * it may as well not use this class at all or extend it.
2403 * @copyright 2013 Marina Glancy
2404 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2406 class coursecat_helper
{
2407 /** @var string [none, collapsed, expanded] how (if) display courses list */
2408 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
2409 /** @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) */
2410 protected $subcatdepth = 1;
2411 /** @var array options to display courses list */
2412 protected $coursesdisplayoptions = array();
2413 /** @var array options to display subcategories list */
2414 protected $categoriesdisplayoptions = array();
2415 /** @var array additional HTML attributes */
2416 protected $attributes = array();
2417 /** @var array search criteria if the list is a search result */
2418 protected $searchcriteria = null;
2421 * Sets how (if) to show the courses - none, collapsed, expanded, etc.
2423 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
2424 * @return coursecat_helper
2426 public function set_show_courses($showcourses) {
2427 $this->showcourses
= $showcourses;
2428 // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses()
2429 // and core_course_category::search_courses().
2430 $this->coursesdisplayoptions
['summary'] = $showcourses >= core_course_renderer
::COURSECAT_SHOW_COURSES_AUTO
;
2431 $this->coursesdisplayoptions
['coursecontacts'] = $showcourses >= core_course_renderer
::COURSECAT_SHOW_COURSES_EXPANDED
;
2432 $this->coursesdisplayoptions
['customfields'] = $showcourses >= core_course_renderer
::COURSECAT_SHOW_COURSES_COLLAPSED
;
2437 * Returns how (if) to show the courses - none, collapsed, expanded, etc.
2439 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
2441 public function get_show_courses() {
2442 return $this->showcourses
;
2446 * Sets the maximum depth to expand subcategories in the tree
2448 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2450 * @param int $subcatdepth
2451 * @return coursecat_helper
2453 public function set_subcat_depth($subcatdepth) {
2454 $this->subcatdepth
= $subcatdepth;
2459 * Returns the maximum depth to expand subcategories in the tree
2461 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2465 public function get_subcat_depth() {
2466 return $this->subcatdepth
;
2470 * Sets options to display list of courses
2472 * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses()
2474 * Options that core_course_category::get_courses() accept:
2475 * - recursive - return courses from subcategories as well. Use with care,
2476 * this may be a huge list!
2477 * - summary - preloads fields 'summary' and 'summaryformat'
2478 * - coursecontacts - preloads course contacts
2479 * - customfields - preloads custom fields data
2480 * - isenrolled - preloads indication whether this user is enrolled in the course
2481 * - sort - list of fields to sort. Example
2482 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
2483 * will sort by idnumber asc, shortname asc and id desc.
2484 * Default: array('sortorder' => 1)
2485 * Only cached fields may be used for sorting!
2487 * - limit - maximum number of children to return, 0 or null for no limit
2489 * Options summary and coursecontacts are filled automatically in the set_show_courses()
2491 * Also renderer can set here any additional options it wants to pass between renderer functions.
2493 * @param array $options
2494 * @return coursecat_helper
2496 public function set_courses_display_options($options) {
2497 $this->coursesdisplayoptions
= $options;
2498 $this->set_show_courses($this->showcourses
); // this will calculate special display options
2503 * Sets one option to display list of courses
2505 * @see coursecat_helper::set_courses_display_options()
2507 * @param string $key
2508 * @param mixed $value
2509 * @return coursecat_helper
2511 public function set_courses_display_option($key, $value) {
2512 $this->coursesdisplayoptions
[$key] = $value;
2517 * Return the specified option to display list of courses
2519 * @param string $optionname option name
2520 * @param mixed $defaultvalue default value for option if it is not specified
2523 public function get_courses_display_option($optionname, $defaultvalue = null) {
2524 if (array_key_exists($optionname, $this->coursesdisplayoptions
)) {
2525 return $this->coursesdisplayoptions
[$optionname];
2527 return $defaultvalue;
2532 * Returns all options to display the courses
2534 * This array is usually passed to {@link core_course_category::get_courses()} or
2535 * {@link core_course_category::search_courses()}
2539 public function get_courses_display_options() {
2540 return $this->coursesdisplayoptions
;
2544 * Sets options to display list of subcategories
2546 * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children().
2547 * Any other options may be used by renderer functions
2549 * @param array $options
2550 * @return coursecat_helper
2552 public function set_categories_display_options($options) {
2553 $this->categoriesdisplayoptions
= $options;
2558 * Return the specified option to display list of subcategories
2560 * @param string $optionname option name
2561 * @param mixed $defaultvalue default value for option if it is not specified
2564 public function get_categories_display_option($optionname, $defaultvalue = null) {
2565 if (array_key_exists($optionname, $this->categoriesdisplayoptions
)) {
2566 return $this->categoriesdisplayoptions
[$optionname];
2568 return $defaultvalue;
2573 * Returns all options to display list of subcategories
2575 * This array is usually passed to {@link core_course_category::get_children()}
2579 public function get_categories_display_options() {
2580 return $this->categoriesdisplayoptions
;
2584 * Sets additional general options to pass between renderer functions, usually HTML attributes
2586 * @param array $attributes
2587 * @return coursecat_helper
2589 public function set_attributes($attributes) {
2590 $this->attributes
= $attributes;
2595 * Return all attributes and erases them so they are not applied again
2597 * @param string $classname adds additional class name to the beginning of $attributes['class']
2600 public function get_and_erase_attributes($classname) {
2601 $attributes = $this->attributes
;
2602 $this->attributes
= array();
2603 if (empty($attributes['class'])) {
2604 $attributes['class'] = '';
2606 $attributes['class'] = $classname . ' '. $attributes['class'];
2611 * Sets the search criteria if the course is a search result
2613 * Search string will be used to highlight terms in course name and description
2615 * @param array $searchcriteria
2616 * @return coursecat_helper
2618 public function set_search_criteria($searchcriteria) {
2619 $this->searchcriteria
= $searchcriteria;
2624 * Returns formatted and filtered description of the given category
2626 * @param core_course_category $coursecat category
2627 * @param stdClass|array $options format options, by default [noclean,overflowdiv],
2628 * if context is not specified it will be added automatically
2629 * @return string|null
2631 public function get_category_formatted_description($coursecat, $options = null) {
2632 if ($coursecat->id
&& $coursecat->is_uservisible() && !empty($coursecat->description
)) {
2633 if (!isset($coursecat->descriptionformat
)) {
2634 $descriptionformat = FORMAT_MOODLE
;
2636 $descriptionformat = $coursecat->descriptionformat
;
2638 if ($options === null) {
2639 $options = array('noclean' => true, 'overflowdiv' => true);
2641 $options = (array)$options;
2643 $context = context_coursecat
::instance($coursecat->id
);
2644 if (!isset($options['context'])) {
2645 $options['context'] = $context;
2647 $text = file_rewrite_pluginfile_urls($coursecat->description
,
2648 'pluginfile.php', $context->id
, 'coursecat', 'description', null);
2649 return format_text($text, $descriptionformat, $options);
2655 * Returns given course's summary with proper embedded files urls and formatted
2657 * @param core_course_list_element $course
2658 * @param array|stdClass $options additional formatting options
2661 public function get_course_formatted_summary($course, $options = array()) {
2663 require_once($CFG->libdir
. '/filelib.php');
2664 if (!$course->has_summary()) {
2667 $options = (array)$options;
2668 $context = context_course
::instance($course->id
);
2669 if (!isset($options['context'])) {
2670 // TODO see MDL-38521
2671 // option 1 (current), page context - no code required
2672 // option 2, system context
2673 // $options['context'] = context_system::instance();
2674 // option 3, course context:
2675 // $options['context'] = $context;
2676 // option 4, course category context:
2677 // $options['context'] = $context->get_parent_context();
2679 $summary = file_rewrite_pluginfile_urls($course->summary
, 'pluginfile.php', $context->id
, 'course', 'summary', null);
2680 $summary = format_text($summary, $course->summaryformat
, $options, $course->id
);
2681 if (!empty($this->searchcriteria
['search'])) {
2682 $summary = highlight($this->searchcriteria
['search'], $summary);
2688 * Returns course name as it is configured to appear in courses lists formatted to course context
2690 * @param core_course_list_element $course
2691 * @param array|stdClass $options additional formatting options
2694 public function get_course_formatted_name($course, $options = array()) {
2695 $options = (array)$options;
2696 if (!isset($options['context'])) {
2697 $options['context'] = context_course
::instance($course->id
);
2699 $name = format_string(get_course_display_name_for_list($course), true, $options);
2700 if (!empty($this->searchcriteria
['search'])) {
2701 $name = highlight($this->searchcriteria
['search'], $name);