Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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