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