Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  defined('MOODLE_INTERNAL') || die();
  18  
  19  /**
  20   * Class for glossary display formats management.
  21   *
  22   * @package mod_glossary
  23   * @copyright 2021 Andrew Davis
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class mod_glossary_admin_setting_display_formats extends admin_setting {
  27      /**
  28       * Calls parent::__construct with specific arguments
  29       */
  30      public function __construct() {
  31          $this->nosave = true;
  32          parent::__construct('glossarydisplayformats', get_string('displayformatssetup', 'glossary'), '', '');
  33      }
  34  
  35      /**
  36       * Always returns true, does nothing
  37       *
  38       * @return true
  39       */
  40      public function get_setting() {
  41          return true;
  42      }
  43  
  44      /**
  45       * Always returns true, does nothing
  46       *
  47       * @return true
  48       */
  49      public function get_defaultsetting() {
  50          return true;
  51      }
  52  
  53      /**
  54       * Always returns '', does not write anything
  55       *
  56       * @param string $data Unused
  57       * @return string Always returns ''
  58       */
  59      public function write_setting($data) {
  60          // Do not write any setting.
  61          return '';
  62      }
  63  
  64      /**
  65       * Checks if $query is one of the available display formats
  66       *
  67       * @param string $query The string to search for
  68       * @return bool Returns true if found, false if not
  69       */
  70      public function is_related($query) {
  71          global $DB;
  72  
  73          if (parent::is_related($query)) {
  74              return true;
  75          }
  76  
  77          $query = core_text::strtolower($query);
  78          $formats = $DB->get_records("glossary_formats");
  79          foreach ($formats as $format) {
  80              if (strpos(core_text::strtolower($format->name), $query) !== false) {
  81                  return true;
  82              }
  83              $localised = get_string("displayformat$format->name", "glossary");
  84              if (strpos(core_text::strtolower($localised), $query) !== false) {
  85                  return true;
  86              }
  87          }
  88          return false;
  89      }
  90  
  91      /**
  92       * Builds the XHTML to display the control
  93       *
  94       * @param string $data Unused
  95       * @param string $query
  96       * @return string
  97       */
  98      public function output_html($data, $query='') {
  99          global $CFG, $OUTPUT, $DB;
 100  
 101          $stredit = get_string("edit");
 102          $strhide = get_string("hide");
 103          $strshow = get_string("show");
 104  
 105          $str = $OUTPUT->heading(get_string('displayformatssetup', 'glossary'), 3, 'main', true);
 106  
 107          $recformats = $DB->get_records("glossary_formats");
 108          $formats = array();
 109  
 110          // Build alphabetized list of formats.
 111          foreach ($recformats as $format) {
 112              $formats[get_string("displayformat$format->name", "glossary")] = $format;
 113          }
 114          ksort($formats);
 115  
 116          $table = new html_table();
 117          $table->align = array('left', 'center');
 118          foreach ($formats as $formatname => $format) {
 119              $editicon = html_writer::link(
 120                  new moodle_url(
 121                      '/mod/glossary/formats.php',
 122                      array('id' => $format->id, 'mode' => 'edit')
 123                  ),
 124                  $OUTPUT->pix_icon('t/edit', $stredit),
 125                  array('title' => $stredit));
 126  
 127              if ( $format->visible ) {
 128                  $vtitle = $strhide;
 129                  $vicon  = "t/hide";
 130              } else {
 131                  $vtitle = $strshow;
 132                  $vicon  = "t/show";
 133              }
 134  
 135              $visibleicon = html_writer::link(
 136                  new moodle_url(
 137                      '/mod/glossary/formats.php',
 138                      array('id' => $format->id, 'mode' => 'visible', 'sesskey' => sesskey())
 139                  ),
 140                  $OUTPUT->pix_icon($vicon, $vtitle),
 141                  array('title' => $vtitle)
 142              );
 143  
 144              $table->data[] = array(
 145                  $formatname,
 146                  $editicon . '&nbsp;&nbsp;' . $visibleicon
 147              );
 148          }
 149          $str .= html_writer::table($table);
 150  
 151          return highlight($query, $str);
 152      }
 153  }