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  //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      public function supports_preview(): bool {
  38          return true;
  39      }
  40  
  41      public function get_data_content_preview(int $recordid): stdClass {
  42          return (object)[
  43              'id' => 0,
  44              'fieldid' => $this->field->id,
  45              'recordid' => $recordid,
  46              'content' => (string) time(),
  47              'content1' => null,
  48              'content2' => null,
  49              'content3' => null,
  50              'content4' => null,
  51          ];
  52      }
  53  
  54      function display_add_field($recordid = 0, $formdata = null) {
  55          global $DB, $OUTPUT;
  56  
  57          if ($formdata) {
  58              $fieldname = 'field_' . $this->field->id . '_day';
  59              $day   = $formdata->$fieldname;
  60              $fieldname = 'field_' . $this->field->id . '_month';
  61              $month   = $formdata->$fieldname;
  62              $fieldname = 'field_' . $this->field->id . '_year';
  63              $year   = $formdata->$fieldname;
  64  
  65              $calendartype = \core_calendar\type_factory::get_calendar_instance();
  66              $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
  67              $content = make_timestamp(
  68                  $gregoriandate['year'],
  69                  $gregoriandate['month'],
  70                  $gregoriandate['day'],
  71                  $gregoriandate['hour'],
  72                  $gregoriandate['minute'],
  73                  0,
  74                  0,
  75                  false);
  76          } else if ($recordid) {
  77              $content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
  78          } else {
  79              $content = time();
  80          }
  81  
  82          $str = '<div title="'.s($this->field->description).'" class="mod-data-input form-inline">';
  83          $dayselector = html_writer::select_time('days', 'field_'.$this->field->id.'_day', $content);
  84          $monthselector = html_writer::select_time('months', 'field_'.$this->field->id.'_month', $content);
  85          $yearselector = html_writer::select_time('years', 'field_'.$this->field->id.'_year', $content);
  86          $str .= $dayselector . $monthselector . $yearselector;
  87          $str .= '</div>';
  88  
  89          return $str;
  90      }
  91  
  92      // Enable the following three functions once core API issues have been addressed.
  93  
  94      /**
  95       * Display the search field in advanced search page
  96       * @param mixed $value
  97       * @return string
  98       * @throws coding_exception
  99       */
 100      public function display_search_field($value = null) {
 101          $currenttime = time();
 102          $selectors = html_writer::select_time('days', 'f_' . $this->field->id . '_d', $value['timestamp'] ?? $currenttime)
 103              . html_writer::select_time('months', 'f_' . $this->field->id . '_m', $value['timestamp'] ?? $currenttime)
 104              . html_writer::select_time('years', 'f_' . $this->field->id . '_y', $value['timestamp'] ?? $currenttime);
 105          $datecheck = html_writer::checkbox('f_' . $this->field->id . '_z', 1, $value['usedate'] ?? 0);
 106          $str = '<div class="form-inline">' . $selectors . ' ' . $datecheck . ' ' . get_string('usedate', 'data') . '</div>';
 107  
 108          return $str;
 109      }
 110  
 111      function generate_sql($tablealias, $value) {
 112          global $DB;
 113  
 114          static $i=0;
 115          $i++;
 116          $name = "df_date_$i";
 117          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
 118          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name => $value['timestamp']));
 119      }
 120  
 121      public function parse_search_field($defaults = null) {
 122          $paramday = 'f_'.$this->field->id.'_d';
 123          $parammonth = 'f_'.$this->field->id.'_m';
 124          $paramyear = 'f_'.$this->field->id.'_y';
 125          $paramusedate = 'f_'.$this->field->id.'_z';
 126          if (empty($defaults[$paramday])) {  // One empty means the other ones are empty too.
 127              $defaults = array($paramday => 0, $parammonth => 0, $paramyear => 0, $paramusedate => 0);
 128          }
 129  
 130          $day   = optional_param($paramday, $defaults[$paramday], PARAM_INT);
 131          $month = optional_param($parammonth, $defaults[$parammonth], PARAM_INT);
 132          $year  = optional_param($paramyear, $defaults[$paramyear], PARAM_INT);
 133          $usedate = optional_param($paramusedate, $defaults[$paramusedate], PARAM_INT);
 134  
 135          $data = array();
 136          if (!empty($day) && !empty($month) && !empty($year) && $usedate == 1) {
 137              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 138              $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
 139  
 140              $data['timestamp'] = make_timestamp(
 141                  $gregoriandate['year'],
 142                  $gregoriandate['month'],
 143                  $gregoriandate['day'],
 144                  $gregoriandate['hour'],
 145                  $gregoriandate['minute'],
 146                  0,
 147                  0,
 148                  false);
 149              $data['usedate'] = 1;
 150              return $data;
 151          } else {
 152              return 0;
 153          }
 154      }
 155  
 156      function update_content($recordid, $value, $name='') {
 157          global $DB;
 158  
 159          $names = explode('_',$name);
 160          $name = $names[2];          // day month or year
 161  
 162          $this->$name = $value;
 163  
 164          if ($this->day and $this->month and $this->year) {  // All of them have been collected now
 165  
 166              $content = new stdClass();
 167              $content->fieldid = $this->field->id;
 168              $content->recordid = $recordid;
 169  
 170              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 171              $gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day);
 172              $content->content = make_timestamp(
 173                  $gregoriandate['year'],
 174                  $gregoriandate['month'],
 175                  $gregoriandate['day'],
 176                  $gregoriandate['hour'],
 177                  $gregoriandate['minute'],
 178                  0,
 179                  0,
 180                  false);
 181  
 182              if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 183                  $content->id = $oldcontent->id;
 184                  return $DB->update_record('data_content', $content);
 185              } else {
 186                  return $DB->insert_record('data_content', $content);
 187              }
 188          }
 189      }
 190  
 191      function display_browse_field($recordid, $template) {
 192          $content = $this->get_data_content($recordid);
 193          if (!$content || empty($content->content)) {
 194              return '';
 195          }
 196          return userdate($content->content, get_string('strftimedate'), 0);
 197      }
 198  
 199      function get_sort_sql($fieldname) {
 200          global $DB;
 201          return $DB->sql_cast_char2real($fieldname, true);
 202      }
 203  
 204      /**
 205       * Return the plugin configs for external functions.
 206       *
 207       * @return array the list of config parameters
 208       * @since Moodle 3.3
 209       */
 210      public function get_config_for_external() {
 211          // Return all the config parameters.
 212          $configs = [];
 213          for ($i = 1; $i <= 10; $i++) {
 214              $configs["param$i"] = $this->field->{"param$i"};
 215          }
 216          return $configs;
 217      }
 218  }