Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Optionally award a badge and redirect to the my badges page.
  19   *
  20   * @package    core_badges
  21   * @copyright  2019 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../config.php');
  26  require_once($CFG->libdir . '/badgeslib.php');
  27  
  28  if (badges_open_badges_backpack_api() != OPEN_BADGES_V2) {
  29      throw new coding_exception('No backpacks support Open Badges V2.');
  30  }
  31  
  32  require_login();
  33  
  34  $id = required_param('hash', PARAM_ALPHANUM);
  35  
  36  $PAGE->set_url('/badges/backpack-add.php', array('hash' => $id));
  37  $PAGE->set_context(context_system::instance());
  38  $output = $PAGE->get_renderer('core', 'badges');
  39  
  40  $issuedbadge = new \core_badges\output\issued_badge($id);
  41  if (!empty($issuedbadge->recipient->id)) {
  42      // The flow for issuing a badge is:
  43      // * Create issuer
  44      // * Create badge
  45      // * Create assertion (Award the badge!)
  46  
  47      // Get the backpack.
  48      $badgeid = $issuedbadge->badgeid;
  49      $badge = new badge($badgeid);
  50      $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
  51      $sitebackpack = badges_get_site_backpack($backpack->externalbackpackid);
  52      $assertion = new core_badges_assertion($id, $sitebackpack->apiversion);
  53      $api = new \core_badges\backpack_api($sitebackpack);
  54      $api->authenticate();
  55  
  56      // Create issuer.
  57      $issuer = $assertion->get_issuer();
  58      if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
  59          $response = $api->put_issuer($issuer);
  60          if (!$response) {
  61              throw new moodle_exception('invalidrequest', 'error');
  62          }
  63          $issuerentityid = $response->id;
  64          badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'], $issuerentityid);
  65      }
  66      // Create badge.
  67      $badge = $assertion->get_badge_class(false);
  68      $badgeid = $assertion->get_badge_id();
  69      if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
  70          $response = $api->put_badgeclass($issuerentityid, $badge);
  71          if (!$response) {
  72              throw new moodle_exception('invalidrequest', 'error');
  73          }
  74          $badgeentityid = $response->id;
  75          badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid, $badgeentityid);
  76      }
  77  
  78      // Create assertion (Award the badge!).
  79      $assertiondata = $assertion->get_badge_assertion(false, false);
  80  
  81      $assertionid = $assertion->get_assertion_hash();
  82  
  83      if (!($assertionentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid))) {
  84          $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
  85          if (!$response) {
  86              throw new moodle_exception('invalidrequest', 'error');
  87          }
  88          $assertionentityid = $response->id;
  89          badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid, $assertionentityid);
  90          $response = ['success' => 'addedtobackpack'];
  91      } else {
  92          $response = ['warning' => 'existsinbackpack'];
  93      }
  94      redirect(new moodle_url('/badges/mybadges.php', $response));
  95  } else {
  96      redirect(new moodle_url('/badges/mybadges.php'));
  97  }