Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

   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   * Front-end class.
  19   *
  20   * @package availability_date
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace availability_date;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Front-end class.
  31   *
  32   * @package availability_date
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class frontend extends \core_availability\frontend {
  37      /**
  38       * The date selector popup is not currently supported because the date
  39       * selector is in a messy state (about to be replaced with a new YUI3
  40       * implementation) and MDL-44814 was rejected. I have left the code in
  41       * place, but disabled. When the date selector situation is finalised,
  42       * then this constant should be removed (either applying MDL-44814 if old
  43       * selector is still in use, or modifying the JavaScript code to support the
  44       * new date selector if it has landed).
  45       *
  46       * @var bool
  47       */
  48      const DATE_SELECTOR_SUPPORTED = false;
  49  
  50      protected function get_javascript_strings() {
  51          return array('ajaxerror', 'direction_before', 'direction_from', 'direction_until',
  52                  'direction_label', 'error_dateconflict');
  53      }
  54  
  55      /**
  56       * Given field values, obtains the corresponding timestamp.
  57       *
  58       * @param int $year Year
  59       * @param int $month Month
  60       * @param int $day Day
  61       * @param int $hour Hour
  62       * @param int $minute Minute
  63       * @return int Timestamp
  64       */
  65      public static function get_time_from_fields($year, $month, $day, $hour, $minute) {
  66          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  67          $gregoriandate = $calendartype->convert_to_gregorian(
  68                  $year, $month, $day, $hour, $minute);
  69          return make_timestamp($gregoriandate['year'], $gregoriandate['month'],
  70                  $gregoriandate['day'], $gregoriandate['hour'], $gregoriandate['minute'], 0);
  71      }
  72  
  73      /**
  74       * Given a timestamp, obtains corresponding field values.
  75       *
  76       * @param int $time Timestamp
  77       * @return \stdClass Object with fields for year, month, day, hour, minute
  78       */
  79      public static function get_fields_from_time($time) {
  80          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  81          $wrongfields = $calendartype->timestamp_to_date_array($time);
  82          return array('day' => $wrongfields['mday'],
  83                  'month' => $wrongfields['mon'], 'year' => $wrongfields['year'],
  84                  'hour' => $wrongfields['hours'], 'minute' => $wrongfields['minutes']);
  85      }
  86  
  87      protected function get_javascript_init_params($course, \cm_info $cm = null,
  88              \section_info $section = null) {
  89          global $CFG, $OUTPUT;
  90          require_once($CFG->libdir . '/formslib.php');
  91  
  92          // Support internationalised calendars.
  93          $calendartype = \core_calendar\type_factory::get_calendar_instance();
  94  
  95          // Get current date, but set time to 00:00 (to make it easier to
  96          // specify whole days) and change name of mday field to match below.
  97          $wrongfields = $calendartype->timestamp_to_date_array(time());
  98          $current = array('day' => $wrongfields['mday'],
  99                  'month' => $wrongfields['mon'], 'year' => $wrongfields['year'],
 100                  'hour' => 0, 'minute' => 0);
 101  
 102          // Time part is handled the same everywhere.
 103          $hours = array();
 104          for ($i = 0; $i <= 23; $i++) {
 105              $hours[$i] = sprintf("%02d", $i);
 106          }
 107          $minutes = array();
 108          for ($i = 0; $i < 60; $i += 5) {
 109              $minutes[$i] = sprintf("%02d", $i);
 110          }
 111  
 112          // List date fields.
 113          $fields = $calendartype->get_date_order(
 114                  $calendartype->get_min_year(), $calendartype->get_max_year());
 115  
 116          // Add time fields - in RTL mode these are switched.
 117          $fields['split'] = '/';
 118          if (right_to_left()) {
 119              $fields['minute'] = $minutes;
 120              $fields['colon'] = ':';
 121              $fields['hour'] = $hours;
 122          } else {
 123              $fields['hour'] = $hours;
 124              $fields['colon'] = ':';
 125              $fields['minute'] = $minutes;
 126          }
 127  
 128          // Output all date fields.
 129          $html = '<span class="availability-group">';
 130          foreach ($fields as $field => $options) {
 131              if ($options === '/') {
 132                  $html = rtrim($html);
 133  
 134                  // In Gregorian calendar mode only, we support a date selector popup, reusing
 135                  // code from form to ensure consistency.
 136                  if ($calendartype->get_name() === 'gregorian' && self::DATE_SELECTOR_SUPPORTED) {
 137                      $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle');
 138                      $html .= ' ' . \html_writer::link('#', $image, array('name' => 'x[calendar]'));
 139                      form_init_date_js();
 140                  }
 141  
 142                  $html .= '</span> <span class="availability-group">';
 143                  continue;
 144              }
 145              if ($options === ':') {
 146                  $html .= ': ';
 147                  continue;
 148              }
 149              $html .= \html_writer::start_tag('label');
 150              $html .= \html_writer::span(get_string($field) . ' ', 'accesshide');
 151              // NOTE: The fields need to have these weird names in order that they
 152              // match the standard Moodle form control, otherwise the date selector
 153              // won't find them.
 154              $html .= \html_writer::start_tag('select', array('name' => 'x[' . $field . ']', 'class' => 'custom-select'));
 155              foreach ($options as $key => $value) {
 156                  $params = array('value' => $key);
 157                  if ($current[$field] == $key) {
 158                      $params['selected'] = 'selected';
 159                  }
 160                  $html .= \html_writer::tag('option', s($value), $params);
 161              }
 162              $html .= \html_writer::end_tag('select');
 163              $html .= \html_writer::end_tag('label');
 164              $html .= ' ';
 165          }
 166          $html = rtrim($html) . '</span>';
 167  
 168          // Also get the time that corresponds to this default date.
 169          $time = self::get_time_from_fields($current['year'], $current['month'],
 170                  $current['day'], $current['hour'], $current['minute']);
 171  
 172          return array($html, $time);
 173      }
 174  }