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

   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  
  77      /**
  78       * Display the search field in advanced search page
  79       * @param mixed $value
  80       * @return string
  81       * @throws coding_exception
  82       */
  83      public function display_search_field($value = null) {
  84          $currenttime = time();
  85          $selectors = html_writer::select_time('days', 'f_' . $this->field->id . '_d', $value['timestamp'] ?? $currenttime)
  86              . html_writer::select_time('months', 'f_' . $this->field->id . '_m', $value['timestamp'] ?? $currenttime)
  87              . html_writer::select_time('years', 'f_' . $this->field->id . '_y', $value['timestamp'] ?? $currenttime);
  88          $datecheck = html_writer::checkbox('f_' . $this->field->id . '_z', 1, $value['usedate'] ?? 0);
  89          $str = '<div class="form-inline">' . $selectors . ' ' . $datecheck . ' ' . get_string('usedate', 'data') . '</div>';
  90  
  91          return $str;
  92      }
  93  
  94      function generate_sql($tablealias, $value) {
  95          global $DB;
  96  
  97          static $i=0;
  98          $i++;
  99          $name = "df_date_$i";
 100          $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
 101          return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name => $value['timestamp']));
 102      }
 103  
 104      public function parse_search_field($defaults = null) {
 105          $paramday = 'f_'.$this->field->id.'_d';
 106          $parammonth = 'f_'.$this->field->id.'_m';
 107          $paramyear = 'f_'.$this->field->id.'_y';
 108          $paramusedate = 'f_'.$this->field->id.'_z';
 109          if (empty($defaults[$paramday])) {  // One empty means the other ones are empty too.
 110              $defaults = array($paramday => 0, $parammonth => 0, $paramyear => 0, $paramusedate => 0);
 111          }
 112  
 113          $day   = optional_param($paramday, $defaults[$paramday], PARAM_INT);
 114          $month = optional_param($parammonth, $defaults[$parammonth], PARAM_INT);
 115          $year  = optional_param($paramyear, $defaults[$paramyear], PARAM_INT);
 116          $usedate = optional_param($paramusedate, $defaults[$paramusedate], PARAM_INT);
 117  
 118          $data = array();
 119          if (!empty($day) && !empty($month) && !empty($year) && $usedate == 1) {
 120              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 121              $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day);
 122  
 123              $data['timestamp'] = make_timestamp(
 124                  $gregoriandate['year'],
 125                  $gregoriandate['month'],
 126                  $gregoriandate['day'],
 127                  $gregoriandate['hour'],
 128                  $gregoriandate['minute'],
 129                  0,
 130                  0,
 131                  false);
 132              $data['usedate'] = 1;
 133              return $data;
 134          } else {
 135              return 0;
 136          }
 137      }
 138  
 139      function update_content($recordid, $value, $name='') {
 140          global $DB;
 141  
 142          $names = explode('_',$name);
 143          $name = $names[2];          // day month or year
 144  
 145          $this->$name = $value;
 146  
 147          if ($this->day and $this->month and $this->year) {  // All of them have been collected now
 148  
 149              $content = new stdClass();
 150              $content->fieldid = $this->field->id;
 151              $content->recordid = $recordid;
 152  
 153              $calendartype = \core_calendar\type_factory::get_calendar_instance();
 154              $gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day);
 155              $content->content = make_timestamp(
 156                  $gregoriandate['year'],
 157                  $gregoriandate['month'],
 158                  $gregoriandate['day'],
 159                  $gregoriandate['hour'],
 160                  $gregoriandate['minute'],
 161                  0,
 162                  0,
 163                  false);
 164  
 165              if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 166                  $content->id = $oldcontent->id;
 167                  return $DB->update_record('data_content', $content);
 168              } else {
 169                  return $DB->insert_record('data_content', $content);
 170              }
 171          }
 172      }
 173  
 174      function display_browse_field($recordid, $template) {
 175          global $CFG, $DB;
 176  
 177          if ($content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
 178              return userdate($content, get_string('strftimedate'), 0);
 179          }
 180      }
 181  
 182      function get_sort_sql($fieldname) {
 183          global $DB;
 184          return $DB->sql_cast_char2int($fieldname, true);
 185      }
 186  
 187      /**
 188       * Return the plugin configs for external functions.
 189       *
 190       * @return array the list of config parameters
 191       * @since Moodle 3.3
 192       */
 193      public function get_config_for_external() {
 194          // Return all the config parameters.
 195          $configs = [];
 196          for ($i = 1; $i <= 10; $i++) {
 197              $configs["param$i"] = $this->field->{"param$i"};
 198          }
 199          return $configs;
 200      }
 201  }