MDL-63729 badges: Return new fields in badge table in WS
authordpalou <dani@moodle.com>
Thu, 8 Nov 2018 08:24:48 +0000 (09:24 +0100)
committerdpalou <dani@moodle.com>
Fri, 9 Nov 2018 11:11:04 +0000 (12:11 +0100)
badges/classes/external.php
badges/classes/external/user_badge_exporter.php
badges/tests/external_test.php

index c7a0293..19a764d 100644 (file)
@@ -137,6 +137,12 @@ class core_badges_external extends external_api {
                     '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,
                 );
             }
 
index f9832c1..0e284fb 100644 (file)
@@ -183,6 +183,42 @@ class user_badge_exporter extends exporter {
                 '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,
+            ],
         ];
     }
 
index 77db1a4..1176b9f 100644 (file)
@@ -85,6 +85,12 @@ class core_badges_external_testcase extends externallib_advanced_testcase {
         $badge->attachment = 1;
         $badge->notification = 0;
         $badge->status = BADGE_STATUS_ACTIVE;
+        $badge->version = '1';
+        $badge->language = 'en';
+        $badge->imageauthorname = 'Image author';
+        $badge->imageauthoremail = 'imageauthor@example.com';
+        $badge->imageauthorurl = 'http://image-author-url.domain.co.nz';
+        $badge->imagecaption = 'Caption';
 
         $badgeid = $DB->insert_record('badge', $badge, true);
         $badge = new badge($badgeid);
@@ -116,15 +122,26 @@ class core_badges_external_testcase extends externallib_advanced_testcase {
 
         $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]);
     }
 
     /**