Skip to content

Commit

Permalink
MDL-20204 legacy time selects converted, please note we should always…
Browse files Browse the repository at this point in the history
… use mforms if possible
  • Loading branch information
skodak committed Feb 10, 2010
1 parent 78bdac6 commit f83b9b6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 111 deletions.
4 changes: 3 additions & 1 deletion admin/webservice/service_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@
//valid until date selector
$contents .= "<div class=\"fitem\"><div class=\"fitemtitle\"><label>".get_string('validuntil','webservice')." </label></div><div class=\"felement\">";
// the following date selector needs to have specific day/month/year field ids because we use javascript (enable/disable).
$selectors = html_select::make_time_selectors(array('days' => 'fromday'.$user->id,'months' => 'frommonth'.$user->id, 'years' => 'fromyear'.$user->id),$user->validuntil);
$selectors = html_writer::select_time('days', 'fromday'.$user->id, $user->validuntil)
. html_writer::select_time('months', 'frommonth'.$user->id, $user->validuntil)
. html_writer::select_time('years', 'fromyear'.$user->id, $user->validuntil);
foreach ($selectors as $select) {
if (empty($user->validuntil)) {
$select->disabled = true;
Expand Down
8 changes: 4 additions & 4 deletions backup/restore_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@
/// (some formats like main page, social..., haven't it and rolling dates
/// from 0 produces crazy dates. MDL-10125
if ($form1->startdate) {
$dayselector = html_select::make_time_selector('days', "startday", $form1->startdate);
$monthselector = html_select::make_time_selector('months', "startmonth", $form1->startdate);
$yearselector = html_select::make_time_selector('years', "startyear", $form1->startdate);
$dayselector = html_writer::select_time('days', "startday", $form1->startdate);
$monthselector = html_writer::select_time('months', "startmonth", $form1->startdate);
$yearselector = html_writer::select_time('years', "startyear", $form1->startdate);

echo $OUTPUT->select($dayselector) . $OUTPUT->select($monthselector) . $OUTPUT->select($yearselector);
echo $dayselector . $monthselector . $yearselector;
echo $OUTPUT->help_icon("coursestartdate", get_string("startdate"));
} else {
print_string('notavailable');
Expand Down
6 changes: 3 additions & 3 deletions enrol/authorize/config_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@
<td align="right">an_cutoff:</td>
<td><?php
$curtime = make_timestamp(2000,1,1,$frm->an_cutoff_hour,$frm->an_cutoff_min);
$hourselector = html_select::make_time_selector('hours', 'an_cutoff_hour', $curtime);
$minselector = html_select::make_time_selector('minutes', 'an_cutoff_min', $curtime);
echo $OUTPUT->select($hourselector) . $OUTPUT->select($minselector);
$hourselector = html_writer::select_time('hours', 'an_cutoff_hour', $curtime);
$minselector = html_writer::select_time('minutes', 'an_cutoff_min', $curtime);
echo $hourselector . $minselector;
?><br />
<?php print_string("cutofftime", "enrol_authorize") ?></td>
</tr>
Expand Down
18 changes: 8 additions & 10 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3233,11 +3233,11 @@ function print_timer_selector($timelimit = 0, $unit = '', $name = 'timelimit', $
*/
function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=false) {
debugging('print_time_selector() has been deprecated. Please change your code to use $OUTPUT->select($timeselector).');
global $OUTPUT;
$hourselector = html_select::make_time_selector('hours', $hour, $currenttime);
$minuteselector = html_select::make_time_selector('minutes', $minute, $currenttime, $step);

$output = $OUTPUT->select($hourselector) . $OUTPUT->select($minuteselector);
$hourselector = html_writer::select_time('hours', $hour, $currenttime);
$minuteselector = html_writer::select_time('minutes', $minute, $currenttime, $step);

$output = $hourselector . $$minuteselector;

if ($return) {
return $output;
Expand All @@ -3259,15 +3259,13 @@ function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=fa
* @return string|bool Depending on value of $return
*/
function print_date_selector($day, $month, $year, $currenttime=0, $return=false) {

debugging('print_date_selector() has been deprecated. Please change your code to use $OUTPUT->select($dateselector).');
global $OUTPUT;

$dayselector = html_select::make_time_selector('days', $day, $currenttime);
$monthselector = html_select::make_time_selector('months', $month, $currenttime);
$yearselector = html_select::make_time_selector('years', $year, $currenttime);
$dayselector = html_writer::select_time('days', $day, $currenttime);
$monthselector = html_writer::select_time('months', $month, $currenttime);
$yearselector = html_writer::select_time('years', $year, $currenttime);

$output = $OUTPUT->select($dayselector) . $OUTPUT->select($monthselector) . $OUTPUT->select($yearselector);
$output = $dayselector . $monthselector . $yearselector;

if ($return) {
return $output;
Expand Down
145 changes: 64 additions & 81 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,70 @@ private static function select_optgroup($groupname, $options, array $selected) {
return self::tag('optgroup', $attributes, $output);
}

/**
* This is a shortcut for making an hour selector menu.
* @param string $type The type of selector (years, months, days, hours, minutes)
* @param string $name fieldname
* @param int $currenttime A default timestamp in GMT
* @param int $step minute spacing
* @param array $attributes - html select element attributes
* @return HTML fragment
*/
public static function select_time($type, $name, $currenttime=0, $step=5, array $attributes=null) {
if (!$currenttime) {
$currenttime = time();
}
$currentdate = usergetdate($currenttime);
$userdatetype = $type;
$timeunits = array();

switch ($type) {
case 'years':
for ($i=1970; $i<=2020; $i++) {
$timeunits[$i] = $i;
}
$userdatetype = 'year';
break;
case 'months':
for ($i=1; $i<=12; $i++) {
$timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
}
$userdatetype = 'month';
$currentdate['month'] = $currentdate['mon'];
break;
case 'days':
for ($i=1; $i<=31; $i++) {
$timeunits[$i] = $i;
}
$userdatetype = 'mday';
break;
case 'hours':
for ($i=0; $i<=23; $i++) {
$timeunits[$i] = sprintf("%02d",$i);
}
break;
case 'minutes':
if ($step != 1) {
$currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
}

for ($i=0; $i<=59; $i+=$step) {
$timeunits[$i] = sprintf("%02d",$i);
}
break;
default:
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, array('id'=>$attributes['id']));
$label = self::tag('label', array('for'=>$attributes['id'], 'class'=>'accesshide'), get_string(substr($type, 0, -1), 'form'));

return $label.$timerselector;
}

/**
* Returns hidden input fields created from url parameters.
* @param moodle_url $url
Expand Down Expand Up @@ -1374,87 +1438,6 @@ public static function make($options, $name, $selected = '', $nothinglabel='choo
return $menu;
}

/**
* This is a shortcut for making an hour selector menu.
* @param string $type The type of selector (years, months, days, hours, minutes)
* @param string $name fieldname
* @param int $currenttime A default timestamp in GMT
* @param int $step minute spacing
* @return html_select A menu initialised with hour options.
*/
public static function make_time_selector($type, $name, $currenttime=0, $step=5) {

if (!$currenttime) {
$currenttime = time();
}
$currentdate = usergetdate($currenttime);
$userdatetype = $type;

switch ($type) {
case 'years':
for ($i=1970; $i<=2020; $i++) {
$timeunits[$i] = $i;
}
$userdatetype = 'year';
break;
case 'months':
for ($i=1; $i<=12; $i++) {
$timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
}
$userdatetype = 'month';
$currentdate['month'] = $currentdate['mon'];
break;
case 'days':
for ($i=1; $i<=31; $i++) {
$timeunits[$i] = $i;
}
$userdatetype = 'mday';
break;
case 'hours':
for ($i=0; $i<=23; $i++) {
$timeunits[$i] = sprintf("%02d",$i);
}
break;
case 'minutes':
if ($step != 1) {
$currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
}

for ($i=0; $i<=59; $i+=$step) {
$timeunits[$i] = sprintf("%02d",$i);
}
break;
default:
throw new coding_exception("Time type $type is not supported by html_select::make_time_selector().");
}

$timerselector = self::make($timeunits, $name, $currentdate[$userdatetype]);
$timerselector->label = new html_label();

$timerselector->label->text = get_string(substr($type, 0, -1), 'form');
$timerselector->label->for = "menu$timerselector->name";
$timerselector->label->add_class('accesshide');
$timerselector->nothinglabel = '';

return $timerselector;
}

/**
* Given an associative array of type => fieldname and an optional timestamp,
* returns an array of html_select components representing date/time selectors.
* @param array $selectors Arrays of type => fieldname. Selectors will be returned in the order of the types given
* @param int $currenttime A UNIX timestamp
* @param int $step minute spacing
* @return array Instantiated date/time selectors
*/
public static function make_time_selectors($selectors, $currenttime=0, $step=5) {
$selects = array();
foreach ($selectors as $type => $name) {
$selects[] = html_select::make_time_selector($type, $name, $currenttime, $step);
}
return $selects;
}

/**
* Adds a help icon next to the select menu.
*
Expand Down
8 changes: 4 additions & 4 deletions mod/data/field/date/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ function display_add_field($recordid=0) {
}

$str = '<div title="'.s($this->field->description).'">';
$dayselector = html_select::make_time_selector('days', 'field_'.$this->field->id.'_day', $content);
$monthselector = html_select::make_time_selector('months', 'field_'.$this->field->id.'_month', $content);
$yearselector = html_select::make_time_selector('years', 'field_'.$this->field->id.'_year', $content);
$str .= $OUTPUT->select($dayselector) . $OUTPUT->select($monthselector) . $OUTPUT->select($yearselector);
$dayselector = html_writer::select_time('days', 'field_'.$this->field->id.'_day', $content);
$monthselector = html_writer::select_time('months', 'field_'.$this->field->id.'_month', $content);
$yearselector = html_writer::select_time('years', 'field_'.$this->field->id.'_year', $content);
$str .= $dayselector . $monthselector . $yearselector;
$str .= '</div>';

return $str;
Expand Down
20 changes: 12 additions & 8 deletions mod/forum/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ function forum_print_big_search_form($course) {
}

echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
$selectors = html_select::make_time_selectors(array('days' => 'fromday','months' => 'frommonth', 'years' => 'fromyear', 'hours' => 'fromhour', 'minutes' => 'fromminute'), $datefrom);
foreach ($selectors as $select) {
echo $OUTPUT->select($select);
}
$selectors = html_writer::select_time('days', 'fromday', $datefrom)
. html_writer::select_time('months', 'frommonth', $datefrom)
. html_writer::select_time('years', 'fromyear', $datefrom)
. html_writer::select_time('hours', 'fromhour', $datefrom)
. html_writer::select_time('minutes', 'fromminute', $datefrom);
echo $selectors;
echo '<input type="hidden" name="hfromday" value="0" />';
echo '<input type="hidden" name="hfrommonth" value="0" />';
echo '<input type="hidden" name="hfromyear" value="0" />';
Expand All @@ -334,10 +336,12 @@ function forum_print_big_search_form($course) {
}

echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
$selectors = html_select::make_time_selectors(array('days' => 'today','months' => 'tomonth', 'years' => 'toyear', 'hours' => 'tohour', 'minutes' => 'tominute'), $dateto);
foreach ($selectors as $select) {
echo $OUTPUT->select($select);
}
$selectors = html_writer::select_time('days', 'fromday', $dateto)
. html_writer::select_time('months', 'frommonth', $dateto)
. html_writer::select_time('years', 'fromyear', $dateto)
. html_writer::select_time('hours', 'fromhour', $dateto)
. html_writer::select_time('minutes', 'fromminute', $dateto);
echo $selectors;

echo '<input type="hidden" name="htoday" value="0" />';
echo '<input type="hidden" name="htomonth" value="0" />';
Expand Down

0 comments on commit f83b9b6

Please sign in to comment.