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/ -> view.php (source)

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Displays available badges to a user
  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  $sortby     = optional_param('sort', 'name', PARAM_ALPHA);
  33  $sorthow    = optional_param('dir', 'DESC', PARAM_ALPHA);
  34  $page       = optional_param('page', 0, PARAM_INT);
  35  
  36  require_login();
  37  
  38  if (empty($CFG->enablebadges)) {
  39      print_error('badgesdisabled', 'badges');
  40  }
  41  
  42  if (empty($CFG->badges_allowcoursebadges) && $courseid != 0) {
  43      print_error('coursebadgesdisabled', 'badges');
  44  }
  45  
  46  if (!in_array($sortby, array('name', 'dateissued'))) {
  47      $sortby = 'name';
  48  }
  49  
  50  if ($sorthow != 'ASC' && $sorthow != 'DESC') {
  51      $sorthow = 'ASC';
  52  }
  53  
  54  if ($page < 0) {
  55      $page = 0;
  56  }
  57  
  58  if ($course = $DB->get_record('course', array('id' => $courseid))) {
  59      $PAGE->set_url('/badges/view.php', array('type' => $type, 'id' => $course->id, 'sort' => $sortby, 'dir' => $sorthow));
  60  } else {
  61      $PAGE->set_url('/badges/view.php', array('type' => $type, 'sort' => $sortby, 'dir' => $sorthow));
  62  }
  63  
  64  if ($type == BADGE_TYPE_SITE) {
  65      $PAGE->set_context(context_system::instance());
  66      $PAGE->set_pagelayout('admin');
  67      $PAGE->set_heading($SITE->fullname);
  68      $title = get_string('sitebadges', 'badges');
  69      $eventotherparams = array('badgetype' => BADGE_TYPE_SITE);
  70  } else {
  71      require_login($course);
  72      $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
  73      $title = $coursename . ': ' . get_string('coursebadges', 'badges');
  74      $PAGE->set_context(context_course::instance($course->id));
  75      $PAGE->set_pagelayout('incourse');
  76      $PAGE->set_heading($coursename);
  77      $eventotherparams = array('badgetype' => BADGE_TYPE_COURSE, 'courseid' => $course->id);
  78  }
  79  
  80  require_capability('moodle/badges:viewbadges', $PAGE->context);
  81  
  82  $PAGE->set_title($title);
  83  $output = $PAGE->get_renderer('core', 'badges');
  84  
  85  echo $output->header();
  86  echo $OUTPUT->heading($title);
  87  
  88  $totalcount = count(badges_get_badges($type, $courseid, '', '', 0, 0, $USER->id));
  89  $records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE, $USER->id);
  90  
  91  if ($totalcount) {
  92      echo $output->heading(get_string('badgestoearn', 'badges', $totalcount), 4);
  93  
  94      if ($course && $course->startdate > time()) {
  95          echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
  96      }
  97  
  98      $badges             = new \core_badges\output\badge_collection($records);
  99      $badges->sort       = $sortby;
 100      $badges->dir        = $sorthow;
 101      $badges->page       = $page;
 102      $badges->perpage    = BADGE_PERPAGE;
 103      $badges->totalcount = $totalcount;
 104  
 105      echo $output->render($badges);
 106  } else {
 107      echo $output->notification(get_string('nobadges', 'badges'));
 108  }
 109  
 110  // Display "Manage badges" button to users with proper capabilities.
 111  $isfrontpage = (empty($courseid) || $courseid == $SITE->id);
 112  if ($isfrontpage) {
 113      $context = context_system::instance();
 114  } else {
 115      $context = context_course::instance($courseid);
 116  }
 117  $canmanage = has_any_capability(array('moodle/badges:viewawarded',
 118                                        'moodle/badges:createbadge',
 119                                        'moodle/badges:awardbadge',
 120                                        'moodle/badges:configurecriteria',
 121                                        'moodle/badges:configuremessages',
 122                                        'moodle/badges:configuredetails',
 123                                        'moodle/badges:deletebadge'), $context);
 124  if ($canmanage) {
 125      echo $output->single_button(new moodle_url('/badges/index.php', array('type' => $type, 'id' => $courseid)),
 126          get_string('managebadges', 'badges'));
 127  }
 128  
 129  // Display "Add new badge" button to users with capability to create badges.
 130  if (has_capability('moodle/badges:createbadge', $PAGE->context)) {
 131      echo $output->single_button(new moodle_url('newbadge.php', array('type' => $type, 'id' => $courseid)),
 132          get_string('newbadge', 'badges'));
 133  }
 134  
 135  // Trigger event, badge listing viewed.
 136  $eventparams = array('context' => $PAGE->context, 'other' => $eventotherparams);
 137  $event = \core\event\badge_listing_viewed::create($eventparams);
 138  $event->trigger();
 139  
 140  echo $output->footer();