Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/badges/ -> index.php (source)

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

   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   * Page for badges management
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  25   */
  26  
  27  use core_badges\reportbuilder\local\systemreports\badges;
  28  use core_reportbuilder\system_report_factory;
  29  
  30  require_once(__DIR__ . '/../config.php');
  31  require_once($CFG->libdir . '/badgeslib.php');
  32  
  33  $type       = required_param('type', PARAM_INT);
  34  $courseid   = optional_param('id', 0, PARAM_INT);
  35  $deactivate = optional_param('lock', 0, PARAM_INT);
  36  $confirm    = optional_param('confirm', false, PARAM_BOOL);
  37  $delete     = optional_param('delete', 0, PARAM_INT);
  38  $archive    = optional_param('archive', 0, PARAM_INT);
  39  $msg        = optional_param('msg', '', PARAM_TEXT);
  40  
  41  require_login();
  42  
  43  if (empty($CFG->enablebadges)) {
  44      throw new \moodle_exception('badgesdisabled', 'badges');
  45  }
  46  
  47  if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
  48      throw new \moodle_exception('coursebadgesdisabled', 'badges');
  49  }
  50  
  51  $urlparams = ['type' => $type];
  52  
  53  if ($course = $DB->get_record('course', ['id' => $courseid])) {
  54      $urlparams['id'] = $course->id;
  55  }
  56  
  57  $hdr = get_string('managebadges', 'badges');
  58  $returnurl = new moodle_url('/badges/index.php', $urlparams);
  59  $PAGE->set_url($returnurl);
  60  $PAGE->add_body_class('limitedwidth');
  61  
  62  if ($type == BADGE_TYPE_SITE) {
  63      $title = get_string('sitebadges', 'badges');
  64      $PAGE->set_context(context_system::instance());
  65      $PAGE->set_pagelayout('admin');
  66      $PAGE->set_heading(get_string('administrationsite'));
  67      navigation_node::override_active_url(new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE)), true);
  68  } else {
  69      require_login($course);
  70      $coursecontext = context_course::instance($course->id);
  71      $title = get_string('coursebadges', 'badges');
  72      $PAGE->set_context($coursecontext);
  73      $PAGE->set_pagelayout('incourse');
  74      $PAGE->set_heading(format_string($course->fullname, true, array('context' => $coursecontext)));
  75      navigation_node::override_active_url(
  76          new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id))
  77      );
  78  }
  79  
  80  if (!has_any_capability(array(
  81          'moodle/badges:viewawarded',
  82          'moodle/badges:createbadge',
  83          'moodle/badges:awardbadge',
  84          'moodle/badges:configurecriteria',
  85          'moodle/badges:configuremessages',
  86          'moodle/badges:configuredetails',
  87          'moodle/badges:deletebadge'), $PAGE->context)) {
  88      redirect($CFG->wwwroot);
  89  }
  90  
  91  $PAGE->set_title($hdr);
  92  $output = $PAGE->get_renderer('core', 'badges');
  93  
  94  if (($delete || $archive) && has_capability('moodle/badges:deletebadge', $PAGE->context)) {
  95      $badgeid = ($archive != 0) ? $archive : $delete;
  96      $badge = new badge($badgeid);
  97      if (!$confirm) {
  98          echo $output->header();
  99          // Archive this badge?
 100          echo $output->heading(get_string('archivebadge', 'badges', $badge->name));
 101          $archivebutton = $output->single_button(
 102                              new moodle_url($PAGE->url, array('archive' => $badge->id, 'confirm' => 1)),
 103                              get_string('archiveconfirm', 'badges'));
 104          echo $output->box(get_string('archivehelp', 'badges') . $archivebutton, 'generalbox');
 105  
 106          // Delete this badge?
 107          echo $output->heading(get_string('delbadge', 'badges', $badge->name));
 108          $deletebutton = $output->single_button(
 109                              new moodle_url($PAGE->url, array('delete' => $badge->id, 'confirm' => 1)),
 110                              get_string('delconfirm', 'badges'));
 111          echo $output->box(get_string('deletehelp', 'badges') . $deletebutton, 'generalbox');
 112  
 113          // Go back.
 114          echo $output->action_link($returnurl, get_string('cancel'));
 115  
 116          echo $output->footer();
 117          die();
 118      } else {
 119          require_sesskey();
 120          $archiveonly = ($archive != 0) ? true : false;
 121          $badge->delete($archiveonly);
 122          redirect($returnurl);
 123      }
 124  }
 125  
 126  if ($deactivate && has_capability('moodle/badges:configuredetails', $PAGE->context)) {
 127      require_sesskey();
 128      $badge = new badge($deactivate);
 129      if ($badge->is_locked()) {
 130          $badge->set_status(BADGE_STATUS_INACTIVE_LOCKED);
 131      } else {
 132          $badge->set_status(BADGE_STATUS_INACTIVE);
 133      }
 134      $msg = 'deactivatesuccess';
 135      $returnurl->param('msg', $msg);
 136      redirect($returnurl);
 137  }
 138  
 139  echo $OUTPUT->header();
 140  $backurl = $type == BADGE_TYPE_SITE ? null : new moodle_url('/badges/view.php', ['type' => $type, 'id' => $courseid]);
 141  $actionbar = new \core_badges\output\standard_action_bar($PAGE, $type, false, true, $backurl);
 142  echo $output->render_tertiary_navigation($actionbar);
 143  
 144  if ($type == BADGE_TYPE_SITE) {
 145      echo $OUTPUT->heading_with_help($hdr, 'sitebadges', 'badges');
 146  } else {
 147      echo $OUTPUT->heading($hdr);
 148  }
 149  
 150  echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
 151  
 152  if ($course && $course->startdate > time()) {
 153      echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
 154  }
 155  
 156  if ($msg !== '') {
 157      echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
 158  }
 159  
 160  $report = system_report_factory::create(badges::class, $PAGE->context);
 161  $report->set_default_no_results_notice(new lang_string('nobadges', 'badges'));
 162  
 163  echo $report->output();
 164  
 165  echo $OUTPUT->footer();