MDL-58546 libraries: Patch ADOdb for PHP 7.2 compat
[moodle.git] / mod / wiki / view.php
blobd5e669b3c5f5cb6265bbd4a4a85d519d41f16c94
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
22 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyright 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
54 $PAGE->force_settings_menu();
57 * Case 0:
59 * User that comes from a course. First wiki page must be shown
61 * URL params: id -> course module id
64 if ($id) {
65 // Cheacking course module instance
66 if (!$cm = get_coursemodule_from_id('wiki', $id)) {
67 print_error('invalidcoursemodule');
70 // Checking course instance
71 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
73 require_login($course, true, $cm);
75 // Checking wiki instance
76 if (!$wiki = wiki_get_wiki($cm->instance)) {
77 print_error('incorrectwikiid', 'wiki');
79 $PAGE->set_cm($cm);
81 // Getting the subwiki corresponding to that wiki, group and user.
83 // Also setting the page if it exists or getting the first page title form
84 // that wiki
86 // Getting current group id
87 $currentgroup = groups_get_activity_group($cm);
89 // Getting current user id
90 if ($wiki->wikimode == 'individual') {
91 $userid = $USER->id;
92 } else {
93 $userid = 0;
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);
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);
111 * Case 1:
113 * A user wants to see a page.
115 * URL Params: pageid -> page id
118 } elseif ($pageid) {
120 // Checking page instance
121 if (!$page = wiki_get_page($pageid)) {
122 print_error('incorrectpageid', 'wiki');
125 // Checking subwiki
126 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
127 print_error('incorrectsubwikiid', 'wiki');
130 // Checking wiki instance of that subwiki
131 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
132 print_error('incorrectwikiid', 'wiki');
135 // Checking course module instance
136 if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
137 print_error('invalidcoursemodule');
140 $currentgroup = $subwiki->groupid;
142 // Checking course instance
143 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
145 require_login($course, true, $cm);
147 * Case 2:
149 * Trying to read a page from another group or user
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
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)
161 } elseif ($wid && $title) {
163 // Setting wiki instance
164 if (!$wiki = wiki_get_wiki($wid)) {
165 print_error('incorrectwikiid', 'wiki');
168 // Checking course module
169 if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
170 print_error('invalidcoursemodule');
173 // Checking course instance
174 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
176 require_login($course, true, $cm);
178 $groupmode = groups_get_activity_groupmode($cm);
180 if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
181 list($gid, $uid) = explode('-', $groupanduser);
182 } else if ($wiki->wikimode == 'individual') {
183 $gid = 0;
184 $uid = $userid;
185 } else if ($groupmode == NOGROUPS) {
186 $gid = 0;
187 $uid = 0;
188 } else {
189 $gid = $currentgroup;
190 $uid = 0;
193 // Getting subwiki instance. If it does not exists, redirect to create page
194 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
195 $context = context_module::instance($cm->id);
197 $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
198 $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
200 $manage = has_capability('mod/wiki:managewiki', $context);
201 $edit = has_capability('mod/wiki:editpage', $context);
202 $manageandedit = $manage && $edit;
204 if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
205 print_error('nocontent','wiki');
208 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $title);
209 $url = new moodle_url('/mod/wiki/create.php', $params);
210 redirect($url);
213 // Checking is there is a page with this title. If it does not exists, redirect to first page
214 if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
215 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
216 // Check to see if the first page has been created
217 if (!wiki_get_page_by_title($subwiki->id, $wiki->firstpagetitle)) {
218 $url = new moodle_url('/mod/wiki/create.php', $params);
219 } else {
220 $url = new moodle_url('/mod/wiki/view.php', $params);
222 redirect($url);
225 // /*
226 // * Case 3:
227 // *
228 // * A user switches group when is 'reading' a non-existent page.
229 // *
230 // * URL Params: wid -> wiki id
231 // * title -> page title
232 // * currentgroup -> group id
233 // *
234 // */
235 //} elseif ($wid && $title && $currentgroup) {
237 // // Checking wiki instance
238 // if (!$wiki = wiki_get_wiki($wid)) {
239 // print_error('incorrectwikiid', 'wiki');
240 // }
242 // // Checking subwiki instance
243 // // @TODO: Fix call to wiki_get_subwiki_by_group
244 // if (!$currentgroup = groups_get_activity_group($cm)){
245 // $currentgroup = 0;
246 // }
247 // if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
248 // print_error('incorrectsubwikiid', 'wiki');
249 // }
251 // // Checking page instance
252 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
253 // unset($title);
254 // }
256 // // Checking course instance
257 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
259 // // Checking course module instance
260 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
261 // print_error('invalidcoursemodule');
262 // }
264 // $subwiki = null;
265 // $page = null;
267 // /*
268 // * Case 4:
269 // *
270 // * Error. No more options
271 // */
272 } else {
273 print_error('invalidparameters', 'wiki');
276 if (!wiki_user_can_view($subwiki, $wiki)) {
277 print_error('cannotviewpage', 'wiki');
280 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
281 $USER->editing = $edit;
284 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
286 $wikipage->set_gid($currentgroup);
287 $wikipage->set_page($page);
289 $context = context_module::instance($cm->id);
290 if ($pageid) {
291 wiki_page_view($wiki, $page, $course, $cm, $context, null, null, $subwiki);
292 } else if ($id) {
293 wiki_view($wiki, $course, $cm, $context);
294 } else if ($wid && $title) {
295 $other = array(
296 'title' => $title,
297 'wid' => $wid,
298 'group' => $gid,
299 'groupanduser' => $groupanduser
301 wiki_page_view($wiki, $page, $course, $cm, $context, $uid, $other, $subwiki);
304 $wikipage->print_header();
305 $wikipage->print_content();
307 $wikipage->print_footer();