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 core\exception\coding_exception;
|
|
|
20 |
use moodle_page;
|
|
|
21 |
use moodle_url;
|
|
|
22 |
use stdClass;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Component representing a paging bar.
|
|
|
26 |
*
|
|
|
27 |
* @copyright 2009 Nicolas Connault
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @since Moodle 2.0
|
|
|
30 |
* @package core
|
|
|
31 |
* @category output
|
|
|
32 |
*/
|
|
|
33 |
class paging_bar implements renderable, templatable {
|
|
|
34 |
/**
|
|
|
35 |
* @var int The maximum number of pagelinks to display.
|
|
|
36 |
*/
|
|
|
37 |
public $maxdisplay = 18;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @var int The total number of entries to be pages through..
|
|
|
41 |
*/
|
|
|
42 |
public $totalcount;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* @var int The page you are currently viewing.
|
|
|
46 |
*/
|
|
|
47 |
public $page;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* @var int The number of entries that should be shown per page.
|
|
|
51 |
*/
|
|
|
52 |
public $perpage;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar,
|
|
|
56 |
* an equals sign and the page number.
|
|
|
57 |
* If this is a moodle_url object then the pagevar param will be replaced by
|
|
|
58 |
* the page no, for each page.
|
|
|
59 |
*/
|
|
|
60 |
public $baseurl;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @var string This is the variable name that you use for the pagenumber in your
|
|
|
64 |
* code (ie. 'tablepage', 'blogpage', etc)
|
|
|
65 |
*/
|
|
|
66 |
public $pagevar;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* @var string A HTML link representing the "previous" page.
|
|
|
70 |
*/
|
|
|
71 |
public $previouslink = null;
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @var string A HTML link representing the "next" page.
|
|
|
75 |
*/
|
|
|
76 |
public $nextlink = null;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* @var string A HTML link representing the first page.
|
|
|
80 |
*/
|
|
|
81 |
public $firstlink = null;
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* @var string A HTML link representing the last page.
|
|
|
85 |
*/
|
|
|
86 |
public $lastlink = null;
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @var array An array of strings. One of them is just a string: the current page
|
|
|
90 |
*/
|
|
|
91 |
public $pagelinks = [];
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Constructor paging_bar with only the required params.
|
|
|
95 |
*
|
|
|
96 |
* @param int $totalcount The total number of entries available to be paged through
|
|
|
97 |
* @param int $page The page you are currently viewing
|
|
|
98 |
* @param int $perpage The number of entries that should be shown per page
|
|
|
99 |
* @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
|
|
|
100 |
* @param string $pagevar name of page parameter that holds the page number
|
|
|
101 |
*/
|
|
|
102 |
public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
|
|
|
103 |
$this->totalcount = $totalcount;
|
|
|
104 |
$this->page = $page;
|
|
|
105 |
$this->perpage = $perpage;
|
|
|
106 |
$this->baseurl = $baseurl;
|
|
|
107 |
$this->pagevar = $pagevar;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Prepares the paging bar for output.
|
|
|
112 |
*
|
|
|
113 |
* This method validates the arguments set up for the paging bar and then
|
|
|
114 |
* produces fragments of HTML to assist display later on.
|
|
|
115 |
*
|
|
|
116 |
* @param renderer_base $output
|
|
|
117 |
* @param moodle_page $page
|
|
|
118 |
* @param string $target
|
|
|
119 |
* @throws coding_exception
|
|
|
120 |
*/
|
|
|
121 |
public function prepare(renderer_base $output, moodle_page $page, $target) {
|
|
|
122 |
if (!isset($this->totalcount) || is_null($this->totalcount)) {
|
|
|
123 |
throw new coding_exception('paging_bar requires a totalcount value.');
|
|
|
124 |
}
|
|
|
125 |
if (!isset($this->page) || is_null($this->page)) {
|
|
|
126 |
throw new coding_exception('paging_bar requires a page value.');
|
|
|
127 |
}
|
|
|
128 |
if (empty($this->perpage)) {
|
|
|
129 |
throw new coding_exception('paging_bar requires a perpage value.');
|
|
|
130 |
}
|
|
|
131 |
if (empty($this->baseurl)) {
|
|
|
132 |
throw new coding_exception('paging_bar requires a baseurl value.');
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if ($this->totalcount > $this->perpage) {
|
|
|
136 |
$pagenum = $this->page - 1;
|
|
|
137 |
|
|
|
138 |
if ($this->page > 0) {
|
|
|
139 |
$this->previouslink = html_writer::link(
|
|
|
140 |
new moodle_url($this->baseurl, [$this->pagevar => $pagenum]),
|
|
|
141 |
get_string('previous'),
|
|
|
142 |
['class' => 'previous'],
|
|
|
143 |
);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
if ($this->perpage > 0) {
|
|
|
147 |
$lastpage = ceil($this->totalcount / $this->perpage);
|
|
|
148 |
} else {
|
|
|
149 |
$lastpage = 1;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
if ($this->page > round(($this->maxdisplay / 3) * 2)) {
|
|
|
153 |
$currpage = $this->page - round($this->maxdisplay / 3);
|
|
|
154 |
|
|
|
155 |
$this->firstlink = html_writer::link(
|
|
|
156 |
new moodle_url($this->baseurl, [$this->pagevar => 0]),
|
|
|
157 |
'1',
|
|
|
158 |
['class' => 'first'],
|
|
|
159 |
);
|
|
|
160 |
} else {
|
|
|
161 |
$currpage = 0;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
$displaycount = $displaypage = 0;
|
|
|
165 |
|
|
|
166 |
while ($displaycount < $this->maxdisplay && $currpage < $lastpage) {
|
|
|
167 |
$displaypage = $currpage + 1;
|
|
|
168 |
|
|
|
169 |
if ($this->page == $currpage) {
|
|
|
170 |
$this->pagelinks[] = html_writer::span($displaypage, 'current-page');
|
|
|
171 |
} else {
|
|
|
172 |
$pagelink = html_writer::link(new moodle_url($this->baseurl, [$this->pagevar => $currpage]), $displaypage);
|
|
|
173 |
$this->pagelinks[] = $pagelink;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
$displaycount++;
|
|
|
177 |
$currpage++;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
if ($currpage < $lastpage) {
|
|
|
181 |
$lastpageactual = $lastpage - 1;
|
|
|
182 |
$this->lastlink = html_writer::link(
|
|
|
183 |
new moodle_url($this->baseurl, [$this->pagevar => $lastpageactual]),
|
|
|
184 |
$lastpage,
|
|
|
185 |
['class' => 'last'],
|
|
|
186 |
);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
$pagenum = $this->page + 1;
|
|
|
190 |
|
|
|
191 |
if ($pagenum != $lastpage) {
|
|
|
192 |
$this->nextlink = html_writer::link(
|
|
|
193 |
new moodle_url($this->baseurl, [$this->pagevar => $pagenum]),
|
|
|
194 |
get_string('next'),
|
|
|
195 |
['class' => 'next'],
|
|
|
196 |
);
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Export for template.
|
|
|
203 |
*
|
|
|
204 |
* @param renderer_base $output The renderer.
|
|
|
205 |
* @return stdClass
|
|
|
206 |
*/
|
|
|
207 |
public function export_for_template(renderer_base $output) {
|
|
|
208 |
$data = new stdClass();
|
|
|
209 |
$data->previous = null;
|
|
|
210 |
$data->next = null;
|
|
|
211 |
$data->first = null;
|
|
|
212 |
$data->last = null;
|
|
|
213 |
$data->label = get_string('page');
|
|
|
214 |
$data->pages = [];
|
|
|
215 |
$data->haspages = $this->totalcount > $this->perpage;
|
|
|
216 |
$data->pagesize = $this->perpage;
|
|
|
217 |
|
|
|
218 |
if (!$data->haspages) {
|
|
|
219 |
return $data;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
if ($this->page > 0) {
|
|
|
223 |
$data->previous = [
|
|
|
224 |
'page' => $this->page,
|
|
|
225 |
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page - 1]))->out(false),
|
|
|
226 |
];
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
$currpage = 0;
|
|
|
230 |
if ($this->page > round(($this->maxdisplay / 3) * 2)) {
|
|
|
231 |
$currpage = $this->page - round($this->maxdisplay / 3);
|
|
|
232 |
$data->first = [
|
|
|
233 |
'page' => 1,
|
|
|
234 |
'url' => (new moodle_url($this->baseurl, [$this->pagevar => 0]))->out(false),
|
|
|
235 |
];
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
$lastpage = 1;
|
|
|
239 |
if ($this->perpage > 0) {
|
|
|
240 |
$lastpage = ceil($this->totalcount / $this->perpage);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
$displaycount = 0;
|
|
|
244 |
$displaypage = 0;
|
|
|
245 |
while ($displaycount < $this->maxdisplay && $currpage < $lastpage) {
|
|
|
246 |
$displaypage = $currpage + 1;
|
|
|
247 |
|
|
|
248 |
$iscurrent = $this->page == $currpage;
|
|
|
249 |
$link = new moodle_url($this->baseurl, [$this->pagevar => $currpage]);
|
|
|
250 |
|
|
|
251 |
$data->pages[] = [
|
|
|
252 |
'page' => $displaypage,
|
|
|
253 |
'active' => $iscurrent,
|
|
|
254 |
'url' => $iscurrent ? null : $link->out(false),
|
|
|
255 |
];
|
|
|
256 |
|
|
|
257 |
$displaycount++;
|
|
|
258 |
$currpage++;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
if ($currpage < $lastpage) {
|
|
|
262 |
$data->last = [
|
|
|
263 |
'page' => $lastpage,
|
|
|
264 |
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $lastpage - 1]))->out(false),
|
|
|
265 |
];
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
if ($this->page + 1 != $lastpage) {
|
|
|
269 |
$data->next = [
|
|
|
270 |
'page' => $this->page + 2,
|
|
|
271 |
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page + 1]))->out(false),
|
|
|
272 |
];
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return $data;
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
// Alias this class to the old name.
|
|
|
280 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
281 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
282 |
class_alias(paging_bar::class, \paging_bar::class);
|