Advertisement
Guest User

admin_setting_configdatetime.php

a guest
Jun 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | None | 0 0
  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.  * Admin setting for date and time
  19.  *
  20.  * @package local_outage
  21.  * @copyright 2018 The Open University
  22.  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23.  */
  24.  
  25. namespace local_outage;
  26.  
  27. defined('MOODLE_INTERNAL') || die();
  28.  
  29. /**
  30.  * Admin setting for date and time
  31.  */
  32. class admin_setting_configdatetime extends \admin_setting {
  33.  
  34.     /**
  35.      * Constructor.
  36.      *
  37.      * @param string $name setting for date/time
  38.      * @param string $visiblename localised
  39.      * @param string $description long localised info
  40.      */
  41.     public function __construct($name, $visiblename, $description) {
  42.         parent::__construct($name, $visiblename, $description, '0');
  43.     }
  44.  
  45.     /**
  46.      * Gets the selected date/time.
  47.      *
  48.      * @return string Current date/time value
  49.      */
  50.     public function get_setting() {
  51.         $result = $this->config_read($this->name);
  52.         if (is_null($result)) {
  53.             return null;
  54.         }
  55.         return $result;
  56.     }
  57.  
  58.     /**
  59.      * Stores the date/time.
  60.      *
  61.      * @param string $data Time value or zero
  62.      * @return string Empty string if OK, error message otherwise
  63.      */
  64.     public function write_setting($data) {
  65.  
  66.         if (!empty($data['enabled'])) {
  67.             $value = make_timestamp($data['years'], $data['months'], $data['days'],
  68.                     $data['hours'], $data['minutes'], 0);
  69.         } else {
  70.             $value = 0;
  71.         }
  72.  
  73.         $result = $this->config_write($this->name, $value);
  74.         return $result ? '' : get_string('errorsetting', 'admin');
  75.     }
  76.  
  77.     /**
  78.      * Returns XHTML date and time select fields and checkbox.
  79.      *
  80.      * @param string $data 0 or a time value
  81.      * @param string $query Search query
  82.      * @return string XHTML time select fields and checkbox and wrapping div(s).
  83.      */
  84.     public function output_html($data, $query = '') {
  85.         global $OUTPUT;
  86.  
  87.         if ($data) {
  88.             $dateinfo = usergetdate($data);
  89.         } else {
  90.             $dateinfo = usergetdate(time());
  91.         }
  92.  
  93.         $context = (object) [
  94.             'id' => $this->get_id(),
  95.             'name' => $this->get_full_name(),
  96.             'years' => array_map(function($i) use ($dateinfo) {
  97.                 return [
  98.                     'value' => $i,
  99.                     'name' => $i,
  100.                     'selected' => $i == $dateinfo['year']
  101.                 ];
  102.             }, range(2018, 2028)),
  103.             'months' => array_map(function($i) use ($dateinfo) {
  104.                 return [
  105.                     'value' => $i,
  106.                     'name' => date_format_string(
  107.                             strtotime('2010-' . ($i < 10 ? '0' : '') . $i . '-01'), '%B'),
  108.                     'selected' => $i == $dateinfo['mon']
  109.                 ];
  110.             }, range(1, 12)),
  111.             'days' => array_map(function($i) use ($dateinfo) {
  112.                 return [
  113.                     'value' => $i,
  114.                     'name' => ($i < 10 ? '0' : '') . $i,
  115.                     'selected' => $i == $dateinfo['mday']
  116.                 ];
  117.             }, range(1, 31)),
  118.             'hours' => array_map(function($i) use ($dateinfo) {
  119.                 return [
  120.                     'value' => $i,
  121.                     'name' => $i,
  122.                     'selected' => $i == $dateinfo['hours']
  123.                 ];
  124.             }, range(0, 23)),
  125.             'minutes' => array_map(function($i) use ($dateinfo) {
  126.                 return [
  127.                     'value' => $i,
  128.                     'name' => ($i < 10 ? '0' : '') . $i,
  129.                     'selected' => $i == $dateinfo['minutes']
  130.                 ];
  131.             }, range(0, 59, 1)),
  132.             'enabled' => $data ? true : false
  133.         ];
  134.  
  135.         $element = $OUTPUT->render_from_template('local_outage/setting_configdatetime', $context);
  136.  
  137.         return format_admin_setting($this, $this->visiblename, $element, $this->description,
  138.                 $this->get_id() . '_enabled', '', null, $query);
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement