Skip to content

Commit 89431ef

Browse files
committed
MDL-83906 core_badges: Replace old backpackapi calls
1 parent 3494953 commit 89431ef

6 files changed

+73
-47
lines changed

badges/backpack-add.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
require_once(__DIR__ . '/../config.php');
2626
require_once($CFG->libdir . '/badgeslib.php');
2727

28+
use core_badges\local\backpack\ob\api_base;
29+
2830
require_login();
2931

3032
$userbackpack = badges_get_user_backpack();
@@ -62,7 +64,7 @@
6264
$badgeadded = false;
6365
if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
6466
$sitebackpack = badges_get_site_primary_backpack();
65-
$api = new \core_badges\backpack_api($sitebackpack);
67+
$api = api_base::create_from_externalbackpack($userbackpack);
6668
$response = $api->authenticate();
6769

6870
// A numeric response indicates a valid successful authentication. Else an error object will be returned.
@@ -137,7 +139,7 @@
137139
// based on email address.
138140
// - This is only needed when the backpacks are from different regions.
139141
if ($assertionentityid && !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid)) {
140-
$userapi = new \core_badges\backpack_api($userbackpack, $backpack);
142+
$userapi = api_base::create_from_externalbackpack($userbackpack);
141143
$userapi->authenticate();
142144
$response = $userapi->import_badge_assertion($assertionentityid);
143145
if (!$response) {

badges/backpack-export.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
require_once(__DIR__ . '/../config.php');
2626
require_once($CFG->libdir . '/badgeslib.php');
2727

28+
use core_badges\local\backpack\ob\api_base;
29+
2830
$hash = optional_param('hash', null, PARAM_RAW);
2931

3032
$PAGE->set_pagelayout('admin');
@@ -52,6 +54,7 @@
5254

5355
$redirecturl = new moodle_url('/badges/mybadges.php');
5456
if ($hash) {
57+
$api = api_base::create_from_externalbackpack($backpack);
5558
$api = new core_badges\backpack_api2p1($backpack);
5659
$notify = $api->put_assertions($hash);
5760
if (!empty($notify['status']) && $notify['status'] == \core\output\notification::NOTIFY_SUCCESS) {
@@ -60,4 +63,4 @@
6063
redirect($redirecturl, $notify['message'], null, \core\output\notification::NOTIFY_ERROR);
6164
}
6265
}
63-
redirect($redirecturl);
66+
redirect($redirecturl);

badges/backpackemailverify.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
require_once(__DIR__ . '/../config.php');
2626
require_once($CFG->libdir . '/badgeslib.php');
2727

28+
use core_badges\local\backpack\ob\api_base;
29+
2830
$data = optional_param('data', '', PARAM_RAW);
2931
require_login();
3032
$PAGE->set_url('/badges/backpackemailverify.php');
@@ -41,11 +43,9 @@
4143

4244
$backpack = badges_get_site_backpack($backpackid);
4345

44-
$data = new stdClass();
45-
$data->email = $storedemail;
46-
$data->password = $password;
47-
$data->externalbackpackid = $backpackid;
48-
$bp = new \core_badges\backpack_api($backpack, $data);
46+
$backpack->email = $storedemail;
47+
$backpack->password = $password;
48+
$bp = api_base::create_from_externalbackpack($backpack);
4949

5050
// Make sure we have all the required information before trying to save the connection.
5151
$backpackuid = $bp->authenticate();
@@ -56,7 +56,7 @@
5656

5757
$values = [
5858
'userid' => $USER->id,
59-
'backpackemail' => $data->email,
59+
'backpackemail' => $storedemail,
6060
'externalbackpackid' => $backpackid,
6161
'backpackuid' => $backpackuid,
6262
'autosync' => 0,

badges/classes/form/backpack.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use html_writer;
3535
use moodleform;
3636
use stdClass;
37+
use core_badges\local\backpack\ob\api_base;
3738

3839
/**
3940
* Form to edit backpack initial details.
@@ -166,11 +167,10 @@ public function validation($data, $files) {
166167
}
167168

168169
// Check the given credentials (email and password) are valid for this backpack.
169-
$check = new stdClass();
170-
$check->email = $data['backpackemail'];
171-
$check->password = $data['password'];
172170
$sitebackpack = badges_get_site_backpack($data['externalbackpackid']);
173-
$bp = new \core_badges\backpack_api($sitebackpack, $check);
171+
$sitebackpack->email = $data['backpackemail'];
172+
$sitebackpack->password = $data['password'];
173+
$bp = api_base::create_from_externalbackpack($sitebackpack);
174174

175175
$result = $bp->authenticate();
176176
if ($result === false || !empty($result->error)) {

badges/mybackpack.php

+47-32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
require_once(__DIR__ . '/../config.php');
2828
require_once($CFG->libdir . '/badgeslib.php');
2929

30+
use core_badges\local\backpack\ob\api_base;
31+
3032
require_login();
3133

3234
if (empty($CFG->enablebadges)) {
@@ -55,53 +57,66 @@
5557

5658
if ($disconnect && $backpack) {
5759
require_sesskey();
60+
5861
$sitebackpack = badges_get_user_backpack();
59-
if ($sitebackpack->apiversion == OPEN_BADGES_V2P1) {
60-
$bp = new \core_badges\backpack_api2p1($sitebackpack);
61-
$bp->disconnect_backpack($backpack);
62-
redirect(new moodle_url('/badges/mybackpack.php'), get_string('backpackdisconnected', 'badges'), null,
63-
\core\output\notification::NOTIFY_SUCCESS);
64-
} else {
65-
// If backpack is connected, need to select collections.
66-
$bp = new \core_badges\backpack_api($sitebackpack, $backpack);
67-
$bp->disconnect_backpack($USER->id, $backpack->id);
68-
redirect(new moodle_url('/badges/mybackpack.php'));
69-
}
62+
$bp = api_base::create_from_externalbackpack($sitebackpack);
63+
$bp->disconnect_backpack();
64+
redirect(
65+
new moodle_url('/badges/mybackpack.php'),
66+
get_string('backpackdisconnected', 'badges'),
67+
null,
68+
\core\output\notification::NOTIFY_SUCCESS,
69+
);
7070
}
7171
$warning = '';
7272
if ($backpack) {
7373

7474
$sitebackpack = badges_get_user_backpack();
7575

76+
$params['email'] = $backpack->email;
77+
$params['backpackweburl'] = $sitebackpack->backpackweburl;
78+
$params['selected'] = '';
79+
7680
// If backpack is connected, need to select collections.
77-
$bp = new \core_badges\backpack_api($sitebackpack, $backpack);
78-
$request = $bp->get_collections();
79-
$groups = $request;
80-
if (isset($request->groups)) {
81-
$groups = $request->groups;
82-
}
83-
if (empty($groups)) {
81+
if ($sitebackpack->apiversion == OPEN_BADGES_V2P1) {
82+
// TODO: Implement the funcionality for displaying external badges in profile for 2.1.
83+
// $bp = new \core_badges\backpack_api2p1(externalbackpack: $sitebackpack);
84+
// $groups = $bp->get_assertions();
85+
8486
$err = get_string('error:nogroupssummary', 'badges');
8587
$err .= get_string('error:nogroupslink', 'badges', $sitebackpack->backpackweburl);
8688
$params['nogroups'] = $err;
89+
90+
$form = new \core_badges\form\collections(new moodle_url('/badges/mybackpack.php'), $params);
8791
} else {
88-
$params['groups'] = $groups;
89-
}
90-
$params['email'] = $backpack->email;
91-
$params['selected'] = $bp->get_collection_record($backpack->id);
92-
$params['backpackweburl'] = $sitebackpack->backpackweburl;
93-
$form = new \core_badges\form\collections(new moodle_url('/badges/mybackpack.php'), $params);
92+
$bp = api_base::create_from_externalbackpack($sitebackpack);
93+
$request = $bp->get_collections();
94+
$groups = $request;
95+
if (isset($request->groups)) {
96+
$groups = $request->groups;
97+
}
9498

95-
if ($form->is_cancelled()) {
96-
redirect(new moodle_url('/badges/mybadges.php'));
97-
} else if ($data = $form->get_data()) {
98-
if (empty($data->group)) {
99-
redirect(new moodle_url('/badges/mybadges.php'));
99+
if (empty($groups)) {
100+
$err = get_string('error:nogroupssummary', 'badges');
101+
$err .= get_string('error:nogroupslink', 'badges', $sitebackpack->backpackweburl);
102+
$params['nogroups'] = $err;
100103
} else {
101-
$groups = array_filter($data->group);
104+
$params['groups'] = $groups;
105+
}
106+
$params['selected'] = $bp->get_collection_record($backpack->id);
107+
$form = new \core_badges\form\collections(new moodle_url('/badges/mybackpack.php'), $params);
108+
109+
if ($form->is_cancelled()) {
110+
redirect(new moodle_url('/badges/mybadges.php'));
111+
} else if ($data = $form->get_data()) {
112+
if (empty($data->group)) {
113+
redirect(new moodle_url('/badges/mybadges.php'));
114+
} else {
115+
$groups = array_filter($data->group);
116+
}
117+
$bp->set_backpack_collections($backpack->id, $groups);
118+
redirect(new moodle_url('/badges/mybadges.php'));
102119
}
103-
$bp->set_backpack_collections($backpack->id, $groups);
104-
redirect(new moodle_url('/badges/mybadges.php'));
105120
}
106121
} else {
107122
// If backpack is not connected, need to connect first.

lib/badgeslib.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use core_badges\external\user_badge_exporter;
3434
/* Include required badge class exporter */
3535
use core_badges\external\badgeclass_exporter;
36+
use core_badges\local\backpack\ob\api_base;
3637

3738
/*
3839
* Number of records per page.
@@ -762,7 +763,12 @@ function get_backpack_settings($userid, $refresh = false) {
762763
$record = $DB->get_record('badge_backpack', array('userid' => $userid));
763764
if ($record) {
764765
$sitebackpack = badges_get_site_backpack($record->externalbackpackid);
765-
$backpack = new \core_badges\backpack_api($sitebackpack, $record);
766+
if ($sitebackpack->apiversion != OPEN_BADGES_V2) {
767+
$sitebackpack->backpackid = $record->externalbackpackid;
768+
return $sitebackpack;
769+
}
770+
$backpack = api_base::create_from_externalbackpack($sitebackpack);
771+
766772
$out = new stdClass();
767773
$out->backpackid = $sitebackpack->id;
768774

@@ -1523,7 +1529,7 @@ function badges_verify_backpack(int $backpackid) {
15231529

15241530
$backpack = badges_get_site_backpack($backpackid);
15251531
if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) {
1526-
$backpackapi = new \core_badges\backpack_api($backpack);
1532+
$backpackapi = api_base::create_from_externalbackpack($backpack);
15271533

15281534
// Clear any cached access tokens in the session.
15291535
$backpackapi->clear_system_user_session();

0 commit comments

Comments
 (0)