Browse Source

Put timezone into the session see BT#15743

In order to improve performance when treating date conversions.
Julio Montoya 5 years ago
parent
commit
c93a451a56
3 changed files with 32 additions and 27 deletions
  1. 4 0
      main/admin/user_edit.php
  2. 5 8
      main/auth/profile.php
  3. 23 19
      main/inc/lib/internationalization.lib.php

+ 4 - 0
main/admin/user_edit.php

@@ -1,6 +1,8 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use ChamiloSession as Session;
+
 /**
  * @package chamilo.admin
  */
@@ -489,6 +491,8 @@ if ($form->validate()) {
             api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
         );
 
+        Session::erase('system_timezone');
+
         Display::addFlash(Display::return_message($message, 'normal', false));
         header('Location: user_list.php');
         exit();

+ 5 - 8
main/auth/profile.php

@@ -26,7 +26,6 @@ $logInfo = [
     'tool_id' => 0,
     'tool_id_detail' => 0,
     'action' => $this_section,
-    'info' => '',
 ];
 Event::registerLog($logInfo);
 
@@ -651,21 +650,19 @@ if ($form->validate()) {
     Session::write('_user', $userInfo);
 
     if ($hook) {
-        Database::getManager()->clear(User::class); //Avoid cache issue (user entity is used before)
-
-        $user = api_get_user_entity(api_get_user_id()); //Get updated user info for hook event
-
+        Database::getManager()->clear(User::class); // Avoid cache issue (user entity is used before)
+        $user = api_get_user_entity(api_get_user_id()); // Get updated user info for hook event
         $hook->setEventData(['user' => $user]);
         $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST);
     }
 
+    Session::erase('system_timezone');
+
     $url = api_get_self();
-    header("Location: ".$url);
+    header("Location: $url");
     exit;
 }
 
-// the header
-
 $actions = '';
 if ($allowSocialTool) {
     if (api_get_setting('extended_profile') === 'true') {

+ 23 - 19
main/inc/lib/internationalization.lib.php

@@ -2,6 +2,7 @@
 /* For licensing terms, see /license.txt */
 
 use Patchwork\Utf8;
+use ChamiloSession as Session;
 
 /**
  * File: internationalization.lib.php
@@ -386,30 +387,33 @@ function api_get_timezones()
  */
 function api_get_timezone()
 {
-    // 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');
+    $timezone = Session::read('system_timezone');
+    if (empty($timezone)) {
+        // First, get the default timezone of the server
+        $timezone = date_default_timezone_get();
+        // Second, see if a timezone has been chosen for the platform
+        $timezoneFromSettings = api_get_setting('timezone_value', 'timezones');
 
-    if ($timezone_value != null) {
-        $to_timezone = $timezone_value;
-    }
-    // If allowed by the administrator
-    $use_users_timezone = api_get_setting('use_users_timezone', 'timezones');
+        if ($timezoneFromSettings != null) {
+            $timezone = $timezoneFromSettings;
+        }
 
-    if ($use_users_timezone === 'true') {
-        $userId = api_get_user_id();
-        // Get the timezone based on user preference, if it exists
-        $timezone_user = UserManager::get_extra_user_data_by_field(
-            $userId,
-            'timezone'
-        );
-        if (isset($timezone_user['timezone']) && $timezone_user['timezone'] != null) {
-            $to_timezone = $timezone_user['timezone'];
+        // If allowed by the administrator
+        $allowUserTimezones = api_get_setting('use_users_timezone', 'timezones');
+
+        if ($allowUserTimezones === 'true') {
+            $userId = api_get_user_id();
+            // Get the timezone based on user preference, if it exists
+            $newExtraField = new ExtraFieldValue('user');
+            $data = $newExtraField->get_values_by_handler_and_field_variable($userId, 'timezone');
+            if (!empty($data) && isset($data['timezone']) && !empty($data['timezone'])) {
+                $timezone = $data['timezone'];
+            }
         }
+        Session::write('system_timezone', $timezone);
     }
 
-    return $to_timezone;
+    return $timezone;
 }
 
 /**