Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-81103

Error "Argument #1 ($value) must be of type Countable|array, stdClass given" when sending badges to backpack

XMLWordPrintable

    • 2
    • Moppies PB candidates

      Steps to reproduce

      1. Login as admin.
      2. Go to Site administration > Badges > Manage backpacks.
      3. Edit the backpack "https://badgr.io".
      4. Set the "Backpack API URL" from https://api.badgr.io/v2 to https://api.badgr.io/bcv1/ims/ob/v2p
      5. Create a badge, enable it and award it to the admin user.
      6. Go to the user preferences and click "Backpack settings" in the Badges section.
      7. Connect to your backpack in Badgr.com using a valid email. You'll need to confirm the email account (so your Moodle site must be configured to send emails).
      8. Open the email client and click the Verify email button that you'll find in the email that has been sent with the subject: "Badges backpack email verification".
      9. Go to your profile and access the badge you were awarded.
      10. Click the Add to backpack button.

       

      Expected behaviour

      The badge is sent to your backpack and a confirmation notification is displayed.

       

      Current behaviour

      The following error is displayed: "count(): Argument #1 ($value) must be of type Countable|array, stdClass given"

      badges/classes/backpack_api_mapping.php around line 383:

              $response = json_decode($response);
              if (isset($response->result)) {
                  $response = $response->result;
              }
              $context = context_system::instance();
              $exporter = $this->responseexporter;
              if (class_exists($exporter)) {
                  $output = $PAGE->get_renderer('core', 'badges');
                  if (!$this->multiple) {
                      if (count($response)) {
                          $response = $response[0];
                      }
                      if (empty($response)) {
                          return null;
                      }
                      $apidata = $exporter::map_external_data($response, $this->backpackapiversion);
                      $exporterinstance = new $exporter($apidata, ['context' => $context]);
                      $data = $exporterinstance->export($output);
                      return $data;

       

      This is the content of $response in that case:

      • timestamp: "2024-03-01T11:19:51.954+00:00",
      • status: 405
      • error: "Method Not Allowed"
      • message: "Request method 'POST' not supported"
      • path: "/bcv1/ims/ob/v2p/backpack/import"
      • traceId: "04acd2e5-05c1-46d8-afbf-a70c1e87c196"

       

      The existing code should be improved to give more information to users to help them to figure out why the badge can't be added to the backpack.

            sarjona Sara Arjona (@sarjona)
            sarjona Sara Arjona (@sarjona)
            Votes:
            3 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 4 hours
                4h

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.