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
34require_once('../../config.php');
35require_once($CFG->dirroot . '/mod/wiki/lib.php');
36require_once($CFG->dirroot . '/mod/wiki/locallib.php');
37require_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
56/*
57 * Case 0:
58 *
59 * User that comes from a course. First wiki page must be shown
60 *
61 * URL params: id -> course module id
62 *
63 */
64if ($id) {
65    // Cheacking course module instance
66    if (!$cm = get_coursemodule_from_id('wiki', $id)) {
67        print_error('invalidcoursemodule');
68    }
69
70    // Checking course instance
71    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
72
73    require_course_login($course, true, $cm);
74
75    // Checking wiki instance
76    if (!$wiki = wiki_get_wiki($cm->instance)) {
77        print_error('incorrectwikiid', 'wiki');
78    }
79    $PAGE->set_cm($cm);
80
81    // Getting the subwiki corresponding to that wiki, group and user.
82    //
83    // Also setting the page if it exists or getting the first page title form
84    // that wiki
85
86    // Getting current group id
87    $currentgroup = groups_get_activity_group($cm);
88
89    // Getting current user id
90    if ($wiki->wikimode == 'individual') {
91        $userid = $USER->id;
92    } else {
93        $userid = 0;
94    }
95
96    // Getting subwiki. If it does not exists, redirecting to create page
97    if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
98        $params = array('wid' => $wiki->id, 'group' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
99        $url = new moodle_url('/mod/wiki/create.php', $params);
100        redirect($url);
101    }
102
103    // Getting first page. If it does not exists, redirecting to create page
104    if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
105        $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
106        $url = new moodle_url('/mod/wiki/create.php', $params);
107        redirect($url);
108    }
109
110    /*
111     * Case 1:
112     *
113     * A user wants to see a page.
114     *
115     * URL Params: pageid -> page id
116     *
117     */
118} elseif ($pageid) {
119
120    // Checking page instance
121    if (!$page = wiki_get_page($pageid)) {
122        print_error('incorrectpageid', 'wiki');
123    }
124
125    // Checking subwiki
126    if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
127        print_error('incorrectsubwikiid', 'wiki');
128    }
129
130    // Checking wiki instance of that subwiki
131    if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
132        print_error('incorrectwikiid', 'wiki');
133    }
134
135    // Checking course module instance
136    if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
137        print_error('invalidcoursemodule');
138    }
139
140    $currentgroup = $subwiki->groupid;
141
142    // Checking course instance
143    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
144
145    require_course_login($course, true, $cm);
146    /*
147     * Case 2:
148     *
149     * Trying to read a page from another group or user
150     *
151     * Page can exists or not.
152     *  * If it exists, page must be shown
153     *  * If it does not exists, system must ask for its creation
154     *
155     * URL params: wid -> subwiki id (required)
156     *             title -> a page title (required)
157     *             group -> group id (optional)
158     *             uid -> user id (optional)
159     *             groupanduser -> (optional)
160     */
161} elseif ($wid && $title) {
162
163    // Setting wiki instance
164    if (!$wiki = wiki_get_wiki($wid)) {
165        print_error('incorrectwikiid', 'wiki');
166    }
167
168    // Checking course module
169    if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
170        print_error('invalidcoursemodule');
171    }
172
173    // Checking course instance
174    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
175
176    require_course_login($course, true, $cm);
177
178    $groupmode = groups_get_activity_groupmode($cm);
179
180    // This is where people will land when they change groups using the drop-down selector.
181    // Set the activity group so tabs and content are shown correctly.
182    $currentgroup = groups_get_activity_group($cm, true);
183
184    if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
185        list($gid, $uid) = explode('-', $groupanduser);
186    } else if ($wiki->wikimode == 'individual') {
187        $gid = 0;
188        $uid = $userid;
189    } else if ($groupmode == NOGROUPS) {
190        $gid = 0;
191        $uid = 0;
192    } else {
193        $gid = $currentgroup;
194        $uid = 0;
195    }
196
197    // Getting subwiki instance. If it does not exists, redirect to create page
198    if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
199        $context = context_module::instance($cm->id);
200
201        $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
202        $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
203
204        $manage = has_capability('mod/wiki:managewiki', $context);
205        $edit = has_capability('mod/wiki:editpage', $context);
206        $manageandedit = $manage && $edit;
207
208        if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
209            print_error('nocontent','wiki');
210        }
211
212        $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $title);
213        $url = new moodle_url('/mod/wiki/create.php', $params);
214        redirect($url);
215    }
216
217    // Checking is there is a page with this title. If it does not exists, redirect to first page
218    if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
219        $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
220        // Check to see if the first page has been created
221        if (!wiki_get_page_by_title($subwiki->id, $wiki->firstpagetitle)) {
222            $url = new moodle_url('/mod/wiki/create.php', $params);
223        } else {
224            $url = new moodle_url('/mod/wiki/view.php', $params);
225        }
226        redirect($url);
227    }
228
229    //    /*
230    //     * Case 3:
231    //     *
232    //     * A user switches group when is 'reading' a non-existent page.
233    //     *
234    //     * URL Params: wid -> wiki id
235    //     *             title -> page title
236    //     *             currentgroup -> group id
237    //     *
238    //     */
239    //} elseif ($wid && $title && $currentgroup) {
240    //
241    //    // Checking wiki instance
242    //    if (!$wiki = wiki_get_wiki($wid)) {
243    //        print_error('incorrectwikiid', 'wiki');
244    //    }
245    //
246    //    // Checking subwiki instance
247    //    // @TODO: Fix call to wiki_get_subwiki_by_group
248    //    if (!$currentgroup = groups_get_activity_group($cm)){
249    //        $currentgroup = 0;
250    //    }
251    //    if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
252    //        print_error('incorrectsubwikiid', 'wiki');
253    //    }
254    //
255    //    // Checking page instance
256    //    if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
257    //        unset($title);
258    //    }
259    //
260    //    // Checking course instance
261    //    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
262    //
263    //    // Checking course module instance
264    //    if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
265    //        print_error('invalidcoursemodule');
266    //    }
267    //
268    //    $subwiki = null;
269    //    $page = null;
270    //
271    //    /*
272    //     * Case 4:
273    //     *
274    //     * Error. No more options
275    //     */
276} else {
277    print_error('invalidparameters', 'wiki');
278}
279
280if (!wiki_user_can_view($subwiki, $wiki)) {
281    print_error('cannotviewpage', 'wiki');
282}
283
284if (($edit != - 1) and $PAGE->user_allowed_editing()) {
285    $USER->editing = $edit;
286}
287
288$wikipage = new page_wiki_view($wiki, $subwiki, $cm);
289
290$wikipage->set_gid($currentgroup);
291$wikipage->set_page($page);
292
293$context = context_module::instance($cm->id);
294if ($pageid) {
295    wiki_page_view($wiki, $page, $course, $cm, $context, null, null, $subwiki);
296} else if ($id) {
297    wiki_view($wiki, $course, $cm, $context);
298} else if ($wid && $title) {
299    $other = array(
300        'title' => $title,
301        'wid' => $wid,
302        'group' => $gid,
303        'groupanduser' => $groupanduser
304    );
305    wiki_page_view($wiki, $page, $course, $cm, $context, $uid, $other, $subwiki);
306}
307
308$wikipage->print_header();
309$wikipage->print_content();
310
311$wikipage->print_footer();
312