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;
90 * @var stdClass course module.
95 * page_wiki constructor
97 * @param $wiki. Current wiki
98 * @param $subwiki. Current subwiki.
99 * @param $cm. Current course_module.
101 function __construct($wiki, $subwiki, $cm) {
103 $this->subwiki = $subwiki;
105 $this->modcontext = context_module::instance($this->cm->id);
107 // initialise wiki renderer
108 $this->wikioutput = $PAGE->get_renderer('mod_wiki');
109 $PAGE->set_cacheable(true);
111 $PAGE->set_activity_record($wiki);
113 $PAGE->set_button(wiki_search_form($cm));
117 * This method prints the top of the page.
119 function print_header() {
120 global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
122 $PAGE->set_heading(format_string($PAGE->course->fullname));
126 if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
127 $this->process_session_url();
129 $this->set_session_url();
131 $this->create_navbar();
134 echo $OUTPUT->header();
136 echo $this->wikioutput->wiki_info();
138 // tabs are associated with pageid, so if page is empty, tabs should be disabled
139 if (!empty($this->page) && !empty($this->tabs)) {
140 echo $this->wikioutput->tabs($this->page, $this->tabs, $this->tabs_options);
145 * Protected method to print current page title.
147 protected function print_pagetitle() {
151 $html .= $OUTPUT->container_start();
152 $html .= $OUTPUT->heading(format_string($this->title), 2, 'wiki_headingtitle');
153 $html .= $OUTPUT->container_end();
158 * Setup page tabs, if options is empty, will set up active tab automatically
159 * @param array $options, tabs options
161 protected function setup_tabs($options = array()) {
163 $groupmode = groups_get_activity_groupmode($this->cm);
165 if (empty($CFG->usecomments) || !has_capability('mod/wiki:viewcomment', $PAGE->context)){
166 unset($this->tabs['comments']);
169 if (!has_capability('mod/wiki:editpage', $PAGE->context)){
170 unset($this->tabs['edit']);
173 if ($groupmode and $groupmode == VISIBLEGROUPS) {
174 $currentgroup = groups_get_activity_group($this->cm);
175 $manage = has_capability('mod/wiki:managewiki', $this->modcontext);
176 $edit = has_capability('mod/wiki:editpage', $PAGE->context);
177 if (!$manage and !($edit and groups_is_member($currentgroup))) {
178 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 // set_title calls format_string itself so no probs there
208 $PAGE->set_title($this->title);
212 * Method to set the current page title.
213 * This method must be called when the current page is not created yet.
214 * @param string $title Current page title.
216 function set_title($title) {
220 $this->title = $title;
221 // set_title calls format_string itself so no probs there
222 $PAGE->set_title($this->title);
226 * Method to set current group id
227 * @param int $gid Current group id
229 function set_gid($gid) {
234 * Method to set current user id
235 * @param int $uid Current user id
237 function set_uid($uid) {
242 * Method to set the URL of the page.
243 * This method must be overwritten by every type of page.
245 protected function set_url() {
246 throw new coding_exception('Page wiki class does not implement method set_url()');
250 * Protected method to create the common items of the navbar in every page type.
252 protected function create_navbar() {
255 $PAGE->navbar->add(format_string($this->title), $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
259 * This method print the footer of the page.
261 function print_footer() {
263 echo $OUTPUT->footer();
266 protected function process_session_url() {
267 global $USER, $SESSION;
269 //delete locks if edit
270 $url = $SESSION->wikipreviousurl;
271 switch ($url['page']) {
273 wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
278 protected function set_session_url() {
280 unset($SESSION->wikipreviousurl);
288 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
290 class page_wiki_view extends page_wiki {
292 function print_header() {
295 parent::print_header();
297 $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page, 'view');
299 if (!empty($this->page)) {
300 echo $this->wikioutput->prettyview_link($this->page);
303 //echo $this->wikioutput->page_index();
305 $this->print_pagetitle();
308 function print_content() {
311 if (wiki_user_can_view($this->subwiki)) {
313 if (!empty($this->page)) {
314 wiki_print_page_content($this->page, $this->modcontext, $this->subwiki->id);
315 $wiki = $PAGE->activityrecord;
317 print_string('nocontent', 'wiki');
318 // TODO: fix this part
320 if (!empty($this->subwiki)) {
321 $swid = $this->subwiki->id;
325 echo get_string('cannotviewpage', 'wiki');
333 if (isset($this->cm->id)) {
334 $params['id'] = $this->cm->id;
335 } else if (!empty($this->page) and $this->page != null) {
336 $params['pageid'] = $this->page->id;
337 } else if (!empty($this->gid)) {
338 $params['wid'] = $this->cm->instance;
339 $params['group'] = $this->gid;
340 } else if (!empty($this->title)) {
341 $params['swid'] = $this->subwiki->id;
342 $params['title'] = $this->title;
344 print_error(get_string('invalidparameters', 'wiki'));
346 $PAGE->set_url(new moodle_url($CFG->wwwroot . '/mod/wiki/view.php', $params));
349 protected function create_navbar() {
352 $PAGE->navbar->add(format_string($this->title));
353 $PAGE->navbar->add(get_string('view', 'wiki'));
358 * Wiki page editing page
360 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
362 class page_wiki_edit extends page_wiki {
364 public static $attachmentoptions;
366 protected $sectioncontent;
367 /** @var string the section name needed to be edited */
369 protected $overridelock = false;
370 protected $versionnumber = -1;
371 protected $upload = false;
372 protected $attachments = 0;
373 protected $deleteuploads = array();
376 function __construct($wiki, $subwiki, $cm) {
378 parent::__construct($wiki, $subwiki, $cm);
379 self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*');
380 $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
383 protected function print_pagetitle() {
386 $title = $this->title;
387 if (isset($this->section)) {
388 $title .= ' : ' . $this->section;
390 echo $OUTPUT->container_start('wiki_clear');
391 echo $OUTPUT->heading(format_string($title), 2, 'wiki_headingtitle');
392 echo $OUTPUT->container_end();
395 function print_header() {
396 global $OUTPUT, $PAGE;
397 $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
399 parent::print_header();
401 $this->print_pagetitle();
403 print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
406 function print_content() {
409 if (wiki_user_can_edit($this->subwiki)) {
412 echo get_string('cannoteditpage', 'wiki');
416 protected function set_url() {
419 $params = array('pageid' => $this->page->id);
421 if (isset($this->section)) {
422 $params['section'] = $this->section;
425 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
428 protected function set_session_url() {
431 $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
434 protected function process_session_url() {
437 function set_section($sectioncontent, $section) {
438 $this->sectioncontent = $sectioncontent;
439 $this->section = $section;
442 public function set_versionnumber($versionnumber) {
443 $this->versionnumber = $versionnumber;
446 public function set_overridelock($override) {
447 $this->overridelock = $override;
450 function set_format($format) {
451 $this->format = $format;
454 public function set_upload($upload) {
455 $this->upload = $upload;
458 public function set_attachments($attachments) {
459 $this->attachments = $attachments;
462 public function set_deleteuploads($deleteuploads) {
463 $this->deleteuploads = $deleteuploads;
466 protected function create_navbar() {
469 parent::create_navbar();
471 $PAGE->navbar->add(get_string('edit', 'wiki'));
474 protected function check_locks() {
475 global $OUTPUT, $USER, $CFG;
477 if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
478 print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
480 if ($this->overridelock) {
481 $params = 'pageid=' . $this->page->id;
483 if ($this->section) {
484 $params .= '§ion=' . urlencode($this->section);
487 $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
488 $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
489 $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
492 print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
499 protected function print_edit($content = null) {
500 global $CFG, $OUTPUT, $USER, $PAGE;
502 if (!$this->check_locks()) {
506 //delete old locks (> 1 hour)
507 wiki_delete_old_locks();
509 $version = wiki_get_current_version($this->page->id);
510 $format = $version->contentformat;
512 if ($content == null) {
513 if (empty($this->section)) {
514 $content = $version->content;
516 $content = $this->sectioncontent;
520 $versionnumber = $version->version;
521 if ($this->versionnumber >= 0) {
522 if ($version->version != $this->versionnumber) {
523 print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
524 $versionnumber = $this->versionnumber;
528 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
529 if (!empty($this->section)) {
530 $url .= "§ion=" . urlencode($this->section);
534 'attachmentoptions' => page_wiki_edit::$attachmentoptions,
535 'format' => $version->contentformat,
536 'version' => $versionnumber,
537 'pagetitle' => $this->page->title,
538 'contextid' => $this->modcontext->id
541 $data = new StdClass();
542 $data->newcontent = $content;
543 $data->version = $versionnumber;
544 $data->format = $format;
548 $data->newcontentformat = FORMAT_HTML;
549 // Append editor context to editor options, giving preference to existing context.
550 page_wiki_edit::$attachmentoptions = array_merge(array('context' => $this->modcontext), page_wiki_edit::$attachmentoptions);
551 $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
557 if ($version->contentformat != 'html') {
558 $params['fileitemid'] = $this->subwiki->id;
559 $params['component'] = 'mod_wiki';
560 $params['filearea'] = 'attachments';
563 if (!empty($CFG->usetags)) {
564 $params['tags'] = tag_get_tags_csv('wiki_pages', $this->page->id, TAG_RETURN_TEXT);
567 $form = new mod_wiki_edit_form($url, $params);
569 if ($formdata = $form->get_data()) {
570 if (!empty($CFG->usetags)) {
571 $data->tags = $formdata->tags;
574 if (!empty($CFG->usetags)) {
575 $data->tags = tag_get_tags_array('wiki', $this->page->id);
579 $form->set_data($data);
586 * Class that models the behavior of wiki's view comments page
588 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
590 class page_wiki_comments extends page_wiki {
592 function print_header() {
594 parent::print_header();
596 $this->print_pagetitle();
600 function print_content() {
601 global $CFG, $OUTPUT, $USER, $PAGE;
602 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
605 $subwiki = $this->subwiki;
606 $wiki = $PAGE->activityrecord;
607 list($context, $course, $cm) = get_context_info_array($this->modcontext->id);
609 require_capability('mod/wiki:viewcomment', $this->modcontext, NULL, true, 'noviewcommentpermission', 'wiki');
611 $comments = wiki_get_comments($this->modcontext->id, $page->id);
613 if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
614 echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
617 $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
618 $version = wiki_get_current_version($this->page->id);
619 $format = $version->contentformat;
621 if (empty($comments)) {
622 echo $OUTPUT->heading(get_string('nocomments', 'wiki'));
625 foreach ($comments as $comment) {
627 $user = wiki_get_user_info($comment->userid);
629 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)));
630 $by = new stdclass();
631 $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&course=' . $course->id . '">' . $fullname . '</a>';
632 $by->date = userdate($comment->timecreated);
634 $t = new html_table();
635 $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
636 $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
637 $cell3 = new html_table_cell();
638 $cell3->atributtes ['width'] = "80%";
639 $cell4 = new html_table_cell();
640 $cell5 = new html_table_cell();
642 $row1 = new html_table_row();
643 $row1->cells[] = $cell1;
644 $row1->cells[] = $cell2;
645 $row2 = new html_table_row();
646 $row2->cells[] = $cell3;
648 if ($format != 'html') {
649 if ($format == 'creole') {
650 $parsedcontent = wiki_parse_content('creole', $comment->content, $options);
651 } else if ($format == 'nwiki') {
652 $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options);
655 $cell4->text = format_text(html_entity_decode($parsedcontent['parsed_text'], ENT_QUOTES, 'UTF-8'), FORMAT_HTML);
657 $cell4->text = format_text($comment->content, FORMAT_HTML);
660 $row2->cells[] = $cell4;
662 $t->data = array($row1, $row2);
664 $actionicons = false;
665 if ((has_capability('mod/wiki:managecomment', $this->modcontext))) {
666 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
667 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
669 } else if ((has_capability('mod/wiki:editcomment', $this->modcontext)) and ($USER->id == $user->id)) {
670 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
671 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
676 $cell6 = new html_table_cell($OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit'),
677 '', array('class' => 'iconsmall'))) . $OUTPUT->action_icon($urldelet, new pix_icon('t/delete',
678 get_string('delete'), '', array('class' => 'iconsmall'))));
679 $row3 = new html_table_row();
680 $row3->cells[] = $cell5;
681 $row3->cells[] = $cell6;
685 echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow'));
692 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
695 protected function create_navbar() {
698 parent::create_navbar();
699 $PAGE->navbar->add(get_string('comments', 'wiki'));
705 * Class that models the behavior of wiki's edit comment
707 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
709 class page_wiki_editcomment extends page_wiki {
717 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
720 function print_header() {
721 parent::print_header();
722 $this->print_pagetitle();
725 function print_content() {
728 require_capability('mod/wiki:editcomment', $this->modcontext, NULL, true, 'noeditcommentpermission', 'wiki');
730 if ($this->action == 'add') {
731 $this->add_comment_form();
732 } else if ($this->action == 'edit') {
733 $this->edit_comment_form($this->comment);
737 function set_action($action, $comment) {
739 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
741 $this->action = $action;
742 $this->comment = $comment;
743 $version = wiki_get_current_version($this->page->id);
744 $this->format = $version->contentformat;
746 if ($this->format == 'html') {
747 $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
748 $this->form = new mod_wiki_comments_form($destination);
752 protected function create_navbar() {
755 $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
757 if ($this->action == 'add') {
758 $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
760 $PAGE->navbar->add(get_string('editcomment', 'wiki'));
764 protected function setup_tabs($options = array()) {
765 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
768 private function add_comment_form() {
770 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
772 $pageid = $this->page->id;
774 if ($this->format == 'html') {
775 $com = new stdClass();
776 $com->action = 'add';
777 $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
778 $this->form->set_data($com);
779 $this->form->display();
781 wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'addcomments');
785 private function edit_comment_form($com) {
787 require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
788 require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
790 if ($this->format == 'html') {
791 $com->action = 'edit';
792 $com->entrycomment_editor['text'] = $com->content;
793 $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
795 $this->form->set_data($com);
796 $this->form->display();
798 wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'editcomments', $com->id);
806 * Wiki page search page
808 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
810 class page_wiki_search extends page_wiki {
811 private $search_result;
813 protected function create_navbar() {
816 $PAGE->navbar->add(format_string($this->title));
819 function set_search_string($search, $searchcontent) {
820 $swid = $this->subwiki->id;
821 if ($searchcontent) {
822 $this->search_result = wiki_search_all($swid, $search);
824 $this->search_result = wiki_search_title($swid, $search);
831 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
833 function print_content() {
836 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
838 echo $this->wikioutput->search_result($this->search_result, $this->subwiki);
844 * Class that models the behavior of wiki's
848 class page_wiki_create extends page_wiki {
857 function print_header() {
859 parent::print_header();
866 $params['swid'] = $this->swid;
867 if ($this->action == 'new') {
868 $params['action'] = 'new';
869 $params['wid'] = $this->wid;
870 if ($this->title != get_string('newpage', 'wiki')) {
871 $params['title'] = $this->title;
874 $params['action'] = 'create';
876 $PAGE->set_url(new moodle_url('/mod/wiki/create.php', $params));
879 function set_format($format) {
880 $this->format = $format;
883 function set_wid($wid) {
887 function set_swid($swid) {
891 function set_availablegroups($group) {
892 $this->groups = $group;
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, 'group' => $this->gid, 'uid' => $this->uid));
901 $formats = wiki_get_formats();
902 $options = array('formats' => $formats, 'defaultformat' => $PAGE->activityrecord->defaultformat, 'forceformat' => $PAGE->activityrecord->forceformat, 'groups' => $this->groups);
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() {
911 // navigation_node::get_content formats this before printing.
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) {
933 $data = $this->mform->get_data();
934 if (isset($data->groupinfo)) {
935 $groupid = $data->groupinfo;
936 } else if (!empty($this->gid)) {
937 $groupid = $this->gid;
941 if (empty($this->subwiki)) {
942 // If subwiki is not set then try find one and set else create one.
943 if (!$this->subwiki = wiki_get_subwiki_by_group($this->wid, $groupid, $this->uid)) {
944 $swid = wiki_add_subwiki($PAGE->activityrecord->id, $groupid, $this->uid);
945 $this->subwiki = wiki_get_subwiki($swid);
949 $this->set_title($data->pagetitle);
950 $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
952 $this->set_title($pagetitle);
953 $id = wiki_create_page($this->subwiki->id, $pagetitle, $PAGE->activityrecord->defaultformat, $USER->id);
960 class page_wiki_preview extends page_wiki_edit {
964 function __construct($wiki, $subwiki, $cm) {
965 global $PAGE, $CFG, $OUTPUT;
966 parent::__construct($wiki, $subwiki, $cm);
967 $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
968 $PAGE->set_button($buttons);
972 function print_header() {
975 parent::print_header();
979 function print_content() {
982 require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
984 $this->print_preview();
987 function set_newcontent($newcontent) {
988 $this->newcontent = $newcontent;
994 $params = array('pageid' => $this->page->id
997 if (isset($this->section)) {
998 $params['section'] = $this->section;
1001 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
1004 protected function setup_tabs($options = array()) {
1005 parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
1008 protected function check_locks() {
1012 protected function print_preview() {
1013 global $CFG, $PAGE, $OUTPUT;
1015 $version = wiki_get_current_version($this->page->id);
1016 $format = $version->contentformat;
1017 $content = $version->content;
1019 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1020 if (!empty($this->section)) {
1021 $url .= "§ion=" . urlencode($this->section);
1024 'attachmentoptions' => page_wiki_edit::$attachmentoptions,
1025 'format' => $this->format,
1026 'version' => $this->versionnumber,
1027 'contextid' => $this->modcontext->id
1030 if ($this->format != 'html') {
1031 $params['component'] = 'mod_wiki';
1032 $params['filearea'] = 'attachments';
1033 $params['fileitemid'] = $this->page->id;
1035 $form = new mod_wiki_edit_form($url, $params);
1038 $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
1040 if ($data = $form->get_data()) {
1041 if (isset($data->newcontent)) {
1043 $text = $data->newcontent;
1046 $text = $data->newcontent_editor['text'];
1048 $parseroutput = wiki_parse_content($data->contentformat, $text, $options);
1049 $this->set_newcontent($text);
1050 echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info');
1051 $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true, 'filter'=>false));
1052 echo $OUTPUT->box($content, 'generalbox wiki_previewbox');
1053 $content = $this->newcontent;
1056 $this->print_edit($content);
1063 * Class that models the behavior of wiki's
1067 class page_wiki_diff extends page_wiki {
1070 private $comparewith;
1072 function print_header() {
1075 parent::print_header();
1077 $this->print_pagetitle();
1078 $vstring = new stdClass();
1079 $vstring->old = $this->compare;
1080 $vstring->new = $this->comparewith;
1081 echo $OUTPUT->heading(get_string('comparewith', 'wiki', $vstring));
1085 * Print the diff view
1087 function print_content() {
1090 require_capability('mod/wiki:viewpage', $this->modcontext, 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($options = array()) {
1115 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1119 * Given two versions of a page, prints a page displaying the differences between them.
1121 * @global object $CFG
1122 * @global object $OUTPUT
1123 * @global object $PAGE
1125 private function print_diff_content() {
1126 global $CFG, $OUTPUT, $PAGE;
1128 $pageid = $this->page->id;
1129 $total = wiki_count_wiki_page_versions($pageid) - 1;
1131 $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
1133 $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
1135 if ($oldversion && $newversion) {
1137 $oldtext = format_text(file_rewrite_pluginfile_urls($oldversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1138 $newtext = format_text(file_rewrite_pluginfile_urls($newversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
1139 list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
1140 $oldversion->diff = $diff1;
1141 $oldversion->user = wiki_get_user_info($oldversion->userid);
1142 $newversion->diff = $diff2;
1143 $newversion->user = wiki_get_user_info($newversion->userid);
1145 echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
1147 print_error('versionerror', 'wiki');
1154 * Class that models the behavior of wiki's history page
1157 class page_wiki_history extends page_wiki {
1159 * @var int $paging current page
1164 * @var int @rowsperpage Items per page
1166 private $rowsperpage = 10;
1169 * @var int $allversion if $allversion != 0, all versions will be printed in a signle table
1171 private $allversion;
1173 function __construct($wiki, $subwiki, $cm) {
1175 parent::__construct($wiki, $subwiki, $cm);
1176 $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
1179 function print_header() {
1180 parent::print_header();
1181 $this->print_pagetitle();
1184 function print_pagetitle() {
1188 $html .= $OUTPUT->container_start();
1189 $html .= $OUTPUT->heading_with_help(format_string($this->title), 'history', 'wiki');
1190 $html .= $OUTPUT->container_end();
1194 function print_content() {
1197 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1199 $this->print_history_content();
1202 function set_url() {
1204 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
1207 function set_paging($paging) {
1208 $this->paging = $paging;
1211 function set_allversion($allversion) {
1212 $this->allversion = $allversion;
1215 protected function create_navbar() {
1218 parent::create_navbar();
1219 $PAGE->navbar->add(get_string('history', 'wiki'));
1223 * Prints the history for a given wiki page
1225 * @global object $CFG
1226 * @global object $OUTPUT
1227 * @global object $PAGE
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 = fullname($creator);
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, 'course' => $this->cm->course));
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;
1281 $rowclass = array();
1283 foreach ($versions as $version) {
1284 $user = wiki_get_user_info($version->userid);
1285 $picture = $OUTPUT->user_picture($user, array('popup' => true));
1286 $date = userdate($version->timecreated, get_string('strftimedate'));
1287 $rowclass[] = 'wiki_histnewdate';
1288 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
1289 $versionid = wiki_get_version($version->id);
1291 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
1292 $viewlink = html_writer::link($url->out(false), $version->version);
1294 $viewlink = $version->version;
1296 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid, 'course' => $this->cm->course));
1297 $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'));
1300 $table = new html_table();
1302 $icon = $OUTPUT->help_icon('diff', 'wiki');
1304 $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
1305 $table->data = $contents;
1306 $table->attributes['class'] = 'generaltable mdl-align';
1307 $table->rowclasses = $rowclass;
1310 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
1311 echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
1312 echo html_writer::table($table);
1313 echo html_writer::start_tag('div', array('class'=>'mdl-align'));
1314 echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
1315 echo html_writer::end_tag('div');
1316 echo html_writer::end_tag('form');
1319 print_string('nohistory', 'wiki');
1321 if (!$this->allversion) {
1322 //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&');
1323 // $pagingbar->pagevar = $pagevar;
1324 echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&');
1325 //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&','paging');
1327 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
1328 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
1330 if ($vcount > $this->rowsperpage && !$this->allversion) {
1331 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
1332 $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
1337 * Given an array of values, creates a group of radio buttons to be part of a form
1339 * @param array $options An array of value-label pairs for the radio group (values as keys).
1340 * @param string $name Name of the radiogroup (unique in the form).
1341 * @param string $onclick Function to be executed when the radios are clicked.
1342 * @param string $checked The value that is already checked.
1343 * @param bool $return If true, return the HTML as a string, otherwise print it.
1345 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
1347 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
1349 static $idcounter = 0;
1355 $output = '<span class="radiogroup ' . $name . "\">\n";
1357 if (!empty($options)) {
1359 foreach ($options as $value => $label) {
1360 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
1361 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
1362 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
1363 if ($value == $checked) {
1364 $output .= ' checked="checked"';
1367 $output .= ' onclick="' . $onclick . '"';
1369 if ($label === '') {
1370 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
1372 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
1374 $currentradio = ($currentradio + 1) % 2;
1378 $output .= '</span>' . "\n";
1389 * Class that models the behavior of wiki's map page
1392 class page_wiki_map extends page_wiki {
1395 * @var int wiki view option
1399 function print_header() {
1400 parent::print_header();
1401 $this->print_pagetitle();
1404 function print_content() {
1407 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
1409 if ($this->view > 0) {
1410 //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
1413 switch ($this->view) {
1415 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1416 $this->print_contributions_content();
1419 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1420 $this->print_navigation_content();
1423 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1424 $this->print_orphaned_content();
1427 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1428 $this->print_index_content();
1431 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1432 $this->print_page_list_content();
1435 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1436 $this->print_updated_content();
1439 echo $this->wikioutput->menu_map($this->page->id, $this->view);
1440 $this->print_page_list_content();
1444 function set_view($option) {
1445 $this->view = $option;
1448 function set_url() {
1450 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
1453 protected function create_navbar() {
1456 parent::create_navbar();
1457 $PAGE->navbar->add(get_string('map', 'wiki'));
1461 * Prints the contributions tab content
1463 * @uses $OUTPUT, $USER
1466 private function print_contributions_content() {
1467 global $CFG, $OUTPUT, $USER;
1468 $page = $this->page;
1470 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1471 $fresh = wiki_refresh_cachedcontent($page);
1472 $page = $fresh['page'];
1475 $swid = $this->subwiki->id;
1477 $table = new html_table();
1478 $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
1479 $table->attributes['class'] = 'wiki_editor generalbox';
1480 $table->data = array();
1481 $table->rowclasses = array();
1483 $lastversions = array();
1487 if ($contribs = wiki_get_contributions($swid, $USER->id)) {
1488 foreach ($contribs as $contrib) {
1489 if (!array_key_exists($contrib->pageid, $pages)) {
1490 $page = wiki_get_page($contrib->pageid);
1491 $pages[$contrib->pageid] = $page;
1496 if (!array_key_exists($page->id, $lastversions)) {
1497 $version = wiki_get_last_version($page->id);
1498 $lastversions[$page->id] = $version;
1500 $version = $lastversions[$page->id];
1503 if (!array_key_exists($version->userid, $users)) {
1504 $user = wiki_get_user_info($version->userid);
1505 $users[$version->userid] = $user;
1507 $user = $users[$version->userid];
1510 $link = wiki_parser_link($page->title, array('swid' => $swid));
1511 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1513 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content'], true, array('context' => $this->modcontext)) . '</a>';
1514 $icon = $OUTPUT->user_picture($user, array('popup' => true));
1516 $table->data[] = array("$icon $linkpage");
1519 $table->data[] = array(get_string('nocontribs', 'wiki'));
1521 echo html_writer::table($table);
1525 * Prints the navigation tab content
1530 private function print_navigation_content() {
1532 $page = $this->page;
1534 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1535 $fresh = wiki_refresh_cachedcontent($page);
1536 $page = $fresh['page'];
1539 $tolinks = wiki_get_linked_to_pages($page->id);
1540 $fromlinks = wiki_get_linked_from_pages($page->id);
1542 $table = new html_table();
1543 $table->attributes['class'] = 'wiki_navigation_from';
1544 $table->head = array(get_string('navigationfrom', 'wiki') . $OUTPUT->help_icon('navigationfrom', 'wiki') . ':');
1545 $table->data = array();
1546 $table->rowclasses = array();
1547 foreach ($fromlinks as $link) {
1548 $lpage = wiki_get_page($link->frompageid);
1549 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1550 $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
1551 $table->rowclasses[] = 'mdl-align';
1554 $table_left = html_writer::table($table);
1556 $table = new html_table();
1557 $table->attributes['class'] = 'wiki_navigation_to';
1558 $table->head = array(get_string('navigationto', 'wiki') . $OUTPUT->help_icon('navigationto', 'wiki') . ':');
1559 $table->data = array();
1560 $table->rowclasses = array();
1561 foreach ($tolinks as $link) {
1562 if ($link->tomissingpage) {
1563 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
1564 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
1566 $lpage = wiki_get_page($link->topageid);
1567 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
1568 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
1570 $table->rowclasses[] = 'mdl-align';
1572 $table_right = html_writer::table($table);
1573 echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
1577 * Prints the index page tab content
1581 private function print_index_content() {
1583 $page = $this->page;
1585 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1586 $fresh = wiki_refresh_cachedcontent($page);
1587 $page = $fresh['page'];
1590 // navigation_node get_content calls format string for us
1591 $node = new navigation_node($page->title);
1595 $tree = wiki_build_tree($page, $node, $keys);
1597 $table = new html_table();
1598 $table->head = array(get_string('pageindex', 'wiki') . $OUTPUT->help_icon('pageindex', 'wiki'));
1599 $table->attributes['class'] = 'wiki_editor generalbox';
1600 $table->data[] = array($this->render_navigation_node($tree));
1602 echo html_writer::table($table);
1606 * Prints the page list tab content
1610 private function print_page_list_content() {
1612 $page = $this->page;
1614 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1615 $fresh = wiki_refresh_cachedcontent($page);
1616 $page = $fresh['page'];
1619 $pages = wiki_get_page_list($this->subwiki->id);
1621 $stdaux = new stdClass();
1622 $strspecial = get_string('special', 'wiki');
1624 foreach ($pages as $page) {
1625 // We need to format the title here to account for any filtering
1626 $letter = format_string($page->title, true, array('context' => $this->modcontext));
1627 $letter = core_text::substr($letter, 0, 1);
1628 if (preg_match('/^[a-zA-Z]$/', $letter)) {
1629 $letter = core_text::strtoupper($letter);
1630 $stdaux->{$letter}[] = wiki_parser_link($page);
1632 $stdaux->{$strspecial}[] = wiki_parser_link($page);
1636 $table = new html_table();
1637 $table->head = array(get_string('pagelist', 'wiki') . $OUTPUT->help_icon('pagelist', 'wiki'));
1638 $table->attributes['class'] = 'wiki_editor generalbox';
1639 $table->align = array('center');
1640 foreach ($stdaux as $key => $elem) {
1641 $table->data[] = array($key);
1642 foreach ($elem as $e) {
1643 $table->data[] = array(html_writer::link($e['url'], format_string($e['content'], true, array('context' => $this->modcontext))));
1646 echo html_writer::table($table);
1650 * Prints the orphaned tab content
1654 private function print_orphaned_content() {
1657 $page = $this->page;
1659 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1660 $fresh = wiki_refresh_cachedcontent($page);
1661 $page = $fresh['page'];
1664 $swid = $this->subwiki->id;
1666 $table = new html_table();
1667 $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
1668 $table->attributes['class'] = 'wiki_editor generalbox';
1669 $table->data = array();
1670 $table->rowclasses = array();
1672 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
1673 foreach ($orphanedpages as $page) {
1674 $link = wiki_parser_link($page->title, array('swid' => $swid));
1675 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1676 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
1679 $table->data[] = array(get_string('noorphanedpages', 'wiki'));
1682 echo html_writer::table($table);
1686 * Prints the updated tab content
1688 * @uses $COURSE, $OUTPUT
1691 private function print_updated_content() {
1692 global $COURSE, $OUTPUT;
1693 $page = $this->page;
1695 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1696 $fresh = wiki_refresh_cachedcontent($page);
1697 $page = $fresh['page'];
1700 $swid = $this->subwiki->id;
1702 $table = new html_table();
1703 $table->head = array(get_string('updatedpages', 'wiki') . $OUTPUT->help_icon('updatedpages', 'wiki'));
1704 $table->attributes['class'] = 'wiki_editor generalbox';
1705 $table->data = array();
1706 $table->rowclasses = array();
1708 if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
1710 foreach ($pages as $page) {
1711 $user = wiki_get_user_info($page->userid);
1712 $strdata = strftime('%d %b %Y', $page->timemodified);
1713 if ($strdata != $strdataux) {
1714 $table->data[] = array($OUTPUT->heading($strdata, 4));
1715 $strdataux = $strdata;
1717 $link = wiki_parser_link($page->title, array('swid' => $swid));
1718 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
1720 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
1721 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
1722 $table->data[] = array("$icon $linkpage");
1725 $table->data[] = array(get_string('noupdatedpages', 'wiki'));
1728 echo html_writer::table($table);
1731 protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
1733 // exit if empty, we don't want an empty ul element
1734 if (count($items) == 0) {
1738 // array of nested li elements
1740 foreach ($items as $item) {
1741 if (!$item->display) {
1744 $content = $item->get_content();
1745 $title = $item->get_title();
1746 if ($item->icon instanceof renderable) {
1747 $icon = $this->wikioutput->render($item->icon);
1748 $content = $icon . ' ' . $content; // use CSS for spacing of icons
1750 if ($item->helpbutton !== null) {
1751 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
1754 if ($content === '') {
1758 if ($item->action instanceof action_link) {
1759 //TODO: to be replaced with something else
1760 $link = $item->action;
1761 if ($item->hidden) {
1762 $link->add_class('dimmed');
1764 $content = $this->output->render($link);
1765 } else if ($item->action instanceof moodle_url) {
1766 $attributes = array();
1767 if ($title !== '') {
1768 $attributes['title'] = $title;
1770 if ($item->hidden) {
1771 $attributes['class'] = 'dimmed_text';
1773 $content = html_writer::link($item->action, $content, $attributes);
1775 } else if (is_string($item->action) || empty($item->action)) {
1776 $attributes = array();
1777 if ($title !== '') {
1778 $attributes['title'] = $title;
1780 if ($item->hidden) {
1781 $attributes['class'] = 'dimmed_text';
1783 $content = html_writer::tag('span', $content, $attributes);
1786 // this applies to the li item which contains all child lists too
1787 $liclasses = array($item->get_css_type(), 'depth_' . $depth);
1788 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
1789 $liclasses[] = 'collapsed';
1791 if ($item->isactive === true) {
1792 $liclasses[] = 'current_branch';
1794 $liattr = array('class' => join(' ', $liclasses));
1795 // class attribute on the div item which only contains the item content
1796 $divclasses = array('tree_item');
1797 if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
1798 $divclasses[] = 'branch';
1800 $divclasses[] = 'leaf';
1802 if (!empty($item->classes) && count($item->classes) > 0) {
1803 $divclasses[] = join(' ', $item->classes);
1805 $divattr = array('class' => join(' ', $divclasses));
1806 if (!empty($item->id)) {
1807 $divattr['id'] = $item->id;
1809 $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
1810 if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
1811 $content = html_writer::empty_tag('hr') . $content;
1813 $content = html_writer::tag('li', $content, $liattr);
1818 return html_writer::tag('ul', implode("\n", $lis), $attrs);
1827 * Class that models the behavior of wiki's restore version page
1830 class page_wiki_restoreversion extends page_wiki {
1833 function print_header() {
1834 parent::print_header();
1835 $this->print_pagetitle();
1838 function print_content() {
1841 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
1843 $this->print_restoreversion();
1846 function set_url() {
1848 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
1851 function set_versionid($versionid) {
1852 $this->version = wiki_get_version($versionid);
1855 protected function create_navbar() {
1858 parent::create_navbar();
1859 $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
1862 protected function setup_tabs($options = array()) {
1863 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
1867 * Prints the restore version content
1871 * @param page $page The page whose version will be restored
1872 * @param int $versionid The version to be restored
1873 * @param bool $confirm If false, shows a yes/no confirmation page.
1874 * If true, restores the old version and redirects the user to the 'view' tab.
1876 private function print_restoreversion() {
1879 $version = wiki_get_version($this->version->id);
1881 $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'versionid'=>$version->id, 'sesskey'=>sesskey());
1882 $restoreurl = new moodle_url('/mod/wiki/restoreversion.php', $optionsyes);
1883 $return = new moodle_url('/mod/wiki/viewversion.php', array('pageid'=>$this->page->id, 'versionid'=>$version->id));
1885 echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
1886 echo $OUTPUT->container_start(false, 'wiki_restoreform');
1887 echo '<form class="wiki_restore_yes" action="' . $restoreurl . '" method="post" id="restoreversion">';
1888 echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
1890 echo '<form class="wiki_restore_no" action="' . $return . '" method="post">';
1891 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1893 echo $OUTPUT->container_end();
1897 * Class that models the behavior of wiki's delete comment confirmation page
1900 class page_wiki_deletecomment extends page_wiki {
1903 function print_header() {
1904 parent::print_header();
1905 $this->print_pagetitle();
1908 function print_content() {
1909 $this->printconfirmdelete();
1912 function set_url() {
1914 $PAGE->set_url('/mod/wiki/instancecomments.php', array('pageid' => $this->page->id, 'commentid' => $this->commentid));
1917 public function set_action($action, $commentid, $content) {
1918 $this->action = $action;
1919 $this->commentid = $commentid;
1920 $this->content = $content;
1923 protected function create_navbar() {
1926 parent::create_navbar();
1927 $PAGE->navbar->add(get_string('deletecommentcheck', 'wiki'));
1930 protected function setup_tabs($options = array()) {
1931 parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
1935 * Prints the comment deletion confirmation form
1937 * @param page $page The page whose version will be restored
1938 * @param int $versionid The version to be restored
1939 * @param bool $confirm If false, shows a yes/no confirmation page.
1940 * If true, restores the old version and redirects the user to the 'view' tab.
1942 private function printconfirmdelete() {
1945 $strdeletecheck = get_string('deletecommentcheck', 'wiki');
1946 $strdeletecheckfull = get_string('deletecommentcheckfull', 'wiki');
1949 $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'action'=>'delete', 'commentid'=>$this->commentid, 'sesskey'=>sesskey());
1950 $deleteurl = new moodle_url('/mod/wiki/instancecomments.php', $optionsyes);
1951 $return = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$this->page->id));
1953 echo $OUTPUT->heading($strdeletecheckfull);
1954 echo $OUTPUT->container_start(false, 'wiki_deletecommentform');
1955 echo '<form class="wiki_deletecomment_yes" action="' . $deleteurl . '" method="post" id="deletecomment">';
1956 echo '<div><input type="submit" name="confirmdeletecomment" value="' . get_string('yes') . '" /></div>';
1958 echo '<form class="wiki_deletecomment_no" action="' . $return . '" method="post">';
1959 echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
1961 echo $OUTPUT->container_end();
1966 * Class that models the behavior of wiki's
1970 class page_wiki_save extends page_wiki_edit {
1972 private $newcontent;
1974 function print_header() {
1977 function print_content() {
1980 $context = context_module::instance($this->cm->id);
1981 require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
1983 $this->print_save();
1986 function set_newcontent($newcontent) {
1987 $this->newcontent = $newcontent;
1990 protected function set_session_url() {
1993 protected function print_save() {
1994 global $CFG, $USER, $OUTPUT, $PAGE;
1996 $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
1997 if (!empty($this->section)) {
1998 $url .= "§ion=" . urlencode($this->section);
2002 'attachmentoptions' => page_wiki_edit::$attachmentoptions,
2003 'format' => $this->format,
2004 'version' => $this->versionnumber,
2005 'contextid' => $this->modcontext->id
2008 if ($this->format != 'html') {
2009 $params['fileitemid'] = $this->page->id;
2010 $params['component'] = 'mod_wiki';
2011 $params['filearea'] = 'attachments';
2014 $form = new mod_wiki_edit_form($url, $params);
2018 if ($data = $form->get_data()) {
2019 if ($this->format == 'html') {
2020 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
2023 if (isset($this->section)) {
2024 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
2026 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
2030 if ($save && $data) {
2031 if (!empty($CFG->usetags)) {
2032 tag_set('wiki_pages', $this->page->id, $data->tags);
2035 $message = '<p>' . get_string('saving', 'wiki') . '</p>';
2037 if (!empty($save['sections'])) {
2038 foreach ($save['sections'] as $s) {
2039 $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
2043 if ($this->versionnumber + 1 != $save['version']) {
2044 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
2047 if (isset($errors) && !empty($errors)) {
2048 foreach ($errors as $e) {
2049 $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
2053 //deleting old locks
2054 wiki_delete_locks($this->page->id, $USER->id, $this->section);
2055 $url = new moodle_url('/mod/wiki/view.php', array('pageid' => $this->page->id, 'group' => $this->subwiki->groupid));
2058 print_error('savingerror', 'wiki');
2064 * Class that models the behavior of wiki's view an old version of a page
2067 class page_wiki_viewversion extends page_wiki {
2071 function print_header() {
2072 parent::print_header();
2073 $this->print_pagetitle();
2076 function print_content() {
2079 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2081 $this->print_version_view();
2084 function set_url() {
2086 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2089 function set_versionid($versionid) {
2090 $this->version = wiki_get_version($versionid);
2093 protected function create_navbar() {
2096 parent::create_navbar();
2097 $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $this->page->id);
2098 $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
2101 protected function setup_tabs($options = array()) {
2102 parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
2106 * Given an old page version, output the version content
2108 * @global object $CFG
2109 * @global object $OUTPUT
2110 * @global object $PAGE
2112 private function print_version_view() {
2113 global $CFG, $OUTPUT, $PAGE;
2114 $pageversion = wiki_get_version($this->version->id);
2117 $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2118 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);
2119 $userinfo = wiki_get_user_info($pageversion->userid);
2120 $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong> ' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
2121 $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
2122 $heading .= ' <strong>' . get_string('user') . ':</strong> ' . html_writer::link($viewlink->out(false), fullname($userinfo));
2123 $heading .= ' → ' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
2124 echo $OUTPUT->container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
2125 $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
2127 $pageversion->content = file_rewrite_pluginfile_urls($pageversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id);
2129 $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
2130 $content = $OUTPUT->container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
2131 echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
2134 print_error('versionerror', 'wiki');
2139 class page_wiki_confirmrestore extends page_wiki_save {
2143 function set_url() {
2145 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
2148 function print_content() {
2151 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
2153 $version = wiki_get_version($this->version->id);
2154 if (wiki_restore_page($this->page, $version->content, $version->userid)) {
2155 redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
2157 print_error('restoreerror', 'wiki', $version->version);
2161 function set_versionid($versionid) {
2162 $this->version = wiki_get_version($versionid);
2166 class page_wiki_prettyview extends page_wiki {
2168 function __construct($wiki, $subwiki, $cm) {
2170 $PAGE->set_pagelayout('embedded');
2171 parent::__construct($wiki, $subwiki, $cm);
2174 function print_header() {
2178 echo $OUTPUT->header();
2179 // Print dialog link.
2180 $printtext = get_string('print', 'wiki');
2181 $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'printicon');
2182 $printiconlink = html_writer::link('#', $printtext, $printlinkatt);
2183 echo html_writer::tag('div', $printiconlink, array('class' => 'displayprinticon'));
2184 echo html_writer::tag('h1', format_string($this->title), array('id' => 'wiki_printable_title'));
2187 function print_content() {
2190 require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2192 $this->print_pretty_view();
2195 function set_url() {
2198 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
2201 private function print_pretty_view() {
2202 $version = wiki_get_current_version($this->page->id);
2204 $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
2206 $html = $content['parsed_text'];
2207 $id = $this->subwiki->wikiid;
2208 if ($cm = get_coursemodule_from_instance("wiki", $id)) {
2209 $context = context_module::instance($cm->id);
2210 $html = file_rewrite_pluginfile_urls($html, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $this->subwiki->id);
2212 echo '<div id="wiki_printable_content">';
2213 echo format_text($html, FORMAT_HTML);
2218 class page_wiki_handlecomments extends page_wiki {
2224 function print_header() {
2228 public function print_content() {
2229 global $CFG, $PAGE, $USER;
2231 if ($this->action == 'add') {
2232 if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
2233 $this->add_comment($this->content, $this->commentid);
2235 } else if ($this->action == 'edit') {
2236 $comment = wiki_get_comment($this->commentid);
2237 $edit = has_capability('mod/wiki:editcomment', $this->modcontext);
2238 $owner = ($comment->userid == $USER->id);
2239 if ($owner && $edit) {
2240 $this->add_comment($this->content, $this->commentid);
2242 } else if ($this->action == 'delete') {
2243 $comment = wiki_get_comment($this->commentid);
2244 $manage = has_capability('mod/wiki:managecomment', $this->modcontext);
2245 $owner = ($comment->userid == $USER->id);
2246 if ($owner || $manage) {
2247 $this->delete_comment($this->commentid);
2248 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
2254 public function set_url() {
2256 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
2259 public function set_action($action, $commentid, $content) {
2260 $this->action = $action;
2261 $this->commentid = $commentid;
2262 $this->content = $content;
2264 $version = wiki_get_current_version($this->page->id);
2265 $format = $version->contentformat;
2267 $this->format = $format;
2270 private function add_comment($content, $idcomment) {
2272 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
2274 $pageid = $this->page->id;
2276 wiki_add_comment($this->modcontext, $pageid, $content, $this->format);
2279 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
2281 $this->delete_comment($idcomment);
2282 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
2286 private function delete_comment($commentid) {
2289 $pageid = $this->page->id;
2291 wiki_delete_comment($commentid, $this->modcontext, $pageid);
2296 class page_wiki_lock extends page_wiki_edit {
2298 public function print_header() {
2302 protected function set_url() {
2305 $params = array('pageid' => $this->page->id);
2307 if ($this->section) {
2308 $params['section'] = $this->section;
2311 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
2314 protected function set_session_url() {
2317 public function print_content() {
2318 global $USER, $PAGE;
2320 require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
2322 wiki_set_lock($this->page->id, $USER->id, $this->section);
2325 public function print_footer() {
2329 class page_wiki_overridelocks extends page_wiki_edit {
2330 function print_header() {
2334 function print_content() {
2337 require_capability('mod/wiki:overridelock', $this->modcontext, NULL, true, 'nooverridelockpermission', 'wiki');
2339 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2341 $args = "pageid=" . $this->page->id;
2343 if (!empty($this->section)) {
2344 $args .= "§ion=" . urlencode($this->section);
2347 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2350 function set_url() {
2353 $params = array('pageid' => $this->page->id);
2355 if (!empty($this->section)) {
2356 $params['section'] = $this->section;
2359 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
2362 protected function set_session_url() {
2365 private function print_overridelocks() {
2368 wiki_delete_locks($this->page->id, null, $this->section, true, true);
2370 $args = "pageid=" . $this->page->id;
2372 if (!empty($this->section)) {
2373 $args .= "§ion=" . urlencode($this->section);
2376 redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
2382 * This class will let user to delete wiki pages and page versions
2385 class page_wiki_admin extends page_wiki {
2387 public $view, $action;
2388 public $listorphan = false;
2393 * @global object $PAGE
2394 * @param mixed $wiki instance of wiki
2395 * @param mixed $subwiki instance of subwiki
2396 * @param stdClass $cm course module
2398 function __construct($wiki, $subwiki, $cm) {
2400 parent::__construct($wiki, $subwiki, $cm);
2401 $PAGE->requires->js_init_call('M.mod_wiki.deleteversion', null, true);
2405 * Prints header for wiki page
2407 function print_header() {
2408 parent::print_header();
2409 $this->print_pagetitle();
2413 * This function will display administration view to users with managewiki capability
2415 function print_content() {
2416 //make sure anyone trying to access this page has managewiki capabilities
2417 require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
2419 //update wiki cache if timedout
2420 $page = $this->page;
2421 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
2422 $fresh = wiki_refresh_cachedcontent($page);
2423 $page = $fresh['page'];
2426 //dispaly admin menu
2427 echo $this->wikioutput->menu_admin($this->page->id, $this->view);
2429 //Display appropriate admin view
2430 switch ($this->view) {
2431 case 1: //delete page view
2432 $this->print_delete_content($this->listorphan);
2434 case 2: //delete version view
2435 $this->print_delete_version();
2437 default: //default is delete view
2438 $this->print_delete_content($this->listorphan);
2444 * Sets admin view option
2446 * @param int $view page view id
2447 * @param bool $listorphan is only valid for view 1.
2449 public function set_view($view, $listorphan = true) {
2450 $this->view = $view;
2451 $this->listorphan = $listorphan;
2457 * @global object $PAGE
2458 * @global object $CFG
2460 function set_url() {
2462 $PAGE->set_url($CFG->wwwroot . '/mod/wiki/admin.php', array('pageid' => $this->page->id));
2466 * sets navigation bar for the page
2468 * @global object $PAGE
2470 protected function create_navbar() {
2473 parent::create_navbar();
2474 $PAGE->navbar->add(get_string('admin', 'wiki'));
2478 * Show wiki page delete options
2480 * @param bool $showorphan
2482 protected function print_delete_content($showorphan = true) {
2483 $contents = array();
2484 $table = new html_table();
2485 $table->head = array('', get_string('pagename','wiki'));
2486 $table->attributes['class'] = 'generaltable mdl-align';
2487 $swid = $this->subwiki->id;
2489 if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
2490 $this->add_page_delete_options($orphanedpages, $swid, $table);
2492 $table->data[] = array('', get_string('noorphanedpages', 'wiki'));
2495 if ($pages = wiki_get_page_list($swid)) {
2496 $this->add_page_delete_options($pages, $swid, $table);
2498 $table->data[] = array('', get_string('nopages', 'wiki'));
2503 echo html_writer::start_tag('form', array(
2504 'action' => new moodle_url('/mod/wiki/admin.php'),
2505 'method' => 'post'));
2506 echo html_writer::tag('div', html_writer::empty_tag('input', array(
2509 'value' => $this->page->id)));
2511 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2512 echo html_writer::table($table);
2513 echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2515 echo html_writer::empty_tag('input', array(
2517 'class' => 'wiki_form-button',
2518 'value' => get_string('listorphan', 'wiki'),
2519 'sesskey' => sesskey()));
2521 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'listall', 'value'=>'1'));
2522 echo html_writer::empty_tag('input', array(
2524 'class' => 'wiki_form-button',
2525 'value' => get_string('listall', 'wiki'),
2526 'sesskey' => sesskey()));
2528 echo html_writer::end_tag('div');
2529 echo html_writer::end_tag('form');
2533 * helper function for print_delete_content. This will add data to the table.
2535 * @global object $OUTPUT
2536 * @param array $pages objects of wiki pages in subwiki
2537 * @param int $swid id of subwiki
2538 * @param object $table reference to the table in which data needs to be added
2540 protected function add_page_delete_options($pages, $swid, &$table) {
2542 foreach ($pages as $page) {
2543 $link = wiki_parser_link($page->title, array('swid' => $swid));
2544 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
2545 $pagelink = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
2546 $urledit = new moodle_url('/mod/wiki/edit.php', array('pageid' => $page->id, 'sesskey' => sesskey()));
2547 $urldelete = new moodle_url('/mod/wiki/admin.php', array(
2548 'pageid' => $this->page->id,
2549 'delete' => $page->id,
2550 'option' => $this->view,
2551 'listall' => !$this->listorphan?'1': '',
2552 'sesskey' => sesskey()));
2554 $editlinks = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit')));
2555 $editlinks .= $OUTPUT->action_icon($urldelete, new pix_icon('t/delete', get_string('delete')));
2556 $table->data[] = array($editlinks, $pagelink);
2561 * Prints lists of versions which can be deleted
2563 * @global core_renderer $OUTPUT
2564 * @global moodle_page $PAGE
2566 private function print_delete_version() {
2567 global $OUTPUT, $PAGE;
2568 $pageid = $this->page->id;
2570 // versioncount is the latest version
2571 $versioncount = wiki_count_wiki_page_versions($pageid) - 1;
2572 $versions = wiki_get_wiki_page_versions($pageid, 0, $versioncount);
2574 // We don't want version 0 to be displayed
2575 // version 0 is blank page
2576 if (end($versions)->version == 0) {
2577 array_pop($versions);
2580 $contents = array();
2581 $version0page = wiki_get_wiki_page_version($this->page->id, 0);
2582 $creator = wiki_get_user_info($version0page->userid);
2583 $a = new stdClass();
2584 $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
2585 $a->username = fullname($creator);
2586 echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
2587 if ($versioncount > 0) {
2588 /// If there is only one version, we don't need radios nor forms
2589 if (count($versions) == 1) {
2590 $row = array_shift($versions);
2591 $username = wiki_get_user_info($row->userid);
2592 $picture = $OUTPUT->user_picture($username);
2593 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
2594 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
2595 $versionid = wiki_get_version($row->id);
2596 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2597 $userlink = new moodle_url('/user/view.php', array('id' => $username->id, 'course' => $this->cm->course));
2598 $picturelink = $picture . html_writer::link($userlink->out(false), fullname($username));
2599 $historydate = $OUTPUT->container($date, 'wiki_histdate');
2600 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picturelink, $time, $historydate);
2602 //Show current version
2603 $table = new html_table();
2604 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
2605 $table->data = $contents;
2606 $table->attributes['class'] = 'mdl-align';
2608 echo html_writer::table($table);
2611 $rowclass = array();
2613 foreach ($versions as $version) {
2614 $user = wiki_get_user_info($version->userid);
2615 $picture = $OUTPUT->user_picture($user, array('popup' => true));
2616 $date = userdate($version->timecreated, get_string('strftimedate'));
2617 if ($date == $lastdate) {
2622 $rowclass[] = 'wiki_histnewdate';
2625 $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
2626 $versionid = wiki_get_version($version->id);
2628 $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
2629 $viewlink = html_writer::link($url->out(false), $version->version);
2631 $viewlink = $version->version;
2634 $userlink = new moodle_url('/user/view.php', array('id' => $version->userid, 'course' => $this->cm->course));
2635 $picturelink = $picture . html_writer::link($userlink->out(false), fullname($user));
2636 $historydate = $OUTPUT->container($date, 'wiki_histdate');
2637 $radiofromelement = $this->choose_from_radio(array($version->version => null), 'fromversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2638 $radiotoelement = $this->choose_from_radio(array($version->version => null), 'toversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
2639 $contents[] = array( $radiofromelement . $radiotoelement, $viewlink, $picturelink, $time, $historydate);
2642 $table = new html_table();
2643 $table->head = array(get_string('deleteversions', 'wiki'), get_string('version'), get_string('user'), get_string('modified'), '');
2644 $table->data = $contents;
2645 $table->attributes['class'] = 'generaltable mdl-align';
2646 $table->rowclasses = $rowclass;
2649 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/admin.php'), 'method' => 'post'));
2650 echo html_writer::tag('div', html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pageid', 'value' => $pageid)));
2651 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
2652 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
2653 echo html_writer::table($table);
2654 echo html_writer::start_tag('div', array('class' => 'mdl-align'));
2655 echo html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'wiki_form-button', 'value' => get_string('deleteversions', 'wiki')));
2656 echo html_writer::end_tag('div');
2657 echo html_writer::end_tag('form');
2660 print_string('nohistory', 'wiki');
2665 * Given an array of values, creates a group of radio buttons to be part of a form
2666 * helper function for print_delete_version
2668 * @param array $options An array of value-label pairs for the radio group (values as keys).
2669 * @param string $name Name of the radiogroup (unique in the form).
2670 * @param string $onclick Function to be executed when the radios are clicked.
2671 * @param string $checked The value that is already checked.
2672 * @param bool $return If true, return the HTML as a string, otherwise print it.
2674 * @return mixed If $return is false, returns nothing, otherwise returns a string of HTML.
2676 private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
2678 static $idcounter = 0;
2684 $output = '<span class="radiogroup ' . $name . "\">\n";
2686 if (!empty($options)) {
2688 foreach ($options as $value => $label) {
2689 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
2690 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
2691 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
2692 if ($value == $checked) {
2693 $output .= ' checked="checked"';
2696 $output .= ' onclick="' . $onclick . '"';
2698 if ($label === '') {
2699 $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
2701 $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
2703 $currentradio = ($currentradio + 1) % 2;
2707 $output .= '</span>' . "\n";