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