Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This file is part of Moodle - http://moodle.org/
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- /**
- * Admin setting for date and time
- *
- * @package local_outage
- * @copyright 2018 The Open University
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- namespace local_outage;
- defined('MOODLE_INTERNAL') || die();
- /**
- * Admin setting for date and time
- */
- class admin_setting_configdatetime extends \admin_setting {
- /**
- * Constructor.
- *
- * @param string $name setting for date/time
- * @param string $visiblename localised
- * @param string $description long localised info
- */
- public function __construct($name, $visiblename, $description) {
- parent::__construct($name, $visiblename, $description, '0');
- }
- /**
- * Gets the selected date/time.
- *
- * @return string Current date/time value
- */
- public function get_setting() {
- $result = $this->config_read($this->name);
- if (is_null($result)) {
- return null;
- }
- return $result;
- }
- /**
- * Stores the date/time.
- *
- * @param string $data Time value or zero
- * @return string Empty string if OK, error message otherwise
- */
- public function write_setting($data) {
- if (!empty($data['enabled'])) {
- $value = make_timestamp($data['years'], $data['months'], $data['days'],
- $data['hours'], $data['minutes'], 0);
- } else {
- $value = 0;
- }
- $result = $this->config_write($this->name, $value);
- return $result ? '' : get_string('errorsetting', 'admin');
- }
- /**
- * Returns XHTML date and time select fields and checkbox.
- *
- * @param string $data 0 or a time value
- * @param string $query Search query
- * @return string XHTML time select fields and checkbox and wrapping div(s).
- */
- public function output_html($data, $query = '') {
- global $OUTPUT;
- if ($data) {
- $dateinfo = usergetdate($data);
- } else {
- $dateinfo = usergetdate(time());
- }
- $context = (object) [
- 'id' => $this->get_id(),
- 'name' => $this->get_full_name(),
- 'years' => array_map(function($i) use ($dateinfo) {
- return [
- 'value' => $i,
- 'name' => $i,
- 'selected' => $i == $dateinfo['year']
- ];
- }, range(2018, 2028)),
- 'months' => array_map(function($i) use ($dateinfo) {
- return [
- 'value' => $i,
- 'name' => date_format_string(
- strtotime('2010-' . ($i < 10 ? '0' : '') . $i . '-01'), '%B'),
- 'selected' => $i == $dateinfo['mon']
- ];
- }, range(1, 12)),
- 'days' => array_map(function($i) use ($dateinfo) {
- return [
- 'value' => $i,
- 'name' => ($i < 10 ? '0' : '') . $i,
- 'selected' => $i == $dateinfo['mday']
- ];
- }, range(1, 31)),
- 'hours' => array_map(function($i) use ($dateinfo) {
- return [
- 'value' => $i,
- 'name' => $i,
- 'selected' => $i == $dateinfo['hours']
- ];
- }, range(0, 23)),
- 'minutes' => array_map(function($i) use ($dateinfo) {
- return [
- 'value' => $i,
- 'name' => ($i < 10 ? '0' : '') . $i,
- 'selected' => $i == $dateinfo['minutes']
- ];
- }, range(0, 59, 1)),
- 'enabled' => $data ? true : false
- ];
- $element = $OUTPUT->render_from_template('local_outage/setting_configdatetime', $context);
- return format_admin_setting($this, $this->visiblename, $element, $this->description,
- $this->get_id() . '_enabled', '', null, $query);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement