-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadminlib.php
122 lines (105 loc) · 4.45 KB
/
adminlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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/>.
/**
* @package block_use_stats
* @author Valery Fremaux ([email protected])
* @copyright Valery Fremaux ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block\use_stats;
/**
* Time selector
*
* This is a liiitle bit messy. we're using two selects, but we're returning
* them as an array named after $name (so we only use $name2 internally for the setting)
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_configdatetime extends \admin_setting {
/**
* Get the selected time
*
* @return mixed An array containing 'h'=>xx, 'm'=>xx, or null if not set
*/
public function get_setting() {
$result = $this->config_read($this->name);
$datearr = getdate($result);
$data = [
'h' => $datearr['hours'],
'm' => $datearr['minutes'],
'y' => $datearr['year'],
'M' => $datearr['mon'],
'd' => $datearr['mday'],
];
return $data;
}
/**
* Store the time as unix timestamp
*
* @param array $data Must be form 'y' => xxxx, 'M' => xx, 'd' => xx, 'h'=>xx, 'm'=>xx
* @return bool true if success, false if not
*/
public function write_setting($data) {
if (!is_array($data)) {
return '';
}
$datetime = mktime($data['h'], $data['m'], 0, $data['M'], $data['d'], $data['y']);
$result = $this->config_write($this->name, $datetime);
return ($result ? '' : get_string('errorsetting', 'admin'));
}
/**
* Returns XHTML time select fields
*
* @param array $data Must be form 'h'=>xx, 'm'=>xx
* @param string $query
* @return string XHTML time select fields and wrapping div(s)
*/
public function output_html($data, $query = '') {
$default = $this->get_defaultsetting();
if (is_array($default)) {
$defaultinfo = $default['y'].'-'.$default['M'].'-'.$default['d'].' '.$default['h'].':'.$default['m'];
} else {
$defaultinfo = null;
}
$return = '<div class="form-datetime defaultsnext">';
$return .= '<select id="'.$this->get_id().'y" name="'.$this->get_full_name().'[y]">';
for ($i = 2010; $i < 2030; $i++) {
$return .= '<option value="'.$i.'"'.($i == $data['y'] ? ' selected="selected"' : '').'>'.$i.'</option>';
}
$return .= '</select><select id="'.$this->get_id().'M" name="'.$this->get_full_name().'[M]">';
for ($i = 1; $i < 12; $i++) {
$sel = ($i == $data['M'] ? ' selected="selected"' : '');
$return .= '<option value="'.$i.'"'.$sel.'>'.sprintf('%02d', $i).'</option>';
}
$return .= '</select><select id="'.$this->get_id().'d" name="'.$this->get_full_name().'[d]">';
for ($i = 1; $i < 31; $i++) {
$sel = ($i == $data['d'] ? ' selected="selected"' : '');
$return .= '<option value="'.$i.'"'.$sel.'>'.sprintf('%02d', $i).'</option>';
}
$return .= '</select><select id="'.$this->get_id().'h" name="'.$this->get_full_name().'[h]">';
for ($i = 0; $i < 24; $i++) {
$sel = ($i == $data['h'] ? ' selected="selected"' : '');
$return .= '<option value="'.$i.'"'.$sel.'>'.$i.'</option>';
}
$return .= '</select>:<select id="'.$this->get_id().'m" name="'.$this->get_full_name().'[m]">';
for ($i = 0; $i < 60; $i += 5) {
$sel = ($i == $data['m'] ? ' selected="selected"' : '');
$return .= '<option value="'.$i.'"'.$sel.'>'.$i.'</option>';
}
$return .= '</select></div>';
return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
}
}