Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 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   * Issued badge renderable.
  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  namespace core_badges\output;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->libdir . '/badgeslib.php');
  32  
  33  use context_course;
  34  use context_system;
  35  use stdClass;
  36  use renderable;
  37  use core_badges\badge;
  38  use moodle_url;
  39  use renderer_base;
  40  
  41  /**
  42   * An issued badges for badge.php page
  43   *
  44   * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class issued_badge implements renderable {
  48      /** @var issued badge */
  49      public $issued;
  50  
  51      /** @var badge recipient */
  52      public $recipient;
  53  
  54      /** @var badge class */
  55      public $badgeclass;
  56  
  57      /** @var badge visibility to others */
  58      public $visible = 0;
  59  
  60      /** @var badge class */
  61      public $badgeid = 0;
  62  
  63      /** @var unique hash identifying the issued badge */
  64      public $hash;
  65  
  66      /**
  67       * Initializes the badge to display
  68       *
  69       * @param string $hash Issued badge hash
  70       */
  71      public function __construct($hash) {
  72          global $DB;
  73  
  74          $this->hash = $hash;
  75          $assertion = new \core_badges_assertion($hash, badges_open_badges_backpack_api());
  76          $this->issued = $assertion->get_badge_assertion();
  77          if (!is_numeric($this->issued['issuedOn'])) {
  78              $this->issued['issuedOn'] = strtotime($this->issued['issuedOn']);
  79          }
  80          $this->badgeclass = $assertion->get_badge_class();
  81  
  82          $rec = $DB->get_record_sql('SELECT userid, visible, badgeid
  83                  FROM {badge_issued}
  84                  WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
  85                  array('hash' => $hash), IGNORE_MISSING);
  86          if ($rec) {
  87              // Get a recipient from database.
  88              $userfieldsapi = \core_user\fields::for_name();
  89              $namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
  90              $user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
  91                          FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
  92              $this->recipient = $user;
  93              $this->visible = $rec->visible;
  94              $this->badgeid = $rec->badgeid;
  95          }
  96      }
  97  
  98      /**
  99       * Export this data so it can be used as the context for a mustache template.
 100       *
 101       * @param renderer_base $output Renderer base.
 102       * @return stdClass
 103       */
 104      public function export_for_template(renderer_base $output): stdClass {
 105          global $CFG, $DB, $SITE, $USER;
 106  
 107          $now = time();
 108          if (isset($this->issued['expires'])) {
 109              if (!is_numeric($this->issued['expires'])) {
 110                  $this->issued['expires'] = strtotime($this->issued['expires']);
 111              }
 112              $expiration = $this->issued['expires'];
 113          } else {
 114              $expiration = $now + 86400;
 115          }
 116  
 117          $context = null;
 118          $data = new stdClass();
 119          $badge = new badge($this->badgeid);
 120          if ($badge->type == BADGE_TYPE_COURSE && isset($badge->courseid)) {
 121              $context = context_course::instance($badge->courseid);
 122              $data->coursefullname = format_string($DB->get_field('course', 'fullname', ['id' => $badge->courseid]),
 123                  true, ['context' => $context]);
 124          } else {
 125              $context = context_system::instance();
 126              $data->sitefullname = format_string($SITE->fullname, true, ['context' => $context]);
 127          }
 128  
 129          // Field: Image.
 130          $data->badgeimage = is_array($this->badgeclass['image']) ? $this->badgeclass['image']['id'] : $this->badgeclass['image'];
 131  
 132          // Field: Expiration date.
 133          if (isset($this->issued['expires'])) {
 134              if ($expiration < $now) {
 135                  $data->expireddate = $this->issued['expires'];
 136                  $data->expireddateformatted = userdate($this->issued['expires'], get_string('strftimedatetime', 'langconfig'));
 137              } else {
 138                  $data->expiredate = $this->issued['expires'];
 139              }
 140          }
 141  
 142          // Fields: Name, description, issuedOn.
 143          $data->badgename = $badge->name;
 144          $data->badgedescription = $badge->description;
 145          $data->badgeissuedon = $this->issued['issuedOn'];
 146  
 147          // Field: Recipient (the badge was awarded to this person).
 148          if ($this->recipient->deleted) {
 149              $strdata = new stdClass();
 150              $strdata->user = fullname($this->recipient);
 151              $strdata->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
 152              $data->recipientname = get_string('error:userdeleted', 'badges', $strdata);
 153          } else {
 154              $data->recipientname = fullname($this->recipient);
 155          }
 156  
 157          // Field: Criteria.
 158          // This method will return the HTML with the badge criteria.
 159          $data->criteria = $output->print_badge_criteria($badge);
 160  
 161          // Field: Issuer.
 162          $data->issuedby = format_string($badge->issuername, true, ['context' => $context]);
 163          if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
 164              $data->issuedbyemailobfuscated = obfuscate_mailto($badge->issuercontact, $data->issuedby);
 165          }
 166  
 167          // Fields: Other details, such as language or version.
 168          $data->hasotherfields = false;
 169          if (!empty($badge->language)) {
 170              $data->hasotherfields = true;
 171              $languages = get_string_manager()->get_list_of_languages();
 172              $data->language = $languages[$badge->language];
 173          }
 174          if (!empty($badge->version)) {
 175              $data->hasotherfields = true;
 176              $data->version = $badge->version;
 177          }
 178          if (!empty($badge->imageauthorname)) {
 179              $data->hasotherfields = true;
 180              $data->imageauthorname = $badge->imageauthorname;
 181          }
 182          if (!empty($badge->imageauthoremail)) {
 183              $data->hasotherfields = true;
 184              $data->imageauthoremail = obfuscate_mailto($badge->imageauthoremail, $badge->imageauthoremail);
 185          }
 186          if (!empty($badge->imageauthorurl)) {
 187              $data->hasotherfields = true;
 188              $data->imageauthorurl = $badge->imageauthorurl;
 189          }
 190          if (!empty($badge->imagecaption)) {
 191              $data->hasotherfields = true;
 192              $data->imagecaption = $badge->imagecaption;
 193          }
 194  
 195          // Field: Endorsement.
 196          $endorsement = $badge->get_endorsement();
 197          if (!empty($endorsement)) {
 198              $data->hasotherfields = true;
 199              $endorsement = $badge->get_endorsement();
 200              $endorsement->issueremail = obfuscate_mailto($endorsement->issueremail, $endorsement->issueremail);
 201              $data->endorsement = (array) $endorsement;
 202          }
 203  
 204          // Field: Related badges.
 205          $relatedbadges = $badge->get_related_badges(true);
 206          if (!empty($relatedbadges)) {
 207              $data->hasotherfields = true;
 208              $data->hasrelatedbadges = true;
 209              $data->relatedbadges = [];
 210              foreach ($relatedbadges as $related) {
 211                  if (isloggedin() && ($context instanceof context_course && !is_guest($context))) {
 212                      $related->url = (new moodle_url('/badges/overview.php', ['id' => $related->id]))->out(false);
 213                  }
 214                  $data->relatedbadges[] = (array)$related;
 215              }
 216          }
 217  
 218          // Field: Alignments.
 219          $alignments = $badge->get_alignments();
 220          if (!empty($alignments)) {
 221              $data->hasotherfields = true;
 222              $data->hasalignments = true;
 223              $data->alignments = [];
 224              foreach ($alignments as $alignment) {
 225                  $data->alignments[] = (array)$alignment;
 226              }
 227          }
 228  
 229          // Buttons to display.
 230          if ($USER->id == $this->recipient->id && !empty($CFG->enablebadges)) {
 231              $data->downloadurl = (new moodle_url('/badges/badge.php', ['hash' => $this->hash, 'bake' => true]))->out(false);
 232  
 233              if (!empty($CFG->badges_allowexternalbackpack) && ($expiration > $now)
 234                  && $userbackpack = badges_get_user_backpack($USER->id)) {
 235  
 236                  if (badges_open_badges_backpack_api($userbackpack->id) == OPEN_BADGES_V2P1) {
 237                      $addtobackpackurl = new moodle_url('/badges/backpack-export.php', ['hash' => $this->hash]);
 238                  } else {
 239                      $addtobackpackurl = new moodle_url('/badges/backpack-add.php', ['hash' => $this->hash]);
 240                  }
 241                  $data->addtobackpackurl = $addtobackpackurl->out(false);
 242              }
 243          }
 244  
 245          return $data;
 246      }
 247  }