Merge branch 'MDL-67166-master' of git://github.com/bmbrands/moodle
[moodle.git] / badges / backpacks.php
blob341d511f47b853dc136625dcdf6405e0fcb96091
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 * Display a list of badge backpacks for the site.
20 * @package core_badges
21 * @copyright 2019 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/badgeslib.php');
27 $context = context_system::instance();
28 $PAGE->set_context($context);
30 require_login(0, false);
31 require_capability('moodle/badges:manageglobalsettings', $context);
32 // There should be an admin setting to completely turn off badges.
33 $output = $PAGE->get_renderer('core', 'badges');
35 $id = optional_param('id', 0, PARAM_INT);
36 $action = optional_param('action', '', PARAM_ALPHA);
38 $PAGE->set_pagelayout('admin');
39 $url = new moodle_url('/badges/backpacks.php');
41 if (empty($CFG->badges_allowexternalbackpack)) {
42 redirect($CFG->wwwroot);
45 $PAGE->set_url($url);
46 $PAGE->set_title(get_string('managebackpacks', 'badges'));
47 $PAGE->set_heading($SITE->fullname);
48 if ($action == 'edit') {
49 $backpack = null;
50 if (!empty($id)) {
51 $backpack = badges_get_site_backpack($id);
53 $form = new \core_badges\form\external_backpack(null, ['externalbackpack' => $backpack]);
54 if ($form->is_cancelled()) {
55 redirect($url);
56 } else if ($data = $form->get_data()) {
57 require_sesskey();
58 if (!empty($data->id)) {
59 badges_update_site_backpack($data->id, $data);
60 } else {
61 badges_create_site_backpack($data);
63 redirect($url);
66 echo $OUTPUT->header();
67 echo $output->heading(get_string('managebackpacks', 'badges'));
69 $form->display();
70 } else {
71 echo $OUTPUT->header();
72 echo $output->heading(get_string('managebackpacks', 'badges'));
74 $page = new \core_badges\output\external_backpacks_page($url);
75 echo $output->render($page);
78 echo $OUTPUT->footer();