Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/wiki/pagelib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
00017 
00036 require_once($CFG->dirroot . '/mod/wiki/edit_form.php');
00037 require_once($CFG->dirroot . '/tag/lib.php');
00038 
00044 abstract class page_wiki {
00045 
00049     protected $subwiki;
00050 
00054     protected $page;
00055 
00059     protected $title;
00060 
00064     protected $gid;
00065 
00069     protected $modcontext;
00070 
00074     protected $uid;
00078     protected $tabs = array('view' => 'view', 'edit' => 'edit', 'comments' => 'comments',
00079                             'history' => 'history', 'map' => 'map', 'files' => 'files',
00080                             'admin' => 'admin');
00084     protected $tabs_options = array();
00088     protected $wikioutput;
00089 
00097     function __construct($wiki, $subwiki, $cm) {
00098         global $PAGE, $CFG;
00099         $this->subwiki = $subwiki;
00100         $this->modcontext = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
00101 
00102         // initialise wiki renderer
00103         $this->wikioutput = $PAGE->get_renderer('mod_wiki');
00104         $PAGE->set_cacheable(true);
00105         $PAGE->set_cm($cm);
00106         $PAGE->set_activity_record($wiki);
00107         // the search box
00108         $PAGE->set_button(wiki_search_form($cm));
00109     }
00110 
00114     function print_header() {
00115         global $OUTPUT, $PAGE, $CFG, $USER, $SESSION;
00116 
00117         $PAGE->set_heading(format_string($PAGE->course->fullname));
00118 
00119         $this->set_url();
00120 
00121         if (isset($SESSION->wikipreviousurl) && is_array($SESSION->wikipreviousurl)) {
00122             $this->process_session_url();
00123         }
00124         $this->set_session_url();
00125 
00126         $this->create_navbar();
00127         $this->setup_tabs();
00128 
00129         echo $OUTPUT->header();
00130 
00131         echo $this->wikioutput->wiki_info();
00132 
00133         // tabs are associated with pageid, so if page is empty, tabs should be disabled
00134         if (!empty($this->page) && !empty($this->tabs)) {
00135             echo $this->wikioutput->tabs($this->page, $this->tabs, $this->tabs_options);
00136         }
00137     }
00138 
00142     protected function print_pagetitle() {
00143         global $OUTPUT;
00144         $html = '';
00145 
00146         $html .= $OUTPUT->container_start();
00147         $html .= $OUTPUT->heading(format_string($this->title), 2, 'wiki_headingtitle');
00148         $html .= $OUTPUT->container_end();
00149         echo $html;
00150     }
00151 
00156     protected function setup_tabs($options = array()) {
00157         global $CFG, $PAGE;
00158         $groupmode = groups_get_activity_groupmode($PAGE->cm);
00159 
00160         if (empty($CFG->usecomments) || !has_capability('mod/wiki:viewcomment', $PAGE->context)){
00161             unset($this->tabs['comments']);
00162         }
00163 
00164         if (!has_capability('mod/wiki:editpage', $PAGE->context)){
00165             unset($this->tabs['edit']);
00166         }
00167 
00168         if ($groupmode and $groupmode == VISIBLEGROUPS) {
00169             $currentgroup = groups_get_activity_group($PAGE->cm);
00170             $manage = has_capability('mod/wiki:managewiki', $PAGE->cm->context);
00171             $edit = has_capability('mod/wiki:editpage', $PAGE->context);
00172             if (!$manage and !($edit and groups_is_member($currentgroup))) {
00173                 unset($this->tabs['edit']);
00174             }
00175         } else {
00176             if (!has_capability('mod/wiki:editpage', $PAGE->context)) {
00177                 unset($this->tabs['edit']);
00178             }
00179         }
00180 
00181 
00182         if (empty($options)) {
00183             $this->tabs_options = array('activetab' => substr(get_class($this), 10));
00184         } else {
00185             $this->tabs_options = $options;
00186         }
00187 
00188     }
00189 
00193     function print_content() {
00194         throw new coding_exception('Page wiki class does not implement method print_content()');
00195     }
00196 
00202     function set_page($page) {
00203         global $PAGE;
00204 
00205         $this->page = $page;
00206         $this->title = $page->title;
00207         $PAGE->set_title($this->title);
00208     }
00209 
00215     function set_title($title) {
00216         global $PAGE;
00217 
00218         $this->page = null;
00219         $this->title = $title;
00220         $PAGE->set_title($this->title);
00221     }
00222 
00227     function set_gid($gid) {
00228         $this->gid = $gid;
00229     }
00230 
00235     function set_uid($uid) {
00236         $this->uid = $uid;
00237     }
00238 
00243     protected function set_url() {
00244         throw new coding_exception('Page wiki class does not implement method set_url()');
00245     }
00246 
00250     protected function create_navbar() {
00251         global $PAGE, $CFG;
00252 
00253         $PAGE->navbar->add(format_string($this->title), $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
00254     }
00255 
00259     function print_footer() {
00260         global $OUTPUT;
00261         echo $OUTPUT->footer();
00262     }
00263 
00264     protected function process_session_url() {
00265         global $USER, $SESSION;
00266 
00267         //delete locks if edit
00268         $url = $SESSION->wikipreviousurl;
00269         switch ($url['page']) {
00270         case 'edit':
00271             wiki_delete_locks($url['params']['pageid'], $USER->id, $url['params']['section'], false);
00272             break;
00273         }
00274     }
00275 
00276     protected function set_session_url() {
00277         global $SESSION;
00278         unset($SESSION->wikipreviousurl);
00279     }
00280 
00281 }
00282 
00288 class page_wiki_view extends page_wiki {
00292     private $coursemodule;
00293 
00294     function print_header() {
00295         global $PAGE;
00296 
00297         parent::print_header();
00298 
00299         $this->wikioutput->wiki_print_subwiki_selector($PAGE->activityrecord, $this->subwiki, $this->page, 'view');
00300 
00301         if (!empty($this->page)) {
00302             echo $this->wikioutput->prettyview_link($this->page);
00303         }
00304 
00305         //echo $this->wikioutput->page_index();
00306 
00307         $this->print_pagetitle();
00308     }
00309 
00310     function print_content() {
00311         global $PAGE, $CFG;
00312 
00313         if (wiki_user_can_view($this->subwiki)) {
00314 
00315             if (!empty($this->page)) {
00316                 wiki_print_page_content($this->page, $this->modcontext, $this->subwiki->id);
00317                 $wiki = $PAGE->activityrecord;
00318             } else {
00319                 print_string('nocontent', 'wiki');
00320                 // TODO: fix this part
00321                 $swid = 0;
00322                 if (!empty($this->subwiki)) {
00323                     $swid = $this->subwiki->id;
00324                 }
00325             }
00326         } else {
00327             // @TODO: Tranlate it
00328             echo "You can not view this page";
00329         }
00330     }
00331 
00332     function set_url() {
00333         global $PAGE, $CFG;
00334         $params = array();
00335 
00336         if (isset($this->coursemodule)) {
00337             $params['id'] = $this->coursemodule;
00338         } else if (!empty($this->page) and $this->page != null) {
00339             $params['pageid'] = $this->page->id;
00340         } else if (!empty($this->gid)) {
00341             $params['wid'] = $PAGE->cm->instance;
00342             $params['group'] = $this->gid;
00343         } else if (!empty($this->title)) {
00344             $params['swid'] = $this->subwiki->id;
00345             $params['title'] = $this->title;
00346         } else {
00347             print_error(get_string('invalidparameters', 'wiki'));
00348         }
00349 
00350         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/view.php', $params);
00351     }
00352 
00353     function set_coursemodule($id) {
00354         $this->coursemodule = $id;
00355     }
00356 
00357     protected function create_navbar() {
00358         global $PAGE, $CFG;
00359 
00360         $PAGE->navbar->add(format_string($this->title));
00361         $PAGE->navbar->add(get_string('view', 'wiki'));
00362     }
00363 }
00364 
00370 class page_wiki_edit extends page_wiki {
00371 
00372     public static $attachmentoptions;
00373 
00374     protected $sectioncontent;
00376     protected $section;
00377     protected $overridelock = false;
00378     protected $versionnumber = -1;
00379     protected $upload = false;
00380     protected $attachments = 0;
00381     protected $deleteuploads = array();
00382     protected $format;
00383 
00384     function __construct($wiki, $subwiki, $cm) {
00385         global $CFG, $PAGE;
00386         parent::__construct($wiki, $subwiki, $cm);
00387         self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*');
00388         $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true);
00389         $PAGE->requires->yui2_lib('connection');
00390     }
00391 
00392     protected function print_pagetitle() {
00393         global $OUTPUT;
00394 
00395         $title = $this->title;
00396         if (isset($this->section)) {
00397             $title .= ' : ' . $this->section;
00398         }
00399         echo $OUTPUT->container_start('wiki_clear');
00400         echo $OUTPUT->heading(format_string($title), 2, 'wiki_headingtitle');
00401         echo $OUTPUT->container_end();
00402     }
00403 
00404     function print_header() {
00405         global $OUTPUT, $PAGE;
00406         $PAGE->requires->data_for_js('wiki', array('renew_lock_timeout' => LOCK_TIMEOUT - 5, 'pageid' => $this->page->id, 'section' => $this->section));
00407 
00408         parent::print_header();
00409 
00410         $this->print_pagetitle();
00411 
00412         print '<noscript>' . $OUTPUT->box(get_string('javascriptdisabledlocks', 'wiki'), 'errorbox') . '</noscript>';
00413     }
00414 
00415     function print_content() {
00416         global $PAGE;
00417 
00418         if (wiki_user_can_edit($this->subwiki)) {
00419             $this->print_edit();
00420         } else {
00421             // @TODO: Translate it
00422             echo "You can not edit this page";
00423         }
00424     }
00425 
00426     protected function set_url() {
00427         global $PAGE, $CFG;
00428 
00429         $params = array('pageid' => $this->page->id);
00430 
00431         if (isset($this->section)) {
00432             $params['section'] = $this->section;
00433         }
00434 
00435         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
00436     }
00437 
00438     protected function set_session_url() {
00439         global $SESSION;
00440 
00441         $SESSION->wikipreviousurl = array('page' => 'edit', 'params' => array('pageid' => $this->page->id, 'section' => $this->section));
00442     }
00443 
00444     protected function process_session_url() {
00445     }
00446 
00447     function set_section($sectioncontent, $section) {
00448         $this->sectioncontent = $sectioncontent;
00449         $this->section = $section;
00450     }
00451 
00452     public function set_versionnumber($versionnumber) {
00453         $this->versionnumber = $versionnumber;
00454     }
00455 
00456     public function set_overridelock($override) {
00457         $this->overridelock = $override;
00458     }
00459 
00460     function set_format($format) {
00461         $this->format = $format;
00462     }
00463 
00464     public function set_upload($upload) {
00465         $this->upload = $upload;
00466     }
00467 
00468     public function set_attachments($attachments) {
00469         $this->attachments = $attachments;
00470     }
00471 
00472     public function set_deleteuploads($deleteuploads) {
00473         $this->deleteuploads = $deleteuploads;
00474     }
00475 
00476     protected function create_navbar() {
00477         global $PAGE, $CFG;
00478 
00479         parent::create_navbar();
00480 
00481         $PAGE->navbar->add(get_string('edit', 'wiki'));
00482     }
00483 
00484     protected function check_locks() {
00485         global $OUTPUT, $USER, $CFG;
00486 
00487         if (!wiki_set_lock($this->page->id, $USER->id, $this->section, true)) {
00488             print $OUTPUT->box(get_string('pageislocked', 'wiki'), 'generalbox boxwidthnormal boxaligncenter');
00489 
00490             if ($this->overridelock) {
00491                 $params = 'pageid=' . $this->page->id;
00492 
00493                 if ($this->section) {
00494                     $params .= '&section=' . urlencode($this->section);
00495                 }
00496 
00497                 $form = '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/overridelocks.php?' . $params . '">';
00498                 $form .= '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
00499                 $form .= '<input type="submit" value="' . get_string('overridelocks', 'wiki') . '" />';
00500                 $form .= '</form>';
00501 
00502                 print $OUTPUT->box($form, 'generalbox boxwidthnormal boxaligncenter');
00503             }
00504             return false;
00505         }
00506         return true;
00507     }
00508 
00509     protected function print_edit($content = null) {
00510         global $CFG, $OUTPUT, $USER, $PAGE;
00511 
00512         if (!$this->check_locks()) {
00513             return;
00514         }
00515 
00516         //delete old locks (> 1 hour)
00517         wiki_delete_old_locks();
00518 
00519         $version = wiki_get_current_version($this->page->id);
00520         $format = $version->contentformat;
00521 
00522         if ($content == null) {
00523             if (empty($this->section)) {
00524                 $content = $version->content;
00525             } else {
00526                 $content = $this->sectioncontent;
00527             }
00528         }
00529 
00530         $versionnumber = $version->version;
00531         if ($this->versionnumber >= 0) {
00532             if ($version->version != $this->versionnumber) {
00533                 print $OUTPUT->box(get_string('wrongversionlock', 'wiki'), 'errorbox');
00534                 $versionnumber = $this->versionnumber;
00535             }
00536         }
00537 
00538         $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
00539         if (!empty($this->section)) {
00540             $url .= "&section=" . urlencode($this->section);
00541         }
00542 
00543         $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $version->contentformat, 'version' => $versionnumber, 'pagetitle'=>$this->page->title);
00544 
00545         $data = new StdClass();
00546         $data->newcontent = $content;
00547         $data->version = $versionnumber;
00548         $data->format = $format;
00549 
00550         switch ($format) {
00551         case 'html':
00552             $data->newcontentformat = FORMAT_HTML;
00553             // Append editor context to editor options, giving preference to existing context.
00554             page_wiki_edit::$attachmentoptions = array_merge(array('context' => $this->modcontext), page_wiki_edit::$attachmentoptions);
00555             $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
00556             break;
00557         default:
00558             break;
00559             }
00560 
00561         if ($version->contentformat != 'html') {
00562             $params['fileitemid'] = $this->subwiki->id;
00563             $params['contextid']  = $this->modcontext->id;
00564             $params['component']  = 'mod_wiki';
00565             $params['filearea']   = 'attachments';
00566         }
00567 
00568         if (!empty($CFG->usetags)) {
00569             $params['tags'] = tag_get_tags_csv('wiki_pages', $this->page->id, TAG_RETURN_TEXT);
00570         }
00571 
00572         $form = new mod_wiki_edit_form($url, $params);
00573 
00574         if ($formdata = $form->get_data()) {
00575             if (!empty($CFG->usetags)) {
00576                 $data->tags = $formdata->tags;
00577             }
00578         } else {
00579             if (!empty($CFG->usetags)) {
00580                 $data->tags = tag_get_tags_array('wiki', $this->page->id);
00581             }
00582         }
00583 
00584         $form->set_data($data);
00585         $form->display();
00586     }
00587 
00588 }
00589 
00595 class page_wiki_comments extends page_wiki {
00596 
00597     function print_header() {
00598 
00599         parent::print_header();
00600 
00601         $this->print_pagetitle();
00602 
00603     }
00604 
00605     function print_content() {
00606         global $CFG, $OUTPUT, $USER, $PAGE;
00607         require_once($CFG->dirroot . '/mod/wiki/locallib.php');
00608 
00609         $page = $this->page;
00610         $subwiki = $this->subwiki;
00611         $wiki = $PAGE->activityrecord;
00612         list($context, $course, $cm) = get_context_info_array($this->modcontext->id);
00613 
00614         require_capability('mod/wiki:viewcomment', $this->modcontext, NULL, true, 'noviewcommentpermission', 'wiki');
00615 
00616         $comments = wiki_get_comments($this->modcontext->id, $page->id);
00617 
00618         if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
00619             echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&amp;pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>';
00620         }
00621 
00622         $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id);
00623         $version = wiki_get_current_version($this->page->id);
00624         $format = $version->contentformat;
00625 
00626         if (empty($comments)) {
00627             echo $OUTPUT->heading(get_string('nocomments', 'wiki'));
00628         }
00629 
00630         foreach ($comments as $comment) {
00631 
00632             $user = wiki_get_user_info($comment->userid);
00633 
00634             $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
00635             $by = new stdclass();
00636             $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . $course->id . '">' . $fullname . '</a>';
00637             $by->date = userdate($comment->timecreated);
00638 
00639             $t = new html_table();
00640             $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true)));
00641             $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by));
00642             $cell3 = new html_table_cell();
00643             $cell3->atributtes ['width'] = "80%";
00644             $cell4 = new html_table_cell();
00645             $cell5 = new html_table_cell();
00646 
00647             $row1 = new html_table_row();
00648             $row1->cells[] = $cell1;
00649             $row1->cells[] = $cell2;
00650             $row2 = new html_table_row();
00651             $row2->cells[] = $cell3;
00652 
00653             if ($format != 'html') {
00654                 if ($format == 'creole') {
00655                     $parsedcontent = wiki_parse_content('creole', $comment->content, $options);
00656                 } else if ($format == 'nwiki') {
00657                     $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options);
00658                 }
00659 
00660                 $cell4->text = format_text(html_entity_decode($parsedcontent['parsed_text']), FORMAT_HTML);
00661             } else {
00662                 $cell4->text = format_text($comment->content, FORMAT_HTML);
00663             }
00664 
00665             $row2->cells[] = $cell4;
00666 
00667             $t->data = array($row1, $row2);
00668 
00669             $actionicons = false;
00670             if ((has_capability('mod/wiki:managecomment', $this->modcontext))) {
00671                 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
00672                 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
00673                 $actionicons = true;
00674             } else if ((has_capability('mod/wiki:editcomment', $this->modcontext)) and ($USER->id == $user->id)) {
00675                 $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit'));
00676                 $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete'));
00677                 $actionicons = true;
00678             }
00679 
00680             if ($actionicons) {
00681                 $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'))));
00682                 $row3 = new html_table_row();
00683                 $row3->cells[] = $cell5;
00684                 $row3->cells[] = $cell6;
00685                 $t->data[] = $row3;
00686             }
00687 
00688             echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow'));
00689 
00690         }
00691     }
00692 
00693     function set_url() {
00694         global $PAGE, $CFG;
00695         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
00696     }
00697 
00698     protected function create_navbar() {
00699         global $PAGE, $CFG;
00700 
00701         parent::create_navbar();
00702         $PAGE->navbar->add(get_string('comments', 'wiki'));
00703     }
00704 
00705 }
00706 
00712 class page_wiki_editcomment extends page_wiki {
00713     private $comment;
00714     private $action;
00715     private $form;
00716     private $format;
00717 
00718     function set_url() {
00719         global $PAGE, $CFG;
00720         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
00721     }
00722 
00723     function print_header() {
00724         parent::print_header();
00725         $this->print_pagetitle();
00726     }
00727 
00728     function print_content() {
00729         global $PAGE;
00730 
00731         require_capability('mod/wiki:editcomment', $this->modcontext, NULL, true, 'noeditcommentpermission', 'wiki');
00732 
00733         if ($this->action == 'add') {
00734             $this->add_comment_form();
00735         } else if ($this->action == 'edit') {
00736             $this->edit_comment_form($this->comment);
00737         }
00738     }
00739 
00740     function set_action($action, $comment) {
00741         global $CFG;
00742         require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
00743 
00744         $this->action = $action;
00745         $this->comment = $comment;
00746         $version = wiki_get_current_version($this->page->id);
00747         $this->format = $version->contentformat;
00748 
00749         if ($this->format == 'html') {
00750             $destination = $CFG->wwwroot . '/mod/wiki/instancecomments.php?pageid=' . $this->page->id;
00751             $this->form = new mod_wiki_comments_form($destination);
00752         }
00753     }
00754 
00755     protected function create_navbar() {
00756         global $PAGE, $CFG;
00757 
00758         $PAGE->navbar->add(get_string('comments', 'wiki'), $CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id);
00759 
00760         if ($this->action == 'add') {
00761             $PAGE->navbar->add(get_string('insertcomment', 'wiki'));
00762         } else {
00763             $PAGE->navbar->add(get_string('editcomment', 'wiki'));
00764         }
00765     }
00766 
00767     protected function setup_tabs() {
00768         parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
00769     }
00770 
00771     private function add_comment_form() {
00772         global $CFG;
00773         require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
00774 
00775         $pageid = $this->page->id;
00776 
00777         if ($this->format == 'html') {
00778             $com = new stdClass();
00779             $com->action = 'add';
00780             $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
00781             $this->form->set_data($com);
00782             $this->form->display();
00783         } else {
00784             wiki_print_editor_wiki($this->page->id, null, $this->format, -1, null, false, null, 'addcomments');
00785         }
00786     }
00787 
00788     private function edit_comment_form($com) {
00789         global $CFG;
00790         require_once($CFG->dirroot . '/mod/wiki/comments_form.php');
00791         require_once($CFG->dirroot . '/mod/wiki/editors/wiki_editor.php');
00792 
00793         if ($this->format == 'html') {
00794             $com->action = 'edit';
00795             $com->entrycomment_editor['text'] = $com->content;
00796             $com->commentoptions = array('trusttext' => true, 'maxfiles' => 0);
00797 
00798             $this->form->set_data($com);
00799             $this->form->display();
00800         } else {
00801             wiki_print_editor_wiki($this->page->id, $com->content, $this->format, -1, null, false, array(), 'editcomments', $com->id);
00802         }
00803 
00804     }
00805 
00806 }
00807 
00813 class page_wiki_search extends page_wiki {
00814     private $search_result;
00815 
00816     protected function create_navbar() {
00817         global $PAGE, $CFG;
00818 
00819         $PAGE->navbar->add(format_string($this->title));
00820     }
00821 
00822     function set_search_string($search, $searchcontent) {
00823         $swid = $this->subwiki->id;
00824         if ($searchcontent) {
00825             $this->search_result = wiki_search_all($swid, $search);
00826         } else {
00827             $this->search_result = wiki_search_title($swid, $search);
00828         }
00829 
00830     }
00831 
00832     function set_url() {
00833         global $PAGE, $CFG;
00834         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/search.php');
00835     }
00836     function print_content() {
00837         global $PAGE;
00838 
00839         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
00840 
00841         echo $this->wikioutput->search_result($this->search_result, $this->subwiki);
00842     }
00843 }
00844 
00851 class page_wiki_create extends page_wiki {
00852 
00853     private $format;
00854     private $swid;
00855     private $wid;
00856     private $action;
00857     private $mform;
00858 
00859     function print_header() {
00860         $this->set_url();
00861         parent::print_header();
00862     }
00863 
00864     function set_url() {
00865         global $PAGE, $CFG;
00866 
00867         $params = array();
00868         if ($this->action == 'new') {
00869             $params['action'] = 'new';
00870             $params['swid'] = $this->swid;
00871             $params['wid'] = $this->wid;
00872             if ($this->title != get_string('newpage', 'wiki')) {
00873                 $params['title'] = $this->title;
00874             }
00875             $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
00876         } else {
00877             $params['action'] = 'create';
00878             $params['swid'] = $this->swid;
00879             $PAGE->set_url($CFG->wwwroot . '/mod/wiki/create.php', $params);
00880         }
00881     }
00882 
00883     function set_format($format) {
00884         $this->format = $format;
00885     }
00886 
00887     function set_wid($wid) {
00888         $this->wid = $wid;
00889     }
00890 
00891     function set_swid($swid) {
00892         $this->swid = $swid;
00893     }
00894 
00895     function set_action($action) {
00896         global $PAGE;
00897         $this->action = $action;
00898 
00899         require_once(dirname(__FILE__) . '/create_form.php');
00900         $url = new moodle_url('/mod/wiki/create.php', array('action' => 'create', 'wid' => $PAGE->activityrecord->id, 'gid' => $this->gid, 'uid' => $this->uid));
00901         $formats = wiki_get_formats();
00902         $options = array('formats' => $formats, 'defaultformat' => $PAGE->activityrecord->defaultformat, 'forceformat' => $PAGE->activityrecord->forceformat);
00903         if ($this->title != get_string('newpage', 'wiki')) {
00904             $options['disable_pagetitle'] = true;
00905         }
00906         $this->mform = new mod_wiki_create_form($url->out(false), $options);
00907     }
00908 
00909     protected function create_navbar() {
00910         global $PAGE;
00911 
00912         $PAGE->navbar->add($this->title);
00913     }
00914 
00915     function print_content($pagetitle = '') {
00916         global $PAGE;
00917 
00918         // @TODO: Change this to has_capability and show an alternative interface.
00919         require_capability('mod/wiki:createpage', $this->modcontext, NULL, true, 'nocreatepermission', 'wiki');
00920         $data = new stdClass();
00921         if (!empty($pagetitle)) {
00922             $data->pagetitle = $pagetitle;
00923         }
00924         $data->pageformat = $PAGE->activityrecord->defaultformat;
00925 
00926         $this->mform->set_data($data);
00927         $this->mform->display();
00928     }
00929 
00930     function create_page($pagetitle) {
00931         global $USER, $CFG, $PAGE;
00932         $data = $this->mform->get_data();
00933         if (empty($this->subwiki)) {
00934             $swid = wiki_add_subwiki($PAGE->activityrecord->id, $this->gid, $this->uid);
00935             $this->subwiki = wiki_get_subwiki($swid);
00936         }
00937         if ($data) {
00938             $id = wiki_create_page($this->subwiki->id, $data->pagetitle, $data->pageformat, $USER->id);
00939         } else {
00940             $id = wiki_create_page($this->subwiki->id, $pagetitle, $PAGE->activityrecord->defaultformat, $USER->id);
00941         }
00942         redirect($CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $id);
00943     }
00944 }
00945 
00946 class page_wiki_preview extends page_wiki_edit {
00947 
00948     private $newcontent;
00949 
00950     function __construct($wiki, $subwiki, $cm) {
00951         global $PAGE, $CFG, $OUTPUT;
00952         parent::__construct($wiki, $subwiki, $cm);
00953         $buttons = $OUTPUT->update_module_button($cm->id, 'wiki');
00954         $PAGE->set_button($buttons);
00955 
00956     }
00957 
00958     function print_header() {
00959         global $PAGE, $CFG;
00960 
00961         parent::print_header();
00962 
00963     }
00964 
00965     function print_content() {
00966         global $PAGE;
00967 
00968         require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
00969 
00970         $this->print_preview();
00971     }
00972 
00973     function set_newcontent($newcontent) {
00974         $this->newcontent = $newcontent;
00975     }
00976 
00977     function set_url() {
00978         global $PAGE, $CFG;
00979 
00980         $params = array('pageid' => $this->page->id
00981         );
00982 
00983         if (isset($this->section)) {
00984             $params['section'] = $this->section;
00985         }
00986 
00987         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/edit.php', $params);
00988     }
00989 
00990     protected function setup_tabs() {
00991         parent::setup_tabs(array('linkedwhenactive' => 'view', 'activetab' => 'view'));
00992     }
00993 
00994     protected function check_locks() {
00995         return true;
00996     }
00997 
00998     protected function print_preview() {
00999         global $CFG, $PAGE, $OUTPUT;
01000 
01001         $version = wiki_get_current_version($this->page->id);
01002         $format = $version->contentformat;
01003         $content = $version->content;
01004 
01005         $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
01006         if (!empty($this->section)) {
01007             $url .= "&section=" . urlencode($this->section);
01008         }
01009         $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
01010 
01011         if ($this->format != 'html') {
01012             $params['contextid'] = $this->modcontext->id;
01013             $params['component'] = 'mod_wiki';
01014             $params['filearea'] = 'attachments';
01015             $params['fileitemid'] = $this->page->id;
01016         }
01017         $form = new mod_wiki_edit_form($url, $params);
01018 
01019 
01020         $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true);
01021 
01022         if ($data = $form->get_data()) {
01023             if (isset($data->newcontent)) {
01024                 // wiki fromat
01025                 $text = $data->newcontent;
01026             } else {
01027                 // html format
01028                 $text = $data->newcontent_editor['text'];
01029             }
01030             $parseroutput = wiki_parse_content($data->contentformat, $text, $options);
01031             $this->set_newcontent($text);
01032             echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info');
01033             $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true, 'filter'=>false));
01034             echo $OUTPUT->box($content, 'generalbox wiki_previewbox');
01035             $content = $this->newcontent;
01036         }
01037 
01038         $this->print_edit($content);
01039     }
01040 
01041 }
01042 
01049 class page_wiki_diff extends page_wiki {
01050 
01051     private $compare;
01052     private $comparewith;
01053 
01054     function print_header() {
01055         global $OUTPUT;
01056 
01057         parent::print_header();
01058 
01059         $this->print_pagetitle();
01060         $vstring = new stdClass();
01061         $vstring->old = $this->compare;
01062         $vstring->new = $this->comparewith;
01063         echo $OUTPUT->heading(get_string('comparewith', 'wiki', $vstring));
01064     }
01065 
01069     function print_content() {
01070         global $PAGE;
01071 
01072         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
01073 
01074         $this->print_diff_content();
01075     }
01076 
01077     function set_url() {
01078         global $PAGE, $CFG;
01079 
01080         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/diff.php', array('pageid' => $this->page->id, 'comparewith' => $this->comparewith, 'compare' => $this->compare));
01081     }
01082 
01083     function set_comparison($compare, $comparewith) {
01084         $this->compare = $compare;
01085         $this->comparewith = $comparewith;
01086     }
01087 
01088     protected function create_navbar() {
01089         global $PAGE, $CFG;
01090 
01091         parent::create_navbar();
01092         $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
01093         $PAGE->navbar->add(get_string('diff', 'wiki'));
01094     }
01095 
01096     protected function setup_tabs() {
01097         parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
01098     }
01099 
01107     private function print_diff_content() {
01108         global $CFG, $OUTPUT, $PAGE;
01109 
01110         $pageid = $this->page->id;
01111         $total = wiki_count_wiki_page_versions($pageid) - 1;
01112 
01113         $oldversion = wiki_get_wiki_page_version($pageid, $this->compare);
01114 
01115         $newversion = wiki_get_wiki_page_version($pageid, $this->comparewith);
01116 
01117         if ($oldversion && $newversion) {
01118 
01119             $oldtext = format_text(file_rewrite_pluginfile_urls($oldversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
01120             $newtext = format_text(file_rewrite_pluginfile_urls($newversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id));
01121             list($diff1, $diff2) = ouwiki_diff_html($oldtext, $newtext);
01122             $oldversion->diff = $diff1;
01123             $oldversion->user = wiki_get_user_info($oldversion->userid);
01124             $newversion->diff = $diff2;
01125             $newversion->user = wiki_get_user_info($newversion->userid);
01126 
01127             echo $this->wikioutput->diff($pageid, $oldversion, $newversion, array('total' => $total));
01128         } else {
01129             print_error('versionerror', 'wiki');
01130         }
01131     }
01132 }
01133 
01139 class page_wiki_history extends page_wiki {
01143     private $paging;
01144 
01148     private $rowsperpage = 10;
01149 
01153     private $allversion;
01154 
01155     function __construct($wiki, $subwiki, $cm) {
01156         global $PAGE;
01157         parent::__construct($wiki, $subwiki, $cm);
01158         $PAGE->requires->js_init_call('M.mod_wiki.history', null, true);
01159     }
01160 
01161     function print_header() {
01162         parent::print_header();
01163         $this->print_pagetitle();
01164     }
01165 
01166     function print_pagetitle() {
01167         global $OUTPUT;
01168         $html = '';
01169 
01170         $html .= $OUTPUT->container_start();
01171         $html .= $OUTPUT->heading_with_help(format_string($this->title), 'history', 'wiki');
01172         $html .= $OUTPUT->container_end();
01173         echo $html;
01174     }
01175 
01176     function print_content() {
01177         global $PAGE;
01178 
01179         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
01180 
01181         $this->print_history_content();
01182     }
01183 
01184     function set_url() {
01185         global $PAGE, $CFG;
01186         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/history.php', array('pageid' => $this->page->id));
01187     }
01188 
01189     function set_paging($paging) {
01190         $this->paging = $paging;
01191     }
01192 
01193     function set_allversion($allversion) {
01194         $this->allversion = $allversion;
01195     }
01196 
01197     protected function create_navbar() {
01198         global $PAGE, $CFG;
01199 
01200         parent::create_navbar();
01201         $PAGE->navbar->add(get_string('history', 'wiki'));
01202     }
01203 
01211     private function print_history_content() {
01212         global $CFG, $OUTPUT, $PAGE;
01213 
01214         $pageid = $this->page->id;
01215         $offset = $this->paging * $this->rowsperpage;
01216         // vcount is the latest version
01217         $vcount = wiki_count_wiki_page_versions($pageid) - 1;
01218         if ($this->allversion) {
01219             $versions = wiki_get_wiki_page_versions($pageid, 0, $vcount);
01220         } else {
01221             $versions = wiki_get_wiki_page_versions($pageid, $offset, $this->rowsperpage);
01222         }
01223         // We don't want version 0 to be displayed
01224         // version 0 is blank page
01225         if (end($versions)->version == 0) {
01226             array_pop($versions);
01227         }
01228 
01229         $contents = array();
01230 
01231         $version0page = wiki_get_wiki_page_version($this->page->id, 0);
01232         $creator = wiki_get_user_info($version0page->userid);
01233         $a = new StdClass;
01234         $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
01235         $a->username = fullname($creator);
01236         echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
01237         if ($vcount > 0) {
01238 
01240             if (count($versions) == 1) {
01241 
01242                 $row = array_shift($versions);
01243 
01244                 $username = wiki_get_user_info($row->userid);
01245                 $picture = $OUTPUT->user_picture($username);
01246                 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
01247                 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
01248                 $versionid = wiki_get_version($row->id);
01249                 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
01250                 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
01251                 $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'));
01252 
01253                 $table = new html_table();
01254                 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
01255                 $table->data = $contents;
01256                 $table->attributes['class'] = 'mdl-align';
01257 
01258                 echo html_writer::table($table);
01259 
01260             } else {
01261 
01262                 $checked = $vcount - $offset;
01263                 $rowclass = array();
01264 
01265                 foreach ($versions as $version) {
01266                     $user = wiki_get_user_info($version->userid);
01267                     $picture = $OUTPUT->user_picture($user, array('popup' => true));
01268                     $date = userdate($version->timecreated, get_string('strftimedate'));
01269                     $rowclass[] = 'wiki_histnewdate';
01270                     $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
01271                     $versionid = wiki_get_version($version->id);
01272                     if ($versionid) {
01273                         $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
01274                         $viewlink = html_writer::link($url->out(false), $version->version);
01275                     } else {
01276                         $viewlink = $version->version;
01277                     }
01278                     $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
01279                     $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'));
01280                 }
01281 
01282                 $table = new html_table();
01283 
01284                 $icon = $OUTPUT->help_icon('diff', 'wiki');
01285 
01286                 $table->head = array(get_string('diff', 'wiki') . $icon, get_string('version'), get_string('user'), get_string('modified'), '');
01287                 $table->data = $contents;
01288                 $table->attributes['class'] = 'generaltable mdl-align';
01289                 $table->rowclasses = $rowclass;
01290 
01291                 /*$table = new StdClass();
01292                  $table->head = array(helpbutton('diff', 'diff', 'wiki', true, false, '', true, ''),
01293                  get_string('version'),
01294                  get_string('user'),
01295                  get_string('modified'),
01296                  '');
01297                  $table->data = $contents;
01298                  $table->class = 'mdl-align';
01299                  $table->rowclass = $rowclass;*/
01300 
01302                 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/diff.php'), 'method'=>'get', 'id'=>'diff'));
01303                 echo html_writer::tag('div', html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$pageid)));
01304                 echo html_writer::table($table);
01305                 echo html_writer::start_tag('div', array('class'=>'mdl-align'));
01306                 echo html_writer::empty_tag('input', array('type'=>'submit', 'class'=>'wiki_form-button', 'value'=>get_string('comparesel', 'wiki')));
01307                 echo html_writer::end_tag('div');
01308                 echo html_writer::end_tag('form');
01309             }
01310         } else {
01311             print_string('nohistory', 'wiki');
01312         }
01313         if (!$this->allversion) {
01314             //$pagingbar = moodle_paging_bar::make($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;');
01315             // $pagingbar->pagevar = $pagevar;
01316             echo $OUTPUT->paging_bar($vcount, $this->paging, $this->rowsperpage, $CFG->wwwroot . '/mod/wiki/history.php?pageid=' . $pageid . '&amp;');
01317             //print_paging_bar($vcount, $paging, $rowsperpage,$CFG->wwwroot.'/mod/wiki/history.php?pageid='.$pageid.'&amp;','paging');
01318             } else {
01319             $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
01320             $OUTPUT->container(html_writer::link($link->out(false), get_string('viewperpage', 'wiki', $this->rowsperpage)), 'mdl-align');
01321         }
01322         if ($vcount > $this->rowsperpage && !$this->allversion) {
01323             $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid, 'allversion' => 1));
01324             $OUTPUT->container(html_writer::link($link->out(false), get_string('viewallhistory', 'wiki')), 'mdl-align');
01325         }
01326     }
01327 
01339     private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
01340 
01341         static $idcounter = 0;
01342 
01343         if (!$name) {
01344             $name = 'unnamed';
01345         }
01346 
01347         $output = '<span class="radiogroup ' . $name . "\">\n";
01348 
01349         if (!empty($options)) {
01350             $currentradio = 0;
01351             foreach ($options as $value => $label) {
01352                 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
01353                 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
01354                 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
01355                 if ($value == $checked) {
01356                     $output .= ' checked="checked"';
01357                 }
01358                 if ($onclick) {
01359                     $output .= ' onclick="' . $onclick . '"';
01360                 }
01361                 if ($label === '') {
01362                     $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
01363                 } else {
01364                     $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
01365                 }
01366                 $currentradio = ($currentradio + 1) % 2;
01367             }
01368         }
01369 
01370         $output .= '</span>' . "\n";
01371 
01372         if ($return) {
01373             return $output;
01374         } else {
01375             echo $output;
01376         }
01377     }
01378 }
01379 
01384 class page_wiki_map extends page_wiki {
01385 
01389     private $view;
01390 
01391     function print_header() {
01392         parent::print_header();
01393         $this->print_pagetitle();
01394     }
01395 
01396     function print_content() {
01397         global $CFG, $PAGE;
01398 
01399         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
01400 
01401         if ($this->view > 0) {
01402             //echo '<div><a href="' . $CFG->wwwroot . '/mod/wiki/map.php?pageid=' . $this->page->id . '">' . get_string('backtomapmenu', 'wiki') . '</a></div>';
01403         }
01404 
01405         switch ($this->view) {
01406         case 1:
01407             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01408             $this->print_contributions_content();
01409             break;
01410         case 2:
01411             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01412             $this->print_navigation_content();
01413             break;
01414         case 3:
01415             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01416             $this->print_orphaned_content();
01417             break;
01418         case 4:
01419             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01420             $this->print_index_content();
01421             break;
01422         case 5:
01423             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01424             $this->print_page_list_content();
01425             break;
01426         case 6:
01427             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01428             $this->print_updated_content();
01429             break;
01430         default:
01431             echo $this->wikioutput->menu_map($this->page->id, $this->view);
01432             $this->print_page_list_content();
01433         }
01434     }
01435 
01436     function set_view($option) {
01437         $this->view = $option;
01438     }
01439 
01440     function set_url() {
01441         global $PAGE, $CFG;
01442         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/map.php', array('pageid' => $this->page->id));
01443     }
01444 
01445     protected function create_navbar() {
01446         global $PAGE;
01447 
01448         parent::create_navbar();
01449         $PAGE->navbar->add(get_string('map', 'wiki'));
01450     }
01451 
01458     private function print_contributions_content() {
01459         global $CFG, $OUTPUT, $USER;
01460         $page = $this->page;
01461 
01462         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01463             $fresh = wiki_refresh_cachedcontent($page);
01464             $page = $fresh['page'];
01465         }
01466 
01467         $swid = $this->subwiki->id;
01468 
01469         $table = new html_table();
01470         $table->head = array(get_string('contributions', 'wiki') . $OUTPUT->help_icon('contributions', 'wiki'));
01471         $table->attributes['class'] = 'wiki_editor generalbox';
01472         $table->data = array();
01473         $table->rowclasses = array();
01474 
01475         $lastversions = array();
01476         $pages = array();
01477         $users = array();
01478 
01479         if ($contribs = wiki_get_contributions($swid, $USER->id)) {
01480             foreach ($contribs as $contrib) {
01481                 if (!array_key_exists($contrib->pageid, $pages)) {
01482                     $page = wiki_get_page($contrib->pageid);
01483                     $pages[$contrib->pageid] = $page;
01484                 } else {
01485                     continue;
01486                 }
01487 
01488                 if (!array_key_exists($page->id, $lastversions)) {
01489                     $version = wiki_get_last_version($page->id);
01490                     $lastversions[$page->id] = $version;
01491                 } else {
01492                     $version = $lastversions[$page->id];
01493                 }
01494 
01495                 if (!array_key_exists($version->userid, $users)) {
01496                     $user = wiki_get_user_info($version->userid);
01497                     $users[$version->userid] = $user;
01498                 } else {
01499                     $user = $users[$version->userid];
01500                 }
01501 
01502                 $link = wiki_parser_link(format_string($page->title), array('swid' => $swid));
01503                 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
01504 
01505                 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . $link['content'] . '</a>';
01506                 $icon = $OUTPUT->user_picture($user, array('popup' => true));
01507 
01508                 $table->data[] = array("$icon&nbsp;$linkpage");
01509             }
01510         } else {
01511             $table->data[] = array(get_string('nocontribs', 'wiki'));
01512         }
01513         echo html_writer::table($table);
01514     }
01515 
01522     private function print_navigation_content() {
01523         global $OUTPUT;
01524         $page = $this->page;
01525 
01526         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01527             $fresh = wiki_refresh_cachedcontent($page);
01528             $page = $fresh['page'];
01529         }
01530 
01531         $tolinks = wiki_get_linked_to_pages($page->id);
01532         $fromlinks = wiki_get_linked_from_pages($page->id);
01533 
01534         $table = new html_table();
01535         $table->attributes['class'] = 'wiki_navigation_from';
01536         $table->head = array(get_string('navigationfrom', 'wiki') . $OUTPUT->help_icon('navigationfrom', 'wiki') . ':');
01537         $table->data = array();
01538         $table->rowclasses = array();
01539         foreach ($fromlinks as $link) {
01540             $lpage = wiki_get_page($link->frompageid);
01541             $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
01542             $table->data[] = array(html_writer::link($link->out(false), format_string($lpage->title)));
01543             $table->rowclasses[] = 'mdl-align';
01544         }
01545 
01546         $table_left = html_writer::table($table);
01547 
01548         $table = new html_table();
01549         $table->attributes['class'] = 'wiki_navigation_to';
01550         $table->head = array(get_string('navigationto', 'wiki') . $OUTPUT->help_icon('navigationto', 'wiki') . ':');
01551         $table->data = array();
01552         $table->rowclasses = array();
01553         foreach ($tolinks as $link) {
01554             if ($link->tomissingpage) {
01555                 $viewlink = new moodle_url('/mod/wiki/create.php', array('swid' => $page->subwikiid, 'title' => $link->tomissingpage, 'action' => 'new'));
01556                 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($link->tomissingpage), array('class' => 'wiki_newentry')));
01557             } else {
01558                 $lpage = wiki_get_page($link->topageid);
01559                 $viewlink = new moodle_url('/mod/wiki/view.php', array('pageid' => $lpage->id));
01560                 $table->data[] = array(html_writer::link($viewlink->out(false), format_string($lpage->title)));
01561             }
01562             $table->rowclasses[] = 'mdl-align';
01563         }
01564         $table_right = html_writer::table($table);
01565         echo $OUTPUT->container($table_left . $table_right, 'wiki_navigation_container');
01566     }
01567 
01573     private function print_index_content() {
01574         global $OUTPUT;
01575         $page = $this->page;
01576 
01577         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01578             $fresh = wiki_refresh_cachedcontent($page);
01579             $page = $fresh['page'];
01580         }
01581 
01582         $node = new navigation_node($page->title);
01583 
01584         $keys = array();
01585         $tree = array();
01586         $tree = wiki_build_tree($page, $node, $keys);
01587 
01588         $table = new html_table();
01589         $table->head = array(get_string('pageindex', 'wiki') . $OUTPUT->help_icon('pageindex', 'wiki'));
01590         $table->attributes['class'] = 'wiki_editor generalbox';
01591         $table->data[] = array($this->render_navigation_node($tree));
01592 
01593         echo html_writer::table($table);
01594     }
01595 
01601     private function print_page_list_content() {
01602         global $OUTPUT;
01603         $page = $this->page;
01604 
01605         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01606             $fresh = wiki_refresh_cachedcontent($page);
01607             $page = $fresh['page'];
01608         }
01609 
01610         $pages = wiki_get_page_list($this->subwiki->id);
01611 
01612         $stdaux = new stdClass();
01613         $strspecial = get_string('special', 'wiki');
01614 
01615         foreach ($pages as $page) {
01616             $letter = textlib::strtoupper(textlib::substr($page->title, 0, 1));
01617             if (preg_match('/[A-Z]/', $letter)) {
01618                 $stdaux->{
01619                     $letter}
01620                 [] = wiki_parser_link($page);
01621             } else {
01622                 $stdaux->{
01623                     $strspecial}
01624                 [] = wiki_parser_link($page);
01625             }
01626         }
01627 
01628         $table = new html_table();
01629         $table->head = array(get_string('pagelist', 'wiki') . $OUTPUT->help_icon('pagelist', 'wiki'));
01630         $table->attributes['class'] = 'wiki_editor generalbox';
01631         $table->align = array('center');
01632         foreach ($stdaux as $key => $elem) {
01633             $table->data[] = array($key);
01634             foreach ($elem as $e) {
01635                 $table->data[] = array(html_writer::link($e['url'], $e['content']));
01636             }
01637         }
01638         echo html_writer::table($table);
01639     }
01640 
01646     private function print_orphaned_content() {
01647         global $OUTPUT;
01648 
01649         $page = $this->page;
01650 
01651         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01652             $fresh = wiki_refresh_cachedcontent($page);
01653             $page = $fresh['page'];
01654         }
01655 
01656         $swid = $this->subwiki->id;
01657 
01658         $table = new html_table();
01659         $table->head = array(get_string('orphaned', 'wiki') . $OUTPUT->help_icon('orphaned', 'wiki'));
01660         $table->attributes['class'] = 'wiki_editor generalbox';
01661         $table->data = array();
01662         $table->rowclasses = array();
01663 
01664         if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
01665             foreach ($orphanedpages as $page) {
01666                 $link = wiki_parser_link($page->title, array('swid' => $swid));
01667                 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
01668                 $table->data[] = array('<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>');
01669             }
01670         } else {
01671             $table->data[] = array(get_string('noorphanedpages', 'wiki'));
01672         }
01673 
01674         echo html_writer::table($table);
01675     }
01676 
01683     private function print_updated_content() {
01684         global $COURSE, $OUTPUT;
01685         $page = $this->page;
01686 
01687         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
01688             $fresh = wiki_refresh_cachedcontent($page);
01689             $page = $fresh['page'];
01690         }
01691 
01692         $swid = $this->subwiki->id;
01693 
01694         $table = new html_table();
01695         $table->head = array(get_string('updatedpages', 'wiki') . $OUTPUT->help_icon('updatedpages', 'wiki'));
01696         $table->attributes['class'] = 'wiki_editor generalbox';
01697         $table->data = array();
01698         $table->rowclasses = array();
01699 
01700         if ($pages = wiki_get_updated_pages_by_subwiki($swid)) {
01701             $strdataux = '';
01702             foreach ($pages as $page) {
01703                 $user = wiki_get_user_info($page->userid);
01704                 $strdata = strftime('%d %b %Y', $page->timemodified);
01705                 if ($strdata != $strdataux) {
01706                     $table->data[] = array($OUTPUT->heading($strdata, 4));
01707                     $strdataux = $strdata;
01708                 }
01709                 $link = wiki_parser_link($page->title, array('swid' => $swid));
01710                 $class = ($link['new']) ? 'class="wiki_newentry"' : '';
01711 
01712                 $linkpage = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
01713                 $icon = $OUTPUT->user_picture($user, array($COURSE->id));
01714                 $table->data[] = array("$icon&nbsp;$linkpage");
01715             }
01716         } else {
01717             $table->data[] = array(get_string('noupdatedpages', 'wiki'));
01718         }
01719 
01720         echo html_writer::table($table);
01721     }
01722 
01723     protected function render_navigation_node($items, $attrs = array(), $expansionlimit = null, $depth = 1) {
01724 
01725         // exit if empty, we don't want an empty ul element
01726         if (count($items) == 0) {
01727             return '';
01728         }
01729 
01730         // array of nested li elements
01731         $lis = array();
01732         foreach ($items as $item) {
01733             if (!$item->display) {
01734                 continue;
01735             }
01736             $content = $item->get_content();
01737             $title = $item->get_title();
01738             if ($item->icon instanceof renderable) {
01739                 $icon = $this->wikioutput->render($item->icon);
01740                 $content = $icon . '&nbsp;' . $content; // use CSS for spacing of icons
01741                 }
01742             if ($item->helpbutton !== null) {
01743                 $content = trim($item->helpbutton) . html_writer::tag('span', $content, array('class' => 'clearhelpbutton'));
01744             }
01745 
01746             if ($content === '') {
01747                 continue;
01748             }
01749 
01750             if ($item->action instanceof action_link) {
01751                 //TODO: to be replaced with something else
01752                 $link = $item->action;
01753                 if ($item->hidden) {
01754                     $link->add_class('dimmed');
01755                 }
01756                 $content = $this->output->render($link);
01757             } else if ($item->action instanceof moodle_url) {
01758                 $attributes = array();
01759                 if ($title !== '') {
01760                     $attributes['title'] = $title;
01761                 }
01762                 if ($item->hidden) {
01763                     $attributes['class'] = 'dimmed_text';
01764                 }
01765                 $content = html_writer::link($item->action, $content, $attributes);
01766 
01767             } else if (is_string($item->action) || empty($item->action)) {
01768                 $attributes = array();
01769                 if ($title !== '') {
01770                     $attributes['title'] = $title;
01771                 }
01772                 if ($item->hidden) {
01773                     $attributes['class'] = 'dimmed_text';
01774                 }
01775                 $content = html_writer::tag('span', $content, $attributes);
01776             }
01777 
01778             // this applies to the li item which contains all child lists too
01779             $liclasses = array($item->get_css_type(), 'depth_' . $depth);
01780             if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
01781                 $liclasses[] = 'collapsed';
01782             }
01783             if ($item->isactive === true) {
01784                 $liclasses[] = 'current_branch';
01785             }
01786             $liattr = array('class' => join(' ', $liclasses));
01787             // class attribute on the div item which only contains the item content
01788             $divclasses = array('tree_item');
01789             if ((empty($expansionlimit) || $item->type != $expansionlimit) && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count() == 0 && isloggedin()))) {
01790                 $divclasses[] = 'branch';
01791             } else {
01792                 $divclasses[] = 'leaf';
01793             }
01794             if (!empty($item->classes) && count($item->classes) > 0) {
01795                 $divclasses[] = join(' ', $item->classes);
01796             }
01797             $divattr = array('class' => join(' ', $divclasses));
01798             if (!empty($item->id)) {
01799                 $divattr['id'] = $item->id;
01800             }
01801             $content = html_writer::tag('p', $content, $divattr) . $this->render_navigation_node($item->children, array(), $expansionlimit, $depth + 1);
01802             if (!empty($item->preceedwithhr) && $item->preceedwithhr === true) {
01803                 $content = html_writer::empty_tag('hr') . $content;
01804             }
01805             $content = html_writer::tag('li', $content, $liattr);
01806             $lis[] = $content;
01807         }
01808 
01809         if (count($lis)) {
01810             return html_writer::tag('ul', implode("\n", $lis), $attrs);
01811         } else {
01812             return '';
01813         }
01814     }
01815 
01816 }
01817 
01822 class page_wiki_restoreversion extends page_wiki {
01823     private $version;
01824 
01825     function print_header() {
01826         parent::print_header();
01827         $this->print_pagetitle();
01828     }
01829 
01830     function print_content() {
01831         global $CFG, $PAGE;
01832 
01833         require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
01834 
01835         $this->print_restoreversion();
01836     }
01837 
01838     function set_url() {
01839         global $PAGE, $CFG;
01840         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
01841     }
01842 
01843     function set_versionid($versionid) {
01844         $this->version = wiki_get_version($versionid);
01845     }
01846 
01847     protected function create_navbar() {
01848         global $PAGE, $CFG;
01849 
01850         parent::create_navbar();
01851         $PAGE->navbar->add(get_string('restoreversion', 'wiki'));
01852     }
01853 
01854     protected function setup_tabs() {
01855         parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history'));
01856     }
01857 
01868     private function print_restoreversion() {
01869         global $OUTPUT;
01870 
01871         $version = wiki_get_version($this->version->id);
01872 
01873         $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'versionid'=>$version->id, 'sesskey'=>sesskey());
01874         $restoreurl = new moodle_url('/mod/wiki/restoreversion.php', $optionsyes);
01875         $return = new moodle_url('/mod/wiki/viewversion.php', array('pageid'=>$this->page->id, 'versionid'=>$version->id));
01876 
01877         echo $OUTPUT->heading(get_string('restoreconfirm', 'wiki', $version->version), 2);
01878         print_container_start(false, 'wiki_restoreform');
01879         echo '<form class="wiki_restore_yes" action="' . $restoreurl . '" method="post" id="restoreversion">';
01880         echo '<div><input type="submit" name="confirm" value="' . get_string('yes') . '" /></div>';
01881         echo '</form>';
01882         echo '<form class="wiki_restore_no" action="' . $return . '" method="post">';
01883         echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
01884         echo '</form>';
01885         print_container_end();
01886     }
01887 }
01892 class page_wiki_deletecomment extends page_wiki {
01893     private $commentid;
01894 
01895     function print_header() {
01896         parent::print_header();
01897         $this->print_pagetitle();
01898     }
01899 
01900     function print_content() {
01901         $this->printconfirmdelete();
01902     }
01903 
01904     function set_url() {
01905         global $PAGE;
01906         $PAGE->set_url('/mod/wiki/instancecomments.php', array('pageid' => $this->page->id, 'commentid' => $this->commentid));
01907     }
01908 
01909     public function set_action($action, $commentid, $content) {
01910         $this->action = $action;
01911         $this->commentid = $commentid;
01912         $this->content = $content;
01913     }
01914 
01915     protected function create_navbar() {
01916         global $PAGE;
01917 
01918         parent::create_navbar();
01919         $PAGE->navbar->add(get_string('deletecommentcheck', 'wiki'));
01920     }
01921 
01922     protected function setup_tabs() {
01923         parent::setup_tabs(array('linkedwhenactive' => 'comments', 'activetab' => 'comments'));
01924     }
01925 
01934     private function printconfirmdelete() {
01935         global $OUTPUT;
01936 
01937         $strdeletecheck = get_string('deletecommentcheck', 'wiki');
01938         $strdeletecheckfull = get_string('deletecommentcheckfull', 'wiki');
01939 
01940         //ask confirmation
01941         $optionsyes = array('confirm'=>1, 'pageid'=>$this->page->id, 'action'=>'delete', 'commentid'=>$this->commentid, 'sesskey'=>sesskey());
01942         $deleteurl = new moodle_url('/mod/wiki/instancecomments.php', $optionsyes);
01943         $return = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$this->page->id));
01944 
01945         echo $OUTPUT->heading($strdeletecheckfull);
01946         print_container_start(false, 'wiki_deletecommentform');
01947         echo '<form class="wiki_deletecomment_yes" action="' . $deleteurl . '" method="post" id="deletecomment">';
01948         echo '<div><input type="submit" name="confirmdeletecomment" value="' . get_string('yes') . '" /></div>';
01949         echo '</form>';
01950         echo '<form class="wiki_deletecomment_no" action="' . $return . '" method="post">';
01951         echo '<div><input type="submit" name="norestore" value="' . get_string('no') . '" /></div>';
01952         echo '</form>';
01953         print_container_end();
01954     }
01955 }
01956 
01962 class page_wiki_save extends page_wiki_edit {
01963 
01964     private $newcontent;
01965 
01966     function print_header() {
01967     }
01968 
01969     function print_content() {
01970         global $PAGE;
01971 
01972         $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
01973         require_capability('mod/wiki:editpage', $context, NULL, true, 'noeditpermission', 'wiki');
01974 
01975         $this->print_save();
01976     }
01977 
01978     function set_newcontent($newcontent) {
01979         $this->newcontent = $newcontent;
01980     }
01981 
01982     protected function set_session_url() {
01983     }
01984 
01985     protected function print_save() {
01986         global $CFG, $USER, $OUTPUT, $PAGE;
01987 
01988         $url = $CFG->wwwroot . '/mod/wiki/edit.php?pageid=' . $this->page->id;
01989         if (!empty($this->section)) {
01990             $url .= "&section=" . urlencode($this->section);
01991         }
01992 
01993         $params = array('attachmentoptions' => page_wiki_edit::$attachmentoptions, 'format' => $this->format, 'version' => $this->versionnumber);
01994 
01995         if ($this->format != 'html') {
01996             $params['fileitemid'] = $this->page->id;
01997             $params['contextid']  = $this->modcontext->id;
01998             $params['component']  = 'mod_wiki';
01999             $params['filearea']   = 'attachments';
02000         }
02001 
02002         $form = new mod_wiki_edit_form($url, $params);
02003 
02004         $save = false;
02005         $data = false;
02006         if ($data = $form->get_data()) {
02007             if ($this->format == 'html') {
02008                 $data = file_postupdate_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id);
02009             }
02010 
02011             if (isset($this->section)) {
02012                 $save = wiki_save_section($this->page, $this->section, $data->newcontent, $USER->id);
02013             } else {
02014                 $save = wiki_save_page($this->page, $data->newcontent, $USER->id);
02015             }
02016         }
02017 
02018         if ($save && $data) {
02019             if (!empty($CFG->usetags)) {
02020                 tag_set('wiki_pages', $this->page->id, $data->tags);
02021             }
02022 
02023             $message = '<p>' . get_string('saving', 'wiki') . '</p>';
02024 
02025             if (!empty($save['sections'])) {
02026                 foreach ($save['sections'] as $s) {
02027                     $message .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
02028                 }
02029             }
02030 
02031             if ($this->versionnumber + 1 != $save['version']) {
02032                 $message .= '<p>' . get_string('wrongversionsave', 'wiki') . '</p>';
02033             }
02034 
02035             if (isset($errors) && !empty($errors)) {
02036                 foreach ($errors as $e) {
02037                     $message .= "<p>" . get_string('filenotuploadederror', 'wiki', $e->get_filename()) . "</p>";
02038                 }
02039             }
02040 
02041             //deleting old locks
02042             wiki_delete_locks($this->page->id, $USER->id, $this->section);
02043 
02044             redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id);
02045         } else {
02046             print_error('savingerror', 'wiki');
02047         }
02048     }
02049 }
02050 
02055 class page_wiki_viewversion extends page_wiki {
02056 
02057     private $version;
02058 
02059     function print_header() {
02060         parent::print_header();
02061         $this->print_pagetitle();
02062     }
02063 
02064     function print_content() {
02065         global $PAGE;
02066 
02067         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
02068 
02069         $this->print_version_view();
02070     }
02071 
02072     function set_url() {
02073         global $PAGE, $CFG;
02074         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
02075     }
02076 
02077     function set_versionid($versionid) {
02078         $this->version = wiki_get_version($versionid);
02079     }
02080 
02081     protected function create_navbar() {
02082         global $PAGE, $CFG;
02083 
02084         parent::create_navbar();
02085         $PAGE->navbar->add(get_string('history', 'wiki'), $CFG->wwwroot . '/mod/wiki/history.php?pageid' . $this->page->id);
02086         $PAGE->navbar->add(get_string('versionnum', 'wiki', $this->version->version));
02087     }
02088 
02089     protected function setup_tabs() {
02090         parent::setup_tabs(array('linkedwhenactive' => 'history', 'activetab' => 'history', 'inactivetabs' => array('edit')));
02091     }
02092 
02100     private function print_version_view() {
02101         global $CFG, $OUTPUT, $PAGE;
02102         $pageversion = wiki_get_version($this->version->id);
02103 
02104         if ($pageversion) {
02105             $restorelink = new moodle_url('/mod/wiki/restoreversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
02106             echo $OUTPUT->heading(get_string('viewversion', 'wiki', $pageversion->version) . '<br />' . html_writer::link($restorelink->out(false), '(' . get_string('restorethis', 'wiki') . ')', array('class' => 'wiki_restore')) . '&nbsp;', 4);
02107             $userinfo = wiki_get_user_info($pageversion->userid);
02108             $heading = '<p><strong>' . get_string('modified', 'wiki') . ':</strong>&nbsp;' . userdate($pageversion->timecreated, get_string('strftimedatetime', 'langconfig'));
02109             $viewlink = new moodle_url('/user/view.php', array('id' => $userinfo->id));
02110             $heading .= '&nbsp;&nbsp;&nbsp;<strong>' . get_string('user') . ':</strong>&nbsp;' . html_writer::link($viewlink->out(false), fullname($userinfo));
02111             $heading .= '&nbsp;&nbsp;&rarr;&nbsp;' . $OUTPUT->user_picture(wiki_get_user_info($pageversion->userid), array('popup' => true)) . '</p>';
02112             print_container($heading, false, 'mdl-align wiki_modifieduser wiki_headingtime');
02113             $options = array('swid' => $this->subwiki->id, 'pretty_print' => true, 'pageid' => $this->page->id);
02114 
02115             $pageversion->content = file_rewrite_pluginfile_urls($pageversion->content, 'pluginfile.php', $this->modcontext->id, 'mod_wiki', 'attachments', $this->subwiki->id);
02116 
02117             $parseroutput = wiki_parse_content($pageversion->contentformat, $pageversion->content, $options);
02118             $content = print_container(format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)), false, '', '', true);
02119             echo $OUTPUT->box($content, 'generalbox wiki_contentbox');
02120 
02121         } else {
02122             print_error('versionerror', 'wiki');
02123         }
02124     }
02125 }
02126 
02127 class page_wiki_confirmrestore extends page_wiki_save {
02128 
02129     private $version;
02130 
02131     function set_url() {
02132         global $PAGE, $CFG;
02133         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/viewversion.php', array('pageid' => $this->page->id, 'versionid' => $this->version->id));
02134     }
02135 
02136     function print_content() {
02137         global $CFG, $PAGE;
02138 
02139         require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki');
02140 
02141         $version = wiki_get_version($this->version->id);
02142         if (wiki_restore_page($this->page, $version->content, $version->userid)) {
02143             redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3);
02144         } else {
02145             print_error('restoreerror', 'wiki', $version->version);
02146         }
02147     }
02148 
02149     function set_versionid($versionid) {
02150         $this->version = wiki_get_version($versionid);
02151     }
02152 }
02153 
02154 class page_wiki_prettyview extends page_wiki {
02155 
02156     function print_header() {
02157         global $CFG, $PAGE, $OUTPUT;
02158         $PAGE->set_pagelayout('embedded');
02159         echo $OUTPUT->header();
02160 
02161         echo '<h1 id="wiki_printable_title">' . format_string($this->title) . '</h1>';
02162     }
02163 
02164     function print_content() {
02165         global $PAGE;
02166 
02167         require_capability('mod/wiki:viewpage', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
02168 
02169         $this->print_pretty_view();
02170     }
02171 
02172     function set_url() {
02173         global $PAGE, $CFG;
02174 
02175         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/prettyview.php', array('pageid' => $this->page->id));
02176     }
02177 
02178     private function print_pretty_view() {
02179         $version = wiki_get_current_version($this->page->id);
02180 
02181         $content = wiki_parse_content($version->contentformat, $version->content, array('printable' => true, 'swid' => $this->subwiki->id, 'pageid' => $this->page->id, 'pretty_print' => true));
02182 
02183         echo '<div id="wiki_printable_content">';
02184         echo format_text($content['parsed_text'], FORMAT_HTML);
02185         echo '</div>';
02186     }
02187 }
02188 
02189 class page_wiki_handlecomments extends page_wiki {
02190     private $action;
02191     private $content;
02192     private $commentid;
02193     private $format;
02194 
02195     function print_header() {
02196         $this->set_url();
02197     }
02198 
02199     public function print_content() {
02200         global $CFG, $PAGE, $USER;
02201 
02202         if ($this->action == 'add') {
02203             if (has_capability('mod/wiki:editcomment', $this->modcontext)) {
02204                 $this->add_comment($this->content, $this->commentid);
02205             }
02206         } else if ($this->action == 'edit') {
02207             $comment = wiki_get_comment($this->commentid);
02208             $edit = has_capability('mod/wiki:editcomment', $this->modcontext);
02209             $owner = ($comment->userid == $USER->id);
02210             if ($owner && $edit) {
02211                 $this->add_comment($this->content, $this->commentid);
02212             }
02213         } else if ($this->action == 'delete') {
02214             $comment = wiki_get_comment($this->commentid);
02215             $manage = has_capability('mod/wiki:managecomment', $this->modcontext);
02216             $owner = ($comment->userid == $USER->id);
02217             if ($owner || $manage) {
02218                 $this->delete_comment($this->commentid);
02219                 redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $this->page->id, get_string('deletecomment', 'wiki'), 2);
02220             }
02221         }
02222 
02223     }
02224 
02225     public function set_url() {
02226         global $PAGE, $CFG;
02227         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/comments.php', array('pageid' => $this->page->id));
02228     }
02229 
02230     public function set_action($action, $commentid, $content) {
02231         $this->action = $action;
02232         $this->commentid = $commentid;
02233         $this->content = $content;
02234 
02235         $version = wiki_get_current_version($this->page->id);
02236         $format = $version->contentformat;
02237 
02238         $this->format = $format;
02239     }
02240 
02241     private function add_comment($content, $idcomment) {
02242         global $CFG, $PAGE;
02243         require_once($CFG->dirroot . "/mod/wiki/locallib.php");
02244 
02245         $pageid = $this->page->id;
02246 
02247         wiki_add_comment($this->modcontext, $pageid, $content, $this->format);
02248 
02249         if (!$idcomment) {
02250             redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('createcomment', 'wiki'), 2);
02251         } else {
02252             $this->delete_comment($idcomment);
02253             redirect($CFG->wwwroot . '/mod/wiki/comments.php?pageid=' . $pageid, get_string('editingcomment', 'wiki'), 2);
02254         }
02255     }
02256 
02257     private function delete_comment($commentid) {
02258         global $CFG, $PAGE;
02259 
02260         $pageid = $this->page->id;
02261 
02262         wiki_delete_comment($commentid, $this->modcontext, $pageid);
02263     }
02264 
02265 }
02266 
02267 class page_wiki_lock extends page_wiki_edit {
02268 
02269     public function print_header() {
02270         $this->set_url();
02271     }
02272 
02273     protected function set_url() {
02274         global $PAGE, $CFG;
02275 
02276         $params = array('pageid' => $this->page->id);
02277 
02278         if ($this->section) {
02279             $params['section'] = $this->section;
02280         }
02281 
02282         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/lock.php', $params);
02283     }
02284 
02285     protected function set_session_url() {
02286     }
02287 
02288     public function print_content() {
02289         global $USER, $PAGE;
02290 
02291         require_capability('mod/wiki:editpage', $this->modcontext, NULL, true, 'noeditpermission', 'wiki');
02292 
02293         wiki_set_lock($this->page->id, $USER->id, $this->section);
02294     }
02295 
02296     public function print_footer() {
02297     }
02298 }
02299 
02300 class page_wiki_overridelocks extends page_wiki_edit {
02301     function print_header() {
02302         $this->set_url();
02303     }
02304 
02305     function print_content() {
02306         global $CFG, $PAGE;
02307 
02308         require_capability('mod/wiki:overridelock', $this->modcontext, NULL, true, 'nooverridelockpermission', 'wiki');
02309 
02310         wiki_delete_locks($this->page->id, null, $this->section, true, true);
02311 
02312         $args = "pageid=" . $this->page->id;
02313 
02314         if (!empty($this->section)) {
02315             $args .= "&section=" . urlencode($this->section);
02316         }
02317 
02318         redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
02319     }
02320 
02321     function set_url() {
02322         global $PAGE, $CFG;
02323 
02324         $params = array('pageid' => $this->page->id);
02325 
02326         if (!empty($this->section)) {
02327             $params['section'] = $this->section;
02328         }
02329 
02330         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/overridelocks.php', $params);
02331     }
02332 
02333     protected function set_session_url() {
02334     }
02335 
02336     private function print_overridelocks() {
02337         global $CFG;
02338 
02339         wiki_delete_locks($this->page->id, null, $this->section, true, true);
02340 
02341         $args = "pageid=" . $this->page->id;
02342 
02343         if (!empty($this->section)) {
02344             $args .= "&section=" . urlencode($this->section);
02345         }
02346 
02347         redirect($CFG->wwwroot . '/mod/wiki/edit.php?' . $args, get_string('overridinglocks', 'wiki'), 2);
02348     }
02349 
02350 }
02351 
02356 class page_wiki_admin extends page_wiki {
02357 
02358     public $view, $action;
02359     public $listorphan = false;
02360 
02369     function __construct($wiki, $subwiki, $cm) {
02370         global $PAGE;
02371         parent::__construct($wiki, $subwiki, $cm);
02372         $PAGE->requires->js_init_call('M.mod_wiki.deleteversion', null, true);
02373     }
02374 
02378     function print_header() {
02379         parent::print_header();
02380         $this->print_pagetitle();
02381     }
02382 
02386     function print_content() {
02387         //make sure anyone trying to access this page has managewiki capabilities
02388         require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'noviewpagepermission', 'wiki');
02389 
02390         //update wiki cache if timedout
02391         $page = $this->page;
02392         if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
02393             $fresh = wiki_refresh_cachedcontent($page);
02394             $page = $fresh['page'];
02395         }
02396 
02397         //dispaly admin menu
02398         echo $this->wikioutput->menu_admin($this->page->id, $this->view);
02399 
02400         //Display appropriate admin view
02401         switch ($this->view) {
02402             case 1: //delete page view
02403                 $this->print_delete_content($this->listorphan);
02404                 break;
02405             case 2: //delete version view
02406                 $this->print_delete_version();
02407                 break;
02408             default: //default is delete view
02409                 $this->print_delete_content($this->listorphan);
02410                 break;
02411         }
02412     }
02413 
02420     public function set_view($view, $listorphan = true) {
02421         $this->view = $view;
02422         $this->listorphan = $listorphan;
02423     }
02424 
02431     function set_url() {
02432         global $PAGE, $CFG;
02433         $PAGE->set_url($CFG->wwwroot . '/mod/wiki/admin.php', array('pageid' => $this->page->id));
02434     }
02435 
02441     protected function create_navbar() {
02442         global $PAGE;
02443 
02444         parent::create_navbar();
02445         $PAGE->navbar->add(get_string('admin', 'wiki'));
02446     }
02447 
02453     protected function print_delete_content($showorphan = true) {
02454         $contents = array();
02455         $table = new html_table();
02456         $table->head = array('','Page name');
02457         $table->attributes['class'] = 'generaltable mdl-align';
02458         $swid = $this->subwiki->id;
02459         if ($showorphan) {
02460             if ($orphanedpages = wiki_get_orphaned_pages($swid)) {
02461                 $this->add_page_delete_options($orphanedpages, $swid, $table);
02462             } else {
02463                 $table->data[] = array('', get_string('noorphanedpages', 'wiki'));
02464             }
02465         } else {
02466             if ($pages = wiki_get_page_list($swid)) {
02467                 $this->add_page_delete_options($pages, $swid, $table);
02468             } else {
02469                 $table->data[] = array('', get_string('nopages', 'wiki'));
02470             }
02471         }
02472 
02474         echo html_writer::start_tag('form', array(
02475                                                 'action' => new moodle_url('/mod/wiki/admin.php'),
02476                                                 'method' => 'post'));
02477         echo html_writer::tag('div', html_writer::empty_tag('input', array(
02478                                                                          'type'  => 'hidden',
02479                                                                          'name'  => 'pageid',
02480                                                                          'value' => $this->page->id)));
02481 
02482         echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
02483         echo html_writer::table($table);
02484         echo html_writer::start_tag('div', array('class' => 'mdl-align'));
02485         if (!$showorphan) {
02486             echo html_writer::empty_tag('input', array(
02487                                                      'type'    => 'submit',
02488                                                      'class'   => 'wiki_form-button',
02489                                                      'value'   => get_string('listorphan', 'wiki'),
02490                                                      'sesskey' => sesskey()));
02491         } else {
02492             echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'listall', 'value'=>'1'));
02493             echo html_writer::empty_tag('input', array(
02494                                                      'type'    => 'submit',
02495                                                      'class'   => 'wiki_form-button',
02496                                                      'value'   => get_string('listall', 'wiki'),
02497                                                      'sesskey' => sesskey()));
02498         }
02499         echo html_writer::end_tag('div');
02500         echo html_writer::end_tag('form');
02501     }
02502 
02511     protected function add_page_delete_options($pages, $swid, &$table) {
02512         global $OUTPUT;
02513         foreach ($pages as $page) {
02514             $link = wiki_parser_link($page->title, array('swid' => $swid));
02515             $class = ($link['new']) ? 'class="wiki_newentry"' : '';
02516             $pagelink = '<a href="' . $link['url'] . '"' . $class . '>' . format_string($link['content']) . '</a>';
02517             $urledit = new moodle_url('/mod/wiki/edit.php', array('pageid' => $page->id, 'sesskey' => sesskey()));
02518             $urldelete = new moodle_url('/mod/wiki/admin.php', array(
02519                                                                    'pageid'  => $this->page->id,
02520                                                                    'delete'  => $page->id,
02521                                                                    'option'  => $this->view,
02522                                                                    'listall' => !$this->listorphan?'1': '',
02523                                                                    'sesskey' => sesskey()));
02524 
02525             $editlinks = $OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit')));
02526             $editlinks .= $OUTPUT->action_icon($urldelete, new pix_icon('t/delete', get_string('delete')));
02527             $table->data[] = array($editlinks, $pagelink);
02528         }
02529     }
02530 
02536     private function print_delete_version() {
02537         global $OUTPUT;
02538         $pageid = $this->page->id;
02539 
02540         // versioncount is the latest version
02541         $versioncount = wiki_count_wiki_page_versions($pageid) - 1;
02542         $versions = wiki_get_wiki_page_versions($pageid, 0, $versioncount);
02543 
02544         // We don't want version 0 to be displayed
02545         // version 0 is blank page
02546         if (end($versions)->version == 0) {
02547             array_pop($versions);
02548         }
02549 
02550         $contents = array();
02551         $version0page = wiki_get_wiki_page_version($this->page->id, 0);
02552         $creator = wiki_get_user_info($version0page->userid);
02553         $a = new stdClass();
02554         $a->date = userdate($this->page->timecreated, get_string('strftimedaydatetime', 'langconfig'));
02555         $a->username = fullname($creator);
02556         echo $OUTPUT->heading(get_string('createddate', 'wiki', $a), 4, 'wiki_headingtime');
02557         if ($versioncount > 0) {
02559             if (count($versions) == 1) {
02560                 $row = array_shift($versions);
02561                 $username = wiki_get_user_info($row->userid);
02562                 $picture = $OUTPUT->user_picture($username);
02563                 $date = userdate($row->timecreated, get_string('strftimedate', 'langconfig'));
02564                 $time = userdate($row->timecreated, get_string('strftimetime', 'langconfig'));
02565                 $versionid = wiki_get_version($row->id);
02566                 $versionlink = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
02567                 $userlink = new moodle_url('/user/view.php', array('id' => $username->id));
02568                 $picturelink = $picture . html_writer::link($userlink->out(false), fullname($username));
02569                 $historydate = $OUTPUT->container($date, 'wiki_histdate');
02570                 $contents[] = array('', html_writer::link($versionlink->out(false), $row->version), $picturelink, $time, $historydate);
02571 
02572                 //Show current version
02573                 $table = new html_table();
02574                 $table->head = array('', get_string('version'), get_string('user'), get_string('modified'), '');
02575                 $table->data = $contents;
02576                 $table->attributes['class'] = 'mdl-align';
02577 
02578                 echo html_writer::table($table);
02579             } else {
02580                 $lastdate = '';
02581                 $rowclass = array();
02582 
02583                 foreach ($versions as $version) {
02584                     $user = wiki_get_user_info($version->userid);
02585                     $picture = $OUTPUT->user_picture($user, array('popup' => true));
02586                     $date = userdate($version->timecreated, get_string('strftimedate'));
02587                     if ($date == $lastdate) {
02588                         $date = '';
02589                         $rowclass[] = '';
02590                     } else {
02591                         $lastdate = $date;
02592                         $rowclass[] = 'wiki_histnewdate';
02593                     }
02594 
02595                     $time = userdate($version->timecreated, get_string('strftimetime', 'langconfig'));
02596                     $versionid = wiki_get_version($version->id);
02597                     if ($versionid) {
02598                         $url = new moodle_url('/mod/wiki/viewversion.php', array('pageid' => $pageid, 'versionid' => $versionid->id));
02599                         $viewlink = html_writer::link($url->out(false), $version->version);
02600                     } else {
02601                         $viewlink = $version->version;
02602                     }
02603 
02604                     $userlink = new moodle_url('/user/view.php', array('id' => $version->userid));
02605                     $picturelink = $picture . html_writer::link($userlink->out(false), fullname($user));
02606                     $historydate = $OUTPUT->container($date, 'wiki_histdate');
02607                     $radiofromelement = $this->choose_from_radio(array($version->version  => null), 'fromversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
02608                     $radiotoelement = $this->choose_from_radio(array($version->version  => null), 'toversion', 'M.mod_wiki.deleteversion()', $versioncount, true);
02609                     $contents[] = array( $radiofromelement . $radiotoelement, $viewlink, $picturelink, $time, $historydate);
02610                 }
02611 
02612                 $table = new html_table();
02613                 $table->head = array(get_string('deleteversions', 'wiki'), get_string('version'), get_string('user'), get_string('modified'), '');
02614                 $table->data = $contents;
02615                 $table->attributes['class'] = 'generaltable mdl-align';
02616                 $table->rowclasses = $rowclass;
02617 
02619                 echo html_writer::start_tag('form', array('action'=>new moodle_url('/mod/wiki/admin.php'), 'method' => 'post'));
02620                 echo html_writer::tag('div', html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pageid', 'value' => $pageid)));
02621                 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'option', 'value' => $this->view));
02622                 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' =>  sesskey()));
02623                 echo html_writer::table($table);
02624                 echo html_writer::start_tag('div', array('class' => 'mdl-align'));
02625                 echo html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'wiki_form-button', 'value' => get_string('deleteversions', 'wiki')));
02626                 echo html_writer::end_tag('div');
02627                 echo html_writer::end_tag('form');
02628             }
02629         } else {
02630             print_string('nohistory', 'wiki');
02631         }
02632     }
02633 
02646     private function choose_from_radio($options, $name, $onclick = '', $checked = '', $return = false) {
02647 
02648         static $idcounter = 0;
02649 
02650         if (!$name) {
02651             $name = 'unnamed';
02652         }
02653 
02654         $output = '<span class="radiogroup ' . $name . "\">\n";
02655 
02656         if (!empty($options)) {
02657             $currentradio = 0;
02658             foreach ($options as $value => $label) {
02659                 $htmlid = 'auto-rb' . sprintf('%04d', ++$idcounter);
02660                 $output .= ' <span class="radioelement ' . $name . ' rb' . $currentradio . "\">";
02661                 $output .= '<input name="' . $name . '" id="' . $htmlid . '" type="radio" value="' . $value . '"';
02662                 if ($value == $checked) {
02663                     $output .= ' checked="checked"';
02664                 }
02665                 if ($onclick) {
02666                     $output .= ' onclick="' . $onclick . '"';
02667                 }
02668                 if ($label === '') {
02669                     $output .= ' /> <label for="' . $htmlid . '">' . $value . '</label></span>' . "\n";
02670                 } else {
02671                     $output .= ' /> <label for="' . $htmlid . '">' . $label . '</label></span>' . "\n";
02672                 }
02673                 $currentradio = ($currentradio + 1) % 2;
02674             }
02675         }
02676 
02677         $output .= '</span>' . "\n";
02678 
02679         if ($return) {
02680             return $output;
02681         } else {
02682             echo $output;
02683         }
02684     }
02685 }
 All Data Structures Namespaces Files Functions Variables Enumerations