MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / wiki / view.php
blobf201cf92c397928c00597ceadcdc4e62dd086543
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 info to the wiki object, for easy access.
69 $wiki->groupmode = $cm->groupmode;
70 $wiki->groupingid = $cm->groupingid;
71 $wiki->groupmembersonly = $cm->groupmembersonly;
72 $wiki->cmid = $cm->id;
74 /// Default format:
75 $moodle_format=FORMAT_MOODLE;
77 ### SAVE ID from Moodle
78 $moodleID=@$_REQUEST["id"];
80 /// Globally disable CamelCase, if the option is selected for this wiki.
81 $moodle_disable_camel_case = ($wiki->disablecamelcase == 1);
83 if (($wiki_entry = wiki_get_default_entry($wiki, $course, $userid, $groupid))) {
84 // OK, now we know the entry ID, we can do lock etc.
86 // If true, we are 'really' on an editing page, not just on edit/something
87 $reallyedit=$actions[0]=='edit' && !$canceledit && !$editsave;
89 // Remove lock when we go to another wiki page (such as the cancel page)
90 if(!$reallyedit) {
91 wiki_release_lock($wiki_entry->id,$pagename);
92 } else if(array_key_exists('content',$_POST)) {
93 // Do not allow blank content because it causes problems (the wiki decides
94 // the page should automatically go into edit mode, but Moodle doesn't realise
95 // this and filters out the JS)
96 if($_POST['content']=='') {
97 $_POST['content']="\n";
98 $_REQUEST['content']="\n";
101 // We must have the edit lock in order to be permitted to save
102 list($ok,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
103 if(!$ok) {
104 $strsavenolock=get_string('savenolock','wiki');
105 error($strsavenolock,$CFG->wwwroot.'/mod/wiki/view.php?id='.$cm->id.'&page=view/'.urlencode($pagename));
109 /// ################# EWIKI Part ###########################
110 /// The wiki_entry->pagename is set to the specified value of the wiki,
111 /// or the default value in the 'lang' file if the specified value was empty.
112 define("EWIKI_PAGE_INDEX",$wiki_entry->pagename);
114 /// If the page has a ' in it, it may have slashes added to it. Remove them if it does.
115 $page = ($page === false) ? stripslashes(EWIKI_PAGE_INDEX) : stripslashes($page);
117 /// # Prevent ewiki getting id as PageID...
118 unset($_REQUEST["id"]);
119 unset($_GET["id"]);
120 unset($_POST["id"]);
121 unset($_POST["id"]);
122 unset($_SERVER["QUERY_STRING"]);
123 if (isset($HTTP_GET_VARS)) {
124 unset($HTTP_GET_VARS["id"]);
126 if (isset($HTTP_POST_VARS)) {
127 unset($HTTP_POST_VARS["id"]);
129 global $ewiki_title;
131 /// #-- predefine some of the configuration constants
134 /// EWIKI_NAME is defined in ewikimoodlelibs, so that also admin.php can use this
135 #define("EWIKI_NAME", $wiki_entry->pagename);
137 /// Search Hilighting
138 if($ewiki_title=="SearchPages") {
139 $qArgument="&amp;q=".urlencode($q);
142 /// Build the ewsiki script constant
143 /// ewbase will also be needed by EWIKI_SCRIPT_BINARY
144 $ewbase = 'view.php?id='.$moodleID;
145 if (isset($userid) && $userid!=0) $ewbase .= '&amp;userid='.$userid;
146 if (isset($groupid) && $groupid!=0) $ewbase .= '&amp;groupid='.$groupid;
147 $ewscript = $ewbase.'&amp;page=';
148 define("EWIKI_SCRIPT", $ewscript);
149 define("EWIKI_SCRIPT_URL", $ewscript);
151 /// # Settings for this specific Wiki
152 define("EWIKI_PRINT_TITLE", $wiki->ewikiprinttitle);
154 define("EWIKI_INIT_PAGES", wiki_content_dir($wiki));
156 /// # Moodle always addslashes to everything so we are going to strip them always
157 /// # to allow wiki itself to add them again. It's a triple add-strip-add but
158 /// # was the only way to solve the problem without modifying how the rest of
159 /// # the module works.
160 include($CFG->dirroot."/mod/wiki/ewiki/fragments/strip_wonderful_slashes.php");
162 if (ini_get("register_globals")) {
163 # include($CFG->dirroot."/mod/wiki/ewiki/fragments/strike_register_globals.php");
166 # Database Handler
167 include_once($CFG->dirroot."/mod/wiki/ewikimoodlelib.php");
168 # Plugins
169 //include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/email_protect.php");
170 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/patchsaving.php");
171 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/notify.php");
172 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/feature/imgresize_gd.php");
173 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_highlight.php");
174 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/f_fixhtml.php");
175 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wikinews.php");
176 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/sitemap.php");
177 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_wikidump.php");
178 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/backlinks.php");
179 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/css.php");
180 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/footnotes.php");
181 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/diff.php");
182 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/pageindex.php");
183 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/orphanedpages.php");
184 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wantedpages.php");
186 # Binary Handling
187 if($wiki->ewikiacceptbinary) {
188 define("EWIKI_UPLOAD_MAXSIZE", get_max_upload_file_size());
189 define("EWIKI_SCRIPT_BINARY", $ewbase."&binary=");
190 define("EWIKI_ALLOW_BINARY",1);
191 define("EWIKI_IMAGE_CACHING",1);
192 #define("EWIKI_AUTOVIEW",1);
193 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/lib/mime_magic.php");
194 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/downloads.php");
195 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/downloads.php");
196 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/db/binary_store.php");
197 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_binary_store.php");
198 } else {
199 define("EWIKI_SCRIPT_BINARY", 0);
200 define("EWIKI_ALLOW_BINARY",0);
203 # The mighty Wiki itself
204 include_once($CFG->dirroot."/mod/wiki/ewiki/ewiki.php");
206 if($canceledit) {
207 if ($delim = strpos($page, EWIKI_ACTION_SEP_CHAR)) {
208 @$page = substr($page, $delim + 1);
209 } else {
210 @$page="";
213 # Language-stuff: eWiki gets language from Browser. Lets correct it. Empty arrayelements do no harm
214 $ewiki_t["languages"]=array(current_language(), $course->lang, $CFG->lang,"en","c");
216 # Check Access Rights
217 $canedit = wiki_can_edit_entry($wiki_entry, $wiki, $USER, $course);
218 if (!$canedit) {
219 # Protected Mode
220 unset($ewiki_plugins["action"]["edit"]);
221 unset($ewiki_plugins["action"]["info"]);
224 # HTML Handling
225 $ewiki_use_editor=0;
226 if($wiki->htmlmode == 0) {
227 # No HTML
228 $ewiki_config["htmlentities"]=array(); // HTML is managed by moodle
229 $moodle_format=FORMAT_TEXT;
231 if($wiki->htmlmode == 1) {
232 # Safe HTML
233 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_rescue_html.php");
234 $moodle_format=FORMAT_HTML;
236 if($wiki->htmlmode == 2) {
237 # HTML Only
238 $moodle_format=FORMAT_HTML;
239 $ewiki_use_editor=1;
240 $ewiki_config["htmlentities"]=array(); // HTML is allowed
241 $ewiki_config["wiki_link_regex"] = "\007 [!~]?(
242 \#?\[[^<>\[\]\n]+\] |
243 \^[-".EWIKI_CHARS_U.EWIKI_CHARS_L."]{3,} |
244 \b([\w]{3,}:)*([".EWIKI_CHARS_U."]+[".EWIKI_CHARS_L."]+){2,}\#?[\w\d]* |
245 \w[-_.+\w]+@(\w[-_\w]+[.])+\w{2,} ) \007x";
248 global $ewiki_author, $USER;
249 $ewiki_author=fullname($USER);
250 $content=ewiki_page($page);
251 $content2='';
253 ### RESTORE ID from Moodle
254 $_REQUEST["id"]=$moodleID;
255 $id=$moodleID;
256 /// ################# EWIKI Part ###########################
258 else {
259 $content = '';
260 $content2 = '<div class="boxaligncenter">'.get_string('nowikicreated', 'wiki').'</div>';
264 # Group wiki, ...: No page and no ewiki_title
265 if(!isset($ewiki_title)) {
266 $ewiki_title="";
270 /// Moodle Log
271 if ($editsave != NULL) { /// We've submitted an edit and have been redirected back here
272 add_to_log($course->id, "wiki", 'edit',
273 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
274 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
275 } else if ($ewiki_action != 'edit') {
276 add_to_log($course->id, "wiki", $ewiki_action,
277 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
278 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
282 /// Print the page header
284 $strwikis = get_string("modulenameplural", "wiki");
285 $strwiki = get_string("modulename", "wiki");
287 $navlinks = array();
288 $navlinks[] = array('name' => $strwikis, 'link' => "index.php?id=$course->id", 'type' => 'activity');
289 $navlinks[] = array('name' => format_string($wiki->name,true), 'link' => "view.php?id=$moodleID", 'type' => 'activityinstace');
291 $navigation = build_navigation($navlinks);
293 print_header_simple($ewiki_title?$ewiki_title:format_string($wiki->name), "", $navigation,
294 "", "", $cacheme, update_module_button($cm->id, $course->id, $strwiki),
295 navmenu($course, $cm));
298 /// Print Page
299 echo ' <div id="wikiPageActions">
301 /// The top row contains links to other wikis, if applicable.
302 if ($wiki_entry && $wiki_list = wiki_get_other_wikis($wiki, $USER, $course, $wiki_entry->id)) {
303 //echo "wiki list ";print_r($wiki_list);
304 $selected="";
306 if (isset($wiki_list['selected'])) {
307 $selected = $wiki_list['selected'];
308 unset($wiki_list['selected']);
310 echo '<tr><td colspan="2">';
312 echo '<form id="otherwikis" action="'.$CFG->wwwroot.'/mod/wiki/view.php">';
313 echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>';
314 echo '<td class="sideblockheading">&nbsp;'
315 .$WIKI_TYPES[$wiki->wtype].' '
316 .get_string('modulename', 'wiki')." ".get_string('for',"wiki")." "
317 .wiki_get_owner($wiki_entry).':</td>';
319 echo '<td class="sideblockheading">'
320 .get_string('otherwikis', 'wiki').':&nbsp;&nbsp;';
321 $script = 'self.location=getElementById(\'otherwikis\').wikiselect.options[getElementById(\'otherwikis\').wikiselect.selectedIndex].value';
322 choose_from_menu($wiki_list, "wikiselect", $selected, "choose", $script);
323 echo '</td>';
324 echo '</tr></table>';
325 echo '</form>';
327 echo '</td>';
328 echo '</tr>';
331 if ($wiki_entry) {
332 $specialpages=array("WikiExport", "SiteMap", "SearchPages", "PageIndex","NewestPages","MostVisitedPages","MostOftenChangedPages","UpdatedPages","FileDownload","FileUpload","OrphanedPages","WantedPages");
333 /// Page Actions
334 echo '<table border="0" width="100%">';
335 echo '<tr>';
337 /// Searchform
338 echo '<td class="wikisearchform">';
339 wiki_print_search_form($cm->id, $q, $userid, $groupid, false);
340 echo '</td>';
342 /// Internal Wikilinks
343 echo '<td class="wikilinksblock">';
344 wiki_print_wikilinks_block($cm->id, $wiki->ewikiacceptbinary);
345 echo '</td>';
347 /// Administrative Links
348 if($canedit) {
349 echo '<td class="wikiadminactions">';
350 wiki_print_administration_actions($wiki, $cm->id, $userid, $groupid, $ewiki_title, $wiki->htmlmode!=2, $course);
351 echo '</td>';
354 /// Formatting Rules
355 echo '<td class="howtowiki">';
356 helpbutton('howtowiki', get_string('howtowiki', 'wiki'), 'wiki');
357 echo '</td>';
359 echo '</tr></table>';
362 echo '</div>
363 <div id="wiki-view" class="mwiki">
366 if($wiki_entry && $ewiki_title==$wiki_entry->pagename && !empty($wiki->summary)) {
367 if (trim(strip_tags($wiki->summary))) {
368 print_box(format_text($wiki->summary, FORMAT_MOODLE), 'generalbox', 'intro');
372 // The wiki Contents
374 if (!empty($canedit)) { /// Print tabs with commands for this page
375 $tabs = array('view', 'edit','links','info');
376 if ($wiki->ewikiacceptbinary) {
377 $tabs[] = 'attachments';
380 $tabrows = array();
381 $row = array();
382 $currenttab = '';
383 foreach ($tabs as $tab) {
384 $tabname = get_string("tab$tab", 'wiki');
385 $row[] = new tabobject($tabname, $ewbase.'&amp;page='.$tab.'/'.$ewiki_id, $tabname);
386 if ($ewiki_action == "$tab" or in_array($page, $specialpages)) {
387 $currenttab = $tabname;
390 $tabrows[] = $row;
392 print_tabs($tabrows, $currenttab);
395 /// Insert a link to force page refresh if new content isn't showing.
397 // build new URL + query string
398 $queries = preg_split('/[?&]/', me());
399 $nqueries = count($queries);
400 $me = $queries[0] . '?';
401 for($i=1; $i < $nqueries; $i++)
403 if( !strstr($queries[$i], 'allowcache') )
404 $me .= $queries[$i] . '&amp;';
406 $me .= 'allowcache=0';
408 // Insert the link
409 $linkdesc = get_string('reloadlinkdescription', 'wiki');
410 $linktext = get_string('reloadlinktext', 'wiki');
411 echo "<div class='wikilinkright'><a href='$me' title='$linkdesc'><input type='button' value='$linktext' /></a></div>";
413 print_simple_box_start('center', '100%', '', '20');
415 /// Don't filter any pages containing wiki actions (except view). A wiki page containing
416 /// actions will have the form [action]/[pagename]. If the action is 'view' or the '/'
417 /// isn't there (so the action defaults to 'view'), filter it.
418 /// If the page does not yet exist, the display will default to 'edit'.
419 if((count($actions) < 2 || $actions[0] == "view") && $wiki_entry &&
420 record_exists('wiki_pages', 'pagename', addslashes($page), 'wiki', $wiki_entry->id)) {
421 print(format_text($content, $moodle_format));
422 } else if($actions[0]=='edit' && $reallyedit) {
423 // Check the page isn't locked before printing out standard wiki content. (Locking
424 // is implemented as a wrapper over the existing wiki.)
425 list($gotlock,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
426 if(!$gotlock) {
427 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
428 $canoverridelock = has_capability('mod/wiki:overridelock', $modcontext);
430 $a=new stdClass;
431 $a->since=userdate($lock->lockedsince);
432 $a->seen=userdate($lock->lockedseen);
433 $user=get_record('user','id',$lock->lockedby);
434 $a->name=fullname($user,
435 has_capability('moodle/site:viewfullnames', $modcontext));
437 print_string('pagelocked','wiki',$a);
439 if($canoverridelock) {
440 $pageesc=htmlspecialchars($page);
441 $stroverrideinfo=get_string('overrideinfo','wiki');
442 $stroverridebutton=get_string('overridebutton','wiki');
443 $sesskey=sesskey();
444 print "
445 <form id='overridelock' method='post' action='overridelock.php'>
446 <div>
447 <input type='hidden' name='sesskey' value='$sesskey' />
448 <input type='hidden' name='id' value='$id' />
449 <input type='hidden' name='page' value='$pageesc' />
450 $stroverrideinfo
451 <input type='submit' value='$stroverridebutton' />
452 </div>
453 </form>
456 } else {
457 if (ajaxenabled()) {
458 // OK, the page is now locked to us. Put in the AJAX for keeping the lock
459 $strlockcancelled=get_string('lockcancelled','wiki');
460 $strnojslockwarning=get_string('nojslockwarning','wiki');
461 $intervalms=WIKI_LOCK_RECONFIRM*1000;
462 print "
463 <script type='text/javascript'>
464 var intervalID;
465 function handleResponse(o) {
466 if(o.responseText=='cancel') {
467 document.forms['ewiki'].elements['preview'].disabled=true;
468 document.forms['ewiki'].elements['save'].disabled=true;
469 clearInterval(intervalID);
470 alert('$strlockcancelled');
473 function handleFailure(o) {
474 // Ignore for now
476 intervalID=setInterval(function() {
477 YAHOO.util.Connect.asyncRequest('POST','confirmlock.php',
478 {success:handleResponse,failure:handleFailure},'lockid={$lock->id}');
479 },$intervalms);
480 </script>
481 <noscript><p>
482 $strnojslockwarning
483 </p></noscript>
486 // Print editor etc
487 print $content;
489 } else {
490 print $content;
492 print $content2;
493 print_simple_box_end();
494 echo "<br />";
496 /// Finish the page
497 echo '
498 </div>
501 print_footer($course);