2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Serve BadgeClass JSON for related badge.
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.
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();
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
;
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
;
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
);
91 $json['related'] = $relateds;
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);
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
;
107 if ($item->targetframework
) {
108 $alignment['targetFramework'] = $item->targetframework
;
110 if ($item->targetcode
) {
111 $alignment['targetCode'] = $item->targetcode
;
113 $json['alignments'][] = $alignment;
116 } else if ($action == 0) {
117 // Get the content for issuer.
118 $json = $badge->get_badge_issuer();
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');
127 echo $OUTPUT->header();
128 echo json_encode($json);