weekly release 3.8.5+
[moodle.git] / badges / assertion.php
blob0c41062851e2c4ef6211257921b9386346f0f5b5
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.
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/>.
17 /**
18 * Serve assertion JSON by unique hash of issued badge
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>
27 define('AJAX_SCRIPT', true);
28 define('NO_MOODLE_COOKIES', true); // No need for a session here.
30 require_once(__DIR__ . '/../config.php');
31 require_once($CFG->libdir . '/badgeslib.php');
33 if (empty($CFG->enablebadges)) {
34 print_error('badgesdisabled', 'badges');
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);
42 $assertion = new core_badges_assertion($hash, $obversion);
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();
59 $assertion['revoked'] = true;
60 echo json_encode($assertion);
61 die();
65 echo $OUTPUT->header();
66 echo json_encode($json);