| 1 | 
           efrain | 
           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 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Paged Content exporter.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    core
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           namespace core\external;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           use renderer_base;
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           /**
  | 
        
        
            | 
            | 
           32 | 
            * Paged Content exporter.
  | 
        
        
            | 
            | 
           33 | 
            *
  | 
        
        
            | 
            | 
           34 | 
            * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           35 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           36 | 
            */
  | 
        
        
            | 
            | 
           37 | 
           class paged_content_exporter extends exporter {
  | 
        
        
            | 
            | 
           38 | 
               /** @var int pagesize The number of records to show on each page */
  | 
        
        
            | 
            | 
           39 | 
               private $pagesize;
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /** @var int pagenumber The current page number */
  | 
        
        
            | 
            | 
           42 | 
               private $pagenumber;
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               /** @var int recordcount The total number of records available */
  | 
        
        
            | 
            | 
           45 | 
               private $recordcount;
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
               /** @var callable The callback to use to determine a page URL */
  | 
        
        
            | 
            | 
           48 | 
               private $pageurlcallback;
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
               /**
  | 
        
        
            | 
            | 
           51 | 
                * Constructor.
  | 
        
        
            | 
            | 
           52 | 
                *
  | 
        
        
            | 
            | 
           53 | 
                * @param int $pagesize The number of records to show on each page
  | 
        
        
            | 
            | 
           54 | 
                * @param int $pagenumber The current page number
  | 
        
        
            | 
            | 
           55 | 
                * @param int $recordcount The total number of records available
  | 
        
        
            | 
            | 
           56 | 
                * @param callable $pageurlcallback The callback to use to determine a page URL
  | 
        
        
            | 
            | 
           57 | 
                * @param array $related List of related elements
  | 
        
        
            | 
            | 
           58 | 
                */
  | 
        
        
            | 
            | 
           59 | 
               public function __construct(int $pagesize, int $pagenumber, int $recordcount, callable $pageurlcallback, array $related = []) {
  | 
        
        
            | 
            | 
           60 | 
                   $this->pagesize = $pagesize;
  | 
        
        
            | 
            | 
           61 | 
                   $this->pagenumber = $pagenumber;
  | 
        
        
            | 
            | 
           62 | 
                   $this->recordcount = $recordcount;
  | 
        
        
            | 
            | 
           63 | 
                   $this->pageurlcallback = $pageurlcallback;
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   return parent::__construct([], $related);
  | 
        
        
            | 
            | 
           66 | 
               }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
               /**
  | 
        
        
            | 
            | 
           69 | 
                * Return the list of additional properties.
  | 
        
        
            | 
            | 
           70 | 
                *
  | 
        
        
            | 
            | 
           71 | 
                * @return array
  | 
        
        
            | 
            | 
           72 | 
                */
  | 
        
        
            | 
            | 
           73 | 
               protected static function define_other_properties() {
  | 
        
        
            | 
            | 
           74 | 
                   return [
  | 
        
        
            | 
            | 
           75 | 
                       'itemsperpage' => ['type' => PARAM_INT],
  | 
        
        
            | 
            | 
           76 | 
                       'buttons' => [
  | 
        
        
            | 
            | 
           77 | 
                           'type' => [
  | 
        
        
            | 
            | 
           78 | 
                               'first' => ['type' => PARAM_BOOL],
  | 
        
        
            | 
            | 
           79 | 
                               'previous' => ['type' => PARAM_BOOL],
  | 
        
        
            | 
            | 
           80 | 
                               'next' => ['type' => PARAM_BOOL],
  | 
        
        
            | 
            | 
           81 | 
                               'last' => ['type' => PARAM_BOOL],
  | 
        
        
            | 
            | 
           82 | 
                           ],
  | 
        
        
            | 
            | 
           83 | 
                       ],
  | 
        
        
            | 
            | 
           84 | 
                       'pages' => [
  | 
        
        
            | 
            | 
           85 | 
                           'multiple' => true,
  | 
        
        
            | 
            | 
           86 | 
                           'type' => [
  | 
        
        
            | 
            | 
           87 | 
                               'page' => ['type' => PARAM_INT],
  | 
        
        
            | 
            | 
           88 | 
                               'url' => ['type' => PARAM_URL],
  | 
        
        
            | 
            | 
           89 | 
                               'active' => ['type' => PARAM_BOOL],
  | 
        
        
            | 
            | 
           90 | 
                               'content' => [
  | 
        
        
            | 
            | 
           91 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           92 | 
                                   'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           93 | 
                               ],
  | 
        
        
            | 
            | 
           94 | 
                           ],
  | 
        
        
            | 
            | 
           95 | 
                       ],
  | 
        
        
            | 
            | 
           96 | 
                   ];
  | 
        
        
            | 
            | 
           97 | 
               }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
               /**
  | 
        
        
            | 
            | 
           100 | 
                * Get the additional values to inject while exporting.
  | 
        
        
            | 
            | 
           101 | 
                *
  | 
        
        
            | 
            | 
           102 | 
                * @param renderer_base $output The renderer.
  | 
        
        
            | 
            | 
           103 | 
                * @return array Keys are the property names, values are their values.
  | 
        
        
            | 
            | 
           104 | 
                */
  | 
        
        
            | 
            | 
           105 | 
               protected function get_other_values(renderer_base $output) {
  | 
        
        
            | 
            | 
           106 | 
                   $pagecount = ceil($this->recordcount / $this->pagesize);
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                   $pages = [];
  | 
        
        
            | 
            | 
           109 | 
                   if ($pagecount > 1) {
  | 
        
        
            | 
            | 
           110 | 
                       for ($pageno = 1; $pageno <= $pagecount; $pageno++) {
  | 
        
        
            | 
            | 
           111 | 
                           $pages[] = [
  | 
        
        
            | 
            | 
           112 | 
                               'page' => $pageno,
  | 
        
        
            | 
            | 
           113 | 
                               'url' => call_user_func_array($this->pageurlcallback, [$pageno, $this->pagesize]),
  | 
        
        
            | 
            | 
           114 | 
                               'active' => $pageno === $this->pagenumber,
  | 
        
        
            | 
            | 
           115 | 
                               'content' => null,
  | 
        
        
            | 
            | 
           116 | 
                           ];
  | 
        
        
            | 
            | 
           117 | 
                       }
  | 
        
        
            | 
            | 
           118 | 
                   }
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   return [
  | 
        
        
            | 
            | 
           121 | 
                       'itemsperpage' => $this->pagesize,
  | 
        
        
            | 
            | 
           122 | 
                       'buttons' => [
  | 
        
        
            | 
            | 
           123 | 
                           'first' => false,
  | 
        
        
            | 
            | 
           124 | 
                           'previous' => false,
  | 
        
        
            | 
            | 
           125 | 
                           'next' => false,
  | 
        
        
            | 
            | 
           126 | 
                           'last' => false,
  | 
        
        
            | 
            | 
           127 | 
                       ],
  | 
        
        
            | 
            | 
           128 | 
                       'pages' => $pages,
  | 
        
        
            | 
            | 
           129 | 
                   ];
  | 
        
        
            | 
            | 
           130 | 
               }
  | 
        
        
            | 
            | 
           131 | 
           }
  |