MDL-9628 Fixed typos and added language strings. Corrected a bug that prevented a...
[moodle-pu.git] / mod / wiki / view.php
blob36852ec3f521b40bff2728b808a12d4003021b8f
1 <?php // $Id$
2 /// Extended by Michael Schneider
3 /// This page prints a particular instance of wiki
5 global $CFG,$USER;
7 require_once("../../config.php");
8 require_once("lib.php");
9 #require_once("$CFG->dirroot/course/lib.php"); // For side-blocks
10 require_once($CFG->libdir . '/ajax/ajaxlib.php');
11 require_js(array('yui_yahoo','yui_connection'));
13 $ewiki_action = optional_param('ewiki_action', '', PARAM_ALPHA); // Action on Wiki-Page
14 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
15 $wid = optional_param('wid', 0, PARAM_INT); // Wiki ID
16 $page = optional_param('page', false); // Wiki Page Name
17 $q = optional_param('q',""); // Search Context
18 $userid = optional_param('userid', 0, PARAM_INT); // User wiki.
19 $groupid = optional_param('groupid', 0, PARAM_INT); // Group wiki.
20 $canceledit = optional_param('canceledit','', PARAM_ALPHA); // Editing has been cancelled
21 $cacheme = optional_param('allowcache', 1, PARAM_INT); // Set this to 0 to try and disable page caching.
23 // Only want to add edit log entries if we have made some changes ie submitted a form
24 $editsave = optional_param('thankyou', '');
26 if($page) {
27 // Split page command into action and page
28 $actions = explode('/', $page,2);
29 if(count($actions)==2) {
30 $pagename=$actions[1];
31 } else {
32 $pagename=$actions[0];
34 } else {
35 $actions=array('');
36 $pagename='';
39 if ($id) {
40 if (! $cm = get_coursemodule_from_id('wiki', $id)) {
41 error("Course Module ID was incorrect");
44 if (! $course = get_record("course", "id", $cm->course)) {
45 error("Course is misconfigured");
48 if (! $wiki = get_record("wiki", "id", $cm->instance)) {
49 error("Course module is incorrect");
52 } else {
53 if (! $wiki = get_record("wiki", "id", $wid)) {
54 error("Course module is incorrect");
56 if (! $course = get_record("course", "id", $wiki->course)) {
57 error("Course is misconfigured");
59 if (! $cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
60 error("Course Module ID was incorrect");
62 $id = $cm->id;
63 $_REQUEST["id"] = $id;
66 require_course_login($course, true, $cm);
68 /// Add the course module 'groupmode' to the wiki object, for easy access.
69 $wiki->groupmode = $cm->groupmode;
71 /// Default format:
72 $moodle_format=FORMAT_MOODLE;
74 ### SAVE ID from Moodle
75 $moodleID=@$_REQUEST["id"];
77 /// Globally disable CamelCase, if the option is selected for this wiki.
78 $moodle_disable_camel_case = ($wiki->disablecamelcase == 1);
80 if (($wiki_entry = wiki_get_default_entry($wiki, $course, $userid, $groupid))) {
81 // OK, now we know the entry ID, we can do lock etc.
83 // If true, we are 'really' on an editing page, not just on edit/something
84 $reallyedit=$actions[0]=='edit' && !$canceledit && !$editsave;
86 // Remove lock when we go to another wiki page (such as the cancel page)
87 if(!$reallyedit) {
88 wiki_release_lock($wiki_entry->id,$pagename);
89 } else if(array_key_exists('content',$_POST)) {
90 // Do not allow blank content because it causes problems (the wiki decides
91 // the page should automatically go into edit mode, but Moodle doesn't realise
92 // this and filters out the JS)
93 if($_POST['content']=='') {
94 $_POST['content']="\n";
95 $_REQUEST['content']="\n";
98 // We must have the edit lock in order to be permitted to save
99 list($ok,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
100 if(!$ok) {
101 $strsavenolock=get_string('savenolock','wiki');
102 error($strsavenolock,$CFG->wwwroot.'/mod/wiki/view.php?id='.$cm->id.'&page=view/'.urlencode($pagename));
106 /// ################# EWIKI Part ###########################
107 /// The wiki_entry->pagename is set to the specified value of the wiki,
108 /// or the default value in the 'lang' file if the specified value was empty.
109 define("EWIKI_PAGE_INDEX",$wiki_entry->pagename);
111 /// If the page has a ' in it, it may have slashes added to it. Remove them if it does.
112 $page = ($page === false) ? stripslashes(EWIKI_PAGE_INDEX) : stripslashes($page);
114 /// # Prevent ewiki getting id as PageID...
115 unset($_REQUEST["id"]);
116 unset($_GET["id"]);
117 unset($_POST["id"]);
118 unset($_POST["id"]);
119 unset($_SERVER["QUERY_STRING"]);
120 if (isset($HTTP_GET_VARS)) {
121 unset($HTTP_GET_VARS["id"]);
123 if (isset($HTTP_POST_VARS)) {
124 unset($HTTP_POST_VARS["id"]);
126 global $ewiki_title;
128 /// #-- predefine some of the configuration constants
131 /// EWIKI_NAME is defined in ewikimoodlelibs, so that also admin.php can use this
132 #define("EWIKI_NAME", $wiki_entry->pagename);
134 /// Search Hilighting
135 if($ewiki_title=="SearchPages") {
136 $qArgument="&amp;q=".urlencode($q);
139 /// Build the ewsiki script constant
140 /// ewbase will also be needed by EWIKI_SCRIPT_BINARY
141 $ewbase = 'view.php?id='.$moodleID;
142 if (isset($userid) && $userid!=0) $ewbase .= '&amp;userid='.$userid;
143 if (isset($groupid) && $groupid!=0) $ewbase .= '&amp;groupid='.$groupid;
144 $ewscript = $ewbase.'&amp;page=';
145 define("EWIKI_SCRIPT", $ewscript);
146 define("EWIKI_SCRIPT_URL", $ewscript);
148 /// # Settings for this specific Wiki
149 define("EWIKI_PRINT_TITLE", $wiki->ewikiprinttitle);
151 define("EWIKI_INIT_PAGES", wiki_content_dir($wiki));
153 /// # Moodle always addslashes to everything so we are going to strip them always
154 /// # to allow wiki itself to add them again. It's a triple add-strip-add but
155 /// # was the only way to solve the problem without modifying how the rest of
156 /// # the module works.
157 include($CFG->dirroot."/mod/wiki/ewiki/fragments/strip_wonderful_slashes.php");
159 if (ini_get("register_globals")) {
160 # include($CFG->dirroot."/mod/wiki/ewiki/fragments/strike_register_globals.php");
163 # Database Handler
164 include_once($CFG->dirroot."/mod/wiki/ewikimoodlelib.php");
165 # Plugins
166 //include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/email_protect.php");
167 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/patchsaving.php");
168 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/notify.php");
169 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/feature/imgresize_gd.php");
170 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_highlight.php");
171 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/f_fixhtml.php");
172 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wikinews.php");
173 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/sitemap.php");
174 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_wikidump.php");
175 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/backlinks.php");
176 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/css.php");
177 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/footnotes.php");
178 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/diff.php");
179 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/pageindex.php");
180 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/orphanedpages.php");
181 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wantedpages.php");
183 # Binary Handling
184 if($wiki->ewikiacceptbinary) {
185 define("EWIKI_UPLOAD_MAXSIZE", get_max_upload_file_size());
186 define("EWIKI_SCRIPT_BINARY", $ewbase."&binary=");
187 define("EWIKI_ALLOW_BINARY",1);
188 define("EWIKI_IMAGE_CACHING",1);
189 #define("EWIKI_AUTOVIEW",1);
190 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/lib/mime_magic.php");
191 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/downloads.php");
192 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/downloads.php");
193 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/db/binary_store.php");
194 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_binary_store.php");
195 } else {
196 define("EWIKI_SCRIPT_BINARY", 0);
197 define("EWIKI_ALLOW_BINARY",0);
200 # The mighty Wiki itself
201 include_once($CFG->dirroot."/mod/wiki/ewiki/ewiki.php");
203 if($canceledit) {
204 if ($delim = strpos($page, EWIKI_ACTION_SEP_CHAR)) {
205 @$page = substr($page, $delim + 1);
206 } else {
207 @$page="";
210 # Language-stuff: eWiki gets language from Browser. Lets correct it. Empty arrayelements do no harm
211 $ewiki_t["languages"]=array(current_language(), $course->lang, $CFG->lang,"en","c");
213 # Check Access Rights
214 $canedit = wiki_can_edit_entry($wiki_entry, $wiki, $USER, $course);
215 if (!$canedit) {
216 # Protected Mode
217 unset($ewiki_plugins["action"]["edit"]);
218 unset($ewiki_plugins["action"]["info"]);
221 # HTML Handling
222 $ewiki_use_editor=0;
223 if($wiki->htmlmode == 0) {
224 # No HTML
225 $ewiki_config["htmlentities"]=array(); // HTML is managed by moodle
226 $moodle_format=FORMAT_TEXT;
228 if($wiki->htmlmode == 1) {
229 # Safe HTML
230 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_rescue_html.php");
231 $moodle_format=FORMAT_HTML;
233 if($wiki->htmlmode == 2) {
234 # HTML Only
235 $moodle_format=FORMAT_HTML;
236 $ewiki_use_editor=1;
237 $ewiki_config["htmlentities"]=array(); // HTML is allowed
238 $ewiki_config["wiki_link_regex"] = "\007 [!~]?(
239 \#?\[[^<>\[\]\n]+\] |
240 \^[-".EWIKI_CHARS_U.EWIKI_CHARS_L."]{3,} |
241 \b([\w]{3,}:)*([".EWIKI_CHARS_U."]+[".EWIKI_CHARS_L."]+){2,}\#?[\w\d]* |
242 \w[-_.+\w]+@(\w[-_\w]+[.])+\w{2,} ) \007x";
245 global $ewiki_author, $USER;
246 $ewiki_author=fullname($USER);
247 $content=ewiki_page($page);
248 $content2='';
250 ### RESTORE ID from Moodle
251 $_REQUEST["id"]=$moodleID;
252 $id=$moodleID;
253 /// ################# EWIKI Part ###########################
255 else {
256 $content = '';
257 $content2 = '<div class="boxaligncenter">'.get_string('nowikicreated', 'wiki').'</div>';
261 # Group wiki, ...: No page and no ewiki_title
262 if(!isset($ewiki_title)) {
263 $ewiki_title="";
267 /// Moodle Log
268 if ($editsave != NULL) { /// We've submitted an edit and have been redirected back here
269 add_to_log($course->id, "wiki", 'edit',
270 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
271 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
272 } else if ($ewiki_action != 'edit') {
273 add_to_log($course->id, "wiki", $ewiki_action,
274 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
275 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
279 /// Print the page header
281 $strwikis = get_string("modulenameplural", "wiki");
282 $strwiki = get_string("modulename", "wiki");
284 $navlinks = array();
285 $navlinks[] = array('name' => $strwikis, 'link' => "index.php?id=$course->id", 'type' => 'activity');
286 $navlinks[] = array('name' => format_string($wiki->name,true), 'link' => "view.php?id=$moodleID", 'type' => 'activityinstace');
288 $navigation = build_navigation($navlinks);
290 print_header_simple($ewiki_title?$ewiki_title:format_string($wiki->name), "", $navigation,
291 "", "", $cacheme, update_module_button($cm->id, $course->id, $strwiki),
292 navmenu($course, $cm));
295 /// Print Page
296 echo ' <div id="wikiPageActions">
298 /// The top row contains links to other wikis, if applicable.
299 if ($wiki_entry && $wiki_list = wiki_get_other_wikis($wiki, $USER, $course, $wiki_entry->id)) {
300 //echo "wiki list ";print_r($wiki_list);
301 $selected="";
303 if (isset($wiki_list['selected'])) {
304 $selected = $wiki_list['selected'];
305 unset($wiki_list['selected']);
307 echo '<tr><td colspan="2">';
309 echo '<form id="otherwikis" action="'.$CFG->wwwroot.'/mod/wiki/view.php">';
310 echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>';
311 echo '<td class="sideblockheading">&nbsp;'
312 .$WIKI_TYPES[$wiki->wtype].' '
313 .get_string('modulename', 'wiki')." ".get_string('for',"wiki")." "
314 .wiki_get_owner($wiki_entry).':</td>';
316 echo '<td class="sideblockheading">'
317 .get_string('otherwikis', 'wiki').':&nbsp;&nbsp;';
318 $script = 'self.location=getElementById(\'otherwikis\').wikiselect.options[getElementById(\'otherwikis\').wikiselect.selectedIndex].value';
319 choose_from_menu($wiki_list, "wikiselect", $selected, "choose", $script);
320 echo '</td>';
321 echo '</tr></table>';
322 echo '</form>';
324 echo '</td>';
325 echo '</tr>';
328 if ($wiki_entry) {
329 $specialpages=array("WikiExport", "SiteMap", "SearchPages", "PageIndex","NewestPages","MostVisitedPages","MostOftenChangedPages","UpdatedPages","FileDownload","FileUpload","OrphanedPages","WantedPages");
330 /// Page Actions
331 echo '<table border="0" width="100%">';
332 echo '<tr>';
334 /// Searchform
335 echo '<td class="wikisearchform">';
336 wiki_print_search_form($cm->id, $q, $userid, $groupid, false);
337 echo '</td>';
339 /// Internal Wikilinks
340 echo '<td class="wikilinksblock">';
341 wiki_print_wikilinks_block($cm->id, $wiki->ewikiacceptbinary);
342 echo '</td>';
344 /// Administrative Links
345 if($canedit) {
346 echo '<td class="wikiadminactions">';
347 wiki_print_administration_actions($wiki, $cm->id, $userid, $groupid, $ewiki_title, $wiki->htmlmode!=2, $course);
348 echo '</td>';
351 /// Formatting Rules
352 echo '<td class="howtowiki">';
353 helpbutton('howtowiki', get_string('howtowiki', 'wiki'), 'wiki');
354 echo '</td>';
356 echo '</tr></table>';
359 echo '</div>
360 <div id="wiki-view" class="mwiki">
363 if($wiki_entry && $ewiki_title==$wiki_entry->pagename && !empty($wiki->summary)) {
364 if (trim(strip_tags($wiki->summary))) {
365 print_box(format_text($wiki->summary, FORMAT_MOODLE), 'generalbox', 'intro');
369 // The wiki Contents
371 if (!empty($canedit)) { /// Print tabs with commands for this page
372 $tabs = array('view', 'edit','links','info');
373 if ($wiki->ewikiacceptbinary) {
374 $tabs[] = 'attachments';
377 $tabrows = array();
378 $row = array();
379 $currenttab = '';
380 foreach ($tabs as $tab) {
381 $tabname = get_string("tab$tab", 'wiki');
382 $row[] = new tabobject($tabname, $ewbase.'&amp;page='.$tab.'/'.$ewiki_id, $tabname);
383 if ($ewiki_action == "$tab" or in_array($page, $specialpages)) {
384 $currenttab = $tabname;
387 $tabrows[] = $row;
389 print_tabs($tabrows, $currenttab);
392 /// Insert a link to force page refresh if new content isn't showing.
394 // build new URL + query string
395 $queries = preg_split('/[?&]/', me());
396 $nqueries = count($queries);
397 $me = $queries[0] . '?';
398 for($i=1; $i < $nqueries; $i++)
400 if( !strstr($queries[$i], 'allowcache') )
401 $me .= $queries[$i] . '&amp;';
403 $me .= 'allowcache=0';
405 // Insert the link
406 $linkdesc = get_string('reloadlinkdescription', 'wiki');
407 $linktext = get_string('reloadlinktext', 'wiki');
408 echo "<div class='wikilinkright'><a href='$me' title='$linkdesc'><input type='button' value='$linktext' /></a></div>";
410 print_simple_box_start('center', '100%', '', '20');
412 /// Don't filter any pages containing wiki actions (except view). A wiki page containing
413 /// actions will have the form [action]/[pagename]. If the action is 'view' or the '/'
414 /// isn't there (so the action defaults to 'view'), filter it.
415 /// If the page does not yet exist, the display will default to 'edit'.
416 if((count($actions) < 2 || $actions[0] == "view") && $wiki_entry &&
417 record_exists('wiki_pages', 'pagename', addslashes($page), 'wiki', $wiki_entry->id)) {
418 print(format_text($content, $moodle_format));
419 } else if($actions[0]=='edit' && $reallyedit) {
420 // Check the page isn't locked before printing out standard wiki content. (Locking
421 // is implemented as a wrapper over the existing wiki.)
422 list($gotlock,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
423 if(!$gotlock) {
424 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
425 $canoverridelock = has_capability('mod/wiki:overridelock', $modcontext);
427 $a=new stdClass;
428 $a->since=userdate($lock->lockedsince);
429 $a->seen=userdate($lock->lockedseen);
430 $user=get_record('user','id',$lock->lockedby);
431 $a->name=fullname($user,
432 has_capability('moodle/site:viewfullnames', $modcontext));
434 print_string('pagelocked','wiki',$a);
436 if($canoverridelock) {
437 $pageesc=htmlspecialchars($page);
438 $stroverrideinfo=get_string('overrideinfo','wiki');
439 $stroverridebutton=get_string('overridebutton','wiki');
440 $sesskey=sesskey();
441 print "
442 <form id='overridelock' method='post' action='overridelock.php'>
443 <div>
444 <input type='hidden' name='sesskey' value='$sesskey' />
445 <input type='hidden' name='id' value='$id' />
446 <input type='hidden' name='page' value='$pageesc' />
447 $stroverrideinfo
448 <input type='submit' value='$stroverridebutton' />
449 </div>
450 </form>
453 } else {
454 // OK, the page is now locked to us. Put in the AJAX for keeping the lock
455 $strlockcancelled=get_string('lockcancelled','wiki');
456 $strnojslockwarning=get_string('nojslockwarning','wiki');
457 $intervalms=WIKI_LOCK_RECONFIRM*1000;
458 print "
459 <script type='text/javascript'>
460 var intervalID;
461 function handleResponse(o) {
462 if(o.responseText=='cancel') {
463 document.forms['ewiki'].elements['preview'].disabled=true;
464 document.forms['ewiki'].elements['save'].disabled=true;
465 clearInterval(intervalID);
466 alert('$strlockcancelled');
469 function handleFailure(o) {
470 // Ignore for now
472 intervalID=setInterval(function() {
473 YAHOO.util.Connect.asyncRequest('POST','confirmlock.php',
474 {success:handleResponse,failure:handleFailure},'lockid={$lock->id}');
475 },$intervalms);
476 </script>
477 <noscript><p>
478 $strnojslockwarning
479 </p></noscript>
481 // Print editor etc
482 print $content;
484 } else {
485 print $content;
487 print $content2;
488 print_simple_box_end();
489 echo "<br />";
491 /// Finish the page
492 echo '
493 </div>
496 print_footer($course);