Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Output the action menu for this activity.
  19   *
  20   * @package   mod_book
  21   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_book\output;
  26  
  27  use templatable;
  28  use renderable;
  29  use moodle_url;
  30  use stdClass;
  31  
  32  /**
  33   * Output the action menu for the book activity.
  34   *
  35   * @package   mod_book
  36   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class main_action_menu implements templatable, renderable {
  40  
  41      /** @var int The course module ID. */
  42      protected $cmid;
  43      /** @var stdClass[] Chapters of the book. */
  44      protected $chapters;
  45      /** @var stdClass Current chapter of the book. */
  46      protected $chapter;
  47  
  48      /**
  49       * Constructor for this class.
  50       *
  51       * @param int      $cmid     The course module ID.
  52       * @param array    $chapters Chapters of this book.
  53       * @param stdClass $chapter  The current chapter.
  54       */
  55      public function __construct(int $cmid, array $chapters, stdClass $chapter) {
  56          $this->cmid = $cmid;
  57          $this->chapters = $chapters;
  58          $this->chapter = $chapter;
  59      }
  60  
  61      /**
  62       * Get the next chapter in the book.
  63       *
  64       * @return ?stdClass The next chapter of the book.
  65       */
  66      protected function get_next_chapter(): ?stdClass {
  67          $nextpageid = $this->chapter->pagenum + 1;
  68          // Early return if the current chapter is also the last chapter.
  69          if ($nextpageid > count($this->chapters)) {
  70              return null;
  71          }
  72          while ((!$nextchapter = $this->get_chapter($nextpageid))) {
  73              // Break the loop if this is the last chapter.
  74              if ($nextpageid === count($this->chapters)) {
  75                  break;
  76              }
  77              $nextpageid++;
  78          }
  79          return $nextchapter;
  80      }
  81  
  82      /**
  83       * Get the previous chapter in the book.
  84       *
  85       * @return ?stdClass The previous chapter of the book.
  86       */
  87      protected function get_previous_chapter(): ?stdClass {
  88          $prevpageid = $this->chapter->pagenum - 1;
  89          // Early return if the current chapter is also the first chapter.
  90          if ($prevpageid < 1) {
  91              return null;
  92          }
  93          while ((!$prevchapter = $this->get_chapter($prevpageid))) {
  94              // Break the loop if this is the first chapter.
  95              if ($prevpageid === 1) {
  96                  break;
  97              }
  98              $prevpageid--;
  99          }
 100          return $prevchapter;
 101      }
 102  
 103      /**
 104       * Get the specific chapter of the book.
 105       *
 106       * @param int $id The chapter id to retrieve.
 107       * @return ?stdClass The requested chapter.
 108       */
 109      protected function get_chapter(int $id): ?stdClass {
 110          $context = \context_module::instance($this->cmid);
 111          $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
 112  
 113          foreach ($this->chapters as $chapter) {
 114              // Also make sure that the chapter is not hidden or the user can view hidden chapters before returning
 115              // the chapter object.
 116              if (($chapter->pagenum == $id) && (!$chapter->hidden || $viewhidden)) {
 117                  return $chapter;
 118              }
 119          }
 120          return null;
 121      }
 122  
 123      /**
 124       * Exports the navigation buttons around the book.
 125       *
 126       * @param \renderer_base $output renderer base output.
 127       * @return array Data to render.
 128       */
 129      public function export_for_template(\renderer_base $output): array {
 130          $next = $this->get_next_chapter();
 131          $previous = $this->get_previous_chapter();
 132  
 133          $context = \context_module::instance($this->cmid);
 134          $data = [];
 135  
 136          if ($next) {
 137              $nextdata = [
 138                  'title' => get_string('navnext', 'mod_book'),
 139                  'url' => (new moodle_url('/mod/book/view.php', ['id' => $this->cmid, 'chapterid' => $next->id]))->out(false)
 140              ];
 141              $data['next'] = $nextdata;
 142          }
 143          if ($previous) {
 144              $previousdata = [
 145                  'title' => get_string('navprev', 'mod_book'),
 146                  'url' => (new moodle_url('/mod/book/view.php', ['id' => $this->cmid, 'chapterid' => $previous->id]))->out(false)
 147              ];
 148              $data['previous'] = $previousdata;
 149          }
 150  
 151          return $data;
 152      }
 153  }