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 * Contains the default section course format output class. 19 * 20 * @package core_courseformat 21 * @copyright 2020 Ferran Recio <ferran@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_courseformat\output\local\content; 26 27 use core\output\named_templatable; 28 use core_courseformat\base as course_format; 29 use core_courseformat\output\local\courseformat_named_templatable; 30 use moodle_url; 31 use renderable; 32 use stdClass; 33 34 /** 35 * Base class to render a course add section buttons. 36 * 37 * @package core_courseformat 38 * @copyright 2020 Ferran Recio <ferran@moodle.com> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class addsection implements named_templatable, renderable { 42 43 use courseformat_named_templatable; 44 45 /** @var course_format the course format class */ 46 protected $format; 47 48 /** 49 * Constructor. 50 * 51 * @param course_format $format the course format 52 */ 53 public function __construct(course_format $format) { 54 $this->format = $format; 55 } 56 57 /** 58 * Export this data so it can be used as the context for a mustache template. 59 * 60 * @param \renderer_base $output typically, the renderer that's calling this function 61 * @return stdClass data context for a mustache template 62 */ 63 public function export_for_template(\renderer_base $output): stdClass { 64 65 // If no editor must be displayed, just return an empty structure. 66 if (!$this->format->show_editor(['moodle/course:update'])) { 67 return new stdClass(); 68 } 69 70 $format = $this->format; 71 $course = $format->get_course(); 72 $options = $format->get_format_options(); 73 74 $lastsection = $format->get_last_section_number(); 75 $maxsections = $format->get_max_sections(); 76 77 // Component based formats handle add section button in the frontend. 78 $show = ($lastsection < $maxsections) || $format->supports_components(); 79 80 $supportsnumsections = array_key_exists('numsections', $options); 81 if ($supportsnumsections) { 82 $data = $this->get_num_sections_data($output, $lastsection, $maxsections); 83 } else if (course_get_format($course)->uses_sections() && $show) { 84 $data = $this->get_add_section_data($output, $lastsection, $maxsections); 85 } 86 87 if (count((array)$data)) { 88 $data->showaddsection = true; 89 } 90 91 return $data; 92 } 93 94 /** 95 * Get the legacy num section add/remove section buttons data. 96 * 97 * Current course format has 'numsections' option, which is very confusing and we suggest course format 98 * developers to get rid of it (see MDL-57769 on how to do it). 99 * 100 * @param \renderer_base $output typically, the renderer that's calling this function 101 * @param int $lastsection the last section number 102 * @param int $maxsections the maximum number of sections 103 * @return stdClass data context for a mustache template 104 */ 105 protected function get_num_sections_data(\renderer_base $output, int $lastsection, int $maxsections): stdClass { 106 $format = $this->format; 107 $course = $format->get_course(); 108 $data = new stdClass(); 109 110 if ($lastsection < $maxsections) { 111 $data->increase = (object) [ 112 'url' => new moodle_url( 113 '/course/changenumsections.php', 114 ['courseid' => $course->id, 'increase' => true, 'sesskey' => sesskey()] 115 ), 116 ]; 117 } 118 119 if ($course->numsections > 0) { 120 $data->decrease = (object) [ 121 'url' => new moodle_url( 122 '/course/changenumsections.php', 123 ['courseid' => $course->id, 'increase' => false, 'sesskey' => sesskey()] 124 ), 125 ]; 126 } 127 return $data; 128 } 129 130 /** 131 * Get the add section button data. 132 * 133 * Current course format does not have 'numsections' option but it has multiple sections suppport. 134 * Display the "Add section" link that will insert a section in the end. 135 * Note to course format developers: inserting sections in the other positions should check both 136 * capabilities 'moodle/course:update' and 'moodle/course:movesections'. 137 * 138 * @param \renderer_base $output typically, the renderer that's calling this function 139 * @param int $lastsection the last section number 140 * @param int $maxsections the maximum number of sections 141 * @return stdClass data context for a mustache template 142 */ 143 protected function get_add_section_data(\renderer_base $output, int $lastsection, int $maxsections): stdClass { 144 $format = $this->format; 145 $course = $format->get_course(); 146 $data = new stdClass(); 147 148 if (get_string_manager()->string_exists('addsections', 'format_' . $course->format)) { 149 $addstring = get_string('addsections', 'format_' . $course->format); 150 } else { 151 $addstring = get_string('addsections'); 152 } 153 154 $params = ['courseid' => $course->id, 'insertsection' => 0, 'sesskey' => sesskey()]; 155 156 $singlesection = $this->format->get_section_number(); 157 if ($singlesection) { 158 $params['sectionreturn'] = $singlesection; 159 } 160 161 $data->addsections = (object) [ 162 'url' => new moodle_url('/course/changenumsections.php', $params), 163 'title' => $addstring, 164 'newsection' => $maxsections - $lastsection, 165 ]; 166 return $data; 167 } 168 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body