.
/**
* This file contains all necessary code to define a wiki file table form element
*
* @package mod-wiki-2.0
* @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
* @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
*
* @author Josep Arus
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('HTML/QuickForm/element.php');
require_once($CFG->dirroot.'/lib/filelib.php');
class MoodleQuickForm_wikifiletable extends HTML_QuickForm_element {
private $_contextid;
private $_filearea;
private $_fileareaitemid;
private $_fileinfo;
private $_value = array();
function MoodleQuickForm_wikifiletable($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_fileinfo = $fileinfo;
$this->_format = $format;
}
function onQuickFormEvent($event, $arg, &$caller) {
global $OUTPUT;
switch ($event) {
case 'addElement':
$this->_contextid = $arg[3]['contextid'];
$this->_filearea = $arg[3]['filearea'];
$this->_fileareaitemid = $arg[3]['itemid'];
$this->_format = $arg[4];
break;
}
return parent::onQuickFormEvent($event, $arg, $caller);
}
function setName($name) {
$this->updateAttributes(array('name' => $name));
}
function getName() {
return $this->getAttribute('name');
}
function setValue($value) {
$this->_value = $value;
}
function getValue() {
return $this->_value;
}
function toHtml() {
global $CFG, $OUTPUT;
$htmltable = new html_table();
$htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
$fs = get_file_storage();
$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)
if (count($files) < 2) {
return get_string('noattachments', 'wiki');
}
//get tags
foreach (array('image', 'attach', 'link') as $tag) {
$tags[$tag] = wiki_parser_get_token($this->_format, $tag);
}
foreach ($files as $file) {
if (!$file->is_directory()) {
$checkbox = 'get_pathnamehash(), $this->_value)) {
$checkbox .= ' checked="checked"';
}
$checkbox .= " />";
//actions
$icon = mimeinfo_from_type('icon', $file->get_mimetype());
$file_url = file_encode_url($CFG->wwwroot.'/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/".$file->get_filename());
$action_icons = "";
if(!empty($tags['attach'])) {
$action_icons .= "printInsertTags($tags['attach'], $file->get_filename())." title=\"".get_string('attachmentattach', 'wiki')."\">pix_url('f/pdf')->out()."\" alt=\"Attach\" />"; //TODO: localize
}
$action_icons .= " printInsertTags($tags['link'], $file_url)." title=\"".get_string('attachmentlink', 'wiki')."\">pix_url('f/web')->out()."\" alt=\"Link\" />";
if ($icon == 'image') {
$action_icons .= " printInsertTags($tags['image'], $file->get_filename())." title=\"".get_string('attachmentimage', 'wiki')."\">pix_url('f/image')->out()."\" alt=\"Image\" />"; //TODO: localize
}
$htmltable->data[] = array($checkbox, ''.$file->get_filename().'', $action_icons);
}
}
return html_writer::table($htmltable);
}
private function printInsertTags($tags, $value) {
return "onclick=\"javascript:insertTags('{$tags[0]}', '{$tags[1]}', '$value');\"";
}
}
//register wikieditor
MoodleQuickForm::registerElementType('wikifiletable', $CFG->dirroot."/mod/wiki/editors/wikifiletable.php", 'MoodleQuickForm_wikifiletable');