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