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