forked from moodleou/moodle-mod_ouwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiindex.php
259 lines (230 loc) · 9.03 KB
/
wikiindex.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
<?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/>.
/**
* 'Wiki index' page. Displays an index of all pages in the wiki, in
* various formats.
*
* @copyright © 2007 The Open University
* @author s.marshall@open.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package ouwiki
*/
require_once(dirname(__FILE__) . '/../../config.php');
require($CFG->dirroot.'/mod/ouwiki/basicpage.php');
$treemode = optional_param('type', '', PARAM_ALPHA) == 'tree';
$id = required_param('id', PARAM_INT); // Course Module ID
$url = new moodle_url('/mod/ouwiki/wikiindex.php', array('id'=>$id));
$PAGE->set_url($url);
if ($id) {
if (!$cm = get_coursemodule_from_id('ouwiki', $id)) {
print_error('invalidcoursemodule');
}
// Checking course instance
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
if (!$ouwiki = $DB->get_record('ouwiki', array('id' => $cm->instance))) {
print_error('invalidcoursemodule');
}
$PAGE->set_cm($cm);
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$PAGE->set_pagelayout('incourse');
require_course_login($course, true, $cm);
$ouwikioutput = $PAGE->get_renderer('mod_ouwiki');
// Get basic wiki parameters
$wikiparams = ouwiki_display_wiki_parameters('', $subwiki, $cm);
// Do header
echo $ouwikioutput->ouwiki_print_start($ouwiki, $cm, $course, $subwiki, get_string('index', 'ouwiki'), $context, null, false);
// Print tabs for selecting index type
$tabrow = array();
$tabrow[] = new tabobject('alpha', 'wikiindex.php?'.$wikiparams,
get_string('tab_index_alpha', 'ouwiki'));
$tabrow[] = new tabobject('tree', 'wikiindex.php?'.$wikiparams.'&type=tree',
get_string('tab_index_tree', 'ouwiki'));
$tabs = array();
$tabs[] = $tabrow;
print_tabs($tabs, $treemode ? 'tree' : 'alpha');
print '<div id="ouwiki_belowtabs">';
global $orphans;
// Get actual index
$index = ouwiki_get_subwiki_index($subwiki->id);
$orphans = false;
if (count($index) == 0) {
print '<p>'.get_string('startpagedoesnotexist', 'ouwiki').'</p>';
} else if ($treemode) {
ouwiki_build_tree($index);
// Print out in hierarchical form...
print '<ul class="ouw_indextree">';
print ouwiki_tree_index(reset($index)->pageid, $index, $subwiki, $cm);
print '</ul>';
foreach ($index as $indexitem) {
if (count($indexitem->linksfrom) == 0 && $indexitem->title !== '') {
$orphans = true;
}
}
} else {
// ...or standard alphabetical
print '<ul class="ouw_index">';
foreach ($index as $indexitem) {
if (count($indexitem->linksfrom)!= 0 || $indexitem->title === '') {
print '<li>'.ouwiki_display_page_in_index($indexitem, $subwiki, $cm).'</li>';
} else {
$orphans = true;
}
}
print '</ul>';
}
if ($orphans) {
print '<h2 class="ouw_orphans">'.get_string('orphanpages', 'ouwiki').'</h2>';
print '<ul class="ouw_index">';
foreach ($index as $indexitem) {
if (count($indexitem->linksfrom) == 0 && $indexitem->title !== '') {
print '<li>'.ouwiki_display_page_in_index($indexitem, $subwiki, $cm).'</li>';
}
}
print '</ul>';
}
$missing = ouwiki_get_subwiki_missingpages($subwiki->id);
if (count($missing) > 0) {
print '<div class="ouw_missingpages"><h2>'.get_string('missingpages', 'ouwiki').'</h2>';
print '<p>'.get_string(count($missing) > 1 ? 'advice_missingpages' : 'advice_missingpage','ouwiki').'</p>';
print '<ul>';
$first = true;
foreach ($missing as $title => $from) {
print '<li>';
if ($first) {
$first = false;
} else {
print ' • ';
}
print '<a href="view.php?'.ouwiki_display_wiki_parameters($title, $subwiki, $cm).'">'.
htmlspecialchars($title).'</a> <span class="ouw_missingfrom">('.
get_string(count($from) > 1 ? 'frompages' : 'frompage','ouwiki',
'<a href="view.php?'.ouwiki_display_wiki_parameters($from[0], $subwiki, $cm).'">'.
($from[0] ? htmlspecialchars($from[0]) : get_string('startpage', 'ouwiki')).'</a>)</span>');
print '</li>';
}
print '</ul>';
print '</div>';
}
if (count($index) != 0) {
print '<div class="ouw_entirewiki"><h2>'.get_string('entirewiki', 'ouwiki').'</h2>';
print '<p>'.get_string('onepageview', 'ouwiki').'</p><ul>';
print '<li id="ouwiki_down_html"><a href="entirewiki.php?'.$wikiparams.'&format=html">'.
get_string('format_html', 'ouwiki').'</a></li>';
// Are there any files in this wiki?
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$result = $DB->get_records_sql("
SELECT
f.id
FROM
{ouwiki_subwikis} sw
JOIN {ouwiki_pages} p ON p.subwikiid = sw.id
JOIN {ouwiki_versions} v ON v.pageid = p.id
JOIN {files} f ON f.itemid = v.id
WHERE
sw.id = ? AND f.contextid = ? AND f.component = 'mod_ouwiki'
", array($subwiki->id, $context->id), 0, 1);
$anyfiles = count($result) > 0;
print $ouwikioutput->render_export_all_li($subwiki, $anyfiles);
if (has_capability('moodle/course:manageactivities', $context)) {
if (!$anyfiles) {
// No images or attachment files found.
print '<li id="ouwiki_down_template"><a href="entirewiki.php?' . $wikiparams . '&format=template">' .
get_string('format_template', 'ouwiki') . '</a></li>';
} else {
print '<li id="ouwiki_down_template">' . get_string('non_format_template', 'ouwiki') . '</li>';
}
}
print '</ul></div>';
}
// Footer
ouwiki_print_footer($course, $cm, $subwiki, $pagename);
/* Helper functions */
function ouwiki_display_page_in_index($indexitem, $subwiki, $cm) {
global $ouwiki;
if ($startpage = $indexitem->title === '') {
$title = get_string('startpage', 'ouwiki');
$output = '<div class="ouw_index_startpage">';
} else {
$title = $indexitem->title;
$output = '';
}
$output .= '<a class="ouw_title" href="view.php?'.
ouwiki_display_wiki_parameters($indexitem->title, $subwiki, $cm).
'">'.htmlspecialchars($title).'</a>';
$lastchange = new StdClass;
$lastchange->userlink = ouwiki_display_user($indexitem, $cm->course);
$lastchange->date = ouwiki_recent_span($indexitem->timecreated).ouwiki_nice_date($indexitem->timecreated).'</span>';
$output .= '<div class="ouw_indexinfo">';
if ($ouwiki->enablewordcount) {
$output .= '<span class="ouw_wordcount">'.get_string('numwords', 'ouwiki', $indexitem->wordcount).'</span>';
$output .= '<div class="spacer"></div>';
}
$output .= ' <span class="ouw_lastchange">'.get_string('lastchange', 'ouwiki', $lastchange).'</span>';
$output .= '</div>';
if ($startpage) {
$output .= '</div>';
}
return $output;
}
/**
* Builds the tree structure for the hierarchical index. This is basically
* a breadth-first search: we place each page on the nearest-to-home level
* that we can find for it.
*/
function ouwiki_build_tree(&$index) {
// Set up new data to fill
foreach ($index as $indexitem) {
$indexitem->linksto = array();
$indexitem->children = array();
}
// Preprocess: build links TO as well as FROM
foreach ($index as $indexitem) {
foreach ($indexitem->linksfrom as $fromid) {
$index[$fromid]->linksto[] = $indexitem->pageid;
}
}
// Begin with top level, which is first in results
reset($index);
$index[key($index)]->placed = true;
$nextlevel = array(reset($index)->pageid);
do {
$thislevel = $nextlevel;
$nextlevel = array();
foreach ($thislevel as $sourcepageid) {
foreach ($index[$sourcepageid]->linksto as $linkto) {
if (empty($index[$linkto]->placed)) {
$index[$linkto]->placed = true;
$index[$sourcepageid]->children[] = $linkto;
$nextlevel[] = $linkto;
}
}
}
} while (count($nextlevel) > 0);
}
function ouwiki_tree_index($pageid, &$index, $subwiki, $cm) {
$thispage = $index[$pageid];
$output = '<li>'.ouwiki_display_page_in_index($thispage, $subwiki, $cm);
if (count($thispage->children) > 0) {
$output .= '<ul>';
foreach ($thispage->children as $childid) {
$output .= ouwiki_tree_index($childid, $index, $subwiki, $cm);
}
$output .= '</ul>';
}
$output .= '</li>';
return $output;
}