2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @package tool_filetypes
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Class containing the renderer functions for displaying file types.
30 * @package tool_filetypes
31 * @copyright 2014 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class tool_filetypes_renderer extends plugin_renderer_base {
37 * Renderer for displaying the file type edit table.
39 * @param array $filetypes An array of file type objects (from get_mimetypes_array)
40 * @param array $deleted An array of deleted file types
41 * @param bool $restricted If true, cannot be edited because set in config.php.
42 * @return string HTML code
44 public function edit_table(array $filetypes, array $deleted, $restricted) {
45 // Get combined array of all types, with deleted marker.
46 $combined = array_merge($filetypes, $deleted);
47 foreach ($deleted as $ext => $value) {
48 $combined[$ext]['deleted'] = true;
52 $out = $this->heading(get_string('pluginname', 'tool_filetypes'));
54 $out .= html_writer::div(
55 html_writer::div(get_string('configoverride', 'admin'), 'form-overridden'),
56 '', array('id' => 'adminsettings'));
58 if (count($combined) > 1) {
59 // Display the file type table if any file types exist (other than 'xxx').
60 $table = new html_table();
61 $headings = new html_table_row();
62 $headings->cells = array();
63 $headings->cells[] = new html_table_cell(get_string('extension', 'tool_filetypes'));
66 new html_table_cell(html_writer::span(get_string('edit'), 'accesshide'));
68 $headings->cells[] = new html_table_cell(get_string('source', 'tool_filetypes'));
69 $headings->cells[] = new html_table_cell(get_string('mimetype', 'tool_filetypes'));
70 $headings->cells[] = new html_table_cell(get_string('groups', 'tool_filetypes'));
71 $headings->cells[] = new html_table_cell(get_string('displaydescription', 'tool_filetypes'));
72 foreach ($headings->cells as $cell) {
75 $table->data = array($headings);
76 foreach ($combined as $extension => $filetype) {
77 if ($extension === 'xxx') {
80 $row = new html_table_row();
81 $row->cells = array();
83 // First cell has icon and extension.
84 $icon = $this->pix_icon('f/' . $filetype['icon'], '');
85 $row->cells[] = new html_table_cell($icon . ' ' . html_writer::span(s($extension)));
87 // Reset URL and button if needed.
88 $reverturl = new \moodle_url('/admin/tool/filetypes/revert.php',
89 array('extension' => $extension));
90 $revertbutton = html_writer::link($reverturl, $this->pix_icon('t/restore',
91 get_string('revert', 'tool_filetypes', s($extension))));
96 // Rest is different for deleted items.
97 if (!empty($filetype['deleted'])) {
98 // Show deleted standard types differently.
100 $row->cells[] = new html_table_cell('');
102 $source = new html_table_cell(get_string('source_deleted', 'tool_filetypes') .
103 ' ' . $revertbutton);
104 $source->attributes = array('class' => 'nonstandard');
105 $row->cells[] = $source;
107 // Other cells are blank.
108 $row->cells[] = new html_table_cell('');
109 $row->cells[] = new html_table_cell('');
110 $row->cells[] = new html_table_cell('');
111 $row->attributes = array('class' => 'deleted');
114 // Edit icons. For accessibility, the name of these links should
115 // be different for each row, so we have to include the extension.
116 $editurl = new \moodle_url('/admin/tool/filetypes/edit.php',
117 array('oldextension' => $extension));
118 $editbutton = html_writer::link($editurl, $this->pix_icon('t/edit',
119 get_string('edita', 'moodle', s($extension))));
120 $deleteurl = new \moodle_url('/admin/tool/filetypes/delete.php',
121 array('extension' => $extension));
122 $deletebutton = html_writer::link($deleteurl, $this->pix_icon('t/delete',
123 get_string('deletea', 'tool_filetypes', s($extension))));
124 $row->cells[] = new html_table_cell($editbutton . ' ' . $deletebutton);
128 $sourcestring = 'source_';
129 if (!empty($filetype['custom'])) {
130 $sourcestring .= 'custom';
131 } else if (!empty($filetype['modified'])) {
132 $sourcestring .= 'modified';
134 $sourcestring .= 'standard';
136 $source = new html_table_cell(get_string($sourcestring, 'tool_filetypes') .
137 ($sourcestring === 'source_modified' ? ' ' . $revertbutton : ''));
138 if ($sourcestring !== 'source_standard') {
139 $source->attributes = array('class' => 'nonstandard');
141 $row->cells[] = $source;
144 $mimetype = html_writer::div(s($filetype['type']), 'mimetype');
145 if (!empty($filetype['defaulticon'])) {
146 // Include the 'default for MIME type' info in the MIME type cell.
147 $mimetype .= html_writer::div(html_writer::tag('i',
148 get_string('defaulticon', 'tool_filetypes')));
150 $row->cells[] = new html_table_cell($mimetype);
153 $groups = !empty($filetype['groups']) ? implode(', ', $filetype['groups']) : '';
154 $row->cells[] = new html_table_cell(s($groups));
157 $description = get_mimetype_description(array('filename' => 'a.' . $extension));
158 // Don't show the description if it's just a copy of the MIME type,
159 // it makes the table ugly with the long duplicate text; leave blank instead.
160 if ($description === $filetype['type']) {
163 $row->cells[] = new html_table_cell($description);
166 $table->data[] = $row;
168 $out .= html_writer::table($table);
170 $out .= html_writer::tag('div', get_string('emptylist', 'tool_filetypes'));
172 // Displaying the 'Add' button.
174 $out .= $this->single_button(new moodle_url('/admin/tool/filetypes/edit.php',
175 array('name' => 'add')), get_string('addfiletypes', 'tool_filetypes'), 'get');