Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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   * Defines the renderer for the book print tool.
  19   *
  20   * @package    booktool_print
  21   * @copyright  2019 Mihail Geshoski
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace booktool_print\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use plugin_renderer_base;
  30  use html_writer;
  31  use context_module;
  32  use moodle_url;
  33  use moodle_exception;
  34  
  35  /**
  36   * The renderer for the book print tool.
  37   *
  38   * @copyright  2019 Mihail Geshoski
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class renderer extends plugin_renderer_base {
  42  
  43      /**
  44       * Render the print book page.
  45       *
  46       * @param print_book_page $page
  47       * @return string html for the page
  48       * @throws moodle_exception
  49       */
  50      public function render_print_book_page(print_book_page $page) {
  51          $data = $page->export_for_template($this);
  52          return parent::render_from_template('booktool_print/print_book', $data);
  53      }
  54  
  55      /**
  56       * Render the print book chapter page.
  57       *
  58       * @param print_book_chapter_page $page
  59       * @return string html for the page
  60       * @throws moodle_exception
  61       */
  62      public function render_print_book_chapter_page(print_book_chapter_page $page) {
  63          $data = $page->export_for_template($this);
  64          return parent::render_from_template('booktool_print/print_book_chapter', $data);
  65      }
  66  
  67      /**
  68       * Render the print book chapter link.
  69       *
  70       * @return string html for the link
  71       */
  72      public function render_print_book_chapter_dialog_link() {
  73          $printtext = get_string('printchapter', 'booktool_print');
  74          $printicon = $this->output->pix_icon('chapter', $printtext, 'booktool_print',
  75                  array('class' => 'icon'));
  76          $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
  77          return html_writer::link('#', $printicon . $printtext, $printlinkatt);
  78      }
  79  
  80      /**
  81       * Render the print book link.
  82       *
  83       * @return string html for the link
  84       */
  85      public function render_print_book_dialog_link() {
  86          $printtext = get_string('printbook', 'booktool_print');
  87          $printicon = $this->output->pix_icon('book', $printtext, 'booktool_print',
  88              array('class' => 'icon'));
  89          $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
  90          return html_writer::link('#', $printicon . $printtext, $printlinkatt);
  91      }
  92  
  93      /**
  94       * Render the print book table of contents.
  95       *
  96       * @param array $chapters Array of book chapters
  97       * @param object $book The book object
  98       * @param object $cm The curse module object
  99       * @return string html for the TOC
 100       */
 101      public function render_print_book_toc($chapters, $book, $cm) {
 102  
 103          $first = true;
 104  
 105          $context = context_module::instance($cm->id);
 106  
 107          $toc = ''; // Representation of toc (HTML).
 108  
 109          switch ($book->numbering) {
 110              case BOOK_NUM_NONE:
 111                  $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
 112                  break;
 113              case BOOK_NUM_NUMBERS:
 114                  $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
 115                  break;
 116              case BOOK_NUM_BULLETS:
 117                  $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
 118                  break;
 119              case BOOK_NUM_INDENTED:
 120                  $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
 121                  break;
 122          }
 123  
 124          $toc .= html_writer::tag('a', '', array('name' => 'toc')); // Representation of toc (HTML).
 125  
 126          $toc .= html_writer::tag('h2', get_string('toc', 'mod_book'), ['class' => 'text-center pb-5']);
 127          $toc .= html_writer::start_tag('ul');
 128          foreach ($chapters as $ch) {
 129              if (!$ch->hidden) {
 130                  $title = book_get_chapter_title($ch->id, $chapters, $book, $context);
 131                  if (!$ch->subchapter) {
 132  
 133                      if ($first) {
 134                          $toc .= html_writer::start_tag('li');
 135                      } else {
 136                          $toc .= html_writer::end_tag('ul');
 137                          $toc .= html_writer::end_tag('li');
 138                          $toc .= html_writer::start_tag('li');
 139                      }
 140  
 141                  } else {
 142  
 143                      if ($first) {
 144                          $toc .= html_writer::start_tag('li');
 145                          $toc .= html_writer::start_tag('ul');
 146                          $toc .= html_writer::start_tag('li');
 147                      } else {
 148                          $toc .= html_writer::start_tag('li');
 149                      }
 150  
 151                  }
 152  
 153                  if (!$ch->subchapter) {
 154                      $toc .= html_writer::link(new moodle_url('#ch' . $ch->id), $title,
 155                              array('title' => s($title), 'class' => 'font-weight-bold text-decoration-none'));
 156                      $toc .= html_writer::start_tag('ul');
 157                  } else {
 158                      $toc .= html_writer::link(new moodle_url('#ch' . $ch->id), $title,
 159                              array('title' => s($title), 'class' => 'text-decoration-none'));
 160                      $toc .= html_writer::end_tag('li');
 161                  }
 162                  $first = false;
 163              }
 164          }
 165  
 166          $toc .= html_writer::end_tag('ul');
 167          $toc .= html_writer::end_tag('li');
 168          $toc .= html_writer::end_tag('ul');
 169          $toc .= html_writer::end_tag('div');
 170  
 171          $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
 172  
 173          return $toc;
 174      }
 175  
 176      /**
 177       * Render the print book chapter.
 178       *
 179       * @param object $chapter The book chapter object
 180       * @param array $chapters The array of book chapters
 181       * @param object $book The book object
 182       * @param object $cm The course module object
 183       * @return array The array containing the content of the book chapter and visibility information
 184       */
 185      public function render_print_book_chapter($chapter, $chapters, $book, $cm) {
 186          $context = context_module::instance($cm->id);
 187          $title = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 188  
 189          $chaptervisible = $chapter->hidden ? false : true;
 190  
 191          $bookchapter = '';
 192          $bookchapter .= html_writer::start_div('book_chapter pt-3', ['id' => 'ch' . $chapter->id]);
 193          if (!$book->customtitles) {
 194              if (!$chapter->subchapter) {
 195                  $bookchapter .= $this->output->heading($title, 2, 'text-center pb-5');
 196              } else {
 197                  $bookchapter .= $this->output->heading($title, 3, 'text-center pb-5');
 198              }
 199          }
 200  
 201          $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id,
 202              'mod_book', 'chapter', $chapter->id);
 203          $bookchapter .= format_text($chaptertext, $chapter->contentformat, array('noclean' => true, 'context' => $context));
 204          $bookchapter .= html_writer::end_div();
 205  
 206          return array($bookchapter, $chaptervisible);
 207      }
 208  }