2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains all necessary code to define a wiki file table form element
21 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
22 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once('HTML/QuickForm/element.php');
30 require_once($CFG->dirroot
.'/lib/filelib.php');
32 class MoodleQuickForm_wikifiletable
extends HTML_QuickForm_element
{
36 private $_fileareaitemid;
38 private $_value = array();
40 public function __construct($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
42 parent
::__construct($elementName, $elementLabel, $attributes);
43 $this->_fileinfo
= $fileinfo;
44 $this->_format
= $format;
48 * Old syntax of class constructor. Deprecated in PHP7.
50 * @deprecated since Moodle 3.1
52 public function MoodleQuickForm_wikifiletable($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
53 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
54 self
::__construct($elementName, $elementLabel, $attributes, $fileinfo, $format);
57 function onQuickFormEvent($event, $arg, &$caller) {
62 $this->_contextid
= $arg[3]['contextid'];
63 $this->_filearea
= $arg[3]['filearea'];
64 $this->_fileareaitemid
= $arg[3]['itemid'];
65 $this->_format
= $arg[4];
69 return parent
::onQuickFormEvent($event, $arg, $caller);
72 function setName($name) {
73 $this->updateAttributes(array('name' => $name));
77 return $this->getAttribute('name');
80 function setValue($value) {
81 $this->_value
= $value;
91 $htmltable = new html_table();
93 $htmltable->head
= array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
95 $fs = get_file_storage();
97 $files = $fs->get_area_files($this->_fileinfo
['contextid'], 'mod_wiki', 'attachments', $this->_fileinfo
['itemid']); //TODO: verify where this is coming from, all params must be validated (skodak)
99 if (count($files) < 2) {
100 return get_string('noattachments', 'wiki');
104 foreach (array('image', 'attach', 'link') as $tag) {
105 $tags[$tag] = wiki_parser_get_token($this->_format
, $tag);
108 foreach ($files as $file) {
109 if (!$file->is_directory()) {
110 $checkbox = '<input type="checkbox" name="'.$this->_attributes
['name'].'[]" value="'.$file->get_pathnamehash().'"';
112 if (in_array($file->get_pathnamehash(), $this->_value
)) {
113 $checkbox .= ' checked="checked"';
118 $icon = file_file_icon($file);
119 $file_url = file_encode_url($CFG->wwwroot
.'/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/".$file->get_filename());
122 if(!empty($tags['attach'])) {
123 $action_icons .= "<a href=\"javascript:void(0)\" class=\"wiki-attachment-attach\" ".$this->printInsertTags($tags['attach'], $file->get_filename())." title=\"".get_string('attachmentattach', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Attach")."</a>"; //TODO: localize
126 $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-link\" ".$this->printInsertTags($tags['link'], $file_url)." title=\"".get_string('attachmentlink', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Link")."</a>";
128 if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
129 $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-image\" ".$this->printInsertTags($tags['image'], $file->get_filename())." title=\"".get_string('attachmentimage', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Image")."</a>"; //TODO: localize
132 $htmltable->data
[] = array($checkbox, '<a href="'.$file_url.'">'.$file->get_filename().'</a>', $action_icons);
136 return html_writer
::table($htmltable);
139 private function printInsertTags($tags, $value) {
140 return "onclick=\"javascript:insertTags('{$tags[0]}', '{$tags[1]}', '$value');\"";
144 //register wikieditor
145 MoodleQuickForm
::registerElementType('wikifiletable', $CFG->dirroot
."/mod/wiki/editors/wikifiletable.php", 'MoodleQuickForm_wikifiletable');