2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Base renderer for outputting course formats.
21 * @copyright 2012 Dan Poltawski
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
30 * This is a convenience renderer which can be used by section based formats
31 * to reduce code duplication. It is not necessary for all course formats to
32 * use this and its likely to change in future releases.
35 * @copyright 2012 Dan Poltawski
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 abstract class format_section_renderer_base extends plugin_renderer_base {
42 * Generate the starting container html for a list of sections
43 * @return string HTML to output.
45 abstract protected function start_section_list();
48 * Generate the closing container html for a list of sections
49 * @return string HTML to output.
51 abstract protected function end_section_list();
54 * Generate the title for this section page
55 * @return string the page title
57 abstract protected function page_title();
60 * Generate the content to displayed on the right part of a section
61 * before course modules are included
63 * @param stdClass $section The course_section entry from DB
64 * @param stdClass $course The course entry from DB
65 * @param bool $onsectionpage true if being printed on a section page
66 * @return string HTML to output.
68 protected function section_right_content($section, $course, $onsectionpage) {
69 $o = $this->output->spacer();
71 if ($section->section != 0) {
72 $controls = $this->section_edit_controls($course, $section, $onsectionpage);
73 if (!empty($controls)) {
74 $o = implode('<br />', $controls);
82 * Generate the content to displayed on the left part of a section
83 * before course modules are included
85 * @param stdClass $section The course_section entry from DB
86 * @param stdClass $course The course entry from DB
87 * @param bool $onsectionpage true if being printed on a section page
88 * @return string HTML to output.
90 protected function section_left_content($section, $course, $onsectionpage) {
91 $o = $this->output->spacer();
93 if ($section->section != 0) {
94 // Only in the non-general sections.
95 if ($course->marker == $section->section) {
96 $o = get_accesshide(get_string('currentsection', 'format_'.$course->format));
104 * Generate the display of the header part of a section before
105 * course modules are included
107 * @param stdClass $section The course_section entry from DB
108 * @param stdClass $course The course entry from DB
109 * @param bool $onsectionpage true if being printed on a section page
110 * @return string HTML to output.
112 protected function section_header($section, $course, $onsectionpage) {
120 if ($section->section != 0) {
121 // Only in the non-general sections.
122 if (!$section->visible) {
123 $sectionstyle = ' hidden';
124 } else if ($course->marker == $section->section) {
125 $sectionstyle = ' current';
127 $linktitle = ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE);
130 $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
131 'class' => 'section main clearfix'.$sectionstyle));
133 $leftcontent = $this->section_left_content($section, $course, $onsectionpage);
134 $o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
136 $rightcontent = $this->section_right_content($section, $course, $onsectionpage);
137 $o.= html_writer::tag('div', $rightcontent, array('class' => 'right side'));
138 $o.= html_writer::start_tag('div', array('class' => 'content'));
140 if (!$onsectionpage) {
141 $title = get_section_name($course, $section);
143 $title = html_writer::link(course_get_url($course, $section->section), $title);
145 $o.= $this->output->heading($title, 3, 'sectionname');
148 $o.= html_writer::start_tag('div', array('class' => 'summary'));
149 $o.= $this->format_summary_text($section);
151 $context = context_course::instance($course->id);
152 if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $context)) {
153 $url = new moodle_url('/course/editsection.php', array('id'=>$section->id));
155 if ($onsectionpage) {
156 $url->param('sectionreturn', 1);
159 $o.= html_writer::link($url,
160 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/edit'), 'class' => 'iconsmall edit')),
161 array('title' => get_string('editsummary')));
163 $o.= html_writer::end_tag('div');
165 $o .= $this->section_availability_message($section);
171 * Generate the display of the footer part of a section
173 * @return string HTML to output.
175 protected function section_footer() {
176 $o = html_writer::end_tag('div');
177 $o.= html_writer::end_tag('li');
183 * Generate the edit controls of a section
185 * @param stdClass $course The course entry from DB
186 * @param stdClass $section The course_section entry from DB
187 * @param bool $onsectionpage true if being printed on a section page
188 * @return array of links with edit controls
190 protected function section_edit_controls($course, $section, $onsectionpage = false) {
193 if (!$PAGE->user_is_editing()) {
197 if (!has_capability('moodle/course:update', context_course::instance($course->id))) {
201 if ($onsectionpage) {
202 $baseurl = course_get_url($course, $section->section);
204 $baseurl = course_get_url($course);
206 $baseurl->param('sesskey', sesskey());
210 $url = clone($baseurl);
211 if ($section->visible) { // Show the hide/show eye.
212 $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
213 $url->param('hide', $section->section);
214 $controls[] = html_writer::link($url,
215 html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'),
216 'class' => 'icon hide', 'alt' => $strhidefromothers)),
217 array('title' => $strhidefromothers, 'class' => 'editing_showhide'));
219 $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
220 $url->param('show', $section->section);
221 $controls[] = html_writer::link($url,
222 html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'),
223 'class' => 'icon hide', 'alt' => $strshowfromothers)),
224 array('title' => $strshowfromothers, 'class' => 'editing_showhide'));
227 if (!$onsectionpage) {
228 $url = clone($baseurl);
229 if ($section->section > 1) { // Add a arrow to move section up.
230 $url->param('section', $section->section);
231 $url->param('move', -1);
232 $strmoveup = get_string('moveup');
234 $controls[] = html_writer::link($url,
235 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/up'),
236 'class' => 'icon up', 'alt' => $strmoveup)),
237 array('title' => $strmoveup, 'class' => 'moveup'));
240 $url = clone($baseurl);
241 if ($section->section < $course->numsections) { // Add a arrow to move section down.
242 $url->param('section', $section->section);
243 $url->param('move', 1);
244 $strmovedown = get_string('movedown');
246 $controls[] = html_writer::link($url,
247 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/down'),
248 'class' => 'icon down', 'alt' => $strmovedown)),
249 array('title' => $strmovedown, 'class' => 'movedown'));
257 * Generate a summary of a section for display on the 'coruse index page'
259 * @param stdClass $section The course_section entry from DB
260 * @param stdClass $course The course entry from DB
261 * @return string HTML to output.
263 protected function section_summary($section, $course) {
266 $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
267 'class' => 'section-summary clearfix'));
269 $title = get_section_name($course, $section);
270 $o.= html_writer::start_tag('a', array('href' => course_get_url($course, $section->section)));
271 $o.= $this->output->heading($title, 3, 'header section-title');
272 $o.= html_writer::end_tag('a');
274 $o.= html_writer::start_tag('div', array('class' => 'summarytext'));
275 $o.= $this->format_summary_text($section);
276 $o.= html_writer::end_tag('div');
278 $o .= $this->section_availability_message($section);
280 $o.= html_writer::end_tag('li');
286 * If section is not visible to current user, display the message about that
287 * ('Not available until...', that sort of thing). Otherwise, returns blank.
289 * @param stdClass $section The course_section entry from DB
290 * @return string HTML to output
292 protected function section_availability_message($section) {
294 if (!$section->uservisible) {
295 $o .= html_writer::start_tag('div', array('class' => 'availabilityinfo'));
296 if (!empty($section->availableinfo)) {
297 $o .= $section->availableinfo;
299 $o .= get_string('notavailable');
301 $o .= html_writer::end_tag('div');
307 * Show if something is on on the course clipboard (moving around)
309 * @param stdClass $course The course entry from DB
310 * @param int $sectionno The section number in the coruse which is being dsiplayed
311 * @return string HTML to output.
313 protected function course_activity_clipboard($course, $sectionno = 0) {
317 // If currently moving a file then show the current clipboard.
318 if (ismoving($course->id)) {
319 $url = new moodle_url('/course/mod.php',
320 array('sesskey' => sesskey(),
321 'cancelcopy' => true,
326 $strcancel= get_string('cancel');
328 $o.= html_writer::start_tag('li', array('class' => 'clipboard'));
329 $o.= strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
330 $o.= ' ('.html_writer::link($url, get_string('cancel')).')';
331 $o.= html_writer::end_tag('li');
338 * Generate next/previous section links for naviation
340 * @param stdClass $course The course entry from DB
341 * @param array $sections The course_sections entries from the DB
342 * @param int $sectionno The section number in the coruse which is being dsiplayed
343 * @return array associative array with previous and next section link
345 protected function get_nav_links($course, $sections, $sectionno) {
346 // FIXME: This is really evil and should by using the navigation API.
347 $canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id))
348 or !$course->hiddensections;
350 $links = array('previous' => '', 'next' => '');
351 $back = $sectionno - 1;
352 while ($back > 0 and empty($links['previous'])) {
353 if ($canviewhidden || $sections[$back]->visible) {
354 $previouslink = html_writer::tag('span', $this->output->larrow(), array('class' => 'larrow'));
355 $previouslink .= get_section_name($course, $sections[$back]);
356 $links['previous'] = html_writer::link(course_get_url($course, $back), $previouslink);
361 $forward = $sectionno + 1;
362 while ($forward <= $course->numsections and empty($links['next'])) {
363 if ($canviewhidden || $sections[$forward]->visible) {
364 $nextlink = get_section_name($course, $sections[$forward]);
365 $nextlink .= html_writer::tag('span', $this->output->rarrow(), array('class' => 'rarrow'));
366 $links['next'] = html_writer::link(course_get_url($course, $forward), $nextlink);
375 * Generate the header html of a stealth section
377 * @param int $sectionno The section number in the coruse which is being dsiplayed
378 * @return string HTML to output.
380 protected function stealth_section_header($sectionno) {
382 $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix orphaned hidden'));
383 $o.= html_writer::tag('div', '', array('class' => 'left side'));
384 $o.= html_writer::tag('div', '', array('class' => 'right side'));
385 $o.= html_writer::start_tag('div', array('class' => 'content'));
386 $o.= $this->output->heading(get_string('orphanedactivities'), 3, 'sectionname');
391 * Generate footer html of a stealth section
393 * @return string HTML to output.
395 protected function stealth_section_footer() {
396 $o = html_writer::end_tag('div');
397 $o.= html_writer::end_tag('li');
402 * Generate the html for a hidden section
404 * @param int $sectionno The section number in the coruse which is being dsiplayed
405 * @return string HTML to output.
407 protected function section_hidden($sectionno) {
409 $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix hidden'));
410 $o.= html_writer::tag('div', '', array('class' => 'left side'));
411 $o.= html_writer::tag('div', '', array('class' => 'right side'));
412 $o.= html_writer::start_tag('div', array('class' => 'content'));
413 $o.= get_string('notavailable');
414 $o.= html_writer::end_tag('div');
415 $o.= html_writer::end_tag('li');
420 * Output the html for a single section page .
422 * @param stdClass $course The course entry from DB
423 * @param array $sections The course_sections entries from the DB
424 * @param array $mods used for print_section()
425 * @param array $modnames used for print_section()
426 * @param array $modnamesused used for print_section()
427 * @param int $displaysection The section number in the course which is being displayed
429 public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
432 // Can we view the section in question?
433 $context = context_course::instance($course->id);
434 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
436 if (!isset($sections[$displaysection])) {
437 // This section doesn't exist
438 print_error('unknowncoursesection', 'error', null, $course->fullname);
442 if (!$sections[$displaysection]->visible && !$canviewhidden) {
443 if (!$course->hiddensections) {
444 echo $this->start_section_list();
445 echo $this->section_hidden($displaysection);
446 echo $this->end_section_list();
447 echo $sectionnavlinks;
449 // Can't view this section.
453 // General section if non-empty.
454 $thissection = $sections[0];
455 if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
456 echo $this->start_section_list();
457 echo $this->section_header($thissection, $course, true);
458 print_section($course, $thissection, $mods, $modnamesused, true);
459 if ($PAGE->user_is_editing()) {
460 print_section_add_menus($course, 0, $modnames);
462 echo $this->section_footer();
463 echo $this->end_section_list();
466 // Title with section navigation links.
467 $sectionnavlinks = $this->get_nav_links($course, $sections, $displaysection);
469 $sectiontitle .= html_writer::start_tag('div', array('class' => 'section-navigation headingblock header'));
470 $sectiontitle .= html_writer::tag('span', $sectionnavlinks['previous'], array('class' => 'mdl-left'));
471 $sectiontitle .= html_writer::tag('span', $sectionnavlinks['next'], array('class' => 'mdl-right'));
472 $sectiontitle .= html_writer::tag('div', get_section_name($course, $sections[$displaysection]), array('class' => 'mdl-align'));
473 $sectiontitle .= html_writer::end_tag('div');
476 // Show completion help icon.
477 $completioninfo = new completion_info($course);
478 echo $completioninfo->display_help_icon();
480 // Copy activity clipboard..
481 echo $this->course_activity_clipboard($course, $displaysection);
483 // Now the list of sections..
484 echo $this->start_section_list();
486 // The requested section page.
487 $thissection = $sections[$displaysection];
488 echo $this->section_header($thissection, $course, true);
489 print_section($course, $thissection, $mods, $modnamesused, true);
490 if ($PAGE->user_is_editing()) {
491 print_section_add_menus($course, $displaysection, $modnames);
493 echo $this->section_footer();
494 echo $this->end_section_list();
496 // Display section bottom navigation.
497 $courselink = html_writer::link(course_get_url($course), get_string('returntomaincoursepage'));
498 $sectionbottomnav = '';
499 $sectionbottomnav .= html_writer::start_tag('div', array('class' => 'section-navigation mdl-bottom'));
500 $sectionbottomnav .= html_writer::tag('span', $sectionnavlinks['previous'], array('class' => 'mdl-left'));
501 $sectionbottomnav .= html_writer::tag('span', $sectionnavlinks['next'], array('class' => 'mdl-right'));
502 $sectionbottomnav .= html_writer::tag('div', $courselink, array('class' => 'mdl-align'));
503 $sectionbottomnav .= html_writer::end_tag('div');
504 echo $sectionbottomnav;
508 * Output the html for a multiple section page
510 * @param stdClass $course The course entry from DB
511 * @param array $sections The course_sections entries from the DB
512 * @param array $mods used for print_section()
513 * @param array $modnames used for print_section()
514 * @param array $modnamesused used for print_section()
516 public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
519 $context = context_course::instance($course->id);
520 // Title with completion help icon.
521 $completioninfo = new completion_info($course);
522 echo $completioninfo->display_help_icon();
523 echo $this->output->heading($this->page_title(), 2, 'accesshide');
525 // Copy activity clipboard..
526 echo $this->course_activity_clipboard($course);
528 // Now the list of sections..
529 echo $this->start_section_list();
531 // General section if non-empty.
532 $thissection = $sections[0];
534 if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
535 echo $this->section_header($thissection, $course, true);
536 print_section($course, $thissection, $mods, $modnamesused, true);
537 if ($PAGE->user_is_editing()) {
538 print_section_add_menus($course, 0, $modnames);
540 echo $this->section_footer();
543 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
544 for ($section = 1; $section <= $course->numsections; $section++) {
545 if (!empty($sections[$section])) {
546 $thissection = $sections[$section];
548 // This will create a course section if it doesn't exist..
549 $thissection = get_course_section($section, $course->id);
551 // Show the section if the user is permitted to access it, OR if it's not available
552 // but showavailability is turned on
553 $showsection = $thissection->uservisible ||
554 ($thissection->visible && !$thissection->available && $thissection->showavailability);
556 // Hidden section message is overridden by 'unavailable' control
557 // (showavailability option).
558 if (!$course->hiddensections && $thissection->available) {
559 echo $this->section_hidden($section);
562 unset($sections[$section]);
566 if (!$PAGE->user_is_editing() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
567 // Display section summary only.
568 echo $this->section_summary($thissection, $course);
570 echo $this->section_header($thissection, $course, false);
571 if ($thissection->uservisible) {
572 print_section($course, $thissection, $mods, $modnamesused);
573 if ($PAGE->user_is_editing()) {
574 print_section_add_menus($course, $section, $modnames);
577 echo $this->section_footer();
580 unset($sections[$section]);
583 if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
584 // Print stealth sections if present.
585 $modinfo = get_fast_modinfo($course);
586 foreach ($sections as $section => $thissection) {
587 if (empty($modinfo->sections[$section])) {
590 echo $this->stealth_section_header($section);
591 print_section($course, $thissection, $mods, $modnamesused);
592 echo $this->stealth_section_footer();
595 echo $this->end_section_list();
597 echo html_writer::start_tag('div', array('class' => 'mdl-right'));
599 // Increase number of sections.
600 $straddsection = get_string('increasesections', 'moodle');
601 $url = new moodle_url('/course/changenumsections.php',
602 array('courseid' => $course->id,
604 'sesskey' => sesskey()));
605 $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
606 echo html_writer::link($url, $icon.get_accesshide($straddsection), array('class' => 'increase-sections'));
608 if ($course->numsections > 0) {
609 // Reduce number of sections sections.
610 $strremovesection = get_string('reducesections', 'moodle');
611 $url = new moodle_url('/course/changenumsections.php',
612 array('courseid' => $course->id,
614 'sesskey' => sesskey()));
615 $icon = $this->output->pix_icon('t/switch_minus', $strremovesection);
616 echo html_writer::link($url, $icon.get_accesshide($strremovesection), array('class' => 'reduce-sections'));
619 echo html_writer::end_tag('div');
621 echo $this->end_section_list();
627 * Generate html for a section summary text
629 * @param stdClass $section The course_section entry from DB
630 * @return string HTML to output.
632 protected function format_summary_text($section) {
633 $context = context_course::instance($section->course);
634 $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
635 $context->id, 'course', 'section', $section->id);
637 $options = new stdClass();
638 $options->noclean = true;
639 $options->overflowdiv = true;
640 return format_text($summarytext, $section->summaryformat, $options);