Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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