MDL-78408 core: fix restoration of anchor to wantsurl during login
[moodle.git] / mod / wiki / view.php
blobc808af72f0b2e15d9f2eb899a0d5b298b65bfac8
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();
55 $PAGE->add_body_class('limitedwidth');
58 * Case 0:
60 * User that comes from a course. First wiki page must be shown
62 * URL params: id -> course module id
65 if ($id) {
66 // Cheacking course module instance
67 if (!$cm = get_coursemodule_from_id('wiki', $id)) {
68 throw new \moodle_exception('invalidcoursemodule');
71 // Checking course instance
72 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
74 require_course_login($course, true, $cm);
76 // Checking wiki instance
77 if (!$wiki = wiki_get_wiki($cm->instance)) {
78 throw new \moodle_exception('incorrectwikiid', 'wiki');
80 $PAGE->set_cm($cm);
82 // Getting the subwiki corresponding to that wiki, group and user.
84 // Also setting the page if it exists or getting the first page title form
85 // that wiki
87 // Getting current group id
88 $currentgroup = groups_get_activity_group($cm);
90 // Getting current user id
91 if ($wiki->wikimode == 'individual') {
92 $userid = $USER->id;
93 } else {
94 $userid = 0;
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);
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);
112 * Case 1:
114 * A user wants to see a page.
116 * URL Params: pageid -> page id
119 } elseif ($pageid) {
121 // Checking page instance
122 if (!$page = wiki_get_page($pageid)) {
123 throw new \moodle_exception('incorrectpageid', 'wiki');
126 // Checking subwiki
127 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
128 throw new \moodle_exception('incorrectsubwikiid', 'wiki');
131 // Checking wiki instance of that subwiki
132 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
133 throw new \moodle_exception('incorrectwikiid', 'wiki');
136 // Checking course module instance
137 if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
138 throw new \moodle_exception('invalidcoursemodule');
141 $currentgroup = $subwiki->groupid;
143 // Checking course instance
144 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
146 require_course_login($course, true, $cm);
148 * Case 2:
150 * Trying to read a page from another group or user
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
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)
162 } elseif ($wid && $title) {
164 // Setting wiki instance
165 if (!$wiki = wiki_get_wiki($wid)) {
166 throw new \moodle_exception('incorrectwikiid', 'wiki');
169 // Checking course module
170 if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
171 throw new \moodle_exception('invalidcoursemodule');
174 // Checking course instance
175 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
177 require_course_login($course, true, $cm);
179 $groupmode = groups_get_activity_groupmode($cm);
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);
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;
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);
202 $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
203 $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
205 $manage = has_capability('mod/wiki:managewiki', $context);
206 $edit = has_capability('mod/wiki:editpage', $context);
207 $manageandedit = $manage && $edit;
209 if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
210 throw new \moodle_exception('nocontent', 'wiki');
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);
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);
227 redirect($url);
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) {
242 // // Checking wiki instance
243 // if (!$wiki = wiki_get_wiki($wid)) {
244 // throw new \moodle_exception('incorrectwikiid', 'wiki');
245 // }
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 // }
256 // // Checking page instance
257 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
258 // unset($title);
259 // }
261 // // Checking course instance
262 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
264 // // Checking course module instance
265 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
266 // throw new \moodle_exception('invalidcoursemodule');
267 // }
269 // $subwiki = null;
270 // $page = null;
272 // /*
273 // * Case 4:
274 // *
275 // * Error. No more options
276 // */
277 } else {
278 throw new \moodle_exception('invalidparameters', 'wiki');
281 if (!wiki_user_can_view($subwiki, $wiki)) {
282 throw new \moodle_exception('cannotviewpage', 'wiki');
285 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
286 $USER->editing = $edit;
289 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
291 $wikipage->set_gid($currentgroup);
292 $wikipage->set_page($page);
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
306 wiki_page_view($wiki, $page, $course, $cm, $context, $uid, $other, $subwiki);
309 $wikipage->print_header();
310 $wikipage->print_content();
312 $wikipage->print_footer();