MDL-32508 course/formats: Respect single page course display mode
[moodle.git] / course / format / renderer.php
CommitLineData
1804b7c1
DP
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Base renderer for outputting course formats.
19 *
20 * @package core
21 * @copyright 2012 Dan Poltawski
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @since Moodle 2.3
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * This renderer is used by section based formats
31 *
32 * @package core
33 * @copyright 2012 Dan Poltawski
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 * @since Moodle 2.3
36 */
37abstract class format_renderer_base extends plugin_renderer_base {
38
39 /**
40 * Generate the starting container html for a list of sections
41 * @return string HTML to output.
42 */
43 abstract public function start_section_list();
44
45 /**
46 * Generate the closing container html for a list of sections
47 * @return string HTML to output.
48 */
49 abstract public function end_section_list();
50
51 /**
52 * Generate the title for this section page
53 * @return string the page title
54 */
55 abstract public function page_title();
56
57 /**
58 * Generate the display of the header part of a section before
59 * course modules are included
60 *
61 * @param stdClass $section The course_section entry from DB
62 * @param stdClass $course The course entry from DB
63 * @param bool $onsectionpage true if being printed on a section page
64 * @return string HTML to output.
65 */
66 public function section_header($section, $course, $onsectionpage) {
67 global $PAGE;
68
69 $o = '';
70 $currenttext = '';
71 $sectionstyle = '';
72 $rightcontent = $this->output->spacer();
73 $leftcontent = $this->output->spacer();
74 $linktitle = false;
75
76 if ($section->section != 0 ) {
77 // Only in the non-general sections.
78 if (!$section->visible) {
79 $sectionstyle = ' hidden';
80 } else if ($course->marker == $section->section) {
81 $sectionstyle = ' current';
82 $currenttext = get_accesshide(get_string('currentsection', 'format_'.$course->format));
83 }
84 $leftcontent = $currenttext.$section->section;
85 $controls = $this->section_edit_controls($course, $section, $onsectionpage);
86 if (!empty($controls)) {
87 $rightcontent = implode('<br />', $controls);
88 }
89 $linktitle = ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE);
90 }
91
92 $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
93 'class' => 'section main clearfix'.$sectionstyle));
94 $o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
95 $o.= html_writer::tag('div', $rightcontent, array('class' => 'right side'));
96 $o.= html_writer::start_tag('div', array('class' => 'content'));
97
98 if (!$onsectionpage) {
99 $title = get_section_name($course, $section);
100 if ($linktitle) {
101 $title = html_writer::link(course_get_url($course, $section->section), $title);
102 }
103 $o.= $this->output->heading($title, 3, 'sectionname');
104 }
105
106 $o.= html_writer::start_tag('div', array('class' => 'summary'));
107
108 $context = context_course::instance($section->course);
109 $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
110 $context->id, 'course', 'section', $section->id);
111 $summaryformatoptions = new stdClass();
112 $summaryformatoptions->noclean = true;
113 $summaryformatoptions->overflowdiv = true;
114
115 $o.= format_text($summarytext, $section->summaryformat, $summaryformatoptions);
116
117 if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $context)) {
118 $url = new moodle_url('/course/editsection.php', array('id'=>$section->id));
119
120 if ($onsectionpage) {
121 $url->param('sectionreturn', 1);
122 }
123
124 $o.= html_writer::link($url,
125 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/edit'), 'class' => 'iconsmall edit')),
126 array('title' => get_string('editsummary')));
127 }
128 $o.= html_writer::end_tag('div');
129
130 return $o;
131 }
132
133 /**
134 * Generate the display of the footer part of a section
135 *
136 * @return string HTML to output.
137 */
138 public function section_footer() {
139 $o = html_writer::end_tag('div');
140 $o.= html_writer::end_tag('li');
141
142 return $o;
143 }
144
145 /**
146 * Generate the edit controls of a section
147 *
148 * @param stdClass $course The course entry from DB
149 * @param stdClass $section The course_section entry from DB
150 * @param bool $onsectionpage true if being printed on a section page
151 * @return array of links with edit controls
152 */
153 public function section_edit_controls($course, $section, $onsectionpage = false) {
154 global $PAGE;
155
156 if (!$PAGE->user_is_editing()) {
157 return array();
158 }
159
160 if (!has_capability('moodle/course:update', context_course::instance($course->id))) {
161 return array();
162 }
163
164 if ($onsectionpage) {
165 $baseurl = course_get_url($course, $section->section);
166 } else {
167 $baseurl = course_get_url($course);
168 }
169 $baseurl->param('sesskey', sesskey());
170
171 $controls = array();
172
173 $url = clone($baseurl);
174 if ($section->visible) { // Show the hide/show eye.
175 $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
176 $url->param('hide', $section->section);
177 $controls[] = html_writer::link($url,
178 html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'),
179 'class' => 'icon hide', 'alt' => $strhidefromothers)),
180 array('title' => $strhidefromothers, 'class' => 'editing_showhide'));
181 } else {
182 $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
183 $url->param('show', $section->section);
184 $controls[] = html_writer::link($url,
185 html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'),
186 'class' => 'icon hide', 'alt' => $strshowfromothers)),
187 array('title' => $strshowfromothers, 'class' => 'editing_showhide'));
188 }
189
190 if (!$onsectionpage) {
191 $url = clone($baseurl);
192 if ($section->section > 1) { // Add a arrow to move section up.
193 $url->param('section', $section->section);
194 $url->param('move', -1);
195 $strmoveup = get_string('moveup');
196
197 $controls[] = html_writer::link($url,
198 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/up'),
199 'class' => 'icon up', 'alt' => $strmoveup)),
200 array('title' => $strmoveup, 'class' => 'moveup'));
201 }
202
203 $url = clone($baseurl);
204 if ($section->section < $course->numsections) { // Add a arrow to move section down.
205 $url->param('section', $section->section);
206 $url->param('move', 1);
207 $strmovedown = get_string('movedown');
208
209 $controls[] = html_writer::link($url,
210 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/down'),
211 'class' => 'icon down', 'alt' => $strmovedown)),
212 array('title' => $strmovedown, 'class' => 'movedown'));
213 }
214 }
215
216 return $controls;
217 }
218
219 /**
220 * Generate a summary of a section for display on the 'coruse index page'
221 *
222 * @param stdClass $section The course_section entry from DB
223 * @param stdClass $course The course entry from DB
224 * @return string HTML to output.
225 */
226 public function section_summary($section, $course) {
227
228 $o = '';
229 $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section));
230
231 $title = get_section_name($course, $section);
232 $o.= html_writer::start_tag('div', array('class' => 'section-summary'));
233 $o.= html_writer::start_tag('a', array('href' => course_get_url($course, $section->section)));
234 $o.= $this->output->heading($title, 3, 'header section-title');
235 $o.= html_writer::end_tag('a');
236
237 $o.= html_writer::start_tag('div', array('class' => 'summarytext'));
238 $o.= format_text($section->summary, $section->summaryformat);
239 $o.= html_writer::end_tag('div');
240 $o.= html_writer::end_tag('div');
241 $o.= html_writer::end_tag('li');
242
243 return $o;
244 }
245
246 /**
247 * Show if something is on on the course clipboard (moving around)
248 *
249 * @param stdClass $course The course entry from DB
250 * @param int $sectionno The section number in the coruse which is being dsiplayed
251 * @return string HTML to output.
252 */
253 public function course_activity_clipboard($course, $sectionno = 0) {
254 global $USER;
255
256 $o = '';
257 // If currently moving a file then show the current clipboard.
258 if (ismoving($course->id)) {
259 $url = new moodle_url('/course/mod.php',
260 array('sesskey' => sesskey(),
261 'cancelcopy' => true,
262 'sr' => $sectionno,
263 )
264 );
265
266 $strcancel= get_string('cancel');
267
268 $o.= html_writer::start_tag('li', array('class' => 'clipboard'));
269 $o.= strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
270 $o.= ' ('.html_writer::link($url, get_string('cancel')).')';
271 $o.= html_writer::end_tag('li');
272 }
273
274 return $o;
275 }
276
277 /**
278 * Generate next/previous section links for naviation
279 *
280 * @param stdClass $course The course entry from DB
281 * @param array $sections The course_sections entries from the DB
282 * @param int $sectionno The section number in the coruse which is being dsiplayed
283 * @return string HTML to output.
284 */
285 public function get_nav_links($course, $sections, $sectionno) {
286 // FIXME: This is really evil and should by using the navigation API.
287 $canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id))
288 or !$course->hiddensections;
289
290 $links = array('previous' => '', 'next' => '');
291 $back = $sectionno - 1;
292 while ($back > 0 and empty($links['previous'])) {
293 if ($canviewhidden || $sections[$back]->visible) {
294 $links['previous'] = html_writer::link(course_get_url($course, $back),
295 $this->output->larrow().$this->output->spacer().get_section_name($course, $sections[$back]));
296 }
297 $back--;
298 }
299
300 $forward = $sectionno + 1;
301 while ($forward <= $course->numsections and empty($links['next'])) {
302 if ($canviewhidden || $sections[$forward]->visible) {
303 $links['next'] = html_writer::link(course_get_url($course, $forward),
304 get_section_name($course, $sections[$forward]).$this->output->spacer().$this->output->rarrow());
305 }
306 $forward++;
307 }
308
309 $o = '';
310 $o.= html_writer::start_tag('div', array('class' => 'section-navigation yui3-g'));
311 $o.= html_writer::tag('div', $links['previous'], array('class' => 'yui3-u'));
312 $o.= html_writer::tag('div', $links['next'], array('class' => 'right yui3-u'));
313 $o.= html_writer::end_tag('div');
314
315 return $o;
316 }
317
318 /**
319 * Generate the header html of a stealth section
320 *
321 * @param int $sectionno The section number in the coruse which is being dsiplayed
322 * @return string HTML to output.
323 */
324 public function stealth_section_header($sectionno) {
325 $o = '';
326 $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix orphaned hidden'));
327 $o.= html_writer::tag('div', '', array('class' => 'left side'));
328 $o.= html_writer::tag('div', '', array('class' => 'right side'));
329 $o.= html_writer::start_tag('div', array('class' => 'content'));
330 $o.= $this->output->heading(get_string('orphanedactivities'), 3, 'sectionname');
331 return $o;
332 }
333
334 /**
335 * Generate footer html of a stealth section
336 *
337 * @return string HTML to output.
338 */
339 public function stealth_section_footer() {
340 $o = html_writer::end_tag('div');
341 $o.= html_writer::end_tag('li');
342 return $o;
343 }
344
345 /**
346 * Generate the html for a hidden section
347 *
348 * @param int $sectionno The section number in the coruse which is being dsiplayed
349 * @return string HTML to output.
350 */
351 public function section_hidden($sectionno) {
352 $o = '';
353 $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix hidden'));
354 $o.= html_writer::tag('div', '', array('class' => 'left side'));
355 $o.= html_writer::tag('div', '', array('class' => 'right side'));
356 $o.= html_writer::start_tag('div', array('class' => 'content'));
357 $o.= get_string('notavailable');
358 $o.= html_writer::end_tag('div');
359 $o.= html_writer::end_tag('li');
360 return $o;
361 }
362
363 /**
364 * Output the html for a single section page .
365 *
366 * @param stdClass $course The course entry from DB
367 * @param array $sections The course_sections entries from the DB
368 * @param array $mods used for print_section()
369 * @param array $modnames used for print_section()
370 * @param array $modnamesused used for print_section()
371 * @param int $displaysection The section number in the course which is being displayed
372 */
373 public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
374 global $PAGE;
375
376 // Section next/previous links.
377 $sectionnavlinks = $this->get_nav_links($course, $sections, $displaysection);
378 echo $sectionnavlinks;
379
380 // Can we view the section in question?
381 $context = context_course::instance($course->id);
382 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
383
384 if (!$sections[$displaysection]->visible && !$canviewhidden) {
385 if (!$course->hiddensections) {
386 echo $this->start_section_list();
387 echo $this->section_hidden($displaysection);
388 echo $this->end_section_list();
389 echo $sectionnavlinks;
390 }
391 // Can't view this section.
392 return;
393 }
394
395 // Title with completion help icon.
396 $completioninfo = new completion_info($course);
397 echo $completioninfo->display_help_icon();
398 $title = get_section_name($course, $sections[$displaysection]);
399 echo $this->output->heading($title, 2, 'headingblock header outline');
400
401 // Copy activity clipboard..
402 echo $this->course_activity_clipboard($course, $displaysection);
403
404 // Now the list of sections..
405 echo $this->start_section_list();
406
407 // General section if non-empty.
408 $thissection = $sections[0];
409 if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
410 echo $this->section_header($thissection, $course, true);
411 print_section($course, $thissection, $mods, $modnamesused, true);
412 if ($PAGE->user_is_editing()) {
413 print_section_add_menus($course, 0, $modnames);
414 }
415 echo $this->section_footer();
416 }
417
418 // The requested section page.
419 $thissection = $sections[$displaysection];
420 echo $this->section_header($thissection, $course, true);
421 print_section($course, $thissection, $mods, $modnamesused, true);
422 if ($PAGE->user_is_editing()) {
423 print_section_add_menus($course, $displaysection, $modnames);
424 }
425 echo $this->section_footer();
426 echo $sectionnavlinks;
427 echo $this->end_section_list();
428 }
429
430 /**
431 * Output the html for a multiple section page
432 *
433 * @param stdClass $course The course entry from DB
434 * @param array $sections The course_sections entries from the DB
435 * @param array $mods used for print_section()
436 * @param array $modnames used for print_section()
437 * @param array $modnamesused used for print_section()
438 */
439 public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
440 global $PAGE;
441
442 $context = context_course::instance($course->id);
443 // Title with completion help icon.
444 $completioninfo = new completion_info($course);
445 echo $completioninfo->display_help_icon();
446 echo $this->output->heading($this->page_title(), 2, 'headingblock header outline');
447
448 // Copy activity clipboard..
449 echo $this->course_activity_clipboard($course);
450
451 // Now the list of sections..
452 echo $this->start_section_list();
453
454 // General section if non-empty.
455 $thissection = $sections[0];
456 unset($sections[0]);
457 if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
458 echo $this->section_header($thissection, $course, true);
459 print_section($course, $thissection, $mods, $modnamesused, true);
460 if ($PAGE->user_is_editing()) {
461 print_section_add_menus($course, 0, $modnames);
462 }
463 echo $this->section_footer();
464 }
465
466 $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
467 for ($section = 1; $section <= $course->numsections; $section++) {
468 if (!empty($sections[$section])) {
469 $thissection = $sections[$section];
470 } else {
471 // This will create a course section if it doesn't exist..
472 $thissection = get_course_section($section, $course->id);
473 }
474 $showsection = ($canviewhidden or $thissection->visible or !$course->hiddensections);
475 if (!$thissection->visible && !$canviewhidden) {
476 if (!$course->hiddensections) {
477 echo $this->section_hidden($section);
478 }
479
480 unset($sections[$section]);
481 continue;
482 }
483
484 if (!$PAGE->user_is_editing() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
485 // Display section summary only.
486 echo $this->section_summary($thissection, $course);
487 } else {
488 echo $this->section_header($thissection, $course, false);
489 print_section($course, $thissection, $mods, $modnamesused);
490 if ($PAGE->user_is_editing()) {
491 print_section_add_menus($course, $section, $modnames);
492 }
493 echo $this->section_footer();
494 }
495
496 unset($sections[$section]);
497 }
498
499 if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
500 // Print stealth sections if present.
501 $modinfo = get_fast_modinfo($course);
502 foreach ($sections as $section => $thissection) {
503 if (empty($modinfo->sections[$section])) {
504 continue;
505 }
506 echo $this->stealth_section_header($section);
507 print_section($course, $thissection, $mods, $modnamesused);
508 echo $this->stealth_section_footer();
509 }
510 }
511
512 echo $this->end_section_list();
513 }
514}