MDL-69283 webservice: New external setting for forcing timezone
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 27 Jul 2020 17:15:13 +0000 (19:15 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 25 Sep 2020 17:08:59 +0000 (19:08 +0200)
lib/externallib.php
webservice/lib.php

index 7f33390..9a4a50e 100644 (file)
@@ -1217,6 +1217,9 @@ class external_settings {
     /** @var string The session lang */
     private $lang = '';
 
+    /** @var string The timezone to use during this WS request */
+    private $timezone = '';
+
     /**
      * Constructor - protected - can not be instanciated
      */
@@ -1337,6 +1340,24 @@ class external_settings {
     public function get_lang() {
         return $this->lang;
     }
+
+    /**
+     * Set timezone
+     *
+     * @param string $timezone
+     */
+    public function set_timezone($timezone) {
+        $this->timezone = $timezone;
+    }
+
+    /**
+     * Get timezone
+     *
+     * @return string
+     */
+    public function get_timezone() {
+        return $this->timezone;
+    }
 }
 
 /**
index fe6c061..5257640 100644 (file)
@@ -1160,6 +1160,7 @@ abstract class webservice_server implements webservice_server_interface {
             'fileurl' => array('default' => true, 'type' => PARAM_BOOL),
             'filter' => array('default' => false, 'type' => PARAM_BOOL),
             'lang' => array('default' => '', 'type' => PARAM_LANG),
+            'timezone' => array('default' => '', 'type' => PARAM_TIMEZONE),
         );
 
         // Load the external settings with the web service settings.
@@ -1235,7 +1236,7 @@ abstract class webservice_base_server extends webservice_server {
      * @uses die
      */
     public function run() {
-        global $CFG, $SESSION;
+        global $CFG, $USER, $SESSION;
 
         // we will probably need a lot of memory in some functions
         raise_memory_limit(MEMORY_EXTRA);
@@ -1287,6 +1288,12 @@ abstract class webservice_base_server extends webservice_server {
             }
         }
 
+        // Change timezone only in sites where it isn't forced.
+        $newtimezone = $settings->get_timezone();
+        if (!empty($newtimezone) && (!isset($CFG->forcetimezone) || $CFG->forcetimezone == 99)) {
+            $USER->timezone = $newtimezone;
+        }
+
         // finally, execute the function - any errors are catched by the default exception handler
         $this->execute();