MDL-66357 core_badges: remove action=0 usage for issuer
authorSara Arjona <sara@moodle.com>
Tue, 24 Mar 2020 14:10:51 +0000 (15:10 +0100)
committerSara Arjona <sara@moodle.com>
Fri, 3 Apr 2020 06:09:55 +0000 (08:09 +0200)
In MDL-67971, issuer_json.php was created, to display issuer JSON content.
However, some files were still using action=0 to display this information.
It has been replaced by issuer_json.php.
The action=0 support won't be removed (at least for now), because several
public badges URLs are using it.

badges/classes/assertion.php
badges/classes/badge.php
badges/issuer_json.php

index 5d98f4a..e1c471f 100644 (file)
@@ -198,11 +198,8 @@ class core_badges_assertion {
             $class['image'] = 'data:image/png;base64,' . $imagedata;
             $class['criteria'] = $this->_url->out(false); // Currently issued badge URL.
             if ($issued) {
-                if ($this->_obversion == OPEN_BADGES_V2) {
-                    $issuerurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->get_badge_id()));
-                } else {
-                    $issuerurl = new moodle_url('/badges/assertion.php', array('b' => $this->_data->uniquehash, 'action' => 0));
-                }
+                $params = ['id' => $this->get_badge_id(), 'obversion' => $this->_obversion];
+                $issuerurl = new moodle_url('/badges/issuer_json.php', $params);
                 $class['issuer'] = $issuerurl->out(false);
             }
             $this->embed_data_badge_version2($class, OPEN_BADGES_V2_TYPE_BADGE);
@@ -223,7 +220,7 @@ class core_badges_assertion {
         $issuer = array();
         if ($this->_data) {
             // Required.
-            if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
+            if ($this->_obversion == OPEN_BADGES_V1) {
                 $issuer['name'] = $this->_data->issuername;
                 $issuer['url'] = $this->_data->issuerurl;
                 // Optional.
index a3dfdba..9bd9d19 100644 (file)
@@ -925,17 +925,28 @@ class badge {
     /**
      * Define issuer information by format Open Badges specification version 2.
      *
+     * @param int $obversion OB version to use.
      * @return array Issuer informations of the badge.
      */
-    public function get_badge_issuer() {
-        $issuer = array();
-        $issuer['name'] = $this->issuername;
-        $issuer['url'] = $this->issuerurl;
-        $issuer['email'] = $this->issuercontact;
-        $issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
-        $issueridurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->id));
-        $issuer['id'] = $issueridurl->out(false);
-        $issuer['type'] = OPEN_BADGES_V2_TYPE_ISSUER;
+    public function get_badge_issuer(?int $obversion = null) {
+        global $DB;
+
+        $issuer = [];
+        if ($obversion == OPEN_BADGES_V1) {
+            $data = $DB->get_record('badge', ['id' => $this->id]);
+            $issuer['name'] = $data->issuername;
+            $issuer['url'] = $data->issuerurl;
+            $issuer['email'] = $data->issuercontact;
+        } else {
+            $issuer['name'] = $this->issuername;
+            $issuer['url'] = $this->issuerurl;
+            $issuer['email'] = $this->issuercontact;
+            $issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
+            $issueridurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->id));
+            $issuer['id'] = $issueridurl->out(false);
+            $issuer['type'] = OPEN_BADGES_V2_TYPE_ISSUER;
+        }
+
         return $issuer;
     }
 }
index ebbf74f..87fe6bd 100644 (file)
@@ -30,6 +30,8 @@ require_once($CFG->libdir . '/badgeslib.php');
 
 
 $id = optional_param('id', null, PARAM_INT);
+// OB specification version. If it's not defined, the site will be used as default.
+$obversion = optional_param('obversion', badges_open_badges_backpack_api(), PARAM_INT);
 
 if (empty($id)) {
     // Get the default issuer for this site.
@@ -38,7 +40,7 @@ if (empty($id)) {
     // Get the issuer for this badge.
     $badge = new badge($id);
     if ($badge->status != BADGE_STATUS_INACTIVE) {
-        $json = $badge->get_badge_issuer();
+        $json = $badge->get_badge_issuer($obversion);
     } else {
         // The badge doen't exist or not accessible for the users.
         header("HTTP/1.0 410 Gone");