MDL-65959 core_badges: Allow ability to upload badges cross domain.
[moodle.git] / badges / backpack-add.php
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/>.
17 /**
18  * Optionally award a badge and redirect to the my badges page.
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  */
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.');
30 }
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_primary_backpack();
52     $userbackpack = badges_get_site_backpack($backpack->externalbackpackid);
53     $assertion = new core_badges_assertion($id, $sitebackpack->apiversion);
54     $api = new \core_badges\backpack_api($sitebackpack);
55     $api->authenticate();
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;
65         badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'],
66             $issuerentityid, $response->openBadgeId);
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;
77         badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid,
78             $badgeentityid, $response->hostedUrl);
79     }
81     // Create assertion (Award the badge!).
82     $assertiondata = $assertion->get_badge_assertion(false, false);
84     $assertionid = $assertion->get_assertion_hash();
85     $assertionentityid = badges_external_get_mapping(
86         $sitebackpack->id,
87         OPEN_BADGES_V2_TYPE_ASSERTION,
88         $assertionid
89     );
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     }
99     // Create an assertion for the recipient in the issuer's account.
100     if (!$assertionentityid) {
101         $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
102         if (!$response) {
103             throw new moodle_exception('invalidrequest', 'error');
104         }
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     }
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         }
118         $assertionentityid = $response->id;
119         badges_external_create_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
120             $assertionentityid);
121         $response = ['success' => 'addedtobackpack'];
122     } else {
123         $response = ['warning' => 'existsinbackpack'];
124     }
126     redirect(new moodle_url('/badges/mybadges.php', $response));
127 } else {
128     redirect(new moodle_url('/badges/mybadges.php'));