Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core\output;
18
 
19
use moodle_url;
20
use stdClass;
21
 
22
/**
23
 * Component representing initials bar.
24
 *
25
 * @copyright 2017 Ilya Tregubov
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @since Moodle 3.3
28
 * @package core
29
 * @category output
30
 */
31
class initials_bar implements renderable, templatable {
32
    /**
33
     * @var string Currently selected letter.
34
     */
35
    public $current;
36
 
37
    /**
38
     * @var string Class name to add to this initial bar.
39
     */
40
    public $class;
41
 
42
    /**
43
     * @var string The name to put in front of this initial bar.
44
     */
45
    public $title;
46
 
47
    /**
48
     * @var string URL parameter name for this initial.
49
     */
50
    public $urlvar;
51
 
52
    /**
53
     * @var moodle_url URL object.
54
     */
55
    public $url;
56
 
57
    /**
58
     * @var array An array of letters in the alphabet.
59
     */
60
    public $alpha;
61
 
62
    /**
63
     * @var bool Omit links if we are doing a mini render.
64
     */
65
    public $minirender;
66
 
67
    /**
68
     * Constructor initials_bar with only the required params.
69
     *
70
     * @param string $current the currently selected letter.
71
     * @param string $class class name to add to this initial bar.
72
     * @param string $title the name to put in front of this initial bar.
73
     * @param string $urlvar URL parameter name for this initial.
74
     * @param string $url URL object.
75
     * @param array $alpha of letters in the alphabet.
76
     * @param bool $minirender Return a trimmed down view of the initials bar.
77
     */
78
    public function __construct($current, $class, $title, $urlvar, $url, $alpha = null, bool $minirender = false) {
79
        $this->current       = $current;
80
        $this->class    = $class;
81
        $this->title    = $title;
82
        $this->urlvar    = $urlvar;
83
        $this->url    = $url;
84
        $this->alpha    = $alpha;
85
        $this->minirender = $minirender;
86
    }
87
 
88
    /**
89
     * Export for template.
90
     *
91
     * @param renderer_base $output The renderer.
92
     * @return stdClass
93
     */
94
    public function export_for_template(renderer_base $output) {
95
        $data = new stdClass();
96
 
97
        if ($this->alpha == null) {
98
            $this->alpha = explode(',', get_string('alphabet', 'langconfig'));
99
        }
100
 
101
        if ($this->current == 'all') {
102
            $this->current = '';
103
        }
104
 
105
        // We want to find a letter grouping size which suits the language so
106
        // find the largest group size which is less than 15 chars.
107
        // The choice of 15 chars is the largest number of chars that reasonably
108
        // fits on the smallest supported screen size. By always using a max number
109
        // of groups which is a factor of 2, we always get nice wrapping, and the
110
        // last row is always the shortest.
111
        $groupsize = count($this->alpha);
112
        $groups = 1;
113
        while ($groupsize > 15) {
114
            $groups *= 2;
115
            $groupsize = ceil(count($this->alpha) / $groups);
116
        }
117
 
118
        $groupsizelimit = 0;
119
        $groupnumber = 0;
120
        foreach ($this->alpha as $letter) {
121
            if ($groupsizelimit++ > 0 && $groupsizelimit % $groupsize == 1) {
122
                $groupnumber++;
123
            }
124
            $groupletter = new stdClass();
125
            $groupletter->name = $letter;
126
            if (!$this->minirender) {
127
                $groupletter->url = $this->url->out(false, [$this->urlvar => $letter]);
128
            } else {
129
                $groupletter->input = $letter;
130
            }
131
            if ($letter == $this->current) {
132
                $groupletter->selected = $this->current;
133
            }
134
            if (!isset($data->group[$groupnumber])) {
135
                $data->group[$groupnumber] = new stdClass();
136
            }
137
            $data->group[$groupnumber]->letter[] = $groupletter;
138
        }
139
 
140
        $data->class = $this->class;
141
        $data->title = $this->title;
142
        if (!$this->minirender) {
143
            $data->url = $this->url->out(false, [$this->urlvar => '']);
144
        } else {
145
            $data->input = 'ALL';
146
        }
147
        $data->current = $this->current;
148
        $data->all = get_string('all');
149
 
150
        return $data;
151
    }
152
}
153
 
154
// Alias this class to the old name.
155
// This file will be autoloaded by the legacyclasses autoload system.
156
// In future all uses of this class will be corrected and the legacy references will be removed.
157
class_alias(initials_bar::class, \initials_bar::class);