3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This file contains all necessary code to view a wiki page
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
27 * @author David Jimenez
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');
60 * User that comes from a course. First wiki page must be shown
62 * URL params: id -> course module 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');
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
87 // Getting current group id
88 $currentgroup = groups_get_activity_group($cm);
90 // Getting current user id
91 if ($wiki->wikimode
== 'individual') {
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);
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);
114 * A user wants to see a page.
116 * URL Params: pageid -> page id
121 // Checking page instance
122 if (!$page = wiki_get_page($pageid)) {
123 throw new \
moodle_exception('incorrectpageid', 'wiki');
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);
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') {
190 } else if ($groupmode == NOGROUPS
) {
194 $gid = $currentgroup;
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);
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);
225 $url = new moodle_url('/mod/wiki/view.php', $params);
233 // * A user switches group when is 'reading' a non-existent page.
235 // * URL Params: wid -> wiki id
236 // * title -> page title
237 // * currentgroup -> group id
240 //} elseif ($wid && $title && $currentgroup) {
242 // // Checking wiki instance
243 // if (!$wiki = wiki_get_wiki($wid)) {
244 // throw new \moodle_exception('incorrectwikiid', 'wiki');
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;
252 // if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
253 // throw new \moodle_exception('incorrectsubwikiid', 'wiki');
256 // // Checking page instance
257 // if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
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');
275 // * Error. No more options
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
);
296 wiki_page_view($wiki, $page, $course, $cm, $context, null, null, $subwiki);
298 wiki_view($wiki, $course, $cm, $context);
299 } else if ($wid && $title) {
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();