MDL-24419 Fix bug with section creation for new courses
[moodle.git] / course / format / renderer.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
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.
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  */
39 abstract class format_section_renderer_base extends plugin_renderer_base {
41     /**
42      * Generate the starting container html for a list of sections
43      * @return string HTML to output.
44      */
45     abstract protected function start_section_list();
47     /**
48      * Generate the closing container html for a list of sections
49      * @return string HTML to output.
50      */
51     abstract protected function end_section_list();
53     /**
54      * Generate the title for this section page
55      * @return string the page title
56      */
57     abstract protected function page_title();
59     /**
60      * Generate the content to displayed on the right part of a section
61      * before course modules are included
62      *
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      */
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);
75             }
76         }
78         return $o;
79     }
81     /**
82      * Generate the content to displayed on the left part of a section
83      * before course modules are included
84      *
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      */
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));
97             }
98         }
100         return $o;
101     }
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      */
112     protected function section_header($section, $course, $onsectionpage) {
113         global $PAGE;
115         $o = '';
116         $currenttext = '';
117         $sectionstyle = '';
118         $linktitle = false;
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';
126             }
127             $linktitle = ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE);
128         }
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);
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         }
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);
157             }
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')));
162         }
163         $o.= html_writer::end_tag('div');
165         $o .= $this->section_availability_message($section);
167         return $o;
168     }
170     /**
171      * Generate the display of the footer part of a section
172      *
173      * @return string HTML to output.
174      */
175     protected function section_footer() {
176         $o = html_writer::end_tag('div');
177         $o.= html_writer::end_tag('li');
179         return $o;
180     }
182     /**
183      * Generate the edit controls of a section
184      *
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
189      */
190     protected function section_edit_controls($course, $section, $onsectionpage = false) {
191         global $PAGE;
193         if (!$PAGE->user_is_editing()) {
194             return array();
195         }
197         if (!has_capability('moodle/course:update', context_course::instance($course->id))) {
198             return array();
199         }
201         if ($onsectionpage) {
202             $baseurl = course_get_url($course, $section->section);
203         } else {
204             $baseurl = course_get_url($course);
205         }
206         $baseurl->param('sesskey', sesskey());
208         $controls = array();
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'));
218         } else {
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'));
225         }
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'));
238             }
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'));
250             }
251         }
253         return $controls;
254     }
256     /**
257      * Generate a summary of a section for display on the 'coruse index page'
258      *
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.
262      */
263     protected function section_summary($section, $course) {
265         $o = '';
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');
282         return $o;
283     }
285     /**
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.
288      *
289      * @param stdClass $section The course_section entry from DB
290      * @return string HTML to output
291      */
292     protected function section_availability_message($section) {
293         $o = '';
294         if (!$section->uservisible) {
295             $o .= html_writer::start_tag('div', array('class' => 'availabilityinfo'));
296             if (!empty($section->availableinfo)) {
297                 $o .= $section->availableinfo;
298             } else {
299                 $o .= get_string('notavailable');
300             }
301             $o .= html_writer::end_tag('div');
302         }
303         return $o;
304     }
306     /**
307      * Show if something is on on the course clipboard (moving around)
308      *
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.
312      */
313     protected function course_activity_clipboard($course, $sectionno = 0) {
314         global $USER;
316         $o = '';
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,
322                       'sr' => $sectionno,
323                 )
324             );
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');
332         }
334         return $o;
335     }
337     /**
338      * Generate next/previous section links for naviation
339      *
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 string HTML to output.
344      */
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                 $links['previous'] = html_writer::link(course_get_url($course, $back),
355                     $this->output->larrow().$this->output->spacer().get_section_name($course, $sections[$back]));
356             }
357             $back--;
358         }
360         $forward = $sectionno + 1;
361         while ($forward <= $course->numsections and empty($links['next'])) {
362             if ($canviewhidden || $sections[$forward]->visible) {
363                 $links['next'] = html_writer::link(course_get_url($course, $forward),
364                     get_section_name($course, $sections[$forward]).$this->output->spacer().$this->output->rarrow());
365             }
366             $forward++;
367         }
369         $o = '';
370         $o.= html_writer::start_tag('div', array('class' => 'section-navigation yui3-g'));
371         $o.= html_writer::tag('div', $links['previous'], array('class' => 'yui3-u'));
372         $o.= html_writer::tag('div', $links['next'], array('class' => 'right yui3-u'));
373         $o.= html_writer::end_tag('div');
375         return $o;
376     }
378     /**
379      * Generate the header html of a stealth section
380      *
381      * @param int $sectionno The section number in the coruse which is being dsiplayed
382      * @return string HTML to output.
383      */
384     protected function stealth_section_header($sectionno) {
385         $o = '';
386         $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix orphaned hidden'));
387         $o.= html_writer::tag('div', '', array('class' => 'left side'));
388         $o.= html_writer::tag('div', '', array('class' => 'right side'));
389         $o.= html_writer::start_tag('div', array('class' => 'content'));
390         $o.= $this->output->heading(get_string('orphanedactivities'), 3, 'sectionname');
391         return $o;
392     }
394     /**
395      * Generate footer html of a stealth section
396      *
397      * @return string HTML to output.
398      */
399     protected function stealth_section_footer() {
400         $o = html_writer::end_tag('div');
401         $o.= html_writer::end_tag('li');
402         return $o;
403     }
405     /**
406      * Generate the html for a hidden section
407      *
408      * @param int $sectionno The section number in the coruse which is being dsiplayed
409      * @return string HTML to output.
410      */
411     protected function section_hidden($sectionno) {
412         $o = '';
413         $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix hidden'));
414         $o.= html_writer::tag('div', '', array('class' => 'left side'));
415         $o.= html_writer::tag('div', '', array('class' => 'right side'));
416         $o.= html_writer::start_tag('div', array('class' => 'content'));
417         $o.= get_string('notavailable');
418         $o.= html_writer::end_tag('div');
419         $o.= html_writer::end_tag('li');
420         return $o;
421     }
423     /**
424      * Output the html for a single section page .
425      *
426      * @param stdClass $course The course entry from DB
427      * @param array $sections The course_sections entries from the DB
428      * @param array $mods used for print_section()
429      * @param array $modnames used for print_section()
430      * @param array $modnamesused used for print_section()
431      * @param int $displaysection The section number in the course which is being displayed
432      */
433     public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
434         global $PAGE;
436         // Can we view the section in question?
437         $context = context_course::instance($course->id);
438         $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
440         if (!isset($sections[$displaysection])) {
441             // This section doesn't exist
442             print_error('unknowncoursesection', 'error', null, $course->fullname);
443             return;
444         }
446         if (!$sections[$displaysection]->visible && !$canviewhidden) {
447             if (!$course->hiddensections) {
448                 echo $this->start_section_list();
449                 echo $this->section_hidden($displaysection);
450                 echo $this->end_section_list();
451                 echo $sectionnavlinks;
452             }
453             // Can't view this section.
454             return;
455         }
457         // General section if non-empty.
458         $thissection = $sections[0];
459         if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
460             echo $this->start_section_list();
461             echo $this->section_header($thissection, $course, true);
462             print_section($course, $thissection, $mods, $modnamesused, true);
463             if ($PAGE->user_is_editing()) {
464                 print_section_add_menus($course, 0, $modnames);
465             }
466             echo $this->section_footer();
467             echo $this->end_section_list();
468         }
470         // Section next/previous links.
471         $sectionnavlinks = $this->get_nav_links($course, $sections, $displaysection);
472         echo $sectionnavlinks;
475         // Title with completion help icon.
476         $completioninfo = new completion_info($course);
477         echo $completioninfo->display_help_icon();
478         $title = get_section_name($course, $sections[$displaysection]);
479         echo $this->output->heading($title, 2, 'headingblock header outline');
481         // Copy activity clipboard..
482         echo $this->course_activity_clipboard($course, $displaysection);
484         // Now the list of sections..
485         echo $this->start_section_list();
487         // The requested section page.
488         $thissection = $sections[$displaysection];
489         echo $this->section_header($thissection, $course, true);
490         print_section($course, $thissection, $mods, $modnamesused, true);
491         if ($PAGE->user_is_editing()) {
492             print_section_add_menus($course, $displaysection, $modnames);
493         }
494         echo $this->section_footer();
495         echo $sectionnavlinks;
496         echo $this->end_section_list();
497     }
499     /**
500      * Output the html for a multiple section page
501      *
502      * @param stdClass $course The course entry from DB
503      * @param array $sections The course_sections entries from the DB
504      * @param array $mods used for print_section()
505      * @param array $modnames used for print_section()
506      * @param array $modnamesused used for print_section()
507      */
508     public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
509         global $PAGE;
511         $context = context_course::instance($course->id);
512         // Title with completion help icon.
513         $completioninfo = new completion_info($course);
514         echo $completioninfo->display_help_icon();
515         echo $this->output->heading($this->page_title(), 2, 'accesshide');
517         // Copy activity clipboard..
518         echo $this->course_activity_clipboard($course);
520         // Now the list of sections..
521         echo $this->start_section_list();
523         // General section if non-empty.
524         $thissection = $sections[0];
525         unset($sections[0]);
526         if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
527             echo $this->section_header($thissection, $course, true);
528             print_section($course, $thissection, $mods, $modnamesused, true);
529             if ($PAGE->user_is_editing()) {
530                 print_section_add_menus($course, 0, $modnames);
531             }
532             echo $this->section_footer();
533         }
535         $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
536         for ($section = 1; $section <= $course->numsections; $section++) {
537             if (!empty($sections[$section])) {
538                 $thissection = $sections[$section];
539             } else {
540                 // This will create a course section if it doesn't exist..
541                 $thissection = get_course_section($section, $course->id);
543                 // The returned section is only a bare database object rather than
544                 // a section_info object - we will need at least the uservisible
545                 // field in it.
546                 $thissection->uservisible = true;
547             }
548             // Show the section if the user is permitted to access it, OR if it's not available
549             // but showavailability is turned on
550             $showsection = $thissection->uservisible ||
551                     ($thissection->visible && !$thissection->available && $thissection->showavailability);
552             if (!$showsection) {
553                 // Hidden section message is overridden by 'unavailable' control
554                 // (showavailability option).
555                 if (!$course->hiddensections && $thissection->available) {
556                     echo $this->section_hidden($section);
557                 }
559                 unset($sections[$section]);
560                 continue;
561             }
563             if (!$PAGE->user_is_editing() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
564                 // Display section summary only.
565                 echo $this->section_summary($thissection, $course);
566             } else {
567                 echo $this->section_header($thissection, $course, false);
568                 if ($thissection->uservisible) {
569                     print_section($course, $thissection, $mods, $modnamesused);
570                     if ($PAGE->user_is_editing()) {
571                         print_section_add_menus($course, $section, $modnames);
572                     }
573                 }
574                 echo $this->section_footer();
575             }
577             unset($sections[$section]);
578         }
580         if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
581             // Print stealth sections if present.
582             $modinfo = get_fast_modinfo($course);
583             foreach ($sections as $section => $thissection) {
584                 if (empty($modinfo->sections[$section])) {
585                     continue;
586                 }
587                 echo $this->stealth_section_header($section);
588                 print_section($course, $thissection, $mods, $modnamesused);
589                 echo $this->stealth_section_footer();
590             }
592             echo $this->end_section_list();
594             echo html_writer::start_tag('div', array('class' => 'mdl-right'));
596             // Increase number of sections.
597             $straddsection = get_string('increasesections', 'moodle');
598             $url = new moodle_url('/course/changenumsections.php',
599                 array('courseid' => $course->id,
600                       'increase' => true,
601                       'sesskey' => sesskey()));
602             $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
603             echo html_writer::link($url, $icon.get_accesshide($straddsection), array('class' => 'increase-sections'));
605             if ($course->numsections > 0) {
606                 // Reduce number of sections sections.
607                 $strremovesection = get_string('reducesections', 'moodle');
608                 $url = new moodle_url('/course/changenumsections.php',
609                     array('courseid' => $course->id,
610                           'increase' => false,
611                           'sesskey' => sesskey()));
612                 $icon = $this->output->pix_icon('t/switch_minus', $strremovesection);
613                 echo html_writer::link($url, $icon.get_accesshide($strremovesection), array('class' => 'reduce-sections'));
614             }
616             echo html_writer::end_tag('div');
617         } else {
618             echo $this->end_section_list();
619         }
621     }
623     /**
624      * Generate html for a section summary text
625      *
626      * @param stdClass $section The course_section entry from DB
627      * @return string HTML to output.
628      */
629     protected function format_summary_text($section) {
630         $context = context_course::instance($section->course);
631         $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
632             $context->id, 'course', 'section', $section->id);
634         $options = new stdClass();
635         $options->noclean = true;
636         $options->overflowdiv = true;
637         return format_text($summarytext, $section->summaryformat, $options);
638     }