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