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 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  namespace core_badges\output;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->libdir . '/badgeslib.php');
  22  
  23  use coding_exception;
  24  use context_course;
  25  use stdClass;
  26  use renderable;
  27  use core_badges\badge;
  28  use moodle_url;
  29  use renderer_base;
  30  
  31  /**
  32   * Page to display badge information, such as name, description or criteria. This information is unrelated to assertions.
  33   *
  34   * @package    core_badges
  35   * @copyright  2022 Sara Arjona (sara@moodle.com)
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class badgeclass implements renderable {
  39  
  40      /** @var badge class */
  41      public $badge;
  42  
  43      /** @var badge class */
  44      public $badgeid = 0;
  45  
  46      /** @var \context The badge context*/
  47      public $context;
  48  
  49      /**
  50       * Initializes the badge to display.
  51       *
  52       * @param int $id Id of the badge to display.
  53       */
  54      public function __construct(int $id) {
  55          $this->badgeid = $id;
  56          $this->badge = new badge($this->badgeid);
  57          if ($this->badge->status == BADGE_STATUS_INACTIVE) {
  58              // Inactive badges that haven't been published previously can't be displayed.
  59              $this->badge = null;
  60          } else {
  61              $this->context = $this->badge->get_context();
  62          }
  63      }
  64  
  65      /**
  66       * Export this data so it can be used as the context for a mustache template.
  67       *
  68       * @param renderer_base $output Renderer base.
  69       * @return stdClass
  70       */
  71      public function export_for_template(renderer_base $output): stdClass {
  72          global $DB, $SITE;
  73  
  74          $data = new stdClass();
  75          if ($this->context instanceof context_course) {
  76              $data->coursefullname = format_string($DB->get_field('course', 'fullname', ['id' => $this->badge->courseid]),
  77                  true, ['context' => $this->context]);
  78          } else {
  79              $data->sitefullname = format_string($SITE->fullname, true, ['context' => $this->context]);
  80          }
  81  
  82          // Field: Image.
  83          $storage = get_file_storage();
  84          $imagefile = $storage->get_file($this->context->id, 'badges', 'badgeimage', $this->badgeid, '/', 'f3.png');
  85          if ($imagefile) {
  86              $imagedata = base64_encode($imagefile->get_content());
  87          } else {
  88              if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
  89                  // Unit tests the file might not exist yet.
  90                  $imagedata = '';
  91              } else {
  92                  throw new coding_exception('Image file does not exist.');
  93              }
  94          }
  95          $data->badgeimage = 'data:image/png;base64,' . $imagedata;
  96  
  97          // Fields: Name, description.
  98          $data->badgename = $this->badge->name;
  99          $data->badgedescription = $this->badge->description;
 100  
 101          // Field: Criteria.
 102          // This method will return the HTML with the badge criteria.
 103          $data->criteria = $output->print_badge_criteria($this->badge);
 104  
 105          // Field: Issuer.
 106          $data->issuedby = format_string($this->badge->issuername, true, ['context' => $this->context]);
 107          if (isset($this->badge->issuercontact) && !empty($this->badge->issuercontact)) {
 108              $data->issuedbyemailobfuscated = obfuscate_mailto($this->badge->issuercontact, $data->issuedby);
 109          }
 110  
 111          // Fields: Other details, such as language or version.
 112          $data->hasotherfields = false;
 113          if (!empty($this->badge->language)) {
 114              $data->hasotherfields = true;
 115              $languages = get_string_manager()->get_list_of_languages();
 116              $data->language = $languages[$this->badge->language];
 117          }
 118          if (!empty($this->badge->version)) {
 119              $data->hasotherfields = true;
 120              $data->version = $this->badge->version;
 121          }
 122          if (!empty($this->badge->imageauthorname)) {
 123              $data->hasotherfields = true;
 124              $data->imageauthorname = $this->badge->imageauthorname;
 125          }
 126          if (!empty($this->badge->imageauthoremail)) {
 127              $data->hasotherfields = true;
 128              $data->imageauthoremail = obfuscate_mailto($this->badge->imageauthoremail, $this->badge->imageauthoremail);
 129          }
 130          if (!empty($this->badge->imageauthorurl)) {
 131              $data->hasotherfields = true;
 132              $data->imageauthorurl = $this->badge->imageauthorurl;
 133          }
 134          if (!empty($this->badge->imagecaption)) {
 135              $data->hasotherfields = true;
 136              $data->imagecaption = $this->badge->imagecaption;
 137          }
 138  
 139          // Field: Endorsement.
 140          $endorsement = $this->badge->get_endorsement();
 141          if (!empty($endorsement)) {
 142              $data->hasotherfields = true;
 143              $endorsement = $this->badge->get_endorsement();
 144              $endorsement->issueremail = obfuscate_mailto($endorsement->issueremail, $endorsement->issueremail);
 145              $data->endorsement = (array) $endorsement;
 146          }
 147  
 148          // Field: Related badges.
 149          $relatedbadges = $this->badge->get_related_badges(true);
 150          if (!empty($relatedbadges)) {
 151              $data->hasotherfields = true;
 152              $data->hasrelatedbadges = true;
 153              $data->relatedbadges = [];
 154              foreach ($relatedbadges as $related) {
 155                  if (isloggedin() && !is_guest($this->context)) {
 156                      $related->url = (new moodle_url('/badges/overview.php', ['id' => $related->id]))->out(false);
 157                  }
 158                  $data->relatedbadges[] = (array)$related;
 159              }
 160          }
 161  
 162          // Field: Alignments.
 163          $alignments = $this->badge->get_alignments();
 164          if (!empty($alignments)) {
 165              $data->hasotherfields = true;
 166              $data->hasalignments = true;
 167              $data->alignments = [];
 168              foreach ($alignments as $alignment) {
 169                  $data->alignments[] = (array)$alignment;
 170              }
 171          }
 172  
 173          return $data;
 174      }
 175  }