Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/badges/ -> index.php (source)

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

   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  require_once(__DIR__ . '/../config.php');
  28  require_once($CFG->libdir . '/badgeslib.php');
  29  
  30  $type       = required_param('type', PARAM_INT);
  31  $courseid   = optional_param('id', 0, PARAM_INT);
  32  $page       = optional_param('page', 0, PARAM_INT);
  33  $deactivate = optional_param('lock', 0, PARAM_INT);
  34  $sortby     = optional_param('sort', 'name', PARAM_ALPHA);
  35  $sorthow    = optional_param('dir', 'ASC', PARAM_ALPHA);
  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  if (!in_array($sortby, array('name', 'status'))) {
  42      $sortby = 'name';
  43  }
  44  
  45  if ($sorthow != 'ASC' and $sorthow != 'DESC') {
  46      $sorthow = 'ASC';
  47  }
  48  
  49  if ($page < 0) {
  50      $page = 0;
  51  }
  52  
  53  require_login();
  54  
  55  if (empty($CFG->enablebadges)) {
  56      print_error('badgesdisabled', 'badges');
  57  }
  58  
  59  if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
  60      print_error('coursebadgesdisabled', 'badges');
  61  }
  62  
  63  $err = '';
  64  $urlparams = array('sort' => $sortby, 'dir' => $sorthow, 'page' => $page);
  65  
  66  if ($course = $DB->get_record('course', array('id' => $courseid))) {
  67      $urlparams['type'] = $type;
  68      $urlparams['id'] = $course->id;
  69  } else {
  70      $urlparams['type'] = $type;
  71  }
  72  
  73  $hdr = get_string('managebadges', 'badges');
  74  $returnurl = new moodle_url('/badges/index.php', $urlparams);
  75  $PAGE->set_url($returnurl);
  76  
  77  if ($type == BADGE_TYPE_SITE) {
  78      $title = get_string('sitebadges', 'badges');
  79      $PAGE->set_context(context_system::instance());
  80      $PAGE->set_pagelayout('admin');
  81      $PAGE->set_heading($title . ': ' . $hdr);
  82      navigation_node::override_active_url(new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE)), true);
  83  } else {
  84      require_login($course);
  85      $coursecontext = context_course::instance($course->id);
  86      $title = get_string('coursebadges', 'badges');
  87      $PAGE->set_context($coursecontext);
  88      $PAGE->set_pagelayout('incourse');
  89      $PAGE->set_heading(format_string($course->fullname, true, array('context' => $coursecontext)) . ': ' . $hdr);
  90      navigation_node::override_active_url(
  91          new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id))
  92      );
  93  }
  94  
  95  if (!has_any_capability(array(
  96          'moodle/badges:viewawarded',
  97          'moodle/badges:createbadge',
  98          'moodle/badges:awardbadge',
  99          'moodle/badges:configurecriteria',
 100          'moodle/badges:configuremessages',
 101          'moodle/badges:configuredetails',
 102          'moodle/badges:deletebadge'), $PAGE->context)) {
 103      redirect($CFG->wwwroot);
 104  }
 105  
 106  $PAGE->set_title($hdr);
 107  $output = $PAGE->get_renderer('core', 'badges');
 108  
 109  if (($delete || $archive) && has_capability('moodle/badges:deletebadge', $PAGE->context)) {
 110      $badgeid = ($archive != 0) ? $archive : $delete;
 111      $badge = new badge($badgeid);
 112      if (!$confirm) {
 113          echo $output->header();
 114          // Archive this badge?
 115          echo $output->heading(get_string('archivebadge', 'badges', $badge->name));
 116          $archivebutton = $output->single_button(
 117                              new moodle_url($PAGE->url, array('archive' => $badge->id, 'confirm' => 1)),
 118                              get_string('archiveconfirm', 'badges'));
 119          echo $output->box(get_string('archivehelp', 'badges') . $archivebutton, 'generalbox');
 120  
 121          // Delete this badge?
 122          echo $output->heading(get_string('delbadge', 'badges', $badge->name));
 123          $deletebutton = $output->single_button(
 124                              new moodle_url($PAGE->url, array('delete' => $badge->id, 'confirm' => 1)),
 125                              get_string('delconfirm', 'badges'));
 126          echo $output->box(get_string('deletehelp', 'badges') . $deletebutton, 'generalbox');
 127  
 128          // Go back.
 129          echo $output->action_link($returnurl, get_string('cancel'));
 130  
 131          echo $output->footer();
 132          die();
 133      } else {
 134          require_sesskey();
 135          $archiveonly = ($archive != 0) ? true : false;
 136          $badge->delete($archiveonly);
 137          redirect($returnurl);
 138      }
 139  }
 140  
 141  if ($deactivate && has_capability('moodle/badges:configuredetails', $PAGE->context)) {
 142      require_sesskey();
 143      $badge = new badge($deactivate);
 144      if ($badge->is_locked()) {
 145          $badge->set_status(BADGE_STATUS_INACTIVE_LOCKED);
 146      } else {
 147          $badge->set_status(BADGE_STATUS_INACTIVE);
 148      }
 149      $msg = 'deactivatesuccess';
 150      $returnurl->param('msg', $msg);
 151      redirect($returnurl);
 152  }
 153  
 154  echo $OUTPUT->header();
 155  if ($type == BADGE_TYPE_SITE) {
 156      echo $OUTPUT->heading_with_help($PAGE->heading, 'sitebadges', 'badges');
 157  } else {
 158      echo $OUTPUT->heading($PAGE->heading);
 159  }
 160  echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
 161  
 162  $totalcount = count(badges_get_badges($type, $courseid, '', '' , 0, 0));
 163  $records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE);
 164  
 165  if ($totalcount) {
 166      echo $output->heading(get_string('badgestoearn', 'badges', $totalcount), 4);
 167  
 168      if ($course && $course->startdate > time()) {
 169          echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
 170      }
 171  
 172      if ($err !== '') {
 173          echo $OUTPUT->notification($err, 'notifyproblem');
 174      }
 175  
 176      if ($msg !== '') {
 177          echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
 178      }
 179  
 180      $badges             = new \core_badges\output\badge_management($records);
 181      $badges->sort       = $sortby;
 182      $badges->dir        = $sorthow;
 183      $badges->page       = $page;
 184      $badges->perpage    = BADGE_PERPAGE;
 185      $badges->totalcount = $totalcount;
 186  
 187      echo $output->render($badges);
 188  } else {
 189      echo $output->notification(get_string('nobadges', 'badges'));
 190  
 191      if (has_capability('moodle/badges:createbadge', $PAGE->context)) {
 192          echo $OUTPUT->box($OUTPUT->single_button(new moodle_url('newbadge.php', array('type' => $type, 'id' => $courseid)),
 193              get_string('newbadge', 'badges')));
 194      }
 195  }
 196  
 197  echo $OUTPUT->footer();