From e529f97ae9528c588947a45a735837a7b11266ee Mon Sep 17 00:00:00 2001 From: Guillaume Viguier Date: Tue, 16 Feb 2010 18:15:08 -0500 Subject: [PATCH] Implemented api_get_local_time in many chamilo tools - First pass (see CT#599) --- main/admin/settings.php | 1 + main/admin/user_edit.php | 2 + main/calendar/agenda.inc.php | 24 +++++----- main/document/document.php | 1 + main/dropbox/index.php | 4 +- main/exercice/exercice.php | 5 +- main/forum/index.php | 2 +- main/forum/viewforum.php | 2 +- main/forum/viewthread_flat.inc.php | 2 +- main/forum/viewthread_nested.inc.php | 2 +- main/forum/viewthread_threaded.inc.php | 2 +- .../gradebook/lib/fe/gradebooktable.class.php | 2 +- .../lib/gradebook_data_generator.class.php | 2 +- main/inc/lib/blog.lib.php | 4 +- main/inc/lib/glossary.lib.php | 6 ++- main/inc/lib/main_api.lib.php | 48 +++++++++++++++++++ main/mySpace/access_details.php | 3 ++ main/work/work.php | 12 +++-- 18 files changed, 93 insertions(+), 31 deletions(-) diff --git a/main/admin/settings.php b/main/admin/settings.php index 7b543484ac..e92e0487e2 100755 --- a/main/admin/settings.php +++ b/main/admin/settings.php @@ -261,6 +261,7 @@ if (!empty($_GET['category']) && !in_array($_GET['category'], array('Plugins', ' */ case "select": $form->addElement('select', $row['variable'], get_lang($row['comment']), call_user_func('select_'.$row['variable']), $hideme); + $default_values[$row['variable']] = $row['selected_value']; break; } } diff --git a/main/admin/user_edit.php b/main/admin/user_edit.php index 3b1e4cef1f..7ac2a62822 100755 --- a/main/admin/user_edit.php +++ b/main/admin/user_edit.php @@ -82,6 +82,8 @@ $user_data = Database::fetch_array($res, 'ASSOC'); $user_data['platform_admin'] = is_null($user_data['is_admin']) ? 0 : 1; $user_data['send_mail'] = 0; $user_data['old_password'] = $user_data['password']; +//Convert the registration date of the user +$user_data['registration_date'] = api_get_local_time($user_data['registration_date'], null, null, date_default_timezone_get()); unset($user_data['password']); $user_data = array_merge($user_data, Usermanager :: get_extra_user_data($user_id, true)); diff --git a/main/calendar/agenda.inc.php b/main/calendar/agenda.inc.php index eabe784d41..b1e337a16d 100755 --- a/main/calendar/agenda.inc.php +++ b/main/calendar/agenda.inc.php @@ -443,8 +443,8 @@ function display_monthcalendar($month, $year) foreach ($agenda_item as $key=>$value) { $month_start_date = (int)substr($value['start_date'],5,2); if ($month == $month_start_date) { - $start_time= date("H:i",strtotime($value['start_date'])); - $end_time= date("H:i",strtotime($value['end_date'])); + $start_time = api_get_local_time($value['start_date'], "H:i", null, date_default_timezone_get()); + $end_time = api_get_local_time($value['end_date'], "H:i", null, date_default_timezone_get()); if ($value['end_date']=='0000-00-00 00:00:00'){ $dayheader .= '
'.get_lang("Work").'
'; @@ -2139,14 +2139,14 @@ function display_agenda_items() --------------------------------------------------*/ echo ""; echo "\t\t".get_lang("StartTimeWindow").": "; - echo api_ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."   "; - echo api_ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"]))).""; + echo api_ucfirst(api_get_local_time($myrow['start_date'], $dateFormatLong, null, date_default_timezone_get()))."   "; + echo api_ucfirst(api_get_local_time($myrow['start_date'], $timeNoSecFormat, null, date_default_timezone_get())).""; echo "\n"; echo "\t\t"; if ($myrow["end_date"]<>"0000-00-00 00:00:00") { echo get_lang("EndTimeWindow").": "; - echo api_ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."   "; - echo api_ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"]))).""; + echo api_ucfirst(api_get_local_time($myrow['end_date'], $dateFormatLong, null, date_default_timezone_get()))."   "; + echo api_ucfirst(api_get_local_time($myrow['end_date'], $timeNoSecFormat, null, date_default_timezone_get())).""; } echo "\n"; @@ -2432,12 +2432,12 @@ function display_one_agenda_item($agenda_id) --------------------------------------------------*/ echo "\t\n"; echo "\t\t".get_lang("StartTime").": "; - echo api_ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."   "; - echo api_ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"]))).""; + echo api_ucfirst(api_get_local_time($myrow['start_date'], $dateFormatLong, null, date_default_timezone_get()))."   "; + echo api_ucfirst(api_get_local_time($myrow['start_date'], $timeNoSecFormat, null, date_default_timezone_get())).""; echo "\n"; echo "\t\t".get_lang("EndTime").": "; - echo api_ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."   "; - echo api_ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"]))).""; + echo api_ucfirst(api_get_local_time($myrow['end_date'], $dateFormatLong, null, date_default_timezone_get()))."   "; + echo api_ucfirst(api_get_local_time($myrow['end_date'], $timeNoSecFormat, null, date_default_timezone_get())).""; echo "\n"; /*-------------------------------------------------- @@ -3118,7 +3118,7 @@ function get_agendaitems($month, $year) while ($item = Database::fetch_array($result)) { $agendaday = date('j',strtotime($item['start_date'])); - $time= date('H:i',strtotime($item['start_date'])); + $time = api_get_local_time($item['start_date'], 'H:i', null, date_default_timezone_get()); $URL = $root_url.'main/calendar/agenda.php?cidReq='.$mycourse['id']."&day=$agendaday&month=$month&year=$year#$agendaday"; // RH //Patrick Cool: to highlight the relevant agenda item $items[$agendaday][$item['start_time']] .= ''.$time.' '.$mycourse['official_code'].' '.$item['title'].'
'; } @@ -3603,7 +3603,7 @@ function get_week_agendaitems($courses_dbs, $month, $year, $week = '') while ($item = Database::fetch_array($result)) { $agendaday = date("j",strtotime($item['start_date'])); - $time= date("H:i",strtotime($item['start_date'])); + $time = api_get_local_time($item['start_date'], "H:i", null, date_default_timezone_get()); if ($setting_agenda_link == 'coursecode') { diff --git a/main/document/document.php b/main/document/document.php index b624be3894..2d824de3e6 100755 --- a/main/document/document.php +++ b/main/document/document.php @@ -803,6 +803,7 @@ if(isset($docs_and_folders) && is_array($docs_and_folders)) //last edit date $last_edit_date=$id['lastedit_date']; + $last_edit_date=api_get_local_time($last_edit_date, null, null, date_default_timezone_get()); $display_date = date_to_str_ago($last_edit_date).'
'.$last_edit_date.''; $row[] = $invisibility_span_open.$display_date.$invisibility_span_close.''; diff --git a/main/dropbox/index.php b/main/dropbox/index.php index f016222e39..4d1b16978f 100755 --- a/main/dropbox/index.php +++ b/main/dropbox/index.php @@ -462,7 +462,7 @@ if ($_GET['view']=='received' OR $dropbox_cnf['sent_received_tabs']==false) { $dropbox_file_data[]=$dropbox_file->author; //$dropbox_file_data[]=$dropbox_file->description; - $dropbox_file_data[]=date_to_str_ago($dropbox_file->last_upload_date).'
'.$dropbox_file->last_upload_date.''; + $dropbox_file_data[]=date_to_str_ago(api_get_local_time($dropbox_file->last_upload_date, null, null, date_default_timezone_get())).'
'.api_get_local_time($dropbox_file->last_upload_date, null, null, date_default_timezone_get()).''; $action_icons=check_number_feedback($dropbox_file->id, $number_feedback).' '.get_lang('Feedback').' '.Display::return_icon('comment_bubble.gif',get_lang('Comment')).' @@ -617,7 +617,7 @@ if (!$_GET['view'] OR $_GET['view']=='sent' OR $dropbox_cnf['sent_received_tabs' } $receivers_celldata = trim(trim($receivers_celldata), ','); // Removing the trailing comma. $dropbox_file_data[]=$receivers_celldata; - $dropbox_file_data[]=date_to_str_ago($dropbox_file->last_upload_date).'
'.$dropbox_file->last_upload_date.''; + $dropbox_file_data[]=date_to_str_ago(api_get_local_time($dropbox_file->last_upload_date, null, null, date_default_timezone_get())).'
'.api_get_local_time($dropbox_file->last_upload_date, null, null, date_default_timezone_get()).''; //$dropbox_file_data[]=$dropbox_file->author; $receivers_celldata=''; diff --git a/main/exercice/exercice.php b/main/exercice/exercice.php index d9efb5eee4..ffb52fb9e7 100755 --- a/main/exercice/exercice.php +++ b/main/exercice/exercice.php @@ -1268,7 +1268,8 @@ if ($_configuration['tracking_enabled'] && ($show == 'result')) { } //echo ''; //echo '' . $add_start_date . format_locale_date('%b %d, %Y %H:%M', $results[$i][4]) . ''; //get_lang('dateTimeFormatLong') - $date_list = $add_start_date . format_locale_date('%b %d, %Y %H:%M', $results[$i][4]); + // Date conversion + $date_list = api_get_local_time((int)$results[$i][7], null, null, date_default_timezone_get()). ' / ' . api_get_local_time((int)$results[$i][4], null, null, date_default_timezone_get()); // there are already a duration test period calculated?? //echo ''.sprintf(get_lang('DurationFormat'), $duration).''; @@ -1410,4 +1411,4 @@ if ($origin != 'learnpath') { //so we are not in learnpath tool \ No newline at end of file +?> diff --git a/main/forum/index.php b/main/forum/index.php index c205667f81..34cc5d2d93 100755 --- a/main/forum/index.php +++ b/main/forum/index.php @@ -422,7 +422,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'notify' AND isset($_GET['conte echo "\t\t"; if (!empty($forum['last_post_id'])) { - echo $forum['last_post_date']."
".get_lang('By').' '.display_user_link($poster_id, $name); + echo api_get_local_time($forum['last_post_date'], null, null, date_default_timezone_get())."
".get_lang('By').' '.display_user_link($poster_id, $name); } echo "\n"; echo "\t\t"; diff --git a/main/forum/viewforum.php b/main/forum/viewforum.php index d3a6af7dc6..bc43ede9ce 100755 --- a/main/forum/viewforum.php +++ b/main/forum/viewforum.php @@ -430,7 +430,7 @@ if(is_array($threads)) { // if the last post is invisible and it is not the teacher who is looking then we have to find the last visible post of the thread if (($row['visible']=='1' OR api_is_allowed_to_edit(false,true)) && $origin!='learnpath') { - $last_post=$row['thread_date']." ".get_lang('By').' '.display_user_link($row['last_poster_user_id'], $name); + $last_post=api_get_local_time($row['thread_date'], null, null, date_default_timezone_get())." ".get_lang('By').' '.display_user_link($row['last_poster_user_id'], $name); } elseif ($origin!='learnpath') { $last_post_sql="SELECT post.*, user.firstname, user.lastname FROM $table_posts post, $table_users user WHERE post.poster_id=user.user_id AND visible='1' AND thread_id='".$row['thread_id']."' ORDER BY post_id DESC"; $last_post_result=Database::query($last_post_sql); diff --git a/main/forum/viewthread_flat.inc.php b/main/forum/viewthread_flat.inc.php index d11072dcc0..521ef544d9 100755 --- a/main/forum/viewthread_flat.inc.php +++ b/main/forum/viewthread_flat.inc.php @@ -60,7 +60,7 @@ foreach ($rows as $row) { } else { echo $name. '
'; } - echo $row['post_date'].'

'; + echo api_get_local_time($row['post_date'], null, null, date_default_timezone_get()).'

'; // get attach id $attachment_list=get_attachment($row['post_id']); $id_attach = !empty($attachment_list)?$attachment_list['id']:''; diff --git a/main/forum/viewthread_nested.inc.php b/main/forum/viewthread_nested.inc.php index f97428e5b4..32dccddae5 100755 --- a/main/forum/viewthread_nested.inc.php +++ b/main/forum/viewthread_nested.inc.php @@ -69,7 +69,7 @@ foreach ($rows as $post) { echo '
'.display_user_image($post['user_id'],$name,$origin).'
'; } echo display_user_link($post['user_id'], $name, $origin).'
'; - echo $post['post_date'].'

'; + echo api_get_local_time($post['post_date'], null, null, date_default_timezone_get()).'

'; // get attach id $attachment_list=get_attachment($post['post_id']); $id_attach = !empty($attachment_list)?$attachment_list['id']:''; diff --git a/main/forum/viewthread_threaded.inc.php b/main/forum/viewthread_threaded.inc.php index 268eb1ae12..e4cd1ea7d2 100755 --- a/main/forum/viewthread_threaded.inc.php +++ b/main/forum/viewthread_threaded.inc.php @@ -216,7 +216,7 @@ else } if (api_get_course_setting('allow_user_image_forum')) {echo '
'.display_user_image($rows[$display_post_id]['user_id'],$name, $origin).'
'; } echo display_user_link($rows[$display_post_id]['user_id'], $name, $origin).'
'; -echo $rows[$display_post_id]['post_date'].'

'; +echo api_get_local_time($rows[$display_post_id]['post_date'], null, null, date_default_timezone_get()).'

'; // get attach id $attachment_list=get_attachment($display_post_id); $id_attach = !empty($attachment_list)?$attachment_list['id']:''; diff --git a/main/gradebook/lib/fe/gradebooktable.class.php b/main/gradebook/lib/fe/gradebooktable.class.php index 97a912eb71..ea7092dfdf 100755 --- a/main/gradebook/lib/fe/gradebooktable.class.php +++ b/main/gradebook/lib/fe/gradebooktable.class.php @@ -337,4 +337,4 @@ private function build_id_column ($item) { } } -} \ No newline at end of file +} diff --git a/main/gradebook/lib/gradebook_data_generator.class.php b/main/gradebook/lib/gradebook_data_generator.class.php index f485ad9663..063a17b464 100755 --- a/main/gradebook/lib/gradebook_data_generator.class.php +++ b/main/gradebook/lib/gradebook_data_generator.class.php @@ -217,7 +217,7 @@ class GradebookDataGenerator if (!isset($date) || empty($date)) { return ''; } else { - return date("j/n/Y g:i", $date); + return api_get_local_time((int)$date, null, null, date_default_timezone_get()); } } } diff --git a/main/inc/lib/blog.lib.php b/main/inc/lib/blog.lib.php index 300429bfc3..d9bb942bb7 100755 --- a/main/inc/lib/blog.lib.php +++ b/main/inc/lib/blog.lib.php @@ -734,8 +734,8 @@ class Blog { // Prepare data $blog_post_id = $blog_post['post_id']; $blog_post_text = make_clickable(stripslashes($blog_post['full_text'])); - $blog_post_date = api_ucfirst(format_locale_date($dateFormatLong,strtotime($blog_post['date_creation']))); - $blog_post_time = date('H:i',strtotime($blog_post['date_creation'])); + $blog_post_date = api_get_local_time($blog_post['date_creation'], $dateFormatLong, null, date_default_timezone_get()); + $blog_post_time = api_get_local_time($blog_post['date_creation'], 'H:i', null, date_default_timezone_get()); // Create an introduction text (but keep FULL sentences) $limit = 100; //nmbr of words in introduction text diff --git a/main/inc/lib/glossary.lib.php b/main/inc/lib/glossary.lib.php index e781984118..5eaa450ce3 100755 --- a/main/inc/lib/glossary.lib.php +++ b/main/inc/lib/glossary.lib.php @@ -379,6 +379,10 @@ class GlossaryManager { if (api_is_allowed_to_edit(null,true)) { $array[5] = $data[5]; } + + // Date treatment for timezones + $array[3] = api_get_local_time($array[3], null, null, date_default_timezone_get()); + $array[4] = api_get_local_time($array[4], null, null, date_default_timezone_get()); $return[] = $array; } @@ -529,4 +533,4 @@ class GlossaryManager { Display::display_confirmation_message(get_lang('TermMoved')); } } -?> \ No newline at end of file +?> diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index 32e9748784..8e54d6692b 100755 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -4467,3 +4467,51 @@ function api_send_mail($to, $subject, $message, $additional_headers = null, $add $mail->ClearAddresses(); return 1; } + +/** + * Returns the local time in a format given as an argument + * @param string The time to be converted + * @param string The format to be used. The default format is DATETIME + * @param string The timezone to be converted to. If null, the timezone will be determined based on user preference, or timezone chosen by the admin for the platform. + * @param string The timezone to be converted from. If null, UTC will be assumed. + * @return string The converted time + * + * @author Guillaume Viguier + */ +function api_get_local_time($time, $format=null, $to_timezone=null, $from_timezone=null) { + // Determining the timezone to be converted from + if ($from_timezone === null) { + $from_timezone = 'UTC'; + } + // Determining the timezone to be converted to + if ($to_timezone === null) { + // First, get the default timezone of the server + $to_timezone = date_default_timezone_get(); + // Second, see if a timezone has been chosen for the platform + $timezone_value = api_get_setting('timezone_value', 'timezones'); + if ($timezone_value !== null) { + $to_timezone = $timezone_value; + } + // TODO: Get the timezone based on user preference + } + // Determine the format + if ($format === null) { + $format = 'Y-m-d H:i:s'; + } + // If time is a timestamp, convert it to a string + if (is_int($time)) { + $time = date("Y-m-d H:i:s", $time); + } + try { + $date = new DateTime($time, new DateTimezone($from_timezone)); + $date->setTimezone(new DateTimeZone($to_timezone)); + // In the following case, the format is an internal Chamilo format, so we are using api_format_date + if (is_int($format) || strpos($format, '%') !== false) { + return api_format_date($format, strtotime($date->format("Y-m-d H:i:s"))); + } else { + return $date->format($format); + } + } catch (Exception $e) { + return null; + } +} diff --git a/main/mySpace/access_details.php b/main/mySpace/access_details.php index 454271758a..a4e8c76941 100755 --- a/main/mySpace/access_details.php +++ b/main/mySpace/access_details.php @@ -255,6 +255,9 @@ function get_connections_to_course($user_id, $course_code) { $login_date = $row['login_course_date']; $logout_date = $row['logout_course_date']; + + $login_date = api_get_local_time($login_date, null, null, date_default_timezone_get()); + $logout_date = api_get_local_time($logout_date, null, null, date_default_timezone_get()); $timestamp_login_date = strtotime($login_date); $timestamp_logout_date = strtotime($logout_date); diff --git a/main/work/work.php b/main/work/work.php index b720daa6cd..530c2bed88 100755 --- a/main/work/work.php +++ b/main/work/work.php @@ -1194,10 +1194,12 @@ if($is_special > 0): if (!$not_ends_on) { define('ASSIGNMENT_EXPIRES',$time_expires); } - $ends_on = api_ucfirst(format_locale_date($dateFormatLong,strtotime($homework['ends_on']))).' '; - $ends_on .= ucfirst(strftime($timeNoSecFormat,strtotime($homework['ends_on']))); - $expires_on = api_ucfirst(format_locale_date($dateFormatLong,strtotime($homework['expires_on']))).' '; - $expires_on .= ucfirst(strftime($timeNoSecFormat,strtotime($homework['expires_on']))); + $ends_on_datetime = api_get_local_time($homework['ends_on'], null, null, date_default_timezone_get()); + $ends_on = api_ucfirst(format_locale_date($dateFormatLong,strtotime($ends_on_datetime))).' '; + $ends_on .= ucfirst(strftime($timeNoSecFormat,strtotime($ends_on_datetime))); + $expires_on_datetime = api_get_local_time($homework['expires_on'], null, null, date_default_timezone_get()); + $expires_on = api_ucfirst(format_locale_date($dateFormatLong,strtotime($expires_on_datetime))).' '; + $expires_on .= ucfirst(strftime($timeNoSecFormat,strtotime($expires_on_datetime))); if($has_ended) { display_action_links($cur_dir_path, $always_show_tool_options,true); Display :: display_error_message(get_lang('EndDateAlreadyPassed').' '.$ends_on); @@ -1560,4 +1562,4 @@ if (!$display_upload_form && !$display_tool_options) { if ($origin != 'learnpath') { //we are not in the learning path tool Display :: display_footer(); -} \ No newline at end of file +}