Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 file table form element
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('HTML/QuickForm/element.php');
30
require_once($CFG->dirroot.'/lib/filelib.php');
31
 
32
class MoodleQuickForm_wikifiletable extends HTML_QuickForm_element {
33
 
34
    private $_contextid;
35
    private $_filearea;
36
    private $_fileareaitemid;
37
    private $_fileinfo;
38
    private $_value = array();
39
 
40
    /** @var string */
41
    protected $_format;
42
 
43
    public function __construct($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
44
 
45
        parent::__construct($elementName, $elementLabel, $attributes);
46
        $this->_fileinfo = $fileinfo;
47
        $this->_format = $format;
48
    }
49
 
50
    /**
51
     * Old syntax of class constructor. Deprecated in PHP7.
52
     *
53
     * @deprecated since Moodle 3.1
54
     */
55
    public function MoodleQuickForm_wikifiletable($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
56
        debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
57
        self::__construct($elementName, $elementLabel, $attributes, $fileinfo, $format);
58
    }
59
 
60
    function onQuickFormEvent($event, $arg, &$caller) {
61
        global $OUTPUT;
62
 
63
        switch ($event) {
64
            case 'addElement':
65
                $this->_contextid = $arg[3]['contextid'];
66
                $this->_filearea = $arg[3]['filearea'];
67
                $this->_fileareaitemid = $arg[3]['itemid'];
68
                $this->_format = $arg[4];
69
                break;
70
        }
71
 
72
        return parent::onQuickFormEvent($event, $arg, $caller);
73
    }
74
 
75
    function setName($name) {
76
        $this->updateAttributes(array('name' => $name));
77
    }
78
 
79
    function getName() {
80
        return $this->getAttribute('name');
81
    }
82
 
83
    function setValue($value) {
84
        $this->_value = $value;
85
    }
86
 
87
    function getValue() {
88
        return $this->_value;
89
    }
90
 
91
    function toHtml() {
92
        global $CFG, $OUTPUT;
93
 
94
        $htmltable = new html_table();
95
 
96
        $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
97
 
98
        $fs = get_file_storage();
99
 
100
        $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)
101
 
102
        if (count($files) < 2) {
103
            return get_string('noattachments', 'wiki');
104
        }
105
 
106
        //get tags
107
        foreach (array('image', 'attach', 'link') as $tag) {
108
            $tags[$tag] = wiki_parser_get_token($this->_format, $tag);
109
        }
110
 
111
        foreach ($files as $file) {
112
            if (!$file->is_directory()) {
113
                $checkbox = '<input type="checkbox" name="'.$this->_attributes['name'].'[]" value="'.$file->get_pathnamehash().'"';
114
 
115
                if (in_array($file->get_pathnamehash(), $this->_value)) {
116
                    $checkbox .= ' checked="checked"';
117
                }
118
                $checkbox .= " />";
119
 
120
                //actions
121
                $icon = file_file_icon($file);
122
                $file_url = file_encode_url($CFG->wwwroot.'/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/".$file->get_filename());
123
 
124
                $action_icons = "";
125
                if(!empty($tags['attach'])) {
126
                    $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
127
                }
128
 
129
                $action_icons .= "&nbsp;&nbsp;<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>";
130
 
131
                if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
132
                    $action_icons .= "&nbsp;&nbsp;<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
133
                }
134
 
135
                $htmltable->data[] = array($checkbox, '<a href="'.$file_url.'">'.$file->get_filename().'</a>', $action_icons);
136
            }
137
        }
138
 
139
        return html_writer::table($htmltable);
140
    }
141
 
142
    private function printInsertTags($tags, $value) {
143
        return "onclick=\"javascript:insertTags('{$tags[0]}', '{$tags[1]}', '$value');\"";
144
    }
145
}
146
 
147
//register wikieditor
148
MoodleQuickForm::registerElementType('wikifiletable', $CFG->dirroot."/mod/wiki/editors/wikifiletable.php", 'MoodleQuickForm_wikifiletable');
149
 
150