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;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/course/renderer.php');
23
 
24
use cm_info;
25
use coding_exception;
26
use core_course_renderer;
27
use core_courseformat\base as course_format;
28
use html_writer;
29
use moodle_page;
30
use renderable;
31
use section_info;
32
use stdClass;
33
 
34
/**
35
 * Contains the default section course format output class.
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
abstract class section_renderer extends core_course_renderer {
42
 
43
    /**
44
     * Constructor method, calls the parent constructor.
45
     *
46
     * @param moodle_page $page
47
     * @param string $target one of rendering target constants
48
     */
49
    public function __construct(moodle_page $page, $target) {
50
        parent::__construct($page, $target);
51
 
52
        // Ensure capabilities for section editing controls match those defined in course/view.php to ensure that they work
53
        // when called via an AJAX request.
54
        if (course_get_format($page->course)->uses_sections()) {
55
            $page->set_other_editing_capability('moodle/course:sectionvisibility');
56
            $page->set_other_editing_capability('moodle/course:movesections');
57
        }
58
    }
59
 
60
    /**
61
     * Renders the provided widget and returns the HTML to display it.
62
     *
63
     * Course format templates uses a similar subfolder structure to the renderable classes.
64
     * This method find out the specific template for a course widget. That's the reason why
65
     * this render method is different from the normal plugin renderer one.
66
     *
67
     * course format templatables can be rendered using the core_course/local/* templates.
68
     * Format plugins are free to override the default template location using render_xxx methods as usual.
69
     *
70
     * @param renderable $widget instance with renderable interface
71
     * @return string the widget HTML
72
     */
73
    public function render(renderable $widget) {
74
        global $CFG;
75
        $fullpath = str_replace('\\', '/', get_class($widget));
76
        $classparts = explode('/', $fullpath);
77
        // Strip namespaces.
78
        $classname = array_pop($classparts);
79
        // Remove _renderable suffixes.
80
        $classname = preg_replace('/_renderable$/', '', $classname);
81
 
82
        $rendermethod = 'render_' . $classname;
83
        if (method_exists($this, $rendermethod)) {
84
            return $this->$rendermethod($widget);
85
        }
86
 
87
        // If nothing works, let the parent class decide.
88
        return parent::render($widget);
89
    }
90
 
91
    /**
92
     * Generate the section title, wraps it in a link to the section page if page is to be displayed on a separate page
93
     *
94
     * @param stdClass $section The course_section entry from DB
95
     * @param stdClass $course The course entry from DB
96
     * @return string HTML to output.
97
     */
98
    public function section_title($section, $course) {
99
        $title = get_section_name($course, $section);
100
        $url = course_get_url($course, $section->section, array('navigation' => true));
101
        if ($url) {
102
            $title = html_writer::link($url, $title);
103
        }
104
        return $title;
105
    }
106
 
107
    /**
108
     * Generate the section title to be displayed on the section page, without a link
109
     *
110
     * @param stdClass $section The course_section entry from DB
111
     * @param stdClass $course The course entry from DB
112
     * @return string HTML to output.
113
     */
114
    public function section_title_without_link($section, $course) {
115
        return get_section_name($course, $section);
116
    }
117
 
118
    /**
119
     * Get the updated rendered version of a cm list item.
120
     *
121
     * This method is used when an activity is duplicated or copied in on the client side without refreshing the page.
122
     * Note that the previous method is used every time an activity is rendered, independent of it is the initial page
123
     * loading or an Ajax update. In this case, course_section_updated_cm_item will only be used when the course editor
124
     * requires to get an updated cm item HTML to perform partial page refresh. It will be used for suporting the course
125
     * editor webservices.
126
     *
127
     * By default, the template used for update a cm_item is the same as when it renders initially, but format plugins are
128
     * free to override this methos to provide extra affects or so.
129
     *
130
     * @param course_format $format the course format
131
     * @param section_info $section the section info
132
     * @param cm_info $cm the course module ionfo
133
     * @param array $displayoptions optional extra display options
134
     * @return string the rendered element
135
     */
136
    public function course_section_updated_cm_item(
137
        course_format $format,
138
        section_info $section,
139
        cm_info $cm,
140
        array $displayoptions = []
141
    ) {
142
 
143
        $cmitemclass = $format->get_output_classname('content\\section\\cmitem');
144
        $cmitem = new $cmitemclass($format, $section, $cm, $displayoptions);
145
        return $this->render($cmitem);
146
    }
147
 
148
    /**
149
     * Get the updated rendered version of a section.
150
     *
151
     * This method will only be used when the course editor requires to get an updated cm item HTML
152
     * to perform partial page refresh. It will be used for supporting the course editor webservices.
153
     *
154
     * By default, the template used for update a section is the same as when it renders initially,
155
     * but format plugins are free to override this method to provide extra effects or so.
156
     *
157
     * @param course_format $format the course format
158
     * @param section_info $section the section info
159
     * @return string the rendered element
160
     */
161
    public function course_section_updated(
162
        course_format $format,
163
        section_info $section
164
    ): string {
165
        $sectionclass = $format->get_output_classname('content\\section');
166
        $output = new $sectionclass($format, $section);
167
        return $this->render($output);
168
    }
169
 
170
    /**
171
     * Get the course index drawer with placeholder.
172
     *
173
     * The default course index is loaded after the page is ready. Format plugins can override
174
     * this method to provide an alternative course index.
175
     *
176
     * If the format is not compatible with the course index, this method will return an empty string.
177
     *
178
     * @param course_format $format the course format
179
     * @return String the course index HTML.
180
     */
181
    public function course_index_drawer(course_format $format): ?String {
182
        if ($format->uses_course_index()) {
183
            include_course_editor($format);
184
            return $this->render_from_template('core_courseformat/local/courseindex/drawer', []);
185
        }
186
        return '';
187
    }
188
 
189
    /**
190
     * Render the enable bulk editing button.
191
     * @param course_format $format the course format
192
     * @return string|null the enable bulk button HTML (or null if no bulk available).
193
     */
194
    public function bulk_editing_button(course_format $format): ?string {
195
        if (!$format->show_editor() || !$format->supports_components()) {
196
            return null;
197
        }
198
        $widgetclass = $format->get_output_classname('content\\bulkedittoggler');
199
        $widget = new $widgetclass($format);
200
        return $this->render($widget);
201
    }
202
 
203
    /**
204
     * Generate the html for a hidden section
205
     *
206
     * @param int $sectionno The section number in the course which is being displayed
207
     * @param int|stdClass $courseorid The course to get the section name for (object or just course id)
208
     * @return string HTML to output.
209
     */
210
    protected function section_hidden($sectionno, $courseorid = null) {
211
        if ($courseorid) {
212
            $sectionname = get_section_name($courseorid, $sectionno);
213
            $strnotavailable = get_string('notavailablecourse', '', $sectionname);
214
        } else {
215
            $strnotavailable = get_string('notavailable');
216
        }
217
 
218
        $o = '';
219
        $o .= html_writer::start_tag('li', [
220
            'id' => 'section-' . $sectionno,
221
            'class' => 'section main clearfix hidden',
222
            'data-sectionid' => $sectionno
223
        ]);
224
        $o .= html_writer::tag('div', '', array('class' => 'left side'));
225
        $o .= html_writer::tag('div', '', array('class' => 'right side'));
226
        $o .= html_writer::start_tag('div', array('class' => 'content'));
227
        $o .= html_writer::tag('div', $strnotavailable);
228
        $o .= html_writer::end_tag('div');
229
        $o .= html_writer::end_tag('li');
230
        return $o;
231
    }
232
}