Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/166919

Change subject: Make wikigrok show multiple occupation suggestions when 
available
......................................................................

Make wikigrok show multiple occupation suggestions when available

If there are 5 possible suggestions of occupations show and allow saving
of them

In this change:
* Add support for getLabels
* Add support for recording multiple claims

Change-Id: I2b813f0a3d35dce2883f80a4f3c768ebf9bd70ed
---
M javascripts/modules/wikigrok/WikiDataApi.js
M javascripts/modules/wikigrok/WikiGrokApi.js
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/WikiGrokDialogB.js
M javascripts/modules/wikigrok/wikigrok.js
5 files changed, 70 insertions(+), 46 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/19/166919/1

diff --git a/javascripts/modules/wikigrok/WikiDataApi.js 
b/javascripts/modules/wikigrok/WikiDataApi.js
index 4ce9ab8..20c362f 100644
--- a/javascripts/modules/wikigrok/WikiDataApi.js
+++ b/javascripts/modules/wikigrok/WikiDataApi.js
@@ -7,6 +7,7 @@
        WikiDataApi = Api.extend( {
                apiUrl: 'https://www.wikidata.org/w/api.php',
                useJsonp: true,
+               language: 'en',
 
                initialize: function( options ) {
                        this.subjectId = options.itemId;
@@ -45,22 +46,26 @@
                 * Get labels for an item from Wikidata
                 * See: https://www.wikidata.org/wiki/Help:Label
                 *
-                * @param {int} itemId for item in Wikidata
+                * @param {array} itemIds for items in Wikidata
                 * @return {jQuery.Deferred} Object returned by ajax call
                 */
-               getLabel: function( itemId ) {
+               getLabels: function( itemIds ) {
+                       var lang = this.language;
                        return this.ajax( {
                                        action: 'wbgetentities',
                                        props: 'labels',
-                                       // FIXME: change this to a parameter
-                                       languages: 'en',
-                                       ids: itemId
+                                       languages: lang,
+                                       ids: itemIds
                                } ).then( function( data ) {
-                                       if ( 
data.entities[itemId].labels.en.value !== undefined ) {
-                                               return 
data.entities[itemId].labels.en.value;
-                                       } else {
-                                               return false;
-                                       }
+                                       var map = {};
+                                       $.each( itemIds, function( i, itemId ) {
+                                               if ( 
data.entities[itemId].labels[lang].value !== undefined ) {
+                                                       map[itemId] = 
data.entities[itemId].labels[lang].value;
+                                               } else {
+                                                       map[itemId] = null;
+                                               }
+                                       } );
+                                       return map;
                                } );
                }
        } );
diff --git a/javascripts/modules/wikigrok/WikiGrokApi.js 
b/javascripts/modules/wikigrok/WikiGrokApi.js
index 2b85c8a..cf70146 100644
--- a/javascripts/modules/wikigrok/WikiGrokApi.js
+++ b/javascripts/modules/wikigrok/WikiGrokApi.js
@@ -14,19 +14,34 @@
                        this.version = options.version;
                        Api.prototype.initialize.apply( this, arguments );
                },
-               recordOccupation: function( occupationId, occupation, 
claimIsCorrect ) {
+               /**
+                * Saves claims to the wikigrok api server
+                * @method
+                * @param {array} claims a list of claims. Each claim must have 
correct, prop, propid, value and valueid set
+                * @return {jQuery.Deferred}
+                */
+               recordClaims: function( claims ) {
                        return this.ajax( {
                                        action: 'record_answer',
                                        subject_id: this.subjectId,
                                        subject: this.subject,
-                                       occupation_id: occupationId,
-                                       occupation: occupation,
+                                       claims: JSON.stringify( claims ),
                                        page_name: mw.config.get( 'wgPageName' 
),
-                                       correct: claimIsCorrect,
                                        user_id: mw.user.getId(),
                                        source: 'mobile ' + this.version
                                } );
                },
+               recordOccupation: function( occupationId, occupation, 
claimIsCorrect ) {
+                       var claim = {
+                               correct: claimIsCorrect,
+                               prop: 'occupation',
+                               propid: 'P106',
+                               value: occupation,
+                               valueid: occupationId
+                       };
+
+                       return this.recordClaims( [ claim ] );
+               },
                getPossibleOccupations: function() {
                        return this.ajax( {
                                        action: 'get_potential_occupations',
diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index b3c3843..a990856 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -68,8 +68,10 @@
                                options.occupationId = 
this.chooseRandomItemFromArray( occupationArray );
 
                                // Get the name of the occupation from Wikidata.
-                               self.apiWikiData.getLabel( options.occupationId 
).done( function( label ) {
-                                       var vowels = [ 'a', 'e', 'i', 'o', 'u' 
];
+                               self.apiWikiData.getLabels( [ 
options.occupationId ] ).done( function( labels ) {
+                                       var vowels = [ 'a', 'e', 'i', 'o', 'u' 
],
+                                               label = 
labels[options.occupationId];
+
                                        if ( label ) {
                                                // Re-render with new content 
for 'Question' step
                                                options.beginQuestions = true;
diff --git a/javascripts/modules/wikigrok/WikiGrokDialogB.js 
b/javascripts/modules/wikigrok/WikiGrokDialogB.js
index 990dc3a..ea98f4b 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialogB.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialogB.js
@@ -38,34 +38,34 @@
                                                        };
 
                                                $.each( claims, function( key, 
claim ) {
-                                                       var btnLabel, choice, 
$chk,
-                                                               id = 'chk-' + 
key;
                                                        if ( !claim && 
suggestions[key] !== undefined ) {
-                                                               choice = 
self.chooseRandomItemFromArray( suggestions[key] );
-                                                               
self.apiWikiData.getLabel( choice ).done( function( label ) {
-                                                                       // 
oohhh let's find a claim and some suggestions
-                                                                       $chk = 
$( '<div class="ui-checkbox-button mw-ui-button">' ).
-                                                                               
on( 'click', function() {
-                                                                               
        var $chkBox = $( this ).find( 'input' );
-                                                                               
        $chkBox.prop( 'checked', !$chkBox.prop( 'checked' ) );
-                                                                               
        setTimeout( function() {
-                                                                               
                self.$save.prop( 'disabled', self.$( '.initial-pane 
input:checked' ).length === 0 );
-                                                                               
        }, 100 );
-                                                                               
} ).appendTo( self.$( '.wg-buttons' ) );
+                                                               
self.apiWikiData.getLabels( suggestions[key] ).done( function( labels ) {
+                                                                       $.each( 
labels, function( itemId, label ) {
+                                                                               
var btnLabel, $chk,
+                                                                               
        id = 'chk-' + key + '-' + itemId;
 
-                                                                       $( 
'<input type="checkbox">' ).
-                                                                               
attr( 'id', id ).
-                                                                               
data( 'choice', choice ).
-                                                                               
data( 'readable', label ).
-                                                                               
appendTo( $chk );
+                                                                               
$chk = $( '<div class="ui-checkbox-button mw-ui-button">' ).
+                                                                               
        on( 'click', function() {
+                                                                               
                var $chkBox = $( this ).find( 'input' );
+                                                                               
                $chkBox.prop( 'checked', !$chkBox.prop( 'checked' ) );
+                                                                               
                setTimeout( function() {
+                                                                               
                        self.$save.prop( 'disabled', self.$( '.initial-pane 
input:checked' ).length === 0 );
+                                                                               
                }, 100 );
+                                                                               
        } ).appendTo( self.$( '.wg-buttons' ) );
 
-                                                                       $( 
'<label>' ).
-                                                                               
text( questions[key] ).appendTo( $chk );
+                                                                               
$( '<input type="checkbox">' ).
+                                                                               
        attr( 'id', id ).
+                                                                               
        data( 'itemId', itemId ).
+                                                                               
        data( 'readable', label ).
+                                                                               
        appendTo( $chk );
 
-                                                                       $( 
'<label>' ).
-                                                                               
text( label ).
-                                                                               
html( btnLabel ).appendTo( $chk );
+                                                                               
$( '<label>' ).
+                                                                               
        text( questions[key] ).appendTo( $chk );
 
+                                                                               
$( '<label>' ).
+                                                                               
        text( label ).
+                                                                               
        html( btnLabel ).appendTo( $chk );
+                                                                       } );
 
                                                                        self.$( 
'.spinner' ).hide();
                                                                        
self.show();
@@ -80,16 +80,18 @@
                                var answers = [];
                                self.$( '.ui-checkbox-button input:checked' 
).hide().each( function() {
                                        answers.push( [
-                                               $( this ).data( 'choice' ),
-                                               $( this ).data( 'readable' ),
-                                               true
+                                               {
+                                                       correct: true,
+                                                       prop: 'occupation',
+                                                       propid: 'P106',
+                                                       value: $( this ).data( 
'readable' ),
+                                                       valueid: $( this 
).data( 'itemId' )
+                                               }
                                        ] );
                                } );
                                $( this ).hide();
                                self.$( '.spinner' ).show();
-                               // FIXME: This only saves the answer to the 
occupation question. We'll need to rethink this api
-                               // as soon as there are multiple choices.
-                               self.apiWikiGrok.recordOccupation.apply( 
self.apiWikiGrok, answers[0] ).done( function() {
+                               self.apiWikiGrok.recordClaims( answers ).done( 
function() {
                                        self.$( '.spinner' ).hide();
                                        self.$( '.initial-pane' ).hide();
                                        self.$( '.final-pane' ).show();
diff --git a/javascripts/modules/wikigrok/wikigrok.js 
b/javascripts/modules/wikigrok/wikigrok.js
index 9850227..78ac77b 100644
--- a/javascripts/modules/wikigrok/wikigrok.js
+++ b/javascripts/modules/wikigrok/wikigrok.js
@@ -27,7 +27,7 @@
                // We're not on the Main Page
                !mw.config.get( 'wgIsMainPage' ) &&
                // We're not on a tablet
-               !M.isWideScreen() &&
+               //!M.isWideScreen() &&
                // We're in 'view' mode
                mw.config.get( 'wgAction' ) === 'view' &&
                // Wikibase is active and this page has an item ID

-- 
To view, visit https://gerrit.wikimedia.org/r/166919
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2b813f0a3d35dce2883f80a4f3c768ebf9bd70ed
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to