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
 * Contains class \core\output\language_menu
19
 *
20
 * @package    core
21
 * @category   output
22
 * @copyright  2021 Adrian Greeve <adrian@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core\output;
27
 
28
/**
29
 * Class for creating the language menu
30
 *
31
 * @package    core
32
 * @category   output
33
 * @copyright  2021 Adrian Greeve <adrian@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
class language_menu implements renderable, templatable {
1 efrain 37
    /** @var \moodle_page $page the moodle page that the languague menu belongs to */
38
    protected $page;
39
 
40
    /** @var string current language code */
41
    protected $currentlang;
42
 
43
    /** @var array localised list of installed translations */
44
    protected $langs;
45
 
46
    /**
47
     * Language menu constructor.
48
     *
49
     * @param \moodle_page $page the moodle page that the languague menu belongs to.
50
     */
51
    public function __construct($page) {
52
        $this->page = $page;
53
        $this->currentlang = \current_language();
54
        $this->langs = \get_string_manager()->get_list_of_translations();
55
    }
56
 
57
    /**
58
     * Determine if the language menu should be shown.
59
     *
60
     * @return bool true if the language menu should be shown.
61
     */
62
    protected function show_language_menu(): bool {
63
        global $CFG;
64
 
65
        if (empty($CFG->langmenu)) {
66
            return false;
67
        }
68
 
1441 ariadna 69
        if ($this->page->course != SITEID && !empty($this->page->course->lang)) {
1 efrain 70
            // Do not show lang menu if language forced.
71
            return false;
72
        }
73
 
74
        if (count($this->langs) < 2) {
75
            return false;
76
        }
77
        return true;
78
    }
79
 
80
    /**
81
     * Export the data.
82
     *
83
     * @param \renderer_base $output
84
     * @return array with the title for the menu and an array of items.
85
     */
86
    public function export_for_template(\renderer_base $output): array {
87
        // Early return if a lang menu does not exists.
88
        if (!$this->show_language_menu()) {
89
            return [];
90
        }
91
 
92
        $nodes = [];
93
        $activelanguage = '';
94
 
95
        // Add the lang picker if needed.
96
        foreach ($this->langs as $langtype => $langname) {
97
            $isactive = $langtype == $this->currentlang;
98
            $attributes = [];
99
            if (!$isactive) {
100
                // Set the lang attribute for languages different from the page's current language.
101
                $attributes[] = [
102
                    'key' => 'lang',
103
                    'value' => get_html_lang_attribute_value($langtype),
104
                ];
105
            }
106
            $node = [
107
                'title' => $langname,
108
                'text' => $langname,
109
                'link' => true,
110
                'isactive' => $isactive,
111
                'url' => $isactive ? new \moodle_url('#') : new \moodle_url($this->page->url, ['lang' => $langtype]),
112
            ];
113
            if (!empty($attributes)) {
114
                $node['attributes'] = $attributes;
115
            }
116
 
117
            $nodes[] = $node;
118
 
119
            if ($isactive) {
120
                $activelanguage = $langname;
121
            }
122
        }
123
 
124
        return [
125
            'title' => $activelanguage,
126
            'items' => $nodes,
127
        ];
128
    }
129
 
130
    /**
131
     * Export the data providing a structure for the core/action_menu template.
132
     *
133
     * @param \renderer_base $output
134
     * @return \stdClass action_menu data export.
135
     */
136
    public function export_for_action_menu(\renderer_base $output): ?\stdClass {
137
        $languagedata = $this->export_for_template($output);
138
        if (empty($languagedata)) {
139
            return null;
140
        }
141
        $langmenu = new \action_menu();
142
        $menuname = \get_string('language');
143
        if (!empty($languagedata['title'])) {
144
            $menuname = $languagedata['title'];
145
        }
146
        $langmenu->set_menu_trigger($menuname);
147
        foreach ($languagedata['items'] as $node) {
148
            $langparam = $node['url']->get_param('lang');
149
            $attributes = [];
150
            if ($langparam) {
151
                $attributes = [
152
                    'data-lang' => $langparam,
153
                    'lang' => get_html_lang_attribute_value($langparam),
154
                ];
155
            }
156
            $lang = new \action_menu_link_secondary($node['url'], null, $node['title'], $attributes);
157
            $langmenu->add($lang);
158
        }
159
        return $langmenu->export_for_template($output);
160
    }
161
 
162
    /**
163
     * Export the data providing a structure for the core/single_select template.
164
     *
165
     * @param \renderer_base $output
166
     * @return \stdClass single_select data export.
167
     */
168
    public function export_for_single_select(\renderer_base $output): ?\stdClass {
169
        if (!$this->show_language_menu()) {
170
            return null;
171
        }
172
        $singleselect = new \single_select($this->page->url, 'lang', $this->langs, $this->currentlang, null);
173
        $singleselect->label = get_accesshide(\get_string('language'));
174
        $singleselect->class = 'langmenu';
175
        return $singleselect->export_for_template($output);
176
    }
177
}