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 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file contains the datetime profile field definition class.
  19   *
  20   * @package profilefield_datetime
  21   * @copyright 2010 Mark Nelson <markn@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  /**
  26   * Define datetime fields.
  27   *
  28   * @copyright 2010 Mark Nelson <markn@moodle.com>
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  30   */
  31  class profile_define_datetime extends profile_define_base {
  32  
  33      /**
  34       * Define the setting for a datetime custom field.
  35       *
  36       * @param moodleform $form the user form
  37       */
  38      public function define_form_specific($form) {
  39          // Get the current calendar in use - see MDL-18375.
  40          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  41  
  42          // Create variables to store start and end.
  43          list($year, $month, $day) = explode('_', date('Y_m_d'));
  44          $currentdate = $calendartype->convert_from_gregorian($year, $month, $day);
  45          $currentyear = $currentdate['year'];
  46  
  47          $arryears = $calendartype->get_years();
  48  
  49          // Add elements.
  50          $form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);
  51          $form->setType('param1', PARAM_INT);
  52          $form->setDefault('param1', $currentyear);
  53  
  54          $form->addElement('select', 'param2', get_string('endyear', 'profilefield_datetime'), $arryears);
  55          $form->setType('param2', PARAM_INT);
  56          $form->setDefault('param2', $currentyear);
  57  
  58          $form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
  59          $form->setType('param3', PARAM_INT);
  60  
  61          $form->addElement('hidden', 'startday', '1');
  62          $form->setType('startday', PARAM_INT);
  63          $form->addElement('hidden', 'startmonth', '1');
  64          $form->setType('startmonth', PARAM_INT);
  65          $form->addElement('hidden', 'startyear', '1');
  66          $form->setType('startyear', PARAM_INT);
  67          $form->addElement('hidden', 'endday', '1');
  68          $form->setType('endday', PARAM_INT);
  69          $form->addElement('hidden', 'endmonth', '1');
  70          $form->setType('endmonth', PARAM_INT);
  71          $form->addElement('hidden', 'endyear', '1');
  72          $form->setType('endyear', PARAM_INT);
  73          $form->addElement('hidden', 'defaultdata', '0');
  74          $form->setType('defaultdata', PARAM_INT);
  75      }
  76  
  77      /**
  78       * Validate the data from the profile field form.
  79       *
  80       * @param stdClass $data from the add/edit profile field form
  81       * @param array $files
  82       * @return array associative array of error messages
  83       */
  84      public function define_validate_specific($data, $files) {
  85          $errors = array();
  86  
  87          // Make sure the start year is not greater than the end year.
  88          if ($data->param1 > $data->param2) {
  89              $errors['param1'] = get_string('startyearafterend', 'profilefield_datetime');
  90          }
  91  
  92          return $errors;
  93      }
  94  
  95      /**
  96       * Alter form based on submitted or existing data.
  97       *
  98       * @param moodleform $mform
  99       */
 100      public function define_after_data(&$mform) {
 101          global $DB;
 102  
 103          // If we are adding a new profile field then the dates have already been set
 104          // by setDefault to the correct dates in the used calendar system. We only want
 105          // to execute the rest of the code when we have the years in the DB saved in
 106          // Gregorian that need converting to the date for this user.
 107          $id = optional_param('id', 0, PARAM_INT);
 108          if ($id === 0) {
 109              return;
 110          }
 111  
 112          // Get the field data from the DB.
 113          $field = $DB->get_record('user_info_field', array('id' => $id), 'param1, param2', MUST_EXIST);
 114  
 115          // Get the current calendar in use - see MDL-18375.
 116          $calendartype = \core_calendar\type_factory::get_calendar_instance();
 117  
 118          // An array to store form values.
 119          $values = array();
 120  
 121          // The start and end year will be set as a Gregorian year in the DB. We want
 122          // convert these to the equivalent year in the current calendar type being used.
 123          $startdate = $calendartype->convert_from_gregorian($field->param1, 1, 1);
 124          $values['startday'] = $startdate['day'];
 125          $values['startmonth'] = $startdate['month'];
 126          $values['startyear'] = $startdate['year'];
 127          $values['param1'] = $startdate['year'];
 128  
 129          $stopdate = $calendartype->convert_from_gregorian($field->param2, 1, 1);
 130          $values['endday'] = $stopdate['day'];
 131          $values['endmonth'] = $stopdate['month'];
 132          $values['endyear'] = $stopdate['year'];
 133          $values['param2'] = $stopdate['year'];
 134  
 135          // Set the values.
 136          foreach ($values as $key => $value) {
 137              $param = $mform->getElement($key);
 138              $param->setValue($value);
 139          }
 140      }
 141  
 142      /**
 143       * Preprocess data from the profile field form before
 144       * it is saved.
 145       *
 146       * @param stdClass $data from the add/edit profile field form
 147       * @return stdClass processed data object
 148       */
 149      public function define_save_preprocess($data) {
 150          // Get the current calendar in use - see MDL-18375.
 151          $calendartype = \core_calendar\type_factory::get_calendar_instance();
 152  
 153          // Check if the start year was changed, if it was then convert from the start of that year.
 154          if ($data->param1 != $data->startyear) {
 155              $startdate = $calendartype->convert_to_gregorian($data->param1, 1, 1);
 156          } else {
 157              $startdate = $calendartype->convert_to_gregorian($data->param1, $data->startmonth, $data->startday);
 158          }
 159  
 160          // Check if the end year was changed, if it was then convert from the start of that year.
 161          if ($data->param2 != $data->endyear) {
 162              $stopdate = $calendartype->convert_to_gregorian($data->param2, 1, 1);
 163          } else {
 164              $stopdate = $calendartype->convert_to_gregorian($data->param2, $data->endmonth, $data->endday);
 165          }
 166  
 167          $data->param1 = $startdate['year'];
 168          $data->param2 = $stopdate['year'];
 169  
 170          if (empty($data->param3)) {
 171              $data->param3 = null;
 172          }
 173  
 174          // No valid value in the default data column needed.
 175          $data->defaultdata = '0';
 176  
 177          return $data;
 178      }
 179  }