Skip to content

Commit

Permalink
MDL-70983 badges: Remove OBv1.0 support
Browse files Browse the repository at this point in the history
The only backpack implementing OBv1.0 was Mozilla Backpack. It closed
in 2019 and their badges were moved to https://backpack.openbadges.org/
by Badgr.
However, as Badgr is supporting OBv2.0 too, it makes no sense keep OBv1.0
implementation in Moodle: it has caused some issues and over-complicates
current code.
  • Loading branch information
sarjona committed Jul 19, 2024
1 parent 1a74342 commit b2f66d4
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 539 deletions.
7 changes: 7 additions & 0 deletions .upgradenotes/MDL-70983-2024062810515930.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
issueNumber: MDL-70983
notes:
core_badges:
- message: Final removal of BADGE_BACKPACKAPIURL and BADGE_BACKPACKWEBURL.
type: removed
- message: OPEN_BADGES_V1 is deprecated and should not be used anymore.
type: deprecated
42 changes: 11 additions & 31 deletions badges/classes/assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Badge assertion library.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/

defined('MOODLE_INTERNAL') || die();

/**
* Open Badges Assertions specification 1.0 {@link https://github.com/mozilla/openbadges-backpack/wiki/Assertions}
* Open Badges Assertions specification 2.0
* {@link https://www.imsglobal.org/sites/default/files/Badges/OBv2p0Final/index.html#Assertion}
*
* Badge asserion is defined by three parts:
* Badge assertion is defined by three parts:
* - Badge Assertion (information regarding a specific badge that was awarded to a badge earner)
* - Badge Class (general information about a badge and what it is intended to represent)
* - Issuer Class (general information of an issuing organisation)
Expand All @@ -40,6 +31,10 @@
/**
* Class that represents badge assertion.
*
* @package core_badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
class core_badges_assertion {
/** @var object Issued badge information from database */
Expand Down Expand Up @@ -205,7 +200,7 @@ public function get_badge_class($issued = true) {
$badgeurl = new moodle_url('/badges/badgeclass.php', $params);
$class['criteria'] = $badgeurl->out(false); // Currently badge URL.
if ($issued) {
$params = ['id' => $this->get_badge_id(), 'obversion' => $this->_obversion];
$params = ['id' => $this->get_badge_id()];
$issuerurl = new moodle_url('/badges/issuer_json.php', $params);
$class['issuer'] = $issuerurl->out(false);
}
Expand All @@ -227,24 +222,9 @@ public function get_badge_class($issued = true) {
* @return array Issuer information.
*/
public function get_issuer() {
global $CFG;
$issuer = array();
if ($this->_data) {
// Required.
if ($this->_obversion == OPEN_BADGES_V1) {
$issuer['name'] = $this->_data->issuername;
$issuer['url'] = $this->_data->issuerurl;
// Optional.
if (!empty($this->_data->issuercontact)) {
$issuer['email'] = $this->_data->issuercontact;
} else {
$issuer['email'] = $CFG->badges_defaultissuercontact;
}
} else {
$badge = new badge($this->get_badge_id());
$issuer = $badge->get_badge_issuer();
}
}
$badge = new badge($this->get_badge_id());
$issuer = $badge->get_badge_issuer();

$this->embed_data_badge_version2($issuer, OPEN_BADGES_V2_TYPE_ISSUER);
return $issuer;
}
Expand Down
512 changes: 175 additions & 337 deletions badges/classes/backpack_api.php

Large diffs are not rendered by default.

34 changes: 2 additions & 32 deletions badges/classes/backpack_api_mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
require_once($CFG->libdir . '/filelib.php');

use context_system;
use core_badges\external\assertion_exporter;
use core_badges\external\collection_exporter;
use core_badges\external\issuer_exporter;
use core_badges\external\badgeclass_exporter;
use curl;

/**
Expand Down Expand Up @@ -82,7 +78,7 @@ class backpack_api_mapping {
/** @var mixed List of parameters for this method. */
protected $postparams;

/** @var int OpenBadges version 1 or 2. */
/** @var int OpenBadges version. */
protected $backpackapiversion;

/**
Expand All @@ -98,7 +94,7 @@ class backpack_api_mapping {
* @param boolean $json json decode the response.
* @param boolean $authrequired Authentication is required for this request.
* @param boolean $isuserbackpack user backpack or a site backpack.
* @param integer $backpackapiversion OpenBadges version 1 or 2.
* @param integer $backpackapiversion OpenBadges version.
*/
public function __construct($action, $url, $postparams, $requestexporter, $responseexporter,
$multiple, $method, $json, $authrequired, $isuserbackpack, $backpackapiversion) {
Expand Down Expand Up @@ -233,32 +229,6 @@ private function get_post_params($email, $password, $param) {
return $request;
}

/**
* Read the response from a V1 user request and save the userID.
*
* @param string $response The request response.
* @param integer $backpackid The backpack id.
* @return mixed
*/
private function convert_email_response($response, $backpackid) {
global $SESSION;

if (isset($response->status) && $response->status == 'okay') {

// Remember the tokens.
$useridkey = $this->get_token_key(BADGE_USER_ID_TOKEN);
$backpackidkey = $this->get_token_key(BADGE_BACKPACK_ID_TOKEN);

$SESSION->$useridkey = $response->userId;
$SESSION->$backpackidkey = $backpackid;
return $response->userId;
}
if (!empty($response->error)) {
self::set_authentication_error($response->error);
}
return false;
}

/**
* Get the user id from a previous user request.
*
Expand Down
39 changes: 10 additions & 29 deletions badges/classes/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Badge assertion library.
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/

namespace core_badges;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -44,8 +34,10 @@
/**
* Class that represents badge.
*
* @package core_badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
class badge {
/** @var int Badge id */
Expand Down Expand Up @@ -954,25 +946,14 @@ public function markdown_badge_criteria() {
* @return array Issuer informations of the badge.
*/
public function get_badge_issuer(?int $obversion = null) {
global $DB;

$issuer = [];
if ($obversion == OPEN_BADGES_V1) {
$data = $DB->get_record('badge', ['id' => $this->id]);
$issuer['name'] = $data->issuername;
$issuer['url'] = $data->issuerurl;
$issuer['email'] = $data->issuercontact;
} else {
$issuer['name'] = $this->issuername;
$issuer['url'] = $this->issuerurl;
$issuer['email'] = $this->issuercontact;
$issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
$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;
return [
'name' => $this->issuername,
'url' => $this->issuerurl,
'email' => $this->issuercontact,
'@context' => OPEN_BADGES_V2_CONTEXT,
'id' => (new moodle_url('/badges/issuer_json.php', ['id' => $this->id]))->out(false),
'type' => OPEN_BADGES_V2_TYPE_ISSUER,
];
}

/**
Expand Down
19 changes: 1 addition & 18 deletions badges/classes/external/collection_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains class for displaying a collection.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_badges\external;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -30,7 +22,7 @@
use stdClass;

/**
* Class for displaying a badge competency.
* Class for displaying a badge collection.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
Expand All @@ -46,15 +38,6 @@ class collection_exporter extends exporter {
* @return stdClass
*/
public static function map_external_data($data, $apiversion) {
if ($apiversion == OPEN_BADGES_V1) {
$result = new stdClass();
$result->entityType = 'BackpackCollection';
$result->entityId = $data->groupId;
$result->name = $data->name;
$result->description = $data->description;
$result->assertions = [];
return $result;
}
return $data;
}

Expand Down
18 changes: 3 additions & 15 deletions badges/classes/external/issuer_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains class for displaying a issuer.
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_badges\external;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -38,17 +30,13 @@
class issuer_exporter extends exporter {

/**
* Either map version 1 data to version 2 or return it untouched.
* Map data depending on the version.
*
* @param stdClass $data The remote data.
* @param \stdClass $data The remote data.
* @param string $apiversion The backpack version used to communicate remotely.
* @return stdClass
* @return \stdClass
*/
public static function map_external_data($data, $apiversion) {
if ($apiversion == OPEN_BADGES_V1) {
$result = new \stdClass();
return $result;
}
$mapped = new \stdClass();
if (isset($data->entityType)) {
$mapped->type = $data->entityType;
Expand Down
32 changes: 7 additions & 25 deletions badges/classes/form/collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form class for mybackpack.php
*
* @package core
* @subpackage badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/

namespace core_badges\form;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -37,8 +27,10 @@
/**
* Form to select backpack collections.
*
* @package core_badges
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
class collections extends moodleform {

Expand Down Expand Up @@ -79,22 +71,12 @@ public function definition() {
foreach ($groups as $group) {
$count = 0;
// Handle attributes based on backpack's supported version.
if ($sitebackpack->apiversion == OPEN_BADGES_V2) {
// OpenBadges v2 data attributes.
if (empty($group->published)) {
// Only public collections.
continue;
}

// Get the number of badges associated with this collection from the assertions array returned.
$count = count($group->assertions);
} else {
// OpenBadges v1 data attributes.
$group->entityId = $group->groupId;

// Get the number of badges associated with this collection. In that case, the number is returned directly.
$count = $group->badges;
if (empty($group->published)) {
// Only public collections.
continue;
}
// Get the number of badges associated with this collection from the assertions array returned.
$count = count($group->assertions);

if (!$hasgroups) {
$mform->addElement('static', 'selectgroup', '', get_string('selectgroup_start', 'badges'));
Expand Down
13 changes: 3 additions & 10 deletions badges/classes/form/external_backpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* External backpack form
*
* @package core_badges
* @copyright 2019 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_badges\form;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/formslib.php');
Expand Down Expand Up @@ -93,8 +86,8 @@ public function definition() {
$mform->hideIf('backpackemail', 'includeauthdetails');
$mform->hideIf('backpackemail', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
$mform->hideIf('password', 'includeauthdetails');
$mform->hideIf('password', 'apiversion', 'in', [OPEN_BADGES_V1, OPEN_BADGES_V2P1]);
$mform->hideIf('backpackapiurl', 'apiversion', 'in', [OPEN_BADGES_V1, OPEN_BADGES_V2P1]);
$mform->hideIf('password', 'apiversion', 'in', [OPEN_BADGES_V2P1]);
$mform->hideIf('backpackapiurl', 'apiversion', 'in', [OPEN_BADGES_V2P1]);

// Disable short forms.
$mform->setDisableShortforms();
Expand Down
8 changes: 2 additions & 6 deletions badges/issuer_json.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
/**
* Serve Issuer JSON for related badge or default Issuer if no badge is defined.
*
* @package core
* @subpackage badges
* @package core_badges
* @copyright 2020 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand All @@ -28,10 +27,7 @@
require_once(__DIR__ . '/../config.php');
require_once($CFG->libdir . '/badgeslib.php');


$id = optional_param('id', null, PARAM_INT);
// OB specification version. If it's not defined, the site will be used as default.
$obversion = optional_param('obversion', badges_open_badges_backpack_api(), PARAM_INT);

if (empty($id)) {
// Get the default issuer for this site.
Expand All @@ -40,7 +36,7 @@
// Get the issuer for this badge.
$badge = new badge($id);
if ($badge->status != BADGE_STATUS_INACTIVE) {
$json = $badge->get_badge_issuer($obversion);
$json = $badge->get_badge_issuer();
} else {
// The badge doen't exist or not accessible for the users.
header("HTTP/1.0 410 Gone");
Expand Down
Loading

0 comments on commit b2f66d4

Please sign in to comment.