mirrored from git://git.moodle.org/moodle.git
/
view.php
307 lines (264 loc) · 9.18 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains all necessary code to view a wiki page
*
* @package mod-wiki-2.0
* @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
* @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
*
* @author Jordi Piguillem
* @author Marc Alier
* @author David Jimenez
* @author Josep Arus
* @author Kenneth Riba
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/wiki/lib.php');
require_once($CFG->dirroot . '/mod/wiki/locallib.php');
require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
$currentgroup = optional_param('group', 0, PARAM_INT); // Group ID
$userid = optional_param('userid', 0, PARAM_INT); // User ID
$title = optional_param('title', '', PARAM_TEXT); // Page Title
$action = optional_param('action', '', PARAM_ALPHA);
$id = optional_param('id', 0, PARAM_INT); // Course Module ID
$swid = optional_param('swid', 0, PARAM_INT); // Subwiki ID
$pageid = optional_param('pageid', 0, PARAM_INT); // Page ID
$wid = optional_param('wid', 0, PARAM_INT); // Wiki ID
$edit = optional_param('edit', -1, PARAM_BOOL);
/*
* Case 0:
*
* User that comes from a course. First wiki page must be shown
*
* URL params: id -> course module id
*
*/
if ($id) {
// Cheacking course module instance
if (!$cm = get_coursemodule_from_id('wiki', $id)) {
print_error('invalidcoursemodule');
}
// Checking course instance
if (!$course = get_course_by_id($cm->course)) {
print_error('coursemisconf');
}
// Checking wiki instance
if (!$wiki = wiki_get_wiki($cm->instance)) {
print_error('incorrectwikiid', 'wiki');
}
$PAGE->set_cm($cm);
// Getting the subwiki corresponding to that wiki, group and user.
//
// Also setting the page if it exists or getting the first page title form
// that wiki
// Getting current group id
$currentgroup = groups_get_activity_group($cm);
$currentgroup = !empty($currentgroup)?$currentgroup:0;
// Getting current user id
if ($wiki->wikimode == 'individual'){
if (empty($userid)){
$userid = $USER->id;
}
} else {
$userid = 0;
}
$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup, $userid);
$page = null;
if (!empty($subwiki)){
$page = wiki_get_first_page($subwiki->id, $wiki);
}
if (!empty($page)){
$pageid = $page->id;
} else {
// the first page doesn't exist, create first page automatically
// Then redirct to editing page
$page = null;
$title = $wiki->firstpagetitle;
$default = $wiki->defaultformat;
if (empty($subwiki)) {
if (!$swid = wiki_add_subwiki($wiki->id, $currentgroup, $userid)) {
print_error('invalidwikiid');
}
} else {
$swid = $subwiki->id;
}
$id = wiki_create_page($swid, $title, $default, $USER->id);
redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $id);
}
/*
* Case 1:
*
* A user wants to see a page.
*
* If group is set, system must show the page with the same name from another group.
* In this case, is probable that there is no version of that page for the
* given group
*
* URL Params: pageid -> page id
* group -> group id (optional)
*
*/
} elseif ($pageid) {
// Checking page instance
if (!$page = wiki_get_page($pageid)) {
print_error('incorrectpageid', 'wiki');
}
if (!empty($swid)){
// User wants to view another subwiki
if ($subwiki = wiki_get_subwiki($swid)){
// Trying to get the same page but from another subwiki
if (!$page = wiki_get_page_by_title($swid, $page->title)) {
// That page does not exists
// Getting the first page of that wiki
$wiki = wiki_get_wiki($subwiki->wikiid);
if (!$page = wiki_get_page_by_title($swid, $wiki->firstpagetitle)){
$url = new moodle_url('/mod/wiki/view.php', array('id'=>$subwiki->id));
print_error('individualpagedoesnotexist', 'wiki', $url->out());
}
}
} else {
print_error('incorrectsubwikiid', 'wiki');
}
} else if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
print_error('incorrectsubwikiid', 'wiki');
}
// Checking wiki instance of that subwiki
if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
print_error('incorrectwikiid', 'wiki');
}
// Checking course module instance
if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
print_error('invalidcoursemodule');
}
// Checking course instance
if (!$course = get_course_by_id($cm->course)) {
print_error('coursemisconf');
}
// Switching to the correct page and subwiki if group param is present
if ($currentgroup = groups_get_activity_group($cm)) {
if ($subwiki->groupid != $currentgroup) {
// Setting new subwiki instance
// @TODO: Fix call to wiki_get_subwiki_by_group
$subwiki = wiki_get_subwiki_by_group($wiki->id, $currentgroup);
// Setting new page instance or page title
$title = $page->title;
if ($page = wiki_get_page_by_title($subwiki->id, $page->title)) {
unset($title);
}
}
}
/*
* Case 2:
*
* Trying to read a page by using subwiki->id and title.
*
* Page can exists or not.
* * If it exists, page must be shown
* * If it does not exists, system must ask for its creation
*
* URL params: swid -> subwiki id
* title -> a page title
*/
} elseif ($swid && $title) {
// Getting subwiki instance
if (!$subwiki = wiki_get_subwiki($swid)) {
print_error('incorrectsubwikiid', 'wiki');
}
// Checking is there is a page with this title
if ($page = wiki_get_page_by_title($swid, $title)) {
unset($title);
}
// Setting wiki instance
if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
print_error('incorrectwikiid', 'wiki');
}
// Checking course module
if (!$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid)) {
print_error('invalidcoursemodule');
}
// Checking course instance
if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
}
/*
* Case 3:
*
* A user switches group when is 'reading' a non-existent page.
*
* URL Params: wid -> wiki id
* title -> page title
* currentgroup -> group id
*
*/
} elseif ($wid && $title && $currentgroup) {
// Checking wiki instance
if (!$wiki = wiki_get_wiki($wid)) {
print_error('incorrectwikiid', 'wiki');
}
// Checking subwiki instance
// @TODO: Fix call to wiki_get_subwiki_by_group
if (!$currentgroup = groups_get_activity_group($cm)){
$currentgroup = 0;
}
if (!$subwiki = wiki_get_subwiki_by_group($wid, $currentgroup)) {
print_error('incorrectsubwikiid', 'wiki');
}
// Checking page instance
if ($page = wiki_get_page_by_title($subwiki->id, $title)) {
unset($title);
}
// Checking course instance
if (!$course = get_course_by_id($wiki->course)) {
print_error('coursemisconf');
}
// Checking course module instance
if (!$cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
print_error('invalidcoursemodule');
}
$subwiki = null;
$page = null;
/*
* Case 4:
*
* Error. No more options
*/
} else {
print_error('incorrectparameters');
}
require_course_login($course, true, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/wiki:viewpage', $context);
add_to_log($course->id, 'wiki', 'view', 'view.php?id='.$cm->id, $wiki->id);
if (($edit != - 1) and $PAGE->user_allowed_editing()) {
$USER->editing = $edit;
}
$wikipage = new page_wiki_view($wiki, $subwiki, $cm);
/*The following piece of code is used in order
* to perform set_url correctly. It is necessary in order
* to make page_wiki_view class know that this page
* has been called via its id.
*/
if ($id) {
$wikipage->set_coursemodule($id);
}
$wikipage->set_gid($currentgroup);
$wikipage->set_page($page);
$wikipage->print_header();
$wikipage->print_content();
$wikipage->print_footer();