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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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_number extends data_field_base {
  26      var $type = 'number';
  27  
  28      public function supports_preview(): bool {
  29          return true;
  30      }
  31  
  32      public function get_data_content_preview(int $recordid): stdClass {
  33          return (object)[
  34              'id' => 0,
  35              'fieldid' => $this->field->id,
  36              'recordid' => $recordid,
  37              'content' => 1233 + $recordid,
  38              'content1' => null,
  39              'content2' => null,
  40              'content3' => null,
  41              'content4' => null,
  42          ];
  43      }
  44  
  45      function update_content($recordid, $value, $name='') {
  46          global $DB;
  47  
  48          $content = new stdClass();
  49          $content->fieldid = $this->field->id;
  50          $content->recordid = $recordid;
  51          $value = trim($value);
  52          if (strlen($value) > 0) {
  53              $content->content = floatval($value);
  54          } else {
  55              $content->content = null;
  56          }
  57          if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
  58              $content->id = $oldcontent->id;
  59              return $DB->update_record('data_content', $content);
  60          } else {
  61              return $DB->insert_record('data_content', $content);
  62          }
  63      }
  64  
  65      function display_browse_field($recordid, $template) {
  66          $content = $this->get_data_content($recordid);
  67          if (!$content || $content->content === '') {
  68              return '';
  69          }
  70          $number = $content->content;
  71          $decimals = trim($this->field->param1 ?? '');
  72          // Only apply number formatting if param1 contains an integer number >= 0.
  73          if (preg_match("/^\d+$/", $decimals)) {
  74              $decimals = $decimals * 1;
  75              // Removes leading zeros (eg. '007' -> '7'; '00' -> '0').
  76              $str = format_float($number, $decimals, true);
  77          } else {
  78              $str = $number;
  79          }
  80          return $str;
  81      }
  82  
  83      function display_search_field($value = '') {
  84          return '<label class="accesshide" for="f_'.$this->field->id.'">' . get_string('fieldname', 'data') . '</label>' .
  85                 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" ' .
  86                 'value="'.s($value).'" class="form-control d-inline"/>';
  87      }
  88  
  89      public function parse_search_field($defaults = null) {
  90          $param = 'f_'.$this->field->id;
  91          if (empty($defaults[$param])) {
  92              $defaults = array($param => '');
  93          }
  94          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
  95      }
  96  
  97      // need to cast?
  98      function generate_sql($tablealias, $value) {
  99          global $DB;
 100  
 101          static $i=0;
 102          $i++;
 103          $name = "df_number_$i";
 104          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
 105          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
 106      }
 107  
 108      function get_sort_sql($fieldname) {
 109          global $DB;
 110          return $DB->sql_cast_char2real($fieldname, true);
 111      }
 112  
 113      /**
 114       * Check if a field from an add form is empty
 115       *
 116       * @param mixed $value
 117       * @param mixed $name
 118       * @return bool
 119       */
 120      function notemptyfield($value, $name) {
 121          return strval($value) !== '';
 122      }
 123  
 124      /**
 125       * Return the plugin configs for external functions.
 126       *
 127       * @return array the list of config parameters
 128       * @since Moodle 3.3
 129       */
 130      public function get_config_for_external() {
 131          // Return all the config parameters.
 132          $configs = [];
 133          for ($i = 1; $i <= 10; $i++) {
 134              $configs["param$i"] = $this->field->{"param$i"};
 135          }
 136          return $configs;
 137      }
 138  }