MDL-34444 course - don't show to 'unavaibile' sections title
[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 section title
61      *
62      * @param stdClass $section The course_section entry from DB
63      * @param stdClass $course The course entry from DB
64      * @return string HTML to output.
65      */
66     public function section_title($section, $course) {
67         $title = get_section_name($course, $section);
68         if ($section->section != 0 && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
69             $title = html_writer::link(course_get_url($course, $section->section), $title);
70         }
71         return $title;
72     }
74     /**
75      * Generate the content to displayed on the right part of a section
76      * before course modules are included
77      *
78      * @param stdClass $section The course_section entry from DB
79      * @param stdClass $course The course entry from DB
80      * @param bool $onsectionpage true if being printed on a section page
81      * @return string HTML to output.
82      */
83     protected function section_right_content($section, $course, $onsectionpage) {
84         $o = $this->output->spacer();
86         if ($section->section != 0) {
87             $controls = $this->section_edit_controls($course, $section, $onsectionpage);
88             if (!empty($controls)) {
89                 $o = implode('<br />', $controls);
90             }
91         }
93         return $o;
94     }
96     /**
97      * Generate the content to displayed on the left part of a section
98      * before course modules are included
99      *
100      * @param stdClass $section The course_section entry from DB
101      * @param stdClass $course The course entry from DB
102      * @param bool $onsectionpage true if being printed on a section page
103      * @return string HTML to output.
104      */
105     protected function section_left_content($section, $course, $onsectionpage) {
106         $o = $this->output->spacer();
108         if ($section->section != 0) {
109             // Only in the non-general sections.
110             if ($this->is_section_current($section, $course)) {
111                 $o = get_accesshide(get_string('currentsection', 'format_'.$course->format));
112             }
113         }
115         return $o;
116     }
118     /**
119      * Generate the display of the header part of a section before
120      * course modules are included
121      *
122      * @param stdClass $section The course_section entry from DB
123      * @param stdClass $course The course entry from DB
124      * @param bool $onsectionpage true if being printed on a single-section page
125      * @param int $sectionreturn The section to return to after an action
126      * @return string HTML to output.
127      */
128     protected function section_header($section, $course, $onsectionpage, $sectionreturn=0) {
129         global $PAGE;
131         $o = '';
132         $currenttext = '';
133         $sectionstyle = '';
135         if ($section->section != 0) {
136             // Only in the non-general sections.
137             if (!$section->visible) {
138                 $sectionstyle = ' hidden';
139             } else if ($this->is_section_current($section, $course)) {
140                 $sectionstyle = ' current';
141             }
142         }
144         $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
145             'class' => 'section main clearfix'.$sectionstyle));
147         $leftcontent = $this->section_left_content($section, $course, $onsectionpage);
148         $o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
150         $rightcontent = $this->section_right_content($section, $course, $onsectionpage);
151         $o.= html_writer::tag('div', $rightcontent, array('class' => 'right side'));
152         $o.= html_writer::start_tag('div', array('class' => 'content'));
154         // When not on a section page, we display the section titles except the general section if null
155         $hasnamenotsecpg = (!$onsectionpage && ($section->section != 0 || !is_null($section->name)));
157         // When on a section page, we only display the general section title, if title is not the default one
158         $hasnamesecpg = ($onsectionpage && ($section->section == 0 && !is_null($section->name)));
160         if ($hasnamenotsecpg || $hasnamesecpg) {
161             $o.= $this->output->heading($this->section_title($section, $course), 3, 'sectionname');
162         }
164         $o.= html_writer::start_tag('div', array('class' => 'summary'));
165         $o.= $this->format_summary_text($section);
167         $context = context_course::instance($course->id);
168         if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $context)) {
169             $url = new moodle_url('/course/editsection.php', array('id'=>$section->id, 'sr'=>$sectionreturn));
170             $o.= html_writer::link($url,
171                 html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/edit'), 'class' => 'iconsmall edit')),
172                 array('title' => get_string('editsummary')));
173         }
174         $o.= html_writer::end_tag('div');
176         $o .= $this->section_availability_message($section);
178         return $o;
179     }
181     /**
182      * Generate the display of the footer part of a section
183      *
184      * @return string HTML to output.
185      */
186     protected function section_footer() {
187         $o = html_writer::end_tag('div');
188         $o.= html_writer::end_tag('li');
190         return $o;
191     }
193     /**
194      * Generate the edit controls of a section
195      *
196      * @param stdClass $course The course entry from DB
197      * @param stdClass $section The course_section entry from DB
198      * @param bool $onsectionpage true if being printed on a section page
199      * @return array of links with edit controls
200      */
201     protected function section_edit_controls($course, $section, $onsectionpage = false) {
202         global $PAGE;
204         if (!$PAGE->user_is_editing()) {
205             return array();
206         }
208         $coursecontext = context_course::instance($course->id);
210         if ($onsectionpage) {
211             $baseurl = course_get_url($course, $section->section);
212         } else {
213             $baseurl = course_get_url($course);
214         }
215         $baseurl->param('sesskey', sesskey());
217         $controls = array();
219         $url = clone($baseurl);
220         if (has_capability('moodle/course:sectionvisibility', $coursecontext)) {
221             if ($section->visible) { // Show the hide/show eye.
222                 $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
223                 $url->param('hide', $section->section);
224                 $controls[] = html_writer::link($url,
225                     html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'),
226                     'class' => 'icon hide', 'alt' => $strhidefromothers)),
227                     array('title' => $strhidefromothers, 'class' => 'editing_showhide'));
228             } else {
229                 $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
230                 $url->param('show',  $section->section);
231                 $controls[] = html_writer::link($url,
232                     html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'),
233                     'class' => 'icon hide', 'alt' => $strshowfromothers)),
234                     array('title' => $strshowfromothers, 'class' => 'editing_showhide'));
235             }
236         }
238         if (!$onsectionpage && has_capability('moodle/course:movesections', $coursecontext)) {
239             $url = clone($baseurl);
240             if ($section->section > 1) { // Add a arrow to move section up.
241                 $url->param('section', $section->section);
242                 $url->param('move', -1);
243                 $strmoveup = get_string('moveup');
245                 $controls[] = html_writer::link($url,
246                     html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/up'),
247                     'class' => 'icon up', 'alt' => $strmoveup)),
248                     array('title' => $strmoveup, 'class' => 'moveup'));
249             }
251             $url = clone($baseurl);
252             if ($section->section < $course->numsections) { // Add a arrow to move section down.
253                 $url->param('section', $section->section);
254                 $url->param('move', 1);
255                 $strmovedown =  get_string('movedown');
257                 $controls[] = html_writer::link($url,
258                     html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/down'),
259                     'class' => 'icon down', 'alt' => $strmovedown)),
260                     array('title' => $strmovedown, 'class' => 'movedown'));
261             }
262         }
264         return $controls;
265     }
267     /**
268      * Generate a summary of a section for display on the 'coruse index page'
269      *
270      * @param stdClass $section The course_section entry from DB
271      * @param stdClass $course The course entry from DB
272      * @param array    $mods course modules indexed by id (from get_all_mods)
273      * @return string HTML to output.
274      */
275     protected function section_summary($section, $course, $mods) {
276         $classattr = 'section main section-summary clearfix';
277         $linkclasses = '';
279         // If section is hidden then display grey section link
280         if (!$section->visible) {
281             $classattr .= ' hidden';
282             $linkclasses .= ' dimmed_text';
283         } else if ($this->is_section_current($section, $course)) {
284             $classattr .= ' current';
285         }
287         $o = '';
288         $o .= html_writer::start_tag('li', array('id' => 'section-'.$section->section, 'class' => $classattr));
290         $o .= html_writer::tag('div', '', array('class' => 'left side'));
291         $o .= html_writer::tag('div', '', array('class' => 'right side'));
292         $o .= html_writer::start_tag('div', array('class' => 'content'));
294         if ($section->uservisible) {
295             $title = html_writer::tag('a', get_section_name($course, $section),
296                     array('href' => course_get_url($course, $section->section), 'class' => $linkclasses));
297             $o .= $this->output->heading($title, 3, 'section-title');
298         }
300         $o.= html_writer::start_tag('div', array('class' => 'summarytext'));
301         $o.= $this->format_summary_text($section);
302         $o.= html_writer::end_tag('div');
303         $o.= $this->section_activity_summary($section, $course, $mods);
305         $o.= $this->section_availability_message($section);
307         $o .= html_writer::end_tag('div');
308         $o .= html_writer::end_tag('li');
310         return $o;
311     }
313     /**
314      * Generate a summary of the activites in a section
315      *
316      * @param stdClass $section The course_section entry from DB
317      * @param stdClass $course the course record from DB
318      * @param array    $mods course modules indexed by id (from get_all_mods)
319      * @return string HTML to output.
320      */
321     private function section_activity_summary($section, $course, $mods) {
322         if (empty($section->sequence)) {
323             return '';
324         }
326         // Generate array with count of activities in this section:
327         $sectionmods = array();
328         $total = 0;
329         $complete = 0;
330         $cancomplete = isloggedin() && !isguestuser();
331         $completioninfo = new completion_info($course);
332         $modsequence = explode(',', $section->sequence);
333         foreach ($modsequence as $cmid) {
334             $thismod = $mods[$cmid];
336             if ($thismod->modname == 'label') {
337                 // Labels are special (not interesting for students)!
338                 continue;
339             }
341             if ($thismod->uservisible) {
342                 if (isset($sectionmods[$thismod->modname])) {
343                     $sectionmods[$thismod->modname]['count']++;
344                 } else {
345                     $sectionmods[$thismod->modname]['name'] = $thismod->modplural;
346                     $sectionmods[$thismod->modname]['count'] = 1;
347                 }
348                 if ($cancomplete && $completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
349                     $total++;
350                     $completiondata = $completioninfo->get_data($thismod, true);
351                     if ($completiondata->completionstate == COMPLETION_COMPLETE) {
352                         $complete++;
353                     }
354                 }
355             }
356         }
358         if (empty($sectionmods)) {
359             // No sections
360             return '';
361         }
363         // Output section activities summary:
364         $o = '';
365         $o.= html_writer::start_tag('div', array('class' => 'section-summary-activities mdl-right'));
366         foreach ($sectionmods as $mod) {
367             $o.= html_writer::start_tag('span', array('class' => 'activity-count'));
368             $o.= $mod['name'].': '.$mod['count'];
369             $o.= html_writer::end_tag('span');
370         }
371         $o.= html_writer::end_tag('div');
373         // Output section completion data
374         if ($total > 0) {
375             $a = new stdClass;
376             $a->complete = $complete;
377             $a->total = $total;
379             $o.= html_writer::start_tag('div', array('class' => 'section-summary-activities mdl-right'));
380             $o.= html_writer::tag('span', get_string('progresstotal', 'completion', $a), array('class' => 'activity-count'));
381             $o.= html_writer::end_tag('div');
382         }
384         return $o;
385     }
387     /**
388      * If section is not visible to current user, display the message about that
389      * ('Not available until...', that sort of thing). Otherwise, returns blank.
390      *
391      * @param stdClass $section The course_section entry from DB
392      * @return string HTML to output
393      */
394     protected function section_availability_message($section) {
395         $o = '';
396         if (!$section->uservisible || $section->availableinfo) {
397             $o .= html_writer::start_tag('div', array('class' => 'availabilityinfo'));
398             if (!empty($section->availableinfo)) {
399                 $o .= $section->availableinfo;
400             } else {
401                 $o .= get_string('notavailable');
402             }
403             $o .= html_writer::end_tag('div');
404         }
405         return $o;
406     }
408     /**
409      * Show if something is on on the course clipboard (moving around)
410      *
411      * @param stdClass $course The course entry from DB
412      * @param int $sectionno The section number in the coruse which is being dsiplayed
413      * @return string HTML to output.
414      */
415     protected function course_activity_clipboard($course, $sectionno = 0) {
416         global $USER;
418         $o = '';
419         // If currently moving a file then show the current clipboard.
420         if (ismoving($course->id)) {
421             $url = new moodle_url('/course/mod.php',
422                 array('sesskey' => sesskey(),
423                       'cancelcopy' => true,
424                       'sr' => $sectionno,
425                 )
426             );
428             $o.= html_writer::start_tag('div', array('class' => 'clipboard'));
429             $o.= strip_tags(get_string('activityclipboard', '', $USER->activitycopyname));
430             $o.= ' ('.html_writer::link($url, get_string('cancel')).')';
431             $o.= html_writer::end_tag('div');
432         }
434         return $o;
435     }
437     /**
438      * Generate next/previous section links for naviation
439      *
440      * @param stdClass $course The course entry from DB
441      * @param array $sections The course_sections entries from the DB
442      * @param int $sectionno The section number in the coruse which is being dsiplayed
443      * @return array associative array with previous and next section link
444      */
445     protected function get_nav_links($course, $sections, $sectionno) {
446         // FIXME: This is really evil and should by using the navigation API.
447         $canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id))
448             or !$course->hiddensections;
450         $links = array('previous' => '', 'next' => '');
451         $back = $sectionno - 1;
452         while ($back > 0 and empty($links['previous'])) {
453             if ($canviewhidden || $sections[$back]->uservisible) {
454                 $params = array();
455                 if (!$sections[$back]->visible) {
456                     $params = array('class' => 'dimmed_text');
457                 }
458                 $previouslink = html_writer::tag('span', $this->output->larrow(), array('class' => 'larrow'));
459                 $previouslink .= get_section_name($course, $sections[$back]);
460                 $links['previous'] = html_writer::link(course_get_url($course, $back), $previouslink, $params);
461             }
462             $back--;
463         }
465         $forward = $sectionno + 1;
466         while ($forward <= $course->numsections and empty($links['next'])) {
467             if ($canviewhidden || $sections[$forward]->uservisible) {
468                 $params = array();
469                 if (!$sections[$forward]->visible) {
470                     $params = array('class' => 'dimmed_text');
471                 }
472                 $nextlink = get_section_name($course, $sections[$forward]);
473                 $nextlink .= html_writer::tag('span', $this->output->rarrow(), array('class' => 'rarrow'));
474                 $links['next'] = html_writer::link(course_get_url($course, $forward), $nextlink, $params);
475             }
476             $forward++;
477         }
479         return $links;
480     }
482     /**
483      * Generate the header html of a stealth section
484      *
485      * @param int $sectionno The section number in the coruse which is being dsiplayed
486      * @return string HTML to output.
487      */
488     protected function stealth_section_header($sectionno) {
489         $o = '';
490         $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix orphaned hidden'));
491         $o.= html_writer::tag('div', '', array('class' => 'left side'));
492         $o.= html_writer::tag('div', '', array('class' => 'right side'));
493         $o.= html_writer::start_tag('div', array('class' => 'content'));
494         $o.= $this->output->heading(get_string('orphanedactivities'), 3, 'sectionname');
495         return $o;
496     }
498     /**
499      * Generate footer html of a stealth section
500      *
501      * @return string HTML to output.
502      */
503     protected function stealth_section_footer() {
504         $o = html_writer::end_tag('div');
505         $o.= html_writer::end_tag('li');
506         return $o;
507     }
509     /**
510      * Generate the html for a hidden section
511      *
512      * @param int $sectionno The section number in the coruse which is being dsiplayed
513      * @return string HTML to output.
514      */
515     protected function section_hidden($sectionno) {
516         $o = '';
517         $o.= html_writer::start_tag('li', array('id' => 'section-'.$sectionno, 'class' => 'section main clearfix hidden'));
518         $o.= html_writer::tag('div', '', array('class' => 'left side'));
519         $o.= html_writer::tag('div', '', array('class' => 'right side'));
520         $o.= html_writer::start_tag('div', array('class' => 'content'));
521         $o.= get_string('notavailable');
522         $o.= html_writer::end_tag('div');
523         $o.= html_writer::end_tag('li');
524         return $o;
525     }
527     /**
528      * Output the html for a single section page .
529      *
530      * @param stdClass $course The course entry from DB
531      * @param array $sections The course_sections entries from the DB
532      * @param array $mods used for print_section()
533      * @param array $modnames used for print_section()
534      * @param array $modnamesused used for print_section()
535      * @param int $displaysection The section number in the course which is being displayed
536      */
537     public function print_single_section_page($course, $sections, $mods, $modnames, $modnamesused, $displaysection) {
538         global $PAGE;
540         // Can we view the section in question?
541         $context = context_course::instance($course->id);
542         $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
544         if (!isset($sections[$displaysection])) {
545             // This section doesn't exist
546             print_error('unknowncoursesection', 'error', null, $course->fullname);
547             return;
548         }
550         if (!$sections[$displaysection]->visible && !$canviewhidden) {
551             if (!$course->hiddensections) {
552                 echo $this->start_section_list();
553                 echo $this->section_hidden($displaysection);
554                 echo $this->end_section_list();
555             }
556             // Can't view this section.
557             return;
558         }
560         // Copy activity clipboard..
561         echo $this->course_activity_clipboard($course, $displaysection);
563         // General section if non-empty.
564         $thissection = $sections[0];
565         if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
566             echo $this->start_section_list();
567             echo $this->section_header($thissection, $course, true, $displaysection);
568             print_section($course, $thissection, $mods, $modnamesused, true, "100%", false, $displaysection);
569             if ($PAGE->user_is_editing()) {
570                 print_section_add_menus($course, 0, $modnames, false, false, $displaysection);
571             }
572             echo $this->section_footer();
573             echo $this->end_section_list();
574         }
576         // Start single-section div
577         echo html_writer::start_tag('div', array('class' => 'single-section'));
579         // Title with section navigation links.
580         $sectionnavlinks = $this->get_nav_links($course, $sections, $displaysection);
581         $sectiontitle = '';
582         $sectiontitle .= html_writer::start_tag('div', array('class' => 'section-navigation header headingblock'));
583         $sectiontitle .= html_writer::tag('span', $sectionnavlinks['previous'], array('class' => 'mdl-left'));
584         $sectiontitle .= html_writer::tag('span', $sectionnavlinks['next'], array('class' => 'mdl-right'));
585         // Title attributes
586         $titleattr = 'mdl-align title';
587         if (!$sections[$displaysection]->visible) {
588             $titleattr .= ' dimmed_text';
589         }
590         $sectiontitle .= html_writer::tag('div', get_section_name($course, $sections[$displaysection]), array('class' => $titleattr));
591         $sectiontitle .= html_writer::end_tag('div');
592         echo $sectiontitle;
594         // Now the list of sections..
595         echo $this->start_section_list();
597         // The requested section page.
598         $thissection = $sections[$displaysection];
599         echo $this->section_header($thissection, $course, true, $displaysection);
600         // Show completion help icon.
601         $completioninfo = new completion_info($course);
602         echo $completioninfo->display_help_icon();
604         print_section($course, $thissection, $mods, $modnamesused, true, '100%', false, $displaysection);
605         if ($PAGE->user_is_editing()) {
606             print_section_add_menus($course, $displaysection, $modnames, false, false, $displaysection);
607         }
608         echo $this->section_footer();
609         echo $this->end_section_list();
611         // Display section bottom navigation.
612         $courselink = html_writer::link(course_get_url($course), get_string('returntomaincoursepage'));
613         $sectionbottomnav = '';
614         $sectionbottomnav .= html_writer::start_tag('div', array('class' => 'section-navigation mdl-bottom'));
615         $sectionbottomnav .= html_writer::tag('span', $sectionnavlinks['previous'], array('class' => 'mdl-left'));
616         $sectionbottomnav .= html_writer::tag('span', $sectionnavlinks['next'], array('class' => 'mdl-right'));
617         $sectionbottomnav .= html_writer::tag('div', $courselink, array('class' => 'mdl-align'));
618         $sectionbottomnav .= html_writer::end_tag('div');
619         echo $sectionbottomnav;
621         // close single-section div.
622         echo html_writer::end_tag('div');
623     }
625     /**
626      * Output the html for a multiple section page
627      *
628      * @param stdClass $course The course entry from DB
629      * @param array $sections The course_sections entries from the DB
630      * @param array $mods used for print_section()
631      * @param array $modnames used for print_section()
632      * @param array $modnamesused used for print_section()
633      */
634     public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
635         global $PAGE;
637         $context = context_course::instance($course->id);
638         // Title with completion help icon.
639         $completioninfo = new completion_info($course);
640         echo $completioninfo->display_help_icon();
641         echo $this->output->heading($this->page_title(), 2, 'accesshide');
643         // Copy activity clipboard..
644         echo $this->course_activity_clipboard($course);
646         // Now the list of sections..
647         echo $this->start_section_list();
649         // General section if non-empty.
650         $thissection = $sections[0];
651         unset($sections[0]);
652         if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
653             echo $this->section_header($thissection, $course, false);
654             print_section($course, $thissection, $mods, $modnamesused, true);
655             if ($PAGE->user_is_editing()) {
656                 print_section_add_menus($course, 0, $modnames);
657             }
658             echo $this->section_footer();
659         }
661         $canviewhidden = has_capability('moodle/course:viewhiddensections', $context);
662         for ($section = 1; $section <= $course->numsections; $section++) {
663             if (!empty($sections[$section])) {
664                 $thissection = $sections[$section];
665             } else {
666                 // This will create a course section if it doesn't exist..
667                 $thissection = get_course_section($section, $course->id);
669                 // The returned section is only a bare database object rather than
670                 // a section_info object - we will need at least the uservisible
671                 // field in it.
672                 $thissection->uservisible = true;
673                 $thissection->availableinfo = null;
674                 $thissection->showavailability = 0;
675             }
676             // Show the section if the user is permitted to access it, OR if it's not available
677             // but showavailability is turned on
678             $showsection = $thissection->uservisible ||
679                     ($thissection->visible && !$thissection->available && $thissection->showavailability);
680             if (!$showsection) {
681                 // Hidden section message is overridden by 'unavailable' control
682                 // (showavailability option).
683                 if (!$course->hiddensections && $thissection->available) {
684                     echo $this->section_hidden($section);
685                 }
687                 unset($sections[$section]);
688                 continue;
689             }
691             if (!$PAGE->user_is_editing() && $course->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
692                 // Display section summary only.
693                 echo $this->section_summary($thissection, $course, $mods);
694             } else {
695                 echo $this->section_header($thissection, $course, false);
696                 if ($thissection->uservisible) {
697                     print_section($course, $thissection, $mods, $modnamesused);
698                     if ($PAGE->user_is_editing()) {
699                         print_section_add_menus($course, $section, $modnames);
700                     }
701                 }
702                 echo $this->section_footer();
703             }
705             unset($sections[$section]);
706         }
708         if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
709             // Print stealth sections if present.
710             $modinfo = get_fast_modinfo($course);
711             foreach ($sections as $section => $thissection) {
712                 if (empty($modinfo->sections[$section])) {
713                     continue;
714                 }
715                 echo $this->stealth_section_header($section);
716                 print_section($course, $thissection, $mods, $modnamesused);
717                 echo $this->stealth_section_footer();
718             }
720             echo $this->end_section_list();
722             echo html_writer::start_tag('div', array('id' => 'changenumsections', 'class' => 'mdl-right'));
724             // Increase number of sections.
725             $straddsection = get_string('increasesections', 'moodle');
726             $url = new moodle_url('/course/changenumsections.php',
727                 array('courseid' => $course->id,
728                       'increase' => true,
729                       'sesskey' => sesskey()));
730             $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
731             echo html_writer::link($url, $icon.get_accesshide($straddsection), array('class' => 'increase-sections'));
733             if ($course->numsections > 0) {
734                 // Reduce number of sections sections.
735                 $strremovesection = get_string('reducesections', 'moodle');
736                 $url = new moodle_url('/course/changenumsections.php',
737                     array('courseid' => $course->id,
738                           'increase' => false,
739                           'sesskey' => sesskey()));
740                 $icon = $this->output->pix_icon('t/switch_minus', $strremovesection);
741                 echo html_writer::link($url, $icon.get_accesshide($strremovesection), array('class' => 'reduce-sections'));
742             }
744             echo html_writer::end_tag('div');
745         } else {
746             echo $this->end_section_list();
747         }
749     }
751     /**
752      * Generate html for a section summary text
753      *
754      * @param stdClass $section The course_section entry from DB
755      * @return string HTML to output.
756      */
757     protected function format_summary_text($section) {
758         $context = context_course::instance($section->course);
759         $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php',
760             $context->id, 'course', 'section', $section->id);
762         $options = new stdClass();
763         $options->noclean = true;
764         $options->overflowdiv = true;
765         return format_text($summarytext, $section->summaryformat, $options);
766     }
768     /**
769      * Is the section passed in the current section? (Note this isn't strictly
770      * a renderering method, but neater here).
771      *
772      * @param stdClass $course The course entry from DB
773      * @param stdClass $section The course_section entry from the DB
774      * @return bool true if the section is current
775      */
776     protected function is_section_current($section, $course) {
777         return ($course->marker == $section->section);
778     }