MDL-51569 mod_choice: Prevent users from updating choices with curl
[moodle.git] / mod / wiki / view.php
blob8d5f5128e9a2630907133d16cd4d0b9dda0be6c6
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
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 require_login($course, true, $cm);
73 // Checking wiki instance
74 if (!$wiki = wiki_get_wiki($cm->instance)) {
75 print_error('incorrectwikiid', 'wiki');
77 $PAGE->set_cm($cm);
79 // Getting the subwiki corresponding to that wiki, group and user.
81 // Also setting the page if it exists or getting the first page title form
82 // that wiki
84 // Getting current group id
85 $currentgroup = groups_get_activity_group($cm);
87 // Getting current user id
88 if ($wiki->wikimode == 'individual') {
89 $userid = $USER->id;
90 } else {
91 $userid = 0;
94 // Getting subwiki. If it does not exists, redirecting to create page
95 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid)) {
96 $params = array('wid' => $wiki->id, 'group' => $currentgroup, 'uid' => $userid, 'title' => $wiki->firstpagetitle);
97 $url = new moodle_url('/mod/wiki/create.php', $params);
98 redirect($url);
101 // Getting first page. If it does not exists, redirecting to create page
102 if (!$page = wiki_get_first_page($subwiki->id, $wiki)) {
103 $params = array('swid'=>$subwiki->id, 'title'=>$wiki->firstpagetitle);
104 $url = new moodle_url('/mod/wiki/create.php', $params);
105 redirect($url);
109 * Case 1:
111 * A user wants to see a page.
113 * URL Params: pageid -> page id
116 } elseif ($pageid) {
118 // Checking page instance
119 if (!$page = wiki_get_page($pageid)) {
120 print_error('incorrectpageid', 'wiki');
123 // Checking subwiki
124 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
125 print_error('incorrectsubwikiid', 'wiki');
128 // Checking wiki instance of that subwiki
129 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
130 print_error('incorrectwikiid', 'wiki');
133 // Checking course module instance
134 if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
135 print_error('invalidcoursemodule');
138 $currentgroup = $subwiki->groupid;
140 // Checking course instance
141 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
143 require_login($course, true, $cm);
145 * Case 2:
147 * Trying to read a page from another group or user
149 * Page can exists or not.
150 * * If it exists, page must be shown
151 * * If it does not exists, system must ask for its creation
153 * URL params: wid -> subwiki id (required)
154 * title -> a page title (required)
155 * group -> group id (optional)
156 * uid -> user id (optional)
157 * groupanduser -> (optional)
159 } elseif ($wid && $title) {
161 // Setting wiki instance
162 if (!$wiki = wiki_get_wiki($wid)) {
163 print_error('incorrectwikiid', 'wiki');
166 // Checking course module
167 if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id)) {
168 print_error('invalidcoursemodule');
171 // Checking course instance
172 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
174 require_login($course, true, $cm);
176 $groupmode = groups_get_activity_groupmode($cm);
178 if ($wiki->wikimode == 'individual' && ($groupmode == SEPARATEGROUPS || $groupmode == VISIBLEGROUPS)) {
179 list($gid, $uid) = explode('-', $groupanduser);
180 } else if ($wiki->wikimode == 'individual') {
181 $gid = 0;
182 $uid = $userid;
183 } else if ($groupmode == NOGROUPS) {
184 $gid = 0;
185 $uid = 0;
186 } else {
187 $gid = $currentgroup;
188 $uid = 0;
191 // Getting subwiki instance. If it does not exists, redirect to create page
192 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id, $gid, $uid)) {
193 $context = context_module::instance($cm->id);
195 $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
196 $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
198 $manage = has_capability('mod/wiki:managewiki', $context);
199 $edit = has_capability('mod/wiki:editpage', $context);
200 $manageandedit = $manage && $edit;
202 if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
203 print_error('nocontent','wiki');
206 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $title);
207 $url = new moodle_url('/mod/wiki/create.php', $params);
208 redirect($url);
211 // Checking is there is a page with this title. If it does not exists, redirect to first page
212 if (!$page = wiki_get_page_by_title($subwiki->id, $title)) {
213 $params = array('wid' => $wiki->id, 'group' => $gid, 'uid' => $uid, 'title' => $wiki->firstpagetitle);
214 // Check to see if the first page has been created
215 if (!wiki_get_page_by_title($subwiki->id, $wiki->firstpagetitle)) {
216 $url = new moodle_url('/mod/wiki/create.php', $params);
217 } else {
218 $url = new moodle_url('/mod/wiki/view.php', $params);
220 redirect($url);
223 // /*
224 // * Case 3:
225 // *
226 // * A user switches group when is 'reading' a non-existent page.
227 // *
228 // * URL Params: wid -> wiki id
229 // * title -> page title
230 // * currentgroup -> group id
231 // *
232 // */
233 //} elseif ($wid && $title && $currentgroup) {
235 // // Checking wiki instance
236 // if (!$wiki = wiki_get_wiki($wid)) {
237 // print_error('incorrectwikiid', 'wiki');
238 // }
240 // // Checking subwiki instance
241 // // @TODO: Fix call to wiki_get_subwiki_by_group
242 // if (!$currentgroup = groups_get_activity_group($cm)){
243 // $currentgroup = 0;
244 // }
245 // if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
246 // print_error('incorrectsubwikiid', 'wiki');
247 // }
249 // // Checking page instance
250 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
251 // unset($title);
252 // }
254 // // Checking course instance
255 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
257 // // Checking course module instance
258 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
259 // print_error('invalidcoursemodule');
260 // }
262 // $subwiki = null;
263 // $page = null;
265 // /*
266 // * Case 4:
267 // *
268 // * Error. No more options
269 // */
270 } else {
271 print_error('incorrectparameters');
274 if (!wiki_user_can_view($subwiki, $wiki)) {
275 print_error('cannotviewpage', 'wiki');
278 // Update 'viewed' state if required by completion system
279 require_once($CFG->libdir . '/completionlib.php');
280 $completion = new completion_info($course);
281 $completion->set_module_viewed($cm);
283 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
284 $USER->editing = $edit;
287 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
289 $wikipage->set_gid($currentgroup);
290 $wikipage->set_page($page);
292 $context = context_module::instance($cm->id);
293 if($pageid) {
294 $event = \mod_wiki\event\page_viewed::create(
295 array(
296 'context' => $context,
297 'objectid' => $pageid
300 $event->add_record_snapshot('wiki_pages', $page);
301 } else if($id) {
302 $event = \mod_wiki\event\course_module_viewed::create(
303 array(
304 'context' => $context,
305 'objectid' => $wiki->id
308 } else if($wid && $title) {
309 $event = \mod_wiki\event\page_viewed::create(
310 array(
311 'context' => $context,
312 'objectid' => $page->id,
313 'relateduserid' => $uid,
314 'other' => array(
315 'title' => $title,
316 'wid' => $wid,
317 'group' => $gid,
318 'groupanduser' => $groupanduser)
321 $event->add_record_snapshot('wiki_pages', $page);
323 $event->add_record_snapshot('course_modules', $cm);
324 $event->add_record_snapshot('course', $course);
325 $event->add_record_snapshot('wiki', $wiki);
326 $event->trigger();
328 $wikipage->print_header();
329 $wikipage->print_content();
331 $wikipage->print_footer();