Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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      throw new \moodle_exception('badgesdisabled', 'badges');
  40  }
  41  
  42  if (empty($CFG->badges_allowcoursebadges) && $courseid != 0) {
  43      throw new \moodle_exception('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  $PAGE->add_body_class('limitedwidth');
  65  
  66  if ($type == BADGE_TYPE_SITE) {
  67      $PAGE->set_context(context_system::instance());
  68      $PAGE->set_pagelayout('admin');
  69      $PAGE->set_heading(get_string('administrationsite'));
  70      $title = get_string('sitebadges', 'badges');
  71      $eventotherparams = array('badgetype' => BADGE_TYPE_SITE);
  72  } else {
  73      require_login($course);
  74      $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
  75      $title = get_string('coursebadges', 'badges');
  76      $PAGE->set_context(context_course::instance($course->id));
  77      $PAGE->set_pagelayout('incourse');
  78      $PAGE->set_heading($coursename);
  79      $eventotherparams = array('badgetype' => BADGE_TYPE_COURSE, 'courseid' => $course->id);
  80  }
  81  
  82  require_capability('moodle/badges:viewbadges', $PAGE->context);
  83  
  84  $PAGE->set_title($title);
  85  $output = $PAGE->get_renderer('core', 'badges');
  86  
  87  // Display "Manage badges" button to users with proper capabilities.
  88  $isfrontpage = (empty($courseid) || $courseid == $SITE->id);
  89  if ($isfrontpage) {
  90      $context = context_system::instance();
  91  } else {
  92      $context = context_course::instance($courseid);
  93  }
  94  $canmanage = has_any_capability(array('moodle/badges:viewawarded',
  95      'moodle/badges:createbadge',
  96      'moodle/badges:awardbadge',
  97      'moodle/badges:configurecriteria',
  98      'moodle/badges:configuremessages',
  99      'moodle/badges:configuredetails',
 100      'moodle/badges:deletebadge'), $context);
 101  
 102  if ($canmanage) {
 103      // Check there are non archived badges on the course.
 104      $allbadgescount = count(badges_get_badges($type, $courseid));
 105      $canmanage = ($allbadgescount > 0);
 106  }
 107  $actionbar = new \core_badges\output\standard_action_bar($PAGE, $type, $canmanage);
 108  echo $output->header();
 109  echo $output->render_tertiary_navigation($actionbar);
 110  echo $OUTPUT->heading($title);
 111  
 112  $totalcount = count(badges_get_badges($type, $courseid, '', '', 0, 0, $USER->id));
 113  $records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE, $USER->id);
 114  
 115  if ($totalcount) {
 116      if ($course && $course->startdate > time()) {
 117          echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
 118      }
 119  
 120      $badges             = new \core_badges\output\badge_collection($records);
 121      $badges->sort       = $sortby;
 122      $badges->dir        = $sorthow;
 123      $badges->page       = $page;
 124      $badges->perpage    = BADGE_PERPAGE;
 125      $badges->totalcount = $totalcount;
 126  
 127      echo $output->render($badges);
 128  } else {
 129      echo $output->notification(get_string('nobadges', 'badges'), 'info');
 130  }
 131  // Trigger event, badge listing viewed.
 132  $eventparams = array('context' => $PAGE->context, 'other' => $eventotherparams);
 133  $event = \core\event\badge_listing_viewed::create($eventparams);
 134  $event->trigger();
 135  
 136  echo $output->footer();