Commit | Line | Data |
---|---|---|
15a00bea TT |
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 | * Export badges to the backpack site. | |
19 | * | |
20 | * @package core_badges | |
21 | * @copyright 2020 Tung Thai | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com> | |
24 | */ | |
25 | require_once(__DIR__ . '/../config.php'); | |
26 | require_once($CFG->libdir . '/badgeslib.php'); | |
27 | ||
15a00bea TT |
28 | $hash = optional_param('hash', null, PARAM_RAW); |
29 | ||
30 | $PAGE->set_pagelayout('admin'); | |
31 | $url = new moodle_url('/badges/backpack-export.php'); | |
32 | ||
33 | require_login(); | |
34 | if (empty($CFG->badges_allowexternalbackpack) || empty($CFG->enablebadges)) { | |
35 | redirect($CFG->wwwroot); | |
36 | } | |
d3af9592 PD |
37 | |
38 | $backpack = badges_get_user_backpack(); | |
39 | if (badges_open_badges_backpack_api($backpack->id) != OPEN_BADGES_V2P1) { | |
40 | throw new coding_exception('backpacks only support Open Badges V2.1'); | |
41 | } | |
42 | ||
15a00bea TT |
43 | $userbadges = badges_get_user_badges($USER->id); |
44 | $context = context_user::instance($USER->id); | |
45 | ||
46 | $PAGE->set_context($context); | |
47 | $PAGE->set_url($url); | |
48 | $title = get_string('badges', 'badges'); | |
49 | $PAGE->set_title($title); | |
50 | $PAGE->set_heading(fullname($USER)); | |
51 | $PAGE->set_pagelayout('standard'); | |
52 | ||
53 | $redirecturl = new moodle_url('/badges/mybadges.php'); | |
54 | if ($hash) { | |
15a00bea TT |
55 | $api = new core_badges\backpack_api2p1($backpack); |
56 | $notify = $api->put_assertions($hash); | |
57 | if (!empty($notify['status']) && $notify['status'] == \core\output\notification::NOTIFY_SUCCESS) { | |
58 | redirect($redirecturl, $notify['message'], null, \core\output\notification::NOTIFY_SUCCESS); | |
59 | } else if (!empty($notify['status']) && $notify['status'] == \core\output\notification::NOTIFY_ERROR) { | |
60 | redirect($redirecturl, $notify['message'], null, \core\output\notification::NOTIFY_ERROR); | |
61 | } | |
62 | } | |
63 | redirect($redirecturl); |