Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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_radiobutton extends data_field_base {
  26  
  27      var $type = 'radiobutton';
  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 $CFG, $DB, $OUTPUT;
  37  
  38          if ($formdata) {
  39              $fieldname = 'field_' . $this->field->id;
  40              if (isset($formdata->$fieldname)) {
  41                  $content = $formdata->$fieldname;
  42              } else {
  43                  $content = '';
  44              }
  45          } else if ($recordid) {
  46              $content = trim($DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)));
  47          } else {
  48              $content = '';
  49          }
  50  
  51          $str = '<div title="' . s($this->field->description) . '">';
  52          $str .= '<fieldset><legend><span class="accesshide">' . $this->field->name;
  53  
  54          if ($this->field->required) {
  55              $str .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
  56              $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
  57              $str .= html_writer::div($image, 'inline-req');
  58          } else {
  59              $str .= '</span></legend>';
  60          }
  61  
  62          $i = 0;
  63          $requiredstr = '';
  64          $options = explode("\n", $this->field->param1);
  65          foreach ($options as $radio) {
  66              $radio = trim($radio);
  67              if ($radio === '') {
  68                  continue; // skip empty lines
  69              }
  70              $str .= '<input type="radio" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '" ';
  71              $str .= 'value="' . s($radio) . '" class="mod-data-input mr-1" ';
  72  
  73              if ($content == $radio) {
  74                  // Selected by user.
  75                  $str .= 'checked />';
  76              } else {
  77                  $str .= '/>';
  78              }
  79  
  80              $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$radio.'</label><br />';
  81              $i++;
  82          }
  83          $str .= '</fieldset>';
  84          $str .= '</div>';
  85          return $str;
  86      }
  87  
  88      function display_search_field($value = '') {
  89          global $CFG, $DB;
  90  
  91          $varcharcontent = $DB->sql_compare_text('content', 255);
  92          $used = $DB->get_records_sql(
  93              "SELECT DISTINCT $varcharcontent AS content
  94                 FROM {data_content}
  95                WHERE fieldid=?
  96               ORDER BY $varcharcontent", array($this->field->id));
  97  
  98          $options = array();
  99          if(!empty($used)) {
 100              foreach ($used as $rec) {
 101                  $options[$rec->content] = $rec->content;  //Build following indicies from the sql.
 102              }
 103          }
 104          $return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
 105              'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
 106          $return .= html_writer::select($options, 'f_'.$this->field->id, $value,
 107              array('' => 'choosedots'), array('class' => 'custom-select'));
 108          return $return;
 109      }
 110  
 111      public function parse_search_field($defaults = null) {
 112          $param = 'f_'.$this->field->id;
 113          if (empty($defaults[$param])) {
 114              $defaults = array($param => '');
 115          }
 116          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 117      }
 118  
 119      function generate_sql($tablealias, $value) {
 120          global $DB;
 121  
 122          static $i=0;
 123          $i++;
 124          $name = "df_radiobutton_$i";
 125          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
 126  
 127          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
 128      }
 129  
 130      /**
 131       * Check if a field from an add form is empty
 132       *
 133       * @param mixed $value
 134       * @param mixed $name
 135       * @return bool
 136       */
 137      function notemptyfield($value, $name) {
 138          return strval($value) !== '';
 139      }
 140  
 141      /**
 142       * Return the plugin configs for external functions.
 143       *
 144       * @return array the list of config parameters
 145       * @since Moodle 3.3
 146       */
 147      public function get_config_for_external() {
 148          // Return all the config parameters.
 149          $configs = [];
 150          for ($i = 1; $i <= 10; $i++) {
 151              $configs["param$i"] = $this->field->{"param$i"};
 152          }
 153          return $configs;
 154      }
 155  }
 156