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
/**
18
 * Base class for course format plugins
19
 *
20
 * @package    core_course
21
 * @copyright  2012 Marina Glancy
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
use core_courseformat\base as course_format;
28
use core_courseformat\output\site_renderer;
29
 
30
/**
31
 * Returns an instance of format class (extending course_format) for given course
32
 *
33
 * @param int|stdClass $courseorid either course id or
34
 *     an object that has the property 'format' and may contain property 'id'
35
 * @return course_format
36
 */
37
function course_get_format($courseorid) {
38
    return course_format::instance($courseorid);
39
}
40
 
41
/**
42
 * Pseudo course format used for the site main page
43
 *
44
 * @package    core_course
45
 * @copyright  2012 Marina Glancy
46
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 */
48
class format_site extends course_format {
49
 
50
    /**
51
     * Returns the display name of the given section that the course prefers.
52
     *
53
     * @param int|stdClass $section Section object from database or just field section.section
1441 ariadna 54
     * @return string Display name that the course format prefers, e.g. "Topic 2"
1 efrain 55
     */
56
    function get_section_name($section) {
57
        $section = $this->get_section($section);
58
        if ((string)$section->name !== '') {
59
            // Return the name the user set.
60
            return format_string($section->name, true, array('context' => context_course::instance($this->courseid)));
61
        }
1441 ariadna 62
        // The section zero is located in a block.
63
        if ($section->sectionnum == 0) {
64
            return get_string('block');
65
        }
66
        return get_string('site');
1 efrain 67
    }
68
 
69
    /**
70
     * For this fake course referring to the whole site, the site homepage is always returned
71
     * regardless of arguments
72
     *
73
     * @param int|stdClass $section
74
     * @param array $options
75
     * @return null|moodle_url
76
     */
77
    public function get_view_url($section, $options = array()) {
78
        return new moodle_url('/', array('redirect' => 0));
79
    }
80
 
81
    /**
82
     * Returns the list of blocks to be automatically added on the site frontpage when moodle is installed
83
     *
84
     * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
85
     *     each of values is an array of block names (for left and right side columns)
86
     */
87
    public function get_default_blocks() {
88
        return blocks_get_default_site_course_blocks();
89
    }
90
 
1441 ariadna 91
    #[\Override]
92
    public function supports_ajax() {
93
        // All home page is rendered in the backend, we only need an ajax editor components in edit mode.
94
        // This will also prevent redirectng to the login page when a guest tries to access the site,
95
        // and will make the home page loading faster.
96
        $ajaxsupport = new stdClass();
97
        $ajaxsupport->capable = $this->show_editor();
98
        return $ajaxsupport;
99
    }
100
 
101
    #[\Override]
102
    public function supports_components() {
103
        return true;
104
    }
105
 
106
    #[\Override]
107
    public function uses_sections() {
108
        return true;
109
    }
110
 
1 efrain 111
    /**
112
     * Definitions of the additional options that site uses
113
     *
114
     * @param bool $foreditform
115
     * @return array of options
116
     */
117
    public function course_format_options($foreditform = false) {
118
        static $courseformatoptions = false;
119
        if ($courseformatoptions === false) {
120
            $courseformatoptions = array(
121
                'numsections' => array(
122
                    'default' => 1,
123
                    'type' => PARAM_INT,
124
                ),
125
            );
126
        }
127
        return $courseformatoptions;
128
    }
129
 
130
    /**
131
     * Returns whether this course format allows the activity to
132
     * have "triple visibility state" - visible always, hidden on course page but available, hidden.
133
     *
134
     * @param stdClass|cm_info $cm course module (may be null if we are displaying a form for adding a module)
135
     * @param stdClass|section_info $section section where this module is located or will be added to
136
     * @return bool
137
     */
138
    public function allow_stealth_module_visibility($cm, $section) {
139
        return true;
140
    }
141
 
142
    /**
143
     * Returns instance of page renderer used by the site page
144
     *
145
     * @param moodle_page $page the current page
146
     * @return renderer_base
147
     */
148
    public function get_renderer(moodle_page $page) {
149
        return new site_renderer($page, null);
150
    }
151
 
152
    /**
153
     * Site format uses only section 1.
154
     *
155
     * @return int
156
     */
157
    public function get_sectionnum(): int {
158
        return 1;
159
    }
160
}
161
 
162
/**
163
 * 'Converts' a value from what is stored in the database into what is used by edit forms.
164
 *
165
 * @param array $dest The destination array
166
 * @param array $source The source array
167
 * @param array $option The definition structure of the option.
168
 * @param string $optionname The name of the option, as provided in the definition.
169
 */
170
function contract_value(array &$dest, array $source, array $option, string $optionname): void {
171
    if (substr($optionname, -7) == '_editor') { // Suffix '_editor' indicates that the element is an editor.
172
        $name = substr($optionname, 0, -7);
173
        if (isset($source[$name])) {
174
            $dest[$optionname] = [
175
                'text' => clean_param_if_not_null($source[$name], $option['type'] ?? PARAM_RAW),
176
                'format' => clean_param_if_not_null($source[$name . 'format'], PARAM_INT),
177
            ];
178
        }
179
    } else {
180
        if (isset($source[$optionname])) {
181
            $dest[$optionname] = clean_param_if_not_null($source[$optionname], $option['type'] ?? PARAM_RAW);
182
        }
183
    }
184
}
185
 
186
/**
187
 * Cleans the given param, unless it is null.
188
 *
189
 * @param mixed $param The variable we are cleaning.
190
 * @param string $type Expected format of param after cleaning.
191
 * @return mixed Null if $param is null, otherwise the cleaned value.
192
 * @throws coding_exception
193
 */
194
function clean_param_if_not_null($param, string $type = PARAM_RAW) {
195
    if ($param === null) {
196
        return null;
197
    } else {
198
        return clean_param($param, $type);
199
    }
200
}
201
 
202
/**
203
 * 'Converts' a value from what is used in edit forms into a value(s) to be stored in the database.
204
 *
205
 * @param array $dest The destination array
206
 * @param array $source The source array
207
 * @param array $option The definition structure of the option.
208
 * @param string $optionname The name of the option, as provided in the definition.
209
 */
210
function expand_value(array &$dest, array $source, array $option, string $optionname): void {
211
    if (substr($optionname, -7) == '_editor') { // Suffix '_editor' indicates that the element is an editor.
212
        $name = substr($optionname, 0, -7);
213
        if (is_string($source[$optionname])) {
214
            $dest[$name]            = clean_param($source[$optionname], $option['type'] ?? PARAM_RAW);
215
            $dest[$name . 'format'] = 1;
216
        } else {
217
            $dest[$name]            = clean_param($source[$optionname]['text'], $option['type'] ?? PARAM_RAW);
218
            $dest[$name . 'format'] = clean_param($source[$optionname]['format'], PARAM_INT);
219
        }
220
        unset($dest[$optionname]);
221
    } else {
222
        $dest[$optionname] = clean_param($source[$optionname], $option['type'] ?? PARAM_RAW);
223
    }
224
}
225
 
226
/**
227
 * Course-module fragment renderer method.
228
 *
229
 * The fragment arguments are id and sr (section return).
230
 *
231
 * @param array $args The fragment arguments.
232
 * @return string The rendered cm item.
233
 *
234
 * @throws require_login_exception
235
 */
236
function core_courseformat_output_fragment_cmitem($args): string {
237
    global $PAGE;
238
 
239
    [$course, $cm] = get_course_and_cm_from_cmid($args['id']);
240
    if (!can_access_course($course, null, '', true) || !$cm->uservisible) {
241
        throw new require_login_exception('Activity is not available');
242
    }
243
 
244
    $format = course_get_format($course);
1441 ariadna 245
    if (isset($args['pagesectionid'])) {
246
        $format->set_sectionid($args['pagesectionid']);
247
    } else if (isset($args['sr'])) {
1 efrain 248
        $format->set_sectionnum($args['sr']);
249
    }
250
    $renderer = $format->get_renderer($PAGE);
251
    $section = $cm->get_section_info();
252
    return $renderer->course_section_updated_cm_item($format, $section, $cm);
253
}
254
 
255
/**
256
 * Section fragment renderer method.
257
 *
258
 * The fragment arguments are courseid, section id and sr (section return).
259
 *
260
 * @param array $args The fragment arguments.
261
 * @return string The rendered section.
262
 *
263
 * @throws require_login_exception
264
 */
265
function core_courseformat_output_fragment_section($args): string {
266
    global $PAGE;
267
 
268
    $course = get_course($args['courseid']);
269
    if (!can_access_course($course, null, '', true)) {
270
        throw new require_login_exception('Course is not available');
271
    }
272
 
273
    $format = course_get_format($course);
1441 ariadna 274
    if (isset($args['pagesectionid'])) {
275
        $format->set_sectionid($args['pagesectionid']);
276
    } else if (isset($args['sr'])) {
1 efrain 277
        $format->set_sectionnum($args['sr']);
278
    }
279
 
280
    $modinfo = $format->get_modinfo();
281
    $section = $modinfo->get_section_info_by_id($args['id'], MUST_EXIST);
282
    if (!$section->uservisible) {
283
        throw new require_login_exception('Section is not available');
284
    }
285
 
286
    $renderer = $format->get_renderer($PAGE);
287
    return $renderer->course_section_updated($format, $section);
288
}