renderer.php 8.4 KB

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