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 311] [Versions 310 and 400] [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  //2/19/07:  Advanced search of the date field is currently disabled because it does not track
  26  // pre 1970 dates and does not handle blank entrys.  Advanced search functionality for this field
  27  // type can be enabled once these issues are addressed in the core API.
  28  
  29  class data_field_date extends data_field_base {
  30  
  31      var $type = 'date';
  32  
  33      var $day   = 0;
  34      var $month = 0;
  35      var $year  = 0;
  36  
  37      function display_add_field($recordid = 0, $formdata = null) {
  38          global $DB, $OUTPUT;
  39  
  40          if ($formdata) {
  41              $fieldname = 'field_' . $this->field->id . '_day';
  42              $day   = $formdata->$fieldname;
  43              $fieldname = 'field_' . $this->field->id . '_month';
  44              $month   = $formdata->$fieldname;
  45              $fieldname = 'field_' . $this->field->id . '_year';
  46              $year   = $formdata->$fieldname;
  47  
  48              $calendartype = \core_calendar\type_factory::get_calendar_instance();
  49              $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
  50              $content = make_timestamp(
  51                  $gregoriandate['year'],
  52                  $gregoriandate['month'],
  53                  $gregoriandate['day'],
  54                  $gregoriandate['hour'],
  55                  $gregoriandate['minute'],
  56                  0,
  57                  0,
  58                  false);
  59          } else if ($recordid) {
  60              $content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  61          } else {
  62              $content = time();
  63          }
  64  
  65          $str = '<div title="'.s($this->field->description).'" class="mod-data-input form-inline">';
  66          $dayselector = html_writer::select_time('days', 'field_'.$this->field->id.'_day', $content);
  67          $monthselector = html_writer::select_time('months', 'field_'.$this->field->id.'_month', $content);
  68          $yearselector = html_writer::select_time('years', 'field_'.$this->field->id.'_year', $content);
  69          $str .= $dayselector . $monthselector . $yearselector;
  70          $str .= '</div>';
  71  
  72          return $str;
  73      }
  74  
  75      //Enable the following three functions once core API issues have been addressed.
  76      function display_search_field($value=0) {
  77          $selectors = html_writer::select_time('days', 'f_'.$this->field->id.'_d', $value['timestamp'])
  78             . html_writer::select_time('months', 'f_'.$this->field->id.'_m', $value['timestamp'])
  79             . html_writer::select_time('years', 'f_'.$this->field->id.'_y', $value['timestamp']);
  80          $datecheck = html_writer::checkbox('f_'.$this->field->id.'_z', 1, $value['usedate']);
  81          $str = '<div class="form-inline">' . $selectors . ' ' . $datecheck . ' ' . get_string('usedate', 'data') . '</div>';
  82  
  83          return $str;
  84      }
  85  
  86      function generate_sql($tablealias, $value) {
  87          global $DB;
  88  
  89          static $i=0;
  90          $i++;
  91          $name = "df_date_$i";
  92          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
  93          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name => $value['timestamp']));
  94      }
  95  
  96      public function parse_search_field($defaults = null) {
  97          $paramday = 'f_'.$this->field->id.'_d';
  98          $parammonth = 'f_'.$this->field->id.'_m';
  99          $paramyear = 'f_'.$this->field->id.'_y';
 100          $paramusedate = 'f_'.$this->field->id.'_z';
 101          if (empty($defaults[$paramday])) {  // One empty means the other ones are empty too.
 102              $defaults = array($paramday => 0, $parammonth => 0, $paramyear => 0, $paramusedate => 0);
 103          }
 104  
 105          $day   = optional_param($paramday, $defaults[$paramday], PARAM_INT);
 106          $month = optional_param($parammonth, $defaults[$parammonth], PARAM_INT);
 107          $year  = optional_param($paramyear, $defaults[$paramyear], PARAM_INT);
 108          $usedate = optional_param($paramusedate, $defaults[$paramusedate], PARAM_INT);
 109  
 110          $data = array();
 111          if (!empty($day) && !empty($month) && !empty($year) && $usedate == 1) {
 112              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 113              $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
 114  
 115              $data['timestamp'] = make_timestamp(
 116                  $gregoriandate['year'],
 117                  $gregoriandate['month'],
 118                  $gregoriandate['day'],
 119                  $gregoriandate['hour'],
 120                  $gregoriandate['minute'],
 121                  0,
 122                  0,
 123                  false);
 124              $data['usedate'] = 1;
 125              return $data;
 126          } else {
 127              return 0;
 128          }
 129      }
 130  
 131      function update_content($recordid, $value, $name='') {
 132          global $DB;
 133  
 134          $names = explode('_',$name);
 135          $name = $names[2];          // day month or year
 136  
 137          $this->$name = $value;
 138  
 139          if ($this->day and $this->month and $this->year) {  // All of them have been collected now
 140  
 141              $content = new stdClass();
 142              $content->fieldid = $this->field->id;
 143              $content->recordid = $recordid;
 144  
 145              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 146              $gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day);
 147              $content->content = make_timestamp(
 148                  $gregoriandate['year'],
 149                  $gregoriandate['month'],
 150                  $gregoriandate['day'],
 151                  $gregoriandate['hour'],
 152                  $gregoriandate['minute'],
 153                  0,
 154                  0,
 155                  false);
 156  
 157              if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 158                  $content->id = $oldcontent->id;
 159                  return $DB->update_record('data_content', $content);
 160              } else {
 161                  return $DB->insert_record('data_content', $content);
 162              }
 163          }
 164      }
 165  
 166      function display_browse_field($recordid, $template) {
 167          global $CFG, $DB;
 168  
 169          if ($content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 170              return userdate($content, get_string('strftimedate'), 0);
 171          }
 172      }
 173  
 174      function get_sort_sql($fieldname) {
 175          global $DB;
 176          return $DB->sql_cast_char2int($fieldname, true);
 177      }
 178  
 179      /**
 180       * Return the plugin configs for external functions.
 181       *
 182       * @return array the list of config parameters
 183       * @since Moodle 3.3
 184       */
 185      public function get_config_for_external() {
 186          // Return all the config parameters.
 187          $configs = [];
 188          for ($i = 1; $i <= 10; $i++) {
 189              $configs["param$i"] = $this->field->{"param$i"};
 190          }
 191          return $configs;
 192      }
 193  }