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.

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