3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file contains all necessary code to view a wiki page
21 * @package mod-wiki-2.0
22 * @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
25 * @author Jordi Piguillem
27 * @author David Jimenez
29 * @author Kenneth Riba
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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');
39 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
41 $pageid = optional_param('pageid', 0, PARAM_INT); // Page ID
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);
49 $edit = optional_param('edit', -1, PARAM_BOOL);
51 $action = optional_param('action', '', PARAM_ALPHA);
52 $swid = optional_param('swid', 0, PARAM_INT); // Subwiki ID
57 * User that comes from a course. First wiki page must be shown
59 * URL params: id -> course module id
63 // Cheacking course module instance
64 if (!$cm = get_coursemodule_from_id('wiki', $id)) {
65 print_error('invalidcoursemodule');
68 // Checking course instance
69 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
71 // Checking wiki instance
72 if (!$wiki = wiki_get_wiki($cm->instance)) {
73 print_error('incorrectwikiid', 'wiki');
77 // Getting the subwiki corresponding to that wiki, group and user.
79 // Also setting the page if it exists or getting the first page title form
82 // Getting current group id
83 $currentgroup = groups_get_activity_group($cm);
84 $currentgroup = !empty($currentgroup) ? $currentgroup : 0;
85 // Getting current user id
86 if ($wiki->wikimode == 'individual') {
92 // Getting subwiki. If it does not exists, redirecting to create page
93 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
94 $params = array('wid' => $wiki->id, 'gid' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
95 $url = new moodle_url('/mod/wiki/create.php', $params);
99 // Getting first page. If it does not exists, redirecting to create page
100 if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
101 $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
102 $url = new moodle_url('/mod/wiki/create.php', $params);
109 * A user wants to see a page.
111 * URL Params: pageid -> page id
116 // Checking page instance
117 if (!$page = wiki_get_page($pageid)) {
118 print_error('incorrectpageid', 'wiki');
122 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
123 print_error('incorrectsubwikiid', 'wiki');
126 // Checking wiki instance of that subwiki
127 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
128 print_error('incorrectwikiid', 'wiki');
131 // Checking course module instance
132 if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
133 print_error('invalidcoursemodule');
136 // Checking course instance
137 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
142 * Trying to read a page from another group or user
144 * Page can exists or not.
145 * * If it exists, page must be shown
146 * * If it does not exists, system must ask for its creation
148 * URL params: wid -> subwiki id (required)
149 * title -> a page title (required)
150 * group -> group id (optional)
151 * uid -> user id (optional)
152 * groupanduser -> (optional)
154 } elseif ($wid && $title) {
156 // Setting wiki instance
157 if (!$wiki = wiki_get_wiki($wid)) {
158 print_error('incorrectwikiid', 'wiki');
161 // Checking course module
162 if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
163 print_error('invalidcoursemodule');
166 // Checking course instance
167 if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
168 print_error('coursemisconf');
171 $groupmode = groups_get_activity_groupmode($cm);
172 if (empty($currentgroup)) {
173 $currentgroup = groups_get_activity_group($cm);
174 $currentgroup = !empty($currentgroup) ? $currentgroup : 0;
177 if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
178 list($gid, $uid) = explode('-', $groupanduser);
179 } else if ($wiki->wikimode == 'individual') {
182 } else if ($groupmode == NOGROUPS) {
186 $gid = $currentgroup;
190 // Getting subwiki instance. If it does not exists, redirect to create page
191 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
192 $params = array('wid' => $wiki->id, 'gid' => $gid, 'uid' => $uid, 'title' => $title);
193 $url = new moodle_url('/mod/wiki/create.php', $params);
197 // Checking is there is a page with this title. If it does not exists, redirect to first page
198 if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
199 $params = array('wid' => $wiki->id, 'gid' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
200 $url = new moodle_url('/mod/wiki/view.php', $params);
207 // * A user switches group when is 'reading' a non-existent page.
209 // * URL Params: wid -> wiki id
210 // * title -> page title
211 // * currentgroup -> group id
214 //} elseif ($wid && $title && $currentgroup) {
216 // // Checking wiki instance
217 // if (!$wiki = wiki_get_wiki($wid)) {
218 // print_error('incorrectwikiid', 'wiki');
221 // // Checking subwiki instance
222 // // @TODO: Fix call to wiki_get_subwiki_by_group
223 // if (!$currentgroup = groups_get_activity_group($cm)){
224 // $currentgroup = 0;
226 // if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
227 // print_error('incorrectsubwikiid', 'wiki');
230 // // Checking page instance
231 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
235 // // Checking course instance
236 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
238 // // Checking course module instance
239 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
240 // print_error('invalidcoursemodule');
249 // * Error. No more options
252 print_error('incorrectparameters');
254 require_login($course, true, $cm);
256 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
257 require_capability('mod/wiki:viewpage', $context);
259 add_to_log($course->id, 'wiki', 'view', 'view.php?id=' . $cm->id, $wiki->id);
261 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
262 $USER->editing = $edit;
265 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
267 /*The following piece of code is used in order
268 * to perform set_url correctly. It is necessary in order
269 * to make page_wiki_view class know that this page
270 * has been called via its id.
273 $wikipage->set_coursemodule($id);
276 $wikipage->set_gid($currentgroup);
277 $wikipage->set_page($page);
279 $wikipage->print_header();
280 $wikipage->print_content();
282 $wikipage->print_footer();