From d5d9bf29b14843a5e07110b2f247e284b430ae1a Mon Sep 17 00:00:00 2001
From: Ryan Smith <0ryansmith1994@gmail.com>
Date: Fri, 6 Jul 2018 14:09:00 +0100
Subject: [PATCH] feat: Adds setting for sending by username to close #35.

---
 classes/log/store.php              |  1 +
 lang/en/logstore_xapi.php          |  6 ++++--
 settings.php                       |  4 ++++
 src/transformer/utils/get_user.php | 17 ++++++++++++++---
 tests/xapi_test_case.php           |  1 +
 5 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/classes/log/store.php b/classes/log/store.php
index e81423b..9d43e15 100755
--- a/classes/log/store.php
+++ b/classes/log/store.php
@@ -96,6 +96,7 @@ class store extends php_obj implements log_writer {
                 'source_version' => $CFG->release,
                 'source_lang' => 'en',
                 'send_mbox' => $this->get_config('mbox', false),
+                'send_username' => $this->get_config('username', false),
                 'plugin_url' => 'https://github.com/xAPI-vle/moodle-logstore_xapi',
                 'plugin_version' => $plugin->release,
                 'repo' => new \src\transformer\repos\MoodleRepository($DB),
diff --git a/lang/en/logstore_xapi.php b/lang/en/logstore_xapi.php
index aa25668..a003dd1 100644
--- a/lang/en/logstore_xapi.php
+++ b/lang/en/logstore_xapi.php
@@ -39,5 +39,7 @@ $string['routes'] = 'Include actions with these routes';
 $string['filters'] = 'Filter logs';
 $string['logguests'] = 'Log guest actions';
 $string['filters_help'] = 'Enable filters that INCLUDE some actions to be logged.';
-$string['mbox'] = 'Send user email';
-$string['mbox_desc'] = 'Statements identify the user with its email (mbox) or with its userid on the moodle platform (account). Checking this will send the email (mbox).';
\ No newline at end of file
+$string['mbox'] = 'Identify users by email';
+$string['mbox_desc'] = 'Statements will identify users with their email (mbox) when this box is ticked.';
+$string['username'] = 'Identify users by id';
+$string['username_desc'] = 'Statements will identify users with their username when this box is ticked, but only if identifying users by email is disabled.';
\ No newline at end of file
diff --git a/settings.php b/settings.php
index dec48e6..a8dd420 100644
--- a/settings.php
+++ b/settings.php
@@ -42,6 +42,10 @@ if ($hassiteconfig) {
     $settings->add(new admin_setting_configcheckbox('logstore_xapi/mbox',
         get_string('mbox', 'logstore_xapi'),
         get_string('mbox_desc', 'logstore_xapi'), 0));
+    
+    $settings->add(new admin_setting_configcheckbox('logstore_xapi/username',
+        get_string('username', 'logstore_xapi'),
+        get_string('username_desc', 'logstore_xapi'), 0));
 
     // Filters.
     $settings->add(new admin_setting_heading('filters',
diff --git a/src/transformer/utils/get_user.php b/src/transformer/utils/get_user.php
index fa4d7aa..ac8aea9 100644
--- a/src/transformer/utils/get_user.php
+++ b/src/transformer/utils/get_user.php
@@ -19,18 +19,29 @@ defined('MOODLE_INTERNAL') || die();
 
 function get_user(array $config, \stdClass $user) {
     $fullname = get_full_name($user);
-    if (array_key_exists('sendmbox', $config) && $config['sendmbox'] == true) {
+
+    if (array_key_exists('send_mbox', $config) && $config['send_mbox'] == true) {
         return [
             'name' => $fullname,
             'mbox' => $user->email,
         ];
-    } else {
+    }
+
+    if (array_key_exists('send_username', $config) && $config['send_username'] === true) {
         return [
             'name' => $fullname,
             'account' => [
                 'homePage' => $config['app_url'],
-                'name' => strval($user->id),
+                'name' => $user->username,
             ],
         ];
     }
+
+    return [
+        'name' => $fullname,
+        'account' => [
+            'homePage' => $config['app_url'],
+            'name' => strval($user->id),
+        ],
+    ];
 }
diff --git a/tests/xapi_test_case.php b/tests/xapi_test_case.php
index 1b1c21b..6883e5b 100644
--- a/tests/xapi_test_case.php
+++ b/tests/xapi_test_case.php
@@ -75,6 +75,7 @@ abstract class xapi_test_case extends PhpUnitTestCase {
             'source_version' => '1.0.0',
             'source_lang' => 'en',
             'send_mbox' => false,
+            'send_username' => false,
             'plugin_url' => 'https://github.com/xAPI-vle/moodle-logstore_xapi',
             'plugin_version' => '0.0.0-development',
             'repo' => new \src\transformer\repos\TestRepository($testdata),
-- 
GitLab