Skip to content

Commit

Permalink
MDL-63729 badges: Return new fields in badge table in WS
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 9, 2018
1 parent c776e1d commit 18d27ac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions badges/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $p
'uniquehash' => $badge->uniquehash,
'dateissued' => $badge->dateissued,
'dateexpire' => $badge->dateexpire,
'version' => $badge->version,
'language' => $badge->language,
'imageauthorname' => $badge->imageauthorname,
'imageauthoremail' => $badge->imageauthoremail,
'imageauthorurl' => $badge->imageauthorurl,
'imagecaption' => $badge->imagecaption,
);
}

Expand Down
36 changes: 36 additions & 0 deletions badges/classes/external/user_badge_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,42 @@ protected static function define_properties() {
'description' => 'User email',
'optional' => true,
],
'version' => [
'type' => PARAM_TEXT,
'description' => 'Version',
'optional' => true,
'null' => NULL_ALLOWED,
],
'language' => [
'type' => PARAM_NOTAGS,
'description' => 'Language',
'optional' => true,
'null' => NULL_ALLOWED,
],
'imageauthorname' => [
'type' => PARAM_TEXT,
'description' => 'Name of the image author',
'optional' => true,
'null' => NULL_ALLOWED,
],
'imageauthoremail' => [
'type' => PARAM_TEXT,
'description' => 'Email of the image author',
'optional' => true,
'null' => NULL_ALLOWED,
],
'imageauthorurl' => [
'type' => PARAM_URL,
'description' => 'URL of the image author',
'optional' => true,
'null' => NULL_ALLOWED,
],
'imagecaption' => [
'type' => PARAM_TEXT,
'description' => 'Caption of the image',
'optional' => true,
'null' => NULL_ALLOWED,
],
];
}

Expand Down
21 changes: 19 additions & 2 deletions badges/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public function setUp() {
$badge->attachment = 1;
$badge->notification = 0;
$badge->status = BADGE_STATUS_ACTIVE;
$badge->version = '1';
$badge->language = 'en';
$badge->imageauthorname = 'Image author';
$badge->imageauthoremail = '[email protected]';
$badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
$badge->imagecaption = 'Caption';

$badgeid = $DB->insert_record('badge', $badge, true);
$badge = new badge($badgeid);
Expand Down Expand Up @@ -116,15 +122,26 @@ public function test_get_my_user_badges() {

$this->setUser($this->student);

$badges = (array) badges_get_user_badges($this->student->id);
$expectedbadges = array();

foreach ($badges as $badge) {
$context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
$badge->badgeurl = moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
'f1')->out(false);

$expectedbadges[] = (array) $badge;
}

$result = core_badges_external::get_user_badges();
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
$this->assertCount(2, $result['badges']);
$this->assertEquals($expectedbadges, $result['badges']);

// Pagination and filtering.
$result = core_badges_external::get_user_badges(0, $this->course->id, 0, 1, '', true);
$result = external_api::clean_returnvalue(core_badges_external::get_user_badges_returns(), $result);
$this->assertCount(1, $result['badges']);
$this->assertEquals($this->course->id, $result['badges'][0]['courseid']);
$this->assertEquals($expectedbadges[1], $result['badges'][0]);
}

/**
Expand Down

0 comments on commit 18d27ac

Please sign in to comment.