Example #1
0
 /**
  * @see lib/moodle_html_component#prepare()
  * @return void
  */
 public function prepare()
 {
     if (empty($this->totalcount)) {
         throw new coding_exception('moodle_paging_bar requires a totalcount value.');
     }
     if (!isset($this->page) || is_null($this->page)) {
         throw new coding_exception('moodle_paging_bar requires a page value.');
     }
     if (empty($this->perpage)) {
         throw new coding_exception('moodle_paging_bar requires a perpage value.');
     }
     if (empty($this->baseurl)) {
         throw new coding_exception('moodle_paging_bar requires a baseurl value.');
     }
     if (!$this->baseurl instanceof moodle_url) {
         $this->baseurl = new moodle_url($this->baseurl);
     }
     if ($this->totalcount > $this->perpage) {
         $pagenum = $this->page - 1;
         if ($this->page > 0) {
             $this->previouslink = new html_link();
             $this->previouslink->add_class('previous');
             $this->previouslink->url = clone $this->baseurl;
             $this->previouslink->url->param($this->pagevar, $pagenum);
             $this->previouslink->text = get_string('previous');
         }
         if ($this->perpage > 0) {
             $lastpage = ceil($this->totalcount / $this->perpage);
         } else {
             $lastpage = 1;
         }
         if ($this->page > 15) {
             $startpage = $this->page - 10;
             $this->firstlink = new html_link();
             $this->firstlink->url = clone $this->baseurl;
             $this->firstlink->url->param($this->pagevar, 0);
             $this->firstlink->text = 1;
             $this->firstlink->add_class('first');
         } else {
             $startpage = 0;
         }
         $currpage = $startpage;
         $displaycount = $displaypage = 0;
         while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
             $displaypage = $currpage + 1;
             if ($this->page == $currpage && empty($this->nocurr)) {
                 $this->pagelinks[] = $displaypage;
             } else {
                 $pagelink = new html_link();
                 $pagelink->url = clone $this->baseurl;
                 $pagelink->url->param($this->pagevar, $currpage);
                 $pagelink->text = $displaypage;
                 $this->pagelinks[] = $pagelink;
             }
             $displaycount++;
             $currpage++;
         }
         if ($currpage < $lastpage) {
             $lastpageactual = $lastpage - 1;
             $this->lastlink = new html_link();
             $this->lastlink->url = clone $this->baseurl;
             $this->lastlink->url->param($this->pagevar, $lastpageactual);
             $this->lastlink->text = $lastpage;
             $this->lastlink->add_class('last');
         }
         $pagenum = $this->page + 1;
         if ($pagenum != $displaypage) {
             $this->nextlink = new html_link();
             $this->nextlink->url = clone $this->baseurl;
             $this->nextlink->url->param($this->pagevar, $pagenum);
             $this->nextlink->text = get_string('next');
             $this->nextlink->add_class('next');
         }
     }
 }