From 7be1f69b005ed7ef8085c14a77153f9ebe2f11a4 Mon Sep 17 00:00:00 2001
From: Benjamin Ledel <benjamin@schule-plus.com>
Date: Mon, 27 Mar 2023 22:06:35 +0200
Subject: [PATCH] * first draft working

---
 README.md                           | 13 +++++--
 classes/external/collect_groups.php | 55 +++++++++++++++++------------
 db/access.php                       | 10 ++++++
 db/services.php                     | 16 ++-------
 lang/en/local_polaris_gateway.php   |  1 +
 5 files changed, 58 insertions(+), 37 deletions(-)
 create mode 100644 db/access.php

diff --git a/README.md b/README.md
index 041e143..050edf1 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,10 @@
 # Polaris Gateway #
 
-TODO Describe the plugin shortly here.
+The plugin is a gateway for the polaris infrastructure, espaclliy the rights engine. 
+We provide an external web service to access the required data.
 
-TODO Provide more detailed description here.
+Finally, you should open the following page and see the course structure the user with the email USER_EMAIL.
+https://HOST/webservice/rest/server.php?wstoken={GENERATED_TOKEN}&wsfunction=local_polaris_gateway_collect_groups&moodlewsrestformat=json&user_email={USER_EMAIL}
 
 ## Installing via uploaded ZIP file ##
 
@@ -27,6 +29,13 @@ Alternatively, you can run
 
 to complete the installation from the command line.
 
+## Configuration ##
+
+1. Activiate the web services in moodle
+2. Create a service user
+3. Allow the service user to use the Polaris Gateway service
+4. Generate a token and add the url to the provider schema
+
 ## License ##
 
 2023 Digital Learning GmbH <support@digitallearning.gmbh>
diff --git a/classes/external/collect_groups.php b/classes/external/collect_groups.php
index 804ab12..11a9b31 100644
--- a/classes/external/collect_groups.php
+++ b/classes/external/collect_groups.php
@@ -1,12 +1,13 @@
 <?php
 namespace local_polaris_gateway\external;
 
+use context_system;
 use external_function_parameters;
 use external_multiple_structure;
 use external_single_structure;
 use external_value;
 
-class create_groups extends \core_external\external_api {
+class collect_groups extends \external_api {
 
     /**
      * Returns description of method parameters
@@ -14,35 +15,45 @@ class create_groups extends \core_external\external_api {
      */
     public static function execute_parameters() {
         return new external_function_parameters([
-            'groups' => new external_multiple_structure(
-                new external_single_structure([
-                    'courseid' => new external_value(PARAM_INT, 'id of course'),
-                    'name' => new external_value(
-                        PARAM_TEXT,
-                        'multilang compatible name, course unique'
-                    ),
-                    'description' => new external_value(
-                        PARAM_RAW,
-                        'group description text'
-                    ),
-                    'enrolmentkey' => new external_value(
-                        PARAM_RAW,
-                        'group enrol secret phrase'
-                    ),
-                ])
-            )
+            'user_email' =>  new external_value(PARAM_TEXT, 'email of the user')
         ]);
     }
 
     public static function execute_returns() {
         return new external_multiple_structure(
             new external_single_structure([
-                'id' => new external_value(PARAM_INT, 'group record id'),
-                'courseid' => new external_value(PARAM_INT, 'id of course'),
+                'group_id' => new external_value(PARAM_INT, 'id of course'),
                 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
-                'description' => new external_value(PARAM_RAW, 'group description text'),
-                'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
+                'description' => new external_value(PARAM_TEXT, 'group description text'),
             ])
         );
     }
+
+    public static function execute($user_email) {
+        global $CFG, $DB;
+
+        $params = self::validate_parameters(self::execute_parameters(), ['user_email' => $user_email]);
+
+        // now security checks
+        $context = context_system::instance();
+        self::validate_context($context);
+        require_capability('local/polaris_gateway:access_api', $context);
+
+        $result = [];
+
+        $user = $DB->get_record('user', ['email' => $params["user_email"]]);
+        $courses = enrol_get_users_courses($user->id, true);
+
+        foreach($courses as $course)
+        {
+            $singleCourse = [];
+            $singleCourse["group_id"] = $course->id;
+            $singleCourse["name"] = $course->shortname;
+            $singleCourse["description"] = $course->name;
+            $result[] = $singleCourse;
+        }
+
+    
+        return $result;
+    }
 }
\ No newline at end of file
diff --git a/db/access.php b/db/access.php
new file mode 100644
index 0000000..92510c5
--- /dev/null
+++ b/db/access.php
@@ -0,0 +1,10 @@
+<?php
+
+
+$capabilities = [
+    'local/polaris_gateway:access_api' => [
+        'riskbitmask' => RISK_PERSONAL ,
+        'captype' => 'write',
+        'contextlevel' => CONTEXT_SYSTEM,
+    ],
+];
\ No newline at end of file
diff --git a/db/services.php b/db/services.php
index 7a4ae3b..8706ba2 100644
--- a/db/services.php
+++ b/db/services.php
@@ -5,6 +5,7 @@ $functions = [
     'local_polaris_gateway_collect_groups' => [
         // The name of the namespaced class that the function is located in.
         'classname'   => 'local_polaris_gateway\external\collect_groups',
+        'classpath' => 'local/polaris_gateway/classes/external/collect_groups.php',
 
         // A brief, human-readable, description of the web service function.
         'description' => 'Collects information about courses members to show them in polaris',
@@ -25,20 +26,9 @@ $services = [
     'Polaris Gateway' => [
         'functions' => [
             'local_polaris_gateway_collect_groups',
-            'local_o365_get_onenoteassignment',
-            'local_o365_update_onenoteassignment',
-            'local_o365_delete_onenoteassignment',
-            'local_o365_get_teachercourses',
-            'local_o365_get_course_users',
-            'local_o365_get_assignments',
-            'local_o365_get_bot_message',
-            'local_o365_update_grade',
-            'mod_assign_get_assignments',
-            'mod_assign_get_grades',
-            'mod_assign_save_grade',
         ],
-        'restrictedusers' => 0,
-        'enabled' => 0,
+        'restrictedusers' => 1,
+        'enabled' => 1,
         'shortname' => 'polaris_gateway',
     ]
 ];
\ No newline at end of file
diff --git a/lang/en/local_polaris_gateway.php b/lang/en/local_polaris_gateway.php
index 782e612..7d600c3 100644
--- a/lang/en/local_polaris_gateway.php
+++ b/lang/en/local_polaris_gateway.php
@@ -26,3 +26,4 @@
 defined('MOODLE_INTERNAL') || die();
 
 $string['pluginname'] = 'Polaris Gateway';
+$string['polaris_gateway:access_api'] = "API Zugriff, nur an den Service-Nutzer vergeben";
\ No newline at end of file
-- 
GitLab