Skip to content

Commit

Permalink
MDL-67971 core_badges: review issuer URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Mar 2, 2020
1 parent d737452 commit 2f45b6f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
8 changes: 4 additions & 4 deletions badges/classes/assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function get_badge_class($issued = true) {
$class['criteria'] = $this->_url->out(false); // Currently issued badge URL.
if ($issued) {
if ($this->_obversion == OPEN_BADGES_V2) {
$issuerurl = new moodle_url('/badges/badge_json.php', array('id' => $this->get_badge_id(), 'action' => 0));
$issuerurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->get_badge_id()));
} else {
$issuerurl = new moodle_url('/badges/assertion.php', array('b' => $this->_data->uniquehash, 'action' => 0));
}
Expand Down Expand Up @@ -233,7 +233,8 @@ public function get_issuer() {
$issuer['email'] = $CFG->badges_defaultissuercontact;
}
} else {
$issuer = badges_get_default_issuer();
$badge = new badge($this->get_badge_id());
$issuer = $badge->get_badge_issuer();
}
}
$this->embed_data_badge_version2($issuer, OPEN_BADGES_V2_TYPE_ISSUER);
Expand Down Expand Up @@ -341,8 +342,7 @@ protected function embed_data_badge_version2 (&$json, $type = OPEN_BADGES_V2_TYP
'/badges/badge_json.php',
array('id' => $this->get_badge_id())
);
$issuerurl = new moodle_url('/badges/badge_json.php', array('id' => $this->get_badge_id(), 'action' => 0,
'obversion' => $this->_obversion));
$issuerurl = new moodle_url('/badges/issuer_json.php', ['id' => $this->get_badge_id()]);
// For assertion.
if ($type == OPEN_BADGES_V2_TYPE_ASSERTION) {
$json['@context'] = OPEN_BADGES_V2_CONTEXT;
Expand Down
4 changes: 2 additions & 2 deletions badges/classes/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,12 @@ public function markdown_badge_criteria() {
*/
public function get_badge_issuer() {
$issuer = array();
$issuerurl = new moodle_url('/badges/badge_json.php', array('id' => $this->id, 'action' => 0));
$issuer['name'] = $this->issuername;
$issuer['url'] = $this->issuerurl;
$issuer['email'] = $this->issuercontact;
$issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
$issuer['id'] = $this->issuerurl;
$issueridurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->id));
$issuer['id'] = $issueridurl->out(false);
$issuer['type'] = OPEN_BADGES_V2_TYPE_ISSUER;
return $issuer;
}
Expand Down
52 changes: 52 additions & 0 deletions badges/issuer_json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Serve Issuer JSON for related badge or default Issuer if no badge is defined.
*
* @package core
* @subpackage badges
* @copyright 2020 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
define('NO_MOODLE_COOKIES', true); // No need for a session here.

require_once(__DIR__ . '/../config.php');
require_once($CFG->libdir . '/badgeslib.php');


$id = optional_param('id', null, PARAM_INT);

if (empty($id)) {
// Get the default issuer for this site.
$json = badges_get_default_issuer();
} else {
// Get the issuer for this badge.
$badge = new badge($id);
if ($badge->status != BADGE_STATUS_INACTIVE) {
$json = $badge->get_badge_issuer();
} else {
// The badge doen't exist or not accessible for the users.
header("HTTP/1.0 410 Gone");
$badgeurl = new moodle_url('/badges/issuer_json.php', array('id' => $id));
$json = ['id' => $badgeurl->out()];
$json['error'] = get_string('error:relatedbadgedoesntexist', 'badges');
}
}

echo $OUTPUT->header();
echo json_encode($json);
5 changes: 3 additions & 2 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,16 @@ function badges_get_default_issuer() {
global $CFG, $SITE;

$issuer = array();
$issuerurl = new moodle_url('/badges/issuer.php');
$issuerurl = new moodle_url('/');
$issuer['name'] = $CFG->badges_defaultissuername;
if (empty($issuer['name'])) {
$issuer['name'] = $SITE->fullname ? $SITE->fullname : $SITE->shortname;
}
$issuer['url'] = $issuerurl->out(false);
$issuer['email'] = $CFG->badges_defaultissuercontact;
$issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
$issuer['id'] = $issuerurl->out(false);
$issuerid = new moodle_url('/badges/issuer_json.php');
$issuer['id'] = $issuerid->out(false);
$issuer['type'] = OPEN_BADGES_V2_TYPE_ISSUER;
return $issuer;
}
Expand Down

0 comments on commit 2f45b6f

Please sign in to comment.