MDL-37009 Use new renderers for frontpage course and category listings
[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
SH
42
43 /**
44 * A cache of strings
45 * @var stdClass
46 */
47 protected $strings;
48
49 /**
50 * Override the constructor so that we can initialise the string cache
51 *
52 * @param moodle_page $page
53 * @param string $target
54 */
55 public function __construct(moodle_page $page, $target) {
56 $this->strings = new stdClass;
57 parent::__construct($page, $target);
a03dfb7d
MG
58 $this->add_modchoosertoggle();
59 }
60
61 /**
62 * Adds the item in course settings navigation to toggle modchooser
63 *
64 * Theme can overwrite as an empty function to exclude it (for example if theme does not
65 * use modchooser at all)
66 */
67 protected function add_modchoosertoggle() {
68 global $CFG;
69 static $modchoosertoggleadded = false;
70 // Add the module chooser toggle to the course page
71 if ($modchoosertoggleadded || $this->page->state > moodle_page::STATE_PRINTING_HEADER ||
72 $this->page->course->id == SITEID ||
aa2b293e
MG
73 !$this->page->user_is_editing() ||
74 !($context = context_course::instance($this->page->course->id)) ||
75 !has_capability('moodle/course:update', $context) ||
76 !course_ajax_enabled($this->page->course) ||
77 !($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) ||
78 !$coursenode->get('editsettings')) {
a03dfb7d 79 // too late or we are on site page or we could not find the course settings node
aa2b293e 80 // or we are not allowed to edit
a03dfb7d
MG
81 return;
82 }
83 $modchoosertoggleadded = true;
84 if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
85 // We are on the course page, retain the current page params e.g. section.
86 $modchoosertoggleurl = clone($this->page->url);
87 } else {
88 // Edit on the main course page.
89 $modchoosertoggleurl = new moodle_url('/course/view.php', array('id' => $this->page->course->id,
90 'return' => $this->page->url->out_as_local_url(false)));
91 }
92 $modchoosertoggleurl->param('sesskey', sesskey());
93 if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) {
94 $modchoosertogglestring = get_string('modchooserdisable', 'moodle');
95 $modchoosertoggleurl->param('modchooser', 'off');
96 } else {
97 $modchoosertogglestring = get_string('modchooserenable', 'moodle');
98 $modchoosertoggleurl->param('modchooser', 'on');
99 }
100 $modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING);
101 $coursenode->add_node($modchoosertoggle, 'editsettings');
102 $modchoosertoggle->add_class('modchoosertoggle');
103 $modchoosertoggle->add_class('visibleifjs');
104 user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
24e27ac0
SH
105 }
106
cb76fec0
PS
107 /**
108 * Renders course info box.
109 *
e2d70cca 110 * @param stdClass|course_in_list $course
cb76fec0
PS
111 * @return string
112 */
113 public function course_info_box(stdClass $course) {
cb76fec0
PS
114 $content = '';
115 $content .= $this->output->box_start('generalbox info');
e2d70cca
MG
116 $chelper = new coursecat_helper();
117 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
118 $content .= $this->coursecat_coursebox($chelper, $course);
cb76fec0 119 $content .= $this->output->box_end();
cb76fec0
PS
120 return $content;
121 }
122
24e27ac0
SH
123 /**
124 * Renderers a structured array of courses and categories into a nice
125 * XHTML tree structure.
126 *
127 * This method was designed initially to display the front page course/category
128 * combo view. The structure can be retrieved by get_course_category_tree()
129 *
130 * @param array $structure
131 * @return string
132 */
133 public function course_category_tree(array $structure) {
24e27ac0
SH
134 $this->strings->summary = get_string('summary');
135
136 // Generate an id and the required JS call to make this a nice widget
137 $id = html_writer::random_id('course_category_tree');
c8ffba95 138 $this->page->requires->js_init_call('M.util.init_toggle_class_on_click', array($id, '.category.with_children .category_label', 'collapsed', '.category.with_children'));
24e27ac0
SH
139
140 // Start content generation
141 $content = html_writer::start_tag('div', array('class'=>'course_category_tree', 'id'=>$id));
142 foreach ($structure as $category) {
143 $content .= $this->course_category_tree_category($category);
144 }
145 $content .= html_writer::start_tag('div', array('class'=>'controls'));
146 $content .= html_writer::tag('div', get_string('collapseall'), array('class'=>'addtoall expandall'));
147 $content .= html_writer::tag('div', get_string('expandall'), array('class'=>'removefromall collapseall'));
148 $content .= html_writer::end_tag('div');
149 $content .= html_writer::end_tag('div');
df997f84 150
24e27ac0
SH
151 // Return the course category tree HTML
152 return $content;
153 }
154
155 /**
156 * Renderers a category for use with course_category_tree
157 *
158 * @param array $category
159 * @param int $depth
160 * @return string
161 */
162 protected function course_category_tree_category(stdClass $category, $depth=1) {
163 $content = '';
3754f4f3
ARN
164 $hassubcategories = (isset($category->categories) && count($category->categories)>0);
165 $hascourses = (isset($category->courses) && count($category->courses)>0);
24e27ac0
SH
166 $classes = array('category');
167 if ($category->parent != 0) {
168 $classes[] = 'subcategory';
169 }
d703f226
SH
170 if (empty($category->visible)) {
171 $classes[] = 'dimmed_category';
172 }
24e27ac0
SH
173 if ($hassubcategories || $hascourses) {
174 $classes[] = 'with_children';
175 if ($depth > 1) {
176 $classes[] = 'collapsed';
177 }
178 }
9a5e297b 179 $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
63390481 180
24e27ac0
SH
181 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
182 $content .= html_writer::start_tag('div', array('class'=>'category_label'));
63390481 183 $content .= html_writer::link(new moodle_url('/course/category.php', array('id'=>$category->id)), $categoryname, array('class'=>'category_link'));
24e27ac0
SH
184 $content .= html_writer::end_tag('div');
185 if ($hassubcategories) {
186 $content .= html_writer::start_tag('div', array('class'=>'subcategories'));
187 foreach ($category->categories as $subcategory) {
188 $content .= $this->course_category_tree_category($subcategory, $depth+1);
189 }
190 $content .= html_writer::end_tag('div');
191 }
192 if ($hascourses) {
193 $content .= html_writer::start_tag('div', array('class'=>'courses'));
194 $coursecount = 0;
6a1273cb 195 $strinfo = new lang_string('info');
24e27ac0
SH
196 foreach ($category->courses as $course) {
197 $classes = array('course');
d2fd38be
PS
198 $linkclass = 'course_link';
199 if (!$course->visible) {
200 $linkclass .= ' dimmed';
201 }
24e27ac0
SH
202 $coursecount ++;
203 $classes[] = ($coursecount%2)?'odd':'even';
204 $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes)));
f4c23f03 205 $content .= html_writer::link(new moodle_url('/course/view.php', array('id'=>$course->id)), format_string($course->fullname), array('class'=>$linkclass));
24e27ac0
SH
206 $content .= html_writer::start_tag('div', array('class'=>'course_info clearfix'));
207
bf423bb1
PS
208 // print enrol info
209 if ($icons = enrol_get_course_info_icons($course)) {
210 foreach ($icons as $pix_icon) {
211 $content .= $this->render($pix_icon);
212 }
213 }
214
24e27ac0 215 if ($course->summary) {
6a1273cb 216 $url = new moodle_url('/course/info.php', array('id' => $course->id));
24e27ac0 217 $image = html_writer::empty_tag('img', array('src'=>$this->output->pix_url('i/info'), 'alt'=>$this->strings->summary));
6a1273cb 218 $content .= $this->action_link($url, $image, new popup_action('click', $url, 'courseinfo'), array('title' => $this->strings->summary));
24e27ac0
SH
219 }
220 $content .= html_writer::end_tag('div');
221 $content .= html_writer::end_tag('div');
222 }
223 $content .= html_writer::end_tag('div');
224 }
225 $content .= html_writer::end_tag('div');
226 return $content;
227 }
01e0e704
ARN
228
229 /**
230 * Build the HTML for the module chooser javascript popup
231 *
232 * @param array $modules A set of modules as returned form @see
233 * get_module_metadata
234 * @param object $course The course that will be displayed
235 * @return string The composed HTML for the module
236 */
237 public function course_modchooser($modules, $course) {
697ff999
MG
238 static $isdisplayed = false;
239 if ($isdisplayed) {
240 return '';
241 }
242 $isdisplayed = true;
243
244 // Add the module chooser
245 $this->page->requires->yui_module('moodle-course-modchooser',
246 'M.course.init_chooser',
247 array(array('courseid' => $course->id, 'closeButtonTitle' => get_string('close', 'editor')))
248 );
249 $this->page->requires->strings_for_js(array(
250 'addresourceoractivity',
251 'modchooserenable',
252 'modchooserdisable',
253 ), 'moodle');
01e0e704
ARN
254
255 // Add the header
1edff8c7 256 $header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
255dd8d1 257 array('class' => 'hd choosertitle'));
01e0e704
ARN
258
259 $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
260 'id' => 'chooserform', 'method' => 'post'));
261 $formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
262 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
263 'name' => 'course', 'value' => $course->id));
264 $formcontent .= html_writer::tag('input', '',
0a2fb910 265 array('type' => 'hidden', 'class' => 'jump', 'name' => 'jump', 'value' => ''));
01e0e704
ARN
266 $formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
267 'value' => sesskey()));
268 $formcontent .= html_writer::end_tag('div');
269
270 // Put everything into one tag 'options'
271 $formcontent .= html_writer::start_tag('div', array('class' => 'options'));
272 $formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
273 array('class' => 'instruction'));
274 // Put all options into one tag 'alloptions' to allow us to handle scrolling
275 $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
276
5035ed63
MA
277 // Activities
278 $activities = array_filter($modules, function($mod) {
279 return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);
280 });
01e0e704
ARN
281 if (count($activities)) {
282 $formcontent .= $this->course_modchooser_title('activities');
283 $formcontent .= $this->course_modchooser_module_types($activities);
284 }
285
463ccee1 286 // Resources
5035ed63
MA
287 $resources = array_filter($modules, function($mod) {
288 return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);
289 });
48a5e55e
ARN
290 if (count($resources)) {
291 $formcontent .= $this->course_modchooser_title('resources');
292 $formcontent .= $this->course_modchooser_module_types($resources);
293 }
294
01e0e704
ARN
295 $formcontent .= html_writer::end_tag('div'); // modoptions
296 $formcontent .= html_writer::end_tag('div'); // types
297
298 $formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
af75421c 299 $formcontent .= html_writer::tag('input', '',
0a2fb910 300 array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add')));
8ff6c5ee 301 $formcontent .= html_writer::tag('input', '',
0a2fb910 302 array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel')));
01e0e704
ARN
303 $formcontent .= html_writer::end_tag('div');
304 $formcontent .= html_writer::end_tag('form');
305
306 // Wrap the whole form in a div
307 $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
308
309 // Put all of the content together
310 $content = $formcontent;
311
255dd8d1
ARN
312 $content = html_writer::tag('div', $content, array('class' => 'choosercontainer'));
313 return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody'));
01e0e704
ARN
314 }
315
316 /**
317 * Build the HTML for a specified set of modules
318 *
319 * @param array $modules A set of modules as used by the
320 * course_modchooser_module function
321 * @return string The composed HTML for the module
322 */
323 protected function course_modchooser_module_types($modules) {
324 $return = '';
325 foreach ($modules as $module) {
326 if (!isset($module->types)) {
327 $return .= $this->course_modchooser_module($module);
328 } else {
329 $return .= $this->course_modchooser_module($module, array('nonoption'));
330 foreach ($module->types as $type) {
331 $return .= $this->course_modchooser_module($type, array('option', 'subtype'));
332 }
333 }
334 }
335 return $return;
336 }
337
338 /**
339 * Return the HTML for the specified module adding any required classes
340 *
341 * @param object $module An object containing the title, and link. An
342 * icon, and help text may optionally be specified. If the module
343 * contains subtypes in the types option, then these will also be
344 * displayed.
345 * @param array $classes Additional classes to add to the encompassing
346 * div element
347 * @return string The composed HTML for the module
348 */
349 protected function course_modchooser_module($module, $classes = array('option')) {
350 $output = '';
351 $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
352 $output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
353 if (!isset($module->types)) {
354 $output .= html_writer::tag('input', '', array('type' => 'radio',
355 'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
356 }
357
358 $output .= html_writer::start_tag('span', array('class' => 'modicon'));
359 if (isset($module->icon)) {
360 // Add an icon if we have one
361 $output .= $module->icon;
362 }
363 $output .= html_writer::end_tag('span');
364
365 $output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
366 if (!isset($module->help)) {
367 // Add help if found
368 $module->help = get_string('nohelpforactivityorresource', 'moodle');
369 }
370
371 // Format the help text using markdown with the following options
372 $options = new stdClass();
373 $options->trusted = false;
374 $options->noclean = false;
375 $options->smiley = false;
376 $options->filter = false;
377 $options->para = true;
378 $options->newlines = false;
379 $options->overflowdiv = false;
380 $module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
381 $output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
382 $output .= html_writer::end_tag('label');
383 $output .= html_writer::end_tag('div');
384
385 return $output;
386 }
387
388 protected function course_modchooser_title($title, $identifier = null) {
389 $module = new stdClass();
390 $module->name = $title;
391 $module->types = array();
392 $module->title = get_string($title, $identifier);
393 $module->help = '';
394 return $this->course_modchooser_module($module, array('moduletypetitle'));
395 }
f558b291
MG
396
397 /**
398 * Renders HTML for displaying the sequence of course module editing buttons
399 *
400 * @see course_get_cm_edit_actions()
401 *
402 * @param array $actions array of action_link or pix_icon objects
403 * @return string
404 */
405 public function course_section_cm_edit_actions($actions) {
406 $output = html_writer::start_tag('span', array('class' => 'commands'));
407 foreach ($actions as $action) {
408 if ($action instanceof renderable) {
409 $output .= $this->output->render($action);
410 } else {
411 $output .= $action;
412 }
413 }
414 $output .= html_writer::end_tag('span');
415 return $output;
416 }
9a6aa5c1
MG
417
418 /**
419 * Renders HTML for the menus to add activities and resources to the current course
420 *
a03dfb7d
MG
421 * Note, if theme overwrites this function and it does not use modchooser,
422 * see also {@link core_course_renderer::add_modchoosertoggle()}
423 *
9a6aa5c1
MG
424 * @param stdClass $course
425 * @param int $section relative section number (field course_sections.section)
426 * @param int $sectionreturn The section to link back to
427 * @param array $displayoptions additional display options, for example blocks add
428 * option 'inblock' => true, suggesting to display controls vertically
429 * @return string
430 */
431 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
432 global $CFG;
433
434 $vertical = !empty($displayoptions['inblock']);
435
436 // check to see if user can add menus and there are modules to add
437 if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
438 || !$this->page->user_is_editing()
439 || !($modnames = get_module_types_names()) || empty($modnames)) {
440 return '';
441 }
442
443 // Retrieve all modules with associated metadata
444 $modules = get_module_metadata($course, $modnames, $sectionreturn);
445 $urlparams = array('section' => $section);
446
447 // We'll sort resources and activities into two lists
448 $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
449
450 foreach ($modules as $module) {
451 if (!array_key_exists($module->archetype, $activities)) {
452 // System modules cannot be added by user, do not add to dropdown
453 } else if (isset($module->types)) {
454 // This module has a subtype
455 // NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
456 $subtypes = array();
457 foreach ($module->types as $subtype) {
458 $link = $subtype->link->out(true, $urlparams);
459 $subtypes[$link] = $subtype->title;
460 }
461
462 // Sort module subtypes into the list
463 if (!empty($module->title)) {
464 // This grouping has a name
465 $activities[$module->archetype][] = array($module->title => $subtypes);
466 } else {
467 // This grouping does not have a name
468 $activities[$module->archetype] = array_merge($activities[$module->archetype], $subtypes);
469 }
470 } else {
471 // This module has no subtypes
472 $link = $module->link->out(true, $urlparams);
473 $activities[$module->archetype][$link] = $module->title;
474 }
475 }
476
477 $straddactivity = get_string('addactivity');
478 $straddresource = get_string('addresource');
479 $sectionname = get_section_name($course, $section);
480 $strresourcelabel = get_string('addresourcetosection', null, $sectionname);
481 $stractivitylabel = get_string('addactivitytosection', null, $sectionname);
482
483 $output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));
484
485 if (!$vertical) {
486 $output .= html_writer::start_tag('div', array('class' => 'horizontal'));
487 }
488
489 if (!empty($activities[MOD_CLASS_RESOURCE])) {
490 $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array(''=>$straddresource), "ressection$section");
491 $select->set_help_icon('resources');
492 $select->set_label($strresourcelabel, array('class' => 'accesshide'));
493 $output .= $this->output->render($select);
494 }
495
496 if (!empty($activities[MOD_CLASS_ACTIVITY])) {
497 $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array(''=>$straddactivity), "section$section");
498 $select->set_help_icon('activities');
499 $select->set_label($stractivitylabel, array('class' => 'accesshide'));
500 $output .= $this->output->render($select);
501 }
502
503 if (!$vertical) {
504 $output .= html_writer::end_tag('div');
505 }
506
507 $output .= html_writer::end_tag('div');
508
509 if (course_ajax_enabled($course) && $course->id == $this->page->course->id) {
510 // modchooser can be added only for the current course set on the page!
511 $straddeither = get_string('addresourceoractivity');
512 // The module chooser link
513 $modchooser = html_writer::start_tag('div', array('class' => 'mdl-right'));
514 $modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
515 $icon = $this->output->pix_icon('t/add', '');
516 $span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
517 $modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
518 $modchooser.= html_writer::end_tag('div');
519 $modchooser.= html_writer::end_tag('div');
520
521 // Wrap the normal output in a noscript div
522 $usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault);
523 if ($usemodchooser) {
524 $output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
525 $modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
526 } else {
527 // If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled
528 $output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
529 $modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
530 }
531 $output = $this->course_modchooser($modules, $course) . $modchooser . $output;
532 }
533
534 return $output;
535 }
7e29340f 536
f4b571ab
MG
537 /**
538 * Renders html to display a course search form
539 *
540 * @param string $value default value to populate the search field
541 * @param string $format display format - 'plain' (default), 'short' or 'navbar'
542 * @return string
543 */
544 function course_search_form($value = '', $format = 'plain') {
545 static $count = 0;
546 $formid = 'coursesearch';
547 if ((++$count) > 1) {
548 $formid .= $count;
549 }
550
551 switch ($format) {
552 case 'navbar' :
553 $formid = 'coursesearchnavbar';
554 $inputid = 'navsearchbox';
555 $inputsize = 20;
556 break;
557 case 'short' :
558 $inputid = 'shortsearchbox';
559 $inputsize = 12;
560 break;
561 default :
562 $inputid = 'coursesearchbox';
563 $inputsize = 30;
564 }
565
566 $strsearchcourses= get_string("searchcourses");
567 $searchurl = new moodle_url('/course/search.php');
568
569 $output = html_writer::start_tag('form', array('id' => $formid, 'action' => $searchurl, 'method' => 'get'));
570 $output .= html_writer::start_tag('fieldset', array('class' => 'coursesearchbox invisiblefieldset'));
571 $output .= html_writer::tag('lavel', $strsearchcourses.': ', array('for' => $inputid));
572 $output .= html_writer::empty_tag('input', array('type' => 'text', 'id' => $inputid,
573 'size' => $inputsize, 'name' => 'search', 'value' => s($value)));
574 $output .= html_writer::empty_tag('input', array('type' => 'submit',
575 'value' => get_string('go')));
576 $output .= html_writer::end_tag('fieldset');
577 $output .= html_writer::end_tag('form');
578
579 return $output;
580 }
581
7e29340f
MG
582 /**
583 * Renders html for completion box on course page
584 *
585 * If completion is disabled, returns empty string
586 * If completion is automatic, returns an icon of the current completion state
587 * If completion is manual, returns a form (with an icon inside) that allows user to
588 * toggle completion
589 *
590 * @param stdClass $course course object
591 * @param completion_info $completioninfo completion info for the course, it is recommended
592 * to fetch once for all modules in course/section for performance
593 * @param cm_info $mod module to show completion for
594 * @param array $displayoptions display options, not used in core
595 * @return string
596 */
597 public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) {
598 global $CFG;
599 $output = '';
600 if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
601 return $output;
602 }
603 if ($completioninfo === null) {
604 $completioninfo = new completion_info($course);
605 }
606 $completion = $completioninfo->is_enabled($mod);
607 if ($completion == COMPLETION_TRACKING_NONE) {
608 return $output;
609 }
610
611 $completiondata = $completioninfo->get_data($mod, true);
612 $completionicon = '';
613
614 if ($this->page->user_is_editing()) {
615 switch ($completion) {
616 case COMPLETION_TRACKING_MANUAL :
617 $completionicon = 'manual-enabled'; break;
618 case COMPLETION_TRACKING_AUTOMATIC :
619 $completionicon = 'auto-enabled'; break;
620 }
621 } else if ($completion == COMPLETION_TRACKING_MANUAL) {
622 switch($completiondata->completionstate) {
623 case COMPLETION_INCOMPLETE:
624 $completionicon = 'manual-n'; break;
625 case COMPLETION_COMPLETE:
626 $completionicon = 'manual-y'; break;
627 }
628 } else { // Automatic
629 switch($completiondata->completionstate) {
630 case COMPLETION_INCOMPLETE:
631 $completionicon = 'auto-n'; break;
632 case COMPLETION_COMPLETE:
633 $completionicon = 'auto-y'; break;
634 case COMPLETION_COMPLETE_PASS:
635 $completionicon = 'auto-pass'; break;
636 case COMPLETION_COMPLETE_FAIL:
637 $completionicon = 'auto-fail'; break;
638 }
639 }
640 if ($completionicon) {
641 $formattedname = $mod->get_formatted_name();
642 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
643 if ($completion == COMPLETION_TRACKING_MANUAL && !$this->page->user_is_editing()) {
644 $imgtitle = get_string('completion-title-' . $completionicon, 'completion', $formattedname);
645 $newstate =
646 $completiondata->completionstate == COMPLETION_COMPLETE
647 ? COMPLETION_INCOMPLETE
648 : COMPLETION_COMPLETE;
649 // In manual mode the icon is a toggle form...
650
651 // If this completion state is used by the
652 // conditional activities system, we need to turn
653 // off the JS.
654 $extraclass = '';
655 if (!empty($CFG->enableavailability) &&
656 condition_info::completion_value_used_as_condition($course, $mod)) {
657 $extraclass = ' preventjs';
658 }
659 $output .= html_writer::start_tag('form', array('method' => 'post',
660 'action' => new moodle_url('/course/togglecompletion.php'),
661 'class' => 'togglecompletion'. $extraclass));
662 $output .= html_writer::start_tag('div');
663 $output .= html_writer::empty_tag('input', array(
664 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
665 $output .= html_writer::empty_tag('input', array(
666 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
667 $output .= html_writer::empty_tag('input', array(
668 'type' => 'hidden', 'name' => 'modulename', 'value' => $mod->name));
669 $output .= html_writer::empty_tag('input', array(
670 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
671 $output .= html_writer::empty_tag('input', array(
672 'type' => 'image',
673 'src' => $this->output->pix_url('i/completion-'.$completionicon),
f0989c3b
RT
674 'alt' => $imgalt, 'title' => $imgtitle,
675 'aria-live' => 'polite'));
7e29340f
MG
676 $output .= html_writer::end_tag('div');
677 $output .= html_writer::end_tag('form');
678 } else {
679 // In auto mode, or when editing, the icon is just an image
680 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
681 array('title' => $imgalt));
682 $output .= html_writer::tag('span', $this->output->render($completionpixicon),
683 array('class' => 'autocompletion'));
684 }
685 }
686 return $output;
687 }
ed513fad
MG
688
689 /**
690 * Checks if course module has any conditions that may make it unavailable for
691 * all or some of the students
692 *
693 * This function is internal and is only used to create CSS classes for the module name/text
694 *
695 * @param cm_info $mod
696 * @return bool
697 */
698 protected function is_cm_conditionally_hidden(cm_info $mod) {
699 global $CFG;
700 $conditionalhidden = false;
701 if (!empty($CFG->enableavailability)) {
702 $conditionalhidden = $mod->availablefrom > time() ||
703 ($mod->availableuntil && $mod->availableuntil < time()) ||
704 count($mod->conditionsgrade) > 0 ||
705 count($mod->conditionscompletion) > 0;
706 }
707 return $conditionalhidden;
708 }
709
710 /**
711 * Renders html to display a name with the link to the course module on a course page
712 *
713 * If module is unavailable for user but still needs to be displayed
714 * in the list, just the name is returned without a link
715 *
716 * Note, that for course modules that never have separate pages (i.e. labels)
717 * this function return an empty string
718 *
719 * @param cm_info $mod
720 * @param array $displayoptions
721 * @return string
722 */
723 public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
724 global $CFG;
725 $output = '';
726 if (!$mod->uservisible &&
727 (empty($mod->showavailability) || empty($mod->availableinfo))) {
728 // nothing to be displayed to the user
729 return $output;
730 }
731 $url = $mod->get_url();
732 if (!$url) {
733 return $output;
734 }
735
736 //Accessibility: for files get description via icon, this is very ugly hack!
737 $instancename = $mod->get_formatted_name();
738 $altname = '';
739 $altname = $mod->modfullname;
740 // Avoid unnecessary duplication: if e.g. a forum name already
741 // includes the word forum (or Forum, etc) then it is unhelpful
742 // to include that in the accessible description that is added.
743 if (false !== strpos(textlib::strtolower($instancename),
744 textlib::strtolower($altname))) {
745 $altname = '';
746 }
747 // File type after name, for alphabetic lists (screen reader).
748 if ($altname) {
749 $altname = get_accesshide(' '.$altname);
750 }
751
752 $conditionalhidden = $this->is_cm_conditionally_hidden($mod);
753 $accessiblebutdim = !$mod->visible || $conditionalhidden;
754
755 $linkclasses = '';
756 $accesstext = '';
757 $textclasses = '';
758 if ($accessiblebutdim) {
759 $linkclasses .= ' dimmed';
760 $textclasses .= ' dimmed_text';
761 if ($conditionalhidden) {
762 $linkclasses .= ' conditionalhidden';
763 $textclasses .= ' conditionalhidden';
764 }
765 if ($mod->uservisible) {
766 // show accessibility note only if user can access the module himself
767 $accesstext = get_accesshide(get_string('hiddenfromstudents').': ');
768 }
769 }
770
771 // Get on-click attribute value if specified and decode the onclick - it
772 // has already been encoded for display (puke).
773 $onclick = htmlspecialchars_decode($mod->get_on_click(), ENT_QUOTES);
774
775 $groupinglabel = '';
776 if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', context_course::instance($mod->course))) {
777 $groupings = groups_get_all_groupings($mod->course);
778 $groupinglabel = html_writer::tag('span', '('.format_string($groupings[$mod->groupingid]->name).')',
779 array('class' => 'groupinglabel '.$textclasses));
780 }
781
782 // Display link itself.
783 $activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(),
784 'class' => 'iconlarge activityicon', 'alt' => $mod->modfullname)) . $accesstext .
785 html_writer::tag('span', $instancename . $altname, array('class' => 'instancename'));
786 if ($mod->uservisible) {
787 $output .= html_writer::link($url, $activitylink, array('class' => $linkclasses, 'onclick' => $onclick)) .
788 $groupinglabel;
789 } else {
790 // We may be displaying this just in order to show information
791 // about visibility, without the actual link ($mod->uservisible)
792 $output .= html_writer::tag('div', $activitylink, array('class' => $textclasses)) .
793 $groupinglabel;
794 }
795 return $output;
796 }
797
798 /**
799 * Renders html to display the module content on the course page (i.e. text of the labels)
800 *
801 * @param cm_info $mod
802 * @param array $displayoptions
803 * @return string
804 */
805 public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
806 $output = '';
807 if (!$mod->uservisible &&
808 (empty($mod->showavailability) || empty($mod->availableinfo))) {
809 // nothing to be displayed to the user
810 return $output;
811 }
812 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
813 $conditionalhidden = $this->is_cm_conditionally_hidden($mod);
814 $accessiblebutdim = !$mod->visible || $conditionalhidden;
815 $textclasses = '';
816 $accesstext = '';
817 if ($accessiblebutdim) {
818 $textclasses .= ' dimmed_text';
819 if ($conditionalhidden) {
820 $textclasses .= ' conditionalhidden';
821 }
822 if ($mod->uservisible) {
823 // show accessibility note only if user can access the module himself
824 $accesstext = get_accesshide(get_string('hiddenfromstudents').': ');
825 }
826 }
827 if ($mod->get_url()) {
828 if ($content) {
829 // If specified, display extra content after link.
830 $output = html_writer::tag('div', $content, array('class' =>
831 trim('contentafterlink ' . $textclasses)));
832 }
833 } else {
834 // No link, so display only content.
835 $output = html_writer::tag('div', $accesstext . $content, array('class' => $textclasses));
836 }
837 return $output;
838 }
839
840 /**
841 * Renders HTML to show course module availability information (for someone who isn't allowed
842 * to see the activity itself, or for staff)
843 *
844 * @param cm_info $mod
845 * @param array $displayoptions
846 * @return string
847 */
848 public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
849 global $CFG;
850 if (!$mod->uservisible) {
851 // this is a student who is not allowed to see the module but might be allowed
852 // to see availability info (i.e. "Available from ...")
853 if (!empty($mod->showavailability) && !empty($mod->availableinfo)) {
854 $output = html_writer::tag('div', $mod->availableinfo, array('class' => 'availabilityinfo'));
855 }
856 return $output;
857 }
858 // this is a teacher who is allowed to see module but still should see the
859 // information that module is not available to all/some students
860 $modcontext = context_module::instance($mod->id);
861 $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
862 if ($canviewhidden && !empty($CFG->enableavailability)) {
863 // Don't add availability information if user is not editing and activity is hidden.
864 if ($mod->visible || $this->page->user_is_editing()) {
865 $hidinfoclass = '';
866 if (!$mod->visible) {
867 $hidinfoclass = 'hide';
868 }
869 $ci = new condition_info($mod);
870 $fullinfo = $ci->get_full_information();
871 if($fullinfo) {
db82b130 872 return '<div class="availabilityinfo '.$hidinfoclass.'">'.get_string($mod->showavailability
ed513fad
MG
873 ? 'userrestriction_visible'
874 : 'userrestriction_hidden','condition',
875 $fullinfo).'</div>';
876 }
877 }
878 }
879 return '';
880 }
c58a25d6
MG
881
882 /**
883 * Renders HTML to display one course module in a course section
884 *
885 * This includes link, content, availability, completion info and additional information
886 * that module type wants to display (i.e. number of unread forum posts)
887 *
888 * This function calls:
889 * {@link core_course_renderer::course_section_cm_name()}
890 * {@link cm_info::get_after_link()}
891 * {@link core_course_renderer::course_section_cm_text()}
892 * {@link core_course_renderer::course_section_cm_availability()}
893 * {@link core_course_renderer::course_section_cm_completion()}
894 * {@link course_get_cm_edit_actions()}
895 * {@link core_course_renderer::course_section_cm_edit_actions()}
33919cca 896 *
c58a25d6
MG
897 * @param stdClass $course
898 * @param completion_info $completioninfo
899 * @param cm_info $mod
900 * @param int|null $sectionreturn
901 * @param array $displayoptions
902 * @return string
903 */
904 public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
905 $output = '';
906 // We return empty string (because course module will not be displayed at all)
907 // if:
908 // 1) The activity is not visible to users
909 // and
910 // 2a) The 'showavailability' option is not set (if that is set,
911 // we need to display the activity so we can show
912 // availability info)
913 // or
914 // 2b) The 'availableinfo' is empty, i.e. the activity was
915 // hidden in a way that leaves no info, such as using the
916 // eye icon.
917 if (!$mod->uservisible &&
918 (empty($mod->showavailability) || empty($mod->availableinfo))) {
919 return $output;
920 }
921
922 $indentclasses = 'mod-indent';
923 if (!empty($mod->indent)) {
924 $indentclasses .= ' mod-indent-'.$mod->indent;
925 if ($mod->indent > 15) {
926 $indentclasses .= ' mod-indent-huge';
927 }
928 }
929 $output .= html_writer::start_tag('div', array('class' => $indentclasses));
930
931 // Start the div for the activity title, excluding the edit icons.
932 $output .= html_writer::start_tag('div', array('class' => 'activityinstance'));
933
934 // Display the link to the module (or do nothing if module has no url)
935 $output .= $this->course_section_cm_name($mod, $displayoptions);
936
937 // Module can put text after the link (e.g. forum unread)
938 $output .= $mod->get_after_link();
939
940 // Closing the tag which contains everything but edit icons. Content part of the module should not be part of this.
941 $output .= html_writer::end_tag('div'); // .activityinstance
942
943 // If there is content but NO link (eg label), then display the
944 // content here (BEFORE any icons). In this case cons must be
945 // displayed after the content so that it makes more sense visually
946 // and for accessibility reasons, e.g. if you have a one-line label
947 // it should work similarly (at least in terms of ordering) to an
948 // activity.
949 $contentpart = $this->course_section_cm_text($mod, $displayoptions);
950 $url = $mod->get_url();
951 if (empty($url)) {
952 $output .= $contentpart;
953 }
954
955 if ($this->page->user_is_editing()) {
956 $editactions = course_get_cm_edit_actions($mod, $mod->indent, $sectionreturn);
957 $output .= ' '. $this->course_section_cm_edit_actions($editactions);
958 $output .= $mod->get_after_edit_icons();
959 }
960
961 $output .= $this->course_section_cm_completion($course, $completioninfo, $mod, $displayoptions);
962
963 // If there is content AND a link, then display the content here
964 // (AFTER any icons). Otherwise it was displayed before
965 if (!empty($url)) {
966 $output .= $contentpart;
967 }
968
969 // show availability info (if module is not available)
970 $output .= $this->course_section_cm_availability($mod, $displayoptions);
971
972 $output .= html_writer::end_tag('div'); // $indentclasses
973 return $output;
974 }
975
976 /**
977 * Renders HTML to display a list of course modules in a course section
978 * Also displays "move here" controls in Javascript-disabled mode
979 *
980 * This function calls {@link core_course_renderer::course_section_cm()}
981 *
982 * @param stdClass $course course object
983 * @param int|stdClass|section_info $section relative section number or section object
984 * @param int $sectionreturn section number to return to
985 * @param int $displayoptions
986 * @return void
987 */
988 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = array()) {
989 global $USER;
990
991 $output = '';
992 $modinfo = get_fast_modinfo($course);
993 if (is_object($section)) {
994 $section = $modinfo->get_section_info($section->section);
995 } else {
996 $section = $modinfo->get_section_info($section);
997 }
998 $completioninfo = new completion_info($course);
999
1000 // check if we are currently in the process of moving a module with JavaScript disabled
1001 $ismoving = $this->page->user_is_editing() && ismoving($course->id);
1002 if ($ismoving) {
1003 $movingpix = new pix_icon('movehere', get_string('movehere'), 'moodle', array('class' => 'movetarget'));
1004 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
1005 }
1006
1007 // Get the list of modules visible to user (excluding the module being moved if there is one)
1008 $moduleshtml = array();
1009 if (!empty($modinfo->sections[$section->section])) {
1010 foreach ($modinfo->sections[$section->section] as $modnumber) {
1011 $mod = $modinfo->cms[$modnumber];
1012
1013 if ($ismoving and $mod->id == $USER->activitycopy) {
1014 // do not display moving mod
1015 continue;
1016 }
1017
1018 if ($modulehtml = $this->course_section_cm($course,
1019 $completioninfo, $mod, $sectionreturn, $displayoptions)) {
1020 $moduleshtml[$modnumber] = $modulehtml;
1021 }
1022 }
1023 }
1024
1025 if (!empty($moduleshtml) || $ismoving) {
1026
1027 $output .= html_writer::start_tag('ul', array('class' => 'section img-text'));
1028
1029 foreach ($moduleshtml as $modnumber => $modulehtml) {
1030 if ($ismoving) {
1031 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey()));
1032 $output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)),
1033 array('class' => 'movehere', 'title' => $strmovefull));
1034 }
1035
1036 $mod = $modinfo->cms[$modnumber];
8df0765a 1037 $modclasses = 'activity '. $mod->modname. ' modtype_'.$mod->modname. ' '. $mod->get_extra_classes();
c58a25d6
MG
1038 $output .= html_writer::start_tag('li', array('class' => $modclasses, 'id' => 'module-'. $mod->id));
1039 $output .= $modulehtml;
1040 $output .= html_writer::end_tag('li');
1041 }
1042
1043 if ($ismoving) {
1044 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
1045 $output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)),
1046 array('class' => 'movehere', 'title' => $strmovefull));
1047 }
1048
1049 $output .= html_writer::end_tag('ul'); // .section
1050 }
1051
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 *
1065 * @param array $courses array of course records (or instances of course_in_list) to show on this page
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
43e389ea
MG
1103 /**
1104 * Displays one course in the list of courses.
1105 *
1106 * This is an internal function, to display an information about just one course
1107 * please use {@link core_course_renderer::course_info_box()}
1108 *
1109 * @param coursecat_helper $chelper various display options
1110 * @param course_in_list|stdClass $course
1111 * @param string $additionalclasses additional classes to add to the main <div> tag (usually
1112 * depend on the course position in list - first/last/even/odd)
1113 * @return string
1114 */
1115 protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') {
1116 global $CFG;
1117 if (!isset($this->strings->summary)) {
1118 $this->strings->summary = get_string('summary');
1119 }
1120 if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) {
1121 return '';
1122 }
1123 if ($course instanceof stdClass) {
1124 require_once($CFG->libdir. '/coursecatlib.php');
1125 $course = new course_in_list($course);
1126 }
1127 $content = '';
1128 $classes = trim('coursebox clearfix '. $additionalclasses);
1129 if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) {
1130 $nametag = 'h3';
1131 } else {
1132 $classes .= ' collapsed';
1133 $nametag = 'div';
1134 }
1135 $content .= html_writer::start_tag('div', array('class' => $classes)); // .coursebox
1136
1137 $content .= html_writer::start_tag('div', array('class' => 'info'));
1138
1139 // course name
1140 $coursename = $chelper->get_course_formatted_name($course);
1141 $coursenamelink = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)),
1142 $coursename, array('class' => $course->visible ? '' : 'dimmed'));
1143 $content .= html_writer::tag($nametag, $coursenamelink, array('class' => 'name'));
1144
1145 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.
1146 $content .= html_writer::start_tag('div', array('class' => 'moreinfo'));
1147 if ($course->has_summary() || $course->has_course_contacts()) {
1148 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1149 $url = new moodle_url('/course/info.php', array('id' => $course->id));
1150 $image = html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/info'),
1151 'alt' => $this->strings->summary));
1152 $content .= $this->action_link($url, $image, new popup_action('click', $url, 'courseinfo'),
1153 array('title' => $this->strings->summary));
1154 }
1155 }
1156 $content .= html_writer::end_tag('div'); // .moreinfo
1157
1158 // print enrolmenticons
1159 if ($icons = enrol_get_course_info_icons($course)) {
1160 $content .= html_writer::start_tag('div', array('class' => 'enrolmenticons'));
1161 foreach ($icons as $pix_icon) {
1162 $content .= $this->render($pix_icon);
1163 }
1164 $content .= html_writer::end_tag('div'); // .enrolmenticons
1165 }
1166
1167 $content .= html_writer::end_tag('div'); // .info
1168
1169 $content .= html_writer::start_tag('div', array('class' => 'content'));
1170 $content .= $this->coursecat_coursebox_content($chelper, $course);
1171 $content .= html_writer::end_tag('div'); // .content
1172
1173 $content .= html_writer::end_tag('div'); // .coursebox
1174 return $content;
1175 }
1176
1177 /**
1178 * Returns HTML to display course content (summary, course contacts and optionally category name)
1179 *
1180 * This method is called from coursecat_coursebox() and may be re-used in AJAX
1181 *
1182 * @param coursecat_helper $chelper various display options
1183 * @param stdClass|course_in_list $course
1184 * @return string
1185 */
1186 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
1187 global $CFG;
1188 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
1189 return '';
1190 }
1191 if ($course instanceof stdClass) {
1192 require_once($CFG->libdir. '/coursecatlib.php');
1193 $course = new course_in_list($course);
1194 }
1195 $content = '';
1196
1197 // display course summary
1198 if ($course->has_summary()) {
1199 $content .= html_writer::start_tag('div', array('class' => 'summary'));
1200 $content .= $chelper->get_course_formatted_summary($course,
1201 array('overflowdiv' => true, 'noclean' => true, 'para' => false));
1202 $content .= html_writer::end_tag('div'); // .summary
1203 }
1204
1205 // display course contacts. See course_in_list::get_course_contacts()
1206 if ($course->has_course_contacts()) {
1207 $content .= html_writer::start_tag('ul', array('class' => 'teachers'));
1208 foreach ($course->get_course_contacts() as $userid => $coursecontact) {
1209 $name = $coursecontact['rolename'].': '.
1210 html_writer::link(new moodle_url('/user/view.php',
1211 array('id' => $userid, 'course' => SITEID)),
1212 $coursecontact['username']);
1213 $content .= html_writer::tag('li', $name);
1214 }
1215 $content .= html_writer::end_tag('ul'); // .teachers
1216 }
1217
1218 // display course category if necessary (for example in search results)
1219 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT
1220 && ($cat = coursecat::get($course->category, IGNORE_MISSING))) {
1221 $content .= html_writer::start_tag('div', array('class' => 'coursecat'));
1222 $content .= get_string('category').': '.
1223 html_writer::link(new moodle_url('/course/category.php', array('id' => $cat->id)),
1224 $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
1225 $content .= html_writer::end_tag('div'); // .coursecat
1226 }
1227
1228 return $content;
1229 }
1230
1231 /**
1232 * Renders the list of courses
1233 *
1234 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public
1235 * method from outside of the class
1236 *
1237 * If list of courses is specified in $courses; the argument $chelper is only used
1238 * to retrieve display options and attributes, only methods get_show_courses(),
1239 * get_courses_display_option() and get_and_erase_attributes() are called.
1240 *
1241 * @param coursecat_helper $chelper various display options
1242 * @param array $courses the list of courses to display
1243 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable),
1244 * defaulted to count($courses)
1245 * @return string
1246 */
1247 protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) {
1248 global $CFG;
1249 if ($totalcount === null) {
1250 $totalcount = count($courses);
1251 }
1252 if (!$totalcount) {
1253 // Courses count is cached during courses retrieval.
1254 return '';
1255 }
1256
1257 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) {
1258 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit
1259 if ($totalcount <= $CFG->courseswithsummarieslimit) {
1260 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
1261 } else {
1262 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1263 }
1264 }
1265
1266 // prepare content of paging bar if it is needed
1267 $paginationurl = $chelper->get_courses_display_option('paginationurl');
1268 $paginationallowall = $chelper->get_courses_display_option('paginationallowall');
1269 if ($totalcount > count($courses)) {
1270 // there are more results that can fit on one page
1271 if ($paginationurl) {
1272 // the option paginationurl was specified, display pagingbar
1273 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage);
1274 $page = $chelper->get_courses_display_option('offset') / $perpage;
1275 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1276 $paginationurl->out(false, array('perpage' => $perpage)));
1277 if ($paginationallowall) {
1278 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1279 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1280 }
1281 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1282 // the option for 'View more' link was specified, display more link
1283 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore'));
1284 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
1285 array('class' => 'paging paging-morelink'));
1286 }
1287 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1288 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1289 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1290 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1291 }
1292
1293 // display list of courses
1294 $attributes = $chelper->get_and_erase_attributes('courses');
1295 $content = html_writer::start_tag('div', $attributes);
1296
1297 if (!empty($pagingbar)) {
1298 $content .= $pagingbar;
1299 }
1300
1301 $coursecount = 0;
1302 foreach ($courses as $course) {
1303 $coursecount ++;
1304 $classes = ($coursecount%2) ? 'odd' : 'even';
1305 if ($coursecount == 1) {
1306 $classes .= ' first';
1307 }
1308 if ($coursecount >= count($courses)) {
1309 $classes .= ' last';
1310 }
1311 $content .= $this->coursecat_coursebox($chelper, $course, $classes);
1312 }
1313
1314 if (!empty($pagingbar)) {
1315 $content .= $pagingbar;
1316 }
1317 if (!empty($morelink)) {
1318 $content .= $morelink;
1319 }
1320
1321 $content .= html_writer::end_tag('div'); // .courses
1322 return $content;
1323 }
1324
1325 /**
1326 * Renders the list of subcategories in a category
1327 *
1328 * @param coursecat_helper $chelper various display options
1329 * @param coursecat $coursecat
1330 * @param int $depth depth of the category in the current tree
1331 * @return string
1332 */
1333 protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) {
1334 global $CFG;
1335 $subcategories = array();
1336 if (!$chelper->get_categories_display_option('nodisplay')) {
1337 $subcategories = $coursecat->get_children($chelper->get_categories_display_options());
1338 }
1339 $totalcount = $coursecat->get_children_count();
1340 if (!$totalcount) {
1341 // Note that we call get_child_categories_count() AFTER get_child_categories() to avoid extra DB requests.
1342 // Categories count is cached during children categories retrieval.
1343 return '';
1344 }
1345
1346 // prepare content of paging bar or more link if it is needed
1347 $paginationurl = $chelper->get_categories_display_option('paginationurl');
1348 $paginationallowall = $chelper->get_categories_display_option('paginationallowall');
1349 if ($totalcount > count($subcategories)) {
1350 if ($paginationurl) {
1351 // the option 'paginationurl was specified, display pagingbar
1352 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage);
1353 $page = $chelper->get_categories_display_option('offset') / $perpage;
1354 $pagingbar = $this->paging_bar($totalcount, $page, $perpage,
1355 $paginationurl->out(false, array('perpage' => $perpage)));
1356 if ($paginationallowall) {
1357 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')),
1358 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall'));
1359 }
1360 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) {
1361 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id)
1362 if ($viewmoreurl->compare(new moodle_url('/course/category.php'), URL_MATCH_BASE)) {
1363 if ($coursecat->id) {
1364 $viewmoreurl->param('id', $coursecat->id);
1365 } else {
1366 $viewmoreurl = new moodle_url('/course/index.php', $viewmoreurl->params());
1367 }
1368 }
1369 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore'));
1370 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext),
1371 array('class' => 'paging paging-morelink'));
1372 }
1373 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) {
1374 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode
1375 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)),
1376 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage'));
1377 }
1378
1379 // display list of subcategories
1380 $content = html_writer::start_tag('div', array('class' => 'subcategories'));
1381
1382 if (!empty($pagingbar)) {
1383 $content .= $pagingbar;
1384 }
1385
1386 foreach ($subcategories as $subcategory) {
1387 $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1);
1388 }
1389
1390 if (!empty($pagingbar)) {
1391 $content .= $pagingbar;
1392 }
1393 if (!empty($morelink)) {
1394 $content .= $morelink;
1395 }
1396
1397 $content .= html_writer::end_tag('div');
1398 return $content;
1399 }
1400
1401 /**
1402 * Returns HTML to display the subcategories and courses in the given category
1403 *
1404 * This method is re-used by AJAX to expand content of not loaded category
1405 *
1406 * @param coursecat_helper $chelper various display options
1407 * @param coursecat $coursecat
1408 * @param int $depth depth of the category in the current tree
1409 * @return string
1410 */
1411 protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) {
1412 $content = '';
1413 // Subcategories
1414 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth);
1415
1416 // AUTO show courses: Courses will be shown expanded if this is not nested category,
1417 // and number of courses no bigger than $CFG->courseswithsummarieslimit.
1418 $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO;
1419 if ($showcoursesauto && $depth) {
1420 // this is definitely collapsed mode
1421 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED);
1422 }
1423
1424 // Courses
1425 if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) {
1426 $courses = $coursecat->get_courses($chelper->get_courses_display_options());
1427 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) {
1428 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id)
1429 if ($viewmoreurl->compare(new moodle_url('/course/category.php'), URL_MATCH_BASE)) {
1430 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('id' => $coursecat->id)));
1431 }
1432 }
1433 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count());
1434 }
1435
1436 if ($showcoursesauto) {
1437 // restore the show_courses back to AUTO
1438 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO);
1439 }
1440
1441 return $content;
1442 }
1443
1444 /**
1445 * Returns HTML to display a course category as a part of a tree
1446 *
1447 * This is an internal function, to display a particular category and all its contents
1448 * use {@link core_course_renderer::course_category()}
1449 *
1450 * @param coursecat_helper $chelper various display options
1451 * @param coursecat $coursecat
1452 * @param int $depth depth of this category in the current tree
1453 * @return string
1454 */
1455 protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) {
1456 // open category tag
1457 $classes = array('category');
1458 if (empty($coursecat->visible)) {
1459 $classes[] = 'dimmed_category';
1460 }
1461 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) {
1462 // do not load content
1463 $categorycontent = '';
1464 $classes[] = 'notloaded';
1465 if ($coursecat->get_children_count() ||
1466 ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) {
1467 $classes[] = 'with_children';
1468 $classes[] = 'collapsed';
1469 }
1470 } else {
1471 // load category content
1472 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth);
1473 $classes[] = 'loaded';
1474 if (!empty($categorycontent)) {
1475 $classes[] = 'with_children';
1476 }
1477 }
1478 $content = html_writer::start_tag('div', array('class' => join(' ', $classes),
1479 'data-categoryid' => $coursecat->id,
1480 'data-depth' => $depth));
1481
1482 // category name
1483 $categoryname = $coursecat->get_formatted_name();
1484 $categoryname = html_writer::link(new moodle_url('/course/category.php',
1485 array('id' => $coursecat->id)),
1486 $categoryname);
1487 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT
1488 && ($coursescount = $coursecat->get_courses_count())) {
1489 $categoryname .= html_writer::tag('span', ' ('. $coursescount.')',
1490 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse'));
1491 }
1492 $content .= html_writer::start_tag('div', array('class' => 'info'));
1493 $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'name'));
1494 $content .= html_writer::end_tag('div'); // .info
1495
1496 // add category content to the output
1497 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1498
1499 $content .= html_writer::end_tag('div'); // .category
1500
1501 // Return the course category tree HTML
1502 return $content;
1503 }
1504
1505 /**
1506 * Returns HTML to display a tree of subcategories and courses in the given category
1507 *
1508 * @param coursecat_helper $chelper various display options
1509 * @param coursecat $coursecat top category (this category's name and description will NOT be added to the tree)
1510 * @return string
1511 */
1512 protected function coursecat_tree(coursecat_helper $chelper, $coursecat) {
1513 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0);
1514 if (empty($categorycontent)) {
1515 return '';
1516 }
1517
1518 // Generate an id and the required JS call to make this a nice widget
1519 $id = html_writer::random_id('course_category_tree');
1520 $this->page->requires->js_init_call('M.util.init_toggle_class_on_click',
1521 array($id, '.category.with_children.loaded > .info .name', 'collapsed', '.category.with_children.loaded'));
1522
1523 // Start content generation
1524 $content = '';
1525 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix');
1526 $content .= html_writer::start_tag('div',
1527 array('id' => $id, 'data-showcourses' => $chelper->get_show_courses()) + $attributes);
1528
1529 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content'));
1530
1531 if ($coursecat->get_children_count() && $chelper->get_subcat_depth() != 1) {
1532 // We don't need to display "Expand all"/"Collapse all" buttons if there are no
1533 // subcategories or there is only one level of subcategories loaded
1534 // TODO if subcategories are loaded by AJAX this might still be needed!
1535 $content .= html_writer::start_tag('div', array('class' => 'controls'));
1536 $content .= html_writer::tag('div', get_string('collapseall'), array('class' => 'addtoall expandall'));
1537 $content .= html_writer::tag('div', get_string('expandall'), array('class' => 'removefromall collapseall'));
1538 $content .= html_writer::end_tag('div');
1539 }
1540
1541 $content .= html_writer::end_tag('div'); // .course_category_tree
1542
1543 return $content;
1544 }
8e57a6df
MG
1545
1546 /**
1547 * Renders HTML to display particular course category - list of it's subcategories and courses
1548 *
1549 * Invoked from /course/index.php
1550 *
1551 * @param int|stdClass|coursecat $category
1552 */
1553 public function course_category($category) {
1554 global $CFG;
1555 require_once($CFG->libdir. '/coursecatlib.php');
1556 $coursecat = coursecat::get(is_object($category) ? $category->id : $category);
1557 $site = get_site();
1558 $output = '';
1559
1560 if (!$coursecat->id && coursecat::count_all() == 1) {
1561 // There exists only one category in the system, do not display link to it
1562 $coursecat = coursecat::get_default();
1563 $strfulllistofcourses = get_string('fulllistofcourses');
1564 $this->page->set_title("$site->shortname: $strfulllistofcourses");
1565 } else if (!$coursecat->id) {
1566 $strcategories = get_string('categories');
1567 $this->page->set_title("$site->shortname: $strcategories");
1568 } else {
1569 $this->page->set_title("$site->shortname: ". $coursecat->get_formatted_name());
1570 $this->page->set_button($this->course_search_form('', 'navbar'));
1571
1572 // Print the category selector
1573 $output .= html_writer::start_tag('div', array('class' => 'categorypicker'));
1574 $select = new single_select(new moodle_url('/course/category.php'), 'id',
1575 coursecat::make_categories_list(), $coursecat->id, null, 'switchcategory');
1576 $select->set_label(get_string('categories').':');
1577 $output .= $this->render($select);
1578 $output .= html_writer::end_tag('div'); // .categorypicker
1579 }
1580
1581 // Print current category description
1582 $chelper = new coursecat_helper();
1583 if ($description = $chelper->get_category_formatted_description($coursecat)) {
1584 $output .= $this->box($description, array('class' => 'generalbox info'));
1585 }
1586
1587 // Prepare parameters for courses and categories lists in the tree
1588 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO)
1589 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id));
1590
1591 $coursedisplayoptions = array();
1592 $catdisplayoptions = array();
1593 $browse = optional_param('browse', null, PARAM_ALPHA);
1594 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
1595 $page = optional_param('page', 0, PARAM_INT);
1596 if ($coursecat->id) {
1597 $baseurl = new moodle_url('/course/category.php', array('id' => $coursecat->id));
1598 } else {
1599 $baseurl = new moodle_url('/course/index.php');
1600 }
1601 if ($browse === 'courses' || !$coursecat->has_children()) {
1602 $coursedisplayoptions['limit'] = $perpage;
1603 $coursedisplayoptions['offset'] = $page * $perpage;
1604 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'perpage' => $perpage));
1605 $catdisplayoptions['nodisplay'] = true;
1606 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 0));
1607 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategores');
1608 } else if ($browse === 'categories' || !$coursecat->has_courses()) {
1609 $coursedisplayoptions['nodisplay'] = true;
1610 $catdisplayoptions['limit'] = $perpage;
1611 $catdisplayoptions['offset'] = $page * $perpage;
1612 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'perpage' => $perpage));
1613 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 0));
1614 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses');
1615 } else {
1616 // we have a category that has both subcategories and courses, display pagination separately
1617 $coursedisplayoptions['limit'] = $CFG->coursesperpage;
1618 $catdisplayoptions['limit'] = $CFG->coursesperpage;
1619 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1));
1620 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1));
1621 }
1622 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions);
1623
1624 // Display course category tree
1625 $output .= $this->coursecat_tree($chelper, $coursecat);
1626
1627 // Add course search form (if we are inside category it was already added to the navbar)
1628 if (!$coursecat->id) {
1629 $output .= $this->course_search_form();
1630 }
1631
1632 // Add action buttons
1633 $output .= $this->container_start('buttons');
1634 $context = get_category_or_system_context($coursecat->id);
1635 if (has_capability('moodle/course:create', $context)) {
1636 // Print link to create a new course, for the 1st available category.
1637 if ($coursecat->id) {
1638 $url = new moodle_url('/course/edit.php', array('category' => $coursecat->id, 'returnto' => 'category'));
1639 } else {
1640 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat'));
1641 }
1642 $output .= $this->single_button($url, get_string('addnewcourse'), 'get');
1643 }
1644 ob_start();
1645 if (coursecat::count_all() == 1) {
1646 print_course_request_buttons(context_system::instance());
1647 } else {
1648 print_course_request_buttons($context);
1649 }
1650 $output .= ob_get_contents();
1651 ob_end_clean();
1652 $output .= $this->container_end();
1653
1654 return $output;
1655 }
60047003
MG
1656
1657 /**
1658 * Renders html to display search result page
1659 *
1660 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid
1661 * @return string
1662 */
1663 public function search_courses($searchcriteria) {
1664 global $CFG;
1665 $content = '';
1666 if (!empty($searchcriteria)) {
1667 // print search results
1668
1669 $displayoptions = array('sort' => array('displayname' => 1));
1670 // take the current page and number of results per page from query
1671 $perpage = optional_param('perpage', 0, PARAM_RAW);
1672 if ($perpage !== 'all') {
1673 $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage;
1674 $page = optional_param('page', 0, PARAM_INT);
1675 $displayoptions['offset'] = $displayoptions['limit'] * $page;
1676 }
1677 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses()
1678 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria);
1679 $displayoptions['paginationallowall'] = true; // allow adding link 'View all'
1680
1681 $class = 'course-search-result';
1682 foreach ($searchcriteria as $key => $value) {
1683 if (!empty($value)) {
1684 $class .= ' course-search-result-'. $key;
1685 }
1686 }
1687 $chelper = new coursecat_helper();
1688 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1689 set_courses_display_options($displayoptions)->
1690 set_search_criteria($searchcriteria)->
1691 set_attributes(array('class' => $class));
1692
1693 $courses = coursecat::search_courses($searchcriteria, $chelper->get_courses_display_options());
1694 $totalcount = coursecat::search_courses_count($searchcriteria);
1695 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount);
1696
1697 if (!$totalcount) {
1698 if (!empty($searchcriteria['search'])) {
1699 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search']));
1700 } else {
1701 $content .= $this->heading(get_string('novalidcourses'));
1702 }
1703 } else {
1704 $content .= $this->heading(get_string('searchresults'). ": $totalcount");
1705 $content .= $courseslist;
1706 }
1707
1708 if (!empty($searchcriteria['search'])) {
1709 // print search form only if there was a search by search string, otherwise it is confusing
1710 $content .= $this->box_start('generalbox mdl-align');
1711 $content .= $this->course_search_form($searchcriteria['search']);
1712 $content .= $this->box_end();
1713 }
1714 } else {
1715 // just print search form
1716 $content .= $this->box_start('generalbox mdl-align');
1717 $content .= $this->course_search_form();
1718 $content .= html_writer::tag('div', get_string("searchhelp"), array('class' => 'searchhelp'));
1719 $content .= $this->box_end();
1720 }
1721 return $content;
1722 }
9e76429d
MG
1723
1724 /**
1725 * Renders html to print list of courses tagged with particular tag
1726 *
1727 * @param int $tagid id of the tag
1728 * @return string empty string if no courses are marked with this tag or rendered list of courses
1729 */
1730 public function tagged_courses($tagid) {
1731 global $CFG;
1732 $displayoptions = array('limit' => $CFG->coursesperpage);
1733 $displayoptions['viewmoreurl'] = new moodle_url('/course/search.php',
1734 array('tagid' => $tagid, 'page' => 1, 'perpage' => $CFG->coursesperpage));
1735 $displayoptions['viewmoretext'] = new lang_string('findmorecourses');
1736 $chelper = new coursecat_helper();
1737 $searchcriteria = array('tagid' => $tagid);
1738 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->
1739 set_search_criteria(array('tagid' => $tagid))->
1740 set_courses_display_options($displayoptions)->
1741 set_attributes(array('class' => 'course-search-result course-search-result-tagid'));
1742 // (we set the same css class as in search results by tagid)
1743 $courses = coursecat::search_courses($searchcriteria, $chelper->get_courses_display_options());
1744 $totalcount = coursecat::search_courses_count($searchcriteria);
1745 $content = $this->coursecat_courses($chelper, $courses, $totalcount);
1746 if ($totalcount) {
1747 require_once $CFG->dirroot.'/tag/lib.php';
1748 $heading = get_string('courses') . ' ' . get_string('taggedwith', 'tag', tag_get_name($tagid)) .': '. $totalcount;
1749 return $this->heading($heading, 3). $content;
1750 }
1751 return '';
1752 }
99a36456
MG
1753
1754 /**
1755 * Returns HTML to display one remote course
1756 *
1757 * @param stdClass $course remote course information, contains properties:
1758 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname
1759 * @return string
1760 */
1761 protected function frontpage_remote_course(stdClass $course) {
1762 $url = new moodle_url('/auth/mnet/jump.php', array(
1763 'hostid' => $course->hostid,
1764 'wantsurl' => '/course/view.php?id='. $course->remoteid
1765 ));
1766
1767 $output = '';
1768 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix'));
1769 $output .= html_writer::start_tag('div', array('class' => 'info'));
1770 $output .= html_writer::start_tag('h3', array('class' => 'name'));
1771 $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse')));
1772 $output .= html_writer::end_tag('h3'); // .name
1773 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1774 $output .= html_writer::end_tag('div'); // .info
1775 $output .= html_writer::start_tag('div', array('class' => 'content'));
1776 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1777 $options = new stdClass();
1778 $options->noclean = true;
1779 $options->para = false;
1780 $options->overflowdiv = true;
1781 $output .= format_text($course->summary, $course->summaryformat, $options);
1782 $output .= html_writer::end_tag('div'); // .summary
1783 $addinfo = format_string($course->hostname) . ' : '
1784 . format_string($course->cat_name) . ' : '
1785 . format_string($course->shortname);
1786 $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo'));
1787 $output .= html_writer::end_tag('div'); // .content
1788 $output .= html_writer::end_tag('div'); // .coursebox
1789 return $output;
1790 }
1791
1792 /**
1793 * Returns HTML to display one remote host
1794 *
1795 * @param array $host host information, contains properties: name, url, count
1796 * @return string
1797 */
1798 protected function frontpage_remote_host($host) {
1799 $output = '';
1800 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix'));
1801 $output .= html_writer::start_tag('div', array('class' => 'info'));
1802 $output .= html_writer::start_tag('h3', array('class' => 'name'));
1803 $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name'])));
1804 $output .= html_writer::end_tag('h3'); // .name
1805 $output .= html_writer::tag('div', '', array('class' => 'moreinfo'));
1806 $output .= html_writer::end_tag('div'); // .info
1807 $output .= html_writer::start_tag('div', array('class' => 'content'));
1808 $output .= html_writer::start_tag('div', array('class' => 'summary'));
1809 $output .= $host['count'] . ' ' . get_string('courses');
1810 $output .= html_writer::end_tag('div'); // .content
1811 $output .= html_writer::end_tag('div'); // .coursebox
1812 return $output;
1813 }
1814
1815 /**
1816 * Returns HTML to print list of courses user is enrolled to for the frontpage
1817 *
1818 * Also lists remote courses or remote hosts if MNET authorisation is used
1819 *
1820 * @return string
1821 */
1822 public function frontpage_my_courses() {
1823 global $USER, $CFG, $DB;
1824
1825 if (!isloggedin() or isguestuser()) {
1826 return '';
1827 }
1828
1829 $output = '';
1830 if (!empty($CFG->navsortmycoursessort)) {
1831 // sort courses the same as in navigation menu
1832 $sortorder = 'visible DESC,'. $CFG->navsortmycoursessort.' ASC';
1833 } else {
1834 $sortorder = 'visible DESC,sortorder ASC';
1835 }
1836 $courses = enrol_get_my_courses('summary, summaryformat', $sortorder);
1837 $rhosts = array();
1838 $rcourses = array();
1839 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') {
1840 $rcourses = get_my_remotecourses($USER->id);
1841 $rhosts = get_my_remotehosts();
1842 }
1843
1844 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) {
1845
1846 $chelper = new coursecat_helper();
1847 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1848 set_courses_display_options(array(
1849 'viewmoreurl' => new moodle_url('/course/index.php'),
1850 'viewmoretext' => new lang_string('fulllistofcourses')
1851 ))->
1852 set_attributes(array('class' => 'frontpage-course-list-enrolled'));
1853 $totalcount = $DB->count_records('course') - 1;
1854 $output .= $this->coursecat_courses($chelper, $courses, $totalcount);
1855
1856 // MNET
1857 if (!empty($rcourses)) {
1858 // at the IDP, we know of all the remote courses
1859 $output .= html_writer::start_tag('div', array('class' => 'courses'));
1860 foreach ($rcourses as $course) {
1861 $output .= $this->frontpage_remote_course($course);
1862 }
1863 $output .= html_writer::end_tag('div'); // .courses
1864 } elseif (!empty($rhosts)) {
1865 // non-IDP, we know of all the remote servers, but not courses
1866 $output .= html_writer::start_tag('div', array('class' => 'courses'));
1867 foreach ($rhosts as $host) {
1868 $output .= $this->frontpage_remote_host($host);
1869 }
1870 $output .= html_writer::end_tag('div'); // .courses
1871 }
1872 }
1873 return $output;
1874 }
1875
1876 /**
1877 * Returns HTML to print list of available courses for the frontpage
1878 *
1879 * @return string
1880 */
1881 public function frontpage_available_courses() {
1882 global $CFG;
1883 require_once($CFG->libdir. '/coursecatlib.php');
1884
1885 $chelper = new coursecat_helper();
1886 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)->
1887 set_courses_display_options(array(
1888 'recursive' => true,
1889 'limit' => FRONTPAGECOURSELIMIT,
1890 'viewmoreurl' => new moodle_url('/course/index.php'),
1891 'viewmoretext' => new lang_string('fulllistofcourses')));
1892
1893 $chelper->set_attributes(array('class' => 'frontpage-course-list-all'));
1894 $courses = coursecat::get(0)->get_courses($chelper->get_courses_display_options());
1895 $totalcount = coursecat::get(0)->get_courses_count($chelper->get_courses_display_options());
1896 return $this->coursecat_courses($chelper, $courses, $totalcount);
1897 }
1898
1899 /**
1900 * Returns HTML to print tree with course categories and courses for the frontpage
1901 *
1902 * @return string
1903 */
1904 public function frontpage_combo_list() {
1905 global $CFG;
1906 require_once($CFG->libdir. '/coursecatlib.php');
1907 $chelper = new coursecat_helper();
1908 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1909 set_categories_display_options(array(
1910 'limit' => $CFG->coursesperpage,
1911 'viewmoreurl' => new moodle_url('/course/category.php',
1912 array('browse' => 'categories', 'page' => 1))
1913 ))->
1914 set_courses_display_options(array(
1915 'limit' => $CFG->coursesperpage,
1916 'viewmoreurl' => new moodle_url('/course/category.php',
1917 array('browse' => 'courses', 'page' => 1))
1918 ))->
1919 set_attributes(array('class' => 'frontpage-category-combo'));
1920 return $this->coursecat_tree($chelper, coursecat::get(0));
1921 }
1922
1923 /**
1924 * Returns HTML to print tree of course categories (with number of courses) for the frontpage
1925 *
1926 * @return string
1927 */
1928 public function frontpage_categories_list() {
1929 global $CFG;
1930 require_once($CFG->libdir. '/coursecatlib.php');
1931 $chelper = new coursecat_helper();
1932 $chelper->set_subcat_depth($CFG->maxcategorydepth)->
1933 set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)->
1934 set_categories_display_options(array(
1935 'limit' => $CFG->coursesperpage,
1936 'viewmoreurl' => new moodle_url('/course/category.php',
1937 array('browse' => 'categories', 'page' => 1))
1938 ))->
1939 set_attributes(array('class' => 'frontpage-category-names'));
1940 return $this->coursecat_tree($chelper, coursecat::get(0));
1941 }
43e389ea
MG
1942}
1943
1944/**
1945 * Class storing display options and functions to help display course category and/or courses lists
1946 *
1947 * This is a wrapper for coursecat objects that also stores display options
1948 * and functions to retrieve sorted and paginated lists of categories/courses.
1949 *
1950 * If theme overrides methods in core_course_renderers that access this class
1951 * it may as well not use this class at all or extend it.
1952 *
1953 * @package core
1954 * @copyright 2013 Marina Glancy
1955 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1956 */
1957class coursecat_helper {
1958 /** @var string [none, collapsed, expanded] how (if) display courses list */
1959 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */
1960 /** @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) */
1961 protected $subcatdepth = 1;
1962 /** @var array options to display courses list */
1963 protected $coursesdisplayoptions = array();
1964 /** @var array options to display subcategories list */
1965 protected $categoriesdisplayoptions = array();
1966 /** @var array additional HTML attributes */
1967 protected $attributes = array();
1968 /** @var array search criteria if the list is a search result */
1969 protected $searchcriteria = null;
1970
1971 /**
1972 * Sets how (if) to show the courses - none, collapsed, expanded, etc.
1973 *
1974 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc.
1975 * @return coursecat_helper
1976 */
1977 public function set_show_courses($showcourses) {
1978 $this->showcourses = $showcourses;
1979 // Automatically set the options to preload summary and coursecontacts for coursecat::get_courses() and coursecat::search_courses()
1980 $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO;
1981 $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED;
1982 return $this;
1983 }
1984
1985 /**
1986 * Returns how (if) to show the courses - none, collapsed, expanded, etc.
1987 *
1988 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc.
1989 */
1990 public function get_show_courses() {
1991 return $this->showcourses;
1992 }
1993
1994 /**
1995 * Sets the maximum depth to expand subcategories in the tree
1996 *
1997 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
1998 *
1999 * @param int $subcatdepth
2000 * @return coursecat_helper
2001 */
2002 public function set_subcat_depth($subcatdepth) {
2003 $this->subcatdepth = $subcatdepth;
2004 return $this;
2005 }
2006
2007 /**
2008 * Returns the maximum depth to expand subcategories in the tree
2009 *
2010 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name
2011 *
2012 * @return int
2013 */
2014 public function get_subcat_depth() {
2015 return $this->subcatdepth;
2016 }
2017
2018 /**
2019 * Sets options to display list of courses
2020 *
2021 * Options are later submitted as argument to coursecat::get_courses() and/or coursecat::search_courses()
2022 *
2023 * Options that coursecat::get_courses() accept:
2024 * - recursive - return courses from subcategories as well. Use with care,
2025 * this may be a huge list!
2026 * - summary - preloads fields 'summary' and 'summaryformat'
2027 * - coursecontacts - preloads course contacts
2028 * - isenrolled - preloads indication whether this user is enrolled in the course
2029 * - sort - list of fields to sort. Example
2030 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
2031 * will sort by idnumber asc, shortname asc and id desc.
2032 * Default: array('sortorder' => 1)
2033 * Only cached fields may be used for sorting!
2034 * - offset
2035 * - limit - maximum number of children to return, 0 or null for no limit
2036 *
2037 * Options summary and coursecontacts are filled automatically in the set_show_courses()
2038 *
2039 * Also renderer can set here any additional options it wants to pass between renderer functions.
2040 *
2041 * @param array $options
2042 * @return coursecat_helper
2043 */
2044 public function set_courses_display_options($options) {
2045 $this->coursesdisplayoptions = $options;
2046 $this->set_show_courses($this->showcourses); // this will calculate special display options
2047 return $this;
2048 }
2049
2050 /**
2051 * Sets one option to display list of courses
2052 *
2053 * @see coursecat_helper::set_courses_display_options()
2054 *
2055 * @param string $key
2056 * @param mixed $value
2057 * @return coursecat_helper
2058 */
2059 public function set_courses_display_option($key, $value) {
2060 $this->coursesdisplayoptions[$key] = $value;
2061 return $this;
2062 }
2063
2064 /**
2065 * Return the specified option to display list of courses
2066 *
2067 * @param string $optionname option name
2068 * @param mixed $defaultvalue default value for option if it is not specified
2069 * @return mixed
2070 */
2071 public function get_courses_display_option($optionname, $defaultvalue = null) {
2072 if (array_key_exists($optionname, $this->coursesdisplayoptions)) {
2073 return $this->coursesdisplayoptions[$optionname];
2074 } else {
2075 return $defaultvalue;
2076 }
2077 }
2078
2079 /**
2080 * Returns all options to display the courses
2081 *
2082 * This array is usually passed to {@link coursecat::get_courses()} or
2083 * {@link coursecat::search_courses()}
2084 *
2085 * @return array
2086 */
2087 public function get_courses_display_options() {
2088 return $this->coursesdisplayoptions;
2089 }
2090
2091 /**
2092 * Sets options to display list of subcategories
2093 *
2094 * Options 'sort', 'offset' and 'limit' are passed to coursecat::get_children().
2095 * Any other options may be used by renderer functions
2096 *
2097 * @param array $options
2098 * @return coursecat_helper
2099 */
2100 public function set_categories_display_options($options) {
2101 $this->categoriesdisplayoptions = $options;
2102 return $this;
2103 }
2104
2105 /**
2106 * Return the specified option to display list of subcategories
2107 *
2108 * @param string $optionname option name
2109 * @param mixed $defaultvalue default value for option if it is not specified
2110 * @return mixed
2111 */
2112 public function get_categories_display_option($optionname, $defaultvalue = null) {
2113 if (array_key_exists($optionname, $this->categoriesdisplayoptions)) {
2114 return $this->categoriesdisplayoptions[$optionname];
2115 } else {
2116 return $defaultvalue;
2117 }
2118 }
2119
2120 /**
2121 * Returns all options to display list of subcategories
2122 *
2123 * This array is usually passed to {@link coursecat::get_children()}
2124 *
2125 * @return array
2126 */
2127 public function get_categories_display_options() {
2128 return $this->categoriesdisplayoptions;
2129 }
2130
2131 /**
2132 * Sets additional general options to pass between renderer functions, usually HTML attributes
2133 *
2134 * @param array $attributes
2135 * @return coursecat_helper
2136 */
2137 public function set_attributes($attributes) {
2138 $this->attributes = $attributes;
2139 return $this;
2140 }
2141
2142 /**
2143 * Return all attributes and erases them so they are not applied again
2144 *
2145 * @param string $classname adds additional class name to the beginning of $attributes['class']
2146 * @return array
2147 */
2148 public function get_and_erase_attributes($classname) {
2149 $attributes = $this->attributes;
2150 $this->attributes = array();
2151 if (empty($attributes['class'])) {
2152 $attributes['class'] = '';
2153 }
2154 $attributes['class'] = $classname . ' '. $attributes['class'];
2155 return $attributes;
2156 }
2157
2158 /**
2159 * Sets the search criteria if the course is a search result
2160 *
2161 * Search string will be used to highlight terms in course name and description
2162 *
2163 * @param array $searchcriteria
2164 * @return coursecat_helper
2165 */
2166 public function set_search_criteria($searchcriteria) {
2167 $this->searchcriteria = $searchcriteria;
2168 return $this;
2169 }
2170
2171 /**
2172 * Returns formatted and filtered description of the given category
2173 *
2174 * @param coursecat $coursecat category
2175 * @param stdClass|array $options format options, by default [noclean,overflowdiv],
2176 * if context is not specified it will be added automatically
2177 * @return string|null
2178 */
2179 public function get_category_formatted_description($coursecat, $options = null) {
2180 if ($coursecat->id && !empty($coursecat->description)) {
2181 if (!isset($coursecat->descriptionformat)) {
2182 $descriptionformat = FORMAT_MOODLE;
2183 } else {
2184 $descriptionformat = $coursecat->descriptionformat;
2185 }
2186 if ($options === null) {
2187 $options = array('noclean' => true, 'overflowdiv' => true);
2188 } else {
2189 $options = (array)$options;
2190 }
2191 $context = context_coursecat::instance($coursecat->id);
2192 if (!isset($options['context'])) {
2193 $options['context'] = $context;
2194 }
2195 $text = file_rewrite_pluginfile_urls($coursecat->description,
2196 'pluginfile.php', $context->id, 'coursecat', 'description', null);
2197 return format_text($text, $descriptionformat, $options);
2198 }
2199 return null;
2200 }
2201
2202 /**
2203 * Returns given course's summary with proper embedded files urls and formatted
2204 *
2205 * @param course_in_list $course
2206 * @param array|stdClass $options additional formatting options
2207 * @return string
2208 */
2209 public function get_course_formatted_summary($course, $options = array()) {
2210 global $CFG;
2211 require_once($CFG->libdir. '/filelib.php');
2212 if (!$course->has_summary()) {
2213 return '';
2214 }
2215 $options = (array)$options;
2216 $context = context_course::instance($course->id);
2217 if (!isset($options['context'])) {
2218 // TODO see MDL-38521
2219 // option 1 (current), page context - no code required
2220 // option 2, system context
2221 // $options['context'] = context_system::instance();
2222 // option 3, course context:
2223 // $options['context'] = $context;
2224 // option 4, course category context:
2225 // $options['context'] = $context->get_parent_context();
2226 }
2227 $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null);
2228 $summary = format_text($summary, $course->summaryformat, $options, $course->id);
2229 if (!empty($this->searchcriteria['search'])) {
2230 $summary = highlight($this->searchcriteria['search'], $summary);
2231 }
2232 return $summary;
2233 }
2234
2235 /**
2236 * Returns course name as it is configured to appear in courses lists formatted to course context
2237 *
2238 * @param course_in_list $course
2239 * @param array|stdClass $options additional formatting options
2240 * @return string
2241 */
2242 public function get_course_formatted_name($course, $options = array()) {
2243 $options = (array)$options;
2244 if (!isset($options['context'])) {
2245 $options['context'] = context_course::instance($course->id);
2246 }
2247 $name = format_string(get_course_display_name_for_list($course), true, $options);
2248 if (!empty($this->searchcriteria['search'])) {
2249 $name = highlight($this->searchcriteria['search'], $name);
2250 }
2251 return $name;
2252 }
f4c23f03 2253}