Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/mod/book/ -> view.php (source)

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
  95  
  96  // No content in the book.
  97  if (!$chapterid) {
  98      $PAGE->set_url('/mod/book/view.php', array('id' => $id));
  99      notice(get_string('nocontent', 'mod_book'), $courseurl->out(false));
 100  }
 101  // Chapter doesnt exist or it is hidden for students
 102  if ((!$chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id))) or ($chapter->hidden and !$viewhidden)) {
 103      print_error('errorchapter', 'mod_book', $courseurl);
 104  }
 105  
 106  $PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
 107  
 108  
 109  // Unset all page parameters.
 110  unset($id);
 111  unset($bid);
 112  unset($chapterid);
 113  
 114  // Read standard strings.
 115  $strbooks = get_string('modulenameplural', 'mod_book');
 116  $strbook  = get_string('modulename', 'mod_book');
 117  $strtoc   = get_string('toc', 'mod_book');
 118  
 119  // prepare header
 120  $pagetitle = $book->name . ": " . $chapter->title;
 121  $PAGE->set_title($pagetitle);
 122  $PAGE->set_heading($course->fullname);
 123  
 124  book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
 125  
 126  // prepare chapter navigation icons
 127  $previd = null;
 128  $prevtitle = null;
 129  $navprevtitle = null;
 130  $nextid = null;
 131  $nexttitle = null;
 132  $navnexttitle = null;
 133  $last = null;
 134  foreach ($chapters as $ch) {
 135      if (!$edit and ($ch->hidden && !$viewhidden)) {
 136          continue;
 137      }
 138      if ($last == $chapter->id) {
 139          $nextid = $ch->id;
 140          $nexttitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
 141          $navnexttitle = get_string('navnexttitle', 'mod_book', $nexttitle);
 142          break;
 143      }
 144      if ($ch->id != $chapter->id) {
 145          $previd = $ch->id;
 146          $prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
 147          $navprevtitle = get_string('navprevtitle', 'mod_book', $prevtitle);
 148      }
 149      $last = $ch->id;
 150  }
 151  
 152  if ($book->navstyle) {
 153      $navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
 154      $navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
 155  
 156      $chnavigation = '';
 157      if ($previd) {
 158          $navprev = get_string('navprev', 'book');
 159          if ($book->navstyle == 1) {
 160              $chnavigation .= '<a title="' . $navprevtitle . '" class="bookprev" href="view.php?id=' .
 161                  $cm->id . '&amp;chapterid=' . $previd .  '">' .
 162                  $OUTPUT->pix_icon($navprevicon, $navprevtitle, 'mod_book') . '</a>';
 163          } else {
 164              $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
 165                  $cm->id . '&amp;chapterid=' . $previd . '">' .
 166                  '<span class="chaptername"><span class="arrow">' . $OUTPUT->larrow() . '&nbsp;</span></span>' .
 167                  $navprev . ':&nbsp;<span class="chaptername">' . $prevtitle . '</span></a>';
 168          }
 169      }
 170      if ($nextid) {
 171          $navnext = get_string('navnext', 'book');
 172          if ($book->navstyle == 1) {
 173              $chnavigation .= '<a title="' . $navnexttitle . '" class="booknext" href="view.php?id=' .
 174                  $cm->id . '&amp;chapterid='.$nextid.'">' .
 175                  $OUTPUT->pix_icon($navnexticon, $navnexttitle, 'mod_book') . '</a>';
 176          } else {
 177              $chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' .
 178                  $cm->id . '&amp;chapterid='.$nextid.'">' .
 179                  $navnext . ':<span class="chaptername">&nbsp;' . $nexttitle.
 180                  '<span class="arrow">&nbsp;' . $OUTPUT->rarrow() . '</span></span></a>';
 181          }
 182      } else {
 183          $navexit = get_string('navexit', 'book');
 184          $sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section));
 185          $returnurl = course_get_url($course, $sec);
 186          if ($book->navstyle == 1) {
 187              $chnavigation .= '<a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
 188                  $OUTPUT->pix_icon('nav_exit', $navexit, 'mod_book') . '</a>';
 189          } else {
 190              $chnavigation .= ' <a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
 191                  '<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
 192          }
 193      }
 194  }
 195  
 196  // We need to discover if this is the last chapter to mark activity as completed.
 197  $islastchapter = false;
 198  if (!$nextid) {
 199      $islastchapter = true;
 200  }
 201  
 202  book_view($book, $chapter, $islastchapter, $course, $cm, $context);
 203  
 204  // =====================================================
 205  // Book display HTML code
 206  // =====================================================
 207  
 208  echo $OUTPUT->header();
 209  echo $OUTPUT->heading(format_string($book->name));
 210  
 211  // Info box.
 212  if ($book->intro) {
 213      echo $OUTPUT->box(format_module_intro('book', $book, $cm->id), 'generalbox', 'intro');
 214  }
 215  
 216  $navclasses = book_get_nav_classes();
 217  
 218  if ($book->navstyle) {
 219      // Upper navigation.
 220      echo '<div class="navtop border-top py-3 clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
 221  }
 222  
 223  // The chapter itself.
 224  $hidden = $chapter->hidden ? ' dimmed_text' : null;
 225  echo $OUTPUT->box_start('generalbox book_content' . $hidden);
 226  
 227  if (!$book->customtitles) {
 228      if (!$chapter->subchapter) {
 229          $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 230          echo $OUTPUT->heading($currtitle, 3);
 231      } else {
 232          $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
 233          $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 234          echo $OUTPUT->heading($currtitle, 3);
 235          echo $OUTPUT->heading($currsubtitle, 4);
 236      }
 237  }
 238  $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
 239  echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'overflowdiv'=>true, 'context'=>$context));
 240  
 241  echo $OUTPUT->box_end();
 242  
 243  if (core_tag_tag::is_enabled('mod_book', 'book_chapters')) {
 244      echo $OUTPUT->tag_list(core_tag_tag::get_item_tags('mod_book', 'book_chapters', $chapter->id), null, 'book-tags');
 245  }
 246  
 247  if ($book->navstyle) {
 248      // Lower navigation.
 249      echo '<div class="navbottom py-3 border-bottom clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
 250  }
 251  
 252  echo $OUTPUT->footer();