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.
/mod/book/ -> view.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 403]

   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   * Book view page
  19   *
  20   * @package    mod_book
  21   * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__.'/../../config.php');
  26  require_once (__DIR__.'/lib.php');
  27  require_once (__DIR__.'/locallib.php');
  28  require_once($CFG->libdir.'/completionlib.php');
  29  
  30  $id        = optional_param('id', 0, PARAM_INT);        // Course Module ID
  31  $bid       = optional_param('b', 0, PARAM_INT);         // Book id
  32  $chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
  33  $edit      = optional_param('edit', -1, PARAM_BOOL);    // Edit mode
  34  
  35  // =========================================================================
  36  // security checks START - teachers edit; students view
  37  // =========================================================================
  38  if ($id) {
  39      $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
  40      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  41      $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
  42  } else {
  43      $book = $DB->get_record('book', array('id'=>$bid), '*', MUST_EXIST);
  44      $cm = get_coursemodule_from_instance('book', $book->id, 0, false, MUST_EXIST);
  45      $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  46      $id = $cm->id;
  47  }
  48  
  49  require_course_login($course, true, $cm);
  50  
  51  $context = context_module::instance($cm->id);
  52  require_capability('mod/book:read', $context);
  53  
  54  $allowedit  = has_capability('mod/book:edit', $context);
  55  $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
  56  
  57  if ($allowedit) {
  58      if ($edit != -1 and confirm_sesskey()) {
  59          $USER->editing = $edit;
  60      } else {
  61          if (isset($USER->editing)) {
  62              $edit = $USER->editing;
  63          } else {
  64              $edit = 0;
  65          }
  66      }
  67  } else {
  68      $edit = 0;
  69  }
  70  
  71  // read chapters
  72  $chapters = book_preload_chapters($book);
  73  
  74  if ($allowedit and !$chapters) {
  75      redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
  76  }
  77  // Check chapterid and read chapter data
  78  if ($chapterid == '0') { // Go to first chapter if no given.
  79      // Trigger course module viewed event.
  80      book_view($book, null, false, $course, $cm, $context);
  81  
  82      foreach ($chapters as $ch) {
  83          if ($edit || ($ch->hidden && $viewhidden)) {
  84              $chapterid = $ch->id;
  85              break;
  86          }
  87          if (!$ch->hidden) {
  88              $chapterid = $ch->id;
  89              break;
  90          }
  91      }
  92  }
  93  
  94  // Prepare header.
  95  $pagetitle = $book->name;
  96  if ($chapter = $DB->get_record('book_chapters', ['id' => $chapterid, 'bookid' => $book->id])) {
  97      $pagetitle .= ": {$chapter->title}";
  98  }
  99  
 100  $PAGE->set_other_editing_capability('mod/book:edit');
 101  $PAGE->set_title($pagetitle);
 102  $PAGE->set_heading($course->fullname);
 103  $PAGE->add_body_class('limitedwidth');
 104  
 105  // No content in the book.
 106  if (!$chapterid) {
 107      $PAGE->set_url('/mod/book/view.php', array('id' => $id));
 108      echo $OUTPUT->header();
 109      echo $OUTPUT->notification(get_string('nocontent', 'mod_book'), 'info', false);
 110  } else {
 111      $PAGE->set_url('/mod/book/view.php', ['id' => $id, 'chapterid' => $chapterid]);
 112      // The chapter doesnt exist or it is hidden for students.
 113      if (!$chapter or ($chapter->hidden and !$viewhidden)) {
 114          $courseurl = new moodle_url('/course/view.php', ['id' => $course->id]);
 115          throw new moodle_exception('errorchapter', 'mod_book', $courseurl);
 116      }
 117      // Add the Book TOC block.
 118      book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
 119      book_view($book, $chapter, \mod_book\helper::is_last_visible_chapter($chapter->id, $chapters), $course, $cm, $context);
 120  
 121      echo $OUTPUT->header();
 122  
 123      $renderer = $PAGE->get_renderer('mod_book');
 124      $actionmenu = new \mod_book\output\main_action_menu($cm->id, $chapters, $chapter, $book);
 125      $renderedmenu = $renderer->render($actionmenu);
 126      echo $renderedmenu;
 127  
 128      // The chapter itself.
 129      $hidden = $chapter->hidden ? ' dimmed_text' : null;
 130      echo $OUTPUT->box_start('generalbox book_content' . $hidden);
 131  
 132      if (!$book->customtitles) {
 133          if (!$chapter->subchapter) {
 134              $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 135              echo $OUTPUT->heading($currtitle, 3);
 136          } else {
 137              $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
 138              $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 139              echo $OUTPUT->heading($currtitle, 3);
 140              echo $OUTPUT->heading($currsubtitle, 4);
 141          }
 142      }
 143      $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book',
 144          'chapter', $chapter->id);
 145      echo format_text($chaptertext, $chapter->contentformat, ['noclean' => true, 'overflowdiv' => true,
 146          'context' => $context]);
 147  
 148      echo $OUTPUT->box_end();
 149  
 150      if (core_tag_tag::is_enabled('mod_book', 'book_chapters')) {
 151          echo $OUTPUT->tag_list(core_tag_tag::get_item_tags('mod_book', 'book_chapters', $chapter->id), null, 'book-tags');
 152      }
 153      echo $renderedmenu;
 154  }
 155  echo $OUTPUT->footer();