MDL-65959 badge: Confirm assertion data is updated before awarding
authorPeter Dias <peter@moodle.com>
Thu, 27 Aug 2020 05:21:25 +0000 (13:21 +0800)
committerPeter Dias <peter@moodle.com>
Mon, 26 Oct 2020 16:27:19 +0000 (00:27 +0800)
badges/backpack-add.php
badges/classes/backpack_api.php
badges/classes/backpack_api_mapping.php
badges/classes/form/external_backpack.php

index 7b80f4c..fd800f2 100644 (file)
@@ -105,6 +105,18 @@ if (!empty($issuedbadge->recipient->id)) {
         $assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id);
         badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
             $response->id);
         $assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id);
         badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
             $response->id);
+    } else {
+        // An assertion already exists. Make sure it's up to date.
+        $internalid = badges_external_get_mapping(
+            $sitebackpack->id,
+            OPEN_BADGES_V2_TYPE_ASSERTION,
+            $assertionid,
+            'externalid'
+        );
+        $response = $api->update_assertion($internalid, $assertiondata);
+        if (!$response) {
+            throw new moodle_exception('invalidrequest', 'error');
+        }
     }
 
     // Now award/upload the badge to the user's account.
     }
 
     // Now award/upload the badge to the user's account.
index 7a8c098..e0db003 100644 (file)
@@ -225,6 +225,17 @@ class backpack_api {
                     true,                                       // JSON Encoded.
                     true                                        // Auth required.
                 ];
                     true,                                       // JSON Encoded.
                     true                                        // Auth required.
                 ];
+                $mapping[] = [
+                    'updateassertion',                                // Action.
+                    '[URL]/assertions/[PARAM2]?expand=badgeclass&expand=issuer',
+                    '[PARAM]',                                  // Post params.
+                    'core_badges\external\assertion_exporter', // Request exporter.
+                    'core_badges\external\assertion_exporter', // Response exporter.
+                    false,                                      // Multiple.
+                    'put',                                     // Method.
+                    true,                                       // JSON Encoded.
+                    true                                        // Auth required.
+                ];
                 foreach ($mapping as $map) {
                     $map[] = false; // Site api function.
                     $map[] = OPEN_BADGES_V2; // V2 function.
                 foreach ($mapping as $map) {
                     $map[] = false; // Site api function.
                     $map[] = OPEN_BADGES_V2; // V2 function.
@@ -419,6 +430,22 @@ class backpack_api {
         return $this->curl_request('assertions', null, $entityid, $data);
     }
 
         return $this->curl_request('assertions', null, $entityid, $data);
     }
 
+    /**
+     * Update a badgeclass assertion.
+     *
+     * @param string $entityid The id of the badge class.
+     * @param array $data The structure of the badge class assertion.
+     * @return mixed
+     */
+    public function update_assertion(string $entityid, array $data) {
+        // V2 Only.
+        if ($this->backpackapiversion == OPEN_BADGES_V1) {
+            throw new coding_exception('Not supported in this backpack API');
+        }
+
+        return $this->curl_request('updateassertion', null, $entityid, $data);
+    }
+
     /**
      * Import a badge assertion into a backpack. This is used to handle cross domain backpacks.
      *
     /**
      * Import a badge assertion into a backpack. This is used to handle cross domain backpacks.
      *
index 5e70fe5..aa9155c 100644 (file)
@@ -362,6 +362,8 @@ class backpack_api_mapping {
             $response = $curl->get($url, $post, $options);
         } else if ($this->method == 'post') {
             $response = $curl->post($url, $post, $options);
             $response = $curl->get($url, $post, $options);
         } else if ($this->method == 'post') {
             $response = $curl->post($url, $post, $options);
+        } else if ($this->method == 'put') {
+            $response = $curl->put($url, $post, $options);
         }
         $response = json_decode($response);
         if (isset($response->result)) {
         }
         $response = json_decode($response);
         if (isset($response->result)) {
index 32390fd..b5d10e0 100644 (file)
@@ -155,7 +155,7 @@ class external_backpack extends \moodleform {
      * Add backpack specific auth details.
      *
      * @param string|null $email The email addressed provided or null if it's new.
      * Add backpack specific auth details.
      *
      * @param string|null $email The email addressed provided or null if it's new.
-     * @param boolean|true $includepassword Include the password field.
+     * @param bool $includepassword Include the password field. Defaults to true
      * @throws \coding_exception
      */
     protected function add_auth_fields(?string $email, bool $includepassword = true) {
      * @throws \coding_exception
      */
     protected function add_auth_fields(?string $email, bool $includepassword = true) {