Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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
 * format_buttons_renderer
19
 *
20
 * @package    format_buttons
21
 * @author     Rodrigo Brandão <https://www.linkedin.com/in/brandaorodrigo>
22
 * @copyright  2020 Rodrigo Brandão <rodrigo.brandao.contato@gmail.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once($CFG->dirroot. '/course/format/topics/lib.php');
29
 
30
/**
31
 * format_buttons
32
 *
33
 * @package    format_buttons
34
 * @author     Rodrigo Brandão (rodrigobrandao.com.br)
35
 * @copyright  2017 Rodrigo Brandão
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class format_buttons extends format_topics {
39
 
40
    /**
41
     * course_format_options
42
     *
43
     * @param bool $foreditform
44
     * @return array
45
     */
46
    public function course_format_options($foreditform = false) {
47
        global $PAGE;
48
 
49
        static $courseformatoptions = false;
50
 
51
        if ($courseformatoptions === false) {
52
            $courseconfig = get_config('moodlecourse');
53
 
54
            $courseformatoptions['numsections'] = array(
55
                'default' => $courseconfig->numsections,
56
                'type' => PARAM_INT,
57
            );
58
 
59
            $courseformatoptions['hiddensections'] = array(
60
                'default' => $courseconfig->hiddensections,
61
                'type' => PARAM_INT,
62
            );
63
 
64
            $courseformatoptions['showdefaultsectionname'] = array(
65
                'default' => get_config('format_buttons', 'showdefaultsectionname'),
66
                'type' => PARAM_INT,
67
            );
68
 
69
            $courseformatoptions['sectionposition'] = array(
70
                'default' => get_config('format_buttons', 'sectionposition'),
71
                'type' => PARAM_INT,
72
            );
73
 
74
            $courseformatoptions['inlinesections'] = array(
75
                'default' => get_config('format_buttons', 'inlinesections'),
76
                'type' => PARAM_INT,
77
            );
78
 
79
            $courseformatoptions['sequential'] = array(
80
                'default' => get_config('format_buttons', 'sequential'),
81
                'type' => PARAM_INT,
82
            );
83
 
84
            $courseformatoptions['sectiontype'] = array(
85
                'default' => get_config('format_buttons', 'sectiontype'),
86
                'type' => PARAM_TEXT,
87
            );
88
 
89
            $courseformatoptions['buttonstyle'] = array(
90
                'default' => get_config('format_buttons', 'buttonstyle'),
91
                'type' => PARAM_TEXT,
92
            );
93
 
94
            for ($i = 1; $i <= 12; $i++) {
95
                $divisortext = get_config('format_buttons', 'divisortext'.$i);
96
                if (!$divisortext) {
97
                    $divisortext = '';
98
                }
99
                $courseformatoptions['divisortext'.$i] = array(
100
                    'default' => $divisortext,
101
                    'type' => PARAM_TEXT,
102
                );
103
                $courseformatoptions['divisor'.$i] = array(
104
                    'default' => get_config('format_buttons', 'divisor'.$i),
105
                    'type' => PARAM_INT,
106
                );
107
            }
108
 
109
            $colorcurrent = get_config('format_buttons', 'colorcurrent');
110
            if (!$colorcurrent) {
111
                $colorcurrent = '';
112
            }
113
 
114
            $courseformatoptions['colorcurrent'] = array(
115
                'default' => $colorcurrent,
116
                'type' => PARAM_TEXT,
117
            );
118
 
119
            $colorvisible = get_config('format_buttons', 'colorvisible');
120
            if (!$colorvisible) {
121
                $colorvisible = '';
122
            }
123
 
124
            $courseformatoptions['colorvisible'] = array(
125
                'default' => $colorvisible,
126
                'type' => PARAM_TEXT,
127
            );
128
        }
129
 
130
        if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
131
            $courseconfig = get_config('moodlecourse');
132
 
133
            $max = $courseconfig->maxsections;
134
            if (!isset($max) || !is_numeric($max)) {
135
                $max = 52;
136
            }
137
 
138
            $sectionmenu = array();
139
            for ($i = 0; $i <= $max; $i++) {
140
                $sectionmenu[$i] = "$i";
141
            }
142
 
143
            $courseformatoptionsedit['numsections'] = array(
144
                'label' => new lang_string('numberweeks'),
145
                'element_type' => 'select',
146
                'element_attributes' => array($sectionmenu),
147
            );
148
 
149
            $courseformatoptionsedit['hiddensections'] = array(
150
                'label' => new lang_string('hiddensections'),
151
                'help' => 'hiddensections',
152
                'help_component' => 'moodle',
153
                'element_type' => 'select',
154
                'element_attributes' => array(
155
                    array(
156
 
157
                        1 => new lang_string('hiddensectionsinvisible')
158
                    )
159
                ),
160
            );
161
 
162
            $courseformatoptionsedit['showdefaultsectionname'] = array(
163
                'label' => get_string('showdefaultsectionname', 'format_buttons'),
164
                'help' => 'showdefaultsectionname',
165
                'help_component' => 'format_buttons',
166
                'element_type' => 'select',
167
                'element_attributes' => array(
168
                    array(
169
                        1 => get_string('yes', 'format_buttons'),
170
 
171
                    ),
172
                ),
173
            );
174
 
175
            $courseformatoptionsedit['sectionposition'] = array(
176
                'label' => get_string('sectionposition', 'format_buttons'),
177
                'help' => 'sectionposition',
178
                'help_component' => 'format_buttons',
179
                'element_type' => 'select',
180
                'element_attributes' => array(
181
                    array(
182
 
183
                        1 => get_string('below', 'format_buttons'),
184
                    ),
185
                ),
186
            );
187
 
188
            $courseformatoptionsedit['inlinesections'] = array(
189
                'label' => get_string('inlinesections', 'format_buttons'),
190
                'help' => 'inlinesections',
191
                'help_component' => 'format_buttons',
192
                'element_type' => 'select',
193
                'element_attributes' => array(
194
                    array(
195
                        1 => get_string('yes', 'format_buttons'),
196
 
197
                    ),
198
                ),
199
            );
200
 
201
            $courseformatoptionsedit['sequential'] = array(
202
                'label' => get_string('sequential', 'format_buttons'),
203
                'help_component' => 'format_buttons',
204
                'element_type' => 'select',
205
                'element_attributes' => array(
206
                    array(
207
 
208
                        1 => get_string('sequentialdesc', 'format_buttons'),
209
                    ),
210
                ),
211
            );
212
 
213
            $courseformatoptionsedit['sectiontype'] = array(
214
                'label' => get_string('sectiontype', 'format_buttons'),
215
                'help_component' => 'format_buttons',
216
                'element_type' => 'select',
217
                'element_attributes' => array(
218
                    array(
219
                        'numeric' => get_string('numeric', 'format_buttons'),
220
                        'roman' => get_string('roman', 'format_buttons'),
221
                        'alphabet' => get_string('alphabet', 'format_buttons'),
222
                    ),
223
                ),
224
            );
225
 
226
            $courseformatoptionsedit['buttonstyle'] = array(
227
                'label' => get_string('buttonstyle', 'format_buttons'),
228
                'help_component' => 'format_buttons',
229
                'element_type' => 'select',
230
                'element_attributes' => array(
231
                    array(
232
                        'circle' => get_string('circle', 'format_buttons'),
233
                        'square' => get_string('square', 'format_buttons'),
234
                    ),
235
                ),
236
            );
237
 
238
            for ($i = 1; $i <= 12; $i++) {
239
                $courseformatoptionsedit['divisortext'.$i] = array(
240
                    'label' => get_string('divisortext', 'format_buttons', $i),
241
                    'help' => 'divisortext',
242
                    'help_component' => 'format_buttons',
243
                    'element_type' => 'text',
244
                );
245
                $courseformatoptionsedit['divisor'.$i] = array(
246
                    'label' => get_string('divisor', 'format_buttons', $i),
247
                    'help' => 'divisortext',
248
                    'help_component' => 'format_buttons',
249
                    'element_type' => 'select',
250
                    'element_attributes' => array($sectionmenu),
251
                );
252
            }
253
 
254
            $courseformatoptionsedit['colorcurrent'] = array(
255
                'label' => get_string('colorcurrent', 'format_buttons'),
256
                'help' => 'colorcurrent',
257
                'help_component' => 'format_buttons',
258
                'element_type' => 'text',
259
            );
260
 
261
            $courseformatoptionsedit['colorvisible'] = array(
262
                'label' => get_string('colorvisible', 'format_buttons'),
263
                'help' => 'colorvisible',
264
                'help_component' => 'format_buttons',
265
                'element_type' => 'text',
266
            );
267
 
268
            $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
269
        }
270
        return $courseformatoptions;
271
    }
272
 
273
    /**
274
     * update_course_format_options
275
     *
276
     * @param stdclass|array $data
277
     * @param stdClass $oldcourse
278
     * @return bool
279
     */
280
    public function update_course_format_options($data, $oldcourse = null) {
281
        global $DB;
282
 
283
        $data = (array)$data;
284
 
285
        if ($oldcourse !== null) {
286
            $oldcourse = (array)$oldcourse;
287
 
288
            $options = $this->course_format_options();
289
 
290
            foreach ($options as $key => $unused) {
291
                if (!array_key_exists($key, $data)) {
292
                    if (array_key_exists($key, $oldcourse)) {
293
                        $data[$key] = $oldcourse[$key];
294
                    } else if ($key === 'numsections') {
295
                        $maxsection = $DB->get_field_sql('SELECT max(section) from
296
                        {course_sections} WHERE course = ?', array($this->courseid));
297
                        if ($maxsection) {
298
                            $data['numsections'] = $maxsection;
299
                        }
300
                    }
301
                }
302
            }
303
        }
304
 
305
        $changed = $this->update_format_options($data);
306
 
307
        if ($changed && array_key_exists('numsections', $data)) {
308
            $numsections = (int)$data['numsections'];
309
            $sql = 'SELECT max(section) from {course_sections} WHERE course = ?';
310
            $maxsection = $DB->get_field_sql($sql, array($this->courseid));
311
            for ($sectionnum = $maxsection; $sectionnum > $numsections; $sectionnum--) {
312
                if (!$this->delete_section($sectionnum, false)) {
313
                    break;
314
                }
315
            }
316
        }
317
        return $changed;
318
    }
319
 
320
    /**
321
     * get_view_url
322
     *
323
     * @param int|stdclass $section
324
     * @param array $options
325
     * @return null|moodle_url
326
     */
327
    public function get_view_url($section, $options = array()) {
328
        global $CFG;
329
 
330
        $course = $this->get_course();
331
 
332
        $url = new moodle_url('/course/view.php', array('id' => $course->id));
333
 
334
        $sr = null;
335
 
336
        if (array_key_exists('sr', $options)) {
337
            $sr = $options['sr'];
338
        }
339
 
340
        if (is_object($section)) {
341
            $sectionno = $section->section;
342
        } else {
343
            $sectionno = $section;
344
        }
345
 
346
        if ($sectionno !== null) {
347
            if ($sr !== null) {
348
                if ($sr) {
349
                    $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
350
                    $sectionno = $sr;
351
                } else {
352
                    $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
353
                }
354
            } else {
355
                $usercoursedisplay = 0;
356
            }
357
            if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
358
                $url->param('section', $sectionno);
359
            } else {
360
                if (empty($CFG->linkcoursesections) && !empty($options['navigation'])) {
361
                    return null;
362
                }
363
                $url->set_anchor('section-'.$sectionno);
364
            }
365
        }
366
 
367
        return $url;
368
    }
369
}
370
 
371
/**
372
 * Implements callback inplace_editable() allowing to edit values in-place
373
 *
374
 * @param string $itemtype
375
 * @param int $itemid
376
 * @param mixed $newvalue
377
 * @return \core\output\inplace_editable
378
 */
379
function format_buttons_inplace_editable($itemtype, $itemid, $newvalue) {
380
    global $DB, $CFG;
381
 
382
    require_once($CFG->dirroot . '/course/lib.php');
383
 
384
    if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
385
        $section = $DB->get_record_sql(
386
            'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?',
387
            array($itemid, 'buttons'),
388
            MUST_EXIST
389
        );
390
        return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
391
    }
392
    return null;
393
}