Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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