Merge branch 'MDL-67166-master' of git://github.com/bmbrands/moodle
[moodle.git] / badges / backpack-add.php
blob9c575203794ed616347ca03e8b73e79bd913e89c
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 * Optionally award a badge and redirect to the my badges page.
20 * @package core_badges
21 * @copyright 2019 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/badgeslib.php');
28 if (badges_open_badges_backpack_api() != OPEN_BADGES_V2) {
29 throw new coding_exception('No backpacks support Open Badges V2.');
32 require_login();
34 $id = required_param('hash', PARAM_ALPHANUM);
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');
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!)
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();
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');
63 $issuerentityid = $response->id;
64 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'], $issuerentityid);
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');
74 $badgeentityid = $response->id;
75 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid, $badgeentityid);
78 // Create assertion (Award the badge!).
79 $assertiondata = $assertion->get_badge_assertion(false, false);
81 $assertionid = $assertion->get_assertion_hash();
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');
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'];
94 redirect(new moodle_url('/badges/mybadges.php', $response));
95 } else {
96 redirect(new moodle_url('/badges/mybadges.php'));