Merge branch 'wip-MDL-31948-master' of git://github.com/phalacee/moodle
[moodle.git] / mod / wiki / view.php
blobfbfb3e0b6947b5e27caf71c2d5a2c790ef4b29be
1 <?php
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.
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/>.
18 /**
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
26 * @author Marc Alier
27 * @author David Jimenez
28 * @author Josep Arus
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
55 * Case 0:
57 * User that comes from a course. First wiki page must be shown
59 * URL params: id -> course module id
62 if ($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');
75 $PAGE->set_cm($cm);
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
80 // that wiki
82 // Getting current group id
83 $currentgroup = groups_get_activity_group($cm);
85 // Getting current user id
86 if ($wiki->wikimode == 'individual') {
87 $userid = $USER->id;
88 } else {
89 $userid = 0;
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, 'group' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
95 $url = new moodle_url('/mod/wiki/create.php', $params);
96 redirect($url);
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);
103 redirect($url);
107 * Case 1:
109 * A user wants to see a page.
111 * URL Params: pageid -> page id
114 } elseif ($pageid) {
116 // Checking page instance
117 if (!$page = wiki_get_page($pageid)) {
118 print_error('incorrectpageid', 'wiki');
121 // Checking subwiki
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 $currentgroup = $subwiki->groupid;
138 // Checking course instance
139 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
142 * Case 2:
144 * Trying to read a page from another group or user
146 * Page can exists or not.
147 * * If it exists, page must be shown
148 * * If it does not exists, system must ask for its creation
150 * URL params: wid -> subwiki id (required)
151 * title -> a page title (required)
152 * group -> group id (optional)
153 * uid -> user id (optional)
154 * groupanduser -> (optional)
156 } elseif ($wid && $title) {
158 // Setting wiki instance
159 if (!$wiki = wiki_get_wiki($wid)) {
160 print_error('incorrectwikiid', 'wiki');
163 // Checking course module
164 if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
165 print_error('invalidcoursemodule');
168 // Checking course instance
169 if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
170 print_error('coursemisconf');
173 $groupmode = groups_get_activity_groupmode($cm);
175 if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
176 list($gid, $uid) = explode('-', $groupanduser);
177 } else if ($wiki->wikimode == 'individual') {
178 $gid = 0;
179 $uid = $userid;
180 } else if ($groupmode == NOGROUPS) {
181 $gid = 0;
182 $uid = 0;
183 } else {
184 $gid = $currentgroup;
185 $uid = 0;
188 // Getting subwiki instance. If it does not exists, redirect to create page
189 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
190 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
192 $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
193 $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
195 $manage = has_capability('mod/wiki:managewiki', $context);
196 $edit = has_capability('mod/wiki:editpage', $context);
197 $manageandedit = $manage && $edit;
199 if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
200 print_error('nocontent','wiki');
203 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $title);
204 $url = new moodle_url('/mod/wiki/create.php', $params);
205 redirect($url);
208 // Checking is there is a page with this title. If it does not exists, redirect to first page
209 if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
210 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
211 // Check to see if the first page has been created
212 if (!wiki_get_page_by_title($subwiki->id, $wiki->firstpagetitle)) {
213 $url = new moodle_url('/mod/wiki/create.php', $params);
214 } else {
215 $url = new moodle_url('/mod/wiki/view.php', $params);
217 redirect($url);
220 // /*
221 // * Case 3:
222 // *
223 // * A user switches group when is 'reading' a non-existent page.
224 // *
225 // * URL Params: wid -> wiki id
226 // * title -> page title
227 // * currentgroup -> group id
228 // *
229 // */
230 //} elseif ($wid && $title && $currentgroup) {
232 // // Checking wiki instance
233 // if (!$wiki = wiki_get_wiki($wid)) {
234 // print_error('incorrectwikiid', 'wiki');
235 // }
237 // // Checking subwiki instance
238 // // @TODO: Fix call to wiki_get_subwiki_by_group
239 // if (!$currentgroup = groups_get_activity_group($cm)){
240 // $currentgroup = 0;
241 // }
242 // if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
243 // print_error('incorrectsubwikiid', 'wiki');
244 // }
246 // // Checking page instance
247 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
248 // unset($title);
249 // }
251 // // Checking course instance
252 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
254 // // Checking course module instance
255 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
256 // print_error('invalidcoursemodule');
257 // }
259 // $subwiki = null;
260 // $page = null;
262 // /*
263 // * Case 4:
264 // *
265 // * Error. No more options
266 // */
267 } else {
268 print_error('incorrectparameters');
270 require_login($course, true, $cm);
272 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
273 require_capability('mod/wiki:viewpage', $context);
275 // Update 'viewed' state if required by completion system
276 require_once($CFG->libdir . '/completionlib.php');
277 $completion = new completion_info($course);
278 $completion->set_module_viewed($cm);
280 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
281 $USER->editing = $edit;
284 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
286 /*The following piece of code is used in order
287 * to perform set_url correctly. It is necessary in order
288 * to make page_wiki_view class know that this page
289 * has been called via its id.
291 if ($id) {
292 $wikipage->set_coursemodule($id);
295 $wikipage->set_gid($currentgroup);
296 $wikipage->set_page($page);
298 if($pageid) {
299 add_to_log($course->id, 'wiki', 'view', "view.php?pageid=".$pageid, $pageid, $cm->id);
300 } else if($id) {
301 add_to_log($course->id, 'wiki', 'view', "view.php?id=".$id, $id, $cm->id);
302 } else if($wid && $title) {
303 add_to_log($course->id, 'wiki', 'view', "view.php?wid=".$wid."&title=".$title, $wid, $cm->id);
306 $wikipage->print_header();
307 $wikipage->print_content();
309 $wikipage->print_footer();