MDL-67451 badges: use the site OB version as default
[moodle.git] / badges / assertion.php
CommitLineData
27806552
YB
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
f85ce0d9 27define('AJAX_SCRIPT', true);
5624b054 28define('NO_MOODLE_COOKIES', true); // No need for a session here.
f85ce0d9 29
1fcf0ca8 30require_once(__DIR__ . '/../config.php');
7444ba74 31require_once($CFG->libdir . '/badgeslib.php');
27806552
YB
32
33if (empty($CFG->enablebadges)) {
34 print_error('badgesdisabled', 'badges');
35}
36
853e506a
Y
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.
d03d1109
SA
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);
27806552 41
d363a5c2 42$assertion = new core_badges_assertion($hash, $obversion);
853e506a
Y
43
44if (!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.
b7374fac
MD
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");
d363a5c2 54 $assertion = array();
5a1ea828 55 if ($obversion == OPEN_BADGES_V2) {
d363a5c2
TT
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);
b7374fac
MD
61 die();
62 }
853e506a 63}
27806552 64
f85ce0d9 65echo $OUTPUT->header();
853e506a 66echo json_encode($json);