Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_courseformat\output\local;
18
 
19
use core\output\named_templatable;
20
use core_courseformat\base as course_format;
21
use course_modinfo;
1441 ariadna 22
use section_info;
1 efrain 23
use renderable;
24
 
25
/**
26
 * Base class to render a course format.
27
 *
28
 * @package   core_courseformat
29
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
30
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class content implements named_templatable, renderable {
33
    use courseformat_named_templatable;
34
 
35
    /** @var \core_courseformat\base the course format class */
36
    protected $format;
37
 
38
    /** @var string the section format class */
39
    protected $sectionclass;
40
 
41
    /** @var string the add section output class name */
42
    protected $addsectionclass;
43
 
44
    /** @var string section navigation class name */
45
    protected $sectionnavigationclass;
46
 
47
    /** @var string section selector class name */
48
    protected $sectionselectorclass;
49
 
50
    /** @var string the section control menu class */
51
    protected $sectioncontrolmenuclass;
52
 
53
    /** @var string bulk editor bar toolbox */
54
    protected $bulkedittoolsclass;
55
 
56
    /** @var bool if uses add section */
57
    protected $hasaddsection = true;
58
 
59
    /**
60
     * Constructor.
61
     *
62
     * @param course_format $format the coruse format
63
     */
64
    public function __construct(course_format $format) {
65
        $this->format = $format;
66
 
67
        // Load output classes names from format.
68
        $this->sectionclass = $format->get_output_classname('content\\section');
69
        $this->addsectionclass = $format->get_output_classname('content\\addsection');
70
        $this->sectionnavigationclass = $format->get_output_classname('content\\sectionnavigation');
71
        $this->sectionselectorclass = $format->get_output_classname('content\\sectionselector');
72
        $this->bulkedittoolsclass = $format->get_output_classname('content\\bulkedittools');
73
        $this->sectioncontrolmenuclass = $format->get_output_classname('content\\section\\controlmenu');
74
    }
75
 
76
    /**
77
     * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
78
     *
79
     * @param \renderer_base $output typically, the renderer that's calling this function
80
     * @return \stdClass data context for a mustache template
81
     */
82
    public function export_for_template(\renderer_base $output) {
83
        global $PAGE;
84
        $format = $this->format;
85
 
86
        $sections = $this->export_sections($output);
87
        $initialsection = '';
88
 
89
        $data = (object)[
90
            'title' => $format->page_title(), // This method should be in the course_format class.
91
            'initialsection' => $initialsection,
92
            'sections' => $sections,
93
            'format' => $format->get_format(),
1441 ariadna 94
            'sectionreturn' => 'null', // Mustache templates don't display NULL, so pass a string value.
95
            'pagesectionid' => $this->format->get_sectionid() ?? 'null', // Pass a string value if NULL.
1 efrain 96
        ];
97
 
98
        // The single section format has extra navigation.
99
        if ($this->format->get_sectionid()) {
100
            $singlesectionnum = $this->format->get_sectionnum();
101
            if (!$PAGE->theme->usescourseindex) {
102
                $sectionnavigation = new $this->sectionnavigationclass($format, $singlesectionnum);
103
                $data->sectionnavigation = $sectionnavigation->export_for_template($output);
104
 
105
                $sectionselector = new $this->sectionselectorclass($format, $sectionnavigation);
106
                $data->sectionselector = $sectionselector->export_for_template($output);
107
            }
108
            $data->hasnavigation = true;
109
            $data->singlesection = array_shift($data->sections);
110
            $data->sectionreturn = $singlesectionnum;
111
        }
112
 
113
        if ($this->hasaddsection) {
114
            $addsection = new $this->addsectionclass($format);
115
            $data->numsections = $addsection->export_for_template($output);
116
        }
117
 
118
        if ($format->show_editor()) {
119
            $bulkedittools = new $this->bulkedittoolsclass($format);
120
            $data->bulkedittools = $bulkedittools->export_for_template($output);
121
        }
122
 
123
        return $data;
124
    }
125
 
126
    /**
127
     * Retrieves the action menu for the page header of the local content section.
128
     *
129
     * @param \renderer_base $output The renderer object used for rendering the action menu.
130
     * @return string|null The rendered action menu HTML, null if page no action menu is available.
131
     */
132
    public function get_page_header_action(\renderer_base $output): ?string {
133
        $sectionid = $this->format->get_sectionid();
134
        if ($sectionid !== null) {
135
            $modinfo = $this->format->get_modinfo();
136
            $sectioninfo = $modinfo->get_section_info_by_id($sectionid);
137
            /** @var \core_courseformat\output\local\content\section\controlmenu */
138
            $controlmenu = new $this->sectioncontrolmenuclass($this->format, $sectioninfo);
139
            return $output->render($controlmenu->get_action_menu($output));
140
        }
141
        return null;
142
    }
143
 
144
    /**
145
     * Export sections array data.
146
     *
1441 ariadna 147
     * @param \renderer_base $output typically, the renderer that's calling this function
1 efrain 148
     * @return array data context for a mustache template
149
     */
150
    protected function export_sections(\renderer_base $output): array {
151
 
152
        $format = $this->format;
153
        $course = $format->get_course();
154
        $modinfo = $this->format->get_modinfo();
155
 
156
        // Generate section list.
157
        $sections = [];
158
        $stealthsections = [];
159
        foreach ($this->get_sections_to_display($modinfo) as $sectionnum => $thissection) {
160
            // The course/view.php check the section existence but the output can be called
161
            // from other parts so we need to check it.
162
            if (!$thissection) {
163
                throw new \moodle_exception('unknowncoursesection', 'error', course_get_url($course),
164
                    format_string($course->fullname));
165
            }
166
 
1441 ariadna 167
            if (!$format->is_section_visible($thissection)) {
168
                continue;
169
            }
170
 
171
            /** @var \core_courseformat\output\local\content\section $section */
1 efrain 172
            $section = new $this->sectionclass($format, $thissection);
173
 
1441 ariadna 174
            if ($section->is_stealth()) {
1 efrain 175
                // Activities inside this section are 'orphaned', this section will be printed as 'stealth' below.
176
                if (!empty($modinfo->sections[$sectionnum])) {
177
                    $stealthsections[] = $section->export_for_template($output);
178
                }
179
                continue;
180
            }
181
 
182
            $sections[] = $section->export_for_template($output);
183
        }
184
        if (!empty($stealthsections)) {
185
            $sections = array_merge($sections, $stealthsections);
186
        }
187
        return $sections;
188
    }
189
 
190
    /**
191
     * Return an array of sections to display.
192
     *
193
     * This method is used to differentiate between display a specific section
194
     * or a list of them.
195
     *
196
     * @param course_modinfo $modinfo the current course modinfo object
197
     * @return section_info[] an array of section_info to display
198
     */
1441 ariadna 199
    protected function get_sections_to_display(course_modinfo $modinfo): array {
1 efrain 200
        $singlesectionid = $this->format->get_sectionid();
201
        if ($singlesectionid) {
202
            return [
203
                $modinfo->get_section_info_by_id($singlesectionid),
204
            ];
205
        }
206
        return $modinfo->get_listed_section_info_all();
207
    }
208
}