source: moodle/trunk/fuentes/files/renderer.php

Last change on this file was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 52.1 KB
Line 
1<?php
2///////////////////////////////////////////////////////////////////////////
3//                                                                       //
4// This file is part of Moodle - http://moodle.org/                      //
5// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
6//                                                                       //
7// Moodle is free software: you can redistribute it and/or modify        //
8// it under the terms of the GNU General Public License as published by  //
9// the Free Software Foundation, either version 3 of the License, or     //
10// (at your option) any later version.                                   //
11//                                                                       //
12// Moodle is distributed in the hope that it will be useful,             //
13// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
15// GNU General Public License for more details.                          //
16//                                                                       //
17// You should have received a copy of the GNU General Public License     //
18// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.       //
19//                                                                       //
20///////////////////////////////////////////////////////////////////////////
21
22defined('MOODLE_INTERNAL') || die();
23
24/**
25 * Rendering of files viewer related widgets.
26 * @package   core
27 * @subpackage file
28 * @copyright 2010 Dongsheng Cai
29 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * @since     Moodle 2.0
31 */
32
33/**
34 * File browser render
35 *
36 * @copyright 2010 Dongsheng Cai
37 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since     Moodle 2.0
39 */
40class core_files_renderer extends plugin_renderer_base {
41
42    public function files_tree_viewer(file_info $file_info, array $options = null) {
43        $tree = new files_tree_viewer($file_info, $options);
44        return $this->render($tree);
45    }
46
47    public function render_files_tree_viewer(files_tree_viewer $tree) {
48        $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
49
50        $html .= $this->output->container_start('coursefilesbreadcrumb');
51        foreach($tree->path as $path) {
52            $html .= $path;
53            $html .= ' / ';
54        }
55        $html .= $this->output->container_end();
56
57        $html .= $this->output->box_start();
58        $table = new html_table();
59        $table->head = array(get_string('name'), get_string('lastmodified'), get_string('size', 'repository'), get_string('type', 'repository'));
60        $table->align = array('left', 'left', 'left', 'left');
61        $table->width = '100%';
62        $table->data = array();
63
64        foreach ($tree->tree as $file) {
65            $filedate = $filesize = $filetype = '';
66            if ($file['filedate']) {
67                $filedate = userdate($file['filedate'], get_string('strftimedatetimeshort', 'langconfig'));
68            }
69            if (empty($file['isdir'])) {
70                if ($file['filesize']) {
71                    $filesize = display_size($file['filesize']);
72                }
73                $fileicon = file_file_icon($file, 24);
74                $filetype = get_mimetype_description($file);
75            } else {
76                $fileicon = file_folder_icon(24);
77            }
78            $table->data[] = array(
79                html_writer::link($file['url'], $this->output->pix_icon($fileicon, get_string('icon')) . ' ' . $file['filename']),
80                $filedate,
81                $filesize,
82                $filetype
83                );
84        }
85
86        $html .= html_writer::table($table);
87        $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
88        $html .= $this->output->box_end();
89        return $html;
90    }
91
92    /**
93     * Prints the file manager and initializes all necessary libraries
94     *
95     * <pre>
96     * $fm = new form_filemanager($options);
97     * $output = get_renderer('core', 'files');
98     * echo $output->render($fm);
99     * </pre>
100     *
101     * @param form_filemanager $fm File manager to render
102     * @return string HTML fragment
103     */
104    public function render_form_filemanager($fm) {
105        $html = $this->fm_print_generallayout($fm);
106        $module = array(
107            'name'=>'form_filemanager',
108            'fullpath'=>'/lib/form/filemanager.js',
109            'requires' => array('moodle-core-notification-dialogue', 'core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
110            'strings' => array(
111                array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
112                array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
113                array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
114                array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
115                array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
116                array('confirmrenamefile', 'repository'), array('newfolder', 'repository'), array('edit', 'moodle')
117            )
118        );
119        if ($this->page->requires->should_create_one_time_item_now('core_file_managertemplate')) {
120            $this->page->requires->js_init_call('M.form_filemanager.set_templates',
121                    array($this->filemanager_js_templates()), true, $module);
122        }
123        $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
124
125        // non javascript file manager
126        $html .= '<noscript>';
127        $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
128        $html .= '</noscript>';
129
130
131        return $html;
132    }
133
134    /**
135     * Returns html for displaying one file manager
136     *
137     * The main element in HTML must have id="filemanager-{$client_id}" and
138     * class="filemanager fm-loading";
139     * After all necessary code on the page (both html and javascript) is loaded,
140     * the class fm-loading will be removed and added class fm-loaded;
141     * The main element (class=filemanager) will be assigned the following classes:
142     * 'fm-maxfiles' - when filemanager has maximum allowed number of files;
143     * 'fm-nofiles' - when filemanager has no files at all (although there might be folders);
144     * 'fm-noitems' - when current view (folder) has no items - neither files nor folders;
145     * 'fm-updating' - when current view is being updated (usually means that loading icon is to be displayed);
146     * 'fm-nomkdir' - when 'Make folder' action is unavailable (empty($fm->options->subdirs) == true)
147     *
148     * Element with class 'filemanager-container' will be holding evens for dnd upload (dragover, etc.).
149     * It will have class:
150     * 'dndupload-ready' - when a file is being dragged over the browser
151     * 'dndupload-over' - when file is being dragged over this filepicker (additional to 'dndupload-ready')
152     * 'dndupload-uploading' - during the upload process (note that after dnd upload process is
153     * over, the file manager will refresh the files list and therefore will have for a while class
154     * fm-updating. Both waiting processes should look similar so the images don't jump for user)
155     *
156     * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
157     * otherwise - 'dndnotsupported';
158     *
159     * Element with class 'fp-content' will be populated with files list;
160     * Element with class 'fp-btn-add' will hold onclick event for adding a file (opening filepicker);
161     * Element with class 'fp-btn-mkdir' will hold onclick event for adding new folder;
162     * Element with class 'fp-btn-download' will hold onclick event for download action;
163     *
164     * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
165     * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
166     * Parent element will receive class 'empty' when there are no folders to be displayed;
167     * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
168     *
169     * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
170     * can be changed or not;
171     * Inside element with class 'fp-viewbar' there are expected elements with classes
172     * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
173     * between the view modes, the last clicked element will have the class 'checked';
174     *
175     * @param form_filemanager $fm
176     * @return string
177     */
178    private function fm_print_generallayout($fm) {
179        global $OUTPUT;
180        $options = $fm->options;
181        $client_id = $options->client_id;
182        $straddfile  = get_string('addfile', 'repository');
183        $strmakedir  = get_string('makeafolder', 'moodle');
184        $strdownload = get_string('downloadfolder', 'repository');
185        $strloading  = get_string('loading', 'repository');
186        $strdroptoupload = get_string('droptoupload', 'moodle');
187        $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
188        $restrictions = $this->fm_print_restrictions($fm);
189        $strdndnotsupported = get_string('dndnotsupported_insentence', 'moodle').$OUTPUT->help_icon('dndnotsupported');
190        $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
191        $loading = get_string('loading', 'repository');
192        $straddfiletext = get_string('addfiletext', 'repository');
193        $strcreatefolder = get_string('createfolder', 'repository');
194        $strdownloadallfiles = get_string('downloadallfiles', 'repository');
195
196        $html = '
197<div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
198    <div class="fp-restrictions">
199        '.$restrictions.'
200        <span class="dnduploadnotsupported-message"> - '.$strdndnotsupported.' </span>
201    </div>
202    <div class="fp-navbar">
203        <div class="filemanager-toolbar">
204            <div class="fp-toolbar">
205                <div class="fp-btn-add">
206                    <a role="button" title="' . $straddfile . '" href="#">
207                        <img src="' . $this->pix_url('a/add_file') . '" alt="' . $straddfiletext . '" />
208                    </a>
209                </div>
210                <div class="fp-btn-mkdir">
211                    <a role="button" title="' . $strmakedir . '" href="#">
212                        <img src="' . $this->pix_url('a/create_folder') . '" alt="' . $strcreatefolder . '" />
213                    </a>
214                </div>
215                <div class="fp-btn-download">
216                    <a role="button" title="' . $strdownload . '" href="#">
217                        <img src="' . $this->pix_url('a/download_all').'" alt="' . $strdownloadallfiles . '" />
218                    </a>
219                </div>
220                <img class="fp-img-downloading" src="'.$this->pix_url('i/loading_small').'" alt="" />
221            </div>
222            <div class="fp-viewbar">
223                <a title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
224                    <img alt="'. get_string('displayasicons', 'repository') .'" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
225                </a>
226                <a title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
227                    <img alt="'. get_string('displayasdetails', 'repository') .'" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
228                </a>
229                <a title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
230                    <img alt="'. get_string('displayastree', 'repository') .'" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
231                </a>
232            </div>
233        </div>
234        <div class="fp-pathbar">
235            <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
236        </div>
237    </div>
238    <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
239    <div class="filemanager-container" >
240        <div class="fm-content-wrapper">
241            <div class="fp-content"></div>
242            <div class="fm-empty-container">
243                <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
244            </div>
245            <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
246            <div class="dndupload-progressbars"></div>
247            <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
248        </div>
249        <div class="filemanager-updating">'.$icon_progress.'</div>
250    </div>
251</div>';
252        return $html;
253    }
254
255    /**
256     * FileManager JS template for displaying one file in 'icon view' mode.
257     *
258     * Except for elements described in fp_js_template_iconfilename, this template may also
259     * contain element with class 'fp-contextmenu'. If context menu is available for this
260     * file, the top element will receive the additional class 'fp-hascontextmenu' and
261     * the element with class 'fp-contextmenu' will hold onclick event for displaying
262     * the context menu.
263     *
264     * @see fp_js_template_iconfilename()
265     * @return string
266     */
267    private function fm_js_template_iconfilename() {
268        $rv = '
269<div class="fp-file">
270    <a href="#">
271    <div style="position:relative;">
272        <div class="fp-thumbnail"></div>
273        <div class="fp-reficons1"></div>
274        <div class="fp-reficons2"></div>
275    </div>
276    <div class="fp-filename-field">
277        <div class="fp-filename"></div>
278    </div>
279    </a>
280    <a class="fp-contextmenu" href="#">'.$this->pix_icon('i/menu', '▶').'</a>
281</div>';
282        return $rv;
283    }
284
285    /**
286     * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
287     *
288     * Except for elements described in fp_js_template_listfilename, this template may also
289     * contain element with class 'fp-contextmenu'. If context menu is available for this
290     * file, the top element will receive the additional class 'fp-hascontextmenu' and
291     * the element with class 'fp-contextmenu' will hold onclick event for displaying
292     * the context menu.
293     *
294     * @todo MDL-32736 remove onclick="return false;"
295     * @see fp_js_template_listfilename()
296     * @return string
297     */
298    private function fm_js_template_listfilename() {
299        $rv = '
300<span class="fp-filename-icon">
301    <a href="#">
302    <span class="fp-icon"></span>
303    <span class="fp-reficons1"></span>
304    <span class="fp-reficons2"></span>
305    <span class="fp-filename"></span>
306    </a>
307    <a class="fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', '▶').'</a>
308</span>';
309        return $rv;
310    }
311
312    /**
313     * FileManager JS template for displaying 'Make new folder' dialog.
314     *
315     * Must be wrapped in an element, CSS for this element must define width and height of the window;
316     *
317     * Must have one input element with type="text" (for users to enter the new folder name);
318     *
319     * content of element with class 'fp-dlg-curpath' will be replaced with current path where
320     * new folder is about to be created;
321     * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
322     *
323     * @return string
324     */
325    private function fm_js_template_mkdir() {
326        $rv = '
327<div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
328    <div class="fp-mkdir-dlg-text">
329        <label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
330        <input type="text" />
331    </div>
332    <button class="fp-dlg-butcreate btn-primary btn">'.get_string('makeafolder').'</button>
333    <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
334</div>';
335        return $rv;
336    }
337
338    /**
339     * FileManager JS template for error/info message displayed as a separate popup window.
340     *
341     * @see fp_js_template_message()
342     * @return string
343     */
344    private function fm_js_template_message() {
345        return $this->fp_js_template_message();
346    }
347
348    /**
349     * FileManager JS template for window with file information/actions.
350     *
351     * All content must be enclosed in one element, CSS for this class must define width and
352     * height of the window;
353     *
354     * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
355     *
356     * Inside the window the elements with the following classnames must be present:
357     * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
358     * one input element (or select in case of fp-license and fp-path). They may also have labels.
359     * The elements will be assign with class 'uneditable' and input/select element will become
360     * disabled if they are not applicable for the particular file;
361     *
362     * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
363     * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
364     * information is unavailable. If there is information available, the content of embedded
365     * element with class 'fp-value' will be substituted with the value;
366     *
367     * The value of Original ('fp-original') is loaded in separate request. When it is applicable
368     * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
369     *
370     * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
371     * is applicable but not yet loaded the 'fp-original' element receives additional class
372     * 'fp-loading'. The string explaining that XX references exist will replace content of element
373     * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
374     *
375     * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
376     * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
377     * events (there may be several elements with class 'fp-file-cancel');
378     *
379     * When confirm button is pressed and file is being selected, the top element receives
380     * additional class 'loading'. It is removed when response from server is received.
381     *
382     * When any of the input fields is changed, the top element receives class 'fp-changed';
383     * When current file can be set as main - top element receives class 'fp-cansetmain';
384     * When current file is folder/zip/file - top element receives respectfully class
385     * 'fp-folder'/'fp-zip'/'fp-file';
386     *
387     * @return string
388     */
389    private function fm_js_template_fileselectlayout() {
390        global $OUTPUT;
391        $strloading  = get_string('loading', 'repository');
392        $iconprogress = $this->pix_icon('i/loading_small', $strloading).'';
393        $rv = '
394<div class="filemanager fp-select">
395    <div class="fp-select-loading">
396        <img src="'.$this->pix_url('i/loading_small').'" />
397    </div>
398    <form class="form-horizontal">
399        <button class="fp-file-download">'.get_string('download').'</button>
400        <button class="fp-file-delete">'.get_string('delete').'</button>
401        <button class="fp-file-setmain">'.get_string('setmainfile', 'repository').'</button>
402        <span class="fp-file-setmain-help">'.$OUTPUT->help_icon('setmainfile', 'repository').'</span>
403        <button class="fp-file-zip">'.get_string('zip', 'editor').'</button>
404        <button class="fp-file-unzip">'.get_string('unzip').'</button>
405        <div class="fp-hr"></div>
406
407        <div class="fp-forminset">
408                <div class="fp-saveas control-group clearfix">
409                    <label class="control-label">'.get_string('name', 'repository').'</label>
410                    <div class="controls">
411                        <input type="text"/>
412                    </div>
413                </div>
414                <div class="fp-author control-group clearfix">
415                    <label class="control-label">'.get_string('author', 'repository').'</label>
416                    <div class="controls">
417                        <input type="text"/>
418                    </div>
419                </div>
420                <div class="fp-license control-group clearfix">
421                    <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
422                    <div class="controls">
423                        <select></select>
424                    </div>
425                </div>
426                <div class="fp-path control-group clearfix">
427                    <label class="control-label">'.get_string('path', 'repository').'</label>
428                    <div class="controls">
429                        <select></select>
430                    </div>
431                </div>
432                <div class="fp-original control-group clearfix">
433                    <label class="control-label">'.get_string('original', 'repository').'</label>
434                    <div class="controls">
435                        <span class="fp-originloading">'.$iconprogress.' '.$strloading.'</span><span class="fp-value"></span>
436                    </div>
437                </div>
438                <div class="fp-reflist control-group clearfix">
439                    <label class="control-label">'.get_string('referenceslist', 'repository').'</label>
440                    <div class="controls">
441                        <p class="fp-refcount"></p>
442                        <span class="fp-reflistloading">'.$iconprogress.' '.$strloading.'</span>
443                        <ul class="fp-value"></ul>
444                    </div>
445                </div>
446        </div>
447        <div class="fp-select-buttons">
448            <button class="fp-file-update btn-primary btn">'.get_string('update', 'moodle').'</button>
449            <button class="fp-file-cancel btn-cancel btn">'.get_string('cancel').'</button>
450        </div>
451    </form>
452    <div class="fp-info clearfix">
453        <div class="fp-hr"></div>
454        <p class="fp-thumbnail"></p>
455        <div class="fp-fileinfo">
456            <div class="fp-datemodified">'.get_string('lastmodified', 'repository').' <span class="fp-value"></span></div>
457            <div class="fp-datecreated">'.get_string('datecreated', 'repository').' <span class="fp-value"></span></div>
458            <div class="fp-size">'.get_string('size', 'repository').' <span class="fp-value"></span></div>
459            <div class="fp-dimensions">'.get_string('dimensions', 'repository').' <span class="fp-value"></span></div>
460        </div>
461    </div>
462</div>';
463        return $rv;
464    }
465
466    /**
467     * FileManager JS template for popup confirm dialogue window.
468     *
469     * Must have one top element, CSS for this element must define width and height of the window;
470     *
471     * content of element with class 'fp-dlg-text' will be replaced with dialog text;
472     * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
473     * hold onclick events;
474     *
475     * @return string
476     */
477    private function fm_js_template_confirmdialog() {
478        $rv = '
479<div class="filemanager fp-dlg">
480    <div class="fp-dlg-text"></div>
481    <button class="fp-dlg-butconfirm btn-primary btn">'.get_string('ok').'</button>
482    <button class="fp-dlg-butcancel btn-cancel btn">'.get_string('cancel').'</button>
483</div>';
484        return $rv;
485    }
486
487    /**
488     * Returns all FileManager JavaScript templates as an array.
489     *
490     * @return array
491     */
492    public function filemanager_js_templates() {
493        $class_methods = get_class_methods($this);
494        $templates = array();
495        foreach ($class_methods as $method_name) {
496            if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
497            $templates[$matches[1]] = $this->$method_name();
498        }
499        return $templates;
500    }
501
502    /**
503     * Displays restrictions for the file manager
504     *
505     * @param form_filemanager $fm
506     * @return string
507     */
508    private function fm_print_restrictions($fm) {
509        $maxbytes = display_size($fm->options->maxbytes);
510        $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
511            'areasize' => display_size($fm->options->areamaxbytes));
512        $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
513        $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
514        if ($hasmaxfiles && $hasarealimit) {
515            $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
516        } else if ($hasmaxfiles) {
517            $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
518        } else if ($hasarealimit) {
519            $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
520        } else {
521            $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
522        }
523        // TODO MDL-32020 also should say about 'File types accepted'
524        return '<span>'. $maxsize . '</span>';
525    }
526
527    /**
528     * Template for FilePicker with general layout (not QuickUpload).
529     *
530     * Must have one top element containing everything else (recommended <div class="file-picker">),
531     * CSS for this element must define width and height of the filepicker window. Or CSS must
532     * define min-width, max-width, min-height and max-height and in this case the filepicker
533     * window will be resizeable;
534     *
535     * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
536     * can be changed or not;
537     * Inside element with class 'fp-viewbar' there are expected elements with classes
538     * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
539     * between the view modes, the last clicked element will have the class 'checked';
540     *
541     * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
542     * will be attached as siblings (classes first/last/even/odd will be added respectfully).
543     * The currently selected repostory will have class 'active'. Contents of element with class
544     * 'fp-repo-name' will be replaced with repository name, source of image with class
545     * 'fp-repo-icon' will be replaced with repository icon;
546     *
547     * Element with class 'fp-content' is obligatory and will hold the current contents;
548     *
549     * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
550     *
551     * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
552     * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
553     * Parent element will receive class 'empty' when there are no folders to be displayed;
554     * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
555     *
556     * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
557     * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
558     *
559     * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
560     * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
561     * class 'enabled' or 'disabled' if particular repository has this functionality.
562     * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
563     * be substituted with the search form returned by repository (in the most cases it
564     * is generated with template core_repository_renderer::repository_default_searchform);
565     * Other elements must have either <a> or <button> element inside, it will hold onclick
566     * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
567     * replaced with those specified by repository;
568     *
569     * @return string
570     */
571    private function fp_js_template_generallayout() {
572        $rv = '
573<div tabindex="0" class="file-picker fp-generallayout" role="dialog" aria-live="assertive">
574    <div class="fp-repo-area">
575        <ul class="fp-list">
576            <li class="fp-repo">
577                <a href="#"><img class="fp-repo-icon" alt=" " width="16" height="16" />&nbsp;<span class="fp-repo-name"></span></a>
578            </li>
579        </ul>
580    </div>
581    <div class="fp-repo-items" tabindex="0">
582        <div class="fp-navbar">
583            <div>
584                <div class="fp-toolbar">
585                    <div class="fp-tb-back">
586                        <a href="#">'.get_string('back', 'repository').'</a>
587                    </div>
588                    <div class="fp-tb-search">
589                        <form></form>
590                    </div>
591                    <div class="fp-tb-refresh">
592                        <a title="'. get_string('refresh', 'repository') .'" href="#">
593                            <img alt=""  src="'.$this->pix_url('a/refresh').'" />
594                        </a>
595                    </div>
596                    <div class="fp-tb-logout">
597                        <a title="'. get_string('logout', 'repository') .'" href="#">
598                            <img alt="" src="'.$this->pix_url('a/logout').'" />
599                        </a>
600                    </div>
601                    <div class="fp-tb-manage">
602                        <a title="'. get_string('settings', 'repository') .'" href="#">
603                            <img alt="" src="'.$this->pix_url('a/setting').'" />
604                        </a>
605                    </div>
606                    <div class="fp-tb-help">
607                        <a title="'. get_string('help', 'repository') .'" href="#">
608                            <img alt="" src="'.$this->pix_url('a/help').'" />
609                        </a>
610                    </div>
611                    <div class="fp-tb-message"></div>
612                </div>
613                <div class="fp-viewbar">
614                    <a role="button" title="'. get_string('displayicons', 'repository') .'" class="fp-vb-icons" href="#">
615                        <img alt="" src="'. $this->pix_url('fp/view_icon_active', 'theme') .'" />
616                    </a>
617                    <a role="button" title="'. get_string('displaydetails', 'repository') .'" class="fp-vb-details" href="#">
618                        <img alt="" src="'. $this->pix_url('fp/view_list_active', 'theme') .'" />
619                    </a>
620                    <a role="button" title="'. get_string('displaytree', 'repository') .'" class="fp-vb-tree" href="#">
621                        <img alt="" src="'. $this->pix_url('fp/view_tree_active', 'theme') .'" />
622                    </a>
623                </div>
624                <div class="fp-clear-left"></div>
625            </div>
626            <div class="fp-pathbar">
627                 <span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
628            </div>
629        </div>
630        <div class="fp-content"></div>
631    </div>
632</div>';
633        return $rv;
634    }
635
636    /**
637     * FilePicker JS template for displaying one file in 'icon view' mode.
638     *
639     * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
640     * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
641     * an element will be replaced with an appropriate img;
642     *
643     * the width of element with class 'fp-filename' will be set to the repository thumbnail width
644     * (unless min-width is set in css) and the content of an element will be replaced with filename
645     * supplied by repository;
646     *
647     * top element(s) will have class fp-folder if the element is a folder;
648     *
649     * List of files will have parent <div> element with class 'fp-iconview'
650     *
651     * @return string
652     */
653    private function fp_js_template_iconfilename() {
654        $rv = '
655<a class="fp-file" href="#" >
656    <div style="position:relative;">
657        <div class="fp-thumbnail"></div>
658        <div class="fp-reficons1"></div>
659        <div class="fp-reficons2"></div>
660    </div>
661    <div class="fp-filename-field">
662        <p class="fp-filename"></p>
663    </div>
664</a>';
665        return $rv;
666    }
667
668    /**
669     * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
670     *
671     * content of the element with class 'fp-icon' will be replaced with an appropriate img;
672     *
673     * content of element with class 'fp-filename' will be replaced with filename supplied by
674     * repository;
675     *
676     * top element(s) will have class fp-folder if the element is a folder;
677     *
678     * Note that tree view and table view are the YUI widgets and therefore there are no
679     * other templates. The widgets will be wrapped in <div> with class fp-treeview or
680     * fp-tableview (respectfully).
681     *
682     * @return string
683     */
684    private function fp_js_template_listfilename() {
685        $rv = '
686<span class="fp-filename-icon">
687    <a href="#">
688        <span class="fp-icon"></span>
689        <span class="fp-filename"></span>
690    </a>
691</span>';
692        return $rv;
693    }
694
695    /**
696     * FilePicker JS template for displaying link/loading progress for fetching of the next page
697     *
698     * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
699     *
700     * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
701     * class 'loading' during loading of the next page (it is recommended that in this case the link
702     * becomes unavailable). Also must contain one element <a> or <button> that will hold
703     * onclick event for displaying of the next page. The event will be triggered automatically
704     * when user scrolls to this link.
705     *
706     * @return string
707     */
708    private function fp_js_template_nextpage() {
709        $rv = '
710<div class="fp-nextpage">
711    <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
712    <div class="fp-nextpage-loading">
713        <img src="'.$this->pix_url('i/loading_small').'" />
714    </div>
715</div>';
716        return $rv;
717    }
718
719    /**
720     * FilePicker JS template for window appearing to select a file.
721     *
722     * All content must be enclosed in one element, CSS for this class must define width and
723     * height of the window;
724     *
725     * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
726     *
727     * Inside the window the elements with the following classnames must be present:
728     * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
729     * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
730     * fp-setlicense). They may also have labels.
731     * The elements will be assign with class 'uneditable' and input/select element will become
732     * disabled if they are not applicable for the particular file;
733     *
734     * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
735     * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
736     * if information is unavailable. If there is information available, the content of embedded
737     * element with class 'fp-value' will be substituted with the value;
738     *
739     * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
740     * onclick events;
741     *
742     * When confirm button is pressed and file is being selected, the top element receives
743     * additional class 'loading'. It is removed when response from server is received.
744     *
745     * @return string
746     */
747    private function fp_js_template_selectlayout() {
748        $rv = '
749<div class="file-picker fp-select">
750    <div class="fp-select-loading">
751        <img src="'.$this->pix_url('i/loading_small').'" />
752    </div>
753    <form class="form-horizontal">
754        <div class="fp-forminset">
755                <div class="fp-linktype-2 control-group control-radio clearfix">
756                    <label class="control-label control-radio">'.get_string('makefileinternal', 'repository').'</label>
757                    <div class="controls control-radio">
758                        <input type="radio"/>
759                    </div>
760                </div>
761                <div class="fp-linktype-1 control-group control-radio clearfix">
762                    <label class="control-label control-radio">'.get_string('makefilelink', 'repository').'</label>
763                    <div class="controls control-radio">
764                        <input type="radio"/>
765                    </div>
766                </div>
767                <div class="fp-linktype-4 control-group control-radio clearfix">
768                    <label class="control-label control-radio">'.get_string('makefilereference', 'repository').'</label>
769                    <div class="controls control-radio">
770                        <input type="radio"/>
771                    </div>
772                </div>
773                <div class="fp-saveas control-group clearfix">
774                    <label class="control-label">'.get_string('saveas', 'repository').'</label>
775                    <div class="controls">
776                        <input type="text"/>
777                    </div>
778                </div>
779                <div class="fp-setauthor control-group clearfix">
780                    <label class="control-label">'.get_string('author', 'repository').'</label>
781                    <div class="controls">
782                        <input type="text"/>
783                    </div>
784                </div>
785                <div class="fp-setlicense control-group clearfix">
786                    <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
787                    <div class="controls">
788                        <select></select>
789                    </div>
790                </div>
791        </div>
792       <div class="fp-select-buttons">
793            <button class="fp-select-confirm btn-primary btn">'.get_string('getfile', 'repository').'</button>
794            <button class="fp-select-cancel btn-cancel btn">'.get_string('cancel').'</button>
795        </div>
796    </form>
797    <div class="fp-info clearfix">
798        <div class="fp-hr"></div>
799        <p class="fp-thumbnail"></p>
800        <div class="fp-fileinfo">
801            <div class="fp-datemodified">'.get_string('lastmodified', 'repository').'<span class="fp-value"></span></div>
802            <div class="fp-datecreated">'.get_string('datecreated', 'repository').'<span class="fp-value"></span></div>
803            <div class="fp-size">'.get_string('size', 'repository').'<span class="fp-value"></span></div>
804            <div class="fp-license">'.get_string('license', 'repository').'<span class="fp-value"></span></div>
805            <div class="fp-author">'.get_string('author', 'repository').'<span class="fp-value"></span></div>
806            <div class="fp-dimensions">'.get_string('dimensions', 'repository').'<span class="fp-value"></span></div>
807        </div>
808    </div>
809</div>';
810        return $rv;
811    }
812
813    /**
814     * FilePicker JS template for 'Upload file' repository
815     *
816     * Content to display when user chooses 'Upload file' repository (will be nested inside
817     * element with class 'fp-content').
818     *
819     * Must contain form (enctype="multipart/form-data" method="POST")
820     *
821     * The elements with the following classnames must be present:
822     * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
823     * one input element (or select in case of fp-setlicense). They may also have labels.
824     *
825     * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
826     *
827     * Please note that some fields may be hidden using CSS if this is part of quickupload form
828     *
829     * @return string
830     */
831    private function fp_js_template_uploadform() {
832        $rv = '
833<div class="fp-upload-form">
834    <div class="fp-content-center">
835        <form enctype="multipart/form-data" method="POST" class="form-horizontal">
836            <div class="fp-formset">
837                <div class="fp-file control-group clearfix">
838                    <label class="control-label">'.get_string('attachment', 'repository').'</label>
839                    <div class="controls">
840                        <input type="file"/>
841                    </div>
842                </div>
843                <div class="fp-saveas control-group clearfix">
844                    <label class="control-label">'.get_string('saveas', 'repository').'</label>
845                    <div class="controls">
846                        <input type="text"/>
847                    </div>
848                </div>
849                <div class="fp-setauthor control-group clearfix">
850                    <label class="control-label">'.get_string('author', 'repository').'</label>
851                    <div class="controls">
852                        <input type="text"/>
853                    </div>
854                </div>
855                <div class="fp-setlicense control-group clearfix">
856                    <label class="control-label">'.get_string('chooselicense', 'repository').'</label>
857                    <div class="controls">
858                        <select ></select>
859                    </div>
860                </div>
861            </div>
862        </form>
863        <div class="mdl-align">
864            <button class="fp-upload-btn btn-primary btn">'.get_string('upload', 'repository').'</button>
865        </div>
866    </div>
867</div> ';
868        return $rv;
869    }
870
871    /**
872     * FilePicker JS template to display during loading process (inside element with class 'fp-content').
873     *
874     * @return string
875     */
876    private function fp_js_template_loading() {
877        return '
878<div class="fp-content-loading">
879    <div class="fp-content-center">
880        <img src="'.$this->pix_url('i/loading_small').'" />
881    </div>
882</div>';
883    }
884
885    /**
886     * FilePicker JS template for error (inside element with class 'fp-content').
887     *
888     * must have element with class 'fp-error', its content will be replaced with error text
889     * and the error code will be assigned as additional class to this element
890     * used errors: invalidjson, nofilesavailable, norepositoriesavailable
891     *
892     * @return string
893     */
894    private function fp_js_template_error() {
895        $rv = '
896<div class="fp-content-error" ><div class="fp-error"></div></div>';
897        return $rv;
898    }
899
900    /**
901     * FilePicker JS template for error/info message displayed as a separate popup window.
902     *
903     * Must be wrapped in one element, CSS for this element must define
904     * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
905     * or 'fp-msg-info' depending on message type;
906     *
907     * content of element with class 'fp-msg-text' will be replaced with error/info text;
908     *
909     * element with class 'fp-msg-butok' will hold onclick event
910     *
911     * @return string
912     */
913    private function fp_js_template_message() {
914        $rv = '
915<div class="file-picker fp-msg" role="alertdialog" aria-live="assertive" aria-labelledby="fp-msg-labelledby">
916    <p class="fp-msg-text" id="fp-msg-labelledby"></p>
917    <button class="fp-msg-butok btn-primary btn">'.get_string('ok').'</button>
918</div>';
919        return $rv;
920    }
921
922    /**
923     * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
924     *
925     * Must have one top element, CSS for this element must define width and height of the window;
926     *
927     * content of element with class 'fp-dlg-text' will be replaced with dialog text;
928     * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
929     * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
930     * hold onclick events;
931     *
932     * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
933     * (Note that it may have long text)
934     *
935     * @return string
936     */
937    private function fp_js_template_processexistingfile() {
938        $rv = '
939<div class="file-picker fp-dlg">
940    <p class="fp-dlg-text"></p>
941    <div class="fp-dlg-buttons">
942        <button class="fp-dlg-butoverwrite btn">'.get_string('overwrite', 'repository').'</button>
943        <button class="fp-dlg-butrename btn"></button>
944        <button class="fp-dlg-butcancel btn btn-cancel">'.get_string('cancel').'</button>
945    </div>
946</div>';
947        return $rv;
948    }
949
950    /**
951     * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
952     *
953     * Must have one top element, CSS for this element must define width and height of the window;
954     *
955     * content of element with class 'fp-dlg-text' will be replaced with dialog text;
956     * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
957     * hold onclick events;
958     *
959     * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
960     * (Note that it may have long text)
961     *
962     * @return string
963     */
964    private function fp_js_template_processexistingfilemultiple() {
965        $rv = '
966<div class="file-picker fp-dlg">
967    <p class="fp-dlg-text"></p>
968    <a class="fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
969    <a class="fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
970    <a class="fp-dlg-butrename fp-panel-button" href="#"></a>
971    <br/>
972    <a class="fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
973    <a class="fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
974</div>';
975        return $rv;
976    }
977
978    /**
979     * FilePicker JS template for repository login form including templates for each element type
980     *
981     * Must contain one <form> element with templates for different input types inside:
982     * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
983     * 'fp-login-input' are templates for displaying respective login form elements. Inside
984     * there must be exactly one element with type <button>, <textarea>, <select> or <input>
985     * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
986     * They may also contain the <label> element and it's content will be substituted with
987     * label;
988     *
989     * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
990     * but if they are not found, 'fp-login-input' will be used;
991     *
992     * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
993     * it should hava a template for one radio input (with class 'fp-login-radio');
994     *
995     * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
996     * will be removed if at least one popup element is present;
997     *
998     * @return string
999     */
1000    private function fp_js_template_loginform() {
1001        $rv = '
1002<div class="fp-login-form">
1003    <div class="fp-content-center">
1004        <form class="form-horizontal">
1005            <div class="fp-formset">
1006                <div class="fp-login-popup control-group clearfix">
1007                    <div class="controls fp-popup">
1008                        <button class="fp-login-popup-but btn-primary btn">'.get_string('login', 'repository').'</button>
1009                    </div>
1010                </div>
1011                <div class="fp-login-textarea control-group clearfix">
1012                    <div class="controls"><textarea></textarea></div>
1013                </div>
1014                <div class="fp-login-select control-group clearfix">
1015                    <label class="control-label"></label>
1016
1017                    <div class="controls"><select></select></div>
1018                </div>';
1019        // HACK to prevent browsers from automatically inserting the user's password into the wrong fields.
1020        $rv .= prevent_form_autofill_password();
1021        $rv .= '
1022                <div class="fp-login-input control-group clearfix">
1023                    <label class="control-label"></label>
1024                    <div class="controls"><input/></div>
1025                </div>
1026                <div class="fp-login-radiogroup control-group clearfix">
1027                    <label class="control-label"></label>
1028                    <div class="controls fp-login-radio"><input /> <label></label></div>
1029                </div>
1030            </div>
1031            <p><button class="fp-login-submit btn-primary btn">'.get_string('submit', 'repository').'</button></p>
1032        </form>
1033    </div>
1034</div>';
1035        return $rv;
1036    }
1037
1038    /**
1039     * Returns all FilePicker JavaScript templates as an array.
1040     *
1041     * @return array
1042     */
1043    public function filepicker_js_templates() {
1044        $class_methods = get_class_methods($this);
1045        $templates = array();
1046        foreach ($class_methods as $method_name) {
1047            if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
1048            $templates[$matches[1]] = $this->$method_name();
1049        }
1050        return $templates;
1051    }
1052
1053    /**
1054     * Returns HTML for default repository searchform to be passed to Filepicker
1055     *
1056     * This will be used as contents for search form defined in generallayout template
1057     * (form with id {TOOLSEARCHID}).
1058     * Default contents is one text input field with name="s"
1059     */
1060    public function repository_default_searchform() {
1061        $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
1062            'reposearch', false, array('class' => 'accesshide'));
1063        $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
1064            'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
1065        $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
1066
1067        return $str;
1068    }
1069}
1070
1071/**
1072 * Data structure representing a general moodle file tree viewer
1073 *
1074 * @copyright 2010 Dongsheng Cai
1075 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1076 * @since     Moodle 2.0
1077 */
1078class files_tree_viewer implements renderable {
1079    public $tree;
1080    public $path;
1081    public $context;
1082
1083    /**
1084     * Constructor of moodle_file_tree_viewer class
1085     * @param file_info $file_info
1086     * @param array $options
1087     */
1088    public function __construct(file_info $file_info, array $options = null) {
1089        global $CFG;
1090
1091        //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
1092        $this->options = (array)$options;
1093        $this->context = $options['context'];
1094
1095        $this->tree = array();
1096        $children = $file_info->get_children();
1097        $current_file_params = $file_info->get_params();
1098        $parent_info = $file_info->get_parent();
1099        $level = $parent_info;
1100        $this->path = array();
1101        while ($level) {
1102            $params = $level->get_params();
1103            $context = context::instance_by_id($params['contextid']);
1104            // $this->context is current context
1105            if ($context->id != $this->context->id or empty($params['filearea'])) {
1106                break;
1107            }
1108            // unset unused parameters
1109            unset($params['component']);
1110            unset($params['filearea']);
1111            unset($params['filename']);
1112            unset($params['itemid']);
1113            $url = new moodle_url('/files/index.php', $params);
1114            $this->path[] = html_writer::link($url, $level->get_visible_name());
1115            $level = $level->get_parent();
1116        }
1117        $this->path = array_reverse($this->path);
1118        if ($current_file_params['filepath'] != '/') {
1119            $this->path[] = $file_info->get_visible_name();
1120        }
1121
1122        foreach ($children as $child) {
1123            $filedate = $child->get_timemodified();
1124            $filesize = $child->get_filesize();
1125            $mimetype = $child->get_mimetype();
1126            $params = $child->get_params();
1127            unset($params['component']);
1128            unset($params['filearea']);
1129            unset($params['filename']);
1130            unset($params['itemid']);
1131            $fileitem = array(
1132                    'params'   => $params,
1133                    'filename' => $child->get_visible_name(),
1134                    'mimetype' => $child->get_mimetype(),
1135                    'filedate' => $filedate ? $filedate : '',
1136                    'filesize' => $filesize ? $filesize : ''
1137                    );
1138            $url = new moodle_url('/files/index.php', $params);
1139            if ($child->is_directory()) {
1140                $fileitem['isdir'] = true;
1141                $fileitem['url'] = $url->out(false);
1142            } else {
1143                $fileitem['url'] = $child->get_url();
1144            }
1145            $this->tree[] = $fileitem;
1146        }
1147    }
1148}
Note: See TracBrowser for help on using the repository browser.