Skip to content

Commit b0da86e

Browse files
Frederic Massartdanpoltawski
authored andcommitted
MDL-55496 output: Make paging_bar a templatable
Part of MDL-55071
1 parent 270dd87 commit b0da86e

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

lib/outputcomponents.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ public function __construct($text = null) {
24172417
* @package core
24182418
* @category output
24192419
*/
2420-
class paging_bar implements renderable {
2420+
class paging_bar implements renderable, templatable {
24212421

24222422
/**
24232423
* @var int The maximum number of pagelinks to display.
@@ -2569,6 +2569,82 @@ public function prepare(renderer_base $output, moodle_page $page, $target) {
25692569
}
25702570
}
25712571
}
2572+
2573+
/**
2574+
* Export for template.
2575+
*
2576+
* @param renderer_base $output The renderer.
2577+
* @return stdClass
2578+
*/
2579+
public function export_for_template(renderer_base $output) {
2580+
$data = new stdClass();
2581+
$data->previous = null;
2582+
$data->next = null;
2583+
$data->first = null;
2584+
$data->last = null;
2585+
$data->label = get_string('page');
2586+
$data->pages = [];
2587+
$data->haspages = $this->totalcount > $this->perpage;
2588+
2589+
if (!$data->haspages) {
2590+
return $data;
2591+
}
2592+
2593+
if ($this->page > 0) {
2594+
$data->previous = [
2595+
'page' => $this->page - 1,
2596+
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page - 1]))->out(false)
2597+
];
2598+
}
2599+
2600+
$currpage = 0;
2601+
if ($this->page > round(($this->maxdisplay / 3) * 2)) {
2602+
$currpage = $this->page - round($this->maxdisplay / 3);
2603+
$data->first = [
2604+
'page' => 1,
2605+
'url' => (new moodle_url($this->baseurl, [$this->pagevar => 0]))->out(false)
2606+
];
2607+
}
2608+
2609+
$lastpage = 1;
2610+
if ($this->perpage > 0) {
2611+
$lastpage = ceil($this->totalcount / $this->perpage);
2612+
}
2613+
2614+
$displaycount = 0;
2615+
$displaypage = 0;
2616+
while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
2617+
$displaypage = $currpage + 1;
2618+
2619+
$iscurrent = $this->page == $currpage;
2620+
$link = new moodle_url($this->baseurl, [$this->pagevar => $currpage]);
2621+
2622+
$data->pages[] = [
2623+
'page' => $displaypage,
2624+
'active' => $iscurrent,
2625+
'url' => $iscurrent ? null : $link->out(false)
2626+
];
2627+
2628+
$displaycount++;
2629+
$currpage++;
2630+
}
2631+
2632+
if ($currpage < $lastpage) {
2633+
$data->last = [
2634+
'page' => $lastpage,
2635+
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $lastpage - 1]))->out(false)
2636+
];
2637+
}
2638+
2639+
if ($this->page + 1 != $lastpage) {
2640+
$data->next = [
2641+
'page' => $this->page + 1,
2642+
'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page + 1]))->out(false)
2643+
];
2644+
}
2645+
2646+
return $data;
2647+
}
25722648
}
25732649

25742650
/**

theme/noname/classes/output/core_renderer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use action_menu;
3030
use help_icon;
3131
use single_select;
32+
use paging_bar;
3233

3334
defined('MOODLE_INTERNAL') || die;
3435

@@ -284,4 +285,14 @@ protected function render_single_select(single_select $select) {
284285
return $this->render_from_template('core/single_select', $select->export_for_template($this));
285286
}
286287

288+
/**
289+
* Renders a paging bar.
290+
*
291+
* @param paging_bar $pagingbar The object.
292+
* @return string HTML
293+
*/
294+
protected function render_paging_bar(paging_bar $pagingbar) {
295+
return $this->render_from_template('core/paging_bar', $pagingbar->export_for_template($this));
296+
}
297+
287298
}

theme/noname/lang/en/theme_noname.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$string['choosereadme'] = 'Hey, change me!';
28+
$string['currentinparentheses'] = '(current)';
2829
$string['pluginname'] = 'NONAME';
2930
$string['region-side-post'] = 'Right';
3031
$string['region-side-pre'] = 'Left';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{{#haspages}}
2+
<nav aria-label="{{label}}">
3+
<ul class="pagination">
4+
{{#previous}}
5+
<li class="page-item">
6+
<a href="{{url}}" class="page-link">
7+
<span aria-hidden="true">&laquo;</span>
8+
<span class="sr-only">{{#str}}previous{{/str}}</span>
9+
</a>
10+
</li>
11+
{{/previous}}
12+
{{#first}}
13+
<li class="page-item">
14+
<a href="{{url}}" class="page-link">{{page}}</a>
15+
</li>
16+
<li class="page-item disabled">
17+
<span class="page-link">&hellip;</a>
18+
</li>
19+
{{/first}}
20+
{{#pages}}
21+
<li class="page-item {{#active}}active{{/active}}">
22+
<a href="{{url}}" class="page-link">
23+
{{page}}
24+
{{#active}}
25+
<span class="sr-only">{{#str}}currentinparentheses, theme_noname{{/str}}</span>
26+
{{/active}}
27+
</a>
28+
</li>
29+
{{/pages}}
30+
{{#last}}
31+
<li class="page-item disabled">
32+
<span class="page-link">&hellip;</a>
33+
</li>
34+
<li class="page-item">
35+
<a href="{{url}}" class="page-link">{{page}}</a>
36+
</li>
37+
{{/last}}
38+
{{#next}}
39+
<li class="page-item">
40+
<a href="{{url}}" class="page-link">
41+
<span aria-hidden="true">&raquo;</span>
42+
<span class="sr-only">{{#str}}next{{/str}}</span>
43+
</a>
44+
</li>
45+
{{/next}}
46+
</ul>
47+
</nav>
48+
{{/haspages}}

0 commit comments

Comments
 (0)