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 */
26define('AJAX_SCRIPT', true);
27define('NO_MOODLE_COOKIES', true); // No need for a session here.
28
29require_once(__DIR__ . '/../config.php');
30require_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);
36if ($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, '/', 'f1')->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, '/', 'f1')->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        $json['criteria']['id'] = $url->out(false);
70        $json['criteria']['narrative'] = $badge->markdown_badge_criteria();
71        $json['issuer'] = $badge->get_badge_issuer();
72        $json['@context'] = OPEN_BADGES_V2_CONTEXT;
73        $json['id'] = $url->out();
74        $json['type'] = OPEN_BADGES_V2_TYPE_BADGE;
75        if (!empty($badge->version)) {
76            $json['version'] = $badge->version;
77        }
78        if (!empty($badge->language)) {
79            $json['@language'] = $badge->language;
80        }
81
82        $relatedbadges = $badge->get_related_badges(true);
83        if (!empty($relatedbadges)) {
84            foreach ($relatedbadges as $related) {
85                $relatedurl = new moodle_url('/badges/badge_json.php', array('id' => $related->id));
86                $relateds[] = array('id' => $relatedurl->out(false),
87                    'version' => $related->version, '@language' => $related->language);
88            }
89             $json['related'] = $relateds;
90        }
91
92        $endorsement = $badge->get_endorsement();
93        if (!empty($endorsement)) {
94            $endorsementurl = new moodle_url('/badges/endorsement_json.php', array('id' => $badge->id));
95            $json['endorsement'] = $endorsementurl->out(false);
96        }
97
98        $alignments = $badge->get_alignments();
99        if (!empty($alignments)) {
100            foreach ($alignments as $item) {
101                $alignment = array('targetName' => $item->targetname, 'targetUrl' => $item->targeturl);
102                if ($item->targetdescription) {
103                    $alignment['targetDescription'] = $item->targetdescription;
104                }
105                if ($item->targetframework) {
106                    $alignment['targetFramework'] = $item->targetframework;
107                }
108                if ($item->targetcode) {
109                    $alignment['targetCode'] = $item->targetcode;
110                }
111                $json['alignments'][] = $alignment;
112            }
113        }
114    } else if ($action == 0) {
115        // Get the content for issuer.
116        $json = $badge->get_badge_issuer();
117    }
118} else {
119    // The badge doen't exist or not accessible for the users.
120    header("HTTP/1.0 410 Gone");
121    $badgeurl = new moodle_url('/badges/badge_json.php', array('id' => $id));
122    $json['id'] = $badgeurl->out();
123    $json['error'] = get_string('error:relatedbadgedoesntexist', 'badges');
124}
125echo $OUTPUT->header();
126echo json_encode($json);
127