MDL-65959 core_badges: Allow ability to upload badges cross domain.
[moodle.git] / badges / backpack-add.php
CommitLineData
aae219ac
DW
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/**
1837b1d5 18 * Optionally award a badge and redirect to the my badges page.
aae219ac
DW
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
25require_once(__DIR__ . '/../config.php');
26require_once($CFG->libdir . '/badgeslib.php');
27
28if (badges_open_badges_backpack_api() != OPEN_BADGES_V2) {
29 throw new coding_exception('No backpacks support Open Badges V2.');
30}
31
32require_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);
41if (!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));
b6435e09
P
51 $sitebackpack = badges_get_site_primary_backpack();
52 $userbackpack = badges_get_site_backpack($backpack->externalbackpackid);
7444ba74 53 $assertion = new core_badges_assertion($id, $sitebackpack->apiversion);
aae219ac
DW
54 $api = new \core_badges\backpack_api($sitebackpack);
55 $api->authenticate();
56
57 // Create issuer.
58 $issuer = $assertion->get_issuer();
59 if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
60 $response = $api->put_issuer($issuer);
61 if (!$response) {
62 throw new moodle_exception('invalidrequest', 'error');
63 }
64 $issuerentityid = $response->id;
b6435e09
P
65 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'],
66 $issuerentityid, $response->openBadgeId);
aae219ac
DW
67 }
68 // Create badge.
69 $badge = $assertion->get_badge_class(false);
70 $badgeid = $assertion->get_badge_id();
71 if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
72 $response = $api->put_badgeclass($issuerentityid, $badge);
73 if (!$response) {
74 throw new moodle_exception('invalidrequest', 'error');
75 }
76 $badgeentityid = $response->id;
b6435e09
P
77 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid,
78 $badgeentityid, $response->hostedUrl);
aae219ac
DW
79 }
80
81 // Create assertion (Award the badge!).
82 $assertiondata = $assertion->get_badge_assertion(false, false);
83
84 $assertionid = $assertion->get_assertion_hash();
b6435e09
P
85 $assertionentityid = badges_external_get_mapping(
86 $sitebackpack->id,
87 OPEN_BADGES_V2_TYPE_ASSERTION,
88 $assertionid
89 );
aae219ac 90
b6435e09
P
91 if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) {
92 $assertionentityid = badges_generate_badgr_open_url(
93 $sitebackpack,
94 OPEN_BADGES_V2_TYPE_ASSERTION,
95 $assertionentityid
96 );
97 }
98
99 // Create an assertion for the recipient in the issuer's account.
100 if (!$assertionentityid) {
aae219ac
DW
101 $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
102 if (!$response) {
103 throw new moodle_exception('invalidrequest', 'error');
104 }
b6435e09
P
105 $assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id);
106 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
107 $response->id);
108 }
109
110 // Now award/upload the badge to the user's account.
111 if ($assertionentityid && !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid)) {
112 $userapi = new \core_badges\backpack_api($userbackpack, $backpack);
113 $userapi->authenticate();
114 $response = $userapi->import_badge_assertion($assertionentityid);
115 if (!$response) {
116 throw new moodle_exception('invalidrequest', 'error');
117 }
aae219ac 118 $assertionentityid = $response->id;
b6435e09
P
119 badges_external_create_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
120 $assertionentityid);
aae219ac
DW
121 $response = ['success' => 'addedtobackpack'];
122 } else {
123 $response = ['warning' => 'existsinbackpack'];
124 }
b6435e09 125
aae219ac
DW
126 redirect(new moodle_url('/badges/mybadges.php', $response));
127} else {
128 redirect(new moodle_url('/badges/mybadges.php'));
129}