Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/wiki/editors/wikieditor.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
00016 
00029 require_once($CFG->dirroot.'/lib/formslib.php');
00030 require_once($CFG->dirroot.'/lib/form/textarea.php');
00031 
00032 class MoodleQuickForm_wikieditor extends MoodleQuickForm_textarea {
00033 
00034     private $files;
00035 
00036     function MoodleQuickForm_wikieditor($elementName = null, $elementLabel = null, $attributes = null) {
00037         if (isset($attributes['wiki_format'])) {
00038             $this->wikiformat = $attributes['wiki_format'];
00039             unset($attributes['wiki_format']);
00040         }
00041         if (isset($attributes['files'])) {
00042             $this->files = $attributes['files'];
00043             unset($attributes['files']);
00044         }
00045 
00046         parent::MoodleQuickForm_textarea($elementName, $elementLabel, $attributes);
00047     }
00048 
00049     function setWikiFormat($wikiformat) {
00050         $this->wikiformat = $wikiformat;
00051     }
00052 
00053     function toHtml() {
00054         $textarea = parent::toHtml();
00055 
00056         return $this->{
00057             $this->wikiformat."Editor"}
00058             ($textarea);
00059     }
00060 
00061     function creoleEditor($textarea) {
00062         return $this->printWikiEditor($textarea);
00063     }
00064 
00065     function nwikiEditor($textarea) {
00066         return $this->printWikiEditor($textarea);
00067     }
00068 
00069     private function printWikiEditor($textarea) {
00070         global $OUTPUT;
00071 
00072         $textarea = $OUTPUT->container_start().$textarea.$OUTPUT->container_end();
00073 
00074         $buttons = $this->getButtons();
00075 
00076         return $buttons.$textarea;
00077     }
00078 
00079     private function getButtons() {
00080         global $PAGE, $OUTPUT, $CFG;
00081 
00082         $editor = $this->wikiformat;
00083 
00084         $tag = $this->getTokens($editor, 'bold');
00085         $wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
00086 
00087         $tag = $this->getTokens($editor, 'italic');
00088         $wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
00089 
00090         $imagetag = $this->getTokens($editor, 'image');
00091         $wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $imagetag[0], $imagetag[1], get_string('wikiimage', 'wiki'));
00092 
00093         $tag = $this->getTokens($editor, 'link');
00094         $wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
00095 
00096         $tag = $this->getTokens($editor, 'url');
00097         $wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag, "", get_string('wikiexternalurl', 'wiki'));
00098 
00099         $tag = $this->getTokens($editor, 'list');
00100         $wiki_editor['u_list'] = array('ed_ul.gif', get_string('wikiunorderedlist', 'wiki'), '\\n'.$tag[0], '', '');
00101         $wiki_editor['o_list'] = array('ed_ol.gif', get_string('wikiorderedlist', 'wiki'), '\\n'.$tag[1], '', '');
00102 
00103         $tag = $this->getTokens($editor, 'header');
00104         $wiki_editor['h1'] = array('ed_h1.gif', get_string('wikiheader', 'wiki', 1), '\\n'.$tag.' ', ' '.$tag.'\\n', get_string('wikiheader', 'wiki', 1));
00105         $wiki_editor['h2'] = array('ed_h2.gif', get_string('wikiheader', 'wiki', 2), '\\n'.$tag.$tag.' ', ' '.$tag.$tag.'\\n', get_string('wikiheader', 'wiki', 2));
00106         $wiki_editor['h3'] = array('ed_h3.gif', get_string('wikiheader', 'wiki', 3), '\\n'.$tag.$tag.$tag.' ', ' '.$tag.$tag.$tag.'\\n', get_string('wikiheader', 'wiki', 3));
00107 
00108         $tag = $this->getTokens($editor, 'line_break');
00109         $wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n'.$tag.'\\n', '', '');
00110 
00111         $tag = $this->getTokens($editor, 'nowiki');
00112         $wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
00113 
00114         $PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
00115 
00116         $html = '<div class="wikieditor-toolbar">';
00117         foreach ($wiki_editor as $button) {
00118             $html .= "<a href=\"javascript:insertTags";
00119             $html .= "('".$button[2]."','".$button[3]."','".$button[4]."');\">";
00120             $html .= html_writer::empty_tag('img', array('alt' => $button[1], 'src' => $CFG->wwwroot . '/mod/wiki/editors/wiki/images/' . $button[0]));
00121             $html .= "</a>";
00122         }
00123         $html .= "<select onchange=\"insertTags('{$imagetag[0]}', '{$imagetag[1]}', this.value)\">";
00124         $html .= "<option value='" . s(get_string('wikiimage', 'wiki')) . "'>" . get_string('insertimage', 'wiki') . '</option>';
00125         foreach ($this->files as $filename) {
00126             $html .= "<option value='".s($filename)."'>";
00127             $html .= $filename;
00128             $html .= '</option>';
00129         }
00130         $html .= '</select>';
00131         $html .= $OUTPUT->help_icon('insertimage', 'wiki');
00132         $html .= '</div>';
00133 
00134         return $html;
00135     }
00136 
00137     private function getTokens($format, $token) {
00138         $tokens = wiki_parser_get_token($format, $token);
00139 
00140         if (is_array($tokens)) {
00141             foreach ($tokens as & $t) {
00142                 $this->escapeToken($t);
00143             }
00144         } else {
00145             $this->escapeToken($tokens);
00146         }
00147 
00148         return $tokens;
00149     }
00150 
00151     private function escapeToken(&$token) {
00152         $token = urlencode(str_replace("'", "\'", $token));
00153     }
00154 }
00155 
00156 //register wikieditor
00157 MoodleQuickForm::registerElementType('wikieditor', $CFG->dirroot."/mod/wiki/editors/wikieditor.php", 'MoodleQuickForm_wikieditor');
 All Data Structures Namespaces Files Functions Variables Enumerations