1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This file contains all necessary code to define a wiki editor
|
|
|
19 |
*
|
|
|
20 |
* @package mod_wiki
|
|
|
21 |
* @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
|
|
|
22 |
* @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
|
|
|
23 |
*
|
|
|
24 |
* @author Josep Arus
|
|
|
25 |
*
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
require_once($CFG->dirroot.'/lib/formslib.php');
|
|
|
30 |
require_once($CFG->dirroot.'/lib/form/textarea.php');
|
|
|
31 |
require_once($CFG->dirroot.'/lib/form/templatable_form_element.php');
|
|
|
32 |
|
|
|
33 |
class MoodleQuickForm_wikieditor extends MoodleQuickForm_textarea {
|
|
|
34 |
use templatable_form_element {
|
|
|
35 |
export_for_template as export_for_template_base;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private $files;
|
|
|
39 |
|
|
|
40 |
/** @var string */
|
|
|
41 |
protected $wikiformat;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Constructor
|
|
|
45 |
*
|
|
|
46 |
* @param string $elementName (optional) name of the text field
|
|
|
47 |
* @param string $elementLabel (optional) text field label
|
|
|
48 |
* @param string $attributes (optional) Either a typical HTML attribute string or an associative array
|
|
|
49 |
*/
|
|
|
50 |
function __construct($elementName = null, $elementLabel = null, $attributes = null) {
|
|
|
51 |
if (isset($attributes['wiki_format'])) {
|
|
|
52 |
$this->wikiformat = $attributes['wiki_format'];
|
|
|
53 |
unset($attributes['wiki_format']);
|
|
|
54 |
}
|
|
|
55 |
if (isset($attributes['files'])) {
|
|
|
56 |
$this->files = $attributes['files'];
|
|
|
57 |
unset($attributes['files']);
|
|
|
58 |
}
|
|
|
59 |
parent::__construct($elementName, $elementLabel, $attributes);
|
|
|
60 |
$this->_type = 'wikieditor';
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Old syntax of class constructor. Deprecated in PHP7.
|
|
|
65 |
*
|
|
|
66 |
* @deprecated since Moodle 3.1
|
|
|
67 |
*/
|
|
|
68 |
public function MoodleQuickForm_wikieditor($elementName = null, $elementLabel = null, $attributes = null) {
|
|
|
69 |
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
|
|
|
70 |
self::__construct($elementName, $elementLabel, $attributes);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
function setWikiFormat($wikiformat) {
|
|
|
74 |
$this->wikiformat = $wikiformat;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
function toHtml() {
|
|
|
78 |
$textarea = parent::toHtml();
|
|
|
79 |
|
|
|
80 |
return $this->{
|
|
|
81 |
$this->wikiformat."Editor"}
|
|
|
82 |
($textarea);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
function creoleEditor($textarea) {
|
|
|
86 |
return $this->printWikiEditor($textarea);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
function nwikiEditor($textarea) {
|
|
|
90 |
return $this->printWikiEditor($textarea);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private function printWikiEditor($textarea) {
|
|
|
94 |
global $OUTPUT;
|
|
|
95 |
|
|
|
96 |
$textarea = $OUTPUT->container_start().$textarea.$OUTPUT->container_end();
|
|
|
97 |
|
|
|
98 |
$buttons = $this->getButtons();
|
|
|
99 |
|
|
|
100 |
return $buttons.$textarea;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private function getButtons() {
|
|
|
104 |
global $PAGE, $OUTPUT, $CFG;
|
|
|
105 |
|
|
|
106 |
$editor = $this->wikiformat;
|
|
|
107 |
|
|
|
108 |
$tag = $this->getTokens($editor, 'bold');
|
|
|
109 |
$wiki_editor['bold'] = array('ed_bold.gif', get_string('wikiboldtext', 'wiki'), $tag[0], $tag[1], get_string('wikiboldtext', 'wiki'));
|
|
|
110 |
|
|
|
111 |
$tag = $this->getTokens($editor, 'italic');
|
|
|
112 |
$wiki_editor['italic'] = array('ed_italic.gif', get_string('wikiitalictext', 'wiki'), $tag[0], $tag[1], get_string('wikiitalictext', 'wiki'));
|
|
|
113 |
|
|
|
114 |
$imagetag = $this->getTokens($editor, 'image');
|
|
|
115 |
$wiki_editor['image'] = array('ed_img.gif', get_string('wikiimage', 'wiki'), $imagetag[0], $imagetag[1], get_string('wikiimage', 'wiki'));
|
|
|
116 |
|
|
|
117 |
$tag = $this->getTokens($editor, 'link');
|
|
|
118 |
$wiki_editor['internal'] = array('ed_internal.gif', get_string('wikiinternalurl', 'wiki'), $tag[0], $tag[1], get_string('wikiinternalurl', 'wiki'));
|
|
|
119 |
|
|
|
120 |
$tag = $this->getTokens($editor, 'url');
|
|
|
121 |
$wiki_editor['external'] = array('ed_external.gif', get_string('wikiexternalurl', 'wiki'), $tag, "", get_string('wikiexternalurl', 'wiki'));
|
|
|
122 |
|
|
|
123 |
$tag = $this->getTokens($editor, 'list');
|
|
|
124 |
$wiki_editor['u_list'] = array('ed_ul.gif', get_string('wikiunorderedlist', 'wiki'), '\\n'.$tag[0], '', '');
|
|
|
125 |
$wiki_editor['o_list'] = array('ed_ol.gif', get_string('wikiorderedlist', 'wiki'), '\\n'.$tag[1], '', '');
|
|
|
126 |
|
|
|
127 |
$tag = $this->getTokens($editor, 'header');
|
|
|
128 |
$wiki_editor['h1'] = array('ed_h1.gif', get_string('wikiheader', 'wiki', 1), '\\n'.$tag.' ', ' '.$tag.'\\n', get_string('wikiheader', 'wiki', 1));
|
|
|
129 |
$wiki_editor['h2'] = array('ed_h2.gif', get_string('wikiheader', 'wiki', 2), '\\n'.$tag.$tag.' ', ' '.$tag.$tag.'\\n', get_string('wikiheader', 'wiki', 2));
|
|
|
130 |
$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));
|
|
|
131 |
|
|
|
132 |
$tag = $this->getTokens($editor, 'line_break');
|
|
|
133 |
$wiki_editor['hr'] = array('ed_hr.gif', get_string('wikihr', 'wiki'), '\\n'.$tag.'\\n', '', '');
|
|
|
134 |
|
|
|
135 |
$tag = $this->getTokens($editor, 'nowiki');
|
|
|
136 |
$wiki_editor['nowiki'] = array('ed_nowiki.gif', get_string('wikinowikitext', 'wiki'), $tag[0], $tag[1], get_string('wikinowikitext', 'wiki'));
|
|
|
137 |
|
|
|
138 |
$PAGE->requires->js('/mod/wiki/editors/wiki/buttons.js');
|
|
|
139 |
|
|
|
140 |
$html = '<div class="wikieditor-toolbar">';
|
|
|
141 |
foreach ($wiki_editor as $button) {
|
|
|
142 |
$html .= "<a href=\"javascript:insertTags";
|
|
|
143 |
$html .= "('".$button[2]."','".$button[3]."','".$button[4]."');\">";
|
|
|
144 |
$html .= html_writer::empty_tag('img', array('alt' => $button[1], 'src' => $CFG->wwwroot . '/mod/wiki/editors/wiki/images/' . $button[0]));
|
|
|
145 |
$html .= "</a>";
|
|
|
146 |
}
|
|
|
147 |
$html .= "<label class='accesshide' for='addtags'>" . get_string('insertimage', 'wiki') . "</label>";
|
|
|
148 |
$html .= "<select id='addtags' class='custom-select mx-1' " .
|
|
|
149 |
"onchange=\"insertTags('{$imagetag[0]}', '{$imagetag[1]}', this.value)\">";
|
|
|
150 |
$html .= "<option value='" . s(get_string('wikiimage', 'wiki')) . "'>" . get_string('insertimage', 'wiki') . '</option>';
|
|
|
151 |
foreach ($this->files as $filename) {
|
|
|
152 |
$html .= "<option value='".s($filename)."'>";
|
|
|
153 |
$html .= $filename;
|
|
|
154 |
$html .= '</option>';
|
|
|
155 |
}
|
|
|
156 |
$html .= '</select>';
|
|
|
157 |
$html .= $OUTPUT->help_icon('insertimage', 'wiki');
|
|
|
158 |
$html .= '</div>';
|
|
|
159 |
|
|
|
160 |
return $html;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
private function getTokens($format, $token) {
|
|
|
164 |
$tokens = wiki_parser_get_token($format, $token);
|
|
|
165 |
|
|
|
166 |
if (is_array($tokens)) {
|
|
|
167 |
foreach ($tokens as & $t) {
|
|
|
168 |
$this->escapeToken($t);
|
|
|
169 |
}
|
|
|
170 |
} else {
|
|
|
171 |
$this->escapeToken($tokens);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
return $tokens;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
private function escapeToken(&$token) {
|
|
|
178 |
$token = urlencode(str_replace("'", "\'", $token));
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public function export_for_template(renderer_base $output) {
|
|
|
182 |
$context = $this->export_for_template_base($output);
|
|
|
183 |
|
|
|
184 |
// We do want the form-control class on the output from toHTML - but we dont' want it when calling export_for_template.
|
|
|
185 |
// This is because in this type of form element the export_for_template calls toHTML to get the html for the context.
|
|
|
186 |
// If we did both we would be duplicating the form-control which messes up the styles.
|
|
|
187 |
$saved = $this->getAttribute('class');
|
|
|
188 |
$this->updateAttributes(['class' => $saved . ' form-control']);
|
|
|
189 |
|
|
|
190 |
$context['html'] = $this->toHtml();
|
|
|
191 |
$this->updateAttributes(['class' => $saved]);
|
|
|
192 |
|
|
|
193 |
return $context;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
//register wikieditor
|
|
|
198 |
MoodleQuickForm::registerElementType('wikieditor', $CFG->dirroot."/mod/wiki/editors/wikieditor.php", 'MoodleQuickForm_wikieditor');
|