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

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file contains all necessary code to view a wiki page
  20   *
  21   * @package mod_wiki
  22   * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  23   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  24   *
  25   * @author Jordi Piguillem
  26   * @author Marc Alier
  27   * @author David Jimenez
  28   * @author Josep Arus
  29   * @author Kenneth Riba
  30   *
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  
  34  require_once('../../config.php');
  35  require_once($CFG->dirroot . '/mod/wiki/lib.php');
  36  require_once($CFG->dirroot . '/mod/wiki/locallib.php');
  37  require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
  38  
  39  $id = optional_param('id', 0, PARAM_INT); // Course Module ID
  40  
  41  $pageid = optional_param('pageid', 0, PARAM_INT); // Page ID
  42  
  43  $wid = optional_param('wid', 0, PARAM_INT); // Wiki ID
  44  $title = optional_param('title', '', PARAM_TEXT); // Page Title
  45  $currentgroup = optional_param('group', 0, PARAM_INT); // Group ID
  46  $userid = optional_param('uid', 0, PARAM_INT); // User ID
  47  $groupanduser = optional_param('groupanduser', 0, PARAM_TEXT);
  48  
  49  $edit = optional_param('edit', -1, PARAM_BOOL);
  50  
  51  $action = optional_param('action', '', PARAM_ALPHA);
  52  $swid = optional_param('swid', 0, PARAM_INT); // Subwiki ID
  53  
  54  $PAGE->force_settings_menu();
  55  $PAGE->add_body_class('limitedwidth');
  56  
  57  /*
  58   * Case 0:
  59   *
  60   * User that comes from a course. First wiki page must be shown
  61   *
  62   * URL params: id -> course module id
  63   *
  64   */
  65  if ($id) {
  66      // Cheacking course module instance
  67      if (!$cm = get_coursemodule_from_id('wiki', $id)) {
  68          throw new \moodle_exception('invalidcoursemodule');
  69      }
  70  
  71      // Checking course instance
  72      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  73  
  74      require_course_login($course, true, $cm);
  75  
  76      // Checking wiki instance
  77      if (!$wiki = wiki_get_wiki($cm->instance)) {
  78          throw new \moodle_exception('incorrectwikiid', 'wiki');
  79      }
  80      $PAGE->set_cm($cm);
  81  
  82      // Getting the subwiki corresponding to that wiki, group and user.
  83      //
  84      // Also setting the page if it exists or getting the first page title form
  85      // that wiki
  86  
  87      // Getting current group id
  88      $currentgroup = groups_get_activity_group($cm);
  89  
  90      // Getting current user id
  91      if ($wiki->wikimode == 'individual') {
  92          $userid = $USER->id;
  93      } else {
  94          $userid = 0;
  95      }
  96  
  97      // Getting subwiki. If it does not exists, redirecting to create page
  98      if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
  99          $params = array('wid' => $wiki->id, 'group' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
 100          $url = new moodle_url('/mod/wiki/create.php', $params);
 101          redirect($url);
 102      }
 103  
 104      // Getting first page. If it does not exists, redirecting to create page
 105      if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
 106          $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
 107          $url = new moodle_url('/mod/wiki/create.php', $params);
 108          redirect($url);
 109      }
 110  
 111      /*
 112       * Case 1:
 113       *
 114       * A user wants to see a page.
 115       *
 116       * URL Params: pageid -> page id
 117       *
 118       */
 119  } elseif ($pageid) {
 120  
 121      // Checking page instance
 122      if (!$page = wiki_get_page($pageid)) {
 123          throw new \moodle_exception('incorrectpageid', 'wiki');
 124      }
 125  
 126      // Checking subwiki
 127      if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
 128          throw new \moodle_exception('incorrectsubwikiid', 'wiki');
 129      }
 130  
 131      // Checking wiki instance of that subwiki
 132      if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
 133          throw new \moodle_exception('incorrectwikiid', 'wiki');
 134      }
 135  
 136      // Checking course module instance
 137      if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
 138          throw new \moodle_exception('invalidcoursemodule');
 139      }
 140  
 141      $currentgroup = $subwiki->groupid;
 142  
 143      // Checking course instance
 144      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 145  
 146      require_course_login($course, true, $cm);
 147      /*
 148       * Case 2:
 149       *
 150       * Trying to read a page from another group or user
 151       *
 152       * Page can exists or not.
 153       *  * If it exists, page must be shown
 154       *  * If it does not exists, system must ask for its creation
 155       *
 156       * URL params: wid -> subwiki id (required)
 157       *             title -> a page title (required)
 158       *             group -> group id (optional)
 159       *             uid -> user id (optional)
 160       *             groupanduser -> (optional)
 161       */
 162  } elseif ($wid && $title) {
 163  
 164      // Setting wiki instance
 165      if (!$wiki = wiki_get_wiki($wid)) {
 166          throw new \moodle_exception('incorrectwikiid', 'wiki');
 167      }
 168  
 169      // Checking course module
 170      if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
 171          throw new \moodle_exception('invalidcoursemodule');
 172      }
 173  
 174      // Checking course instance
 175      $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 176  
 177      require_course_login($course, true, $cm);
 178  
 179      $groupmode = groups_get_activity_groupmode($cm);
 180  
 181      // This is where people will land when they change groups using the drop-down selector.
 182      // Set the activity group so tabs and content are shown correctly.
 183      $currentgroup = groups_get_activity_group($cm, true);
 184  
 185      if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
 186          list($gid, $uid) = explode('-', $groupanduser);
 187      } else if ($wiki->wikimode == 'individual') {
 188          $gid = 0;
 189          $uid = $userid;
 190      } else if ($groupmode == NOGROUPS) {
 191          $gid = 0;
 192          $uid = 0;
 193      } else {
 194          $gid = $currentgroup;
 195          $uid = 0;
 196      }
 197  
 198      // Getting subwiki instance. If it does not exists, redirect to create page
 199      if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
 200          $context = context_module::instance($cm->id);
 201  
 202          $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
 203          $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
 204  
 205          $manage = has_capability('mod/wiki:managewiki', $context);
 206          $edit = has_capability('mod/wiki:editpage', $context);
 207          $manageandedit = $manage && $edit;
 208  
 209          if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
 210              throw new \moodle_exception('nocontent', 'wiki');
 211          }
 212  
 213          $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $title);
 214          $url = new moodle_url('/mod/wiki/create.php', $params);
 215          redirect($url);
 216      }
 217  
 218      // Checking is there is a page with this title. If it does not exists, redirect to first page
 219      if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
 220          $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
 221          // Check to see if the first page has been created
 222          if (!wiki_get_page_by_title($subwiki->id, $wiki->firstpagetitle)) {
 223              $url = new moodle_url('/mod/wiki/create.php', $params);
 224          } else {
 225              $url = new moodle_url('/mod/wiki/view.php', $params);
 226          }
 227          redirect($url);
 228      }
 229  
 230      //    /*
 231      //     * Case 3:
 232      //     *
 233      //     * A user switches group when is 'reading' a non-existent page.
 234      //     *
 235      //     * URL Params: wid -> wiki id
 236      //     *             title -> page title
 237      //     *             currentgroup -> group id
 238      //     *
 239      //     */
 240      //} elseif ($wid && $title && $currentgroup) {
 241      //
 242      //    // Checking wiki instance
 243      //    if (!$wiki = wiki_get_wiki($wid)) {
 244      //        throw new \moodle_exception('incorrectwikiid', 'wiki');
 245      //    }
 246      //
 247      //    // Checking subwiki instance
 248      //    // @TODO: Fix call to wiki_get_subwiki_by_group
 249      //    if (!$currentgroup = groups_get_activity_group($cm)){
 250      //        $currentgroup = 0;
 251      //    }
 252      //    if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
 253      //        throw new \moodle_exception('incorrectsubwikiid', 'wiki');
 254      //    }
 255      //
 256      //    // Checking page instance
 257      //    if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
 258      //        unset($title);
 259      //    }
 260      //
 261      //    // Checking course instance
 262      //    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 263      //
 264      //    // Checking course module instance
 265      //    if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
 266      //        throw new \moodle_exception('invalidcoursemodule');
 267      //    }
 268      //
 269      //    $subwiki = null;
 270      //    $page = null;
 271      //
 272      //    /*
 273      //     * Case 4:
 274      //     *
 275      //     * Error. No more options
 276      //     */
 277  } else {
 278      throw new \moodle_exception('invalidparameters', 'wiki');
 279  }
 280  
 281  if (!wiki_user_can_view($subwiki, $wiki)) {
 282      throw new \moodle_exception('cannotviewpage', 'wiki');
 283  }
 284  
 285  if (($edit != - 1) and $PAGE->user_allowed_editing()) {
 286      $USER->editing = $edit;
 287  }
 288  
 289  $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
 290  
 291  $wikipage->set_gid($currentgroup);
 292  $wikipage->set_page($page);
 293  
 294  $context = context_module::instance($cm->id);
 295  if ($pageid) {
 296      wiki_page_view($wiki, $page, $course, $cm, $context, null, null, $subwiki);
 297  } else if ($id) {
 298      wiki_view($wiki, $course, $cm, $context);
 299  } else if ($wid && $title) {
 300      $other = array(
 301          'title' => $title,
 302          'wid' => $wid,
 303          'group' => $gid,
 304          'groupanduser' => $groupanduser
 305      );
 306      wiki_page_view($wiki, $page, $course, $cm, $context, $uid, $other, $subwiki);
 307  }
 308  
 309  $wikipage->print_header();
 310  $wikipage->print_content();
 311  
 312  $wikipage->print_footer();