Merge branch 'MDL-69002-master' of git://github.com/sarjona/moodle
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 11 Jun 2020 03:08:14 +0000 (11:08 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Thu, 11 Jun 2020 03:08:14 +0000 (11:08 +0800)
admin/settings/badges.php
badges/backpacks.php
badges/classes/output/external_backpacks_page.php
badges/renderer.php
badges/templates/external_backpacks_page.mustache
lang/en/badges.php
lang/en/deprecated.txt
lib/badgeslib.php

index 2451390..a97097f 100644 (file)
@@ -59,8 +59,12 @@ if (($hassiteconfig || has_any_capability(array(
 
     $backpacks = badges_get_site_backpacks();
     $choices = array();
+    $defaultchoice = 0;
     foreach ($backpacks as $backpack) {
         $choices[$backpack->id] = $backpack->backpackweburl;
+        if ($backpack->backpackweburl == BADGRIO_BACKPACKWEBURL) {
+            $defaultchoice = $backpack->id;
+        }
     }
 
     $globalsettings->add(new admin_setting_configcheckbox('badges_allowcoursebadges',
@@ -100,18 +104,10 @@ if (($hassiteconfig || has_any_capability(array(
             new lang_string('allowexternalbackpack', 'badges'),
             new lang_string('allowexternalbackpack_desc', 'badges'), 1));
 
-    $bp = $DB->get_record('badge_external_backpack', ['backpackweburl' => BADGRIO_BACKPACKWEBURL]);
     $backpacksettings->add(new admin_setting_configselect('badges_site_backpack',
             new lang_string('sitebackpack', 'badges'),
             new lang_string('sitebackpack_help', 'badges'),
-            $bp->id, $choices));
-
-    $warning = badges_verify_site_backpack();
-    if (!empty($warning)) {
-        $backpacksettings->add(new admin_setting_description('badges_site_backpack_verify',
-            new lang_string('sitebackpackverify', 'badges'),
-            $warning));
-    }
+            $defaultchoice, $choices));
 
     $ADMIN->add('badges', $backpacksettings);
 
index ae63fd6..74beadd 100644 (file)
@@ -80,6 +80,14 @@ if ($action == 'edit') {
     echo $output->heading(get_string('managebackpacks', 'badges'));
 
     $form->display();
+} else if ($action == 'test') {
+    // If no backpack has been selected, there isn't anything to test.
+    if (empty($id)) {
+        redirect($url);
+    }
+
+    echo $OUTPUT->header();
+    echo $output->render_test_backpack_result($id);
 } else {
     echo $OUTPUT->header();
     echo $output->heading(get_string('managebackpacks', 'badges'));
index f9258b3..28da925 100644 (file)
@@ -72,11 +72,11 @@ class external_backpacks_page implements \renderable {
             } else {
                 $backpack->canedit = false;
             }
+            $backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2);
             $backpack->iscurrent = ($backpack->id == $CFG->badges_site_backpack);
 
             $data->backpacks[] = $backpack;
         }
-        $data->warning = badges_verify_site_backpack();
 
         return $data;
     }
index 2f7bad8..f268bd7 100644 (file)
@@ -1368,4 +1368,35 @@ class core_badges_renderer extends plugin_renderer_base {
         $data = $page->export_for_template($this);
         return parent::render_from_template('core_badges/external_backpacks_page', $data);
     }
+
+    /**
+     * Get the result of a backpack validation with its settings. It returns:
+     * - A informative message if the backpack version is different from OBv2.
+     * - A warning with the error if it's not possible to connect to this backpack.
+     * - A successful message if the connection has worked.
+     *
+     * @param  int    $backpackid The backpack identifier.
+     * @return string A message with the validation result.
+     */
+    public function render_test_backpack_result(int $backpackid): string {
+        // Get the backpack.
+        $backpack = badges_get_site_backpack($backpackid);
+
+        // Add the header to the result.
+        $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));
+
+        if ($backpack->apiversion != OPEN_BADGES_V2) {
+            // Only OBv2 supports this validation.
+            $result .= get_string('backpackconnectionnottested', 'badges');
+        } else {
+            $message = badges_verify_backpack($backpackid);
+            if (empty($message)) {
+                $result .= get_string('backpackconnectionok', 'badges');
+            } else {
+                $result .= $message;
+            }
+        }
+
+        return $result;
+    }
 }
index aab0f2f..0f9df01 100644 (file)
@@ -25,9 +25,8 @@
     Example context (json):
     {
         "backpacks": [
-            {"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false}
-        ],
-        "warning": "<span class='text-warning'>Could not login</span>"
+            {"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false, "cantest": true}
+        ]
     }
 }}
 
                     {{#pix}}t/delete, core,{{#str}}delete{{/str}}{{/pix}}
                 </a>
             {{/iscurrent}}
+            {{#cantest}}
+                <a href="{{baseurl}}?id={{id}}&action=test">{{#pix}}t/check, core,{{#str}}testsettings, core_badges{{/str}}{{/pix}}</a>
+            {{/cantest}}
             </td>
         </tr>
         {{/backpacks}}
     </tbody>
-</table>
-{{{warning}}}
+</table>
\ No newline at end of file
index ef17226..85abc8a 100644 (file)
@@ -81,6 +81,8 @@ $string['awardedtoyou'] = 'Issued to me';
 $string['awardoncron'] = 'Access to the badges was successfully enabled. Too many users can instantly earn this badge. To ensure site performance, this action will take some time to process.';
 $string['awards'] = 'Recipients';
 $string['backpackavailability'] = 'External badge verification';
+$string['backpackconnectionok'] = 'Backpack connection successfully established';
+$string['backpackconnectionnottested'] = 'Connection can not be tested for this backpack because only OBv2.0 backpacks support it.';
 $string['backpackneedsupdate'] = 'The backpack connected to this profile does not match the backpack for the site. You need to disconnect and reconnect the backpack.';
 $string['backpackavailability_help'] = 'For badge recipients to be able to prove they earned their badges from you, an external backpack service should be able to access your site and verify badges issued from it. Your site does not currently appear to be accessible, which means that badges you have already issued or will issue in the future cannot be verified.
 
@@ -516,7 +518,6 @@ $string['sitebackpack'] = 'Active external backpack';
 $string['sitebackpack_help'] = 'The external backpack that users can connect to from this site. Note that changing this setting after users have connected their backpacks will require each user to go to their backpack settings page and disconnect then reconnect.';
 $string['sitebackpackdeleted'] = 'The site backpack has been deleted.';
 $string['sitebackpacknotdeleted'] = 'This backpack couldn\'t be deleted because it\'s currently the site default.';
-$string['sitebackpackverify'] = 'Backpack connection';
 $string['sitebackpackwarning'] = 'Could not connect to backpack. <br/><br/>Check that the "Badge issuer email address" admin setting is the valid email for an account on the backpack website. <br/><br/>Check that the "Badge issuer password" on the <a href="{$a->url}">site backpack settings page</a>, is the correct password for the account on the backpack website. <br/><br/>The backpack returned: "{$a->warning}"';
 $string['sitebadges'] = 'Site badges';
 $string['sitebadges_help'] = 'Site badges can only be awarded to users for site-related activities. These include completing a set of courses or parts of user profiles. Site badges can also be issued manually by one user to another.
@@ -550,6 +551,8 @@ $string['targetframework'] = 'Framework';
 $string['targetframework_help'] = 'The name of the external skill or standard framework.';
 $string['targetcode'] = 'Code';
 $string['targetcode_help'] = 'A unique string identifier for referencing the external skill or standard within its framework.';
+$string['testbackpack'] = 'Test backpack \'{$a}\'';
+$string['testsettings'] = 'Test settings';
 $string['type'] = 'Type';
 $string['variablesubstitution'] = 'Variable substitution in messages.';
 $string['variablesubstitution_help'] = 'In a badge message, certain variables can be inserted into the subject and/or body of a message so that they will be replaced with real values when the message is sent. The variables should be inserted into the text exactly as they are shown below. The following variables can be used:
@@ -578,3 +581,4 @@ $string['nobackpackcollections'] = 'No badge collections have been selected. <a
 
 // Deprecated since Moodle 3.9.
 $string['editsettings'] = 'Edit settings';
+$string['sitebackpackverify'] = 'Backpack connection';
index 4203ac9..949af22 100644 (file)
@@ -145,3 +145,4 @@ editsettings,core_badges
 availablelicenses,core_admin
 managelicenses,core_admin
 userfilterplaceholder,core
+sitebackpackverify,core_badges
index c1d2d0f..352a086 100644 (file)
@@ -1194,14 +1194,28 @@ function badge_assemble_notification(stdClass $badge) {
  * @return string
  */
 function badges_verify_site_backpack() {
+    global $CFG;
+
+    return badges_verify_backpack($CFG->badges_site_backpack);
+}
+
+/**
+ * Attempt to authenticate with a backpack credentials and return an error
+ * if the authentication fails.
+ * If external backpacks are not enabled or the backpack version is different
+ * from OBv2, this will not perform any test.
+ *
+ * @param int $backpackid Backpack identifier to verify.
+ * @return string The result of the verification process.
+ */
+function badges_verify_backpack(int $backpackid) {
     global $OUTPUT, $CFG;
 
     if (empty($CFG->badges_allowexternalbackpack)) {
         return '';
     }
 
-    $backpack = badges_get_site_backpack($CFG->badges_site_backpack);
-
+    $backpack = badges_get_site_backpack($backpackid);
     if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) {
         $backpackapi = new \core_badges\backpack_api($backpack);
 
@@ -1221,6 +1235,7 @@ function badges_verify_site_backpack() {
             return $OUTPUT->container($icon . $message, 'text-error');
         }
     }
+
     return '';
 }