Skip to content

Commit

Permalink
MDL-55594 output: Adding template for html_writer::select_time
Browse files Browse the repository at this point in the history
Part of MDL-55071
  • Loading branch information
Frederic Massart authored and danpoltawski committed Sep 23, 2016
1 parent d893a41 commit 14395f9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/outputcomponents.php
Expand Up @@ -1689,6 +1689,8 @@ private static function select_optgroup($groupname, $options, array $selected) {
* @return HTML fragment
*/
public static function select_time($type, $name, $currenttime = 0, $step = 5, array $attributes = null) {
global $OUTPUT;

if (!$currenttime) {
$currenttime = time();
}
Expand Down Expand Up @@ -1729,13 +1731,30 @@ public static function select_time($type, $name, $currenttime = 0, $step = 5, ar
throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
}

if (empty($attributes['id'])) {
$attributes['id'] = self::random_id('ts_');
}
$timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, $attributes);
$label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
$attributes = (array) $attributes;
$data = (object) [
'name' => $name,
'id' => !empty($attributes['id']) ? $attributes['id'] : self::random_id('ts_'),
'label' => get_string(substr($type, 0, -1), 'form'),
'options' => array_map(function($value) use ($timeunits, $currentdate, $userdatetype) {
return [
'name' => $timeunits[$value],
'value' => $value,
'selected' => $currentdate[$userdatetype] == $value
];
}, array_keys($timeunits)),
];

unset($attributes['id']);
unset($attributes['name']);
$data->attributes = array_map(function($name) use ($attributes) {
return [
'name' => $name,
'value' => $attributes[$name]
];
}, array_keys($attributes));

return $label.$timerselector;
return $OUTPUT->render_from_template('core/select_time', $data);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/templates/select_time.mustache
@@ -0,0 +1,25 @@
{{!
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/>.
}}
{{!
Select time.
}}
<label for="{{id}}" class="accesshide">{{label}}</label>
<select name="{{name}}" id="{{id}}" {{#attributes}} {{name}}="{{value}}"{{/attributes}}>
{{#options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
{{/options}}
</select>
25 changes: 25 additions & 0 deletions theme/noname/templates/core/select_time.mustache
@@ -0,0 +1,25 @@
{{!
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/>.
}}
{{!
Select time.
}}
<label for="{{id}}" class="sr-only">{{label}}</label>
<select name="{{name}}" id="{{id}}" {{#attributes}} {{name}}="{{value}}"{{/attributes}} class="form-control">
{{#options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
{{/options}}
</select>

0 comments on commit 14395f9

Please sign in to comment.