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/ -> lib.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   * Defines various library functions.
  19   *
  20   * @package   core_badges
  21   * @copyright 2015 onwards Ankit Agarwal
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Add nodes to myprofile page.
  29   *
  30   * @param \core_user\output\myprofile\tree $tree Tree object
  31   * @param stdClass $user user object
  32   * @param bool $iscurrentuser
  33   * @param stdClass $course Course object
  34   *
  35   * @return bool
  36   */
  37  function core_badges_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
  38      global $CFG, $PAGE, $USER, $SITE;
  39      require_once($CFG->dirroot . '/badges/renderer.php');
  40      if (empty($CFG->enablebadges) || (!empty($course) && empty($CFG->badges_allowcoursebadges))) {
  41          // Y U NO LIKE BADGES ?
  42          return true;
  43      }
  44  
  45      // Add category. This node should appear after 'contact' so that administration block appears towards the end. Refer MDL-49928.
  46      $category = new core_user\output\myprofile\category('badges', get_string('badges', 'badges'), 'contact');
  47      $tree->add_category($category);
  48      $context = context_user::instance($user->id);
  49      $courseid = empty($course) ? 0 : $course->id;
  50  
  51      if ($USER->id == $user->id || has_capability('moodle/badges:viewotherbadges', $context)) {
  52          $records = badges_get_user_badges($user->id, $courseid, null, null, null, true);
  53          $renderer = new core_badges_renderer($PAGE, '');
  54  
  55          // Local badges.
  56          if ($records) {
  57              $title = get_string('localbadgesp', 'badges', format_string($SITE->fullname));
  58              $content = $renderer->print_badges_list($records, $user->id, true);
  59              $localnode = $mybadges = new core_user\output\myprofile\node('badges', 'localbadges', $title, null, null, $content);
  60              $tree->add_node($localnode);
  61          }
  62  
  63          // External badges.
  64          if ($courseid == 0 && !empty($CFG->badges_allowexternalbackpack)) {
  65              $backpack = get_backpack_settings($user->id);
  66              if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
  67                  $title = get_string('externalbadgesp', 'badges');
  68                  $content = $renderer->print_badges_list($backpack->badges, $user->id, true, true);
  69                  $externalnode = $mybadges = new core_user\output\myprofile\node('badges', 'externalbadges', $title, null, null,
  70                      $content);
  71                  $tree->add_node($externalnode);
  72              }
  73          }
  74      }
  75  }
  76  
  77  /**
  78   * Returns badges tagged with a specified tag.
  79   *
  80   * @param object $tag
  81   * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
  82   *             are displayed on the page and the per-page limit may be bigger
  83   * @param null|int $fromctx context id where the link was displayed, may be used by callbacks
  84   *            to display items in the same context first
  85   * @param null|int $ctx context id where to search for records
  86   * @param bool $rec search in subcontexts as well
  87   * @param int $page 0-based number of page being displayed
  88   * @return \core_tag\output\tagindex
  89   */
  90  function badge_get_tagged_badges(object $tag, bool $exclusivemode = false, null|int $fromctx = 0, null|int $ctx = 0,
  91                                   bool $rec = true, int $page = 0): object {
  92      global $OUTPUT;
  93  
  94      $badgecount = $tag->count_tagged_items('core_badges', 'badge');
  95      $perpage = $exclusivemode ? 20 : 5;
  96      $content = '';
  97      $totalpages = ceil($badgecount / $perpage);
  98  
  99      if ($badgecount) {
 100          $badges = $tag->get_tagged_items('core_badges', 'badge', $page * $perpage, $perpage);
 101          $tagfeed = new core_tag\output\tagfeed();
 102          foreach ($badges as $badge) {
 103              $badgelink = new moodle_url('/badges/badgeclass.php', ['id' => $badge->id]);
 104              $fullname = html_writer::link($badgelink, $badge->name);
 105              $icon = html_writer::link($badgelink, html_writer::empty_tag('img',
 106                  ['src' => $OUTPUT->image_url('i/badge')]));
 107              $tagfeed->add($icon, $fullname, '<br>');
 108          }
 109  
 110          $items = $tagfeed->export_for_template($OUTPUT);
 111  
 112          $content .= $OUTPUT->render_from_template('core_tag/tagfeed', $items);
 113      }
 114      return new core_tag\output\tagindex($tag, 'core_badges', 'badge', $content,
 115          $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
 116  }