/** * Initalize javascript for date type form element * * @staticvar bool $done make sure it gets initalize once. * @global moodle_page $PAGE */ function form_init_date_js() { global $PAGE; static $done = false; if (!$done) { $calendar = \core_calendar\type_factory::get_calendar_instance(); $module = 'moodle-form-dateselector'; $function = 'M.form.dateselector.init_date_selectors'; $config = array(array('firstdayofweek' => $calendar->get_starting_weekday(), 'mon' => date_format_string(strtotime("Monday"), '%a', 99), 'tue' => date_format_string(strtotime("Tuesday"), '%a', 99), 'wed' => date_format_string(strtotime("Wednesday"), '%a', 99), 'thu' => date_format_string(strtotime("Thursday"), '%a', 99), 'fri' => date_format_string(strtotime("Friday"), '%a', 99), 'sat' => date_format_string(strtotime("Saturday"), '%a', 99), 'sun' => date_format_string(strtotime("Sunday"), '%a', 99), 'january' => date_format_string(strtotime("January 1"), '%B', 99), 'february' => date_format_string(strtotime("February 1"), '%B', 99), 'march' => date_format_string(strtotime("March 1"), '%B', 99), 'april' => date_format_string(strtotime("April 1"), '%B', 99), 'may' => date_format_string(strtotime("May 1"), '%B', 99), 'june' => date_format_string(strtotime("June 1"), '%B', 99), 'july' => date_format_string(strtotime("July 1"), '%B', 99), 'august' => date_format_string(strtotime("August 1"), '%B', 99), 'september' => date_format_string(strtotime("September 1"), '%B', 99), 'october' => date_format_string(strtotime("October 1"), '%B', 99), 'november' => date_format_string(strtotime("November 1"), '%B', 99), 'december' => date_format_string(strtotime("December 1"), '%B', 99))); $PAGE->requires->yui_module($module, $function, $config); $done = true; } }
/** * Handles editing datetime fields. * * @param moodleform $mform */ public function edit_field_add($mform) { // Get the current calendar in use - see MDL-18375. $calendartype = \core_calendar\type_factory::get_calendar_instance(); // Check if the field is required. if ($this->field->required) { $optional = false; } else { $optional = true; } // Convert the year stored in the DB as gregorian to that used by the calendar type. $startdate = $calendartype->convert_from_gregorian($this->field->param1, 1, 1); $stopdate = $calendartype->convert_from_gregorian($this->field->param2, 1, 1); $attributes = array('startyear' => $startdate['year'], 'stopyear' => $stopdate['year'], 'optional' => $optional); // Check if they wanted to include time as well. if (!empty($this->field->param3)) { $mform->addElement('date_time_selector', $this->inputname, format_string($this->field->name), $attributes); } else { $mform->addElement('date_selector', $this->inputname, format_string($this->field->name), $attributes); } $mform->setType($this->inputname, PARAM_INT); $mform->setDefault($this->inputname, time()); }
/** * Test converting dates from unixtime to a date for the calendar type specified. * * @param string $type the calendar type we want to test * @param array $date the date variables */ private function convert_unixtime_to_dateselector_test($type, $date) { $this->set_calendar_type($type); // Get the calendar. $calendar = \core_calendar\type_factory::get_calendar_instance(); $usergetdate = $calendar->timestamp_to_date_array($date['timestamp'], 0.0); $comparedate = array('minute' => $usergetdate['minutes'], 'hour' => $usergetdate['hours'], 'day' => $usergetdate['mday'], 'month' => $usergetdate['mon'], 'year' => $usergetdate['year'], 'timestamp' => $date['timestamp']); $this->assertEquals($comparedate, $date); }
/** * Displays a month in detail * * @param calendar_information $calendar * @param moodle_url $returnurl the url to return to * @return string */ public function show_month_detailed(calendar_information $calendar, moodle_url $returnurl = null) { global $CFG; if (empty($returnurl)) { $returnurl = $this->page->url; } // Get the calendar type we are using. $calendartype = \core_calendar\type_factory::get_calendar_instance(); // Store the display settings. $display = new stdClass(); $display->thismonth = false; // Get the specified date in the calendar type being used. $date = $calendartype->timestamp_to_date_array($calendar->time); $thisdate = $calendartype->timestamp_to_date_array(time()); if ($date['mon'] == $thisdate['mon'] && $date['year'] == $thisdate['year']) { $display->thismonth = true; $date = $thisdate; $calendar->time = time(); } // Get Gregorian date for the start of the month. $gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1); // Store the gregorian date values to be used later. list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], $gregoriandate['hour'], $gregoriandate['minute']); // Get the starting week day for this month. $startwday = dayofweek(1, $date['mon'], $date['year']); // Get the days in a week. $daynames = calendar_get_days(); // Store the number of days in a week. $numberofdaysinweek = $calendartype->get_num_weekdays(); $display->minwday = calendar_get_starting_weekday(); $display->maxwday = $display->minwday + ($numberofdaysinweek - 1); $display->maxdays = calendar_days_in_month($date['mon'], $date['year']); // These are used for DB queries, so we want unixtime, so we need to use Gregorian dates. $display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0); $display->tend = $display->tstart + $display->maxdays * DAYSECS - 1; // Align the starting weekday to fall in our display range // This is simple, not foolproof. if ($startwday < $display->minwday) { $startwday += $numberofdaysinweek; } // Get events from database $events = calendar_get_events($display->tstart, $display->tend, $calendar->users, $calendar->groups, $calendar->courses); if (!empty($events)) { foreach ($events as $eventid => $event) { $event = new calendar_event($event); if (!empty($event->modulename)) { $cm = get_coursemodule_from_instance($event->modulename, $event->instance); if (!\core_availability\info_module::is_user_visible($cm, 0, false)) { unset($events[$eventid]); } } } } // Extract information: events vs. time calendar_events_by_day($events, $date['mon'], $date['year'], $eventsbyday, $durationbyday, $typesbyday, $calendar->courses); $output = html_writer::start_tag('div', array('class' => 'header')); $output .= $this->course_filter_selector($returnurl, get_string('detailedmonthviewfor', 'calendar')); if (calendar_user_can_add_event($calendar->course)) { $output .= $this->add_event_button($calendar->course->id, 0, 0, 0, $calendar->time); } $output .= html_writer::end_tag('div', array('class' => 'header')); // Controls $output .= html_writer::tag('div', calendar_top_controls('month', array('id' => $calendar->courseid, 'time' => $calendar->time)), array('class' => 'controls')); $table = new html_table(); $table->attributes = array('class' => 'calendarmonth calendartable'); $table->summary = get_string('calendarheading', 'calendar', userdate($calendar->time, get_string('strftimemonthyear'))); $table->data = array(); // Get the day names as the header. $header = array(); for ($i = $display->minwday; $i <= $display->maxwday; ++$i) { $header[] = $daynames[$i % $numberofdaysinweek]['shortname']; } $table->head = $header; // For the table display. $week is the row; $dayweek is the column. $week = 1; $dayweek = $startwday; $row = new html_table_row(array()); // Paddding (the first week may have blank days in the beginning) for ($i = $display->minwday; $i < $startwday; ++$i) { $cell = new html_table_cell(' '); $cell->attributes = array('class' => 'nottoday dayblank'); $row->cells[] = $cell; } // Now display all the calendar $weekend = CALENDAR_DEFAULT_WEEKEND; if (isset($CFG->calendar_weekend)) { $weekend = intval($CFG->calendar_weekend); } $daytime = $display->tstart - DAYSECS; for ($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) { $daytime = $daytime + DAYSECS; if ($dayweek > $display->maxwday) { // We need to change week (table row) $table->data[] = $row; $row = new html_table_row(array()); $dayweek = $display->minwday; ++$week; } // Reset vars $cell = new html_table_cell(); $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', array('view' => 'day', 'course' => $calendar->courseid)), 0, 0, 0, $daytime); $cellclasses = array(); if ($weekend & 1 << $dayweek % $numberofdaysinweek) { // Weekend. This is true no matter what the exact range is. $cellclasses[] = 'weekend'; } // Special visual fx if an event is defined if (isset($eventsbyday[$day])) { if (count($eventsbyday[$day]) == 1) { $title = get_string('oneevent', 'calendar'); } else { $title = get_string('manyevents', 'calendar', count($eventsbyday[$day])); } $cell->text = html_writer::tag('div', html_writer::link($dayhref, $day, array('title' => $title)), array('class' => 'day')); } else { $cell->text = html_writer::tag('div', $day, array('class' => 'day')); } // Special visual fx if an event spans many days $durationclass = false; if (isset($typesbyday[$day]['durationglobal'])) { $durationclass = 'duration_global'; } else { if (isset($typesbyday[$day]['durationcourse'])) { $durationclass = 'duration_course'; } else { if (isset($typesbyday[$day]['durationgroup'])) { $durationclass = 'duration_group'; } else { if (isset($typesbyday[$day]['durationuser'])) { $durationclass = 'duration_user'; } } } } if ($durationclass) { $cellclasses[] = 'duration'; $cellclasses[] = $durationclass; } // Special visual fx for today if ($display->thismonth && $day == $date['mday']) { $cellclasses[] = 'day today'; } else { $cellclasses[] = 'day nottoday'; } $cell->attributes = array('class' => join(' ', $cellclasses)); if (isset($eventsbyday[$day])) { $cell->text .= html_writer::start_tag('ul', array('class' => 'events-new')); foreach ($eventsbyday[$day] as $eventindex) { // If event has a class set then add it to the event <li> tag $attributes = array(); if (!empty($events[$eventindex]->class)) { $attributes['class'] = $events[$eventindex]->class; } $dayhref->set_anchor('event_' . $events[$eventindex]->id); $link = html_writer::link($dayhref, format_string($events[$eventindex]->name, true)); $cell->text .= html_writer::tag('li', $link, $attributes); } $cell->text .= html_writer::end_tag('ul'); } if (isset($durationbyday[$day])) { $cell->text .= html_writer::start_tag('ul', array('class' => 'events-underway')); foreach ($durationbyday[$day] as $eventindex) { $cell->text .= html_writer::tag('li', '[' . format_string($events[$eventindex]->name, true) . ']', array('class' => 'events-underway')); } $cell->text .= html_writer::end_tag('ul'); } $row->cells[] = $cell; } // Paddding (the last week may have blank days at the end) for ($i = $dayweek; $i <= $display->maxwday; ++$i) { $cell = new html_table_cell(' '); $cell->attributes = array('class' => 'nottoday dayblank'); $row->cells[] = $cell; } $table->data[] = $row; $output .= html_writer::table($table); return $output; }
/** * Get per-day basis events * * @param array $events list of events * @param int $month the number of the month * @param int $year the number of the year * @param array $eventsbyday event on specific day * @param array $durationbyday duration of the event in days * @param array $typesbyday event type (eg: global, course, user, or group) * @param array $courses list of courses * @return void */ function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) { // Get the calendar type we are using. $calendartype = \core_calendar\type_factory::get_calendar_instance(); $eventsbyday = array(); $typesbyday = array(); $durationbyday = array(); if ($events === false) { return; } foreach ($events as $event) { $startdate = $calendartype->timestamp_to_date_array($event->timestart); // Set end date = start date if no duration if ($event->timeduration) { $enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1); } else { $enddate = $startdate; } // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair if (!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && $enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month) { // Out of bounds continue; } $eventdaystart = intval($startdate['mday']); if ($startdate['mon'] == $month && $startdate['year'] == $year) { // Give the event to its day $eventsbyday[$eventdaystart][] = $event->id; // Mark the day as having such an event if ($event->courseid == SITEID && $event->groupid == 0) { $typesbyday[$eventdaystart]['startglobal'] = true; // Set event class for global event $events[$event->id]->class = 'calendar_event_global'; } else { if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { $typesbyday[$eventdaystart]['startcourse'] = true; // Set event class for course event $events[$event->id]->class = 'calendar_event_course'; } else { if ($event->groupid) { $typesbyday[$eventdaystart]['startgroup'] = true; // Set event class for group event $events[$event->id]->class = 'calendar_event_group'; } else { if ($event->userid) { $typesbyday[$eventdaystart]['startuser'] = true; // Set event class for user event $events[$event->id]->class = 'calendar_event_user'; } } } } } if ($event->timeduration == 0) { // Proceed with the next continue; } // The event starts on $month $year or before. So... $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0; // Also, it ends on $month $year or later... $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year); // Mark all days between $lowerbound and $upperbound (inclusive) as duration for ($i = $lowerbound + 1; $i <= $upperbound; ++$i) { $durationbyday[$i][] = $event->id; if ($event->courseid == SITEID && $event->groupid == 0) { $typesbyday[$i]['durationglobal'] = true; } else { if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { $typesbyday[$i]['durationcourse'] = true; } else { if ($event->groupid) { $typesbyday[$i]['durationgroup'] = true; } else { if ($event->userid) { $typesbyday[$i]['durationuser'] = true; } } } } } } return; }
/** * Calculate the position in the week of a specific calendar day * * @package core * @category time * @param int $day The day of the date whose position in the week is sought * @param int $month The month of the date whose position in the week is sought * @param int $year The year of the date whose position in the week is sought * @return int */ function dayofweek($day, $month, $year) { $calendartype = \core_calendar\type_factory::get_calendar_instance(); return $calendartype->get_weekday($year, $month, $day); }
protected function get_javascript_init_params($course, \cm_info $cm = null, \section_info $section = null) { global $CFG, $OUTPUT; require_once $CFG->libdir . '/formslib.php'; // Support internationalised calendars. $calendartype = \core_calendar\type_factory::get_calendar_instance(); // Get current date, but set time to 00:00 (to make it easier to // specify whole days) and change name of mday field to match below. $wrongfields = $calendartype->timestamp_to_date_array(time()); $current = array('day' => $wrongfields['mday'], 'month' => $wrongfields['mon'], 'year' => $wrongfields['year'], 'hour' => 0, 'minute' => 0); // Time part is handled the same everywhere. $hours = array(); for ($i = 0; $i <= 23; $i++) { $hours[$i] = sprintf("%02d", $i); } $minutes = array(); for ($i = 0; $i < 60; $i += 5) { $minutes[$i] = sprintf("%02d", $i); } // List date fields. $fields = $calendartype->get_date_order($calendartype->get_min_year(), $calendartype->get_max_year()); // Add time fields - in RTL mode these are switched. $fields['split'] = '/'; if (right_to_left()) { $fields['minute'] = $minutes; $fields['colon'] = ':'; $fields['hour'] = $hours; } else { $fields['hour'] = $hours; $fields['colon'] = ':'; $fields['minute'] = $minutes; } // Output all date fields. $html = '<span class="availability-group">'; foreach ($fields as $field => $options) { if ($options === '/') { $html = rtrim($html); // In Gregorian calendar mode only, we support a date selector popup, reusing // code from form to ensure consistency. if ($calendartype->get_name() === 'gregorian' && self::DATE_SELECTOR_SUPPORTED) { $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle'); $html .= ' ' . \html_writer::link('#', $image, array('name' => 'x[calendar]')); form_init_date_js(); } $html .= '</span> <span class="availability-group">'; continue; } if ($options === ':') { $html .= ': '; continue; } $html .= \html_writer::start_tag('label'); $html .= \html_writer::span(get_string($field) . ' ', 'accesshide'); // NOTE: The fields need to have these weird names in order that they // match the standard Moodle form control, otherwise the date selector // won't find them. $html .= \html_writer::start_tag('select', array('name' => 'x[' . $field . ']', 'class' => 'custom-select')); foreach ($options as $key => $value) { $params = array('value' => $key); if ($current[$field] == $key) { $params['selected'] = 'selected'; } $html .= \html_writer::tag('option', s($value), $params); } $html .= \html_writer::end_tag('select'); $html .= \html_writer::end_tag('label'); $html .= ' '; } $html = rtrim($html) . '</span>'; // Also get the time that corresponds to this default date. $time = self::get_time_from_fields($current['year'], $current['month'], $current['day'], $current['hour'], $current['minute']); return array($html, $time); }
function update_content($recordid, $value, $name='') { global $DB; $names = explode('_',$name); $name = $names[2]; // day month or year $this->$name = $value; if ($this->day and $this->month and $this->year) { // All of them have been collected now $content = new stdClass(); $content->fieldid = $this->field->id; $content->recordid = $recordid; $calendartype = \core_calendar\type_factory::get_calendar_instance(); $gregoriandate = $calendartype->convert_to_gregorian($this->year, $this->month, $this->day); $content->content = make_timestamp( $gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], $gregoriandate['hour'], $gregoriandate['minute'], 0, 0, false); if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { $content->id = $oldcontent->id; return $DB->update_record('data_content', $content); } else { return $DB->insert_record('data_content', $content); } } }
/** * Output a timestamp. Give it the name of the group. * * @param array $submitValues values submitted. * @param bool $assoc specifies if returned array is associative * @return array */ function exportValue(&$submitValues, $assoc = false) { $value = null; $valuearray = array(); foreach ($this->_elements as $element) { $thisexport = $element->exportValue($submitValues[$this->getName()], true); if ($thisexport != null) { $valuearray += $thisexport; } } if (count($valuearray)) { if ($this->_options['optional']) { // If checkbox is on, the value is zero, so go no further if (empty($valuearray['enabled'])) { $value[$this->getName()] = 0; return $value; } } // Get the calendar type used - see MDL-18375. $calendartype = \core_calendar\type_factory::get_calendar_instance(); $gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'], $valuearray['month'], $valuearray['day']); $value[$this->getName()] = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 0, 0, 0, $this->_options['timezone'], true); return $value; } else { return null; } }
/** * Preprocess data from the profile field form before * it is saved. * * @param stdClass $data from the add/edit profile field form * @return stdClass processed data object */ public function define_save_preprocess($data) { // Get the current calendar in use - see MDL-18375. $calendartype = \core_calendar\type_factory::get_calendar_instance(); // Check if the start year was changed, if it was then convert from the start of that year. if ($data->param1 != $data->startyear) { $startdate = $calendartype->convert_to_gregorian($data->param1, 1, 1); } else { $startdate = $calendartype->convert_to_gregorian($data->param1, $data->startmonth, $data->startday); } // Check if the end year was changed, if it was then convert from the start of that year. if ($data->param2 != $data->endyear) { $stopdate = $calendartype->convert_to_gregorian($data->param2, 1, 1); } else { $stopdate = $calendartype->convert_to_gregorian($data->param2, $data->endmonth, $data->endday); } $data->param1 = $startdate['year']; $data->param2 = $stopdate['year']; if (empty($data->param3)) { $data->param3 = null; } // No valid value in the default data column needed. $data->defaultdata = '0'; return $data; }
$calendar->prepare_for_view($course, $courses); $pagetitle = get_string('export', 'calendar'); // Print title and header if ($issite) { $PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id' => $course->id))); } $link = new moodle_url(CALENDAR_URL . 'view.php', array('view' => 'upcoming', 'course' => $calendar->courseid)); $PAGE->navbar->add(get_string('calendar', 'calendar'), calendar_get_link_href($link, 0, 0, 0, $time)); $PAGE->navbar->add($pagetitle); $PAGE->set_title($course->shortname . ': ' . get_string('calendar', 'calendar') . ': ' . $pagetitle); $PAGE->set_heading($course->fullname); $PAGE->set_pagelayout('standard'); $renderer = $PAGE->get_renderer('core_calendar'); $calendar->add_sidecalendar_blocks($renderer); // Get the calendar type we are using. $calendartype = \core_calendar\type_factory::get_calendar_instance(); $now = $calendartype->timestamp_to_date_array($time); $weekend = CALENDAR_DEFAULT_WEEKEND; if (isset($CFG->calendar_weekend)) { $weekend = intval($CFG->calendar_weekend); } $numberofdaysinweek = $calendartype->get_num_weekdays(); $formdata = array('allownextweek' => $weekend & 1 << $now['wday'], 'allownextmonth' => calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek, 'allowthisweek' => !($weekend & 1 << $now['wday'] && !($weekend & 1 << ($now['wday'] + 1) % $numberofdaysinweek))); $exportform = new core_calendar_export_form(null, $formdata); $calendarurl = ''; if ($data = $exportform->get_data()) { $password = $DB->get_record('user', array('id' => $USER->id), 'password'); $params = array(); $params['userid'] = $USER->id; $params['authtoken'] = sha1($USER->id . (isset($password->password) ? $password->password : '') . $CFG->calendar_exportsalt); $params['preset_what'] = $data->events['exportevents'];
/** * 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(); } $calendartype = \core_calendar\type_factory::get_calendar_instance(); $currentdate = $calendartype->timestamp_to_date_array($currenttime); $userdatetype = $type; $timeunits = array(); switch ($type) { case 'years': $timeunits = $calendartype->get_years(); $userdatetype = 'year'; break; case 'months': $timeunits = $calendartype->get_months(); $userdatetype = 'month'; $currentdate['month'] = (int) $currentdate['mon']; break; case 'days': $timeunits = $calendartype->get_days(); $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, $attributes); $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for' => $attributes['id'], 'class' => 'accesshide')); return $label . $timerselector; }
/** * 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) { global $OUTPUT; if (!$currenttime) { $currenttime = time(); } $calendartype = \core_calendar\type_factory::get_calendar_instance(); $currentdate = $calendartype->timestamp_to_date_array($currenttime); $userdatetype = $type; $timeunits = array(); switch ($type) { case 'years': $timeunits = $calendartype->get_years(); $userdatetype = 'year'; break; case 'months': $timeunits = $calendartype->get_months(); $userdatetype = 'month'; $currentdate['month'] = (int)$currentdate['mon']; break; case 'days': $timeunits = $calendartype->get_days(); $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()."); } $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 $OUTPUT->render_from_template('core/select_time', $data); }
/** * Given a $time timestamp in GMT (seconds since epoch), returns an array that represents * the date in user time. * * @param int $time timestamp in GMT * @param float|int|string $timezone the timezone to use to calculate the time * {@link http://docs.moodle.org/dev/Time_API#Timezone} * @return array an array that represents the date in user time */ public function timestamp_to_date_array($time, $timezone = 99) { $gregoriancalendar = \core_calendar\type_factory::get_calendar_instance('gregorian'); $date = $gregoriancalendar->timestamp_to_date_array($time, $timezone); $newdate = $this->convert_from_gregorian($date['year'], $date['mon'], $date['mday'], $date['hours'], $date['minutes']); $date['year'] = $newdate['year']; $date['mon'] = $newdate['month']; $date['mday'] = $newdate['day']; $date['hours'] = $newdate['hour']; $date['minutes'] = $newdate['minute']; return $date; }
/** * */ protected function get_timestamp_from_value($value) { if (empty($value)) { return null; } $timestamp = null; // Timestamp or time string. if (!is_array($value)) { if ((string) (int) $value === (string) $value && $value <= PHP_INT_MAX && $value >= ~PHP_INT_MAX) { // It's a timestamp. $timestamp = $value; } else { if ($value = strtotime($value)) { // It's a valid time string. $timestamp = $value; } } } else { // Assuming any of year, month, day, hour, minute is passed. $enabled = 0; $year = 0; $month = 1; $day = 1; $hour = 0; $minute = 0; foreach ($value as $name => $val) { if (!empty($val)) { ${$name} = $val; } } if ($enabled and $year) { $calendartype = \core_calendar\type_factory::get_calendar_instance(); $gregoriandate = $calendartype->convert_to_gregorian($year, $month, $day, $hour, $minute); $timestamp = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], $gregoriandate['hour'], $gregoriandate['minute']); } } return $timestamp; }