diff --git a/admin/webservice/service_users.php b/admin/webservice/service_users.php index bf9b8798f84..47d74b7a67f 100644 --- a/admin/webservice/service_users.php +++ b/admin/webservice/service_users.php @@ -170,7 +170,9 @@ if (!empty($allowedusers)) { //valid until date selector $contents .= "
"; // 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; diff --git a/backup/restore_form.html b/backup/restore_form.html index d8e1d75a97d..04ed06dd76e 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -298,11 +298,11 @@ function selectItemInCheckboxByName(formId, checkName, checked ) { /// (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'); diff --git a/enrol/authorize/config_form.php b/enrol/authorize/config_form.php index 6da095ef041..5df5b6b3d18 100755 --- a/enrol/authorize/config_form.php +++ b/enrol/authorize/config_form.php @@ -113,9 +113,9 @@ if (!isset($frm->acceptechecktypes)) { an_cutoff: 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; ?>
diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 4d02058495c..7d4b053e872 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -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; @@ -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; diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index b6cce339300..b87a104e37b 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -788,6 +788,70 @@ class html_writer { 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 @@ -1374,87 +1438,6 @@ class html_select extends labelled_html_component { 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. * diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 597f07ea796..6f0dc211c8e 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -44,10 +44,10 @@ class data_field_date extends data_field_base { } $str = '
'; - $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 .= '
'; return $str; diff --git a/mod/forum/search.php b/mod/forum/search.php index 6484fb4a1fe..7d4871efbf3 100644 --- a/mod/forum/search.php +++ b/mod/forum/search.php @@ -310,10 +310,12 @@ function forum_print_big_search_form($course) { } echo ' '; - $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 ''; echo ''; echo ''; @@ -334,10 +336,12 @@ function forum_print_big_search_form($course) { } echo ' '; - $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 ''; echo '';