MDL-31901: show shorttile instead of title where applicable
[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 }
85}
86
87
88/**
89 * Data structure representing a general moodle file tree viewer
90 *
91 * @copyright 2010 Dongsheng Cai
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 * @since Moodle 2.0
94 */
95class files_tree_viewer implements renderable {
96 public $tree;
97 public $path;
e921afa8 98 public $context;
64f93798
PS
99
100 /**
101 * Constructor of moodle_file_tree_viewer class
102 * @param file_info $file_info
103 * @param array $options
104 */
105 public function __construct(file_info $file_info, array $options = null) {
106 global $CFG;
107
108 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 109 $this->options = (array)$options;
e921afa8
DC
110 $this->context = $options['context'];
111
64f93798
PS
112 $this->tree = array();
113 $children = $file_info->get_children();
0b259d91 114 $current_file_params = $file_info->get_params();
64f93798 115 $parent_info = $file_info->get_parent();
64f93798
PS
116 $level = $parent_info;
117 $this->path = array();
118 while ($level) {
119 $params = $level->get_params();
120 $context = get_context_instance_by_id($params['contextid']);
0b259d91
DC
121 // $this->context is current context
122 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
123 break;
124 }
e921afa8
DC
125 // unset unused parameters
126 unset($params['component']);
127 unset($params['filearea']);
0b259d91 128 unset($params['filename']);
e921afa8 129 unset($params['itemid']);
64f93798 130 $url = new moodle_url('/files/index.php', $params);
e921afa8 131 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
132 $level = $level->get_parent();
133 }
134 $this->path = array_reverse($this->path);
0b259d91
DC
135 if ($current_file_params['filepath'] != '/') {
136 $this->path[] = $file_info->get_visible_name();
137 }
64f93798
PS
138
139 foreach ($children as $child) {
140 $filedate = $child->get_timemodified();
141 $filesize = $child->get_filesize();
142 $mimetype = $child->get_mimetype();
143 $params = $child->get_params();
0b259d91
DC
144 unset($params['component']);
145 unset($params['filearea']);
146 unset($params['filename']);
147 unset($params['itemid']);
64f93798
PS
148 $fileitem = array(
149 'params' => $params,
150 'filename' => $child->get_visible_name(),
151 'filedate' => $filedate ? userdate($filedate) : '',
152 'filesize' => $filesize ? display_size($filesize) : ''
153 );
e921afa8 154 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
155 if ($child->is_directory()) {
156 $fileitem['isdir'] = true;
157 $fileitem['url'] = $url->out(false);
64f93798
PS
158 } else {
159 $fileitem['url'] = $child->get_url();
160 }
161 $this->tree[] = $fileitem;
162 }
163 }
164}