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 * Display details of an issued badge with criteria and evidence
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
30 $json = optional_param('badge', null, PARAM_RAW);
31 // Redirect to homepage if users are trying to access external badge through old url.
33 redirect($CFG->wwwroot, get_string('invalidrequest', 'error'), 3);
36 $hash = required_param('hash', PARAM_ALPHANUM);
37 $userid = required_param('user', PARAM_INT);
39 $PAGE->set_url(new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid)));
41 // Using the same setting as user profile page.
42 if (!empty($CFG->forceloginforprofiles)) {
45 $SESSION->wantsurl = $PAGE->url->out(false);
46 redirect(get_login_url());
48 } else if (!empty($CFG->forcelogin)) {
52 // Get all external badges of a user.
53 $out = get_backpack_settings($userid);
54 $badges = $out->badges;
56 // Loop through the badges and check if supplied badge hash exists in user external badges.
57 foreach ($badges as $b) {
58 if ($hash == hash("md5", $b->hostedUrl)) {
64 // If we didn't find the badge, a user might be trying to replace userid parameter.
65 if (is_null($badge)) {
66 print_error(get_string('error:externalbadgedoesntexist', 'badges'));
69 $PAGE->set_context(context_system::instance());
70 $output = $PAGE->get_renderer('core', 'badges');
72 $badge = new external_badge($badge, $userid);
74 $PAGE->set_pagelayout('base');
75 $PAGE->set_title(get_string('issuedbadge', 'badges'));
77 echo $OUTPUT->header();
79 echo $output->render($badge);
81 echo $OUTPUT->footer();