Proyectos de Subversion Moodle

Rev

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