[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/book/ -> view.php (source)

   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) {
  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  $nextid = null;
 130  $nexttitle = null;
 131  $last = null;
 132  foreach ($chapters as $ch) {
 133      if (!$edit and $ch->hidden) {
 134          continue;
 135      }
 136      if ($last == $chapter->id) {
 137          $nextid = $ch->id;
 138          $nexttitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
 139          break;
 140      }
 141      if ($ch->id != $chapter->id) {
 142          $previd = $ch->id;
 143          $prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
 144      }
 145      $last = $ch->id;
 146  }
 147  
 148  $islastchapter = false;
 149  if ($book->navstyle) {
 150      $navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
 151      $navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
 152      $navprevdisicon = right_to_left() ? 'nav_next_dis' : 'nav_prev_dis';
 153  
 154      $chnavigation = '';
 155      if ($previd) {
 156          $navprev = get_string('navprev', 'book');
 157          if ($book->navstyle == 1) {
 158              $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
 159                  $cm->id . '&amp;chapterid=' . $previd .  '">' .
 160                  '<img src="' . $OUTPUT->pix_url($navprevicon, 'mod_book') . '" class="icon" alt="' . $navprev . '"/></a>';
 161          } else {
 162              $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
 163                  $cm->id . '&amp;chapterid=' . $previd . '">' .
 164                  '<span class="chaptername"><span class="arrow">' . $OUTPUT->larrow() . '&nbsp;</span></span>' .
 165                  $navprev . ':&nbsp;<span class="chaptername">' . $prevtitle . '</span></a>';
 166          }
 167      } else {
 168          if ($book->navstyle == 1) {
 169              $chnavigation .= '<img src="' . $OUTPUT->pix_url($navprevdisicon, 'mod_book') . '" class="icon" alt="" />';
 170          }
 171      }
 172      if ($nextid) {
 173          $navnext = get_string('navnext', 'book');
 174          if ($book->navstyle == 1) {
 175              $chnavigation .= '<a title="' . $navnext . '" class="booknext" href="view.php?id=' .
 176                  $cm->id . '&amp;chapterid='.$nextid.'">' .
 177                  '<img src="' . $OUTPUT->pix_url($navnexticon, 'mod_book').'" class="icon" alt="' . $navnext . '" /></a>';
 178          } else {
 179              $chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' .
 180                  $cm->id . '&amp;chapterid='.$nextid.'">' .
 181                  $navnext . ':<span class="chaptername">&nbsp;' . $nexttitle.
 182                  '<span class="arrow">&nbsp;' . $OUTPUT->rarrow() . '</span></span></a>';
 183          }
 184      } else {
 185          $navexit = get_string('navexit', 'book');
 186          $sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section));
 187          $returnurl = course_get_url($course, $sec);
 188          if ($book->navstyle == 1) {
 189              $chnavigation .= '<a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
 190                  '<img src="' . $OUTPUT->pix_url('nav_exit', 'mod_book') . '" class="icon" alt="' . $navexit . '" /></a>';
 191          } else {
 192              $chnavigation .= ' <a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
 193                  '<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
 194          }
 195  
 196          $islastchapter = true;
 197      }
 198  }
 199  
 200  book_view($book, $chapter, $islastchapter, $course, $cm, $context);
 201  
 202  // =====================================================
 203  // Book display HTML code
 204  // =====================================================
 205  
 206  echo $OUTPUT->header();
 207  echo $OUTPUT->heading(format_string($book->name));
 208  
 209  $navclasses = book_get_nav_classes();
 210  
 211  if ($book->navstyle) {
 212      // Upper navigation.
 213      echo '<div class="navtop clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
 214  }
 215  
 216  // The chapter itself.
 217  $hidden = $chapter->hidden ? ' dimmed_text' : null;
 218  echo $OUTPUT->box_start('generalbox book_content' . $hidden);
 219  
 220  if (!$book->customtitles) {
 221      if (!$chapter->subchapter) {
 222          $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 223          echo $OUTPUT->heading($currtitle, 3);
 224      } else {
 225          $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
 226          $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
 227          echo $OUTPUT->heading($currtitle, 3);
 228          echo $OUTPUT->heading($currsubtitle, 4);
 229      }
 230  }
 231  $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
 232  echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'overflowdiv'=>true, 'context'=>$context));
 233  
 234  echo $OUTPUT->box_end();
 235  
 236  if ($book->navstyle) {
 237      // Lower navigation.
 238      echo '<div class="navbottom clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
 239  }
 240  
 241  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1