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