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 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   * Serve BadgeClass JSON for related badge.
  19   *
  20   * @package    core
  21   * @subpackage badges
  22   * @copyright  2018 Tung Thai
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
  25   */
  26  define('AJAX_SCRIPT', true);
  27  define('NO_MOODLE_COOKIES', true); // No need for a session here.
  28  
  29  require_once(__DIR__ . '/../config.php');
  30  require_once($CFG->libdir . '/badgeslib.php');
  31  
  32  $id = required_param('id', PARAM_INT);
  33  $action = optional_param('action', null, PARAM_INT); // Generates badge class if true.
  34  $json = array();
  35  $badge = new badge($id);
  36  if ($badge->status != BADGE_STATUS_INACTIVE) {
  37      if (is_null($action)) {
  38          // Get the content of badge class.
  39          if (empty($badge->courseid)) {
  40              $context = context_system::instance();
  41          } else {
  42              $context = context_course::instance($badge->courseid);
  43          }
  44          $urlimage = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f3')->out(false);
  45  
  46          $url = new moodle_url('/badges/badge_json.php', array('id' => $badge->id));
  47  
  48          $json['name'] = $badge->name;
  49          $json['description'] = $badge->description;
  50          if ($badge->imageauthorname ||
  51                  $badge->imageauthoremail ||
  52                  $badge->imageauthorurl ||
  53                  $badge->imagecaption) {
  54              $urlimage = moodle_url::make_pluginfile_url($context->id,
  55                  'badges', 'badgeimage', $badge->id, '/', 'f3')->out(false);
  56              $json['image'] = array();
  57              $json['image']['id'] = $urlimage;
  58              if ($badge->imageauthorname || $badge->imageauthoremail || $badge->imageauthorurl) {
  59                  $authorimage = new moodle_url('/badges/image_author_json.php', array('id' => $badge->id));
  60                  $json['image']['author'] = $authorimage->out(false);
  61              }
  62              if ($badge->imagecaption) {
  63                  $json['image']['caption'] = $badge->imagecaption;
  64              }
  65          } else {
  66              $json['image'] = $urlimage;
  67          }
  68  
  69          $params = ['id' => $badge->id];
  70          $badgeurl = new moodle_url('/badges/badgeclass.php', $params);
  71          $json['criteria']['id'] = $badgeurl->out(false);
  72          $json['criteria']['narrative'] = $badge->markdown_badge_criteria();
  73          $json['issuer'] = $badge->get_badge_issuer();
  74          $json['@context'] = OPEN_BADGES_V2_CONTEXT;
  75          $json['id'] = $url->out();
  76          $json['type'] = OPEN_BADGES_V2_TYPE_BADGE;
  77          if (!empty($badge->version)) {
  78              $json['version'] = $badge->version;
  79          }
  80          if (!empty($badge->language)) {
  81              $json['@language'] = $badge->language;
  82          }
  83  
  84          $relatedbadges = $badge->get_related_badges(true);
  85          if (!empty($relatedbadges)) {
  86              foreach ($relatedbadges as $related) {
  87                  $relatedurl = new moodle_url('/badges/badge_json.php', array('id' => $related->id));
  88                  $relateds[] = array('id' => $relatedurl->out(false),
  89                      'version' => $related->version, '@language' => $related->language);
  90              }
  91               $json['related'] = $relateds;
  92          }
  93  
  94          $endorsement = $badge->get_endorsement();
  95          if (!empty($endorsement)) {
  96              $endorsementurl = new moodle_url('/badges/endorsement_json.php', array('id' => $badge->id));
  97              $json['endorsement'] = $endorsementurl->out(false);
  98          }
  99  
 100          $alignments = $badge->get_alignments();
 101          if (!empty($alignments)) {
 102              foreach ($alignments as $item) {
 103                  $alignment = array('targetName' => $item->targetname, 'targetUrl' => $item->targeturl);
 104                  if ($item->targetdescription) {
 105                      $alignment['targetDescription'] = $item->targetdescription;
 106                  }
 107                  if ($item->targetframework) {
 108                      $alignment['targetFramework'] = $item->targetframework;
 109                  }
 110                  if ($item->targetcode) {
 111                      $alignment['targetCode'] = $item->targetcode;
 112                  }
 113                  $json['alignments'][] = $alignment;
 114              }
 115          }
 116      } else if ($action == 0) {
 117          // Get the content for issuer.
 118          $json = $badge->get_badge_issuer();
 119      }
 120  } else {
 121      // The badge doen't exist or not accessible for the users.
 122      header("HTTP/1.0 410 Gone");
 123      $badgeurl = new moodle_url('/badges/badge_json.php', array('id' => $id));
 124      $json['id'] = $badgeurl->out();
 125      $json['error'] = get_string('error:relatedbadgedoesntexist', 'badges');
 126  }
 127  echo $OUTPUT->header();
 128  echo json_encode($json);