Merge branch 'MDL-81472' of https://github.com/paulholden/moodle
[moodle.git] / badges / backpack-export.php
blob60a0ace324817f7ff3b5db09e16c31ddc3b7707f
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 * Export badges to the backpack site.
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>
25 require_once(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/badgeslib.php');
28 $hash = optional_param('hash', null, PARAM_RAW);
30 $PAGE->set_pagelayout('admin');
31 $url = new moodle_url('/badges/backpack-export.php');
33 require_login();
34 if (empty($CFG->badges_allowexternalbackpack) || empty($CFG->enablebadges)) {
35 redirect($CFG->wwwroot);
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');
43 $userbadges = badges_get_user_badges($USER->id);
44 $context = context_user::instance($USER->id);
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');
53 $redirecturl = new moodle_url('/badges/mybadges.php');
54 if ($hash) {
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);
63 redirect($redirecturl);