MDL-51360 core_grades: Improve documentation of grade_get_grades().
[moodle.git] / lib / adodb / adodb-pager.inc.php
blobcfe981d32c8ca6dc92261452374ed154eebacf4b
1 <?php
2 /**
3 * Recordset pagination with First/Prev/Next/Last links
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
7 * @package ADOdb
8 * @link https://adodb.org Project's web site and documentation
9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
13 * any later version. This means you can use it in proprietary products.
14 * See the LICENSE.md file distributed with this source code for details.
15 * @license BSD-3-Clause
16 * @license LGPL-2.1-or-later
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
22 class ADODB_Pager {
23 var $id; // unique id for pager (defaults to 'adodb')
24 var $db; // ADODB connection object
25 var $sql; // sql used
26 var $rs; // recordset generated
27 var $curr_page; // current page number before Render() called, calculated in constructor
28 var $rows; // number of rows per page
29 var $linksPerPage=10; // number of links per page in navigation bar
30 var $showPageLinks;
32 var $gridAttributes = 'width=100% border=1 bgcolor=white';
34 // Localize text strings here
35 var $first = '<code>|&lt;</code>';
36 var $prev = '<code>&lt;&lt;</code>';
37 var $next = '<code>>></code>';
38 var $last = '<code>>|</code>';
39 var $moreLinks = '...';
40 var $startLinks = '...';
41 var $gridHeader = false;
42 var $htmlSpecialChars = true;
43 var $page = 'Page';
44 var $linkSelectedColor = 'red';
45 var $cache = 0; #secs to cache with CachePageExecute()
47 //----------------------------------------------
48 // constructor
50 // $db adodb connection object
51 // $sql sql statement
52 // $id optional id to identify which pager,
53 // if you have multiple on 1 page.
54 // $id should be only be [a-z0-9]*
56 function __construct(&$db,$sql,$id = 'adodb', $showPageLinks = false)
58 global $PHP_SELF;
60 $curr_page = $id.'_curr_page';
61 if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
63 $this->sql = $sql;
64 $this->id = $id;
65 $this->db = $db;
66 $this->showPageLinks = $showPageLinks;
68 $next_page = $id.'_next_page';
70 if (isset($_GET[$next_page])) {
71 $_SESSION[$curr_page] = (integer) $_GET[$next_page];
73 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
75 $this->curr_page = $_SESSION[$curr_page];
79 //---------------------------
80 // Display link to first page
81 function Render_First($anchor=true)
83 global $PHP_SELF;
84 if ($anchor) {
86 <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a> &nbsp;
87 <?php
88 } else {
89 print "$this->first &nbsp; ";
93 //--------------------------
94 // Display link to next page
95 function render_next($anchor=true)
97 global $PHP_SELF;
99 if ($anchor) {
101 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a> &nbsp;
102 <?php
103 } else {
104 print "$this->next &nbsp; ";
108 //------------------
109 // Link to last page
111 // for better performance with large recordsets, you can set
112 // $this->db->pageExecuteCountRows = false, which disables
113 // last page counting.
114 function render_last($anchor=true)
116 global $PHP_SELF;
118 if (!$this->db->pageExecuteCountRows) return;
120 if ($anchor) {
122 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a> &nbsp;
123 <?php
124 } else {
125 print "$this->last &nbsp; ";
129 //---------------------------------------------------
130 // original code by "Pablo Costa" <pablo@cbsp.com.br>
131 function render_pagelinks()
133 global $PHP_SELF;
134 $pages = $this->rs->LastPageNo();
135 $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
136 for($i=1; $i <= $pages; $i+=$linksperpage)
138 if($this->rs->AbsolutePage() >= $i)
140 $start = $i;
143 $numbers = '';
144 $end = $start+$linksperpage-1;
145 $link = $this->id . "_next_page";
146 if($end > $pages) $end = $pages;
149 if ($this->startLinks && $start > 1) {
150 $pos = $start - 1;
151 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
154 for($i=$start; $i <= $end; $i++) {
155 if ($this->rs->AbsolutePage() == $i)
156 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
157 else
158 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
161 if ($this->moreLinks && $end < $pages)
162 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
163 print $numbers . ' &nbsp; ';
165 // Link to previous page
166 function render_prev($anchor=true)
168 global $PHP_SELF;
169 if ($anchor) {
171 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a> &nbsp;
172 <?php
173 } else {
174 print "$this->prev &nbsp; ";
178 //--------------------------------------------------------
179 // Simply rendering of grid. You should override this for
180 // better control over the format of the grid
182 // We use output buffering to keep code clean and readable.
183 function RenderGrid()
185 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
186 include_once(ADODB_DIR.'/tohtml.inc.php');
187 ob_start();
188 $gSQLBlockRows = $this->rows;
189 rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
190 $s = ob_get_contents();
191 ob_end_clean();
192 return $s;
195 //-------------------------------------------------------
196 // Navigation bar
198 // we use output buffering to keep the code easy to read.
199 function RenderNav()
201 ob_start();
202 if (!$this->rs->AtFirstPage()) {
203 $this->Render_First();
204 $this->Render_Prev();
205 } else {
206 $this->Render_First(false);
207 $this->Render_Prev(false);
209 if ($this->showPageLinks){
210 $this->Render_PageLinks();
212 if (!$this->rs->AtLastPage()) {
213 $this->Render_Next();
214 $this->Render_Last();
215 } else {
216 $this->Render_Next(false);
217 $this->Render_Last(false);
219 $s = ob_get_contents();
220 ob_end_clean();
221 return $s;
224 //-------------------
225 // This is the footer
226 function RenderPageCount()
228 if (!$this->db->pageExecuteCountRows) return '';
229 $lastPage = $this->rs->LastPageNo();
230 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
231 if ($this->curr_page > $lastPage) $this->curr_page = 1;
232 return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
235 //-----------------------------------
236 // Call this class to draw everything.
237 function Render($rows=10)
239 global $ADODB_COUNTRECS;
241 $this->rows = $rows;
243 if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL;
245 $savec = $ADODB_COUNTRECS;
246 if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
247 if ($this->cache)
248 $rs = $this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
249 else
250 $rs = $this->db->PageExecute($this->sql,$rows,$this->curr_page);
251 $ADODB_COUNTRECS = $savec;
253 $this->rs = $rs;
254 if (!$rs) {
255 print "<h3>Query failed: $this->sql</h3>";
256 return;
259 if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
260 $header = $this->RenderNav();
261 else
262 $header = "&nbsp;";
264 $grid = $this->RenderGrid();
265 $footer = $this->RenderPageCount();
267 $this->RenderLayout($header,$grid,$footer);
269 $rs->Close();
270 $this->rs = false;
273 //------------------------------------------------------
274 // override this to control overall layout and formatting
275 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
277 echo "<table ".$attributes."><tr><td>",
278 $header,
279 "</td></tr><tr><td>",
280 $grid,
281 "</td></tr><tr><td>",
282 $footer,
283 "</td></tr></table>";