Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * Serve assertion JSON by unique hash of issued badge
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  25   */
  26  
  27  define('AJAX_SCRIPT', true);
  28  define('NO_MOODLE_COOKIES', true); // No need for a session here.
  29  
  30  require_once(__DIR__ . '/../config.php');
  31  require_once($CFG->libdir . '/badgeslib.php');
  32  
  33  if (empty($CFG->enablebadges)) {
  34      throw new \moodle_exception('badgesdisabled', 'badges');
  35  }
  36  
  37  $hash = required_param('b', PARAM_ALPHANUM); // Issued badge unique hash for badge assertion.
  38  $action = optional_param('action', null, PARAM_BOOL); // Generates badge class if true.
  39  // OB specification version. If it's not defined, the site will be used as default.
  40  $obversion = optional_param('obversion', badges_open_badges_backpack_api(), PARAM_INT);
  41  
  42  $assertion = new core_badges_assertion($hash, $obversion);
  43  
  44  if (!is_null($action)) {
  45      // Get badge class or issuer information depending on $action.
  46      $json = ($action) ? $assertion->get_badge_class() : $assertion->get_issuer();
  47  } else {
  48      // Otherwise, get badge assertion.
  49      $column = $DB->sql_compare_text('uniquehash', 255);
  50      if ($DB->record_exists_sql(sprintf('SELECT * FROM {badge_issued} WHERE %s = ?', $column), array($hash))) {
  51          $json = $assertion->get_badge_assertion();
  52      } else { // Revoked badge.
  53          header("HTTP/1.0 410 Gone");
  54          $assertion = array();
  55          if ($obversion >= OPEN_BADGES_V2) {
  56              $assertionurl = new moodle_url('/badges/assertion.php', array('b' => $hash));
  57              $assertion['id'] = $assertionurl->out();
  58          }
  59          $assertion['revoked'] = true;
  60          echo json_encode($assertion);
  61          die();
  62      }
  63  }
  64  
  65  echo $OUTPUT->header();
  66  echo json_encode($json);