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 * Display a list of badge backpacks for the site.
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
25require_once(__DIR__ . '/../config.php');
26require_once($CFG->libdir . '/badgeslib.php');
27$context = context_system::instance();
28$PAGE->set_context($context);
29
30require_login(0, false);
31require_capability('moodle/badges:manageglobalsettings', $context);
32
33$id = optional_param('id', 0, PARAM_INT);
34$action = optional_param('action', '', PARAM_ALPHA);
35$confirm = optional_param('confirm', 1, PARAM_BOOL);
36
37$PAGE->set_pagelayout('admin');
38$url = new moodle_url('/badges/backpacks.php');
39
40if (empty($CFG->badges_allowexternalbackpack)) {
41    redirect($CFG->wwwroot);
42}
43
44$PAGE->set_url($url);
45$PAGE->set_title(get_string('managebackpacks', 'badges'));
46$PAGE->set_heading($SITE->fullname);
47
48$output = $PAGE->get_renderer('core', 'badges');
49
50$msg = '';
51$msgtype = 'error';
52if ($action == 'delete' && $confirm && confirm_sesskey()) {
53    if (badges_delete_site_backpack($id)) {
54        $msg = get_string('sitebackpackdeleted', 'badges');
55        $msgtype = 'notifysuccess';
56    } else {
57        $msg = get_string('sitebackpacknotdeleted', 'badges');
58    }
59} else if ($action == 'moveup' || $action == 'movedown') {
60    // If no backpack has been selected, there isn't anything to move.
61    if (empty($id)) {
62        redirect($url);
63    }
64
65    $direction = BACKPACK_MOVE_DOWN;
66    if ($action == 'moveup') {
67        $direction = BACKPACK_MOVE_UP;
68    }
69    badges_change_sortorder_backpacks($id, $direction);
70}
71
72if ($action == 'edit') {
73    $backpack = null;
74    if (!empty($id)) {
75        $backpack = badges_get_site_backpack($id);
76    }
77    $form = new \core_badges\form\external_backpack(null, ['externalbackpack' => $backpack]);
78    if ($form->is_cancelled()) {
79        redirect($url);
80    } else if ($data = $form->get_data()) {
81        require_sesskey();
82        if (!empty($data->id)) {
83            $id = $data->id;
84            badges_update_site_backpack($id, $data);
85            // Apart from the password, any change here would result in an error in other parts of the badge systems.
86            // In order to negate this, we restart any further mapping from scratch.
87            badges_external_delete_mappings($id);
88        } else {
89            badges_create_site_backpack($data);
90        }
91        redirect($url);
92    }
93
94    echo $OUTPUT->header();
95    echo $output->heading(get_string('managebackpacks', 'badges'));
96
97    $form->display();
98} else if ($action == 'test') {
99    // If no backpack has been selected, there isn't anything to test.
100    if (empty($id)) {
101        redirect($url);
102    }
103
104    echo $OUTPUT->header();
105    echo $output->render_test_backpack_result($id);
106} else {
107    echo $OUTPUT->header();
108    echo $output->heading(get_string('managebackpacks', 'badges'));
109
110    if ($msg) {
111        echo $OUTPUT->notification($msg, $msgtype);
112    }
113    $page = new \core_badges\output\external_backpacks_page($url);
114    echo $output->render($page);
115}
116
117echo $OUTPUT->footer();
118