Merge branch 'MDL-79668-main' of https://github.com/kevpercy/moodle
[moodle.git] / badges / badge_json.php
blobabcb4fe248942f2536b39dd8b105b2338a9ae09a
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.
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/>.
17 /**
18 * Serve BadgeClass JSON for related badge.
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>
26 define('AJAX_SCRIPT', true);
27 define('NO_MOODLE_COOKIES', true); // No need for a session here.
29 require_once(__DIR__ . '/../config.php');
30 require_once($CFG->libdir . '/badgeslib.php');
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);
44 $urlimage = moodle_url::make_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 'f3')->out(false);
46 $url = new moodle_url('/badges/badge_json.php', array('id' => $badge->id));
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);
62 if ($badge->imagecaption) {
63 $json['image']['caption'] = $badge->imagecaption;
65 } else {
66 $json['image'] = $urlimage;
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;
80 if (!empty($badge->language)) {
81 $json['@language'] = $badge->language;
83 $badgetags = $badge->get_badge_tags();
84 if ($badgetags) {
85 $json['tags'] = $badgetags;
88 $relatedbadges = $badge->get_related_badges(true);
89 if (!empty($relatedbadges)) {
90 foreach ($relatedbadges as $related) {
91 $relatedurl = new moodle_url('/badges/badge_json.php', array('id' => $related->id));
92 $relateds[] = array('id' => $relatedurl->out(false),
93 'version' => $related->version, '@language' => $related->language);
95 $json['related'] = $relateds;
98 $endorsement = $badge->get_endorsement();
99 if (!empty($endorsement)) {
100 $endorsementurl = new moodle_url('/badges/endorsement_json.php', array('id' => $badge->id));
101 $json['endorsement'] = $endorsementurl->out(false);
104 $alignments = $badge->get_alignments();
105 if (!empty($alignments)) {
106 foreach ($alignments as $item) {
107 $alignment = array('targetName' => $item->targetname, 'targetUrl' => $item->targeturl);
108 if ($item->targetdescription) {
109 $alignment['targetDescription'] = $item->targetdescription;
111 if ($item->targetframework) {
112 $alignment['targetFramework'] = $item->targetframework;
114 if ($item->targetcode) {
115 $alignment['targetCode'] = $item->targetcode;
117 $json['alignments'][] = $alignment;
120 } else if ($action == 0) {
121 // Get the content for issuer.
122 $json = $badge->get_badge_issuer();
124 } else {
125 // The badge doen't exist or not accessible for the users.
126 header("HTTP/1.0 410 Gone");
127 $badgeurl = new moodle_url('/badges/badge_json.php', array('id' => $id));
128 $json['id'] = $badgeurl->out();
129 $json['error'] = get_string('error:relatedbadgedoesntexist', 'badges');
131 echo $OUTPUT->header();
132 echo json_encode($json);