Merge branch 'MDL-56119-390' of https://github.com/inigozendegi/moodle into MOODLE_39...
[moodle.git] / badges / backpack-export.php
blobe1015b762ff68d6a5426d05876479a0112f2455c
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 if (badges_open_badges_backpack_api() != OPEN_BADGES_V2P1) {
29 throw new coding_exception('backpacks only support Open Badges V2.1');
31 $hash = optional_param('hash', null, PARAM_RAW);
33 $PAGE->set_pagelayout('admin');
34 $url = new moodle_url('/badges/backpack-export.php');
36 require_login();
37 if (empty($CFG->badges_allowexternalbackpack) || empty($CFG->enablebadges)) {
38 redirect($CFG->wwwroot);
40 $backpack = badges_get_site_backpack($CFG->badges_site_backpack);
41 $userbadges = badges_get_user_badges($USER->id);
42 $context = context_user::instance($USER->id);
44 $PAGE->set_context($context);
45 $PAGE->set_url($url);
46 $title = get_string('badges', 'badges');
47 $PAGE->set_title($title);
48 $PAGE->set_heading(fullname($USER));
49 $PAGE->set_pagelayout('standard');
51 $redirecturl = new moodle_url('/badges/mybadges.php');
52 if ($hash) {
53 $backpack = badges_get_site_backpack($CFG->badges_site_backpack);
54 $api = new core_badges\backpack_api2p1($backpack);
55 $notify = $api->put_assertions($hash);
56 if (!empty($notify['status']) && $notify['status'] == \core\output\notification::NOTIFY_SUCCESS) {
57 redirect($redirecturl, $notify['message'], null, \core\output\notification::NOTIFY_SUCCESS);
58 } else if (!empty($notify['status']) && $notify['status'] == \core\output\notification::NOTIFY_ERROR) {
59 redirect($redirecturl, $notify['message'], null, \core\output\notification::NOTIFY_ERROR);
62 redirect($redirecturl);