Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * 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
 */
24
 
25
require_once(__DIR__ . '/../config.php');
26
require_once($CFG->libdir . '/badgeslib.php');
27
 
28
require_login();
29
 
30
$userbackpack = badges_get_user_backpack();
31
if (badges_open_badges_backpack_api($userbackpack->id) != OPEN_BADGES_V2) {
32
    throw new coding_exception('No backpacks support Open Badges V2.');
33
}
34
 
35
$id = required_param('hash', PARAM_ALPHANUM);
36
 
37
$PAGE->set_url('/badges/backpack-add.php', array('hash' => $id));
38
$PAGE->set_context(context_system::instance());
39
$output = $PAGE->get_renderer('core', 'badges');
40
 
41
$issuedbadge = new \core_badges\output\issued_badge($id);
42
if (!empty($issuedbadge->recipient->id)) {
43
    // The flow for issuing a badge is:
44
    // * Create issuer
45
    // * Create badge
46
    // * Create assertion (Award the badge!)
47
 
48
    // With the introduction OBv2.1 and MDL-65959 to allow cross region Badgr imports the above (old) procedure will
49
    // only be completely performed if both the site and user backpacks conform to the same apiversion.
50
    // Else we will attempt at pushing the assertion to the user's backpack. In this case, the id set against the assertion
51
    // has to be a publicly accessible resource.
52
 
53
    // Get the backpack.
54
    $badgeid = $issuedbadge->badgeid;
55
    $badge = new badge($badgeid);
56
    $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
57
    $userbackpack = badges_get_site_backpack($backpack->externalbackpackid, $USER->id);
58
    $assertion = new core_badges_assertion($id, OPEN_BADGES_V2);
59
    $assertiondata = $assertion->get_badge_assertion(false, false);
60
    $assertionid = $assertion->get_assertion_hash();
61
    $assertionentityid = $assertiondata['id'];
62
    $badgeadded = false;
1441 ariadna 63
    $issuerexists = false;
1 efrain 64
    if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
65
        $sitebackpack = badges_get_site_primary_backpack();
66
        $api = new \core_badges\backpack_api($sitebackpack);
67
        $response = $api->authenticate();
68
 
69
        // A numeric response indicates a valid successful authentication. Else an error object will be returned.
70
        if (is_numeric($response)) {
71
            // Create issuer.
72
            $issuer = $assertion->get_issuer();
73
            if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
74
                $response = $api->put_issuer($issuer);
1441 ariadna 75
                if ($response) {
76
                    $issuerexists = true;
77
                    $issuerentityid = $response->id;
78
                    badges_external_create_mapping(
79
                        $sitebackpack->id,
80
                        OPEN_BADGES_V2_TYPE_ISSUER,
81
                        $issuer['email'],
82
                        $issuerentityid,
83
                    );
1 efrain 84
                }
85
            }
1441 ariadna 86
            if ($issuerexists) {
87
                // Create badge.
88
                $badge = $assertion->get_badge_class(false);
89
                $badgeid = $assertion->get_badge_id();
90
                if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
91
                    $response = $api->put_badgeclass($issuerentityid, $badge);
92
                    if ($response) {
93
                        $badgeentityid = $response->id;
94
                        badges_external_create_mapping(
95
                            $sitebackpack->id,
96
                            OPEN_BADGES_V2_TYPE_BADGE,
97
                            $badgeid,
98
                            $badgeentityid,
99
                        );
100
                    }
1 efrain 101
                }
102
 
1441 ariadna 103
                // Create assertion (Award the badge!).
104
                $assertionentityid = badges_external_get_mapping(
105
                    $sitebackpack->id,
1 efrain 106
                    OPEN_BADGES_V2_TYPE_ASSERTION,
1441 ariadna 107
                    $assertionid
1 efrain 108
                );
109
 
1441 ariadna 110
                if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) {
111
                    $assertionentityid = badges_generate_badgr_open_url(
112
                        $sitebackpack,
113
                        OPEN_BADGES_V2_TYPE_ASSERTION,
114
                        $assertionentityid
115
                    );
1 efrain 116
                }
1441 ariadna 117
 
118
                // Create an assertion for the recipient in the issuer's account.
119
                if (!$assertionentityid) {
120
                    $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
121
                    if ($response) {
122
                        $assertionentityid = badges_generate_badgr_open_url(
123
                            $sitebackpack,
124
                            OPEN_BADGES_V2_TYPE_ASSERTION,
125
                            $response->id,
126
                        );
127
                        $badgeadded = true;
128
                        badges_external_create_mapping(
129
                            $sitebackpack->id,
130
                            OPEN_BADGES_V2_TYPE_ASSERTION,
131
                            $assertionid,
132
                            $response->id,
133
                        );
134
                    }
135
                } else {
136
                    // An assertion already exists. Make sure it's up to date.
137
                    $internalid = badges_external_get_mapping(
138
                        $sitebackpack->id,
139
                        OPEN_BADGES_V2_TYPE_ASSERTION,
140
                        $assertionid,
141
                        'externalid'
142
                    );
143
                    $response = $api->update_assertion($internalid, $assertiondata);
1 efrain 144
                }
145
            }
146
        }
147
    }
148
 
149
    // Now award/upload the badge to the user's account.
150
    // - If a user and site backpack have the same provider we can skip this as Badgr automatically maps recipients
151
    // based on email address.
152
    // - This is only needed when the backpacks are from different regions.
1441 ariadna 153
    if (
154
        $assertionentityid
155
        && (!$issuerexists || !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid))
156
    ) {
1 efrain 157
        $userapi = new \core_badges\backpack_api($userbackpack, $backpack);
158
        $userapi->authenticate();
159
        $response = $userapi->import_badge_assertion($assertionentityid);
1441 ariadna 160
        if ($response) {
161
            $assertionentityid = $response->id;
162
            $badgeadded = true;
163
            badges_external_create_mapping(
164
                $userbackpack->id,
165
                OPEN_BADGES_V2_TYPE_ASSERTION,
166
                $assertionid,
167
                $assertionentityid,
168
            );
1 efrain 169
        }
170
    }
171
 
1441 ariadna 172
    if ($badgeadded) {
173
        $message = get_string('addedtobackpack', 'badges');
174
        $messagetype = \core\output\notification::NOTIFY_SUCCESS;
175
    } else {
176
        if (isset($userapi) && !empty($userapi->get_errors())) {
177
            // If the api used to import the badge to the backpack has errors, show them to inform the user.
178
            if (array_filter($userapi->get_errors(), fn($element) => str_contains($element, "DUPLICATE_BADGE"))) {
179
                // Duplicated badges are displayed as a warning.
180
                $message = get_string('existsinbackpack', 'badges');
181
                $messagetype = \core\output\notification::NOTIFY_WARNING;
182
            } else {
183
                // If the userapi has any other errors, we will use those to inform the user.
184
                $message = get_string(
185
                    'error:cannotsendtobackpack',
186
                    'badges',
187
                    implode($userapi->get_errors()),
188
                );
189
                $messagetype = \core\output\notification::NOTIFY_ERROR;
190
            }
191
        } else if (isset($api) && !empty($api->get_errors())) {
192
            // If the api used to create/update the issuer has errors, show them to inform the user.
193
            $errors = $api->get_errors() ?? [get_string('invalidrequest', 'error')];
194
            $message = get_string(
195
                'error:cannotsendtobackpack',
196
                'badges',
197
                implode($errors),
198
            );
199
            $messagetype = \core\output\notification::NOTIFY_ERROR;
200
        }
201
    }
202
 
203
    redirect(
204
        url: new \core\url('/badges/mybadges.php'),
205
        message: $message,
206
        messagetype: $messagetype,
207
    );
1 efrain 208
} else {
1441 ariadna 209
    redirect(new \core\url('/badges/mybadges.php'));
1 efrain 210
}