MDL-58454 badges: Fix some minor errors for Open Badges v2
authorSara Arjona <sara@moodle.com>
Mon, 5 Nov 2018 13:17:59 +0000 (14:17 +0100)
committerSara Arjona <sara@moodle.com>
Mon, 5 Nov 2018 17:41:54 +0000 (18:41 +0100)
badges/assertion.php
badges/classes/assertion.php
badges/competency_action.php
badges/endorsement.php
badges/related_action.php
badges/related_form.php
badges/tests/badgeslib_test.php
badges/tests/behat/add_badge.feature
lib/badgeslib.php
lib/db/install.xml
lib/db/upgrade.php

index 9cd256d..67b4144 100644 (file)
@@ -35,7 +35,7 @@ if (empty($CFG->enablebadges)) {
 
 $hash = required_param('b', PARAM_ALPHANUM); // Issued badge unique hash for badge assertion.
 $action = optional_param('action', null, PARAM_BOOL); // Generates badge class if true.
-$obversion = optional_param('obversion', 1, PARAM_INT); // For control format OB specification version.
+$obversion = optional_param('obversion', OPEN_BADGES_V1, PARAM_INT); // For control format OB specification version.
 
 $assertion = new core_badges_assertion($hash, $obversion);
 
@@ -50,7 +50,7 @@ if (!is_null($action)) {
     } else { // Revoked badge.
         header("HTTP/1.0 410 Gone");
         $assertion = array();
-        if ($obversion == 2) {
+        if ($obversion == OPEN_BADGES_V2) {
             $assertionurl = new moodle_url('/badges/assertion.php', array('b' => $hash));
             $assertion['id'] = $assertionurl->out();
         }
index 4e356bc..417594d 100644 (file)
@@ -49,7 +49,7 @@ class core_badges_assertion {
     private $_url;
 
     /** @var int $obversion to control version JSON-LD. */
-    private $_obversion = 1;
+    private $_obversion = OPEN_BADGES_V1;
 
     /**
      * Constructs with issued badge unique hash.
@@ -57,7 +57,7 @@ class core_badges_assertion {
      * @param string $hash Badge unique hash from badge_issued table.
      * @param int $obversion to control version JSON-LD.
      */
-    public function __construct($hash, $obversion = 1) {
+    public function __construct($hash, $obversion = OPEN_BADGES_V1) {
         global $DB;
 
         $this->_data = $DB->get_record_sql('
@@ -256,7 +256,7 @@ class core_badges_assertion {
      */
     protected function embed_data_badge_version2 (&$json, $type = OPEN_BADGES_V2_TYPE_ASSERTION) {
         // Specification Version 2.0.
-        if ($this->_obversion == 2) {
+        if ($this->_obversion == OPEN_BADGES_V2) {
             $badge = new badge($this->_data->id);
             if (empty($this->_data->courseid)) {
                 $context = context_system::instance();
@@ -265,10 +265,13 @@ class core_badges_assertion {
             }
 
             $hash = $this->_data->uniquehash;
-            $assertionsurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'obversion' => 2));
-            $classurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'action' => 1, 'obversion' => 2));
+            $assertionsurl = new moodle_url('/badges/assertion.php', array('b' => $hash, 'obversion' => $this->_obversion));
+            $classurl = new moodle_url(
+                '/badges/assertion.php',
+                array('b' => $hash, 'action' => 1, 'obversion' => $this->_obversion)
+            );
             $issuerurl = new moodle_url('/badges/assertion.php', array('b' => $this->_data->uniquehash, 'action' => 0,
-                'obversion' => 2));
+                'obversion' => $this->_obversion));
             // For assertion.
             if ($type == OPEN_BADGES_V2_TYPE_ASSERTION) {
                 $json['@context'] = OPEN_BADGES_V2_CONTEXT;
index ced8e64..f56da92 100644 (file)
@@ -32,6 +32,9 @@ $action = optional_param('action', 'remove', PARAM_TEXT); // Remove.
 require_login();
 $return = new moodle_url('/badges/competency.php', array('id' => $badgeid));
 $badge = new badge($badgeid);
+$context = $badge->get_context();
+require_capability('moodle/badges:configuredetails', $context);
+
 if ($action == 'remove') {
     $badge->delete_alignment($alignmentid);
 }
index 6f5cde5..ec3e145 100644 (file)
@@ -37,6 +37,7 @@ if (empty($CFG->enablebadges)) {
 $badge = new badge($badgeid);
 $context = $badge->get_context();
 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
+require_capability('moodle/badges:configuredetails', $context);
 
 if ($badge->type == BADGE_TYPE_COURSE) {
     if (empty($CFG->badges_allowcoursebadges)) {
index 21c46a2..8011aed 100644 (file)
@@ -32,6 +32,9 @@ $action = optional_param('action', 'remove', PARAM_TEXT); // Add, remove option.
 require_login();
 $return = new moodle_url('/badges/related.php', array('id' => $badgeid));
 $badge = new badge($badgeid);
+$context = $badge->get_context();
+require_capability('moodle/badges:configuredetails', $context);
+
 if ($action == 'remove') {
     $badge->delete_related_badge($relatedid);
 }
index 9f272fc..8e7cd56 100644 (file)
@@ -79,10 +79,10 @@ class edit_relatedbadge_form extends moodleform {
      */
     public function get_badges_option($badge) {
         global $DB;
-        $sql = "SELECT b.id, b.name, b.version, b.language, b.type 
+        $sql = "SELECT b.id, b.name, b.version, b.language, b.type
                   FROM {badge} b
                  WHERE b.id <> :badgeid
-                       AND b.id NOT IN (SELECT br.relatedbadgeid 
+                       AND b.id NOT IN (SELECT br.relatedbadgeid
                   FROM {badge_related} br WHERE br.badgeid = :badgeid1)";
         $params = array('badgeid' => $badge->id, 'badgeid1' => $badge->id);
         if ($badge->type == BADGE_TYPE_COURSE) {
index 41e924f..86d6ecb 100644 (file)
@@ -165,28 +165,27 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
     public function test_clone_badge() {
         $badge = new badge($this->badgeid);
         $newid = $badge->make_clone();
-        $cloned_badge = new badge($newid);
-
-        $this->assertEquals($badge->description, $cloned_badge->description);
-        $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
-        $this->assertEquals($badge->issuername, $cloned_badge->issuername);
-        $this->assertEquals($badge->issuercontact, $cloned_badge->issuercontact);
-        $this->assertEquals($badge->issuerurl, $cloned_badge->issuerurl);
-        $this->assertEquals($badge->expiredate, $cloned_badge->expiredate);
-        $this->assertEquals($badge->expireperiod, $cloned_badge->expireperiod);
-        $this->assertEquals($badge->type, $cloned_badge->type);
-        $this->assertEquals($badge->courseid, $cloned_badge->courseid);
-        $this->assertEquals($badge->message, $cloned_badge->message);
-        $this->assertEquals($badge->messagesubject, $cloned_badge->messagesubject);
-        $this->assertEquals($badge->attachment, $cloned_badge->attachment);
-        $this->assertEquals($badge->notification, $cloned_badge->notification);
-        $this->assertEquals($badge->version, $cloned_badge->version);
-        $this->assertEquals($badge->language, $cloned_badge->language);
-        $this->assertEquals($badge->imagecaption, $cloned_badge->imagecaption);
-        $this->assertEquals($badge->imageauthorname, $cloned_badge->imageauthorname);
-        $this->assertEquals($badge->imageauthoremail, $cloned_badge->imageauthoremail);
-        $this->assertEquals($badge->imageauthorurl, $cloned_badge->imageauthorurl);
-
+        $clonedbadge = new badge($newid);
+
+        $this->assertEquals($badge->description, $clonedbadge->description);
+        $this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
+        $this->assertEquals($badge->issuername, $clonedbadge->issuername);
+        $this->assertEquals($badge->issuercontact, $clonedbadge->issuercontact);
+        $this->assertEquals($badge->issuerurl, $clonedbadge->issuerurl);
+        $this->assertEquals($badge->expiredate, $clonedbadge->expiredate);
+        $this->assertEquals($badge->expireperiod, $clonedbadge->expireperiod);
+        $this->assertEquals($badge->type, $clonedbadge->type);
+        $this->assertEquals($badge->courseid, $clonedbadge->courseid);
+        $this->assertEquals($badge->message, $clonedbadge->message);
+        $this->assertEquals($badge->messagesubject, $clonedbadge->messagesubject);
+        $this->assertEquals($badge->attachment, $clonedbadge->attachment);
+        $this->assertEquals($badge->notification, $clonedbadge->notification);
+        $this->assertEquals($badge->version, $clonedbadge->version);
+        $this->assertEquals($badge->language, $clonedbadge->language);
+        $this->assertEquals($badge->imagecaption, $clonedbadge->imagecaption);
+        $this->assertEquals($badge->imageauthorname, $clonedbadge->imageauthorname);
+        $this->assertEquals($badge->imageauthoremail, $clonedbadge->imageauthoremail);
+        $this->assertEquals($badge->imageauthorurl, $clonedbadge->imageauthorurl);
     }
 
     public function test_badge_status() {
@@ -626,7 +625,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
         // Test Openbadge specification version 2.
         // Get assertion version 2.
         $award = reset($awards);
-        $assertion2 = new core_badges_assertion($award->uniquehash, 2);
+        $assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2);
         $testassertion2 = $this->assertion2;
 
         // Make sure JSON strings have the same structure.
@@ -746,7 +745,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
         // Insert an related badge.
         $badge->add_related_badges([$newid1, $newid2, $newid3]);
         $this->assertCount(3, $badge->get_related_badges());
-        
+
         // Only get related is active.
         $clonedbage1 = new badge($newid1);
         $clonedbage1->status = BADGE_STATUS_ACTIVE;
index 686819e..f11c510 100644 (file)
@@ -82,7 +82,7 @@ Feature: Add badges to the system
     And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
     And I press "Create badge"
     And I follow "Related badges (0)"
-    And I should see "This badge does not have an related badge."
+    And I should see "This badge does not have related badges."
     And I press "Add related badge"
     And I follow "Related badges"
     And I wait until the page is ready
index 99807ef..84241df 100644 (file)
@@ -98,6 +98,12 @@ define('BADGE_MESSAGE_MONTHLY', 4);
  */
 define('BADGE_BACKPACKURL', 'https://backpack.openbadges.org');
 
+/*
+ * Open Badges specifications.
+ */
+define('OPEN_BADGES_V1', 1);
+define('OPEN_BADGES_V2', 2);
+
 /*
  * Only use for Open Badges 2.0 specification
  */
@@ -708,6 +714,11 @@ class badge {
         $badgecontext = $this->get_context();
         $fs->delete_area_files($badgecontext->id, 'badges', 'badgeimage', $this->id);
 
+        // Delete endorsements, competencies and related badges.
+        $DB->delete_records('badge_endorsement', array('badgeid' => $this->id));
+        $DB->delete_records('badge_related', array('badgeid' => $this->id));
+        $DB->delete_records('badge_competencies', array('badgeid' => $this->id));
+
         // Finally, remove badge itself.
         $DB->delete_records('badge', array('id' => $this->id));
 
@@ -805,9 +816,9 @@ class badge {
      * @param int $alignmentid ID competency alignment.
      * @return bool A status for delete a competency alignment.
      */
-    public function delete_alignment($alignmentid = 0) {
+    public function delete_alignment($alignmentid) {
         global $DB;
-        return $DB->delete_records('badge_competencies', array('id' => $alignmentid));
+        return $DB->delete_records('badge_competencies', array('badgeid' => $this->id, 'id' => $alignmentid));
     }
 
     /**
index a032e16..7fd383d 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20181030" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20181105" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
-        <KEY NAME="relatedbadge" TYPE="foreign" FIELDS="badgeid" REFTABLE="badge" REFFIELDS="id"/>
+        <KEY NAME="badgeid" TYPE="foreign" FIELDS="badgeid" REFTABLE="badge" REFFIELDS="id"/>
+        <KEY NAME="relatedbadgeid" TYPE="foreign" FIELDS="relatedbadgeid" REFTABLE="badge" REFFIELDS="id"/>
+        <KEY NAME="badgeid-relatedbadgeid" TYPE="unique" FIELDS="badgeid, relatedbadgeid"/>
       </KEYS>
     </TABLE>
     <TABLE NAME="user_devices" COMMENT="This table stores user's mobile devices information in order to send PUSH notifications">
       </INDEXES>
     </TABLE>
   </TABLES>
-</XMLDB>
+</XMLDB>
\ No newline at end of file
index c34631d..64ab977 100644 (file)
@@ -2649,7 +2649,6 @@ function xmldb_main_upgrade($oldversion) {
             $dbman->add_index($table, $index);
         }
 
-        // Main savepoint reached.
         upgrade_main_savepoint(true, 2018102300.02);
     }
 
@@ -2679,8 +2678,10 @@ function xmldb_main_upgrade($oldversion) {
         $fieldversion = new xmldb_field('version', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'nextcron');
         $fieldlanguage = new xmldb_field('language', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'version');
         $fieldimageauthorname = new xmldb_field('imageauthorname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'language');
-        $fieldimageauthoremail =  new xmldb_field('imageauthoremail', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imageauthorname');
-        $fieldimageauthorurl = new xmldb_field('imageauthorurl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imageauthoremail');
+        $fieldimageauthoremail = new xmldb_field('imageauthoremail', XMLDB_TYPE_CHAR, '255', null, null,
+            null, null, 'imageauthorname');
+        $fieldimageauthorurl = new xmldb_field('imageauthorurl', XMLDB_TYPE_CHAR, '255', null, null,
+            null, null, 'imageauthoremail');
         $fieldimagecaption = new xmldb_field('imagecaption', XMLDB_TYPE_TEXT, null, null, null, null, null, 'imageauthorurl');
 
         if (!$dbman->field_exists($tablebadge, $fieldversion)) {
@@ -2734,7 +2735,9 @@ function xmldb_main_upgrade($oldversion) {
 
         // Adding keys to table badge_related.
         $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
-        $table->add_key('relatedbadge', XMLDB_KEY_FOREIGN, ['badgeid'], 'badge', ['id']);
+        $table->add_key('badgeid', XMLDB_KEY_FOREIGN, ['badgeid'], 'badge', ['id']);
+        $table->add_key('relatedbadgeid', XMLDB_KEY_FOREIGN, ['relatedbadgeid'], 'badge', ['id']);
+        $table->add_key('badgeid-relatedbadgeid', XMLDB_KEY_UNIQUE, ['badgeid', 'relatedbadgeid']);
 
         // Conditionally launch create table for badge_related.
         if (!$dbman->table_exists($table)) {