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