MDL-69115 course: More course management accessibility fixes
[moodle.git] / mod / wiki / view.php
blob423d8c296d2c6a5175e71457562e3e45c877b4df
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_course_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_course_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_course_login($course, true, $cm);
178 $groupmode = groups_get_activity_groupmode($cm);
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);
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;
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);
201 $modeanduser = $wiki->wikimode == 'individual' && $uid != $USER->id;
202 $modeandgroupmember = $wiki->wikimode == 'collaborative' && !groups_is_member($gid);
204 $manage = has_capability('mod/wiki:managewiki', $context);
205 $edit = has_capability('mod/wiki:editpage', $context);
206 $manageandedit = $manage && $edit;
208 if ($groupmode == VISIBLEGROUPS and ($modeanduser || $modeandgroupmember) and !$manageandedit) {
209 print_error('nocontent','wiki');
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);
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);
226 redirect($url);
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) {
241 // // Checking wiki instance
242 // if (!$wiki = wiki_get_wiki($wid)) {
243 // print_error('incorrectwikiid', 'wiki');
244 // }
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 // }
255 // // Checking page instance
256 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
257 // unset($title);
258 // }
260 // // Checking course instance
261 // $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
263 // // Checking course module instance
264 // if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
265 // print_error('invalidcoursemodule');
266 // }
268 // $subwiki = null;
269 // $page = null;
271 // /*
272 // * Case 4:
273 // *
274 // * Error. No more options
275 // */
276 } else {
277 print_error('invalidparameters', 'wiki');
280 if (!wiki_user_can_view($subwiki, $wiki)) {
281 print_error('cannotviewpage', 'wiki');
284 if (($edit != - 1) and $PAGE->user_allowed_editing()) {
285 $USER->editing = $edit;
288 $wikipage = new page_wiki_view($wiki, $subwiki, $cm);
290 $wikipage->set_gid($currentgroup);
291 $wikipage->set_page($page);
293 $context = context_module::instance($cm->id);
294 if ($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
305 wiki_page_view($wiki, $page, $course, $cm, $context, $uid, $other, $subwiki);
308 $wikipage->print_header();
309 $wikipage->print_content();
311 $wikipage->print_footer();