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