Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 * Contains the default section selector.
19
 *
20
 * @package   core_courseformat
21
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_courseformat\output\local\content;
26
 
27
use core\output\named_templatable;
28
use core_courseformat\base as course_format;
29
use core_courseformat\output\local\courseformat_named_templatable;
30
use renderable;
1441 ariadna 31
use section_info;
1 efrain 32
use stdClass;
33
use url_select;
34
 
35
/**
36
 * Represents the section selector.
37
 *
38
 * @package   core_courseformat
39
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class sectionselector implements named_templatable, renderable {
43
 
44
    use courseformat_named_templatable;
45
 
1441 ariadna 46
    /** @var string the indenter */
47
    private const INDENTER = '&nbsp;&nbsp;&nbsp;&nbsp;';
1 efrain 48
    /** @var course_format the course format class */
49
    protected $format;
50
    /** @var sectionnavigation the main section navigation class */
51
    protected $navigation;
52
 
1441 ariadna 53
    /** @var array $sectionmenu the sections indexed by url. */
54
    protected $sectionmenu = [];
55
 
1 efrain 56
    /**
57
     * Constructor.
58
     *
59
     * In the current imeplementaiton the seciton selector is almost a variation of the section navigator
60
     * but in the 4.0 this selector will be a kind of dropdown menu. When this happens the construct params
61
     * will change.
62
     *
63
     * @param course_format $format the course format
64
     * @param sectionnavigation $navigation the current section navigation
65
     */
66
    public function __construct(course_format $format, sectionnavigation $navigation) {
67
        $this->format = $format;
68
        $this->navigation = $navigation;
69
    }
70
 
71
    /**
72
     * Export this data so it can be used as the context for a mustache template.
73
     *
1441 ariadna 74
     * @param \renderer_base $output typically, the renderer that's calling this function
1 efrain 75
     * @return stdClass data context for a mustache template
76
     */
77
    public function export_for_template(\renderer_base $output): stdClass {
78
 
79
        $format = $this->format;
80
        $course = $format->get_course();
81
 
82
        $modinfo = $this->format->get_modinfo();
83
 
84
        $data = $this->navigation->export_for_template($output);
85
 
1441 ariadna 86
        $this->sectionmenu[course_get_url($course)->out(false)] = get_string('maincoursepage');
87
 
1 efrain 88
        // Add the section selector.
1441 ariadna 89
        $allsections = $modinfo->get_section_info_all();
90
        $disabledlink = $this->get_section_url($course, $allsections[$data->currentsection]);
91
        $sectionwithchildren = [];
92
        // First get any section with chidren (easier to process later in a regular loop).
93
        foreach ($allsections as $section) {
94
            if (!$section->uservisible) {
95
                unset($allsections[$section->sectionnum]);
96
                continue;
1 efrain 97
            }
1441 ariadna 98
            $sectiondelegated = $section->get_component_instance();
99
            if ($sectiondelegated) {
100
                unset($allsections[$section->sectionnum]);
101
                $parentsection = $sectiondelegated->get_parent_section();
102
                // If the section is delegated we need to get the parent section and add the section to the parent section array.
103
                if ($parentsection) {
104
                    $sectionwithchildren[$parentsection->sectionnum][] = $section;
105
                }
106
            }
1 efrain 107
        }
108
 
1441 ariadna 109
        foreach ($allsections as $section) {
110
            $this->add_section_menu($format, $course, $section);
111
            if (isset($sectionwithchildren[$section->sectionnum])) {
112
                foreach ($sectionwithchildren[$section->sectionnum] as $subsection) {
113
                    $this->add_section_menu($format, $course, $subsection, true);
114
                }
115
            }
116
        }
117
        $select = new url_select(
118
            urls: $this->sectionmenu,
119
            selected: '',
120
            nothing: ['' => get_string('jumpto')],
121
        );
122
        // Disable the current section.
123
        $select->set_option_disabled($disabledlink);
1 efrain 124
        $select->class = 'jumpmenu';
125
        $select->formid = 'sectionmenu';
126
 
127
        $data->selector = $output->render($select);
128
        return $data;
129
    }
1441 ariadna 130
 
131
    /**
132
     * Add a section to the section menu.
133
     *
134
     * @param course_format $format
135
     * @param stdClass $course
136
     * @param section_info $section
137
     * @param bool $indent
138
     */
139
    private function add_section_menu(
140
        course_format $format,
141
        stdClass $course,
142
        section_info $section,
143
        bool $indent = false
144
    ) {
145
        $url = $this->get_section_url($course, $section);
146
        $indentation = $indent ? self::INDENTER : '';
147
        $this->sectionmenu[$url] = $indentation . $format->get_section_name($section);
148
    }
149
 
150
    /**
151
     * Get the section url.
152
     * @param stdClass $course
153
     * @param section_info $section
154
     * @return string
155
     */
156
    private function get_section_url(stdClass $course, section_info $section): string {
157
        return course_get_url($course, (object) $section, ['navigation' => true])->out(false);
158
    }
1 efrain 159
}