Merge branch 'MDL-81456-main' of https://github.com/andrewnicols/moodle
[moodle.git] / badges / external.php
blobc9f7b3b9ab5427e5395400d73cf5b6f5c98fbec1
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 * Display details of an issued badge with criteria and evidence
20 * @package core
21 * @subpackage badges
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(__DIR__ . '/../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.
32 if ($json) {
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)));
40 $PAGE->set_context(context_system::instance());
42 // Using the same setting as user profile page.
43 if (!empty($CFG->forceloginforprofiles)) {
44 require_login();
45 if (isguestuser()) {
46 $SESSION->wantsurl = $PAGE->url->out(false);
47 redirect(get_login_url());
49 } else if (!empty($CFG->forcelogin)) {
50 require_login();
53 // Get all external badges of a user.
54 $out = get_backpack_settings($userid);
56 // If we didn't find any badges then print an error.
57 if (is_null($out)) {
58 throw new \moodle_exception('error:externalbadgedoesntexist', 'badges');
61 $badges = $out->badges;
63 // The variable to store the badge we want.
64 $badge = '';
66 // Loop through the badges and check if supplied badge hash exists in user external badges.
67 foreach ($badges as $b) {
68 if ($hash == hash("md5", $b->hostedUrl)) {
69 $badge = $b;
70 break;
74 // If we didn't find the badge a user might be trying to replace the userid parameter.
75 if (empty($badge)) {
76 throw new \moodle_exception('error:externalbadgedoesntexist', 'badges');
79 $output = $PAGE->get_renderer('core', 'badges');
81 $badge = new \core_badges\output\external_badge($badge, $userid);
83 $PAGE->set_pagelayout('base');
84 $PAGE->set_title(get_string('issuedbadge', 'badges'));
85 $badgename = '';
86 if (!empty($badge->issued->name)) {
87 $badgename = s($badge->issued->name);
89 if (!empty($badge->issued->assertion->badge->name)) {
90 $badgename = s($badge->issued->assertion->badge->name);
92 $PAGE->set_heading($badgename);
93 $PAGE->navbar->add($badgename);
94 if (isloggedin() && $USER->id == $userid) {
95 $url = new moodle_url('/badges/mybadges.php');
96 } else {
97 $url = new moodle_url($CFG->wwwroot);
99 navigation_node::override_active_url($url);
101 echo $OUTPUT->header();
103 echo $output->render($badge);
105 echo $OUTPUT->footer();