Skip to content

Commit

Permalink
Merge pull request #54 from YourAcclaim/bug/CRED-3828-moodle-badgeurl…
Browse files Browse the repository at this point in the history
…-error

bug: [CRED-3828] Moodle badgeurl upgrade error
  • Loading branch information
ahripak authored Apr 8, 2024
2 parents 69e76b3 + c002462 commit 6fa0488
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 25 deletions.
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<FIELD NAME="badgeid" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Badge Template ID"/>
<FIELD NAME="expiration" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Badge Expiration"/>
<FIELD NAME="badgename" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Badge Name"/>
<FIELD NAME="badgeurl" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Badge URL"/>
<FIELD NAME="badgeurl" TYPE="char" LENGTH="1000" NOTNULL="true" DEFAULT="none-set" SEQUENCE="false" COMMENT="Badge URL"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
37 changes: 37 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,42 @@ function xmldb_block_acclaim_upgrade($oldversion) {
// Acclaim savepoint reached.
upgrade_block_savepoint(true, 2020042200, 'acclaim');
}

if ($oldversion < 2024013100) {
$table = new xmldb_table('block_acclaim_courses');
$field = new xmldb_field('badgeurl', XMLDB_TYPE_CHAR, '1000', null, XMLDB_NOTNULL, null, 'none-set', 'badgename');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

$records = $DB->get_records(
'block_acclaim_courses',
['badgeurl' => 'none-set'],
'id'
);

if ($records) {
$api = new block_acclaim_lib();
foreach ($records as $record) {
$record->badgeurl = $api->fetch_template_url($record->badgeid);
$DB->update_record('block_acclaim_courses', $record);
}
}

// Acclaim savepoint reached.
// Beacon savepoint reached.
upgrade_block_savepoint(true, 2024013100, 'acclaim');
}

if ($oldversion < 2024040800) {
$table = new xmldb_table('block_acclaim_courses');
$field = new xmldb_field('badgeurl', XMLDB_TYPE_CHAR, '1000', null, XMLDB_NOTNULL, null, 'none-set', 'badgename');

$dbman->change_field_type($table, $field);

// Acclaim savepoint reached.
upgrade_block_savepoint(true, 2024040800, 'acclaim');
}

return true;
}
28 changes: 25 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ public function issue_pending_badges($curl) {
$url, $payload, array('CURLOPT_USERPWD' => block_acclaim_lib::$config->token . ':')
);

print $curl->info['http_code'];

if ($curl->info['http_code'] == 201) {
// The badge has been issued so we remove it from pending.
$DB->delete_records('block_acclaim_pending_badges', array('id' => $badge->id));
Expand Down Expand Up @@ -218,6 +216,30 @@ function badge_names($returntype = '') {
return $badge_items;
}

/**
* Send a request to Credly's Acclaim API to retrieve
* a single badge template URL
*
* @param string $id - ID of the template
* @return object - The JSON response.
*/
public function fetch_template_url($id) {
if (is_null($url)) {
$config = self::$config;
$url = "{$config->url}/organizations/{$config->org}/badge_templates/{$id}";
}

$options = array('CURLOPT_USERPWD' => self::$config->token . ':');
$result = (new curl())->get($url, $params, $options);
$arr = json_decode($result, true);

if (!isset($arr['data'])) {
return 'unknown';
}

return $arr['data']['url'];
}

////////////////////
// Private functions
////////////////////
Expand Down Expand Up @@ -262,7 +284,7 @@ private function accumulate_badge_names($json, &$badge_items, &$badge_urls) {
* configured organization.
* @return object - The JSON response.
*/
private function query_api($url) {
protected function query_api($url) {
if (is_null($url)) {
$config = self::$config;
$url = "{$config->url}/organizations/{$config->org}/badge_templates?sort=name&filter=state::active";
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'block_acclaim/url',
get_string('setting_domain', 'block_acclaim'),
null,
0,
array_keys($urls)[0],
$urls
)
);
Expand Down
70 changes: 51 additions & 19 deletions tests/block_acclaim_lib_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class block_acclaim_lib_test extends advanced_testcase {
private $acclaim = null;

function setUp() {
function setUp(): void {
$this->resetAfterTest(true);

$this->acclaim = new block_acclaim_lib();
Expand All @@ -52,11 +52,32 @@ function setUp() {
public function test_accumulate_badge_names() {
$this->acclaim = new block_acclaim_lib();
$badge_items = array();
$json = array('data' => array(0 => array('id' => 1, 'name' => 'johnny')));
$this->invokePrivate('accumulate_badge_names', array($json, &$badge_items));
$badge_urls = array();
$json = json_encode(
array(
'data' => array(
array(
'id' => 1,
'name' => 'johnny',
'url' => 'https://example.com',
)
)
)
);
$this->invokePrivate('accumulate_badge_names', array($json, &$badge_items, &$badge_urls));
$this->assertEquals(array(1 => 'johnny'), $badge_items);
$json = array('data' => array(0 => array('id' => 2, 'name' => 'billy')));
$this->invokePrivate('accumulate_badge_names', array($json, &$badge_items));
$json = json_encode(
array(
'data' => array(
array(
'id' => 2,
'name' => 'billy',
'url' => 'https://example.com',
)
)
)
);
$this->invokePrivate('accumulate_badge_names', array($json, &$badge_items, &$badge_urls));
$this->assertEquals(array(1 => 'johnny', 2 => 'billy'), $badge_items);
}

Expand All @@ -65,8 +86,7 @@ public function test_badge_names() {
$this->acclaim = new block_acclaim_lib_badge_names_test();

$badge_items = $this->acclaim->badge_names();

$this->assertEquals(array('one' => 'First', 'two' => 'Second', 'three' => 'Third'), $badge_items);
$this->assertEquals(array('two' => 'Second', 'three' => 'Third'), $badge_items);
}

public function test_create_pending_badge() {
Expand Down Expand Up @@ -249,6 +269,7 @@ private function mock_form() {
$badgenames = [
'123' => 'mos',
'1edb816d-a9fb-445d-b024-bb52075718e5' => 'def',
'1edb816d-a9fb-445d-b024-bb52075718e5_url' => 'https://example.com/mos/badge/def',
];

$json = json_encode($badgenames);
Expand Down Expand Up @@ -358,20 +379,31 @@ private function invokePrivate($methodName, array $parameters = array()) {
// Class to override protected methods for a specific test (since phpunit doesn't allow stubbing of those methods).
// This class tests that badge_names() accumulates badges over multiple pages.
class block_acclaim_lib_badge_names_test extends block_acclaim_lib {
function search_badges($search = null) {
return array(
'data' => array(array('name' => 'First', 'id' => 'one')),
'metadata' => array('next_page_url' => 'p1')
);
}
function query_api($url) {
if ($url == 'p1') {
return array(
'data' => array(array('name' => 'Third', 'id' => 'three')),
'metadata' => array('next_page_url' => 'p2')
protected function query_api($url) {
if ($url == 'p2&sort=name&filter=state::active') {
$result = array(
'data' => array(
array(
'name' => 'Third',
'id' => 'three',
'url' => 'https://example.com/def/badge/third'
)
),
'metadata' => array('next_page_url' => null),
);
} else {
return array('data' => array(array('name' => 'Second', 'id' => 'two')));
$result = array(
'data' => array(
array(
'name' => 'Second',
'id' => 'two',
'url' => 'https://example.com/def/badge/second'
)
),
'metadata' => array('next_page_url' => 'p2'),
);
}

return json_encode($result);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

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

$plugin->version = 2023031500; // YYYYMMDDHH (year, month, day, 24-hr time)
$plugin->version = 2024040800; // YYYYMMDDHH (year, month, day, 24-hr time)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'block_acclaim'; // Full name of the plugin (used for diagnostics)

0 comments on commit 6fa0488

Please sign in to comment.