MDL-68076 core: webservice to record feedback actions users take
authorShamim Rezaie <shamim@moodle.com>
Thu, 30 Apr 2020 05:39:06 +0000 (15:39 +1000)
committerShamim Rezaie <shamim@moodle.com>
Wed, 3 Jun 2020 07:43:52 +0000 (17:43 +1000)
lib/classes/external/userfeedback/record_action.php [new file with mode: 0644]
lib/db/services.php
lib/tests/external/userfeedback/record_action_test.php [new file with mode: 0644]
version.php

diff --git a/lib/classes/external/userfeedback/record_action.php b/lib/classes/external/userfeedback/record_action.php
new file mode 100644 (file)
index 0000000..26f79fe
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * External API to record users action on the feedback notification.
+ *
+ * @package    core
+ * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\external\userfeedback;
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once("$CFG->libdir/externallib.php");
+
+use external_api;
+use external_function_parameters;
+use external_value;
+
+/**
+ * The external API to record users action on the feedback notification.
+ *
+ * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class record_action extends external_api {
+    /**
+     * Returns description of parameters.
+     *
+     * @return external_function_parameters
+     */
+    public static function execute_parameters() {
+        return new external_function_parameters([
+            'action' => new external_value(PARAM_ALPHA, 'The action taken by user'),
+        ]);
+    }
+
+    /**
+     * Record users action to the feedback CTA
+     *
+     * @param string $action The action the user took
+     * @throws \invalid_parameter_exception
+     */
+    public static function execute(string $action) {
+        external_api::validate_parameters(self::execute_parameters(), ['action' => $action]);
+
+        switch ($action) {
+            case 'give':
+                set_user_preference('core_userfeedback_give', time());
+                break;
+            case 'remind':
+                set_user_preference('core_userfeedback_remind', time());
+                break;
+            default:
+                throw new \invalid_parameter_exception('Invalid value for action parameter (value: ' . $action . '),' .
+                        'allowed values are: give,remind');
+        }
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return null
+     */
+    public static function execute_returns() {
+        return null;
+    }
+}
index e8c1350..3a1521d 100644 (file)
@@ -2800,6 +2800,15 @@ $functions = array(
         'ajax'          => 'true',
         'capabilities'  => 'moodle/contentbank:manageowncontent',
     ],
+    'core_create_userfeedback_action_record' => [
+        'classname'     => 'core\external\userfeedback\record_action',
+        'methodname'    => 'execute',
+        'classpath'     => '',
+        'description'   => 'Record the action that the user takes in the user feedback notification for future use.',
+        'type'          => 'write',
+        'ajax'          => 'true',
+        'capabilities'  => '',
+    ],
 );
 
 $services = array(
diff --git a/lib/tests/external/userfeedback/record_action_test.php b/lib/tests/external/userfeedback/record_action_test.php
new file mode 100644 (file)
index 0000000..610acad
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * External functions test for record_action.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\external\userfeedback;
+
+defined('MOODLE_INTERNAL') || die();
+
+use externallib_advanced_testcase;
+use context_system;
+
+global $CFG;
+
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+
+/**
+ * Class record_action_testcase
+ *
+ * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass record_action
+ */
+class record_action_testcase extends externallib_advanced_testcase {
+
+    /**
+     * Data provider for test_record_action.
+     *
+     * @return  array
+     */
+    public function record_action_provider() {
+        return [
+            'give action' => ['give'],
+            'remind action' => ['remind'],
+        ];
+    }
+
+    /**
+     * Test the behaviour of record_action().
+     *
+     * @dataProvider record_action_provider
+     * @param string $action The action taken by the user
+     *
+     * @covers ::execute
+     */
+    public function test_record_action(string $action) {
+        $this->resetAfterTest();
+
+        $context = context_system::instance();
+        $user = $this->getDataGenerator()->create_user();
+        $this->setUser($user);
+
+        $now = time();
+
+        // Call the WS and check the action is recorded as expected.
+        $result = record_action::execute($action, $context->id);
+        $this->assertNull($result);
+
+        $preference = get_user_preferences('core_userfeedback_' . $action);
+        $this->assertGreaterThanOrEqual($now, $preference);
+    }
+}
index 32f956c..17fac9b 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020052900.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2020052900.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '3.9dev+ (Build: 20200529)'; // Human-friendly version name