MDL-22112, push wiki 2.0 into head
[moodle.git] / mod / wiki / pagelib.php
CommitLineData
00710f4c
DC
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * This file contains several classes uses to render the diferent pages
20 * of the wiki module
21 *
22 * @package mod-wiki-2.0
23 * @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
24 * @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
25 *
26 * @author Jordi Piguillem
27 * @author Marc Alier
28 * @author David Jimenez
29 * @author Josep Arus
30 * @author Daniel Serrano
31 * @author Kenneth Riba
32 *
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35
36require_once($CFG->dirroot . '/mod/wiki/edit_form.php');
37require_once($CFG->dirroot . '/tag/lib.php');
38
39/**
40 * Class page_wiki contains the common code between all pages
41 *
42 * @package mod-wiki
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45abstract class page_wiki {
46
47 /**
48 * @var object Current subwiki
49 */
50 protected $subwiki;
51
52 /**
53 * @var int Current page
54 */
55 protected $page;
56
57 /**
58 * @var string Current page title
59 */
60 protected $title;
61
62 /**
63 * @var int Current group ID
64 */
65 protected $gid;
66
67 /**
68 * @var array The tabs set used in wiki module
69 */
70 protected $tabs = array('view', 'edit', 'comments', 'history', 'map');
71 /**
72 * @var array tabs options
73 */
74 protected $tabs_options = array();
75 /**
76 * @var object wiki renderer
77 */
78 protected $wikioutput;
79
80 /**
81 * page_wiki constructor
82 *
83 * @param $wiki. Current wiki
84 * @param $subwiki. Current subwiki.
85 * @param $cm. Current course_module.
86 */
87 function __construct($wiki, $subwiki, $cm) {
88 global $PAGE, $CFG;
89 $this->subwiki = $subwiki;
90
91 // initialise wiki renderer
92 $this->wikioutput = $PAGE->get_renderer('mod_wiki');
93
94 $PAGE->set_focuscontrol('');
95 $PAGE->set_cacheable(true);
96 $PAGE->set_cm($cm);
97 $PAGE->set_activity_record($wiki);
98 // the search box
99 $PAGE->set_button(wiki_search_form($cm));
100 }
101
102 /**
103 * This method prints the top of the page.
104 */
105 function print_header() {
106 global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
107
108 $PAGE->set_heading($PAGE->course->fullname);
109
110 $this->set_url();
111
112 if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
113 $this->process_session_url();
114 }
115 $this->set_session_url();
116
117 $this->create_navbar();
118 $this->setup_tabs();
119
120 echo $OUTPUT->header();
121
122 echo $this->wikioutput->wiki_info();
123
124 // tabs are associated with pageid, so if page is empty, tabs should be disabled
125 if (!empty($this->page) && !empty($this->tabs)) {
126 echo $this->wikioutput->tabs($this->page, $this->tabs, $this->tabs_options);
127 }
128 }
129
130 /**
131 * Protected method to print current page title.
132 */
133 protected function print_pagetitle() {
134 global $OUTPUT;
135 $html = '';
136
137 $html .= $OUTPUT->container_start();
138 $html .= $OUTPUT->heading($this->title, 2, 'wiki_headingtitle');
139 $html .= $OUTPUT->container_end();
140 echo $html;
141 }
142
143 /**
144 * Setup page tabs, if options is empty, will set up active tab automatically
145 * @param array $options, tabs options
146 */
147 protected function setup_tabs($options = array()) {
148 if (empty($options)) {
149 $this->tabs_options = array('activetab' => substr(get_class($this), 10));
150 } else {
151 $this->tabs_options = $options;
152 }
153
154 }
155
156 /**
157 * This method must be overwritten to print the page content.
158 */
159 function print_content() {
160 throw new coding_exception('Page wiki class does not implement method print_content()');
161 }
162
163 /**
164 * Method to set the current page
165 *
166 * @param object $page Current page
167 */
168 function set_page($page) {
169 global $PAGE;
170
171 $this->page = $page;
172 $this->title = $page->title;
173 $PAGE->set_title($this->title);
174 }
175
176 /**
177 * Method to set the current page title.
178 * This method must be called when the current page is not created yet.
179 * @param string $title Current page title.
180 */
181 function set_title($title) {
182 global $PAGE;
183
184 $this->page = null;
185 $this->title = $title;
186 $PAGE->set_title($this->title);
187 }
188
189 /**
190 * Method to set current group id
191 * @param int $gid Current group id
192 */
193 function set_gid($gid) {
194 $this->gid = $gid;
195 }
196
197 /**
198 * Method to set the URL of the page.
199 * This method must be overwritten by every type of page.
200 */
201 protected function set_url() {
202 throw new coding_exception('Page wiki class does not implement method set_url()');
203 }
204
205 /**
206 * Protected method to create the common items of the navbar in every page type.
207 */
208 protected function create_navbar() {
209 global $PAGE, $CFG;
210
211 $PAGE->navbar->add($this->title, $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
212 }
213
214 /**
215 * This method print the footer of the page.
216 */
217 function print_footer() {
218 global $OUTPUT;
219 echo $OUTPUT->footer();
220 }
221
222 protected function process_session_url() {
223 global $USER, $SESSION;
224
225 //delete locks if edit
226 $url = $SESSION->wikipreviousurl;
227 switch ($url['page']) {
228 case 'edit':
229 wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
230 break;
231 }
232 }
233
234 protected function set_session_url() {
235 global $SESSION;
236 unset($SESSION->wikipreviousurl);
237 }
238
239}
240
241/**
242 * View a wiki page
243 *
244 * @package mod-wiki
245 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
246 */
247class page_wiki_view extends page_wiki {
248 /**
249 * @var int the coursemodule id
250 */
251 private $coursemodule;
252
253 function print_header() {
254 global $PAGE;
255
256 parent::print_header();
257
258 $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page);
259
260 if (!empty($this->page)) {
261 echo $this->wikioutput->prettyview_link($this->page);
262 }
263
264 //echo $this->wikioutput->page_index();
265
266 $this->print_pagetitle();
267 }
268
269 function print_content() {
270 global $PAGE, $CFG;
271
272 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
273 if (wiki_user_can_view($this->subwiki)) {
274
275 if (!empty($this->page)) {
276 wiki_print_page_content($this->page, $context, $this->subwiki->id);
277 $wiki = $PAGE->activityrecord;
278 } else {
279 print_string('nocontent', 'wiki');
280 // TODO: fix this part
281 $swid = 0;
282 if (!empty($this->subwiki)) {
283 $swid = $this->subwiki->id;
284 }
285 }
286 } else {
287 // @TODO: Tranlate it
288 echo "You can not view this page";
289 }
290 }
291
292 function set_url() {
293 global $PAGE, $CFG;
294 $params = array();
295
296 if (isset($this->coursemodule)) {
297 $params['id'] = $this->coursemodule;
298 } else if (!empty($this->page) and $this->page != null) {
299 $params['pageid'] = $this->page->id;
300 } else if (!empty($this->gid)) {
301 $params['wid'] = $PAGE->cm->instance;
302 $params['group'] = $this->gid;
303 } else if (!empty($this->title)) {
304 $params['swid'] = $this->subwiki->id;
305 $params['title'] = $this->title;
306 } else {
307 print_error(get_string('invalidparameters', 'wiki'));
308 }
309
310 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/view.php', $params);
311 }
312
313 function set_coursemodule($id) {
314 $this->coursemodule = $id;
315 }
316
317 protected function create_navbar() {
318 global $PAGE, $CFG;
319
320 $PAGE->navbar->add($this->title);
321 $PAGE->navbar->add(get_string('view', 'wiki'));
322 }
323}
324
325/**
326 * Wiki page editing page
327 *
328 * @package mod-wiki
329 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
330 */
331class page_wiki_edit extends page_wiki {
332
333 public static $attachmentoptions;
334
335 protected $sectioncontent;
336 /** @var string the section name needed to be edited */
337 protected $section;
338 protected $overridelock = false;
339 protected $versionnumber = -1;
340 protected $upload = false;
341 protected $attachments = 0;
342 protected $deleteuploads = array();
343 protected $format;
344
345 function __construct($wiki, $subwiki, $cm) {
346 global $CFG, $PAGE;
347 parent::__construct($wiki, $subwiki, $cm);
348 self::$attachmentoptions = array(
349 'subdirs' => false,
350 'maxfiles' => -1,
351 'maxbytes' => $CFG->maxbytes,
352 'accepted_types'=>'*'
353 );
354 $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
355 $PAGE->requires->yui2_lib('connection');
356 }
357
358 protected function print_pagetitle() {
359 global $OUTPUT;
360
361 $title = $this->title;
362 if (isset($this->section)) {
363 $title .= ' : ' . $this->section;
364 }
365 echo $OUTPUT->container_start('wiki_clear');
366 echo $OUTPUT->heading($title, 2, 'wiki_headingtitle');
367 echo $OUTPUT->container_end();
368 }
369
370 function print_header() {
371 global $OUTPUT, $PAGE;
372 $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
373
374 parent::print_header();
375
376 $this->print_pagetitle();
377
378 print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
379 }
380
381 function print_content() {
382 global $PAGE;
383
384 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
385 if (wiki_user_can_edit($this->subwiki)) {
386 $this->print_edit();
387 } else {
388 // @TODO: Translate it
389 echo "You can not edit this page";
390 }
391 }
392
393 protected function set_url() {
394 global $PAGE, $CFG;
395
396 $params = array('pageid' => $this->page->id);
397
398 if (isset($this->section)) {
399 $params['section'] = $this->section;
400 }
401
402 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
403 }
404
405 protected function set_session_url() {
406 global $SESSION;
407
408 $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
409 }
410
411 protected function process_session_url() {
412 }
413
414 function set_section($sectioncontent, $section) {
415 $this->sectioncontent = $sectioncontent;
416 $this->section = $section;
417 }
418
419 public function set_versionnumber($versionnumber) {
420 $this->versionnumber = $versionnumber;
421 }
422
423 public function set_overridelock($override) {
424 $this->overridelock = $override;
425 }
426
427 function set_format($format) {
428 $this->format = $format;
429 }
430
431 public function set_upload($upload) {
432 $this->upload = $upload;
433 }
434
435 public function set_attachments($attachments) {
436 $this->attachments = $attachments;
437 }
438
439 public function set_deleteuploads($deleteuploads) {
440 $this->deleteuploads = $deleteuploads;
441 }
442
443 protected function create_navbar() {
444 global $PAGE, $CFG;
445
446 parent::create_navbar();
447
448 $PAGE->navbar->add(get_string('edit', 'wiki'));
449 }
450
451 protected function check_locks() {
452 global $OUTPUT, $USER, $CFG;
453
454 if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
455 print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
456
457 if ($this->overridelock) {
458 $params = 'pageid=' . $this->page->id;
459
460 if ($this->section) {
461 $params .= '&section=' . $this->section;
462 }
463
464 $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
465 $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
466 $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
467 $form .= '</form>';
468
469 print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
470 }
471 return false;
472 }
473 return true;
474 }
475
476 protected function print_edit($content = null) {
477 global $CFG, $OUTPUT, $USER, $PAGE;
478
479 if (!$this->check_locks()) {
480 return;
481 }
482
483 //delete old locks (> 1 hour)
484 wiki_delete_old_locks();
485
486 $version = wiki_get_current_version($this->page->id);
487 $format = $version->contentformat;
488
489 if ($content == null) {
490 if (empty($this->section)) {
491 $content = $version->content;
492 } else {
493 $content = $this->sectioncontent;
494 }
495 }
496
497 $versionnumber = $version->version;
498 if ($this->versionnumber >= 0) {
499 if ($version->version != $this->versionnumber) {
500 print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
501 $versionnumber = $this->versionnumber;
502 }
503 }
504
505 $cm = $PAGE->cm;
506 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
507
508 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
509 if (!empty($this->section)) {
510 $url .= "&section=" . $this->section;
511 }
512
513 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $version->contentformat, 'version' => $versionnumber);
514
515 $data = new StdClass();
516 $data->newcontent = $content;
517 $data->version = $versionnumber;
518 $data->format = $format;
519
520 switch ($format) {
521 case 'html':
522 $data->newcontentformat = FORMAT_HTML;
523 $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'wiki_attachments', $this->subwiki->id);
524 break;
525 default:
526 //$draftitemid = file_get_submitted_draft_itemid('attachments');
527 //file_prepare_draft_area($draftitemid, $context->id, 'wiki_attachments', $this->subwiki->id);
528 //$data->attachments = $draftitemid;
529 }
530
531 if ($version->contentformat != 'html') {
532 $params['contextid'] = $context->id;
533 $params['filearea'] = 'wiki_attachments';
534 $params['fileitemid'] = $this->subwiki->id;
535 }
536
537 $form = new mod_wiki_edit_form($url, $params);
538
539
540 if ($formdata = $form->get_data()) {
541 if ($format != 'html') {
542 $errors = $this->process_uploads($context);
543 if (!empty($errors)) {
544 $contenterror = "";
545 foreach ($errors as $e) {
546 $contenterror .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
547 }
548 print $OUTPUT->box($contenterror, 'errorbox');
549 }
550 }
551 if (!empty($CFG->usetags)) {
552 $data->tags = $formdata->tags;
553 }
554 } else {
555 if (!empty($CFG->usetags)) {
556 $data->tags = tag_get_tags_array('wiki', $this->page->id);
557 }
558 }
559
560 $form->set_data($data);
561 $form->display();
562 }
563
564 protected function process_uploads($context) {
565 global $PAGE, $OUTPUT;
566
567 if ($this->upload) {
568 file_save_draft_area_files($this->attachments, $context->id, 'wiki_attachments', $this->subwiki->id);
569 return null;
570 //return wiki_process_attachments($this->attachments, $this->deleteuploads, $context->id, 'wiki_attachments', $this->subwiki->id);
571 }
572 }
573}
574
575/**
576 * Class that models the behavior of wiki's view comments page
577 *
578 * @package mod-wiki
579 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
580 */
581class page_wiki_comments extends page_wiki {
582
583 function print_header() {
584
585 parent::print_header();
586
587 $this->print_pagetitle();
588
589 }
590
591 function print_content() {
592 global $CFG, $OUTPUT, $USER, $PAGE, $COURSE;
593 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
594
595 $page = $this->page;
596 $subwiki = $this->subwiki;
597 $wiki = $PAGE->activityrecord;
598 $cm = $PAGE->cm;
599 $course = $COURSE;
600 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
601
602 require_capability('mod/wiki:viewcomment', $context, NULL, true, 'noviewcommentpermission', 'wiki');
603
604 $comments = wiki_get_comments($context, $page->id);
605
606 if (has_capability('mod/wiki:editcomment', $context)) {
607 echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&amp;pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
608 }
609
610 $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
611 $version = wiki_get_current_version($this->page->id);
612 $format = $version->contentformat;
613
614 if (empty($comments)) {
615 echo $OUTPUT->heading(get_string('nocomments', 'wiki'));
616 }
617
618 foreach ($comments as $num) {
619
620 $user = wiki_get_user_info($num->userid);
621
622 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
623 $by = new stdclass();
624 $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . $course->id . '">' . $fullname . '</a>';
625 $by->date = userdate($num->timecreated);
626
627 $t = new html_table();
628 $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
629 $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
630 $cell3 = new html_table_cell();
631 $cell3->atributtes ['width'] = "80%";
632 $cell4 = new html_table_cell();
633 $cell5 = new html_table_cell();
634
635 $row1 = new html_table_row();
636 $row1->cells[] = $cell1;
637 $row1->cells[] = $cell2;
638 $row2 = new html_table_row();
639 $row2->cells[] = $cell3;
640
641 if ($format != 'html') {
642 if ($format == 'creole') {
643 $parsedcontent = wiki_parse_content('creole', $num->content, $options);
644 } else if ($format == 'nwiki') {
645 $parsedcontent = wiki_parse_content('nwiki', $num->content, $options);
646 }
647
648 $cell4->text = html_entity_decode($parsedcontent['parsed_text']);
649 } else {
650 $cell4->text = $num->content;
651 }
652
653 $row2->cells[] = $cell4;
654
655 if ((has_capability('mod/wiki:managecomment', $context)) and ($USER->id == $num->userid)) {
656
657 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $num->id, 'pageid' => $page->id, 'action' => 'edit'));
658 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $num->id, 'pageid' => $page->id, 'action' => 'delete'));
659 $cell6 = new html_table_cell($OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit'))) . $OUTPUT->action_icon($urldelet, new pix_icon('t/delete', get_string('delete'))));
660 $row3 = new html_table_row();
661 $row3->cells[] = $cell5;
662 $row3->cells[] = $cell6;
663 $t->data = array($row1, $row2, $row3);
664
665 } else {
666 $t->data = array($row1, $row2);
667 }
668
669 echo html_writer::table($t);
670
671 }
672 }
673
674 function set_url() {
675 global $PAGE, $CFG;
676 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
677 }
678
679 protected function create_navbar() {
680 global $PAGE, $CFG;
681
682 parent::create_navbar();
683 $PAGE->navbar->add(get_string('comments', 'wiki'));
684 }
685
686}
687
688/**
689 * Class that models the behavior of wiki's edit comment
690 *
691 * @package mod-wiki
692 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
693 */
694class page_wiki_editcomment extends page_wiki {
695 private $comment;
696 private $action;
697 private $form;
698 private $format;
699
700 function set_url() {
701 global $PAGE, $CFG;
702 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
703 }
704
705 function print_header() {
706 parent::print_header();
707 $this->print_pagetitle();
708 }
709
710 function print_content() {
711 global $PAGE;
712
713 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
714 require_capability('mod/wiki:editcomment', $context, NULL, true, 'noeditcommentpermission', 'wiki');
715
716 if ($this->action == 'add') {
717 $this->add_comment_form();
718 } else if ($this->action == 'edit') {
719 $this->edit_comment_form($this->comment);
720 }
721 }
722
723 function set_action($action, $comment) {
724 global $CFG;
725 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
726
727 $this->action = $action;
728 $this->comment = $comment;
729 $version = wiki_get_current_version($this->page->id);
730 $this->format = $version->contentformat;
731
732 if ($this->format == 'html') {
733 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
734 $this->form = new mod_wiki_comments_form($destination);
735 }
736 }
737
738 protected function create_navbar() {
739 global $PAGE, $CFG;
740
741 $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
742
743 if ($this->action == 'add') {
744 $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
745 } else {
746 $PAGE->navbar->add(get_string('editcomment', 'wiki'));
747 }
748 }
749
750 protected function setup_tabs() {
751 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
752 }
753
754 private function add_comment_form() {
755 global $CFG;
756 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
757
758 $pageid = $this->page->id;
759
760 if ($this->format == 'html') {
761 $this->form->display();
762 } else {
763 wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'fromcomments');
764 }
765 }
766
767 private function edit_comment_form($com) {
768 global $CFG;
769 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
770 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
771
772 if ($this->format == 'html') {
773 $commentoptions = array('trusttext' => true, 'maxfiles' => 0);
774 $com->action = 'edit';
775 $com->entrycomment_editor['text'] = $com->content;
776
777 $this->form->set_data($com, $commentoptions);
778 $this->form->display();
779 } else {
780 $action = 'edit';
781 $commentid = $com->id;
782 $pageid = $this->page->id;
783 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=' . $action;
784 wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'fromcomments', $commentid);
785 }
786
787 }
788
789}
790
791/**
792 * Wiki page search page
793 *
794 * @package mod-wiki
795 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
796 */
797class page_wiki_search extends page_wiki {
798 private $search_result;
799
800 protected function create_navbar() {
801 global $PAGE, $CFG;
802
803 $PAGE->navbar->add($PAGE->activityrecord->name, $CFG->wwwroot . '/mod/wiki/view.php?id=' . $PAGE->cm->id);
804 }
805
806 function set_search_string($search, $searchcontent) {
807 $swid = $this->subwiki->id;
808 if ($searchcontent) {
809 $this->search_result = wiki_search_all($swid, $search);
810 } else {
811 $this->search_result = wiki_search_title($swid, $search);
812 }
813
814 }
815
816 function set_url() {
817 global $PAGE, $CFG;
818 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
819 }
820 function print_content() {
821 global $PAGE;
822
823 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
824 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
825
826 echo $this->wikioutput->search_result($this->search_result);
827 }
828}
829
830/**
831 *
832 * Class that models the behavior of wiki's
833 * create page
834 *
835 */
836class page_wiki_create extends page_wiki {
837
838 private $format;
839 private $swid;
840 private $action;
841 private $mform;
842
843 function print_header() {
844 $this->set_url();
845 parent::print_header();
846 }
847
848 function set_url() {
849 global $PAGE, $CFG;
850
851 $params = array();
852 if ($this->action == 'new') {
853 $params['action'] = 'new';
854 $params['swid'] = $this->swid;
855 if ($this->title != get_string('newpage', 'wiki')) {
856 $params['title'] = $this->title;
857 }
858 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
859 } else {
860 $params['action'] = 'create';
861 $params['swid'] = $this->swid;
862 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
863 }
864 }
865
866 function set_format($format) {
867 $this->format = $format;
868 }
869
870 function set_swid($swid) {
871 $this->swid = $swid;
872 }
873
874 function set_action($action) {
875 $this->action = $action;
876
877 require_once(dirname(__FILE__) . '/create_form.php');
878 $url = new moodle_url('/mod/wiki/create.php', array('action' => 'create', 'swid' => $this->swid));
879 $formats = wiki_get_formats();
880 $options = array('formats' => $formats);
881 if ($this->title != get_string('newpage', 'wiki')) {
882 $options['disable_pagetitle'] = true;
883 }
884 $this->mform = new mod_wiki_create_form($url->out(false), $options);
885 }
886
887 protected function create_navbar() {
888 global $PAGE;
889
890 $PAGE->navbar->add($this->title);
891 }
892
893 function print_content($pagetitle = '') {
894 global $PAGE;
895
896 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
897 $wiki = wiki_get_wiki($this->swid);
898
899 // @TODO: Change this to has_capability and show an alternative interface.
900 require_capability('mod/wiki:createpage', $context, NULL, true, 'nocreatepermission', 'wiki');
901 $data = new stdclass;
902 if (!empty($pagetitle)) {
903 $data->pagetitle = $pagetitle;
904 }
905 $data->pageformat = $PAGE->activityrecord->defaultformat;
906
907 $this->mform->set_data($data);
908 $this->mform->display();
909 }
910
911 function create_page() {
912 global $USER, $CFG;
913 $data = $this->mform->get_data();
914 $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
915 redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $id);
916 }
917}
918
919class page_wiki_preview extends page_wiki_edit {
920
921 private $newcontent;
922
923 function __construct($wiki, $subwiki, $cm) {
924 global $PAGE, $CFG, $OUTPUT;
925 parent::__construct($wiki, $subwiki, $cm);
926 $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
927 $PAGE->set_button($buttons);
928 }
929
930 function print_header() {
931 global $PAGE, $CFG;
932
933 parent::print_header();
934
935 }
936
937 function print_content() {
938 global $PAGE;
939
940 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
941 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
942
943 $this->print_preview();
944 }
945
946 function set_newcontent($newcontent) {
947 $this->newcontent = $newcontent;
948 }
949
950 function set_url() {
951 global $PAGE, $CFG;
952
953 $params = array('pageid' => $this->page->id
954 );
955
956 if (isset($this->section)) {
957 $params['section'] = $this->section;
958 }
959
960 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
961 }
962
963 protected function setup_tabs() {
964 parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
965 }
966
967 protected function check_locks() {
968 return true;
969 }
970
971 protected function print_preview() {
972 global $CFG, $OUTPUT;
973
974 $version = wiki_get_current_version($this->page->id);
975 $format = $version->contentformat;
976 $content = $version->content;
977
978 $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
979
980 $parseroutput = wiki_parse_content($format, $this->newcontent, $options);
981 echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info');
982 echo $OUTPUT->box($parseroutput['parsed_text'], 'generalbox wiki_previewbox');
983 $content = $this->newcontent;
984
985 $this->print_edit($content);
986 }
987
988}
989
990/**
991 *
992 * Class that models the behavior of wiki's
993 * view differences
994 *
995 */
996class page_wiki_diff extends page_wiki {
997
998 private $compare;
999 private $comparewith;
1000
1001 function print_header() {
1002 global $OUTPUT;
1003
1004 parent::print_header();
1005
1006 $this->print_pagetitle();
1007 $vstring = new stdclass;
1008 $vstring->old = $this->compare;
1009 $vstring->new = $this->comparewith;
1010 echo $OUTPUT->heading(get_string('comparewith', 'wiki', $vstring));
1011 }
1012
1013 /**
1014 * Print the diff view
1015 */
1016 function print_content() {
1017 global $PAGE;
1018
1019 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1020 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1021
1022 $this->print_diff_content();
1023 }
1024
1025 function set_url() {
1026 global $PAGE, $CFG;
1027
1028 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/diff.php', array('pageid' => $this->page->id, 'comparewith' => $this->comparewith, 'compare' => $this->compare));
1029 }
1030
1031 function set_comparison($compare, $comparewith) {
1032 $this->compare = $compare;
1033 $this->comparewith = $comparewith;
1034 }
1035
1036 protected function create_navbar() {
1037 global $PAGE, $CFG;
1038
1039 parent::create_navbar();
1040 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
1041 $PAGE->navbar->add(get_string('diff', 'wiki'));
1042 }
1043
1044 protected function setup_tabs() {
1045 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1046 }
1047
1048 /**
1049 * Given two versions of a page, prints a page displaying the differences between them.
1050 *
1051 * @uses $CFG
1052 *
1053 * @param int $pageid The page id whose versions will be displayed.
1054 * @param int $cm The course id, needed to print user images.
1055 * @param int $compare The first version number to be compared.
1056 * @param int $comparewith The second version number to be compared.
1057 */
1058 private function print_diff_content() {
1059 global $CFG, $OUTPUT, $PAGE;
1060
1061 $pageid = $this->page->id;
1062 $total = wiki_count_wiki_page_versions($pageid) - 1;
1063
1064 $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
1065 $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
1066
1067 if ($oldversion && $newversion) {
1068
1069 $oldtext = format_text($oldversion->content);
1070 $newtext = format_text($newversion->content);
1071 list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
1072 $oldversion->diff = $diff1;
1073 $oldversion->user = wiki_get_user_info($oldversion->userid);
1074 $newversion->diff = $diff2;
1075 $newversion->user = wiki_get_user_info($newversion->userid);
1076
1077 echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
1078 } else {
1079 print_error('versionerror', 'wiki');
1080 }
1081 }
1082}
1083
1084/**
1085 *
1086 * Class that models the behavior of wiki's history page
1087 *
1088 */
1089class page_wiki_history extends page_wiki {
1090 /**
1091 * @var int $paging current page
1092 */
1093 private $paging;
1094
1095 /**
1096 * @var int @rowsperpage Items per page
1097 */
1098 private $rowsperpage = 10;
1099
1100 /**
1101 * @var int $allversion if $allversion != 0, all versions will be printed in a signle table
1102 */
1103 private $allversion;
1104
1105 function __construct($wiki, $subwiki, $cm) {
1106 global $PAGE;
1107 parent::__construct($wiki, $subwiki, $cm);
1108 $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
1109 }
1110
1111 function print_header() {
1112 parent::print_header();
1113 $this->print_pagetitle();
1114 }
1115
1116 function print_content() {
1117 global $PAGE;
1118
1119 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1120 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1121
1122 $this->print_history_content();
1123 }
1124
1125 function set_url() {
1126 global $PAGE, $CFG;
1127 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1128 }
1129
1130 function set_paging($paging) {
1131 $this->paging = $paging;
1132 }
1133
1134 function set_allversion($allversion) {
1135 $this->allversion = $allversion;
1136 }
1137
1138 protected function create_navbar() {
1139 global $PAGE, $CFG;
1140
1141 parent::create_navbar();
1142 $PAGE->navbar->add(get_string('history', 'wiki'));
1143 }
1144
1145 /**
1146 * Prints the history for a given wiki page
1147 *
1148 * @uses $CFG
1149 *
1150 * @param int $pageid The page id whose history should be displayed.
1151 * @param int $cm The course id, needed to print user images.
1152 * @param wikipage $wikipage
1153 * @param int $allversion This tells how results should be displayed.
1154 * If $allversion == 0 : Only $rowsperpage will be displayed at a time, and $paging is the page being displayed.
1155 * If $allversion != 0 : All versions will be printed in a single table.
1156 * @param int $paging Desired page to be displayed, this is ignored if $allversion!=0.
1157 * @param int $rowsperpage How many rows to be displayed in each page, this is ignored if $allversion!=0.
1158 */
1159 private function print_history_content() {
1160 global $CFG, $OUTPUT, $PAGE;
1161
1162 $pageid = $this->page->id;
1163 $offset = $this->paging * $this->rowsperpage;
1164 // vcount is the latest version
1165 $vcount = wiki_count_wiki_page_versions($pageid) - 1;
1166 if ($this->allversion) {
1167 $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
1168 } else {
1169 $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
1170 }
1171 // We don't want version 0 to be displayed
1172 // version 0 is blank page
1173 if (end($versions)->version == 0) {
1174 array_pop($versions);
1175 }
1176
1177 $contents = array();
1178
1179 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
1180 $creator = wiki_get_user_info($version0page->userid);
1181 $a = new StdClass;
1182 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
1183 $a->username = $creator->username;
1184 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
1185 if ($vcount > 0) {
1186
1187 /// If there is only one version, we don't need radios nor forms
1188 if (count($versions) == 1) {
1189
1190 $row = array_shift($versions);
1191
1192 $username = wiki_get_user_info($row->userid);
1193 $picture = $OUTPUT->user_picture($username);
1194 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
1195 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
1196 $versionid = wiki_get_version($row->id);
1197 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1198 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
1199 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picture . html_writer::link($userlink->out(false), fullname($username)), $time, print_container($date, false, 'wiki_histdate', '', true));
1200
1201 $table = new html_table();
1202 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
1203 $table->data = $contents;
1204 $table->attributes['class'] = 'mdl-align';
1205
1206 echo html_writer::table($table);
1207
1208 } else {
1209
1210 $checked = $vcount - $offset;
1211 $lastdate = '';
1212 $rowclass = array();
1213
1214 foreach ($versions as $version) {
1215 $user = wiki_get_user_info($version->userid);
1216 $picture = $OUTPUT->user_picture($user, array('popup' => true));
1217 $date = userdate($version->timecreated, get_string('strftimedate'));
1218 if ($date == $lastdate) {
1219 $date = '';
1220 $rowclass[] = '';
1221 } else {
1222 $lastdate = $date;
1223 $rowclass[] = 'wiki_histnewdate';
1224 }
1225 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1226 $versionid = wiki_get_version($version->id);
1227 if ($versionid) {
1228 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1229 $viewlink = html_writer::link($url->out(false), $version->version);
1230 } else {
1231 $viewlink = $version->version;
1232 }
1233 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
1234 $contents[] = array($this->choose_from_radio(array($version->version => null), 'compare', 'M.mod_wiki.history()', $checked - 1, true) . $this->choose_from_radio(array($version->version => null), 'comparewith', 'M.mod_wiki.history()', $checked, true), $viewlink, $picture . html_writer::link($userlink->out(false), fullname($user)), $time, print_container($date, false, 'wiki_histdate', '', true));
1235 }
1236
1237 $table = new html_table();
1238 $icon = $OUTPUT->help_icon('diff', 'wiki', get_string('diff', 'wiki'));
1239
1240 $table->head = array($icon, get_string('version'), get_string('user'), get_string('modified'), '');
1241 $table->data = $contents;
1242 $table->attributes['class'] = 'mdl-align';
1243 $table->rowclasses = $rowclass;
1244
1245 /*$table = new StdClass();
1246 $table->head = array(helpbutton('diff', 'diff', 'wiki', true, false, '', true, ''),
1247 get_string('version'),
1248 get_string('user'),
1249 get_string('modified'),
1250 '');
1251 $table->data = $contents;
1252 $table->class = 'mdl-align';
1253 $table->rowclass = $rowclass;*/
1254
1255 ///Print the form
1256 echo '<form action="' . $CFG->wwwroot . '/mod/wiki/diff.php" method="get" id="diff">';
1257 echo '<div><input type="hidden" name="pageid" value="' . $pageid . '" /></div>';
1258 echo html_writer::table($table);
1259 echo '<div><input class="wiki_form-button" type="submit" value="' . get_string('comparesel', 'wiki') . '"/></div>';
1260 echo '</form>';
1261 }
1262 } else {
1263 print_string('nohistory', 'wiki');
1264 }
1265 if (!$this->allversion) {
1266 //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;');
1267 // $pagingbar->pagevar = $pagevar;
1268 echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&amp;');
1269 //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;','paging');
1270 } else {
1271 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1272 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1273 }
1274 if ($vcount > $this->rowsperpage && !$this->allversion) {
1275 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1276 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1277 }
1278 }
1279
1280 /**
1281 * Given an array of values, creates a group of radio buttons to be part of a form
1282 *
1283 * @param array $options An array of value-label pairs for the radio group (values as keys).
1284 * @param string $name Name of the radiogroup (unique in the form).
1285 * @param string $onclick Function to be executed when the radios are clicked.
1286 * @param string $checked The value that is already checked.
1287 * @param bool $return If true, return the HTML as a string, otherwise print it.
1288 *
1289 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1290 */
1291 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1292
1293 static $idcounter = 0;
1294
1295 if (!$name) {
1296 $name = 'unnamed';
1297 }
1298
1299 $output = '<span class="radiogroup ' . $name . "\">\n";
1300
1301 if (!empty($options)) {
1302 $currentradio = 0;
1303 foreach ($options as $value => $label) {
1304 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1305 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1306 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1307 if ($value == $checked) {
1308 $output .= ' checked="checked"';
1309 }
1310 if ($onclick) {
1311 $output .= ' onclick="' . $onclick . '"';
1312 }
1313 if ($label === '') {
1314 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1315 } else {
1316 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1317 }
1318 $currentradio = ($currentradio + 1) % 2;
1319 }
1320 }
1321
1322 $output .= '</span>' . "\n";
1323
1324 if ($return) {
1325 return $output;
1326 } else {
1327 echo $output;
1328 }
1329 }
1330}
1331
1332/**
1333 * Class that models the behavior of wiki's map page
1334 *
1335 */
1336class page_wiki_map extends page_wiki {
1337
1338 /**
1339 * @var int wiki view option
1340 */
1341 private $view;
1342
1343 function print_header() {
1344 parent::print_header();
1345 $this->print_pagetitle();
1346 }
1347
1348 function print_content() {
1349 global $CFG, $PAGE;
1350
1351 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1352 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1353
1354 if ($this->view > 0) {
1355 //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
1356 }
1357
1358 switch ($this->view) {
1359 case 1:
1360 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1361 $this->print_contributions_content();
1362 break;
1363 case 2:
1364 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1365 $this->print_navigation_content();
1366 break;
1367 case 3:
1368 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1369 $this->print_orphaned_content();
1370 break;
1371 case 4:
1372 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1373 $this->print_index_content();
1374 break;
1375 case 5:
1376 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1377 $this->print_page_list_content();
1378 break;
1379 case 6:
1380 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1381 $this->print_updated_content();
1382 break;
1383 default:
1384 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1385 $this->print_page_list_content();
1386 }
1387 }
1388
1389 function set_view($option) {
1390 $this->view = $option;
1391 }
1392
1393 function print_menu_map() {
1394 $options = array('contributions', 'links', 'orphaned', 'pageindex', 'pagelist', 'updatedpages');
1395 $items = array();
1396 foreach ($options as $opt) {
1397 $items[] = get_string($opt, 'wiki');
1398 }
1399 $table = new html_table();
1400 $table->head = array(get_string('mapmenu', 'wiki'));
1401 $table->attributes['class'] = 'wiki_editor generalbox';
1402 $table->data = array();
1403 $table->rowclasses = array();
1404 foreach ($items as $key => $item) {
1405 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $this->page->id, 'option' => $key + 1));
1406 $table->data[] = array(html_writer::link($link, $item));
1407 }
1408 echo html_writer::table($table);
1409 }
1410
1411 function set_url() {
1412 global $PAGE, $CFG;
1413 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1414 }
1415
1416 protected function create_navbar() {
1417 global $PAGE;
1418
1419 parent::create_navbar();
1420 $PAGE->navbar->add(get_string('map', 'wiki'));
1421 }
1422
1423 /**
1424 * Prints the contributions tab content
1425 *
1426 * @uses $OUTPUT, $USER
1427 *
1428 */
1429 private function print_contributions_content() {
1430 global $CFG, $OUTPUT, $USER;
1431 $page = $this->page;
1432
1433 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1434 $fresh = wiki_refresh_cachedcontent($page);
1435 $page = $fresh['page'];
1436 }
1437
1438 $swid = $this->subwiki->id;
1439
1440 $table = new html_table();
1441 $table->head = array(get_string('contributions', 'wiki'));
1442 $table->attributes['class'] = 'wiki_editor generalbox';
1443 $table->data = array();
1444 $table->rowclasses = array();
1445
1446 $lastversions = array();
1447 $pages = array();
1448 $users = array();
1449
1450 if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1451 foreach ($contribs as $contrib) {
1452 if (!array_key_exists($contrib->pageid, $pages)) {
1453 $page = wiki_get_page($contrib->pageid);
1454 $pages[$contrib->pageid] = $page;
1455 } else {
1456 continue;
1457 }
1458
1459 if (!array_key_exists($page->id, $lastversions)) {
1460 $version = wiki_get_last_version($page->id);
1461 $lastversions[$page->id] = $version;
1462 } else {
1463 $version = $lastversions[$page->id];
1464 }
1465
1466 if (!array_key_exists($version->userid, $users)) {
1467 $user = wiki_get_user_info($version->userid);
1468 $users[$version->userid] = $user;
1469 } else {
1470 $user = $users[$version->userid];
1471 }
1472
1473 $link = wiki_parser_link(wiki_trim_string($page->title), array('swid' => $swid));
1474 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1475
1476 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
1477 $icon = $OUTPUT->user_picture($user, array('popup' => true));
1478
1479 $table->data[] = array("$icon&nbsp;$linkpage");
1480 }
1481 } else {
1482 $table->data[] = array(get_string('nocontribs', 'wiki'));
1483 }
1484 echo html_writer::table($table);
1485 }
1486
1487 /**
1488 * Prints the navigation tab content
1489 *
1490 * @uses $OUTPUT
1491 *
1492 */
1493 private function print_navigation_content() {
1494 global $OUTPUT;
1495 $page = $this->page;
1496
1497 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1498 $fresh = wiki_refresh_cachedcontent($page);
1499 $page = $fresh['page'];
1500 }
1501
1502 $tolinks = wiki_get_linked_to_pages($page->id);
1503 $fromlinks = wiki_get_linked_from_pages($page->id);
1504
1505 $table = new html_table();
1506 $table->attributes['class'] = 'wiki_navigation_from';
1507 $table->head = array(get_string('navigationfrom', 'wiki') . ':');
1508 $table->data = array();
1509 $table->rowclasses = array();
1510 foreach ($fromlinks as $link) {
1511 $lpage = wiki_get_page($link->frompageid);
1512 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1513 $table->data[] = array(html_writer::link($link->out(false), $lpage->title));
1514 $table->rowclasses[] = 'mdl-align';
1515 }
1516
1517 $table_left = html_writer::table($table);
1518
1519 $table = new html_table();
1520 $table->attributes['class'] = 'wiki_navigation_to';
1521 $table->head = array(get_string('navigationto', 'wiki') . ':');
1522 $table->data = array();
1523 $table->rowclasses = array();
1524 foreach ($tolinks as $link) {
1525 if ($link->tomissingpage) {
1526 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
1527 $table->data[] = array(html_writer::link($viewlink->out(false), $link->tomissingpage, array('class' => 'wiki_newentry')));
1528 } else {
1529 $lpage = wiki_get_page($link->topageid);
1530 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1531 $table->data[] = array(html_writer::link($viewlink->out(false), $lpage->title));
1532 }
1533 $table->rowclasses[] = 'mdl-align';
1534 }
1535 $table_right = html_writer::table($table);
1536 echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1537 }
1538
1539 /**
1540 * Prints the index page tab content
1541 *
1542 *
1543 */
1544 private function print_index_content() {
1545 $page = $this->page;
1546
1547 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1548 $fresh = wiki_refresh_cachedcontent($page);
1549 $page = $fresh['page'];
1550 }
1551
1552 $node = new navigation_node($page->title);
1553
1554 $keys = array();
1555 $tree = array();
1556 $tree = wiki_build_tree($page, $node, $keys);
1557
1558 $table = new html_table();
1559 $table->head = array(get_string('pageindex', 'wiki'));
1560 $table->attributes['class'] = 'wiki_editor generalbox';
1561 $table->data[] = array($this->render_navigation_node($tree));
1562
1563 echo html_writer::table($table);
1564 }
1565
1566 /**
1567 * Prints the page list tab content
1568 *
1569 *
1570 */
1571 private function print_page_list_content() {
1572 $page = $this->page;
1573
1574 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1575 $fresh = wiki_refresh_cachedcontent($page);
1576 $page = $fresh['page'];
1577 }
1578
1579 $pages = wiki_get_page_list($this->subwiki->id);
1580
1581 $stdaux = new stdClass();
1582 $strspecial = get_string('special', 'wiki');
1583
1584 foreach ($pages as $page) {
1585 $letter = strtoupper(substr($page->title, 0, 1));
1586 if (preg_match('/[A-Z]/', $letter)) {
1587 $stdaux->{
1588 $letter}
1589 [] = wiki_parser_link($page);
1590 } else {
1591 $stdaux->{
1592 $strspecial}
1593 [] = wiki_parser_link($page);
1594 }
1595 }
1596
1597 $table = new html_table();
1598 $table->head = array(get_string('pagelist', 'wiki'));
1599 $table->attributes['class'] = 'wiki_editor generalbox';
1600 $table->align = array('center');
1601 foreach ($stdaux as $key => $elem) {
1602 $table->data[] = array($key);
1603 foreach ($elem as $e) {
1604 $table->data[] = array(html_writer::link($e['url'], $e['content']));
1605 }
1606 }
1607 echo html_writer::table($table);
1608 }
1609
1610 /**
1611 * Prints the orphaned tab content
1612 *
1613 *
1614 */
1615 private function print_orphaned_content() {
1616 $page = $this->page;
1617
1618 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1619 $fresh = wiki_refresh_cachedcontent($page);
1620 $page = $fresh['page'];
1621 }
1622
1623 $swid = $this->subwiki->id;
1624
1625 $table = new html_table();
1626 $table->head = array(get_string('orphaned', 'wiki'));
1627 $table->attributes['class'] = 'wiki_editor generalbox';
1628 $table->data = array();
1629 $table->rowclasses = array();
1630
1631 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1632 foreach ($orphanedpages as $page) {
1633 $link = wiki_parser_link(wiki_trim_string($page->title), array('swid' => $swid));
1634 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1635 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>');
1636 }
1637 } else {
1638 $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1639 }
1640
1641 echo html_writer::table($table);
1642 }
1643
1644 /**
1645 * Prints the updated tab content
1646 *
1647 * @uses $COURSE, $OUTPUT
1648 *
1649 */
1650 private function print_updated_content() {
1651 global $COURSE, $OUTPUT;
1652 $page = $this->page;
1653
1654 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1655 $fresh = wiki_refresh_cachedcontent($page);
1656 $page = $fresh['page'];
1657 }
1658
1659 $swid = $this->subwiki->id;
1660
1661 $table = new html_table();
1662 $table->head = array(get_string('updatedpages', 'wiki'));
1663 $table->attributes['class'] = 'wiki_editor generalbox';
1664 $table->data = array();
1665 $table->rowclasses = array();
1666
1667 if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1668 $strdataux = '';
1669 foreach ($pages as $page) {
1670 $user = wiki_get_user_info($page->userid);
1671 $strdata = strftime('%d %b %Y', $page->timemodified);
1672 if ($strdata != $strdataux) {
1673 $table->data[] = array($OUTPUT->heading($strdata, 4));
1674 $strdataux = $strdata;
1675 }
1676 $link = wiki_parser_link(wiki_trim_string($page->title), array('swid' => $swid));
1677 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1678
1679 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
1680 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1681 $table->data[] = array("$icon&nbsp;$linkpage");
1682 }
1683 } else {
1684 $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1685 }
1686
1687 echo html_writer::table($table);
1688 }
1689
1690 protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1691
1692 // exit if empty, we don't want an empty ul element
1693 if (count($items) == 0) {
1694 return '';
1695 }
1696
1697 // array of nested li elements
1698 $lis = array();
1699 foreach ($items as $item) {
1700 if (!$item->display) {
1701 continue;
1702 }
1703 $content = $item->get_content();
1704 $title = $item->get_title();
1705 if ($item->icon instanceof renderable) {
1706 $icon = $this->wikioutput->render($item->icon);
1707 $content = $icon . '&nbsp;' . $content; // use CSS for spacing of icons
1708 }
1709 if ($item->helpbutton !== null) {
1710 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1711 }
1712
1713 if ($content === '') {
1714 continue;
1715 }
1716
1717 if ($item->action instanceof action_link) {
1718 //TODO: to be replaced with something else
1719 $link = $item->action;
1720 if ($item->hidden) {
1721 $link->add_class('dimmed');
1722 }
1723 $content = $this->output->render($link);
1724 } else if ($item->action instanceof moodle_url) {
1725 $attributes = array();
1726 if ($title !== '') {
1727 $attributes['title'] = $title;
1728 }
1729 if ($item->hidden) {
1730 $attributes['class'] = 'dimmed_text';
1731 }
1732 $content = html_writer::link($item->action, $content, $attributes);
1733
1734 } else if (is_string($item->action) || empty($item->action)) {
1735 $attributes = array();
1736 if ($title !== '') {
1737 $attributes['title'] = $title;
1738 }
1739 if ($item->hidden) {
1740 $attributes['class'] = 'dimmed_text';
1741 }
1742 $content = html_writer::tag('span', $content, $attributes);
1743 }
1744
1745 // this applies to the li item which contains all child lists too
1746 $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1747 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1748 $liclasses[] = 'collapsed';
1749 }
1750 if ($item->isactive === true) {
1751 $liclasses[] = 'current_branch';
1752 }
1753 $liattr = array('class' => join(' ', $liclasses));
1754 // class attribute on the div item which only contains the item content
1755 $divclasses = array('tree_item');
1756 if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1757 $divclasses[] = 'branch';
1758 } else {
1759 $divclasses[] = 'leaf';
1760 }
1761 if (!empty($item->classes) && count($item->classes) > 0) {
1762 $divclasses[] = join(' ', $item->classes);
1763 }
1764 $divattr = array('class' => join(' ', $divclasses));
1765 if (!empty($item->id)) {
1766 $divattr['id'] = $item->id;
1767 }
1768 $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1769 if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1770 $content = html_writer::empty_tag('hr') . $content;
1771 }
1772 $content = html_writer::tag('li', $content, $liattr);
1773 $lis[] = $content;
1774 }
1775
1776 if (count($lis)) {
1777 return html_writer::tag('ul', implode("\n", $lis), $attrs);
1778 } else {
1779 return '';
1780 }
1781 }
1782
1783}
1784
1785/**
1786 * Class that models the behavior of wiki's restore version page
1787 *
1788 */
1789class page_wiki_restoreversion extends page_wiki {
1790 private $version;
1791
1792 function print_header() {
1793 parent::print_header();
1794 $this->print_pagetitle();
1795 }
1796
1797 function print_content() {
1798 global $CFG, $PAGE;
1799
1800 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1801 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
1802
1803 $this->print_restoreversion();
1804 }
1805
1806 function set_url() {
1807 global $PAGE, $CFG;
1808 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1809 }
1810
1811 function set_versionid($versionid) {
1812 $this->version = wiki_get_version($versionid);
1813 }
1814
1815 protected function create_navbar() {
1816 global $PAGE, $CFG;
1817
1818 parent::create_navbar();
1819 $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1820 }
1821
1822 protected function setup_tabs() {
1823 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1824 }
1825
1826 /**
1827 * Prints the restore version content
1828 *
1829 * @uses $CFG
1830 *
1831 * @param page $page The page whose version will be restored
1832 * @param int $versionid The version to be restored
1833 * @param bool $confirm If false, shows a yes/no confirmation page.
1834 * If true, restores the old version and redirects the user to the 'view' tab.
1835 */
1836 private function print_restoreversion() {
1837 global $CFG, $OUTPUT;
1838
1839 $version = wiki_get_version($this->version->id);
1840
1841 echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
1842 print_container_start(false, 'wiki_restoreform');
1843 echo '<form class="wiki_restore_yes" action="' . $CFG->wwwroot . '/mod/wiki/restoreversion.php?pageid=' . $this->page->id . '&amp;versionid=' . $version->id . '" method="post" id="restoreversion">';
1844 echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1845 echo '</form>';
1846 echo '<form class="wiki_restore_no" action="' . $CFG->wwwroot . '/mod/wiki/viewversion.php?pageid=' . $this->page->id . '&amp;versionid=' . $version->id . '" method="post">';
1847 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1848 echo '</form>';
1849 print_container_end();
1850 }
1851}
1852
1853/**
1854 *
1855 * Class that models the behavior of wiki's
1856 * save page
1857 *
1858 */
1859class page_wiki_save extends page_wiki_edit {
1860
1861 private $newcontent;
1862
1863 function print_header() {
1864 }
1865
1866 function print_content() {
1867 global $PAGE;
1868
1869 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1870 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
1871
1872 $this->print_save();
1873 }
1874
1875 function set_newcontent($newcontent) {
1876 $this->newcontent = $newcontent;
1877 }
1878
1879 protected function set_session_url() {
1880 }
1881
1882 protected function print_save() {
1883 global $CFG, $USER, $OUTPUT, $PAGE;
1884
1885 $cm = $PAGE->cm;
1886 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1887
1888 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1889 if (!empty($this->section)) {
1890 $url .= "&section=" . $this->section;
1891 }
1892
1893 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
1894
1895 if ($this->format != 'html') {
1896 $params['contextid'] = $context->id;
1897 $params['filearea'] = 'wiki_attachments';
1898 $params['fileitemid'] = $this->page->id;
1899 }
1900
1901 $form = new mod_wiki_edit_form($url, $params);
1902
1903 $save = false;
1904 $data = false;
1905 if ($data = $form->get_data()) {
1906 if ($this->format == 'html') {
1907 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'wiki_attachments', $this->subwiki->id);
1908 }
1909
1910 if (isset($this->section)) {
1911 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
1912 } else {
1913 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
1914 }
1915 }
1916
1917 if ($save && $data) {
1918 if ($this->format != 'html') {
1919 $errors = $this->process_uploads($context);
1920 }
1921 if (!empty($CFG->usetags)) {
1922 tag_set('wiki', $this->page->id, $data->tags);
1923 }
1924
1925 $message = '<p>' . get_string('saving', 'wiki') . '</p>';
1926
1927 if (!empty($save['sections'])) {
1928 foreach ($save['sections'] as $s) {
1929 $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
1930 }
1931 }
1932
1933 if ($this->versionnumber + 1 != $save['version']) {
1934 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
1935 }
1936
1937 if (isset($errors) && !empty($errors)) {
1938 foreach ($errors as $e) {
1939 $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
1940 }
1941 }
1942
1943 //deleting old locks
1944 wiki_delete_locks($this->page->id, $USER->id, $this->section);
1945
1946 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
1947 } else {
1948 print_error('savingerror', 'wiki');
1949 }
1950 }
1951}
1952
1953/**
1954 * Class that models the behavior of wiki's view an old version of a page
1955 *
1956 */
1957class page_wiki_viewversion extends page_wiki {
1958
1959 private $version;
1960
1961 function print_header() {
1962 parent::print_header();
1963 $this->print_pagetitle();
1964 }
1965
1966 function print_content() {
1967 global $PAGE;
1968
1969 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1970 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1971
1972 $this->print_version_view();
1973 }
1974
1975 function set_url() {
1976 global $PAGE, $CFG;
1977 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1978 }
1979
1980 function set_versionid($versionid) {
1981 $this->version = wiki_get_version($versionid);
1982 }
1983
1984 protected function create_navbar() {
1985 global $PAGE, $CFG;
1986
1987 parent::create_navbar();
1988 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
1989 $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
1990 }
1991
1992 protected function setup_tabs() {
1993 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
1994 }
1995
1996 /**
1997 * Given an old page version, output the version content
1998 *
1999 * @uses $CFG
2000 *
2001 * @param page $page The page whose version will be displayed.
2002 * @param int $cm The course id, needed to print user images.
2003 * @param int $version The version number to be displayed.
2004 */
2005 private function print_version_view() {
2006 global $CFG, $OUTPUT, $PAGE;
2007 $cm = $PAGE->cm;
2008 $pageversion = wiki_get_version($this->version->id);
2009
2010 if ($pageversion) {
2011 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2012 echo $OUTPUT->heading(get_string('viewversion', 'wiki', $pageversion->version) . '<br />' . html_writer::link($restorelink->out(false), '(' . get_string('restorethis', 'wiki') . ')', array('class' => 'wiki_restore')) . '&nbsp;', 4);
2013 $userinfo = wiki_get_user_info($pageversion->userid);
2014 $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong>&nbsp;' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2015 $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2016 $heading .= '&nbsp;&nbsp;&nbsp;<strong>' . get_string('user') . ':</strong>&nbsp;' . html_writer::link($viewlink->out(false), fullname($userinfo));
2017 $heading .= '&nbsp;&nbsp;&rarr;&nbsp;' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2018 print_container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
2019 $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2020 $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
2021 $content = print_container($parseroutput['parsed_text'], false, '', '', true);
2022 echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2023
2024 } else {
2025 print_error('versionerror', 'wiki');
2026 }
2027 }
2028}
2029
2030class page_wiki_confirmrestore extends page_wiki_save {
2031
2032 private $version;
2033
2034 function set_url() {
2035 global $PAGE, $CFG;
2036 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2037 }
2038
2039 function print_content() {
2040 global $CFG, $PAGE;
2041
2042 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2043 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
2044
2045 $version = wiki_get_version($this->version->id);
2046 if (wiki_restore_page($this->page, $version->content, $version->userid)) {
2047 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2048 } else {
2049 print_error('restoreerror', 'wiki', $version->version);
2050 }
2051 }
2052
2053 function set_versionid($versionid) {
2054 $this->version = wiki_get_version($versionid);
2055 }
2056}
2057
2058class page_wiki_prettyview extends page_wiki {
2059
2060 function print_header() {
2061 global $CFG, $OUTPUT;
2062
2063 echo $OUTPUT->doctype();
2064 echo '<html>';
2065 echo '<head>';
2066 echo $OUTPUT->standard_head_html();
2067 echo '</head>';
2068 echo '<body>';
2069
2070 echo '<h1 id="wiki_printable_title">' . $this->title . '</h1>';
2071 }
2072
2073 function print_content() {
2074 global $PAGE;
2075
2076 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2077 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
2078
2079 $this->print_pretty_view();
2080 }
2081
2082 function set_url() {
2083 global $PAGE, $CFG;
2084
2085 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2086 }
2087
2088 function print_footer() {
2089 echo '</body>';
2090 echo '</html>';
2091 }
2092
2093 private function print_pretty_view() {
2094 $version = wiki_get_current_version($this->page->id);
2095
2096 $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2097
2098 echo '<div id="wiki_printable_content">';
2099 echo $content['parsed_text'];
2100 echo '</div>';
2101 }
2102}
2103
2104class page_wiki_handlecomments extends page_wiki {
2105 private $action;
2106 private $content;
2107 private $commentid;
2108 private $format;
2109
2110 function print_header() {
2111 $this->set_url();
2112 }
2113
2114 public function print_content() {
2115 global $PAGE;
2116
2117 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2118 require_capability('mod/wiki:managecomment', $context, NULL, true, 'nomanagecommentpermission', 'wiki');
2119
2120 if ($this->action == 'add' or $this->action == 'edit') {
2121 $this->add_comment($this->content, $this->commentid);
2122 } else if ($this->action == 'delete') {
2123 $this->delete_comment($this->commentid);
2124 }
2125
2126 }
2127
2128 public function set_url() {
2129 global $PAGE, $CFG;
2130 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2131 }
2132
2133 public function set_action($action, $commentid, $content) {
2134 $this->action = $action;
2135 $this->commentid = $commentid;
2136 $this->content = $content;
2137
2138 $version = wiki_get_current_version($this->page->id);
2139 $format = $version->contentformat;
2140
2141 $this->format = $format;
2142 }
2143
2144 private function add_comment($content, $idcomment) {
2145 global $CFG, $PAGE;
2146 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2147
2148 $pageid = $this->page->id;
2149 $cm = $PAGE->cm;
2150 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2151
2152 wiki_add_comment($context, $pageid, $content, $this->format);
2153
2154 if (!$idcomment) {
2155 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2156 } else {
2157 $this->delete_comment($idcomment);
2158 }
2159 }
2160
2161 private function delete_comment($commentid) {
2162 global $CFG, $PAGE;
2163
2164 $cm = $PAGE->cm;
2165 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2166 $pageid = $this->page->id;
2167
2168 wiki_delete_comment($commentid, $context, $pageid);
2169
2170 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('deletecomment', 'wiki'), 2);
2171 }
2172
2173}
2174
2175class page_wiki_lock extends page_wiki_edit {
2176
2177 public function print_header() {
2178 $this->set_url();
2179 }
2180
2181 protected function set_url() {
2182 global $PAGE, $CFG;
2183
2184 $params = array('pageid' => $this->page->id);
2185
2186 if ($this->section) {
2187 $params['section'] = $this->section;
2188 }
2189
2190 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2191 }
2192
2193 protected function set_session_url() {
2194 }
2195
2196 public function print_content() {
2197 global $USER, $PAGE;
2198
2199 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2200 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
2201
2202 wiki_set_lock($this->page->id, $USER->id, $this->section);
2203 }
2204
2205 public function print_footer() {
2206 }
2207}
2208
2209class page_wiki_overridelocks extends page_wiki_edit {
2210 function print_header() {
2211 $this->set_url();
2212 }
2213
2214 function print_content() {
2215 global $CFG, $PAGE;
2216
2217 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2218 require_capability('mod/wiki:overridelock', $context, NULL, true, 'nooverridelockpermission', 'wiki');
2219
2220 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2221
2222 $args = "pageid=" . $this->page->id;
2223
2224 if (!empty($this->section)) {
2225 $args .= "&section=" . $this->section;
2226 }
2227
2228 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2229 }
2230
2231 function set_url() {
2232 global $PAGE, $CFG;
2233
2234 $params = array('pageid' => $this->page->id);
2235
2236 if (!empty($this->section)) {
2237 $params['section'] = $this->section;
2238 }
2239
2240 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2241 }
2242
2243 protected function set_session_url() {
2244 }
2245
2246 private function print_overridelocks() {
2247 global $CFG;
2248
2249 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2250
2251 $args = "pageid=" . $this->page->id;
2252
2253 if (!empty($this->section)) {
2254 $args .= "&section=" . $this->section;
2255 }
2256
2257 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2258 }
2259
2260}