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/renderer.php');
29
 
30
/**
31
 * Format_buttons_renderer
32
 *
33
 * @package    format_buttons
34
 * @author     Rodrigo Brandão <https://www.linkedin.com/in/brandaorodrigo>
35
 * @copyright  2020 Rodrigo Brandão <rodrigo.brandao.contato@gmail.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class format_buttons_renderer extends format_topics_renderer
39
{
40
 
41
    /**
42
     * Get_button_section
43
     *
44
     * @param stdclass $course
45
     * @param string $name
46
     * @return string
47
     */
48
    protected function get_color_config($course, $name) {
49
        $return = false;
50
        if (isset($course->{$name})) {
51
            $color = str_replace('#', '', $course->{$name});
52
            $color = substr($color, 0, 6);
53
            if (preg_match('/^#?[a-f0-9]{6}$/i', $color)) {
54
                $return = '#'.$color;
55
            }
56
        }
57
        return $return;
58
    }
59
 
60
    /**
61
     * Number_to_roman
62
     *
63
     * @param integer $number
64
     * @return string
65
     */
66
    protected function number_to_roman($number) {
67
        $number = intval($number);
68
        $return = '';
69
        $romanarray = [
70
            'M' => 1000,
71
            'CM' => 900,
72
            'D' => 500,
73
            'CD' => 400,
74
            'C' => 100,
75
            'XC' => 90,
76
            'L' => 50,
77
            'XL' => 40,
78
            'X' => 10,
79
            'IX' => 9,
80
            'V' => 5,
81
            'IV' => 4,
82
            'I' => 1
83
        ];
84
        foreach ($romanarray as $roman => $value) {
85
            $matches = intval($number / $value);
86
            $return .= str_repeat($roman, $matches);
87
            $number = $number % $value;
88
        }
89
        return $return;
90
    }
91
 
92
    /**
93
     * Number_to_alphabet
94
     *
95
     * @param integer $number
96
     * @return string
97
     */
98
    protected function number_to_alphabet($number) {
99
        $number = $number - 1;
100
        $alphabet = range("A", "Z");
101
        if ($number <= 25) {
102
            return $alphabet[$number];
103
        } else if ($number > 25) {
104
            $dividend = ($number + 1);
105
            $alpha = '';
106
            while ($dividend > 0) {
107
                $modulo = ($dividend - 1) % 26;
108
                $alpha = $alphabet[$modulo] . $alpha;
109
                $dividend = floor((($dividend - $modulo) / 26));
110
            }
111
            return $alpha;
112
        }
113
    }
114
 
115
    /**
116
     * Get_button_section
117
     *
118
     * @param stdclass $course
119
     * @param string $sectionvisible
120
     * @return string
121
     */
122
    protected function get_button_section($course, $sectionvisible) {
123
        global $PAGE;
124
        $html = '';
125
        $css = '';
126
        if ($colorcurrent = $this->get_color_config($course, 'colorcurrent')) {
127
            $css .=
128
            '#buttonsectioncontainer .buttonsection.current {
129
                background: ' . $colorcurrent . ';
130
            }
131
            ';
132
        }
133
        if ($colorvisible = $this->get_color_config($course, 'colorvisible')) {
134
            $css .=
135
            '#buttonsectioncontainer .buttonsection.sectionvisible {
136
                background: ' . $colorvisible . ';
137
            }
138
            ';
139
        }
140
        if ($css) {
141
            $html .= html_writer::tag('style', $css);
142
        }
143
        $withoutdivisor = true;
144
        for ($k = 1; $k <= 12; $k++) {
145
            if ($course->{'divisor' . $k}) {
146
                $withoutdivisor = false;
147
            }
148
        }
149
        if ($withoutdivisor) {
150
            $course->divisor1 = 999;
151
        }
152
        $divisorshow = false;
153
        $count = 1;
154
        $currentdivisor = 1;
155
        $modinfo = get_fast_modinfo($course);
156
        $inline = '';
157
        foreach ($modinfo->get_section_info_all() as $section => $thissection) {
158
            if ($section == 0) {
159
                continue;
160
            }
161
            if ($section > $course->numsections) {
162
                continue;
163
            }
164
            if ($course->hiddensections && !(int)$thissection->visible) {
165
                continue;
166
            }
167
            if (isset($course->{'divisor' . $currentdivisor}) &&
168
                $count > $course->{'divisor' . $currentdivisor}) {
169
                $currentdivisor++;
170
                $count = 1;
171
            }
172
            if (isset($course->{'divisor' . $currentdivisor}) &&
173
                $course->{'divisor' . $currentdivisor} != 0 &&
174
                !isset($divisorshow[$currentdivisor])) {
175
                $currentdivisorhtml = format_string($course->{'divisortext' . $currentdivisor});
176
                $currentdivisorhtml = str_replace('[br]', '<br>', $currentdivisorhtml);
177
                $currentdivisorhtml = html_writer::tag('div', $currentdivisorhtml, ['class' => 'divisortext']);
178
                if ($course->inlinesections) {
179
                    $inline = 'inlinebuttonsections';
180
                }
181
                $html .= html_writer::tag('div', $currentdivisorhtml, ['class' => "divisorsection $inline"]);
182
                $divisorshow[$currentdivisor] = true;
183
            }
184
            $id = 'buttonsection-' . $section;
185
            if ($course->sequential) {
186
                $name = $section;
187
            } else {
188
                if (isset($course->{'divisor' . $currentdivisor}) &&
189
                $course->{'divisor' . $currentdivisor} == 1) {
190
                    $name = '&bull;&bull;&bull;';
191
                } else {
192
                    $name = $count;
193
                }
194
            }
195
            if ($course->sectiontype == 'alphabet' && is_numeric($name)) {
196
                $name = $this->number_to_alphabet($name);
197
            }
198
            if ($course->sectiontype == 'roman' && is_numeric($name)) {
199
                $name = $this->number_to_roman($name);
200
            }
201
            $class = 'buttonsection';
202
            $onclick = 'M.format_buttons.show(' . $section . ',' . $course->id . ')';
203
            if (!$thissection->available &&
204
                !empty($thissection->availableinfo)) {
205
                $class .= ' sectionhidden';
206
            } else if (!$thissection->uservisible || !$thissection->visible) {
207
                $class .= ' sectionhidden';
208
                $onclick = false;
209
            }
210
            if ($course->marker == $section) {
211
                $class .= ' current';
212
            }
213
            if ($sectionvisible == $section) {
214
                $class .= ' sectionvisible';
215
            }
216
            if ($PAGE->user_is_editing()) {
217
                $onclick = false;
218
            }
219
            $html .= html_writer::tag('div', $name, ['id' => $id, 'class' => $class, 'onclick' => $onclick]);
220
            $count++;
221
        }
222
        $html = html_writer::tag('div', $html, ['id' => 'buttonsectioncontainer', 'class' => $course->buttonstyle]);
223
        if ($PAGE->user_is_editing()) {
224
            $html .= html_writer::tag('div', get_string('editing', 'format_buttons'), ['class' => 'alert alert-warning alert-block fade in']);
225
        }
226
        return $html;
227
    }
228
 
229
    /**
230
     * Start_section_list
231
     *
232
     * @return string
233
     */
234
    protected function start_section_list() {
235
        return html_writer::start_tag('ul', ['class' => 'buttons']);
236
    }
237
 
238
    /**
239
     * Section_header
240
     *
241
     * @param stdclass $section
242
     * @param stdclass $course
243
     * @param bool $onsectionpage
244
     * @param int $sectionreturn
245
     * @return string
246
     */
247
    protected function section_header($section, $course, $onsectionpage, $sectionreturn = null) {
248
        global $PAGE;
249
 
250
        $o = '';
251
        $currenttext = '';
252
        $sectionstyle = '';
253
 
254
        if ($section->section != 0) {
255
            // Only in the non-general sections.
256
            if (!$section->visible) {
257
                $sectionstyle = ' hidden';
258
            }
259
            if (course_get_format($course)->is_section_current($section)) {
260
                $sectionstyle = ' current';
261
            }
262
        }
263
 
264
        $o.= html_writer::start_tag('li', array('id' => 'section-'.$section->section,
265
             'class' => 'section main clearfix'.$sectionstyle, 'role'=>'region',
266
             'aria-label'=> get_section_name($course, $section)));
267
 
268
        // Create a span that contains the section title to be used to create the keyboard section move menu.
269
        $o .= html_writer::tag('span', get_section_name($course, $section), array('class' => 'hidden sectionname'));
270
 
271
        $leftcontent = $this->section_left_content($section, $course, $onsectionpage);
272
        $o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
273
 
274
        $rightcontent = $this->section_right_content($section, $course, $onsectionpage);
275
        $o.= html_writer::tag('div', $rightcontent, array('class' => 'right side'));
276
        $o.= html_writer::start_tag('div', array('class' => 'content'));
277
 
278
        // When not on a section page, we display the section titles except the general section if null
279
        $hasnamenotsecpg = (!$onsectionpage && ($section->section != 0 || !is_null($section->name)));
280
 
281
        // When on a section page, we only display the general section title, if title is not the default one
282
        $hasnamesecpg = ($onsectionpage && ($section->section == 0 && !is_null($section->name)));
283
 
284
        $classes = ' accesshide';
285
        if ($hasnamenotsecpg || $hasnamesecpg) {
286
            $classes = '';
287
        }
288
        $sectionname = html_writer::tag('span', $this->section_title($section, $course));
289
 
290
        // Button format - ini
291
        if ($course->showdefaultsectionname) {
292
            $o.= $this->output->heading($sectionname, 3, 'sectionname' . $classes);
293
        }
294
        // Button format - end
295
 
296
        $o .= $this->section_availability($section);
297
 
298
        $o .= html_writer::start_tag('div', array('class' => 'summary'));
299
        if ($section->uservisible || $section->visible) {
300
            // Show summary if section is available or has availability restriction information.
301
            // Do not show summary if section is hidden but we still display it because of course setting
302
            // "Hidden sections are shown in collapsed form".
303
            $o .= $this->format_summary_text($section);
304
        }
305
        $o .= html_writer::end_tag('div');
306
 
307
        return $o;
308
    }
309
 
310
    /**
311
     * Print_multiple_section_page
312
     *
313
     * @param stdclass $course
314
     * @param array $sections (argument not used)
315
     * @param array $mods (argument not used)
316
     * @param array $modnames (argument not used)
317
     * @param array $modnamesused (argument not used)
318
     */
319
    public function print_multiple_section_page($course, $sections, $mods, $modnames, $modnamesused) {
320
        global $PAGE;
321
 
322
        $modinfo = get_fast_modinfo($course);
323
        $course = course_get_format($course)->get_course();
324
 
325
        $context = context_course::instance($course->id);
326
        // Title with completion help icon.
327
        $completioninfo = new completion_info($course);
328
 
329
        // buttons format - ini
330
        if (isset($_COOKIE['sectionvisible_' . $course->id])) {
331
            $sectionvisible = $_COOKIE['sectionvisible_' . $course->id];
332
        } else if ($course->marker > 0) {
333
            $sectionvisible = $course->marker;
334
        } else {
335
            $sectionvisible = 1;
336
        }
337
        $htmlsection = false;
338
        foreach ($modinfo->get_section_info_all() as $section => $thissection) {
339
            $htmlsection[$section] = '';
340
            if ($section == 0) {
341
                $section0 = $thissection;
342
                continue;
343
            }
344
            if ($section > $course->numsections) {
345
                continue;
346
            }
347
            /* If is not editing verify the rules to display the sections */
348
            if (!$PAGE->user_is_editing()) {
349
                if ($course->hiddensections && !(int)$thissection->visible) {
350
                    continue;
351
                }
352
                if (!$thissection->available && !empty($thissection->availableinfo)) {
353
                    $htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
354
                    continue;
355
                }
356
                if (!$thissection->uservisible || !$thissection->visible) {
357
                    $htmlsection[$section] .= $this->section_hidden($section, $course->id);
358
                    continue;
359
                }
360
            }
361
            $htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
362
            if ($thissection->uservisible) {
363
                $htmlsection[$section] .= $this->courserenderer->course_section_cm_list($course, $thissection, 0);
364
                $htmlsection[$section] .= $this->courserenderer->course_section_add_cm_control($course, $section, 0);
365
            }
366
            $htmlsection[$section] .= $this->section_footer();
367
        }
368
        if ($section0->summary || !empty($modinfo->sections[0]) || $PAGE->user_is_editing()) {
369
            $htmlsection0 = $this->section_header($section0, $course, false, 0);
370
            $htmlsection0 .= $this->courserenderer->course_section_cm_list($course, $section0, 0);
371
            $htmlsection0 .= $this->courserenderer->course_section_add_cm_control($course, 0, 0);
372
            $htmlsection0 .= $this->section_footer();
373
        }
374
        echo $completioninfo->display_help_icon();
375
        echo $this->output->heading($this->page_title(), 2, 'accesshide');
376
        echo $this->course_activity_clipboard($course, 0);
377
        echo $this->start_section_list();
378
        if ($course->sectionposition == 0 and isset($htmlsection0)) {
379
            echo html_writer::tag('span', $htmlsection0, ['class' => 'above']);
380
        }
381
        echo $this->get_button_section($course, $sectionvisible);
382
        foreach ($htmlsection as $current) {
383
            echo $current;
384
        }
385
        if ($course->sectionposition == 1 and isset($htmlsection0)) {
386
            echo html_writer::tag('span', $htmlsection0, ['class' => 'below']);
387
        }
388
        if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
389
            foreach ($modinfo->get_section_info_all() as $section => $thissection) {
390
                if ($section <= $course->numsections or empty($modinfo->sections[$section])) {
391
                    continue;
392
                }
393
                echo $this->stealth_section_header($section);
394
                echo $this->courserenderer->course_section_cm_list($course, $thissection, 0);
395
                echo $this->stealth_section_footer();
396
            }
397
            echo $this->end_section_list();
398
            echo html_writer::start_tag('div', ['id' => 'changenumsections', 'class' => 'mdl-right']);
399
            $straddsection = get_string('increasesections', 'moodle');
400
            $url = new moodle_url('/course/changenumsections.php', ['courseid' => $course->id,
401
                'increase' => true, 'sesskey' => sesskey()]);
402
            $icon = $this->output->pix_icon('t/switch_plus', $straddsection);
403
            echo html_writer::link($url, $icon.get_accesshide($straddsection), ['class' => 'increase-sections']);
404
            if ($course->numsections > 0) {
405
                $strremovesection = get_string('reducesections', 'moodle');
406
                $url = new moodle_url('/course/changenumsections.php', ['courseid' => $course->id,
407
                    'increase' => false, 'sesskey' => sesskey()]);
408
                $icon = $this->output->pix_icon('t/switch_minus', $strremovesection);
409
                echo html_writer::link(
410
                    $url,
411
                    $icon.get_accesshide($strremovesection),
412
                    ['class' => 'reduce-sections']
413
                );
414
            }
415
            echo html_writer::end_tag('div');
416
        } else {
417
            echo $this->end_section_list();
418
        }
419
        if (!$PAGE->user_is_editing()) {
420
            $PAGE->requires->js_init_call('M.format_buttons.init', [$course->numsections, $sectionvisible, $course->id]);
421
        }
422
        // Button format - end
423
    }
424
}