From e95cf005232fcdde05dde9420cf765125b75f88e Mon Sep 17 00:00:00 2001
From: Paul Holden <paulh@moodle.com>
Date: Fri, 22 Mar 2024 20:55:46 +0000
Subject: [PATCH] MDL-81399 reportbuilder: add language/timezone reporting for
 users.

---
 reportbuilder/classes/local/entities/user.php | 21 +++++++++++++
 .../reportbuilder/datasource/users_test.php   | 30 +++++++++++++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/reportbuilder/classes/local/entities/user.php b/reportbuilder/classes/local/entities/user.php
index 3dfa5fca683..4e1c112992e 100644
--- a/reportbuilder/classes/local/entities/user.php
+++ b/reportbuilder/classes/local/entities/user.php
@@ -23,6 +23,7 @@ use context_system;
 use context_user;
 use core\context;
 use core_component;
+use core_date;
 use html_writer;
 use lang_string;
 use moodle_url;
@@ -429,6 +430,8 @@ class user extends base {
             'email' => new lang_string('email'),
             'city' => new lang_string('city'),
             'country' => new lang_string('country'),
+            'lang' => new lang_string('language'),
+            'timezone' => new lang_string('timezone'),
             'theme' => new lang_string('theme'),
             'description' => new lang_string('description'),
             'firstnamephonetic' => new lang_string('firstnamephonetic'),
@@ -596,6 +599,24 @@ class user extends base {
         return get_string_manager()->get_list_of_countries();
     }
 
+    /**
+     * List of options for the field lang.
+     *
+     * @return string[]
+     */
+    public static function get_options_for_lang(): array {
+        return get_string_manager()->get_list_of_translations();
+    }
+
+    /**
+     * List of options for the field timezone.
+     *
+     * @return string[]
+     */
+    public static function get_options_for_timezone(): array {
+        return core_date::get_list_of_timezones(null, true);
+    }
+
     /**
      * List of options for the field theme.
      *
diff --git a/user/tests/reportbuilder/datasource/users_test.php b/user/tests/reportbuilder/datasource/users_test.php
index 29e6d45a374..46bcb06f9e8 100644
--- a/user/tests/reportbuilder/datasource/users_test.php
+++ b/user/tests/reportbuilder/datasource/users_test.php
@@ -40,7 +40,7 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
  * @copyright   2022 Paul Holden <paulh@moodle.com>
  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class users_test extends core_reportbuilder_testcase {
+final class users_test extends core_reportbuilder_testcase {
 
     /**
      * Test default datasource
@@ -77,6 +77,8 @@ class users_test extends core_reportbuilder_testcase {
             'idnumber' => 'U0001',
             'city' => 'London',
             'country' => 'GB',
+            'lang' => 'en',
+            'timezone' => 'Europe/London',
             'theme' => 'boost',
             'interests' => ['Horses'],
         ]);
@@ -98,6 +100,8 @@ class users_test extends core_reportbuilder_testcase {
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
+        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lang']);
+        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:timezone']);
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:description']);
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstnamephonetic']);
         $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastnamephonetic']);
@@ -155,6 +159,8 @@ class users_test extends core_reportbuilder_testcase {
             $lastname,
             $city,
             $country,
+            $lang,
+            $timezone,
             $description,
             $firstnamephonetic,
             $lastnamephonetic,
@@ -187,6 +193,8 @@ class users_test extends core_reportbuilder_testcase {
         $this->assertEquals($user->lastname, $lastname);
         $this->assertEquals($user->city, $city);
         $this->assertEquals('United Kingdom', $country);
+        $this->assertEquals('English ‎(en)‎', $lang);
+        $this->assertEquals('Europe/London', $timezone);
         $this->assertEquals($user->description, $description);
         $this->assertEquals($user->firstnamephonetic, $firstnamephonetic);
         $this->assertEquals($user->lastnamephonetic, $lastnamephonetic);
@@ -216,7 +224,7 @@ class users_test extends core_reportbuilder_testcase {
      *
      * @return array[]
      */
-    public function datasource_filters_provider(): array {
+    public static function datasource_filters_provider(): array {
         return [
             // User.
             'Filter user' => ['user:userselect', [
@@ -327,6 +335,22 @@ class users_test extends core_reportbuilder_testcase {
                 'user:country_operator' => select::EQUAL_TO,
                 'user:country_value' => 'AU',
             ], false],
+            'Filter lang' => ['user:lang', [
+                'user:lang_operator' => select::EQUAL_TO,
+                'user:lang_value' => 'en',
+            ], true],
+            'Filter lang (no match)' => ['user:lang', [
+                'user:lang_operator' => select::EQUAL_TO,
+                'user:lang_value' => 'de',
+            ], false],
+            'Filter timezone' => ['user:timezone', [
+                'user:timezone_operator' => select::EQUAL_TO,
+                'user:timezone_value' => 'Europe/Barcelona',
+            ], true],
+            'Filter timezone (no match)' => ['user:timezone', [
+                'user:timezone_operator' => select::EQUAL_TO,
+                'user:timezone_value' => 'Australia/Perth',
+            ], false],
             'Filter theme' => ['user:theme', [
                 'user:theme_operator' => select::EQUAL_TO,
                 'user:theme_value' => 'boost',
@@ -487,6 +511,8 @@ class users_test extends core_reportbuilder_testcase {
             'address' => 'Big Farm',
             'city' => 'Barcelona',
             'country' => 'ES',
+            'lang' => 'en',
+            'timezone' => 'Europe/Barcelona',
             'theme' => 'boost',
             'description' => 'Hello there',
             'moodlenetprofile' => '@zoe1@example.com',