Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  ///////////////////////////////////////////////////////////////////////////
   3  //                                                                       //
   4  // NOTICE OF COPYRIGHT                                                   //
   5  //                                                                       //
   6  // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
   7  //          http://moodle.org                                            //
   8  //                                                                       //
   9  // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
  10  //                                                                       //
  11  // This program is free software; you can redistribute it and/or modify  //
  12  // it under the terms of the GNU General Public License as published by  //
  13  // the Free Software Foundation; either version 2 of the License, or     //
  14  // (at your option) any later version.                                   //
  15  //                                                                       //
  16  // This program is distributed in the hope that it will be useful,       //
  17  // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
  18  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
  19  // GNU General Public License for more details:                          //
  20  //                                                                       //
  21  //          http://www.gnu.org/copyleft/gpl.html                         //
  22  //                                                                       //
  23  ///////////////////////////////////////////////////////////////////////////
  24  
  25  class data_field_menu extends data_field_base {
  26  
  27      var $type = 'menu';
  28      /**
  29       * priority for globalsearch indexing
  30       *
  31       * @var int
  32       */
  33      protected static $priority = self::HIGH_PRIORITY;
  34  
  35      function display_add_field($recordid = 0, $formdata = null) {
  36          global $DB, $OUTPUT;
  37  
  38          if ($formdata) {
  39              $fieldname = 'field_' . $this->field->id;
  40              $content = $formdata->$fieldname;
  41          } else if ($recordid) {
  42              $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  43              $content = trim($content);
  44          } else {
  45              $content = '';
  46          }
  47          $str = '<div title="' . s($this->field->description) . '">';
  48  
  49          $options = array();
  50          $rawoptions = explode("\n",$this->field->param1);
  51          foreach ($rawoptions as $option) {
  52              $option = trim($option);
  53              if (strlen($option) > 0) {
  54                  $options[$option] = $option;
  55              }
  56          }
  57  
  58          $str .= '<label for="' . 'field_' . $this->field->id . '">';
  59          $str .= html_writer::span($this->field->name, 'accesshide');
  60          if ($this->field->required) {
  61              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  62              $str .= html_writer::div($image, 'inline-req');
  63          }
  64          $str .= '</label>';
  65          $str .= html_writer::select($options, 'field_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
  66                                      array('id' => 'field_'.$this->field->id, 'class' => 'mod-data-input custom-select'));
  67  
  68          $str .= '</div>';
  69  
  70          return $str;
  71      }
  72  
  73      function display_search_field($content = '') {
  74          global $CFG, $DB;
  75  
  76          $varcharcontent =  $DB->sql_compare_text('content', 255);
  77          $sql = "SELECT DISTINCT $varcharcontent AS content
  78                    FROM {data_content}
  79                   WHERE fieldid=? AND content IS NOT NULL";
  80  
  81          $usedoptions = array();
  82          if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
  83              foreach ($used as $data) {
  84                  $value = $data->content;
  85                  if ($value === '') {
  86                      continue;
  87                  }
  88                  $usedoptions[$value] = $value;
  89              }
  90          }
  91  
  92          $options = array();
  93          foreach (explode("\n",$this->field->param1) as $option) {
  94              $option = trim($option);
  95              if (!isset($usedoptions[$option])) {
  96                  continue;
  97              }
  98              $options[$option] = $option;
  99          }
 100          if (!$options) {
 101              // oh, nothing to search for
 102              return '';
 103          }
 104  
 105          $return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
 106              'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
 107          $return .= html_writer::select($options, 'f_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
 108                  array('class' => 'custom-select'));
 109          return $return;
 110      }
 111  
 112      public function parse_search_field($defaults = null) {
 113          $param = 'f_'.$this->field->id;
 114          if (empty($defaults[$param])) {
 115              $defaults = array($param => '');
 116          }
 117          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 118      }
 119  
 120      function generate_sql($tablealias, $value) {
 121          global $DB;
 122  
 123          static $i=0;
 124          $i++;
 125          $name = "df_menu_$i";
 126          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
 127  
 128          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
 129      }
 130  
 131      /**
 132       * Check if a field from an add form is empty
 133       *
 134       * @param mixed $value
 135       * @param mixed $name
 136       * @return bool
 137       */
 138      function notemptyfield($value, $name) {
 139          return strval($value) !== '';
 140      }
 141  
 142      /**
 143       * Return the plugin configs for external functions.
 144       *
 145       * @return array the list of config parameters
 146       * @since Moodle 3.3
 147       */
 148      public function get_config_for_external() {
 149          // Return all the config parameters.
 150          $configs = [];
 151          for ($i = 1; $i <= 10; $i++) {
 152              $configs["param$i"] = $this->field->{"param$i"};
 153          }
 154          return $configs;
 155      }
 156  }