3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file contains several classes uses to render the diferent pages
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
26 * @author Jordi Piguillem
28 * @author David Jimenez
30 * @author Daniel Serrano
31 * @author Kenneth Riba
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 require_once($CFG->dirroot . '/mod/wiki/edit_form.php');
37 require_once($CFG->dirroot . '/tag/lib.php');
40 * Class page_wiki contains the common code between all pages
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 abstract class page_wiki {
47 * @var object Current subwiki
52 * @var int Current page
57 * @var string Current page title
62 * @var int Current group ID
67 * @var object module context object
69 protected $modcontext;
72 * @var int Current user ID
76 * @var array The tabs set used in wiki module
78 protected $tabs = array('view' => 'view', 'edit' => 'edit', 'comments' => 'comments',
79 'history' => 'history', 'map' => 'map', 'files' => 'files',
82 * @var array tabs options
84 protected $tabs_options = array();
86 * @var object wiki renderer
88 protected $wikioutput;
91 * page_wiki constructor
93 * @param $wiki. Current wiki
94 * @param $subwiki. Current subwiki.
95 * @param $cm. Current course_module.
97 function __construct($wiki, $subwiki, $cm) {
99 $this->subwiki = $subwiki;
100 $this->modcontext = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
102 // initialise wiki renderer
103 $this->wikioutput = $PAGE->get_renderer('mod_wiki');
104 $PAGE->set_cacheable(true);
106 $PAGE->set_activity_record($wiki);
108 $PAGE->set_button(wiki_search_form($cm));
112 * This method prints the top of the page.
114 function print_header() {
115 global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
117 $PAGE->set_heading(format_string($PAGE->course->fullname));
121 if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
122 $this->process_session_url();
124 $this->set_session_url();
126 $this->create_navbar();
129 echo $OUTPUT->header();
131 echo $this->wikioutput->wiki_info();
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);
140 * Protected method to print current page title.
142 protected function print_pagetitle() {
146 $html .= $OUTPUT->container_start();
147 $html .= $OUTPUT->heading(format_string($this->title), 2, 'wiki_headingtitle');
148 $html .= $OUTPUT->container_end();
153 * Setup page tabs, if options is empty, will set up active tab automatically
154 * @param array $options, tabs options
156 protected function setup_tabs($options = array()) {
158 $groupmode = groups_get_activity_groupmode($PAGE->cm);
160 if (empty($CFG->usecomments) || !has_capability('mod/wiki:viewcomment', $PAGE->context)){
161 unset($this->tabs['comments']);
164 if (!has_capability('mod/wiki:editpage', $PAGE->context)){
165 unset($this->tabs['edit']);
168 if ($groupmode and $groupmode == VISIBLEGROUPS) {
169 $currentgroup = groups_get_activity_group($PAGE->cm);
170 $manage = has_capability('mod/wiki:managewiki', $PAGE->cm->context);
171 $edit = has_capability('mod/wiki:editpage', $PAGE->context);
172 if (!$manage and !($edit and groups_is_member($currentgroup))) {
173 unset($this->tabs['edit']);
176 if (!has_capability('mod/wiki:editpage', $PAGE->context)) {
177 unset($this->tabs['edit']);
182 if (empty($options)) {
183 $this->tabs_options = array('activetab' => substr(get_class($this), 10));
185 $this->tabs_options = $options;
191 * This method must be overwritten to print the page content.
193 function print_content() {
194 throw new coding_exception('Page wiki class does not implement method print_content()');
198 * Method to set the current page
200 * @param object $page Current page
202 function set_page($page) {
206 $this->title = $page->title;
207 $PAGE->set_title($this->title);
211 * Method to set the current page title.
212 * This method must be called when the current page is not created yet.
213 * @param string $title Current page title.
215 function set_title($title) {
219 $this->title = $title;
220 $PAGE->set_title($this->title);
224 * Method to set current group id
225 * @param int $gid Current group id
227 function set_gid($gid) {
232 * Method to set current user id
233 * @param int $uid Current user id
235 function set_uid($uid) {
240 * Method to set the URL of the page.
241 * This method must be overwritten by every type of page.
243 protected function set_url() {
244 throw new coding_exception('Page wiki class does not implement method set_url()');
248 * Protected method to create the common items of the navbar in every page type.
250 protected function create_navbar() {
253 $PAGE->navbar->add(format_string($this->title), $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
257 * This method print the footer of the page.
259 function print_footer() {
261 echo $OUTPUT->footer();
264 protected function process_session_url() {
265 global $USER, $SESSION;
267 //delete locks if edit
268 $url = $SESSION->wikipreviousurl;
269 switch ($url['page']) {
271 wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
276 protected function set_session_url() {
278 unset($SESSION->wikipreviousurl);
286 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
288 class page_wiki_view extends page_wiki {
290 * @var int the coursemodule id
292 private $coursemodule;
294 function print_header() {
297 parent::print_header();
299 $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page, 'view');
301 if (!empty($this->page)) {
302 echo $this->wikioutput->prettyview_link($this->page);
305 //echo $this->wikioutput->page_index();
307 $this->print_pagetitle();
310 function print_content() {
313 if (wiki_user_can_view($this->subwiki)) {
315 if (!empty($this->page)) {
316 wiki_print_page_content($this->page, $this->modcontext, $this->subwiki->id);
317 $wiki = $PAGE->activityrecord;
319 print_string('nocontent', 'wiki');
320 // TODO: fix this part
322 if (!empty($this->subwiki)) {
323 $swid = $this->subwiki->id;
327 // @TODO: Tranlate it
328 echo "You can not view this page";
336 if (isset($this->coursemodule)) {
337 $params['id'] = $this->coursemodule;
338 } else if (!empty($this->page) and $this->page != null) {
339 $params['pageid'] = $this->page->id;
340 } else if (!empty($this->gid)) {
341 $params['wid'] = $PAGE->cm->instance;
342 $params['group'] = $this->gid;
343 } else if (!empty($this->title)) {
344 $params['swid'] = $this->subwiki->id;
345 $params['title'] = $this->title;
347 print_error(get_string('invalidparameters', 'wiki'));
350 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/view.php', $params);
353 function set_coursemodule($id) {
354 $this->coursemodule = $id;
357 protected function create_navbar() {
360 $PAGE->navbar->add(format_string($this->title));
361 $PAGE->navbar->add(get_string('view', 'wiki'));
366 * Wiki page editing page
368 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
370 class page_wiki_edit extends page_wiki {
372 public static $attachmentoptions;
374 protected $sectioncontent;
375 /** @var string the section name needed to be edited */
377 protected $overridelock = false;
378 protected $versionnumber = -1;
379 protected $upload = false;
380 protected $attachments = 0;
381 protected $deleteuploads = array();
384 function __construct($wiki, $subwiki, $cm) {
386 parent::__construct($wiki, $subwiki, $cm);
387 self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*');
388 $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
389 $PAGE->requires->yui2_lib('connection');
392 protected function print_pagetitle() {
395 $title = $this->title;
396 if (isset($this->section)) {
397 $title .= ' : ' . $this->section;
399 echo $OUTPUT->container_start('wiki_clear');
400 echo $OUTPUT->heading(format_string($title), 2, 'wiki_headingtitle');
401 echo $OUTPUT->container_end();
404 function print_header() {
405 global $OUTPUT, $PAGE;
406 $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
408 parent::print_header();
410 $this->print_pagetitle();
412 print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
415 function print_content() {
418 if (wiki_user_can_edit($this->subwiki)) {
421 // @TODO: Translate it
422 echo "You can not edit this page";
426 protected function set_url() {
429 $params = array('pageid' => $this->page->id);
431 if (isset($this->section)) {
432 $params['section'] = $this->section;
435 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
438 protected function set_session_url() {
441 $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
444 protected function process_session_url() {
447 function set_section($sectioncontent, $section) {
448 $this->sectioncontent = $sectioncontent;
449 $this->section = $section;
452 public function set_versionnumber($versionnumber) {
453 $this->versionnumber = $versionnumber;
456 public function set_overridelock($override) {
457 $this->overridelock = $override;
460 function set_format($format) {
461 $this->format = $format;
464 public function set_upload($upload) {
465 $this->upload = $upload;
468 public function set_attachments($attachments) {
469 $this->attachments = $attachments;
472 public function set_deleteuploads($deleteuploads) {
473 $this->deleteuploads = $deleteuploads;
476 protected function create_navbar() {
479 parent::create_navbar();
481 $PAGE->navbar->add(get_string('edit', 'wiki'));
484 protected function check_locks() {
485 global $OUTPUT, $USER, $CFG;
487 if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
488 print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
490 if ($this->overridelock) {
491 $params = 'pageid=' . $this->page->id;
493 if ($this->section) {
494 $params .= '§ion=' . urlencode($this->section);
497 $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
498 $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
499 $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
502 print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
509 protected function print_edit($content = null) {
510 global $CFG, $OUTPUT, $USER, $PAGE;
512 if (!$this->check_locks()) {
516 //delete old locks (> 1 hour)
517 wiki_delete_old_locks();
519 $version = wiki_get_current_version($this->page->id);
520 $format = $version->contentformat;
522 if ($content == null) {
523 if (empty($this->section)) {
524 $content = $version->content;
526 $content = $this->sectioncontent;
530 $versionnumber = $version->version;
531 if ($this->versionnumber >= 0) {
532 if ($version->version != $this->versionnumber) {
533 print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
534 $versionnumber = $this->versionnumber;
538 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
539 if (!empty($this->section)) {
540 $url .= "§ion=" . urlencode($this->section);
543 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $version->contentformat, 'version' => $versionnumber, 'pagetitle'=>$this->page->title);
545 $data = new StdClass();
546 $data->newcontent = $content;
547 $data->version = $versionnumber;
548 $data->format = $format;
552 $data->newcontentformat = FORMAT_HTML;
553 // Append editor context to editor options, giving preference to existing context.
554 page_wiki_edit::$attachmentoptions = array_merge(array('context' => $this->modcontext), page_wiki_edit::$attachmentoptions);
555 $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
561 if ($version->contentformat != 'html') {
562 $params['fileitemid'] = $this->subwiki->id;
563 $params['contextid'] = $this->modcontext->id;
564 $params['component'] = 'mod_wiki';
565 $params['filearea'] = 'attachments';
568 if (!empty($CFG->usetags)) {
569 $params['tags'] = tag_get_tags_csv('wiki_pages', $this->page->id, TAG_RETURN_TEXT);
572 $form = new mod_wiki_edit_form($url, $params);
574 if ($formdata = $form->get_data()) {
575 if (!empty($CFG->usetags)) {
576 $data->tags = $formdata->tags;
579 if (!empty($CFG->usetags)) {
580 $data->tags = tag_get_tags_array('wiki', $this->page->id);
584 $form->set_data($data);
591 * Class that models the behavior of wiki's view comments page
593 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
595 class page_wiki_comments extends page_wiki {
597 function print_header() {
599 parent::print_header();
601 $this->print_pagetitle();
605 function print_content() {
606 global $CFG, $OUTPUT, $USER, $PAGE;
607 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
610 $subwiki = $this->subwiki;
611 $wiki = $PAGE->activityrecord;
612 list($context, $course, $cm) = get_context_info_array($this->modcontext->id);
614 require_capability('mod/wiki:viewcomment', $this->modcontext, NULL, true, 'noviewcommentpermission', 'wiki');
616 $comments = wiki_get_comments($this->modcontext->id, $page->id);
618 if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
619 echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
622 $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
623 $version = wiki_get_current_version($this->page->id);
624 $format = $version->contentformat;
626 if (empty($comments)) {
627 echo $OUTPUT->heading(get_string('nocomments', 'wiki'));
630 foreach ($comments as $comment) {
632 $user = wiki_get_user_info($comment->userid);
634 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
635 $by = new stdclass();
636 $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&course=' . $course->id . '">' . $fullname . '</a>';
637 $by->date = userdate($comment->timecreated);
639 $t = new html_table();
640 $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
641 $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
642 $cell3 = new html_table_cell();
643 $cell3->atributtes ['width'] = "80%";
644 $cell4 = new html_table_cell();
645 $cell5 = new html_table_cell();
647 $row1 = new html_table_row();
648 $row1->cells[] = $cell1;
649 $row1->cells[] = $cell2;
650 $row2 = new html_table_row();
651 $row2->cells[] = $cell3;
653 if ($format != 'html') {
654 if ($format == 'creole') {
655 $parsedcontent = wiki_parse_content('creole', $comment->content, $options);
656 } else if ($format == 'nwiki') {
657 $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options);
660 $cell4->text = format_text(html_entity_decode($parsedcontent['parsed_text']), FORMAT_HTML);
662 $cell4->text = format_text($comment->content, FORMAT_HTML);
665 $row2->cells[] = $cell4;
667 $t->data = array($row1, $row2);
669 $actionicons = false;
670 if ((has_capability('mod/wiki:managecomment', $this->modcontext))) {
671 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
672 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
674 } else if ((has_capability('mod/wiki:editcomment', $this->modcontext)) and ($USER->id == $user->id)) {
675 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
676 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
681 $cell6 = new html_table_cell($OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit'))) . $OUTPUT->action_icon($urldelet, new pix_icon('t/delete', get_string('delete'))));
682 $row3 = new html_table_row();
683 $row3->cells[] = $cell5;
684 $row3->cells[] = $cell6;
688 echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow'));
695 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
698 protected function create_navbar() {
701 parent::create_navbar();
702 $PAGE->navbar->add(get_string('comments', 'wiki'));
708 * Class that models the behavior of wiki's edit comment
710 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
712 class page_wiki_editcomment extends page_wiki {
720 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
723 function print_header() {
724 parent::print_header();
725 $this->print_pagetitle();
728 function print_content() {
731 require_capability('mod/wiki:editcomment', $this->modcontext, NULL, true, 'noeditcommentpermission', 'wiki');
733 if ($this->action == 'add') {
734 $this->add_comment_form();
735 } else if ($this->action == 'edit') {
736 $this->edit_comment_form($this->comment);
740 function set_action($action, $comment) {
742 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
744 $this->action = $action;
745 $this->comment = $comment;
746 $version = wiki_get_current_version($this->page->id);
747 $this->format = $version->contentformat;
749 if ($this->format == 'html') {
750 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
751 $this->form = new mod_wiki_comments_form($destination);
755 protected function create_navbar() {
758 $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
760 if ($this->action == 'add') {
761 $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
763 $PAGE->navbar->add(get_string('editcomment', 'wiki'));
767 protected function setup_tabs() {
768 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
771 private function add_comment_form() {
773 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
775 $pageid = $this->page->id;
777 if ($this->format == 'html') {
778 $com = new stdClass();
779 $com->action = 'add';
780 $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
781 $this->form->set_data($com);
782 $this->form->display();
784 wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'addcomments');
788 private function edit_comment_form($com) {
790 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
791 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
793 if ($this->format == 'html') {
794 $com->action = 'edit';
795 $com->entrycomment_editor['text'] = $com->content;
796 $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
798 $this->form->set_data($com);
799 $this->form->display();
801 wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'editcomments', $com->id);
809 * Wiki page search page
811 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
813 class page_wiki_search extends page_wiki {
814 private $search_result;
816 protected function create_navbar() {
819 $PAGE->navbar->add(format_string($this->title));
822 function set_search_string($search, $searchcontent) {
823 $swid = $this->subwiki->id;
824 if ($searchcontent) {
825 $this->search_result = wiki_search_all($swid, $search);
827 $this->search_result = wiki_search_title($swid, $search);
834 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
836 function print_content() {
839 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
841 echo $this->wikioutput->search_result($this->search_result, $this->subwiki);
847 * Class that models the behavior of wiki's
851 class page_wiki_create extends page_wiki {
859 function print_header() {
861 parent::print_header();
868 if ($this->action == 'new') {
869 $params['action'] = 'new';
870 $params['swid'] = $this->swid;
871 $params['wid'] = $this->wid;
872 if ($this->title != get_string('newpage', 'wiki')) {
873 $params['title'] = $this->title;
875 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
877 $params['action'] = 'create';
878 $params['swid'] = $this->swid;
879 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
883 function set_format($format) {
884 $this->format = $format;
887 function set_wid($wid) {
891 function set_swid($swid) {
895 function set_action($action) {
897 $this->action = $action;
899 require_once(dirname(__FILE__) . '/create_form.php');
900 $url = new moodle_url('/mod/wiki/create.php', array('action' => 'create', 'wid' => $PAGE->activityrecord->id, 'gid' => $this->gid, 'uid' => $this->uid));
901 $formats = wiki_get_formats();
902 $options = array('formats' => $formats, 'defaultformat' => $PAGE->activityrecord->defaultformat, 'forceformat' => $PAGE->activityrecord->forceformat);
903 if ($this->title != get_string('newpage', 'wiki')) {
904 $options['disable_pagetitle'] = true;
906 $this->mform = new mod_wiki_create_form($url->out(false), $options);
909 protected function create_navbar() {
912 $PAGE->navbar->add($this->title);
915 function print_content($pagetitle = '') {
918 // @TODO: Change this to has_capability and show an alternative interface.
919 require_capability('mod/wiki:createpage', $this->modcontext, NULL, true, 'nocreatepermission', 'wiki');
920 $data = new stdClass();
921 if (!empty($pagetitle)) {
922 $data->pagetitle = $pagetitle;
924 $data->pageformat = $PAGE->activityrecord->defaultformat;
926 $this->mform->set_data($data);
927 $this->mform->display();
930 function create_page($pagetitle) {
931 global $USER, $CFG, $PAGE;
932 $data = $this->mform->get_data();
933 if (empty($this->subwiki)) {
934 $swid = wiki_add_subwiki($PAGE->activityrecord->id, $this->gid, $this->uid);
935 $this->subwiki = wiki_get_subwiki($swid);
938 $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
940 $id = wiki_create_page($this->subwiki->id, $pagetitle, $PAGE->activityrecord->defaultformat, $USER->id);
942 redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $id);
946 class page_wiki_preview extends page_wiki_edit {
950 function __construct($wiki, $subwiki, $cm) {
951 global $PAGE, $CFG, $OUTPUT;
952 parent::__construct($wiki, $subwiki, $cm);
953 $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
954 $PAGE->set_button($buttons);
958 function print_header() {
961 parent::print_header();
965 function print_content() {
968 require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
970 $this->print_preview();
973 function set_newcontent($newcontent) {
974 $this->newcontent = $newcontent;
980 $params = array('pageid' => $this->page->id
983 if (isset($this->section)) {
984 $params['section'] = $this->section;
987 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
990 protected function setup_tabs() {
991 parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
994 protected function check_locks() {
998 protected function print_preview() {
999 global $CFG, $PAGE, $OUTPUT;
1001 $version = wiki_get_current_version($this->page->id);
1002 $format = $version->contentformat;
1003 $content = $version->content;
1005 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1006 if (!empty($this->section)) {
1007 $url .= "§ion=" . urlencode($this->section);
1009 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
1011 if ($this->format != 'html') {
1012 $params['contextid'] = $this->modcontext->id;
1013 $params['component'] = 'mod_wiki';
1014 $params['filearea'] = 'attachments';
1015 $params['fileitemid'] = $this->page->id;
1017 $form = new mod_wiki_edit_form($url, $params);
1020 $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
1022 if ($data = $form->get_data()) {
1023 if (isset($data->newcontent)) {
1025 $text = $data->newcontent;
1028 $text = $data->newcontent_editor['text'];
1030 $parseroutput = wiki_parse_content($data->contentformat, $text, $options);
1031 $this->set_newcontent($text);
1032 echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info');
1033 $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true, 'filter'=>false));
1034 echo $OUTPUT->box($content, 'generalbox wiki_previewbox');
1035 $content = $this->newcontent;
1038 $this->print_edit($content);
1045 * Class that models the behavior of wiki's
1049 class page_wiki_diff extends page_wiki {
1052 private $comparewith;
1054 function print_header() {
1057 parent::print_header();
1059 $this->print_pagetitle();
1060 $vstring = new stdClass();
1061 $vstring->old = $this->compare;
1062 $vstring->new = $this->comparewith;
1063 echo $OUTPUT->heading(get_string('comparewith', 'wiki', $vstring));
1067 * Print the diff view
1069 function print_content() {
1072 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1074 $this->print_diff_content();
1077 function set_url() {
1080 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/diff.php', array('pageid' => $this->page->id, 'comparewith' => $this->comparewith, 'compare' => $this->compare));
1083 function set_comparison($compare, $comparewith) {
1084 $this->compare = $compare;
1085 $this->comparewith = $comparewith;
1088 protected function create_navbar() {
1091 parent::create_navbar();
1092 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
1093 $PAGE->navbar->add(get_string('diff', 'wiki'));
1096 protected function setup_tabs() {
1097 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1101 * Given two versions of a page, prints a page displaying the differences between them.
1103 * @global object $CFG
1104 * @global object $OUTPUT
1105 * @global object $PAGE
1107 private function print_diff_content() {
1108 global $CFG, $OUTPUT, $PAGE;
1110 $pageid = $this->page->id;
1111 $total = wiki_count_wiki_page_versions($pageid) - 1;
1113 $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
1115 $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
1117 if ($oldversion && $newversion) {
1119 $oldtext = format_text(file_rewrite_pluginfile_urls($oldversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1120 $newtext = format_text(file_rewrite_pluginfile_urls($newversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1121 list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
1122 $oldversion->diff = $diff1;
1123 $oldversion->user = wiki_get_user_info($oldversion->userid);
1124 $newversion->diff = $diff2;
1125 $newversion->user = wiki_get_user_info($newversion->userid);
1127 echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
1129 print_error('versionerror', 'wiki');
1136 * Class that models the behavior of wiki's history page
1139 class page_wiki_history extends page_wiki {
1141 * @var int $paging current page
1146 * @var int @rowsperpage Items per page
1148 private $rowsperpage = 10;
1151 * @var int $allversion if $allversion != 0, all versions will be printed in a signle table
1153 private $allversion;
1155 function __construct($wiki, $subwiki, $cm) {
1157 parent::__construct($wiki, $subwiki, $cm);
1158 $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
1161 function print_header() {
1162 parent::print_header();
1163 $this->print_pagetitle();
1166 function print_pagetitle() {
1170 $html .= $OUTPUT->container_start();
1171 $html .= $OUTPUT->heading_with_help(format_string($this->title), 'history', 'wiki');
1172 $html .= $OUTPUT->container_end();
1176 function print_content() {
1179 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1181 $this->print_history_content();
1184 function set_url() {
1186 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1189 function set_paging($paging) {
1190 $this->paging = $paging;
1193 function set_allversion($allversion) {
1194 $this->allversion = $allversion;
1197 protected function create_navbar() {
1200 parent::create_navbar();
1201 $PAGE->navbar->add(get_string('history', 'wiki'));
1205 * Prints the history for a given wiki page
1207 * @global object $CFG
1208 * @global object $OUTPUT
1209 * @global object $PAGE
1211 private function print_history_content() {
1212 global $CFG, $OUTPUT, $PAGE;
1214 $pageid = $this->page->id;
1215 $offset = $this->paging * $this->rowsperpage;
1216 // vcount is the latest version
1217 $vcount = wiki_count_wiki_page_versions($pageid) - 1;
1218 if ($this->allversion) {
1219 $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
1221 $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
1223 // We don't want version 0 to be displayed
1224 // version 0 is blank page
1225 if (end($versions)->version == 0) {
1226 array_pop($versions);
1229 $contents = array();
1231 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
1232 $creator = wiki_get_user_info($version0page->userid);
1234 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
1235 $a->username = fullname($creator);
1236 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
1239 /// If there is only one version, we don't need radios nor forms
1240 if (count($versions) == 1) {
1242 $row = array_shift($versions);
1244 $username = wiki_get_user_info($row->userid);
1245 $picture = $OUTPUT->user_picture($username);
1246 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
1247 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
1248 $versionid = wiki_get_version($row->id);
1249 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1250 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
1251 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picture . html_writer::link($userlink->out(false), fullname($username)), $time, $OUTPUT->container($date, 'wiki_histdate'));
1253 $table = new html_table();
1254 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
1255 $table->data = $contents;
1256 $table->attributes['class'] = 'mdl-align';
1258 echo html_writer::table($table);
1262 $checked = $vcount - $offset;
1264 $rowclass = array();
1266 foreach ($versions as $version) {
1267 $user = wiki_get_user_info($version->userid);
1268 $picture = $OUTPUT->user_picture($user, array('popup' => true));
1269 $date = userdate($version->timecreated, get_string('strftimedate'));
1270 if ($date == $lastdate) {
1275 $rowclass[] = 'wiki_histnewdate';
1277 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1278 $versionid = wiki_get_version($version->id);
1280 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1281 $viewlink = html_writer::link($url->out(false), $version->version);
1283 $viewlink = $version->version;
1285 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
1286 $contents[] = array($this->choose_from_radio(array($version->version => null), 'compare', 'M.mod_wiki.history()', $checked - 1, true) . $this->choose_from_radio(array($version->version => null), 'comparewith', 'M.mod_wiki.history()', $checked, true), $viewlink, $picture . html_writer::link($userlink->out(false), fullname($user)), $time, $OUTPUT->container($date, 'wiki_histdate'));
1289 $table = new html_table();
1291 $icon = $OUTPUT->help_icon('diff', 'wiki');
1293 $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
1294 $table->data = $contents;
1295 $table->attributes['class'] = 'generaltable mdl-align';
1296 $table->rowclasses = $rowclass;
1298 /*$table = new StdClass();
1299 $table->head = array(helpbutton('diff', 'diff', 'wiki', true, false, '', true, ''),
1300 get_string('version'),
1302 get_string('modified'),
1304 $table->data = $contents;
1305 $table->class = 'mdl-align';
1306 $table->rowclass = $rowclass;*/
1309 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
1310 echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
1311 echo html_writer::table($table);
1312 echo html_writer::start_tag('div', array('class'=>'mdl-align'));
1313 echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
1314 echo html_writer::end_tag('div');
1315 echo html_writer::end_tag('form');
1318 print_string('nohistory', 'wiki');
1320 if (!$this->allversion) {
1321 //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&');
1322 // $pagingbar->pagevar = $pagevar;
1323 echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&');
1324 //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&','paging');
1326 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1327 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1329 if ($vcount > $this->rowsperpage && !$this->allversion) {
1330 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1331 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1336 * Given an array of values, creates a group of radio buttons to be part of a form
1338 * @param array $options An array of value-label pairs for the radio group (values as keys).
1339 * @param string $name Name of the radiogroup (unique in the form).
1340 * @param string $onclick Function to be executed when the radios are clicked.
1341 * @param string $checked The value that is already checked.
1342 * @param bool $return If true, return the HTML as a string, otherwise print it.
1344 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1346 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1348 static $idcounter = 0;
1354 $output = '<span class="radiogroup ' . $name . "\">\n";
1356 if (!empty($options)) {
1358 foreach ($options as $value => $label) {
1359 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1360 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1361 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1362 if ($value == $checked) {
1363 $output .= ' checked="checked"';
1366 $output .= ' onclick="' . $onclick . '"';
1368 if ($label === '') {
1369 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1371 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1373 $currentradio = ($currentradio + 1) % 2;
1377 $output .= '</span>' . "\n";
1388 * Class that models the behavior of wiki's map page
1391 class page_wiki_map extends page_wiki {
1394 * @var int wiki view option
1398 function print_header() {
1399 parent::print_header();
1400 $this->print_pagetitle();
1403 function print_content() {
1406 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1408 if ($this->view > 0) {
1409 //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
1412 switch ($this->view) {
1414 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1415 $this->print_contributions_content();
1418 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1419 $this->print_navigation_content();
1422 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1423 $this->print_orphaned_content();
1426 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1427 $this->print_index_content();
1430 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1431 $this->print_page_list_content();
1434 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1435 $this->print_updated_content();
1438 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1439 $this->print_page_list_content();
1443 function set_view($option) {
1444 $this->view = $option;
1447 function set_url() {
1449 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1452 protected function create_navbar() {
1455 parent::create_navbar();
1456 $PAGE->navbar->add(get_string('map', 'wiki'));
1460 * Prints the contributions tab content
1462 * @uses $OUTPUT, $USER
1465 private function print_contributions_content() {
1466 global $CFG, $OUTPUT, $USER;
1467 $page = $this->page;
1469 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1470 $fresh = wiki_refresh_cachedcontent($page);
1471 $page = $fresh['page'];
1474 $swid = $this->subwiki->id;
1476 $table = new html_table();
1477 $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
1478 $table->attributes['class'] = 'wiki_editor generalbox';
1479 $table->data = array();
1480 $table->rowclasses = array();
1482 $lastversions = array();
1486 if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1487 foreach ($contribs as $contrib) {
1488 if (!array_key_exists($contrib->pageid, $pages)) {
1489 $page = wiki_get_page($contrib->pageid);
1490 $pages[$contrib->pageid] = $page;
1495 if (!array_key_exists($page->id, $lastversions)) {
1496 $version = wiki_get_last_version($page->id);
1497 $lastversions[$page->id] = $version;
1499 $version = $lastversions[$page->id];
1502 if (!array_key_exists($version->userid, $users)) {
1503 $user = wiki_get_user_info($version->userid);
1504 $users[$version->userid] = $user;
1506 $user = $users[$version->userid];
1509 $link = wiki_parser_link(format_string($page->title), array('swid' => $swid));
1510 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1512 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
1513 $icon = $OUTPUT->user_picture($user, array('popup' => true));
1515 $table->data[] = array("$icon $linkpage");
1518 $table->data[] = array(get_string('nocontribs', 'wiki'));
1520 echo html_writer::table($table);
1524 * Prints the navigation tab content
1529 private function print_navigation_content() {
1531 $page = $this->page;
1533 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1534 $fresh = wiki_refresh_cachedcontent($page);
1535 $page = $fresh['page'];
1538 $tolinks = wiki_get_linked_to_pages($page->id);
1539 $fromlinks = wiki_get_linked_from_pages($page->id);
1541 $table = new html_table();
1542 $table->attributes['class'] = 'wiki_navigation_from';
1543 $table->head = array(get_string('navigationfrom', 'wiki') . $OUTPUT->help_icon('navigationfrom', 'wiki') . ':');
1544 $table->data = array();
1545 $table->rowclasses = array();
1546 foreach ($fromlinks as $link) {
1547 $lpage = wiki_get_page($link->frompageid);
1548 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1549 $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
1550 $table->rowclasses[] = 'mdl-align';
1553 $table_left = html_writer::table($table);
1555 $table = new html_table();
1556 $table->attributes['class'] = 'wiki_navigation_to';
1557 $table->head = array(get_string('navigationto', 'wiki') . $OUTPUT->help_icon('navigationto', 'wiki') . ':');
1558 $table->data = array();
1559 $table->rowclasses = array();
1560 foreach ($tolinks as $link) {
1561 if ($link->tomissingpage) {
1562 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
1563 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
1565 $lpage = wiki_get_page($link->topageid);
1566 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1567 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
1569 $table->rowclasses[] = 'mdl-align';
1571 $table_right = html_writer::table($table);
1572 echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1576 * Prints the index page tab content
1580 private function print_index_content() {
1582 $page = $this->page;
1584 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1585 $fresh = wiki_refresh_cachedcontent($page);
1586 $page = $fresh['page'];
1589 $node = new navigation_node($page->title);
1593 $tree = wiki_build_tree($page, $node, $keys);
1595 $table = new html_table();
1596 $table->head = array(get_string('pageindex', 'wiki') . $OUTPUT->help_icon('pageindex', 'wiki'));
1597 $table->attributes['class'] = 'wiki_editor generalbox';
1598 $table->data[] = array($this->render_navigation_node($tree));
1600 echo html_writer::table($table);
1604 * Prints the page list tab content
1608 private function print_page_list_content() {
1610 $page = $this->page;
1612 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1613 $fresh = wiki_refresh_cachedcontent($page);
1614 $page = $fresh['page'];
1617 $pages = wiki_get_page_list($this->subwiki->id);
1619 $stdaux = new stdClass();
1620 $strspecial = get_string('special', 'wiki');
1622 foreach ($pages as $page) {
1623 $letter = textlib::strtoupper(textlib::substr($page->title, 0, 1));
1624 if (preg_match('/[A-Z]/', $letter)) {
1627 [] = wiki_parser_link($page);
1631 [] = wiki_parser_link($page);
1635 $table = new html_table();
1636 $table->head = array(get_string('pagelist', 'wiki') . $OUTPUT->help_icon('pagelist', 'wiki'));
1637 $table->attributes['class'] = 'wiki_editor generalbox';
1638 $table->align = array('center');
1639 foreach ($stdaux as $key => $elem) {
1640 $table->data[] = array($key);
1641 foreach ($elem as $e) {
1642 $table->data[] = array(html_writer::link($e['url'], $e['content']));
1645 echo html_writer::table($table);
1649 * Prints the orphaned tab content
1653 private function print_orphaned_content() {
1656 $page = $this->page;
1658 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1659 $fresh = wiki_refresh_cachedcontent($page);
1660 $page = $fresh['page'];
1663 $swid = $this->subwiki->id;
1665 $table = new html_table();
1666 $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
1667 $table->attributes['class'] = 'wiki_editor generalbox';
1668 $table->data = array();
1669 $table->rowclasses = array();
1671 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1672 foreach ($orphanedpages as $page) {
1673 $link = wiki_parser_link($page->title, array('swid' => $swid));
1674 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1675 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
1678 $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1681 echo html_writer::table($table);
1685 * Prints the updated tab content
1687 * @uses $COURSE, $OUTPUT
1690 private function print_updated_content() {
1691 global $COURSE, $OUTPUT;
1692 $page = $this->page;
1694 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1695 $fresh = wiki_refresh_cachedcontent($page);
1696 $page = $fresh['page'];
1699 $swid = $this->subwiki->id;
1701 $table = new html_table();
1702 $table->head = array(get_string('updatedpages', 'wiki') . $OUTPUT->help_icon('updatedpages', 'wiki'));
1703 $table->attributes['class'] = 'wiki_editor generalbox';
1704 $table->data = array();
1705 $table->rowclasses = array();
1707 if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1709 foreach ($pages as $page) {
1710 $user = wiki_get_user_info($page->userid);
1711 $strdata = strftime('%d %b %Y', $page->timemodified);
1712 if ($strdata != $strdataux) {
1713 $table->data[] = array($OUTPUT->heading($strdata, 4));
1714 $strdataux = $strdata;
1716 $link = wiki_parser_link($page->title, array('swid' => $swid));
1717 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1719 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
1720 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1721 $table->data[] = array("$icon $linkpage");
1724 $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1727 echo html_writer::table($table);
1730 protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1732 // exit if empty, we don't want an empty ul element
1733 if (count($items) == 0) {
1737 // array of nested li elements
1739 foreach ($items as $item) {
1740 if (!$item->display) {
1743 $content = $item->get_content();
1744 $title = $item->get_title();
1745 if ($item->icon instanceof renderable) {
1746 $icon = $this->wikioutput->render($item->icon);
1747 $content = $icon . ' ' . $content; // use CSS for spacing of icons
1749 if ($item->helpbutton !== null) {
1750 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1753 if ($content === '') {
1757 if ($item->action instanceof action_link) {
1758 //TODO: to be replaced with something else
1759 $link = $item->action;
1760 if ($item->hidden) {
1761 $link->add_class('dimmed');
1763 $content = $this->output->render($link);
1764 } else if ($item->action instanceof moodle_url) {
1765 $attributes = array();
1766 if ($title !== '') {
1767 $attributes['title'] = $title;
1769 if ($item->hidden) {
1770 $attributes['class'] = 'dimmed_text';
1772 $content = html_writer::link($item->action, $content, $attributes);
1774 } else if (is_string($item->action) || empty($item->action)) {
1775 $attributes = array();
1776 if ($title !== '') {
1777 $attributes['title'] = $title;
1779 if ($item->hidden) {
1780 $attributes['class'] = 'dimmed_text';
1782 $content = html_writer::tag('span', $content, $attributes);
1785 // this applies to the li item which contains all child lists too
1786 $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1787 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1788 $liclasses[] = 'collapsed';
1790 if ($item->isactive === true) {
1791 $liclasses[] = 'current_branch';
1793 $liattr = array('class' => join(' ', $liclasses));
1794 // class attribute on the div item which only contains the item content
1795 $divclasses = array('tree_item');
1796 if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1797 $divclasses[] = 'branch';
1799 $divclasses[] = 'leaf';
1801 if (!empty($item->classes) && count($item->classes) > 0) {
1802 $divclasses[] = join(' ', $item->classes);
1804 $divattr = array('class' => join(' ', $divclasses));
1805 if (!empty($item->id)) {
1806 $divattr['id'] = $item->id;
1808 $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1809 if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1810 $content = html_writer::empty_tag('hr') . $content;
1812 $content = html_writer::tag('li', $content, $liattr);
1817 return html_writer::tag('ul', implode("\n", $lis), $attrs);
1826 * Class that models the behavior of wiki's restore version page
1829 class page_wiki_restoreversion extends page_wiki {
1832 function print_header() {
1833 parent::print_header();
1834 $this->print_pagetitle();
1837 function print_content() {
1840 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
1842 $this->print_restoreversion();
1845 function set_url() {
1847 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1850 function set_versionid($versionid) {
1851 $this->version = wiki_get_version($versionid);
1854 protected function create_navbar() {
1857 parent::create_navbar();
1858 $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1861 protected function setup_tabs() {
1862 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1866 * Prints the restore version content
1870 * @param page $page The page whose version will be restored
1871 * @param int $versionid The version to be restored
1872 * @param bool $confirm If false, shows a yes/no confirmation page.
1873 * If true, restores the old version and redirects the user to the 'view' tab.
1875 private function print_restoreversion() {
1878 $version = wiki_get_version($this->version->id);
1880 $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'versionid'=>$version->id, 'sesskey'=>sesskey());
1881 $restoreurl = new moodle_url('/mod/wiki/restoreversion.php', $optionsyes);
1882 $return = new moodle_url('/mod/wiki/viewversion.php', array('pageid'=>$this->page->id, 'versionid'=>$version->id));
1884 echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
1885 print_container_start(false, 'wiki_restoreform');
1886 echo '<form class="wiki_restore_yes" action="' . $restoreurl . '" method="post" id="restoreversion">';
1887 echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1889 echo '<form class="wiki_restore_no" action="' . $return . '" method="post">';
1890 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1892 print_container_end();
1896 * Class that models the behavior of wiki's delete comment confirmation page
1899 class page_wiki_deletecomment extends page_wiki {
1902 function print_header() {
1903 parent::print_header();
1904 $this->print_pagetitle();
1907 function print_content() {
1908 $this->printconfirmdelete();
1911 function set_url() {
1913 $PAGE->set_url('/mod/wiki/instancecomments.php', array('pageid' => $this->page->id, 'commentid' => $this->commentid));
1916 public function set_action($action, $commentid, $content) {
1917 $this->action = $action;
1918 $this->commentid = $commentid;
1919 $this->content = $content;
1922 protected function create_navbar() {
1925 parent::create_navbar();
1926 $PAGE->navbar->add(get_string('deletecommentcheck', 'wiki'));
1929 protected function setup_tabs() {
1930 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
1934 * Prints the comment deletion confirmation form
1936 * @param page $page The page whose version will be restored
1937 * @param int $versionid The version to be restored
1938 * @param bool $confirm If false, shows a yes/no confirmation page.
1939 * If true, restores the old version and redirects the user to the 'view' tab.
1941 private function printconfirmdelete() {
1944 $strdeletecheck = get_string('deletecommentcheck', 'wiki');
1945 $strdeletecheckfull = get_string('deletecommentcheckfull', 'wiki');
1948 $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'action'=>'delete', 'commentid'=>$this->commentid, 'sesskey'=>sesskey());
1949 $deleteurl = new moodle_url('/mod/wiki/instancecomments.php', $optionsyes);
1950 $return = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$this->page->id));
1952 echo $OUTPUT->heading($strdeletecheckfull);
1953 print_container_start(false, 'wiki_deletecommentform');
1954 echo '<form class="wiki_deletecomment_yes" action="' . $deleteurl . '" method="post" id="deletecomment">';
1955 echo '<div><input type="submit" name="confirmdeletecomment" value="' . get_string('yes') . '" /></div>';
1957 echo '<form class="wiki_deletecomment_no" action="' . $return . '" method="post">';
1958 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1960 print_container_end();
1965 * Class that models the behavior of wiki's
1969 class page_wiki_save extends page_wiki_edit {
1971 private $newcontent;
1973 function print_header() {
1976 function print_content() {
1979 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1980 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
1982 $this->print_save();
1985 function set_newcontent($newcontent) {
1986 $this->newcontent = $newcontent;
1989 protected function set_session_url() {
1992 protected function print_save() {
1993 global $CFG, $USER, $OUTPUT, $PAGE;
1995 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1996 if (!empty($this->section)) {
1997 $url .= "§ion=" . urlencode($this->section);
2000 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
2002 if ($this->format != 'html') {
2003 $params['fileitemid'] = $this->page->id;
2004 $params['contextid'] = $this->modcontext->id;
2005 $params['component'] = 'mod_wiki';
2006 $params['filearea'] = 'attachments';
2009 $form = new mod_wiki_edit_form($url, $params);
2013 if ($data = $form->get_data()) {
2014 if ($this->format == 'html') {
2015 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
2018 if (isset($this->section)) {
2019 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
2021 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
2025 if ($save && $data) {
2026 if (!empty($CFG->usetags)) {
2027 tag_set('wiki_pages', $this->page->id, $data->tags);
2030 $message = '<p>' . get_string('saving', 'wiki') . '</p>';
2032 if (!empty($save['sections'])) {
2033 foreach ($save['sections'] as $s) {
2034 $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
2038 if ($this->versionnumber + 1 != $save['version']) {
2039 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
2042 if (isset($errors) && !empty($errors)) {
2043 foreach ($errors as $e) {
2044 $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
2048 //deleting old locks
2049 wiki_delete_locks($this->page->id, $USER->id, $this->section);
2051 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
2053 print_error('savingerror', 'wiki');
2059 * Class that models the behavior of wiki's view an old version of a page
2062 class page_wiki_viewversion extends page_wiki {
2066 function print_header() {
2067 parent::print_header();
2068 $this->print_pagetitle();
2071 function print_content() {
2074 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2076 $this->print_version_view();
2079 function set_url() {
2081 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2084 function set_versionid($versionid) {
2085 $this->version = wiki_get_version($versionid);
2088 protected function create_navbar() {
2091 parent::create_navbar();
2092 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
2093 $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
2096 protected function setup_tabs() {
2097 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
2101 * Given an old page version, output the version content
2103 * @global object $CFG
2104 * @global object $OUTPUT
2105 * @global object $PAGE
2107 private function print_version_view() {
2108 global $CFG, $OUTPUT, $PAGE;
2109 $pageversion = wiki_get_version($this->version->id);
2112 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2113 echo $OUTPUT->heading(get_string('viewversion', 'wiki', $pageversion->version) . '<br />' . html_writer::link($restorelink->out(false), '(' . get_string('restorethis', 'wiki') . ')', array('class' => 'wiki_restore')) . ' ', 4);
2114 $userinfo = wiki_get_user_info($pageversion->userid);
2115 $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong> ' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2116 $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2117 $heading .= ' <strong>' . get_string('user') . ':</strong> ' . html_writer::link($viewlink->out(false), fullname($userinfo));
2118 $heading .= ' → ' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2119 print_container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
2120 $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2122 $pageversion->content = file_rewrite_pluginfile_urls($pageversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id);
2124 $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
2125 $content = print_container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
2126 echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2129 print_error('versionerror', 'wiki');
2134 class page_wiki_confirmrestore extends page_wiki_save {
2138 function set_url() {
2140 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2143 function print_content() {
2146 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
2148 $version = wiki_get_version($this->version->id);
2149 if (wiki_restore_page($this->page, $version->content, $version->userid)) {
2150 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2152 print_error('restoreerror', 'wiki', $version->version);
2156 function set_versionid($versionid) {
2157 $this->version = wiki_get_version($versionid);
2161 class page_wiki_prettyview extends page_wiki {
2163 function print_header() {
2164 global $CFG, $PAGE, $OUTPUT;
2165 $PAGE->set_pagelayout('embedded');
2166 echo $OUTPUT->header();
2168 echo '<h1 id="wiki_printable_title">' . format_string($this->title) . '</h1>';
2171 function print_content() {
2174 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2176 $this->print_pretty_view();
2179 function set_url() {
2182 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2185 private function print_pretty_view() {
2186 $version = wiki_get_current_version($this->page->id);
2188 $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2190 echo '<div id="wiki_printable_content">';
2191 echo format_text($content['parsed_text'], FORMAT_HTML);
2196 class page_wiki_handlecomments extends page_wiki {
2202 function print_header() {
2206 public function print_content() {
2207 global $CFG, $PAGE, $USER;
2209 if ($this->action == 'add') {
2210 if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
2211 $this->add_comment($this->content, $this->commentid);
2213 } else if ($this->action == 'edit') {
2214 $comment = wiki_get_comment($this->commentid);
2215 $edit = has_capability('mod/wiki:editcomment', $this->modcontext);
2216 $owner = ($comment->userid == $USER->id);
2217 if ($owner && $edit) {
2218 $this->add_comment($this->content, $this->commentid);
2220 } else if ($this->action == 'delete') {
2221 $comment = wiki_get_comment($this->commentid);
2222 $manage = has_capability('mod/wiki:managecomment', $this->modcontext);
2223 $owner = ($comment->userid == $USER->id);
2224 if ($owner || $manage) {
2225 $this->delete_comment($this->commentid);
2226 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
2232 public function set_url() {
2234 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2237 public function set_action($action, $commentid, $content) {
2238 $this->action = $action;
2239 $this->commentid = $commentid;
2240 $this->content = $content;
2242 $version = wiki_get_current_version($this->page->id);
2243 $format = $version->contentformat;
2245 $this->format = $format;
2248 private function add_comment($content, $idcomment) {
2250 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2252 $pageid = $this->page->id;
2254 wiki_add_comment($this->modcontext, $pageid, $content, $this->format);
2257 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2259 $this->delete_comment($idcomment);
2260 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
2264 private function delete_comment($commentid) {
2267 $pageid = $this->page->id;
2269 wiki_delete_comment($commentid, $this->modcontext, $pageid);
2274 class page_wiki_lock extends page_wiki_edit {
2276 public function print_header() {
2280 protected function set_url() {
2283 $params = array('pageid' => $this->page->id);
2285 if ($this->section) {
2286 $params['section'] = $this->section;
2289 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2292 protected function set_session_url() {
2295 public function print_content() {
2296 global $USER, $PAGE;
2298 require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
2300 wiki_set_lock($this->page->id, $USER->id, $this->section);
2303 public function print_footer() {
2307 class page_wiki_overridelocks extends page_wiki_edit {
2308 function print_header() {
2312 function print_content() {
2315 require_capability('mod/wiki:overridelock', $this->modcontext, NULL, true, 'nooverridelockpermission', 'wiki');
2317 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2319 $args = "pageid=" . $this->page->id;
2321 if (!empty($this->section)) {
2322 $args .= "§ion=" . urlencode($this->section);
2325 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2328 function set_url() {
2331 $params = array('pageid' => $this->page->id);
2333 if (!empty($this->section)) {
2334 $params['section'] = $this->section;
2337 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2340 protected function set_session_url() {
2343 private function print_overridelocks() {
2346 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2348 $args = "pageid=" . $this->page->id;
2350 if (!empty($this->section)) {
2351 $args .= "§ion=" . urlencode($this->section);
2354 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2360 * This class will let user to delete wiki pages and page versions
2363 class page_wiki_admin extends page_wiki {
2365 public $view, $action;
2366 public $listorphan = false;
2371 * @global object $PAGE
2372 * @param mixed $wiki instance of wiki
2373 * @param mixed $subwiki instance of subwiki
2374 * @param stdClass $cm course module
2376 function __construct($wiki, $subwiki, $cm) {
2378 parent::__construct($wiki, $subwiki, $cm);
2379 $PAGE->requires->js_init_call('M.mod_wiki.deleteversion', null, true);
2383 * Prints header for wiki page
2385 function print_header() {
2386 parent::print_header();
2387 $this->print_pagetitle();
2391 * This function will display administration view to users with managewiki capability
2393 function print_content() {
2394 //make sure anyone trying to access this page has managewiki capabilities
2395 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2397 //update wiki cache if timedout
2398 $page = $this->page;
2399 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
2400 $fresh = wiki_refresh_cachedcontent($page);
2401 $page = $fresh['page'];
2404 //dispaly admin menu
2405 echo $this->wikioutput->menu_admin($this->page->id, $this->view);
2407 //Display appropriate admin view
2408 switch ($this->view) {
2409 case 1: //delete page view
2410 $this->print_delete_content($this->listorphan);
2412 case 2: //delete version view
2413 $this->print_delete_version();
2415 default: //default is delete view
2416 $this->print_delete_content($this->listorphan);
2422 * Sets admin view option
2424 * @param int $view page view id
2425 * @param bool $listorphan is only valid for view 1.
2427 public function set_view($view, $listorphan = true) {
2428 $this->view = $view;
2429 $this->listorphan = $listorphan;
2435 * @global object $PAGE
2436 * @global object $CFG
2438 function set_url() {
2440 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/admin.php', array('pageid' => $this->page->id));
2444 * sets navigation bar for the page
2446 * @global object $PAGE
2448 protected function create_navbar() {
2451 parent::create_navbar();
2452 $PAGE->navbar->add(get_string('admin', 'wiki'));
2456 * Show wiki page delete options
2458 * @param bool $showorphan
2460 protected function print_delete_content($showorphan = true) {
2461 $contents = array();
2462 $table = new html_table();
2463 $table->head = array('','Page name');
2464 $table->attributes['class'] = 'generaltable mdl-align';
2465 $swid = $this->subwiki->id;
2467 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
2468 $this->add_page_delete_options($orphanedpages, $swid, $table);
2470 $table->data[] = array('', get_string('noorphanedpages', 'wiki'));
2473 if ($pages = wiki_get_page_list($swid)) {
2474 $this->add_page_delete_options($pages, $swid, $table);
2476 $table->data[] = array('', get_string('nopages', 'wiki'));
2481 echo html_writer::start_tag('form', array(
2482 'action' => new moodle_url('/mod/wiki/admin.php'),
2483 'method' => 'post'));
2484 echo html_writer::tag('div', html_writer::empty_tag('input', array(
2487 'value' => $this->page->id)));
2489 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2490 echo html_writer::table($table);
2491 echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2493 echo html_writer::empty_tag('input', array(
2495 'class' => 'wiki_form-button',
2496 'value' => get_string('listorphan', 'wiki'),
2497 'sesskey' => sesskey()));
2499 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'listall', 'value'=>'1'));
2500 echo html_writer::empty_tag('input', array(
2502 'class' => 'wiki_form-button',
2503 'value' => get_string('listall', 'wiki'),
2504 'sesskey' => sesskey()));
2506 echo html_writer::end_tag('div');
2507 echo html_writer::end_tag('form');
2511 * helper function for print_delete_content. This will add data to the table.
2513 * @global object $OUTPUT
2514 * @param array $pages objects of wiki pages in subwiki
2515 * @param int $swid id of subwiki
2516 * @param object $table reference to the table in which data needs to be added
2518 protected function add_page_delete_options($pages, $swid, &$table) {
2520 foreach ($pages as $page) {
2521 $link = wiki_parser_link($page->title, array('swid' => $swid));
2522 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
2523 $pagelink = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
2524 $urledit = new moodle_url('/mod/wiki/edit.php', array('pageid' => $page->id, 'sesskey' => sesskey()));
2525 $urldelete = new moodle_url('/mod/wiki/admin.php', array(
2526 'pageid' => $this->page->id,
2527 'delete' => $page->id,
2528 'option' => $this->view,
2529 'listall' => !$this->listorphan?'1': '',
2530 'sesskey' => sesskey()));
2532 $editlinks = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit')));
2533 $editlinks .= $OUTPUT->action_icon($urldelete, new pix_icon('t/delete', get_string('delete')));
2534 $table->data[] = array($editlinks, $pagelink);
2539 * Prints lists of versions which can be deleted
2541 * @global object $OUTPUT
2543 private function print_delete_version() {
2545 $pageid = $this->page->id;
2547 // versioncount is the latest version
2548 $versioncount = wiki_count_wiki_page_versions($pageid) - 1;
2549 $versions = wiki_get_wiki_page_versions($pageid, 0, $versioncount);
2551 // We don't want version 0 to be displayed
2552 // version 0 is blank page
2553 if (end($versions)->version == 0) {
2554 array_pop($versions);
2557 $contents = array();
2558 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
2559 $creator = wiki_get_user_info($version0page->userid);
2560 $a = new stdClass();
2561 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
2562 $a->username = fullname($creator);
2563 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
2564 if ($versioncount > 0) {
2565 /// If there is only one version, we don't need radios nor forms
2566 if (count($versions) == 1) {
2567 $row = array_shift($versions);
2568 $username = wiki_get_user_info($row->userid);
2569 $picture = $OUTPUT->user_picture($username);
2570 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
2571 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
2572 $versionid = wiki_get_version($row->id);
2573 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2574 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
2575 $picturelink = $picture . html_writer::link($userlink->out(false), fullname($username));
2576 $historydate = $OUTPUT->container($date, 'wiki_histdate');
2577 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picturelink, $time, $historydate);
2579 //Show current version
2580 $table = new html_table();
2581 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
2582 $table->data = $contents;
2583 $table->attributes['class'] = 'mdl-align';
2585 echo html_writer::table($table);
2588 $rowclass = array();
2590 foreach ($versions as $version) {
2591 $user = wiki_get_user_info($version->userid);
2592 $picture = $OUTPUT->user_picture($user, array('popup' => true));
2593 $date = userdate($version->timecreated, get_string('strftimedate'));
2594 if ($date == $lastdate) {
2599 $rowclass[] = 'wiki_histnewdate';
2602 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
2603 $versionid = wiki_get_version($version->id);
2605 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2606 $viewlink = html_writer::link($url->out(false), $version->version);
2608 $viewlink = $version->version;
2611 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
2612 $picturelink = $picture . html_writer::link($userlink->out(false), fullname($user));
2613 $historydate = $OUTPUT->container($date, 'wiki_histdate');
2614 $radiofromelement = $this->choose_from_radio(array($version->version => null), 'fromversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2615 $radiotoelement = $this->choose_from_radio(array($version->version => null), 'toversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2616 $contents[] = array( $radiofromelement . $radiotoelement, $viewlink, $picturelink, $time, $historydate);
2619 $table = new html_table();
2620 $table->head = array(get_string('deleteversions', 'wiki'), get_string('version'), get_string('user'), get_string('modified'), '');
2621 $table->data = $contents;
2622 $table->attributes['class'] = 'generaltable mdl-align';
2623 $table->rowclasses = $rowclass;
2626 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/admin.php'), 'method' => 'post'));
2627 echo html_writer::tag('div', html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pageid', 'value' => $pageid)));
2628 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2629 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
2630 echo html_writer::table($table);
2631 echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2632 echo html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'wiki_form-button', 'value' => get_string('deleteversions', 'wiki')));
2633 echo html_writer::end_tag('div');
2634 echo html_writer::end_tag('form');
2637 print_string('nohistory', 'wiki');
2642 * Given an array of values, creates a group of radio buttons to be part of a form
2643 * helper function for print_delete_version
2645 * @param array $options An array of value-label pairs for the radio group (values as keys).
2646 * @param string $name Name of the radiogroup (unique in the form).
2647 * @param string $onclick Function to be executed when the radios are clicked.
2648 * @param string $checked The value that is already checked.
2649 * @param bool $return If true, return the HTML as a string, otherwise print it.
2651 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
2653 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
2655 static $idcounter = 0;
2661 $output = '<span class="radiogroup ' . $name . "\">\n";
2663 if (!empty($options)) {
2665 foreach ($options as $value => $label) {
2666 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
2667 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
2668 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
2669 if ($value == $checked) {
2670 $output .= ' checked="checked"';
2673 $output .= ' onclick="' . $onclick . '"';
2675 if ($label === '') {
2676 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
2678 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
2680 $currentradio = ($currentradio + 1) % 2;
2684 $output .= '</span>' . "\n";