Bmansurov has uploaded a new change for review. https://gerrit.wikimedia.org/r/168663
Change subject: Support different types of claims in WikiGrok version A. ...................................................................... Support different types of claims in WikiGrok version A. * Add support for 'nationality' Change-Id: I43d46fbaf4ef313331a22368797179dffef46aa1 --- M javascripts/modules/wikigrok/WikiGrokApi.js M javascripts/modules/wikigrok/WikiGrokDialog.js M javascripts/modules/wikigrok/WikiGrokDialogB.js 3 files changed, 82 insertions(+), 32 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/63/168663/1 diff --git a/javascripts/modules/wikigrok/WikiGrokApi.js b/javascripts/modules/wikigrok/WikiGrokApi.js index 83abb09..5d005d5 100644 --- a/javascripts/modules/wikigrok/WikiGrokApi.js +++ b/javascripts/modules/wikigrok/WikiGrokApi.js @@ -32,6 +32,17 @@ source: 'mobile ' + this.version } ); }, + recordNationality: function ( nationalityId, nationality, claimIsCorrect ) { + var claim = { + correct: claimIsCorrect, + prop: 'nationality', + propid: 'P27', + value: nationality, + valueid: nationalityId + }; + + return this.recordClaims( [ claim ] ); + }, recordOccupation: function ( occupationId, occupation, claimIsCorrect ) { var claim = { correct: claimIsCorrect, diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js b/javascripts/modules/wikigrok/WikiGrokDialog.js index e678152..840939b 100644 --- a/javascripts/modules/wikigrok/WikiGrokDialog.js +++ b/javascripts/modules/wikigrok/WikiGrokDialog.js @@ -50,6 +50,7 @@ Panel.prototype.initialize.apply( this, arguments ); // log page impression and widget impression when the widget is shown + // FIXME: stop listening to 'show' once the below execute successfully this.on( 'show', function () { self.logPageImpression(); self.initializeWidgetImpressionLogging(); @@ -74,8 +75,8 @@ }, /** * Return a new array from 'array' with 'count' randomly selected elements. - * @param {array} array Array from which random elements are selected - * @param {integer} count - Positive number of random elements to select + * @param {Array} array Array from which random elements are selected + * @param {Integer} count - Positive number of random elements to select * @returns {Array} */ chooseRandomItemsFromArray: function ( array, count ) { @@ -97,36 +98,39 @@ randomIndex = Math.round( Math.random() * ( arrayLength - 1 ) ); result.push( array[ randomIndex ] ); } - } return result; }, askWikidataQuestion: function ( options ) { var self = this, - occupationArray = options.occupations; + vowels = [ 'a', 'e', 'i', 'o', 'u' ]; - // If there are potential occupations for this person, select one at - // random and ask if it is a correct occupation for the person. - if ( occupationArray.length ) { - // Choose a random occupation from the list of possible occupations. - options.occupationId = this.chooseRandomItemsFromArray( occupationArray, 1 )[0]; + if ( options.suggestions.length ) { + // choose a suggestion category (dob, dod, occupation, or nationality) randomly + options.suggestion = this.chooseRandomItemsFromArray( options.suggestions, 1 )[0]; + // pick a claim randomly + options.claimId = this.chooseRandomItemsFromArray( options.suggestion.list, 1 )[0]; - // Get the name of the occupation from Wikidata. - self.apiWikiData.getLabels( [ options.occupationId ] ).done( function ( labels ) { - var vowels = [ 'a', 'e', 'i', 'o', 'u' ], - label = labels[options.occupationId]; + // Get the name of the claim from Wikidata. + self.apiWikiData.getLabels( [ options.claimId ] ).done( function ( labels ) { + options.claimLabel = labels[ options.claimId ]; + if ( options.claimLabel ) { + // ask if it is a correct occupation for the person. + // FIXME: add support for DOB and DOD + if ( options.suggestion.name === 'occupations' ) { + // Hack for English prototype + if ( $.inArray( options.claimLabel.charAt(0), vowels ) === -1 ) { + options.contentMsg = 'Was ' + options.name + ' a ' + options.claimLabel + '?'; + } else { + options.contentMsg = 'Was ' + options.name + ' an ' + options.claimLabel + '?'; + } + } else if ( options.suggestion.name === 'nationality' ) { + options.contentMsg = 'Was ' + options.name + ' a citizen of ' + options.claimLabel + '?'; + } - if ( label ) { // Re-render with new content for 'Question' step options.beginQuestions = true; - options.occupation = label; - // Hack for English prototype - if ( $.inArray( options.occupation.charAt(0), vowels ) === -1 ) { - options.contentMsg = 'Was ' + options.name + ' a ' + options.occupation + '?'; - } else { - options.contentMsg = 'Was ' + options.name + ' an ' + options.occupation + '?'; - } options.buttons = [ { classes: 'yes inline mw-ui-button mw-ui-progressive', label: 'Yes' }, { classes: 'not-sure inline mw-ui-button', label: 'Not Sure' }, @@ -153,12 +157,23 @@ // Eventually answers will be recorded directly to Wikidata. recordClaim: function ( options ) { var self = this, - args = [ options.occupationId, - options.occupation, options.claimIsCorrect ]; + args = [ options.claimId, + options.claimLabel, options.claimIsCorrect ], + recordMethod; - this.apiWikiGrok.recordOccupation.apply( this.apiWikiGrok, args ).done( function () { - self.thankUser( options, true ); - } ); + // FIXME: add support for DOB and DOD + if ( options.suggestion.name === 'occupations' ) { + recordMethod = 'recordOccupation'; + } else if ( options.suggestion.name === 'nationality' ) { + recordMethod = 'recordNationality'; + } + + if ( recordMethod ) { + this.apiWikiGrok[recordMethod].apply( this.apiWikiGrok, args ).done( function () { + options.claimRecorded = true; + self.thankUser( options, options.claimRecorded ); + } ); + } }, thankUser: function ( options, claimRecorded ) { @@ -315,12 +330,35 @@ }, reveal: function ( options ) { var self = this; - this.apiWikiGrok.getPossibleOccupations().done( function ( occupations ) { - if ( occupations.length ) { - options.occupations = occupations; - self.show(); - } - } ); + + // fetch suggestions only if we didn't try loading suggestions before + if ( options.suggestions ) { + self.show(); + } else { + options.suggestions = []; + self.apiWikiData.getClaims().done( function ( claims ) { + if ( claims.isHuman ) { + self.apiWikiGrok.getSuggestions().done( function ( suggestions ) { + // FIXME: find out how the DOB and DOD suggestions look and enable the code below + //if ( suggestions.dob.list.length ) { + // options.suggestions.push( suggestions.dob ); + //} + //if ( suggestions.dod.list.length ) { + // options.suggestions.push( suggestions.dod ); + //} + if ( suggestions.occupations.list.length ) { + options.suggestions.push( suggestions.occupations ); + } + if ( suggestions.nationalities.list.length ) { + options.suggestions.push( suggestions.nationalities ); + } + if ( options.suggestions.length ) { + self.show(); + } + } ); + } + } ); + } } } ); diff --git a/javascripts/modules/wikigrok/WikiGrokDialogB.js b/javascripts/modules/wikigrok/WikiGrokDialogB.js index dd8775d..58375da 100644 --- a/javascripts/modules/wikigrok/WikiGrokDialogB.js +++ b/javascripts/modules/wikigrok/WikiGrokDialogB.js @@ -22,6 +22,7 @@ WikiGrokDialog.prototype.initialize.apply( this, arguments ); // log page impression and widget impression when the widget is shown + // FIXME: stop listening to 'show' once the below execute successfully this.on( 'show', function () { self.logPageImpression(); self.initializeWidgetImpressionLogging(); -- To view, visit https://gerrit.wikimedia.org/r/168663 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I43d46fbaf4ef313331a22368797179dffef46aa1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Bmansurov <bmansu...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits