Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 403]

   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  /**
  18   * Renderer.
  19   *
  20   * @package tool_filetypes
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Class containing the renderer functions for displaying file types.
  29   *
  30   * @package tool_filetypes
  31   * @copyright 2014 The Open University
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class tool_filetypes_renderer extends plugin_renderer_base {
  35  
  36      /**
  37       * Renderer for displaying the file type edit table.
  38       *
  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
  43       */
  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;
  49          }
  50          ksort($combined);
  51  
  52          $out = $this->heading(get_string('pluginname', 'tool_filetypes'));
  53          if ($restricted) {
  54              $out .= html_writer::div(
  55                      html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info'),
  56                      '', array('id' => 'adminsettings'));
  57          }
  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'));
  64              if (!$restricted) {
  65                  $headings->cells[] =
  66                          new html_table_cell(html_writer::span(get_string('edit'), 'accesshide'));
  67              }
  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) {
  73                  $cell->header = true;
  74              }
  75              $table->data = array($headings);
  76              foreach ($combined as $extension => $filetype) {
  77                  if ($extension === 'xxx') {
  78                      continue;
  79                  }
  80                  $row = new html_table_row();
  81                  $row->cells = array();
  82  
  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)));
  86  
  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))));
  92                  if ($restricted) {
  93                      $revertbutton = '';
  94                  }
  95  
  96                  // Rest is different for deleted items.
  97                  if (!empty($filetype['deleted'])) {
  98                      // Show deleted standard types differently.
  99                      if (!$restricted) {
 100                          $row->cells[] = new html_table_cell('');
 101                      }
 102                      $source = new html_table_cell(get_string('source_deleted', 'tool_filetypes') .
 103                              ' ' . $revertbutton);
 104                      $source->attributes = array('class' => 'nonstandard');
 105                      $row->cells[] = $source;
 106  
 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');
 112                  } else {
 113                      if (!$restricted) {
 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 . '&nbsp;' . $deletebutton);
 125                      }
 126  
 127                      // Source.
 128                      $sourcestring = 'source_';
 129                      if (!empty($filetype['custom'])) {
 130                          $sourcestring .= 'custom';
 131                      } else if (!empty($filetype['modified'])) {
 132                          $sourcestring .= 'modified';
 133                      } else {
 134                          $sourcestring .= 'standard';
 135                      }
 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');
 140                      }
 141                      $row->cells[] = $source;
 142  
 143                      // MIME type.
 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')));
 149                      }
 150                      $row->cells[] = new html_table_cell($mimetype);
 151  
 152                      // Groups.
 153                      $groups = !empty($filetype['groups']) ? implode(', ', $filetype['groups']) : '';
 154                      $row->cells[] = new html_table_cell(s($groups));
 155  
 156                      // Description.
 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']) {
 161                          $description = '';
 162                      }
 163                      $row->cells[] = new html_table_cell($description);
 164                  }
 165  
 166                  $table->data[] = $row;
 167              }
 168              $out .= html_writer::table($table);
 169          } else {
 170              $out .= html_writer::tag('div', get_string('emptylist', 'tool_filetypes'));
 171          }
 172          // Displaying the 'Add' button.
 173          if (!$restricted) {
 174              $out .= $this->single_button(new moodle_url('/admin/tool/filetypes/edit.php',
 175                      array('name' => 'add')), get_string('addfiletypes', 'tool_filetypes'), 'get');
 176          }
 177          return $out;
 178      }
 179  }