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
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 abstract class page_wiki {
48 * @var object Current subwiki
53 * @var int Current page
58 * @var string Current page title
63 * @var int Current group ID
68 * @var int Current user ID
72 * @var array The tabs set used in wiki module
74 protected $tabs = array('view' => 'view', 'edit' => 'edit', 'comments' => 'comments',
75 'history' => 'history', 'map' => 'map');
77 * @var array tabs options
79 protected $tabs_options = array();
81 * @var object wiki renderer
83 protected $wikioutput;
86 * page_wiki constructor
88 * @param $wiki. Current wiki
89 * @param $subwiki. Current subwiki.
90 * @param $cm. Current course_module.
92 function __construct($wiki, $subwiki, $cm) {
94 $this->subwiki = $subwiki;
96 // initialise wiki renderer
97 $this->wikioutput = $PAGE->get_renderer('mod_wiki');
98 $PAGE->set_cacheable(true);
100 $PAGE->set_activity_record($wiki);
102 $PAGE->set_button(wiki_search_form($cm));
106 * This method prints the top of the page.
108 function print_header() {
109 global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
111 $PAGE->set_heading(format_string($PAGE->course->fullname));
115 if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
116 $this->process_session_url();
118 $this->set_session_url();
120 $this->create_navbar();
123 echo $OUTPUT->header();
125 echo $this->wikioutput->wiki_info();
127 // tabs are associated with pageid, so if page is empty, tabs should be disabled
128 if (!empty($this->page) && !empty($this->tabs)) {
129 echo $this->wikioutput->tabs($this->page, $this->tabs, $this->tabs_options);
134 * Protected method to print current page title.
136 protected function print_pagetitle() {
140 $html .= $OUTPUT->container_start();
141 $html .= $OUTPUT->heading(format_string($this->title), 2, 'wiki_headingtitle');
142 $html .= $OUTPUT->container_end();
147 * Setup page tabs, if options is empty, will set up active tab automatically
148 * @param array $options, tabs options
150 protected function setup_tabs($options = array()) {
153 if (empty($CFG->usecomments) || !has_capability('mod/wiki:viewcomment', $PAGE->context)){
154 unset($this->tabs['comments']);
157 if (!has_capability('mod/wiki:editpage', $PAGE->context)){
158 unset($this->tabs['edit']);
162 if (empty($options)) {
163 $this->tabs_options = array('activetab' => substr(get_class($this), 10));
165 $this->tabs_options = $options;
171 * This method must be overwritten to print the page content.
173 function print_content() {
174 throw new coding_exception('Page wiki class does not implement method print_content()');
178 * Method to set the current page
180 * @param object $page Current page
182 function set_page($page) {
186 $this->title = $page->title;
187 $PAGE->set_title($this->title);
191 * Method to set the current page title.
192 * This method must be called when the current page is not created yet.
193 * @param string $title Current page title.
195 function set_title($title) {
199 $this->title = $title;
200 $PAGE->set_title($this->title);
204 * Method to set current group id
205 * @param int $gid Current group id
207 function set_gid($gid) {
212 * Method to set current user id
213 * @param int $uid Current user id
215 function set_uid($uid) {
220 * Method to set the URL of the page.
221 * This method must be overwritten by every type of page.
223 protected function set_url() {
224 throw new coding_exception('Page wiki class does not implement method set_url()');
228 * Protected method to create the common items of the navbar in every page type.
230 protected function create_navbar() {
233 $PAGE->navbar->add(format_string($this->title), $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
237 * This method print the footer of the page.
239 function print_footer() {
241 echo $OUTPUT->footer();
244 protected function process_session_url() {
245 global $USER, $SESSION;
247 //delete locks if edit
248 $url = $SESSION->wikipreviousurl;
249 switch ($url['page']) {
251 wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
256 protected function set_session_url() {
258 unset($SESSION->wikipreviousurl);
267 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
269 class page_wiki_view extends page_wiki {
271 * @var int the coursemodule id
273 private $coursemodule;
275 function print_header() {
278 parent::print_header();
280 $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page);
282 if (!empty($this->page)) {
283 echo $this->wikioutput->prettyview_link($this->page);
286 //echo $this->wikioutput->page_index();
288 $this->print_pagetitle();
291 function print_content() {
294 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
295 if (wiki_user_can_view($this->subwiki)) {
297 if (!empty($this->page)) {
298 wiki_print_page_content($this->page, $context, $this->subwiki->id);
299 $wiki = $PAGE->activityrecord;
301 print_string('nocontent', 'wiki');
302 // TODO: fix this part
304 if (!empty($this->subwiki)) {
305 $swid = $this->subwiki->id;
309 // @TODO: Tranlate it
310 echo "You can not view this page";
318 if (isset($this->coursemodule)) {
319 $params['id'] = $this->coursemodule;
320 } else if (!empty($this->page) and $this->page != null) {
321 $params['pageid'] = $this->page->id;
322 } else if (!empty($this->gid)) {
323 $params['wid'] = $PAGE->cm->instance;
324 $params['group'] = $this->gid;
325 } else if (!empty($this->title)) {
326 $params['swid'] = $this->subwiki->id;
327 $params['title'] = $this->title;
329 print_error(get_string('invalidparameters', 'wiki'));
332 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/view.php', $params);
335 function set_coursemodule($id) {
336 $this->coursemodule = $id;
339 protected function create_navbar() {
342 $PAGE->navbar->add(format_string($this->title));
343 $PAGE->navbar->add(get_string('view', 'wiki'));
348 * Wiki page editing page
351 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
353 class page_wiki_edit extends page_wiki {
355 public static $attachmentoptions;
357 protected $sectioncontent;
358 /** @var string the section name needed to be edited */
360 protected $overridelock = false;
361 protected $versionnumber = -1;
362 protected $upload = false;
363 protected $attachments = 0;
364 protected $deleteuploads = array();
367 function __construct($wiki, $subwiki, $cm) {
369 parent::__construct($wiki, $subwiki, $cm);
370 self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*');
371 $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
372 $PAGE->requires->yui2_lib('connection');
375 protected function print_pagetitle() {
378 $title = $this->title;
379 if (isset($this->section)) {
380 $title .= ' : ' . $this->section;
382 echo $OUTPUT->container_start('wiki_clear');
383 echo $OUTPUT->heading(format_string($title), 2, 'wiki_headingtitle');
384 echo $OUTPUT->container_end();
387 function print_header() {
388 global $OUTPUT, $PAGE;
389 $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
391 parent::print_header();
393 $this->print_pagetitle();
395 print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
398 function print_content() {
401 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
402 if (wiki_user_can_edit($this->subwiki)) {
405 // @TODO: Translate it
406 echo "You can not edit this page";
410 protected function set_url() {
413 $params = array('pageid' => $this->page->id);
415 if (isset($this->section)) {
416 $params['section'] = $this->section;
419 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
422 protected function set_session_url() {
425 $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
428 protected function process_session_url() {
431 function set_section($sectioncontent, $section) {
432 $this->sectioncontent = $sectioncontent;
433 $this->section = $section;
436 public function set_versionnumber($versionnumber) {
437 $this->versionnumber = $versionnumber;
440 public function set_overridelock($override) {
441 $this->overridelock = $override;
444 function set_format($format) {
445 $this->format = $format;
448 public function set_upload($upload) {
449 $this->upload = $upload;
452 public function set_attachments($attachments) {
453 $this->attachments = $attachments;
456 public function set_deleteuploads($deleteuploads) {
457 $this->deleteuploads = $deleteuploads;
460 protected function create_navbar() {
463 parent::create_navbar();
465 $PAGE->navbar->add(get_string('edit', 'wiki'));
468 protected function check_locks() {
469 global $OUTPUT, $USER, $CFG;
471 if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
472 print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
474 if ($this->overridelock) {
475 $params = 'pageid=' . $this->page->id;
477 if ($this->section) {
478 $params .= '§ion=' . $this->section;
481 $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
482 $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
483 $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
486 print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
493 protected function print_edit($content = null) {
494 global $CFG, $OUTPUT, $USER, $PAGE;
496 if (!$this->check_locks()) {
500 //delete old locks (> 1 hour)
501 wiki_delete_old_locks();
503 $version = wiki_get_current_version($this->page->id);
504 $format = $version->contentformat;
506 if ($content == null) {
507 if (empty($this->section)) {
508 $content = $version->content;
510 $content = $this->sectioncontent;
514 $versionnumber = $version->version;
515 if ($this->versionnumber >= 0) {
516 if ($version->version != $this->versionnumber) {
517 print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
518 $versionnumber = $this->versionnumber;
523 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
525 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
526 if (!empty($this->section)) {
527 $url .= "§ion=" . $this->section;
530 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $version->contentformat, 'version' => $versionnumber, 'pagetitle'=>$this->page->title);
532 $data = new StdClass();
533 $data->newcontent = $content;
534 $data->version = $versionnumber;
535 $data->format = $format;
539 $data->newcontentformat = FORMAT_HTML;
540 $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id);
543 //$draftitemid = file_get_submitted_draft_itemid('attachments');
544 //file_prepare_draft_area($draftitemid, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
545 //$data->attachments = $draftitemid;
548 if ($version->contentformat != 'html') {
549 $params['contextid'] = $context->id;
550 $params['component'] = 'mod_wiki';
551 $params['filearea'] = 'attachments';
552 $params['fileitemid'] = $this->subwiki->id;
555 if (!empty($CFG->usetags)) {
556 $params['tags'] = tag_get_tags_csv('wiki_pages', $this->page->id, TAG_RETURN_TEXT);
559 $form = new mod_wiki_edit_form($url, $params);
561 if ($formdata = $form->get_data()) {
562 if ($format != 'html') {
563 $errors = $this->process_uploads($context);
564 if (!empty($errors)) {
566 foreach ($errors as $e) {
567 $contenterror .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
569 print $OUTPUT->box($contenterror, 'errorbox');
572 if (!empty($CFG->usetags)) {
573 $data->tags = $formdata->tags;
576 if (!empty($CFG->usetags)) {
577 $data->tags = tag_get_tags_array('wiki', $this->page->id);
581 $form->set_data($data);
585 protected function process_uploads($context) {
586 global $PAGE, $OUTPUT;
589 file_save_draft_area_files($this->attachments, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
591 //return wiki_process_attachments($this->attachments, $this->deleteuploads, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
597 * Class that models the behavior of wiki's view comments page
600 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
602 class page_wiki_comments extends page_wiki {
604 function print_header() {
606 parent::print_header();
608 $this->print_pagetitle();
612 function print_content() {
613 global $CFG, $OUTPUT, $USER, $PAGE, $COURSE;
614 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
617 $subwiki = $this->subwiki;
618 $wiki = $PAGE->activityrecord;
621 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
623 require_capability('mod/wiki:viewcomment', $context, NULL, true, 'noviewcommentpermission', 'wiki');
625 $comments = wiki_get_comments($context->id, $page->id);
627 if (has_capability('mod/wiki:editcomment', $context)) {
628 echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
631 $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
632 $version = wiki_get_current_version($this->page->id);
633 $format = $version->contentformat;
635 if (empty($comments)) {
636 echo $OUTPUT->heading(get_string('nocomments', 'wiki'));
639 foreach ($comments as $comment) {
641 $user = wiki_get_user_info($comment->userid);
643 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
644 $by = new stdclass();
645 $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&course=' . $course->id . '">' . $fullname . '</a>';
646 $by->date = userdate($comment->timecreated);
648 $t = new html_table();
649 $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
650 $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
651 $cell3 = new html_table_cell();
652 $cell3->atributtes ['width'] = "80%";
653 $cell4 = new html_table_cell();
654 $cell5 = new html_table_cell();
656 $row1 = new html_table_row();
657 $row1->cells[] = $cell1;
658 $row1->cells[] = $cell2;
659 $row2 = new html_table_row();
660 $row2->cells[] = $cell3;
662 if ($format != 'html') {
663 if ($format == 'creole') {
664 $parsedcontent = wiki_parse_content('creole', $comment->content, $options);
665 } else if ($format == 'nwiki') {
666 $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options);
669 $cell4->text = html_entity_decode($parsedcontent['parsed_text']);
671 $cell4->text = format_text($comment->content, FORMAT_HTML);
674 $row2->cells[] = $cell4;
676 $t->data = array($row1, $row2);
678 $actionicons = false;
679 if ((has_capability('mod/wiki:managecomment', $context))) {
680 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
681 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
683 } else if ((has_capability('mod/wiki:editcomment', $context)) and ($USER->id == $user->id)) {
684 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
685 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
690 $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'))));
691 $row3 = new html_table_row();
692 $row3->cells[] = $cell5;
693 $row3->cells[] = $cell6;
697 echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow'));
704 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
707 protected function create_navbar() {
710 parent::create_navbar();
711 $PAGE->navbar->add(get_string('comments', 'wiki'));
717 * Class that models the behavior of wiki's edit comment
720 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
722 class page_wiki_editcomment extends page_wiki {
730 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
733 function print_header() {
734 parent::print_header();
735 $this->print_pagetitle();
738 function print_content() {
741 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
742 require_capability('mod/wiki:editcomment', $context, NULL, true, 'noeditcommentpermission', 'wiki');
744 if ($this->action == 'add') {
745 $this->add_comment_form();
746 } else if ($this->action == 'edit') {
747 $this->edit_comment_form($this->comment);
751 function set_action($action, $comment) {
753 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
755 $this->action = $action;
756 $this->comment = $comment;
757 $version = wiki_get_current_version($this->page->id);
758 $this->format = $version->contentformat;
760 if ($this->format == 'html') {
761 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
762 $this->form = new mod_wiki_comments_form($destination);
766 protected function create_navbar() {
769 $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
771 if ($this->action == 'add') {
772 $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
774 $PAGE->navbar->add(get_string('editcomment', 'wiki'));
778 protected function setup_tabs() {
779 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
782 private function add_comment_form() {
784 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
786 $pageid = $this->page->id;
788 if ($this->format == 'html') {
789 $this->form->display();
791 wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'addcomments');
795 private function edit_comment_form($com) {
797 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
798 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
800 if ($this->format == 'html') {
801 $commentoptions = array('trusttext' => true, 'maxfiles' => 0);
802 $com->action = 'edit';
803 $com->entrycomment_editor['text'] = $com->content;
805 $this->form->set_data($com, $commentoptions);
806 $this->form->display();
809 $commentid = $com->id;
810 $pageid = $this->page->id;
811 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $pageid . '&id=' . $commentid . '&action=' . $action;
812 wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'editcomments', $commentid);
820 * Wiki page search page
823 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
825 class page_wiki_search extends page_wiki {
826 private $search_result;
828 protected function create_navbar() {
831 $PAGE->navbar->add(format_string($this->title));
834 function set_search_string($search, $searchcontent) {
835 $swid = $this->subwiki->id;
836 if ($searchcontent) {
837 $this->search_result = wiki_search_all($swid, $search);
839 $this->search_result = wiki_search_title($swid, $search);
846 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
848 function print_content() {
851 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
852 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
854 echo $this->wikioutput->search_result($this->search_result);
860 * Class that models the behavior of wiki's
864 class page_wiki_create extends page_wiki {
872 function print_header() {
874 parent::print_header();
881 if ($this->action == 'new') {
882 $params['action'] = 'new';
883 $params['swid'] = $this->swid;
884 $params['wid'] = $this->wid;
885 if ($this->title != get_string('newpage', 'wiki')) {
886 $params['title'] = $this->title;
888 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
890 $params['action'] = 'create';
891 $params['swid'] = $this->swid;
892 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
896 function set_format($format) {
897 $this->format = $format;
900 function set_wid($wid) {
904 function set_swid($swid) {
908 function set_action($action) {
910 $this->action = $action;
912 require_once(dirname(__FILE__) . '/create_form.php');
913 $url = new moodle_url('/mod/wiki/create.php', array('action' => 'create', 'wid' => $PAGE->activityrecord->id, 'gid' => $this->gid, 'uid' => $this->uid));
914 $formats = wiki_get_formats();
915 $options = array('formats' => $formats, 'defaultformat' => $PAGE->activityrecord->defaultformat, 'forceformat' => $PAGE->activityrecord->forceformat);
916 if ($this->title != get_string('newpage', 'wiki')) {
917 $options['disable_pagetitle'] = true;
919 $this->mform = new mod_wiki_create_form($url->out(false), $options);
922 protected function create_navbar() {
925 $PAGE->navbar->add($this->title);
928 function print_content($pagetitle = '') {
931 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
933 // @TODO: Change this to has_capability and show an alternative interface.
934 require_capability('mod/wiki:createpage', $context, NULL, true, 'nocreatepermission', 'wiki');
935 $data = new stdClass();
936 if (!empty($pagetitle)) {
937 $data->pagetitle = $pagetitle;
939 $data->pageformat = $PAGE->activityrecord->defaultformat;
941 $this->mform->set_data($data);
942 $this->mform->display();
945 function create_page($pagetitle) {
946 global $USER, $CFG, $PAGE;
947 $data = $this->mform->get_data();
948 if (empty($this->subwiki)) {
949 $swid = wiki_add_subwiki($PAGE->activityrecord->id, $this->gid, $this->uid);
950 $this->subwiki = wiki_get_subwiki($swid);
953 $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
955 $id = wiki_create_page($this->subwiki->id, $pagetitle, $PAGE->activityrecord->defaultformat, $USER->id);
957 redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $id);
961 class page_wiki_preview extends page_wiki_edit {
965 function __construct($wiki, $subwiki, $cm) {
966 global $PAGE, $CFG, $OUTPUT;
967 parent::__construct($wiki, $subwiki, $cm);
968 $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
969 $PAGE->set_button($buttons);
973 function print_header() {
976 parent::print_header();
980 function print_content() {
983 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
984 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
986 $this->print_preview();
989 function set_newcontent($newcontent) {
990 $this->newcontent = $newcontent;
996 $params = array('pageid' => $this->page->id
999 if (isset($this->section)) {
1000 $params['section'] = $this->section;
1003 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
1006 protected function setup_tabs() {
1007 parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
1010 protected function check_locks() {
1014 protected function print_preview() {
1015 global $CFG, $PAGE, $OUTPUT;
1017 $version = wiki_get_current_version($this->page->id);
1018 $format = $version->contentformat;
1019 $content = $version->content;
1020 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1022 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1023 if (!empty($this->section)) {
1024 $url .= "§ion=" . $this->section;
1026 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
1028 if ($this->format != 'html') {
1029 $params['contextid'] = $context->id;
1030 $params['component'] = 'mod_wiki';
1031 $params['filearea'] = 'attachments';
1032 $params['fileitemid'] = $this->page->id;
1034 $form = new mod_wiki_edit_form($url, $params);
1037 $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
1039 if ($data = $form->get_data()) {
1040 if (isset($data->newcontent)) {
1042 $text = $data->newcontent;
1045 $text = $data->newcontent_editor['text'];
1047 $parseroutput = wiki_parse_content($data->contentformat, $text, $options);
1048 $this->set_newcontent($text);
1049 echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info');
1050 $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true));
1051 echo $OUTPUT->box($content, 'generalbox wiki_previewbox');
1052 $content = $this->newcontent;
1055 $this->print_edit($content);
1062 * Class that models the behavior of wiki's
1066 class page_wiki_diff extends page_wiki {
1069 private $comparewith;
1071 function print_header() {
1074 parent::print_header();
1076 $this->print_pagetitle();
1077 $vstring = new stdClass();
1078 $vstring->old = $this->compare;
1079 $vstring->new = $this->comparewith;
1080 echo $OUTPUT->heading(get_string('comparewith', 'wiki', $vstring));
1084 * Print the diff view
1086 function print_content() {
1089 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1090 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1092 $this->print_diff_content();
1095 function set_url() {
1098 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/diff.php', array('pageid' => $this->page->id, 'comparewith' => $this->comparewith, 'compare' => $this->compare));
1101 function set_comparison($compare, $comparewith) {
1102 $this->compare = $compare;
1103 $this->comparewith = $comparewith;
1106 protected function create_navbar() {
1109 parent::create_navbar();
1110 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
1111 $PAGE->navbar->add(get_string('diff', 'wiki'));
1114 protected function setup_tabs() {
1115 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1119 * Given two versions of a page, prints a page displaying the differences between them.
1123 * @param int $pageid The page id whose versions will be displayed.
1124 * @param int $cm The course id, needed to print user images.
1125 * @param int $compare The first version number to be compared.
1126 * @param int $comparewith The second version number to be compared.
1128 private function print_diff_content() {
1129 global $CFG, $OUTPUT, $PAGE;
1131 $pageid = $this->page->id;
1132 $total = wiki_count_wiki_page_versions($pageid) - 1;
1134 $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
1135 $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
1137 if ($oldversion && $newversion) {
1139 $oldtext = format_text($oldversion->content);
1140 $newtext = format_text($newversion->content);
1141 list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
1142 $oldversion->diff = $diff1;
1143 $oldversion->user = wiki_get_user_info($oldversion->userid);
1144 $newversion->diff = $diff2;
1145 $newversion->user = wiki_get_user_info($newversion->userid);
1147 echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
1149 print_error('versionerror', 'wiki');
1156 * Class that models the behavior of wiki's history page
1159 class page_wiki_history extends page_wiki {
1161 * @var int $paging current page
1166 * @var int @rowsperpage Items per page
1168 private $rowsperpage = 10;
1171 * @var int $allversion if $allversion != 0, all versions will be printed in a signle table
1173 private $allversion;
1175 function __construct($wiki, $subwiki, $cm) {
1177 parent::__construct($wiki, $subwiki, $cm);
1178 $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
1181 function print_header() {
1182 parent::print_header();
1183 $this->print_pagetitle();
1186 function print_content() {
1189 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1190 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1192 $this->print_history_content();
1195 function set_url() {
1197 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1200 function set_paging($paging) {
1201 $this->paging = $paging;
1204 function set_allversion($allversion) {
1205 $this->allversion = $allversion;
1208 protected function create_navbar() {
1211 parent::create_navbar();
1212 $PAGE->navbar->add(get_string('history', 'wiki'));
1216 * Prints the history for a given wiki page
1220 * @param int $pageid The page id whose history should be displayed.
1221 * @param int $cm The course id, needed to print user images.
1222 * @param wikipage $wikipage
1223 * @param int $allversion This tells how results should be displayed.
1224 * If $allversion == 0 : Only $rowsperpage will be displayed at a time, and $paging is the page being displayed.
1225 * If $allversion != 0 : All versions will be printed in a single table.
1226 * @param int $paging Desired page to be displayed, this is ignored if $allversion!=0.
1227 * @param int $rowsperpage How many rows to be displayed in each page, this is ignored if $allversion!=0.
1229 private function print_history_content() {
1230 global $CFG, $OUTPUT, $PAGE;
1232 $pageid = $this->page->id;
1233 $offset = $this->paging * $this->rowsperpage;
1234 // vcount is the latest version
1235 $vcount = wiki_count_wiki_page_versions($pageid) - 1;
1236 if ($this->allversion) {
1237 $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
1239 $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
1241 // We don't want version 0 to be displayed
1242 // version 0 is blank page
1243 if (end($versions)->version == 0) {
1244 array_pop($versions);
1247 $contents = array();
1249 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
1250 $creator = wiki_get_user_info($version0page->userid);
1252 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
1253 $a->username = $creator->username;
1254 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
1257 /// If there is only one version, we don't need radios nor forms
1258 if (count($versions) == 1) {
1260 $row = array_shift($versions);
1262 $username = wiki_get_user_info($row->userid);
1263 $picture = $OUTPUT->user_picture($username);
1264 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
1265 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
1266 $versionid = wiki_get_version($row->id);
1267 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1268 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
1269 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picture . html_writer::link($userlink->out(false), fullname($username)), $time, $OUTPUT->container($date, 'wiki_histdate'));
1271 $table = new html_table();
1272 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
1273 $table->data = $contents;
1274 $table->attributes['class'] = 'mdl-align';
1276 echo html_writer::table($table);
1280 $checked = $vcount - $offset;
1282 $rowclass = array();
1284 foreach ($versions as $version) {
1285 $user = wiki_get_user_info($version->userid);
1286 $picture = $OUTPUT->user_picture($user, array('popup' => true));
1287 $date = userdate($version->timecreated, get_string('strftimedate'));
1288 if ($date == $lastdate) {
1293 $rowclass[] = 'wiki_histnewdate';
1295 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1296 $versionid = wiki_get_version($version->id);
1298 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1299 $viewlink = html_writer::link($url->out(false), $version->version);
1301 $viewlink = $version->version;
1303 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
1304 $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'));
1307 $table = new html_table();
1309 $icon = $OUTPUT->help_icon('diff', 'wiki');
1311 $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
1312 $table->data = $contents;
1313 $table->attributes['class'] = 'generaltable mdl-align';
1314 $table->rowclasses = $rowclass;
1316 /*$table = new StdClass();
1317 $table->head = array(helpbutton('diff', 'diff', 'wiki', true, false, '', true, ''),
1318 get_string('version'),
1320 get_string('modified'),
1322 $table->data = $contents;
1323 $table->class = 'mdl-align';
1324 $table->rowclass = $rowclass;*/
1327 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
1328 echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
1329 echo html_writer::table($table);
1330 echo html_writer::start_tag('div', array('class'=>'mdl-align'));
1331 echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
1332 echo html_writer::end_tag('div');
1333 echo html_writer::end_tag('form');
1336 print_string('nohistory', 'wiki');
1338 if (!$this->allversion) {
1339 //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&');
1340 // $pagingbar->pagevar = $pagevar;
1341 echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&');
1342 //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&','paging');
1344 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1345 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1347 if ($vcount > $this->rowsperpage && !$this->allversion) {
1348 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1349 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1354 * Given an array of values, creates a group of radio buttons to be part of a form
1356 * @param array $options An array of value-label pairs for the radio group (values as keys).
1357 * @param string $name Name of the radiogroup (unique in the form).
1358 * @param string $onclick Function to be executed when the radios are clicked.
1359 * @param string $checked The value that is already checked.
1360 * @param bool $return If true, return the HTML as a string, otherwise print it.
1362 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1364 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1366 static $idcounter = 0;
1372 $output = '<span class="radiogroup ' . $name . "\">\n";
1374 if (!empty($options)) {
1376 foreach ($options as $value => $label) {
1377 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1378 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1379 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1380 if ($value == $checked) {
1381 $output .= ' checked="checked"';
1384 $output .= ' onclick="' . $onclick . '"';
1386 if ($label === '') {
1387 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1389 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1391 $currentradio = ($currentradio + 1) % 2;
1395 $output .= '</span>' . "\n";
1406 * Class that models the behavior of wiki's map page
1409 class page_wiki_map extends page_wiki {
1412 * @var int wiki view option
1416 function print_header() {
1417 parent::print_header();
1418 $this->print_pagetitle();
1421 function print_content() {
1424 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1425 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
1427 if ($this->view > 0) {
1428 //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
1431 switch ($this->view) {
1433 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1434 $this->print_contributions_content();
1437 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1438 $this->print_navigation_content();
1441 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1442 $this->print_orphaned_content();
1445 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1446 $this->print_index_content();
1449 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1450 $this->print_page_list_content();
1453 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1454 $this->print_updated_content();
1457 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1458 $this->print_page_list_content();
1462 function set_view($option) {
1463 $this->view = $option;
1466 function set_url() {
1468 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1471 protected function create_navbar() {
1474 parent::create_navbar();
1475 $PAGE->navbar->add(get_string('map', 'wiki'));
1479 * Prints the contributions tab content
1481 * @uses $OUTPUT, $USER
1484 private function print_contributions_content() {
1485 global $CFG, $OUTPUT, $USER;
1486 $page = $this->page;
1488 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1489 $fresh = wiki_refresh_cachedcontent($page);
1490 $page = $fresh['page'];
1493 $swid = $this->subwiki->id;
1495 $table = new html_table();
1496 $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
1497 $table->attributes['class'] = 'wiki_editor generalbox';
1498 $table->data = array();
1499 $table->rowclasses = array();
1501 $lastversions = array();
1505 if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1506 foreach ($contribs as $contrib) {
1507 if (!array_key_exists($contrib->pageid, $pages)) {
1508 $page = wiki_get_page($contrib->pageid);
1509 $pages[$contrib->pageid] = $page;
1514 if (!array_key_exists($page->id, $lastversions)) {
1515 $version = wiki_get_last_version($page->id);
1516 $lastversions[$page->id] = $version;
1518 $version = $lastversions[$page->id];
1521 if (!array_key_exists($version->userid, $users)) {
1522 $user = wiki_get_user_info($version->userid);
1523 $users[$version->userid] = $user;
1525 $user = $users[$version->userid];
1528 $link = wiki_parser_link(format_string($page->title), array('swid' => $swid));
1529 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1531 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
1532 $icon = $OUTPUT->user_picture($user, array('popup' => true));
1534 $table->data[] = array("$icon $linkpage");
1537 $table->data[] = array(get_string('nocontribs', 'wiki'));
1539 echo html_writer::table($table);
1543 * Prints the navigation tab content
1548 private function print_navigation_content() {
1550 $page = $this->page;
1552 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1553 $fresh = wiki_refresh_cachedcontent($page);
1554 $page = $fresh['page'];
1557 $tolinks = wiki_get_linked_to_pages($page->id);
1558 $fromlinks = wiki_get_linked_from_pages($page->id);
1560 $table = new html_table();
1561 $table->attributes['class'] = 'wiki_navigation_from';
1562 $table->head = array(get_string('navigationfrom', 'wiki') . ':');
1563 $table->data = array();
1564 $table->rowclasses = array();
1565 foreach ($fromlinks as $link) {
1566 $lpage = wiki_get_page($link->frompageid);
1567 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1568 $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
1569 $table->rowclasses[] = 'mdl-align';
1572 $table_left = html_writer::table($table);
1574 $table = new html_table();
1575 $table->attributes['class'] = 'wiki_navigation_to';
1576 $table->head = array(get_string('navigationto', 'wiki') . ':');
1577 $table->data = array();
1578 $table->rowclasses = array();
1579 foreach ($tolinks as $link) {
1580 if ($link->tomissingpage) {
1581 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
1582 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
1584 $lpage = wiki_get_page($link->topageid);
1585 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1586 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
1588 $table->rowclasses[] = 'mdl-align';
1590 $table_right = html_writer::table($table);
1591 echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1595 * Prints the index page tab content
1599 private function print_index_content() {
1600 $page = $this->page;
1602 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1603 $fresh = wiki_refresh_cachedcontent($page);
1604 $page = $fresh['page'];
1607 $node = new navigation_node($page->title);
1611 $tree = wiki_build_tree($page, $node, $keys);
1613 $table = new html_table();
1614 $table->head = array(get_string('pageindex', 'wiki'));
1615 $table->attributes['class'] = 'wiki_editor generalbox';
1616 $table->data[] = array($this->render_navigation_node($tree));
1618 echo html_writer::table($table);
1622 * Prints the page list tab content
1626 private function print_page_list_content() {
1627 $page = $this->page;
1629 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1630 $fresh = wiki_refresh_cachedcontent($page);
1631 $page = $fresh['page'];
1634 $pages = wiki_get_page_list($this->subwiki->id);
1636 $stdaux = new stdClass();
1637 $strspecial = get_string('special', 'wiki');
1639 foreach ($pages as $page) {
1640 $letter = strtoupper(substr($page->title, 0, 1));
1641 if (preg_match('/[A-Z]/', $letter)) {
1644 [] = wiki_parser_link($page);
1648 [] = wiki_parser_link($page);
1652 $table = new html_table();
1653 $table->head = array(get_string('pagelist', 'wiki'));
1654 $table->attributes['class'] = 'wiki_editor generalbox';
1655 $table->align = array('center');
1656 foreach ($stdaux as $key => $elem) {
1657 $table->data[] = array($key);
1658 foreach ($elem as $e) {
1659 $table->data[] = array(html_writer::link($e['url'], $e['content']));
1662 echo html_writer::table($table);
1666 * Prints the orphaned tab content
1670 private function print_orphaned_content() {
1673 $page = $this->page;
1675 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1676 $fresh = wiki_refresh_cachedcontent($page);
1677 $page = $fresh['page'];
1680 $swid = $this->subwiki->id;
1682 $table = new html_table();
1683 $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
1684 $table->attributes['class'] = 'wiki_editor generalbox';
1685 $table->data = array();
1686 $table->rowclasses = array();
1688 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1689 foreach ($orphanedpages as $page) {
1690 $link = wiki_parser_link($page->title, array('swid' => $swid));
1691 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1692 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
1695 $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1698 echo html_writer::table($table);
1702 * Prints the updated tab content
1704 * @uses $COURSE, $OUTPUT
1707 private function print_updated_content() {
1708 global $COURSE, $OUTPUT;
1709 $page = $this->page;
1711 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1712 $fresh = wiki_refresh_cachedcontent($page);
1713 $page = $fresh['page'];
1716 $swid = $this->subwiki->id;
1718 $table = new html_table();
1719 $table->head = array(get_string('updatedpages', 'wiki'));
1720 $table->attributes['class'] = 'wiki_editor generalbox';
1721 $table->data = array();
1722 $table->rowclasses = array();
1724 if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1726 foreach ($pages as $page) {
1727 $user = wiki_get_user_info($page->userid);
1728 $strdata = strftime('%d %b %Y', $page->timemodified);
1729 if ($strdata != $strdataux) {
1730 $table->data[] = array($OUTPUT->heading($strdata, 4));
1731 $strdataux = $strdata;
1733 $link = wiki_parser_link($page->title, array('swid' => $swid));
1734 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1736 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
1737 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1738 $table->data[] = array("$icon $linkpage");
1741 $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1744 echo html_writer::table($table);
1747 protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1749 // exit if empty, we don't want an empty ul element
1750 if (count($items) == 0) {
1754 // array of nested li elements
1756 foreach ($items as $item) {
1757 if (!$item->display) {
1760 $content = $item->get_content();
1761 $title = $item->get_title();
1762 if ($item->icon instanceof renderable) {
1763 $icon = $this->wikioutput->render($item->icon);
1764 $content = $icon . ' ' . $content; // use CSS for spacing of icons
1766 if ($item->helpbutton !== null) {
1767 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1770 if ($content === '') {
1774 if ($item->action instanceof action_link) {
1775 //TODO: to be replaced with something else
1776 $link = $item->action;
1777 if ($item->hidden) {
1778 $link->add_class('dimmed');
1780 $content = $this->output->render($link);
1781 } else if ($item->action instanceof moodle_url) {
1782 $attributes = array();
1783 if ($title !== '') {
1784 $attributes['title'] = $title;
1786 if ($item->hidden) {
1787 $attributes['class'] = 'dimmed_text';
1789 $content = html_writer::link($item->action, $content, $attributes);
1791 } else if (is_string($item->action) || empty($item->action)) {
1792 $attributes = array();
1793 if ($title !== '') {
1794 $attributes['title'] = $title;
1796 if ($item->hidden) {
1797 $attributes['class'] = 'dimmed_text';
1799 $content = html_writer::tag('span', $content, $attributes);
1802 // this applies to the li item which contains all child lists too
1803 $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1804 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1805 $liclasses[] = 'collapsed';
1807 if ($item->isactive === true) {
1808 $liclasses[] = 'current_branch';
1810 $liattr = array('class' => join(' ', $liclasses));
1811 // class attribute on the div item which only contains the item content
1812 $divclasses = array('tree_item');
1813 if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1814 $divclasses[] = 'branch';
1816 $divclasses[] = 'leaf';
1818 if (!empty($item->classes) && count($item->classes) > 0) {
1819 $divclasses[] = join(' ', $item->classes);
1821 $divattr = array('class' => join(' ', $divclasses));
1822 if (!empty($item->id)) {
1823 $divattr['id'] = $item->id;
1825 $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1826 if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1827 $content = html_writer::empty_tag('hr') . $content;
1829 $content = html_writer::tag('li', $content, $liattr);
1834 return html_writer::tag('ul', implode("\n", $lis), $attrs);
1843 * Class that models the behavior of wiki's restore version page
1846 class page_wiki_restoreversion extends page_wiki {
1849 function print_header() {
1850 parent::print_header();
1851 $this->print_pagetitle();
1854 function print_content() {
1857 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1858 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
1860 $this->print_restoreversion();
1863 function set_url() {
1865 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1868 function set_versionid($versionid) {
1869 $this->version = wiki_get_version($versionid);
1872 protected function create_navbar() {
1875 parent::create_navbar();
1876 $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1879 protected function setup_tabs() {
1880 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1884 * Prints the restore version content
1888 * @param page $page The page whose version will be restored
1889 * @param int $versionid The version to be restored
1890 * @param bool $confirm If false, shows a yes/no confirmation page.
1891 * If true, restores the old version and redirects the user to the 'view' tab.
1893 private function print_restoreversion() {
1894 global $CFG, $OUTPUT;
1896 $version = wiki_get_version($this->version->id);
1898 echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
1899 print_container_start(false, 'wiki_restoreform');
1900 echo '<form class="wiki_restore_yes" action="' . $CFG->wwwroot . '/mod/wiki/restoreversion.php?pageid=' . $this->page->id . '&versionid=' . $version->id . '" method="post" id="restoreversion">';
1901 echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1903 echo '<form class="wiki_restore_no" action="' . $CFG->wwwroot . '/mod/wiki/viewversion.php?pageid=' . $this->page->id . '&versionid=' . $version->id . '" method="post">';
1904 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1906 print_container_end();
1912 * Class that models the behavior of wiki's
1916 class page_wiki_save extends page_wiki_edit {
1918 private $newcontent;
1920 function print_header() {
1923 function print_content() {
1926 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
1927 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
1929 $this->print_save();
1932 function set_newcontent($newcontent) {
1933 $this->newcontent = $newcontent;
1936 protected function set_session_url() {
1939 protected function print_save() {
1940 global $CFG, $USER, $OUTPUT, $PAGE;
1943 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1945 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1946 if (!empty($this->section)) {
1947 $url .= "§ion=" . $this->section;
1950 $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
1952 if ($this->format != 'html') {
1953 $params['contextid'] = $context->id;
1954 $params['component'] = 'mod_wiki';
1955 $params['filearea'] = 'attachments';
1956 $params['fileitemid'] = $this->page->id;
1959 $form = new mod_wiki_edit_form($url, $params);
1963 if ($data = $form->get_data()) {
1964 if ($this->format == 'html') {
1965 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id);
1968 if (isset($this->section)) {
1969 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
1971 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
1975 if ($save && $data) {
1976 if ($this->format != 'html') {
1977 $errors = $this->process_uploads($context);
1979 if (!empty($CFG->usetags)) {
1980 tag_set('wiki_pages', $this->page->id, $data->tags);
1983 $message = '<p>' . get_string('saving', 'wiki') . '</p>';
1985 if (!empty($save['sections'])) {
1986 foreach ($save['sections'] as $s) {
1987 $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
1991 if ($this->versionnumber + 1 != $save['version']) {
1992 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
1995 if (isset($errors) && !empty($errors)) {
1996 foreach ($errors as $e) {
1997 $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
2001 //deleting old locks
2002 wiki_delete_locks($this->page->id, $USER->id, $this->section);
2004 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
2006 print_error('savingerror', 'wiki');
2012 * Class that models the behavior of wiki's view an old version of a page
2015 class page_wiki_viewversion extends page_wiki {
2019 function print_header() {
2020 parent::print_header();
2021 $this->print_pagetitle();
2024 function print_content() {
2027 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2028 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
2030 $this->print_version_view();
2033 function set_url() {
2035 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2038 function set_versionid($versionid) {
2039 $this->version = wiki_get_version($versionid);
2042 protected function create_navbar() {
2045 parent::create_navbar();
2046 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
2047 $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
2050 protected function setup_tabs() {
2051 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
2055 * Given an old page version, output the version content
2059 * @param page $page The page whose version will be displayed.
2060 * @param int $cm The course id, needed to print user images.
2061 * @param int $version The version number to be displayed.
2063 private function print_version_view() {
2064 global $CFG, $OUTPUT, $PAGE;
2066 $pageversion = wiki_get_version($this->version->id);
2069 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2070 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);
2071 $userinfo = wiki_get_user_info($pageversion->userid);
2072 $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong> ' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2073 $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2074 $heading .= ' <strong>' . get_string('user') . ':</strong> ' . html_writer::link($viewlink->out(false), fullname($userinfo));
2075 $heading .= ' → ' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2076 print_container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
2077 $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2078 $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
2079 $content = print_container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
2080 echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2083 print_error('versionerror', 'wiki');
2088 class page_wiki_confirmrestore extends page_wiki_save {
2092 function set_url() {
2094 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2097 function print_content() {
2100 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2101 require_capability('mod/wiki:managewiki', $context, NULL, true, 'nomanagewikipermission', 'wiki');
2103 $version = wiki_get_version($this->version->id);
2104 if (wiki_restore_page($this->page, $version->content, $version->userid)) {
2105 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2107 print_error('restoreerror', 'wiki', $version->version);
2111 function set_versionid($versionid) {
2112 $this->version = wiki_get_version($versionid);
2116 class page_wiki_prettyview extends page_wiki {
2118 function print_header() {
2119 global $CFG, $PAGE, $OUTPUT;
2120 $PAGE->set_pagelayout('embedded');
2121 echo $OUTPUT->header();
2123 echo '<h1 id="wiki_printable_title">' . format_string($this->title) . '</h1>';
2126 function print_content() {
2129 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2130 require_capability('mod/wiki:viewpage', $context, NULL, true, 'noviewpagepermission', 'wiki');
2132 $this->print_pretty_view();
2135 function set_url() {
2138 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2141 private function print_pretty_view() {
2142 $version = wiki_get_current_version($this->page->id);
2144 $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2146 echo '<div id="wiki_printable_content">';
2147 echo format_text($content['parsed_text'], FORMAT_HTML);
2152 class page_wiki_handlecomments extends page_wiki {
2158 function print_header() {
2162 public function print_content() {
2163 global $CFG, $PAGE, $USER;
2165 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2167 if ($this->action == 'add') {
2168 if (has_capability('mod/wiki:editcomment', $context)) {
2169 $this->add_comment($this->content, $this->commentid);
2171 } else if ($this->action == 'edit') {
2172 $comment = wiki_get_comment($this->commentid);
2173 $edit = has_capability('mod/wiki:editcomment', $context);
2174 $owner = ($comment->userid == $USER->id);
2175 if ($owner && $edit) {
2176 $this->add_comment($this->content, $this->commentid);
2178 } else if ($this->action == 'delete') {
2179 $comment = wiki_get_comment($this->commentid);
2180 $manage = has_capability('mod/wiki:managecomment', $context);
2181 $owner = ($comment->userid == $USER->id);
2182 if ($owner || $manage) {
2183 $this->delete_comment($this->commentid);
2184 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
2190 public function set_url() {
2192 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2195 public function set_action($action, $commentid, $content) {
2196 $this->action = $action;
2197 $this->commentid = $commentid;
2198 $this->content = $content;
2200 $version = wiki_get_current_version($this->page->id);
2201 $format = $version->contentformat;
2203 $this->format = $format;
2206 private function add_comment($content, $idcomment) {
2208 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2210 $pageid = $this->page->id;
2212 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2214 wiki_add_comment($context, $pageid, $content, $this->format);
2217 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2219 $this->delete_comment($idcomment);
2220 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
2224 private function delete_comment($commentid) {
2228 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2229 $pageid = $this->page->id;
2231 wiki_delete_comment($commentid, $context, $pageid);
2236 class page_wiki_lock extends page_wiki_edit {
2238 public function print_header() {
2242 protected function set_url() {
2245 $params = array('pageid' => $this->page->id);
2247 if ($this->section) {
2248 $params['section'] = $this->section;
2251 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2254 protected function set_session_url() {
2257 public function print_content() {
2258 global $USER, $PAGE;
2260 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2261 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
2263 wiki_set_lock($this->page->id, $USER->id, $this->section);
2266 public function print_footer() {
2270 class page_wiki_overridelocks extends page_wiki_edit {
2271 function print_header() {
2275 function print_content() {
2278 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2279 require_capability('mod/wiki:overridelock', $context, NULL, true, 'nooverridelockpermission', 'wiki');
2281 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2283 $args = "pageid=" . $this->page->id;
2285 if (!empty($this->section)) {
2286 $args .= "§ion=" . $this->section;
2289 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2292 function set_url() {
2295 $params = array('pageid' => $this->page->id);
2297 if (!empty($this->section)) {
2298 $params['section'] = $this->section;
2301 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2304 protected function set_session_url() {
2307 private function print_overridelocks() {
2310 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2312 $args = "pageid=" . $this->page->id;
2314 if (!empty($this->section)) {
2315 $args .= "§ion=" . $this->section;
2318 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);