weekly release
[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);
23c79945 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:
23c79945
PS
543 //$draftitemid = file_get_submitted_draft_itemid('attachments');
544 //file_prepare_draft_area($draftitemid, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
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;
23c79945 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
23c79945 1037 $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
00710f4c 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');
23c79945 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
d92b5e7f
DC
1186 function print_pagetitle() {
1187 global $OUTPUT;
1188 $html = '';
1189
1190 $html .= $OUTPUT->container_start();
1191 $html .= $OUTPUT->heading_with_help(format_string($this->title), 'history', 'wiki');
1192 $html .= $OUTPUT->container_end();
1193 echo $html;
1194 }
1195
00710f4c
DC
1196 function print_content() {
1197 global $PAGE;
1198
1199 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1200 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1201
1202 $this->print_history_content();
1203 }
1204
1205 function set_url() {
1206 global $PAGE, $CFG;
1207 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1208 }
1209
1210 function set_paging($paging) {
1211 $this->paging = $paging;
1212 }
1213
1214 function set_allversion($allversion) {
1215 $this->allversion = $allversion;
1216 }
1217
1218 protected function create_navbar() {
1219 global $PAGE, $CFG;
1220
1221 parent::create_navbar();
1222 $PAGE->navbar->add(get_string('history', 'wiki'));
1223 }
1224
1225 /**
1226 * Prints the history for a given wiki page
1227 *
1228 * @uses $CFG
1229 *
1230 * @param int $pageid The page id whose history should be displayed.
1231 * @param int $cm The course id, needed to print user images.
1232 * @param wikipage $wikipage
1233 * @param int $allversion This tells how results should be displayed.
1234 * If $allversion == 0 : Only $rowsperpage will be displayed at a time, and $paging is the page being displayed.
1235 * If $allversion != 0 : All versions will be printed in a single table.
1236 * @param int $paging Desired page to be displayed, this is ignored if $allversion!=0.
1237 * @param int $rowsperpage How many rows to be displayed in each page, this is ignored if $allversion!=0.
1238 */
1239 private function print_history_content() {
1240 global $CFG, $OUTPUT, $PAGE;
1241
1242 $pageid = $this->page->id;
1243 $offset = $this->paging * $this->rowsperpage;
1244 // vcount is the latest version
1245 $vcount = wiki_count_wiki_page_versions($pageid) - 1;
1246 if ($this->allversion) {
1247 $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
1248 } else {
1249 $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
1250 }
1251 // We don't want version 0 to be displayed
1252 // version 0 is blank page
1253 if (end($versions)->version == 0) {
1254 array_pop($versions);
1255 }
1256
1257 $contents = array();
1258
1259 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
1260 $creator = wiki_get_user_info($version0page->userid);
1261 $a = new StdClass;
1262 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
1263 $a->username = $creator->username;
1264 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
1265 if ($vcount > 0) {
1266
1267 /// If there is only one version, we don't need radios nor forms
1268 if (count($versions) == 1) {
1269
1270 $row = array_shift($versions);
1271
1272 $username = wiki_get_user_info($row->userid);
1273 $picture = $OUTPUT->user_picture($username);
1274 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
1275 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
1276 $versionid = wiki_get_version($row->id);
1277 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1278 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
c4238816 1279 $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
1280
1281 $table = new html_table();
1282 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
1283 $table->data = $contents;
1284 $table->attributes['class'] = 'mdl-align';
1285
1286 echo html_writer::table($table);
1287
1288 } else {
1289
1290 $checked = $vcount - $offset;
1291 $lastdate = '';
1292 $rowclass = array();
1293
1294 foreach ($versions as $version) {
1295 $user = wiki_get_user_info($version->userid);
1296 $picture = $OUTPUT->user_picture($user, array('popup' => true));
1297 $date = userdate($version->timecreated, get_string('strftimedate'));
1298 if ($date == $lastdate) {
1299 $date = '';
1300 $rowclass[] = '';
1301 } else {
1302 $lastdate = $date;
1303 $rowclass[] = 'wiki_histnewdate';
1304 }
1305 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1306 $versionid = wiki_get_version($version->id);
1307 if ($versionid) {
1308 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1309 $viewlink = html_writer::link($url->out(false), $version->version);
1310 } else {
1311 $viewlink = $version->version;
1312 }
1313 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
c4238816 1314 $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
1315 }
1316
1317 $table = new html_table();
00710f4c 1318
f8be7ad4
JP
1319 $icon = $OUTPUT->help_icon('diff', 'wiki');
1320
1321 $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
00710f4c 1322 $table->data = $contents;
41a42c1d 1323 $table->attributes['class'] = 'generaltable mdl-align';
00710f4c
DC
1324 $table->rowclasses = $rowclass;
1325
1326 /*$table = new StdClass();
1327 $table->head = array(helpbutton('diff', 'diff', 'wiki', true, false, '', true, ''),
1328 get_string('version'),
1329 get_string('user'),
1330 get_string('modified'),
1331 '');
1332 $table->data = $contents;
1333 $table->class = 'mdl-align';
1334 $table->rowclass = $rowclass;*/
1335
1336 ///Print the form
41a42c1d
SH
1337 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
1338 echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
00710f4c 1339 echo html_writer::table($table);
41a42c1d
SH
1340 echo html_writer::start_tag('div', array('class'=>'mdl-align'));
1341 echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
1342 echo html_writer::end_tag('div');
1343 echo html_writer::end_tag('form');
00710f4c
DC
1344 }
1345 } else {
1346 print_string('nohistory', 'wiki');
1347 }
1348 if (!$this->allversion) {
1349 //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;');
1350 // $pagingbar->pagevar = $pagevar;
1351 echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&amp;');
1352 //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;','paging');
1353 } else {
1354 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1355 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1356 }
1357 if ($vcount > $this->rowsperpage && !$this->allversion) {
1358 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1359 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1360 }
1361 }
1362
1363 /**
1364 * Given an array of values, creates a group of radio buttons to be part of a form
1365 *
1366 * @param array $options An array of value-label pairs for the radio group (values as keys).
1367 * @param string $name Name of the radiogroup (unique in the form).
1368 * @param string $onclick Function to be executed when the radios are clicked.
1369 * @param string $checked The value that is already checked.
1370 * @param bool $return If true, return the HTML as a string, otherwise print it.
1371 *
1372 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1373 */
1374 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1375
1376 static $idcounter = 0;
1377
1378 if (!$name) {
1379 $name = 'unnamed';
1380 }
1381
1382 $output = '<span class="radiogroup ' . $name . "\">\n";
1383
1384 if (!empty($options)) {
1385 $currentradio = 0;
1386 foreach ($options as $value => $label) {
1387 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1388 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1389 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1390 if ($value == $checked) {
1391 $output .= ' checked="checked"';
1392 }
1393 if ($onclick) {
1394 $output .= ' onclick="' . $onclick . '"';
1395 }
1396 if ($label === '') {
1397 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1398 } else {
1399 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1400 }
1401 $currentradio = ($currentradio + 1) % 2;
1402 }
1403 }
1404
1405 $output .= '</span>' . "\n";
1406
1407 if ($return) {
1408 return $output;
1409 } else {
1410 echo $output;
1411 }
1412 }
1413}
1414
1415/**
1416 * Class that models the behavior of wiki's map page
1417 *
1418 */
1419class page_wiki_map extends page_wiki {
1420
1421 /**
1422 * @var int wiki view option
1423 */
1424 private $view;
1425
1426 function print_header() {
1427 parent::print_header();
1428 $this->print_pagetitle();
1429 }
1430
1431 function print_content() {
1432 global $CFG, $PAGE;
1433
1434 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1435 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1436
1437 if ($this->view > 0) {
1438 //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
d92b5e7f 1439 }
00710f4c
DC
1440
1441 switch ($this->view) {
1442 case 1:
1443 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1444 $this->print_contributions_content();
1445 break;
1446 case 2:
1447 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1448 $this->print_navigation_content();
1449 break;
1450 case 3:
1451 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1452 $this->print_orphaned_content();
1453 break;
1454 case 4:
1455 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1456 $this->print_index_content();
1457 break;
1458 case 5:
1459 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1460 $this->print_page_list_content();
1461 break;
1462 case 6:
1463 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1464 $this->print_updated_content();
1465 break;
1466 default:
1467 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1468 $this->print_page_list_content();
1469 }
1470 }
1471
1472 function set_view($option) {
1473 $this->view = $option;
1474 }
1475
00710f4c
DC
1476 function set_url() {
1477 global $PAGE, $CFG;
1478 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1479 }
1480
1481 protected function create_navbar() {
1482 global $PAGE;
1483
1484 parent::create_navbar();
1485 $PAGE->navbar->add(get_string('map', 'wiki'));
1486 }
1487
1488 /**
1489 * Prints the contributions tab content
1490 *
1491 * @uses $OUTPUT, $USER
1492 *
1493 */
1494 private function print_contributions_content() {
1495 global $CFG, $OUTPUT, $USER;
1496 $page = $this->page;
1497
1498 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1499 $fresh = wiki_refresh_cachedcontent($page);
1500 $page = $fresh['page'];
1501 }
1502
1503 $swid = $this->subwiki->id;
1504
1505 $table = new html_table();
b7d0b32f 1506 $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
00710f4c
DC
1507 $table->attributes['class'] = 'wiki_editor generalbox';
1508 $table->data = array();
1509 $table->rowclasses = array();
1510
1511 $lastversions = array();
1512 $pages = array();
1513 $users = array();
1514
1515 if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1516 foreach ($contribs as $contrib) {
1517 if (!array_key_exists($contrib->pageid, $pages)) {
1518 $page = wiki_get_page($contrib->pageid);
1519 $pages[$contrib->pageid] = $page;
1520 } else {
1521 continue;
1522 }
1523
1524 if (!array_key_exists($page->id, $lastversions)) {
1525 $version = wiki_get_last_version($page->id);
1526 $lastversions[$page->id] = $version;
1527 } else {
1528 $version = $lastversions[$page->id];
1529 }
1530
1531 if (!array_key_exists($version->userid, $users)) {
1532 $user = wiki_get_user_info($version->userid);
1533 $users[$version->userid] = $user;
1534 } else {
1535 $user = $users[$version->userid];
1536 }
1537
110f4c93 1538 $link = wiki_parser_link(format_string($page->title), array('swid' => $swid));
00710f4c
DC
1539 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1540
1541 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
1542 $icon = $OUTPUT->user_picture($user, array('popup' => true));
1543
1544 $table->data[] = array("$icon&nbsp;$linkpage");
1545 }
1546 } else {
1547 $table->data[] = array(get_string('nocontribs', 'wiki'));
1548 }
1549 echo html_writer::table($table);
1550 }
1551
1552 /**
1553 * Prints the navigation tab content
1554 *
1555 * @uses $OUTPUT
1556 *
1557 */
1558 private function print_navigation_content() {
1559 global $OUTPUT;
1560 $page = $this->page;
1561
1562 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1563 $fresh = wiki_refresh_cachedcontent($page);
1564 $page = $fresh['page'];
1565 }
1566
1567 $tolinks = wiki_get_linked_to_pages($page->id);
1568 $fromlinks = wiki_get_linked_from_pages($page->id);
1569
1570 $table = new html_table();
1571 $table->attributes['class'] = 'wiki_navigation_from';
d92b5e7f 1572 $table->head = array(get_string('navigationfrom', 'wiki') . $OUTPUT->help_icon('navigationfrom', 'wiki') . ':');
00710f4c
DC
1573 $table->data = array();
1574 $table->rowclasses = array();
1575 foreach ($fromlinks as $link) {
1576 $lpage = wiki_get_page($link->frompageid);
1577 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
110f4c93 1578 $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
00710f4c
DC
1579 $table->rowclasses[] = 'mdl-align';
1580 }
1581
1582 $table_left = html_writer::table($table);
1583
1584 $table = new html_table();
1585 $table->attributes['class'] = 'wiki_navigation_to';
d92b5e7f 1586 $table->head = array(get_string('navigationto', 'wiki') . $OUTPUT->help_icon('navigationto', 'wiki') . ':');
00710f4c
DC
1587 $table->data = array();
1588 $table->rowclasses = array();
1589 foreach ($tolinks as $link) {
1590 if ($link->tomissingpage) {
1591 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
af857b05 1592 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
00710f4c
DC
1593 } else {
1594 $lpage = wiki_get_page($link->topageid);
1595 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
110f4c93 1596 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
00710f4c
DC
1597 }
1598 $table->rowclasses[] = 'mdl-align';
1599 }
1600 $table_right = html_writer::table($table);
1601 echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1602 }
1603
1604 /**
1605 * Prints the index page tab content
1606 *
1607 *
1608 */
1609 private function print_index_content() {
d92b5e7f 1610 global $OUTPUT;
00710f4c
DC
1611 $page = $this->page;
1612
1613 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1614 $fresh = wiki_refresh_cachedcontent($page);
1615 $page = $fresh['page'];
1616 }
1617
1618 $node = new navigation_node($page->title);
1619
1620 $keys = array();
1621 $tree = array();
1622 $tree = wiki_build_tree($page, $node, $keys);
1623
1624 $table = new html_table();
d92b5e7f 1625 $table->head = array(get_string('pageindex', 'wiki') . $OUTPUT->help_icon('pageindex', 'wiki'));
00710f4c
DC
1626 $table->attributes['class'] = 'wiki_editor generalbox';
1627 $table->data[] = array($this->render_navigation_node($tree));
1628
1629 echo html_writer::table($table);
1630 }
1631
1632 /**
1633 * Prints the page list tab content
1634 *
1635 *
1636 */
1637 private function print_page_list_content() {
d92b5e7f 1638 global $OUTPUT;
00710f4c
DC
1639 $page = $this->page;
1640
1641 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1642 $fresh = wiki_refresh_cachedcontent($page);
1643 $page = $fresh['page'];
1644 }
1645
1646 $pages = wiki_get_page_list($this->subwiki->id);
1647
1648 $stdaux = new stdClass();
1649 $strspecial = get_string('special', 'wiki');
1650
1651 foreach ($pages as $page) {
1652 $letter = strtoupper(substr($page->title, 0, 1));
1653 if (preg_match('/[A-Z]/', $letter)) {
1654 $stdaux->{
1655 $letter}
1656 [] = wiki_parser_link($page);
1657 } else {
1658 $stdaux->{
1659 $strspecial}
1660 [] = wiki_parser_link($page);
1661 }
1662 }
1663
1664 $table = new html_table();
d92b5e7f 1665 $table->head = array(get_string('pagelist', 'wiki') . $OUTPUT->help_icon('pagelist', 'wiki'));
00710f4c
DC
1666 $table->attributes['class'] = 'wiki_editor generalbox';
1667 $table->align = array('center');
1668 foreach ($stdaux as $key => $elem) {
1669 $table->data[] = array($key);
1670 foreach ($elem as $e) {
1671 $table->data[] = array(html_writer::link($e['url'], $e['content']));
1672 }
1673 }
1674 echo html_writer::table($table);
1675 }
1676
1677 /**
1678 * Prints the orphaned tab content
1679 *
1680 *
1681 */
1682 private function print_orphaned_content() {
9e11d507
JP
1683 global $OUTPUT;
1684
00710f4c
DC
1685 $page = $this->page;
1686
1687 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1688 $fresh = wiki_refresh_cachedcontent($page);
1689 $page = $fresh['page'];
1690 }
1691
1692 $swid = $this->subwiki->id;
1693
1694 $table = new html_table();
b7d0b32f 1695 $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
00710f4c
DC
1696 $table->attributes['class'] = 'wiki_editor generalbox';
1697 $table->data = array();
1698 $table->rowclasses = array();
1699
1700 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1701 foreach ($orphanedpages as $page) {
110f4c93 1702 $link = wiki_parser_link($page->title, array('swid' => $swid));
00710f4c 1703 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
110f4c93 1704 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
00710f4c
DC
1705 }
1706 } else {
1707 $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1708 }
1709
1710 echo html_writer::table($table);
1711 }
1712
1713 /**
1714 * Prints the updated tab content
1715 *
1716 * @uses $COURSE, $OUTPUT
1717 *
1718 */
1719 private function print_updated_content() {
1720 global $COURSE, $OUTPUT;
1721 $page = $this->page;
1722
1723 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1724 $fresh = wiki_refresh_cachedcontent($page);
1725 $page = $fresh['page'];
1726 }
1727
1728 $swid = $this->subwiki->id;
1729
1730 $table = new html_table();
d92b5e7f 1731 $table->head = array(get_string('updatedpages', 'wiki') . $OUTPUT->help_icon('updatedpages', 'wiki'));
00710f4c
DC
1732 $table->attributes['class'] = 'wiki_editor generalbox';
1733 $table->data = array();
1734 $table->rowclasses = array();
1735
1736 if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1737 $strdataux = '';
1738 foreach ($pages as $page) {
1739 $user = wiki_get_user_info($page->userid);
1740 $strdata = strftime('%d %b %Y', $page->timemodified);
1741 if ($strdata != $strdataux) {
1742 $table->data[] = array($OUTPUT->heading($strdata, 4));
1743 $strdataux = $strdata;
1744 }
110f4c93 1745 $link = wiki_parser_link($page->title, array('swid' => $swid));
00710f4c
DC
1746 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1747
110f4c93 1748 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
00710f4c
DC
1749 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1750 $table->data[] = array("$icon&nbsp;$linkpage");
1751 }
1752 } else {
1753 $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1754 }
1755
1756 echo html_writer::table($table);
1757 }
1758
1759 protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1760
1761 // exit if empty, we don't want an empty ul element
1762 if (count($items) == 0) {
1763 return '';
1764 }
1765
1766 // array of nested li elements
1767 $lis = array();
1768 foreach ($items as $item) {
1769 if (!$item->display) {
1770 continue;
1771 }
1772 $content = $item->get_content();
1773 $title = $item->get_title();
1774 if ($item->icon instanceof renderable) {
1775 $icon = $this->wikioutput->render($item->icon);
1776 $content = $icon . '&nbsp;' . $content; // use CSS for spacing of icons
1777 }
1778 if ($item->helpbutton !== null) {
1779 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1780 }
1781
1782 if ($content === '') {
1783 continue;
1784 }
1785
1786 if ($item->action instanceof action_link) {
1787 //TODO: to be replaced with something else
1788 $link = $item->action;
1789 if ($item->hidden) {
1790 $link->add_class('dimmed');
1791 }
1792 $content = $this->output->render($link);
1793 } else if ($item->action instanceof moodle_url) {
1794 $attributes = array();
1795 if ($title !== '') {
1796 $attributes['title'] = $title;
1797 }
1798 if ($item->hidden) {
1799 $attributes['class'] = 'dimmed_text';
1800 }
1801 $content = html_writer::link($item->action, $content, $attributes);
1802
1803 } else if (is_string($item->action) || empty($item->action)) {
1804 $attributes = array();
1805 if ($title !== '') {
1806 $attributes['title'] = $title;
1807 }
1808 if ($item->hidden) {
1809 $attributes['class'] = 'dimmed_text';
1810 }
1811 $content = html_writer::tag('span', $content, $attributes);
1812 }
1813
1814 // this applies to the li item which contains all child lists too
1815 $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1816 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1817 $liclasses[] = 'collapsed';
1818 }
1819 if ($item->isactive === true) {
1820 $liclasses[] = 'current_branch';
1821 }
1822 $liattr = array('class' => join(' ', $liclasses));
1823 // class attribute on the div item which only contains the item content
1824 $divclasses = array('tree_item');
1825 if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1826 $divclasses[] = 'branch';
1827 } else {
1828 $divclasses[] = 'leaf';
1829 }
1830 if (!empty($item->classes) && count($item->classes) > 0) {
1831 $divclasses[] = join(' ', $item->classes);
1832 }
1833 $divattr = array('class' => join(' ', $divclasses));
1834 if (!empty($item->id)) {
1835 $divattr['id'] = $item->id;
1836 }
1837 $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1838 if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1839 $content = html_writer::empty_tag('hr') . $content;
1840 }
1841 $content = html_writer::tag('li', $content, $liattr);
1842 $lis[] = $content;
1843 }
1844
1845 if (count($lis)) {
1846 return html_writer::tag('ul', implode("\n", $lis), $attrs);
1847 } else {
1848 return '';
1849 }
1850 }
1851
1852}
1853
1854/**
1855 * Class that models the behavior of wiki's restore version page
1856 *
1857 */
1858class page_wiki_restoreversion extends page_wiki {
1859 private $version;
1860
1861 function print_header() {
1862 parent::print_header();
1863 $this->print_pagetitle();
1864 }
1865
1866 function print_content() {
1867 global $CFG, $PAGE;
1868
1869 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1870 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
1871
1872 $this->print_restoreversion();
1873 }
1874
1875 function set_url() {
1876 global $PAGE, $CFG;
1877 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1878 }
1879
1880 function set_versionid($versionid) {
1881 $this->version = wiki_get_version($versionid);
1882 }
1883
1884 protected function create_navbar() {
1885 global $PAGE, $CFG;
1886
1887 parent::create_navbar();
1888 $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1889 }
1890
1891 protected function setup_tabs() {
1892 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1893 }
1894
1895 /**
1896 * Prints the restore version content
1897 *
1898 * @uses $CFG
1899 *
1900 * @param page $page The page whose version will be restored
1901 * @param int $versionid The version to be restored
1902 * @param bool $confirm If false, shows a yes/no confirmation page.
1903 * If true, restores the old version and redirects the user to the 'view' tab.
1904 */
1905 private function print_restoreversion() {
1906 global $CFG, $OUTPUT;
1907
1908 $version = wiki_get_version($this->version->id);
1909
1910 echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
1911 print_container_start(false, 'wiki_restoreform');
1912 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">';
1913 echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1914 echo '</form>';
1915 echo '<form class="wiki_restore_no" action="' . $CFG->wwwroot . '/mod/wiki/viewversion.php?pageid=' . $this->page->id . '&amp;versionid=' . $version->id . '" method="post">';
1916 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1917 echo '</form>';
1918 print_container_end();
1919 }
1920}
1921
1922/**
1923 *
1924 * Class that models the behavior of wiki's
1925 * save page
1926 *
1927 */
1928class page_wiki_save extends page_wiki_edit {
1929
1930 private $newcontent;
1931
1932 function print_header() {
1933 }
1934
1935 function print_content() {
1936 global $PAGE;
1937
1938 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1939 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
1940
1941 $this->print_save();
1942 }
1943
1944 function set_newcontent($newcontent) {
1945 $this->newcontent = $newcontent;
1946 }
1947
1948 protected function set_session_url() {
1949 }
1950
1951 protected function print_save() {
1952 global $CFG, $USER, $OUTPUT, $PAGE;
1953
1954 $cm = $PAGE->cm;
1955 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1956
1957 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1958 if (!empty($this->section)) {
1959 $url .= "&section=" . $this->section;
1960 }
1961
1962 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
1963
1964 if ($this->format != 'html') {
cfffb69c
JP
1965 $params['contextid'] = $context->id;
1966 $params['component'] = 'mod_wiki';
1967 $params['filearea'] = 'attachments';
00710f4c
DC
1968 $params['fileitemid'] = $this->page->id;
1969 }
1970
1971 $form = new mod_wiki_edit_form($url, $params);
1972
1973 $save = false;
1974 $data = false;
1975 if ($data = $form->get_data()) {
1976 if ($this->format == 'html') {
64f93798 1977 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id);
00710f4c
DC
1978 }
1979
1980 if (isset($this->section)) {
1981 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
1982 } else {
1983 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
1984 }
1985 }
1986
1987 if ($save && $data) {
1988 if ($this->format != 'html') {
1989 $errors = $this->process_uploads($context);
1990 }
1991 if (!empty($CFG->usetags)) {
f0f77dfa 1992 tag_set('wiki_pages', $this->page->id, $data->tags);
00710f4c
DC
1993 }
1994
1995 $message = '<p>' . get_string('saving', 'wiki') . '</p>';
1996
1997 if (!empty($save['sections'])) {
1998 foreach ($save['sections'] as $s) {
1999 $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
2000 }
2001 }
2002
2003 if ($this->versionnumber + 1 != $save['version']) {
2004 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
2005 }
2006
2007 if (isset($errors) && !empty($errors)) {
2008 foreach ($errors as $e) {
2009 $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
2010 }
2011 }
2012
2013 //deleting old locks
2014 wiki_delete_locks($this->page->id, $USER->id, $this->section);
2015
2016 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
2017 } else {
2018 print_error('savingerror', 'wiki');
2019 }
2020 }
2021}
2022
2023/**
2024 * Class that models the behavior of wiki's view an old version of a page
2025 *
2026 */
2027class page_wiki_viewversion extends page_wiki {
2028
2029 private $version;
2030
2031 function print_header() {
2032 parent::print_header();
2033 $this->print_pagetitle();
2034 }
2035
2036 function print_content() {
2037 global $PAGE;
2038
2039 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2040 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
2041
2042 $this->print_version_view();
2043 }
2044
2045 function set_url() {
2046 global $PAGE, $CFG;
2047 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2048 }
2049
2050 function set_versionid($versionid) {
2051 $this->version = wiki_get_version($versionid);
2052 }
2053
2054 protected function create_navbar() {
2055 global $PAGE, $CFG;
2056
2057 parent::create_navbar();
2058 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
2059 $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
2060 }
2061
2062 protected function setup_tabs() {
2063 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
2064 }
2065
2066 /**
2067 * Given an old page version, output the version content
2068 *
2069 * @uses $CFG
2070 *
2071 * @param page $page The page whose version will be displayed.
2072 * @param int $cm The course id, needed to print user images.
2073 * @param int $version The version number to be displayed.
2074 */
2075 private function print_version_view() {
2076 global $CFG, $OUTPUT, $PAGE;
2077 $cm = $PAGE->cm;
2078 $pageversion = wiki_get_version($this->version->id);
2079
2080 if ($pageversion) {
2081 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2082 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);
2083 $userinfo = wiki_get_user_info($pageversion->userid);
2084 $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong>&nbsp;' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2085 $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2086 $heading .= '&nbsp;&nbsp;&nbsp;<strong>' . get_string('user') . ':</strong>&nbsp;' . html_writer::link($viewlink->out(false), fullname($userinfo));
2087 $heading .= '&nbsp;&nbsp;&rarr;&nbsp;' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2088 print_container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
2089 $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2090 $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
367a75fa 2091 $content = print_container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
00710f4c
DC
2092 echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2093
2094 } else {
2095 print_error('versionerror', 'wiki');
2096 }
2097 }
2098}
2099
2100class page_wiki_confirmrestore extends page_wiki_save {
2101
2102 private $version;
2103
2104 function set_url() {
2105 global $PAGE, $CFG;
2106 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2107 }
2108
2109 function print_content() {
2110 global $CFG, $PAGE;
2111
2112 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2113 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
2114
2115 $version = wiki_get_version($this->version->id);
2116 if (wiki_restore_page($this->page, $version->content, $version->userid)) {
2117 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2118 } else {
2119 print_error('restoreerror', 'wiki', $version->version);
2120 }
2121 }
2122
2123 function set_versionid($versionid) {
2124 $this->version = wiki_get_version($versionid);
2125 }
2126}
2127
2128class page_wiki_prettyview extends page_wiki {
2129
2130 function print_header() {
231b0b30
DC
2131 global $CFG, $PAGE, $OUTPUT;
2132 $PAGE->set_pagelayout('embedded');
2133 echo $OUTPUT->header();
00710f4c 2134
110f4c93 2135 echo '<h1 id="wiki_printable_title">' . format_string($this->title) . '</h1>';
00710f4c
DC
2136 }
2137
2138 function print_content() {
2139 global $PAGE;
2140
2141 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2142 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
2143
2144 $this->print_pretty_view();
2145 }
2146
2147 function set_url() {
2148 global $PAGE, $CFG;
2149
2150 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2151 }
2152
00710f4c
DC
2153 private function print_pretty_view() {
2154 $version = wiki_get_current_version($this->page->id);
2155
2156 $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2157
2158 echo '<div id="wiki_printable_content">';
110f4c93 2159 echo format_text($content['parsed_text'], FORMAT_HTML);
00710f4c
DC
2160 echo '</div>';
2161 }
2162}
2163
2164class page_wiki_handlecomments extends page_wiki {
2165 private $action;
2166 private $content;
2167 private $commentid;
2168 private $format;
2169
2170 function print_header() {
2171 $this->set_url();
2172 }
2173
2174 public function print_content() {
d6887efc 2175 global $CFG, $PAGE, $USER;
00710f4c
DC
2176
2177 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
00710f4c 2178
cfffb69c
JP
2179 if ($this->action == 'add') {
2180 if (has_capability('mod/wiki:editcomment', $context)) {
9bf1b716
JP
2181 $this->add_comment($this->content, $this->commentid);
2182 }
2183 } else if ($this->action == 'edit') {
2184 $comment = wiki_get_comment($this->commentid);
2185 $edit = has_capability('mod/wiki:editcomment', $context);
d6887efc 2186 $owner = ($comment->userid == $USER->id);
cfffb69c 2187 if ($owner && $edit) {
9bf1b716
JP
2188 $this->add_comment($this->content, $this->commentid);
2189 }
00710f4c 2190 } else if ($this->action == 'delete') {
9bf1b716
JP
2191 $comment = wiki_get_comment($this->commentid);
2192 $manage = has_capability('mod/wiki:managecomment', $context);
d6887efc 2193 $owner = ($comment->userid == $USER->id);
cfffb69c 2194 if ($owner || $manage) {
9bf1b716 2195 $this->delete_comment($this->commentid);
d6887efc 2196 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
9bf1b716 2197 }
00710f4c
DC
2198 }
2199
2200 }
2201
2202 public function set_url() {
2203 global $PAGE, $CFG;
2204 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2205 }
2206
2207 public function set_action($action, $commentid, $content) {
2208 $this->action = $action;
2209 $this->commentid = $commentid;
2210 $this->content = $content;
2211
2212 $version = wiki_get_current_version($this->page->id);
2213 $format = $version->contentformat;
2214
2215 $this->format = $format;
2216 }
2217
2218 private function add_comment($content, $idcomment) {
2219 global $CFG, $PAGE;
2220 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2221
2222 $pageid = $this->page->id;
2223 $cm = $PAGE->cm;
2224 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2225
2226 wiki_add_comment($context, $pageid, $content, $this->format);
2227
2228 if (!$idcomment) {
2229 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2230 } else {
2231 $this->delete_comment($idcomment);
d6887efc 2232 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
00710f4c
DC
2233 }
2234 }
2235
2236 private function delete_comment($commentid) {
2237 global $CFG, $PAGE;
2238
2239 $cm = $PAGE->cm;
2240 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2241 $pageid = $this->page->id;
2242
2243 wiki_delete_comment($commentid, $context, $pageid);
00710f4c
DC
2244 }
2245
2246}
2247
2248class page_wiki_lock extends page_wiki_edit {
2249
2250 public function print_header() {
2251 $this->set_url();
2252 }
2253
2254 protected function set_url() {
2255 global $PAGE, $CFG;
2256
2257 $params = array('pageid' => $this->page->id);
2258
2259 if ($this->section) {
2260 $params['section'] = $this->section;
2261 }
2262
2263 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2264 }
2265
2266 protected function set_session_url() {
2267 }
2268
2269 public function print_content() {
2270 global $USER, $PAGE;
2271
2272 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2273 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
2274
2275 wiki_set_lock($this->page->id, $USER->id, $this->section);
2276 }
2277
2278 public function print_footer() {
2279 }
2280}
2281
2282class page_wiki_overridelocks extends page_wiki_edit {
2283 function print_header() {
2284 $this->set_url();
2285 }
2286
2287 function print_content() {
2288 global $CFG, $PAGE;
2289
2290 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2291 require_capability('mod/wiki:overridelock', $context, NULL, true, 'nooverridelockpermission', 'wiki');
2292
2293 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2294
2295 $args = "pageid=" . $this->page->id;
2296
2297 if (!empty($this->section)) {
2298 $args .= "&section=" . $this->section;
2299 }
2300
2301 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2302 }
2303
2304 function set_url() {
2305 global $PAGE, $CFG;
2306
2307 $params = array('pageid' => $this->page->id);
2308
2309 if (!empty($this->section)) {
2310 $params['section'] = $this->section;
2311 }
2312
2313 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2314 }
2315
2316 protected function set_session_url() {
2317 }
2318
2319 private function print_overridelocks() {
2320 global $CFG;
2321
2322 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2323
2324 $args = "pageid=" . $this->page->id;
2325
2326 if (!empty($this->section)) {
2327 $args .= "&section=" . $this->section;
2328 }
2329
2330 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2331 }
2332
2333}