Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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;
1441 ariadna 50
        $urlimage = moodle_url::make_pluginfile_url($context->id,
51
            'badges', 'badgeimage', $badge->id, '/', 'f3')->out(false);
52
        $json['image'] = [];
53
        $json['image']['id'] = $urlimage;
54
        if ($badge->imagecaption) {
55
            $json['image']['caption'] = $badge->imagecaption;
1 efrain 56
        }
57
 
58
        $params = ['id' => $badge->id];
59
        $badgeurl = new moodle_url('/badges/badgeclass.php', $params);
60
        $json['criteria']['id'] = $badgeurl->out(false);
61
        $json['criteria']['narrative'] = $badge->markdown_badge_criteria();
62
        $json['issuer'] = $badge->get_badge_issuer();
63
        $json['@context'] = OPEN_BADGES_V2_CONTEXT;
64
        $json['id'] = $url->out();
65
        $json['type'] = OPEN_BADGES_V2_TYPE_BADGE;
66
        if (!empty($badge->version)) {
67
            $json['version'] = $badge->version;
68
        }
69
        if (!empty($badge->language)) {
70
            $json['@language'] = $badge->language;
71
        }
72
        $badgetags = $badge->get_badge_tags();
73
        if ($badgetags) {
74
            $json['tags'] = $badgetags;
75
        }
76
 
77
        $relatedbadges = $badge->get_related_badges(true);
78
        if (!empty($relatedbadges)) {
79
            foreach ($relatedbadges as $related) {
80
                $relatedurl = new moodle_url('/badges/badge_json.php', array('id' => $related->id));
81
                $relateds[] = array('id' => $relatedurl->out(false),
82
                    'version' => $related->version, '@language' => $related->language);
83
            }
84
             $json['related'] = $relateds;
85
        }
86
 
87
        $alignments = $badge->get_alignments();
88
        if (!empty($alignments)) {
89
            foreach ($alignments as $item) {
90
                $alignment = array('targetName' => $item->targetname, 'targetUrl' => $item->targeturl);
91
                if ($item->targetdescription) {
92
                    $alignment['targetDescription'] = $item->targetdescription;
93
                }
94
                if ($item->targetframework) {
95
                    $alignment['targetFramework'] = $item->targetframework;
96
                }
97
                if ($item->targetcode) {
98
                    $alignment['targetCode'] = $item->targetcode;
99
                }
100
                $json['alignments'][] = $alignment;
101
            }
102
        }
103
    } else if ($action == 0) {
104
        // Get the content for issuer.
105
        $json = $badge->get_badge_issuer();
106
    }
107
} else {
108
    // The badge doen't exist or not accessible for the users.
109
    header("HTTP/1.0 410 Gone");
110
    $badgeurl = new moodle_url('/badges/badge_json.php', array('id' => $id));
111
    $json['id'] = $badgeurl->out();
112
    $json['error'] = get_string('error:relatedbadgedoesntexist', 'badges');
113
}
114
echo $OUTPUT->header();
115
echo json_encode($json);