MDL-31901: removed accidentally commited testing code
[moodle.git] / files / renderer.php
CommitLineData
64f93798
PS
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/**
e921afa8 34 * File browser render
64f93798
PS
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) {
e0873f13 48 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
0b259d91
DC
49
50 $html .= $this->output->container_start('coursefilesbreadcrumb');
64f93798
PS
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
54 }
0b259d91 55 $html .= $this->output->container_end();
64f93798 56
0b259d91
DC
57 $html .= $this->output->box_start();
58 $table = new html_table();
276943d4
DC
59 $table->head = array(get_string('filename', 'backup'), get_string('size'), get_string('modified'));
60 $table->align = array('left', 'right', 'right');
0b259d91
DC
61 $table->width = '100%';
62 $table->data = array();
63
64 foreach ($tree->tree as $file) {
65 if (!empty($file['isdir'])) {
66 $table->data[] = array(
67 html_writer::link($file['url'], $this->output->pix_icon('f/folder', 'icon') . ' ' . $file['filename']),
0b259d91 68 '',
276943d4 69 $file['filedate'],
0b259d91
DC
70 );
71 } else {
72 $table->data[] = array(
73 html_writer::link($file['url'], $this->output->pix_icon('f/'.mimeinfo('icon', $file['filename']), get_string('icon')) . ' ' . $file['filename']),
276943d4 74 $file['filesize'],
0b259d91 75 $file['filedate'],
0b259d91 76 );
64f93798 77 }
64f93798 78 }
0b259d91
DC
79
80 $html .= html_writer::table($table);
e921afa8 81 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
0b259d91 82 $html .= $this->output->box_end();
64f93798
PS
83 return $html;
84 }
906e7d89
MG
85
86 /**
87 * Prints the file manager and initializes all necessary libraries
88 *
89 * <pre>
90 * $fm = new form_filemanager($options);
91 * $output = get_renderer('core', 'files');
92 * echo $output->render($fm);
93 * </pre>
94 *
95 * @param form_filemanager $fm File manager to render
96 * @return string HTML fragment
97 */
d1d18691 98 public function render_form_filemanager($fm) {
906e7d89 99 static $filemanagertemplateloaded;
d1d18691 100 $html = $this->fm_print_generallayout($fm);
906e7d89
MG
101 $module = array(
102 'name'=>'form_filemanager',
103 'fullpath'=>'/lib/form/filemanager.js',
104 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview', 'core_dndupload'),
105 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
106 array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
107 array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
108 array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
109 array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
110 array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
111 array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
112 array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
113 array('moving', 'repository'), array('files', 'moodle'), array('serverconnection', 'error')
114 )
115 );
116 if (empty($filemanagertemplateloaded)) {
117 $filemanagertemplateloaded = true;
118 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
e709ddd2 119 array($this->filemanager_js_templates()), true, $module);
906e7d89
MG
120 }
121 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
122
123 // non javascript file manager
124 $html .= '<noscript>';
125 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
126 $html .= '</noscript>';
127
128
129 return $html;
130 }
131
132 /**
133 * Returns html for displaying one file manager
134 *
135 * The main element in HTML must have id="filemanager-{$client_id}" and
136 * class="filemanager fm-loading";
137 * After all necessary code on the page (both html and javascript) is loaded,
138 * the class fm-loading will be removed and added class fm-loaded;
139 * The main element (class=filemanager) will be assigned the following classes:
140 * 'fm-maxfiles' - when filemanager has maximum allowed number of files;
141 * 'fm-nofiles' - when filemanager has no files at all (although there might be folders);
142 * 'fm-noitems' - when current view (folder) has no items - neither files nor folders;
143 * 'fm-updating' - when current view is being updated (usually means that loading icon is to be displayed);
144 * 'fm-nomkdir' - when 'Make folder' action is unavailable (empty($fm->options->subdirs) == true)
145 *
146 * Element with class 'filemanager-container' will be holding evens for dnd upload (dragover, etc.).
147 * It will have class:
148 * 'dndupload-ready' - when a file is being dragged over the browser
149 * 'dndupload-over' - when file is being dragged over this filepicker (additional to 'dndupload-ready')
150 * 'dndupload-uploading' - during the upload process (note that after dnd upload process is
151 * over, the file manager will refresh the files list and therefore will have for a while class
152 * fm-updating. Both waiting processes should look similar so the images don't jump for user)
153 *
154 * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
155 * otherwise - 'dndnotsupported';
156 *
e709ddd2
MG
157 * Element with class 'fp-content' will be populated with files list;
158 * Element with class 'fp-btn-add' will hold onclick event for adding a file (opening filepicker);
159 * Element with class 'fp-btn-mkdir' will hold onclick event for adding new folder;
160 * Element with class 'fp-btn-download' will hold onclick event for download action;
161 *
162 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
163 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
164 * Parent element will receive class 'empty' when there are no folders to be displayed;
165 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
166 *
167 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
168 * can be changed or not;
169 * Inside element with class 'fp-viewbar' there are expected elements with classes
170 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
171 * between the view modes, the last clicked element will have the class 'checked';
906e7d89
MG
172 *
173 * @param form_filemanager $fm
174 * @return string
175 */
d1d18691 176 private function fm_print_generallayout($fm) {
906e7d89
MG
177 global $OUTPUT;
178 $options = $fm->options;
179 $client_id = $options->client_id;
180 $straddfile = get_string('addfile', 'repository');
181 $strmakedir = get_string('makeafolder', 'moodle');
182 $strdownload = get_string('downloadfolder', 'repository');
183 $strloading = get_string('loading', 'repository');
184 $strnofilesattached = get_string('nofilesattached', 'repository');
185 $strdroptoupload = get_string('droptoupload', 'moodle');
186 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
d1d18691 187 $restrictions = $this->fm_print_restrictions($fm);
906e7d89
MG
188 $strdndenabled = get_string('dndenabled_insentence', 'moodle').$OUTPUT->help_icon('dndenabled');
189 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
190 $loading = get_string('loading', 'repository');
191
e709ddd2
MG
192 $html = '
193<div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
194 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
195 <div class="fp-pathbar">
196 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name"></a><span>/</span></span>
197 </div>
906e7d89 198 <div class="filemanager-toolbar">
e709ddd2
MG
199 <input type="button" class="{!}fp-btn-add" value="'.$straddfile.'" />
200 <input type="button" class="{!}fp-btn-mkdir" value="'.$strmakedir.'" />
201 <input type="button" class="{!}fp-btn-download" value="'.$strdownload.'" />
202 '.$restrictions.'
203 <span class="dndupload-message"> - '.$strdndenabled.' </span>
204 <div class="{!}fp-viewbar" style="float:none;">
205 <span class=""><button class="{!}fp-vb-icons">'.get_string('iconview', 'repository').'</button></span>
206 <span class=""><button class="{!}fp-vb-tree">'.get_string('listview', 'repository').'</button></span>
207 <span class=""><button class="{!}fp-vb-details">'.get_string('detailview', 'repository').'</button></span>
208 </div>
906e7d89
MG
209 </div>
210 <div class="filemanager-container" >
e709ddd2
MG
211 <div class="fm-content-wrapper">
212 <ul class="fp-content"></ul>
213 <div class="fm-empty-container mdl-align">'.$strnofilesattached.'
214 <span class="dndupload-message">'.$strdndenabledinbox.'</span>
215 </div>
216 <div class="dndupload-target">'.$strdroptoupload.'</div>
217 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
906e7d89 218 </div>
e709ddd2 219 <div class="filemanager-updating">'.$icon_progress.'</div>
906e7d89
MG
220 </div>
221</div>
222<div class="clearer"></div>
e709ddd2
MG
223';
224 return preg_replace('/\{\!\}/', '', $html);
225 }
226
227 /**
228 * FileManager JS template for displaying one file in 'icon view' mode.
229 *
230 * @see fp_js_template_iconfilename()
231 * @return string
232 */
233 private function fm_js_template_iconfilename() {
234 return $this->fp_js_template_iconfilename();
235 }
236
237 /**
238 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
239 *
240 * @see fp_js_template_listfilename()
241 * @return string
242 */
243 private function fm_js_template_listfilename() {
244 return $this->fp_js_template_listfilename();
245 }
246
247 /**
248 * FileManager JS template for window with file information/actions.
249 *
250 * All content must be enclosed in an element with class 'fp-select', CSS for this class
251 * must define width and height of the window;
252 *
253 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
254 *
255 * Inside the window the elements with the following classnames must be present:
256 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
257 * one input element (or select in case of fp-license and fp-path). They may also have labels.
258 * The elements will be assign with class 'uneditable' and input/select element will become
259 * disabled if they are not applicable for the particular file;
260 *
261 * There may be present elements with classes 'fp-origpath', 'fp-datemodified', 'fp-datecreated',
262 * 'fp-size', 'fp-dimensions'. They will receive additional class 'fp-unknown' if information
263 * is unavailable. If there is information available, the content of embedded element
264 * with class 'fp-value' will be substituted with the value;
265 *
266 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
267 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
268 * events (there may be several elements with class 'fp-file-cancel');
269 *
270 * When confirm button is pressed and file is being selected, the top element receives
271 * additional class 'loading'. It is removed when response from server is received.
272 *
273 * When any of the input fields is changed, the top element receives class 'fp-changed';
274 * When current file can be set as main - top element receives class 'fp-cansetmain';
275 * When current file is folder/zip/file - top element receives respectfully class
276 * 'fp-folder'/'fp-zip'/'fp-file';
277 *
278 * @return string
279 */
280 private function fm_js_template_fileselectlayout() {
281 $rv = '<div class="{!}fp-select">
282<div class="fp-select-loading">
283<img src="'.$this->pix_url('i/loading').'" />
284<p>'.get_string('loading', 'repository').'</p>
285</div>
286<form>
287<p class="{!}fp-thumbnail"></p>
288<table width="100%">
289<tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
290<td class="mdl-left"><input type="text"/></td></tr>
291<tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
292<td class="mdl-left"><input type="text" /></td></tr>
293<tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
294<td class="mdl-left"><select></select></td></tr>
295<tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
296<td class="mdl-left"><select></select></td></tr>
297<tr class="{!}fp-origpath"><td class="mdl-right"><label>'.get_string('originalpath', 'moodle').'</label>:</td>
298<td class="mdl-left"><span class="fp-value"/></td></tr>
299</table>
300<p><button class="{!}fp-file-update" >'.get_string('update', 'moodle').'</button>
301<button class="{!}fp-file-download" >'.get_string('download').'</button>
302<button class="{!}fp-file-delete" >'.get_string('delete').'</button>
303<button class="{!}fp-file-zip" >'.get_string('zip', 'editor').'</button>
304<button class="{!}fp-file-unzip" >'.get_string('unzip').'</button>
305<button class="{!}fp-file-setmain" >'.get_string('setmainfile', 'repository').'</button>
306<button class="{!}fp-file-cancel" >'.get_string('cancel').'</button>
307</p>
308</form>
309<div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
310<div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
311<div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
312<div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
313</div>';
314 return preg_replace('/\{\!\}/', '', $rv);
315 }
316
317 /**
318 * Returns all FileManager JavaScript templates as an array.
319 *
320 * @return array
321 */
322 public function filemanager_js_templates() {
323 $class_methods = get_class_methods($this);
324 $templates = array();
325 foreach ($class_methods as $method_name) {
326 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
327 $templates[$matches[1]] = $this->$method_name();
328 }
329 return $templates;
906e7d89
MG
330 }
331
332 /**
333 * Displays restrictions for the file manager
334 *
335 * @param form_filemanager $fm
336 * @return string
337 */
d1d18691 338 private function fm_print_restrictions($fm) {
906e7d89
MG
339 $maxbytes = display_size($fm->options->maxbytes);
340 if (empty($options->maxfiles) || $options->maxfiles == -1) {
341 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
342 //$string['maxfilesize'] = 'Maximum size for new files: {$a}';
343 } else {
344 $strparam = (object)array('size' => $maxbytes, 'attachments' => $options->maxfiles);
345 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
346 //$string['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}';
347 }
348 // TODO MDL-32020 also should say about 'File types accepted'
349 return '<span>'. $maxsize. '</span>';
350 }
64f93798 351
d1d18691
MG
352 /**
353 * Template for FilePicker with general layout (not QuickUpload).
354 *
355 * Must have one top element containing everything else (recommended <div class="file-picker">),
356 * CSS for this element must define width and height of the filepicker window. Or CSS must
357 * define min-width, max-width, min-height and max-height and in this case the filepicker
358 * window will be resizeable;
359 *
360 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
361 * can be changed or not;
362 * Inside element with class 'fp-viewbar' there are expected elements with classes
363 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
364 * between the view modes, the last clicked element will have the class 'checked';
365 *
366 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
367 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
368 * The currently selected repostory will have class 'active'. Contents of element with class
369 * 'fp-repo-name' will be replaced with repository name, source of image with class
370 * 'fp-repo-icon' will be replaced with repository icon;
371 *
372 * Element with class 'fp-content' is obligatory and will hold the current contents;
373 *
374 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
375 *
e709ddd2 376 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
d1d18691 377 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
d1d18691 378 * Parent element will receive class 'empty' when there are no folders to be displayed;
e709ddd2 379 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
d1d18691
MG
380 *
381 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
382 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
383 *
384 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
385 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
386 * class 'enabled' or 'disabled' if particular repository has this functionality.
387 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
388 * be substituted with the search form returned by repository (in the most cases it
389 * is generated with template core_repository_renderer::repository_default_searchform);
390 * Other elements must have either <a> or <button> element inside, it will hold onclick
391 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
392 * replaced with those specified by repository;
393 *
394 * @return string
395 */
396 private function fp_js_template_generallayout() {
397 $rv = '
398<div class="file-picker fp-generallayout">
e88d5641
BR
399 <div class="fp-repo-area">
400 <ul class="fp-list">
401 <li class="{!}fp-repo"><img class="{!}fp-repo-icon" width="16" height="16" />&nbsp;<a class="{!}fp-repo-name" href="#"></a></li>
402 </ul>
d1d18691 403 </div>
e88d5641
BR
404 <div class="fp-repo-items">
405 <div class="fp-navbar" >
406 <div class="{!}fp-viewbar">
407 <a class="{!}fp-vb-icons" href="#"></a>
408 <a class="{!}fp-vb-details" href="#"></a>
409 <a class="{!}fp-vb-tree" href="#"></a>
410 </div>
411 <div class="{!}fp-toolbar">
412 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
413 <div class="{!}fp-tb-search fp-search"><form/></div>
414 <div class="{!}fp-tb-refresh"><a href="#"><img src="'.$this->pix_url('a/refresh').'" /></a></div>
415 <div class="{!}fp-tb-logout"><img src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
416 <div class="{!}fp-tb-manage"><a href="#"><img src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
417 <div class="{!}fp-tb-help"><a href="#"><img src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
418 </div>
419 <div class="fp-pathbar">
420 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
421 </div>
d1d18691 422 </div>
e88d5641 423 <div class="{!}fp-content"></div>
d1d18691 424 </div>
d1d18691
MG
425</div>';
426 return preg_replace('/\{\!\}/', '', $rv);
427 }
428
429 /**
e709ddd2 430 * FilePicker JS template for displaying one file in 'icon view' mode.
d1d18691
MG
431 *
432 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
433 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
434 * an element will be replaced with an appropriate img;
435 *
436 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
437 * (unless min-width is set in css) and the content of an element will be replaced with filename
438 * supplied by repository;
439 *
e709ddd2
MG
440 * top element(s) will have class fp-folder if the element is a folder;
441 *
442 * List of files will have parent <div> element with class 'fp-iconview'
443 *
d1d18691
MG
444 * @return string
445 */
e709ddd2 446 private function fp_js_template_iconfilename() {
e88d5641 447 $rv = '<a class="{!}fp-file" href="#" >
d1d18691
MG
448 <div class="{!}fp-thumbnail"></div>
449 <div class="{!}fp-filename"></div>
e88d5641 450</a>';
d1d18691
MG
451 return preg_replace('/\{\!\}/', '', $rv);
452 }
453
454 /**
455 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
456 *
457 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
458 *
459 * content of element with class 'fp-filename' will be replaced with filename supplied by
460 * repository;
461 *
e709ddd2
MG
462 * top element(s) will have class fp-folder if the element is a folder;
463 *
d1d18691
MG
464 * Note that tree view and table view are the YUI widgets and therefore there are no
465 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
466 * fp-tableview (respectfully).
467 *
468 * @return string
469 */
470 private function fp_js_template_listfilename() {
e709ddd2 471 $rv = '<span><span class="{!}fp-icon"></span> <span class="{!}fp-filename"></span></span>';
d1d18691
MG
472 return preg_replace('/\{\!\}/', '', $rv);
473 }
474
475 /**
476 * FilePicker JS template for displaying link/loading progress for fetching of the next page
477 *
478 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
479 *
480 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
481 * class 'loading' during loading of the next page (it is recommended that in this case the link
482 * becomes unavailable). Also must contain one element <a> or <button> that will hold
483 * onclick event for displaying of the next page. The event will be triggered automatically
484 * when user scrolls to this link.
485 *
486 * @return string
487 */
488 private function fp_js_template_nextpage() {
489 $rv = '<div class="{!}fp-nextpage">
490 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
491 <div class="fp-nextpage-loading">
492 <img src="'.$this->pix_url('i/loading').'" />
493 <p>'.get_string('loading', 'repository').'</p>
494 </div>
495</div>';
496 return preg_replace('/\{\!\}/', '', $rv);
497 }
498
499 /**
e709ddd2 500 * FilePicker JS template for window appearing to select a file.
d1d18691
MG
501 *
502 * All content must be enclosed in an element with class 'fp-select', CSS for this class
503 * must define width and height of the window;
504 *
505 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
506 *
507 * Inside the window the elements with the following classnames must be present:
508 * 'fp-saveas', 'fp-linkexternal', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
509 * one input element (or select in case of fp-setlicense). They may also have labels.
510 * The elements will be assign with class 'uneditable' and input/select element will become
511 * disabled if they are not applicable for the particular file;
512 *
513 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
e709ddd2
MG
514 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
515 * if information is unavailable. If there is information available, the content of embedded
516 * element with class 'fp-value' will be substituted with the value;
d1d18691
MG
517 *
518 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
519 * onclick events;
520 *
521 * When confirm button is pressed and file is being selected, the top element receives
522 * additional class 'loading'. It is removed when response from server is received.
523 *
524 * @return string
525 */
526 private function fp_js_template_selectlayout() {
527 $rv = '<div class="{!}fp-select">
528<div class="fp-select-loading">
529<img src="'.$this->pix_url('i/loading').'" />
530<p>'.get_string('loading', 'repository').'</p>
531</div>
532<form>
e88d5641
BR
533<p><a class="{!}fp-select-confirm" href="#">'.get_string('getfile', 'repository').'</a>
534<a class="{!}fp-select-cancel" href="#">'.get_string('cancel').'</a></p>
535<p class="fp-hr">&nbsp;</p>
d1d18691
MG
536<table width="100%">
537<tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
538<td class="mdl-left"><input type="text"/></td></tr>
539<tr class="{!}fp-linkexternal"><td></td>
540<td class="mdl-left"><input type="checkbox"/><label>'.get_string('linkexternal', 'repository').'</label></td></tr>
541<tr class="{!}fp-setauthor"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
542<td class="mdl-left"><input type="text" /></td></tr>
543<tr class="{!}fp-setlicense"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
544<td class="mdl-left"><select></select></td></tr>
545</table>
d1d18691 546</form>
e88d5641
BR
547<p class="{!}fp-thumbnail"></p>
548<div class="fp-fileinfo">
d1d18691
MG
549<div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
550<div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
551<div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
552<div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"/></div>
553<div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"/></div>
554<div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
e88d5641 555</div>
d1d18691
MG
556</div>';
557 return preg_replace('/\{\!\}/', '', $rv);
558 }
559
560 /**
561 * FilePicker JS template for 'Upload file' repository
562 *
563 * Content to display when user chooses 'Upload file' repository (will be nested inside
564 * element with class 'fp-content').
565 *
566 * Must contain form (enctype="multipart/form-data" method="POST")
567 *
568 * The elements with the following classnames must be present:
569 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
570 * one input element (or select in case of fp-setlicense). They may also have labels.
571 *
572 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
573 *
574 * Please note that some fields may be hidden using CSS if this is part of quickupload form
575 *
576 * @return string
577 */
578 private function fp_js_template_uploadform() {
579 $rv = '<div class="fp-upload-form mdl-align">
e88d5641 580<div class="fp-content-center">
d1d18691 581<form enctype="multipart/form-data" method="POST">
e88d5641 582 <table >
d1d18691
MG
583 <tr class="{!}fp-file">
584 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
585 <td class="mdl-left"><input type="file"/></td>
586 </tr>
587 <tr class="{!}fp-saveas">
588 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
589 <td class="mdl-left"><input type="text"/></td>
590 </tr>
591 <tr class="{!}fp-setauthor">
592 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
593 <td class="mdl-left"><input type="text"/></td>
594 </tr>
595 <tr class="{!}fp-setlicense">
596 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
597 <td class="mdl-left"><select/></td>
598 </tr>
599 </table>
600</form>
601<div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
e88d5641 602</div>
d1d18691
MG
603</div> ';
604 return preg_replace('/\{\!\}/', '', $rv);
605 }
606
607 /**
608 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
609 *
610 * @return string
611 */
612 private function fp_js_template_loading() {
613 return '<div style="text-align:center">
614<img src="'.$this->pix_url('i/loading').'" />
615<p>'.get_string('loading', 'repository').'</p>
616</div>';
617 }
618
619 /**
620 * FilePicker JS template for error (inside element with class 'fp-content').
621 *
622 * must have element with class 'fp-error', its content will be replaced with error text
623 * and the error code will be assigned as additional class to this element
624 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
625 *
626 * @return string
627 */
628 private function fp_js_template_error() {
e88d5641 629 $rv = '<div class="fp-content-error" ><div class="{!}fp-error" /></div>';
d1d18691
MG
630 return preg_replace('/\{\!\}/', '', $rv);
631 }
632
633 /**
634 * FilePicker JS template for error/info message displayed as a separate popup window.
635 *
636 * Must be wrapped in an element with class 'fp-msg', CSS for this element must define
637 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
638 * or 'fp-msg-info' depending on message type;
639 *
640 * content of element with class 'fp-msg-text' will be replaced with error/info text;
641 *
642 * element with class 'fp-msg-butok' will hold onclick event
643 *
644 * @return string
645 */
646 private function fp_js_template_message() {
647 $rv = '<div class="{!}fp-msg">
648 <div class="{!}fp-msg-text"></div>
649 <div><button class="{!}fp-msg-butok">'.get_string('ok').'</button></div>
650 </div>';
651 return preg_replace('/\{\!\}/', '', $rv);
652 }
653
654 /**
655 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
656 *
657 * Must be wrapped in an element with class 'fp-dlg', CSS for this element must define width
658 * and height of the window;
659 *
660 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
661 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
662 * hold onclick events;
663 *
664 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
665 * (Note that it may have long text)
666 *
667 * @return string
668 */
669 private function fp_js_template_processexistingfile() {
670 $rv = '<div class="{!}fp-dlg"><div class="{!}fp-dlg-text"></div>
671<div class="fp-dlg-but"><button class="{!}fp-dlg-butoverwrite" >'.get_string('overwrite', 'repository').'</button></div>
672<div class="fp-dlg-but"><button class="{!}fp-dlg-butrename" /></div>
673<div class="fp-dlg-but"><button class="{!}fp-dlg-butcancel" >'.get_string('cancel').'</button></div>
674</div>';
675 return preg_replace('/\{\!\}/', '', $rv);
676 }
677
678 /**
679 * FilePicker JS template for repository login form including templates for each element type
680 *
681 * Must contain one <form> element with templates for different input types inside:
682 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
683 * 'fp-login-input' are templates for displaying respective login form elements. Inside
684 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
685 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
686 * They may also contain the <label> element and it's content will be substituted with
687 * label;
688 *
689 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
690 * but if they are not found, 'fp-login-input' will be used;
691 *
692 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
693 * it should hava a template for one radio input (with class 'fp-login-radio');
694 *
695 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
696 * will be removed if at least one popup element is present;
697 *
698 * @return string
699 */
700 private function fp_js_template_loginform() {
701 $rv = '
702<div class="fp-login-form">
e88d5641 703<div class="fp-content-center">
d1d18691 704 <form>
e88d5641 705 <table >
d1d18691
MG
706 <tr class="{!}fp-login-popup">
707 <td colspan="2">
708 <label>'.get_string('popup', 'repository').'</label>
709 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p>
710 </td>
711 </tr>
712 <tr class="{!}fp-login-textarea">
713 <td colspan="2"><p><textarea></textarea></p></td>
714 </tr>
715 <tr class="{!}fp-login-select">
716 <td align="right"><label></label></td>
717 <td align="left"><select></select></td>
718 </tr>
719 <tr class="{!}fp-login-input">
e88d5641
BR
720 <td class="label"><label /></td>
721 <td class="input"><input/></td>
d1d18691
MG
722 </tr>
723 <tr class="{!}fp-login-radiogroup">
724 <td align="right" width="30%" valign="top"><label /></td>
725 <td align="left" valign="top">
726 <p class="{!}fp-login-radio"><input /> <label /></p>
727 </td>
728 </tr>
729 </table>
730 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
731 </form>
e88d5641 732</div>
d1d18691
MG
733</div>';
734 return preg_replace('/\{\!\}/', '', $rv);
735 }
736
737 /**
738 * Returns all FilePicker JavaScript templates as an array.
739 *
740 * @return array
741 */
742 public function filepicker_js_templates() {
743 $class_methods = get_class_methods($this);
744 $templates = array();
745 foreach ($class_methods as $method_name) {
746 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
747 $templates[$matches[1]] = $this->$method_name();
748 }
749 return $templates;
750 }
751
752 /**
753 * Returns HTML for default repository searchform to be passed to Filepicker
754 *
755 * This will be used as contents for search form defined in generallayout template
756 * (form with id {TOOLSEARCHID}).
757 * Default contents is one text input field with name="s"
758 */
759 public function repository_default_searchform() {
e88d5641 760 $str = '<input class="search-entry" name="s" value="Search" />';
d1d18691
MG
761 return $str;
762 }
763}
64f93798
PS
764
765/**
766 * Data structure representing a general moodle file tree viewer
767 *
768 * @copyright 2010 Dongsheng Cai
769 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
770 * @since Moodle 2.0
771 */
772class files_tree_viewer implements renderable {
773 public $tree;
774 public $path;
e921afa8 775 public $context;
64f93798
PS
776
777 /**
778 * Constructor of moodle_file_tree_viewer class
779 * @param file_info $file_info
780 * @param array $options
781 */
782 public function __construct(file_info $file_info, array $options = null) {
783 global $CFG;
784
785 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 786 $this->options = (array)$options;
e921afa8
DC
787 $this->context = $options['context'];
788
64f93798
PS
789 $this->tree = array();
790 $children = $file_info->get_children();
0b259d91 791 $current_file_params = $file_info->get_params();
64f93798 792 $parent_info = $file_info->get_parent();
64f93798
PS
793 $level = $parent_info;
794 $this->path = array();
795 while ($level) {
796 $params = $level->get_params();
797 $context = get_context_instance_by_id($params['contextid']);
0b259d91
DC
798 // $this->context is current context
799 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
800 break;
801 }
e921afa8
DC
802 // unset unused parameters
803 unset($params['component']);
804 unset($params['filearea']);
0b259d91 805 unset($params['filename']);
e921afa8 806 unset($params['itemid']);
64f93798 807 $url = new moodle_url('/files/index.php', $params);
e921afa8 808 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
809 $level = $level->get_parent();
810 }
811 $this->path = array_reverse($this->path);
0b259d91
DC
812 if ($current_file_params['filepath'] != '/') {
813 $this->path[] = $file_info->get_visible_name();
814 }
64f93798
PS
815
816 foreach ($children as $child) {
817 $filedate = $child->get_timemodified();
818 $filesize = $child->get_filesize();
819 $mimetype = $child->get_mimetype();
820 $params = $child->get_params();
0b259d91
DC
821 unset($params['component']);
822 unset($params['filearea']);
823 unset($params['filename']);
824 unset($params['itemid']);
64f93798
PS
825 $fileitem = array(
826 'params' => $params,
827 'filename' => $child->get_visible_name(),
828 'filedate' => $filedate ? userdate($filedate) : '',
829 'filesize' => $filesize ? display_size($filesize) : ''
830 );
e921afa8 831 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
832 if ($child->is_directory()) {
833 $fileitem['isdir'] = true;
834 $fileitem['url'] = $url->out(false);
64f93798
PS
835 } else {
836 $fileitem['url'] = $child->get_url();
837 }
838 $this->tree[] = $fileitem;
839 }
840 }
841}