r96448 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96447‎ | r96448 | r96449 >
Date:16:39, 7 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey submission
Modified paths:
  • /trunk/extensions/Survey/api/ApiSubmitSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (modified) (history)
  • /trunk/extensions/Survey/resources/jquery.survey.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -47,6 +47,7 @@
4848 }
4949
5050 $this->parameters['class'] = 'surveytag';
 51+ $this->parameters['survey-data-token'] = $GLOBALS['wgUser']->editToken();
5152 }
5253 else {
5354 throw new MWException( 'Invalid parameters for survey tag.' );
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php
@@ -54,7 +54,7 @@
5555 ) );
5656
5757 foreach ( FormatJson::decode( $params['answers'] ) as $answer ) {
58 - $submission->addAnswer( SurveyAnswer::newFromArray( $answer ) );
 58+ $submission->addAnswer( SurveyAnswer::newFromArray( (array)$answer ) );
5959 }
6060
6161 $submission->writeToDB();
Index: trunk/extensions/Survey/resources/jquery.survey.js
@@ -9,7 +9,11 @@
1010 ( function ( $ ) { $.fn.mwSurvey = function( options ) {
1111
1212 var _this = this;
 13+ this.inputs = [];
1314
 15+ this.identifier = null;
 16+ this.identifierType = null;
 17+
1418 this.getSurveyData = function( options, callback ) {
1519 var requestArgs = {
1620 'action': 'query',
@@ -20,7 +24,7 @@
2125 'suprops': '*'
2226 };
2327
24 - requestArgs = $.extend( requestArgs, options.requestArgs )
 28+ requestArgs[ 'su' + this.identifierType + 's' ] = this.identifier;
2529
2630 $.getJSON(
2731 wgScriptPath + '/api.php',
@@ -85,6 +89,10 @@
8690 break;
8791 }
8892
 93+ $input.data( 'question-id', question.id );
 94+
 95+ this.inputs.push( $input );
 96+
8997 return $input;
9098 };
9199
@@ -111,21 +119,31 @@
112120 this.getAnswers = function( surveyId ) {
113121 var answers = [];
114122
 123+ for ( i in this.inputs ) {
 124+ var $input = this.inputs[i];
 125+
 126+ answers.push( {
 127+ 'text': $input.val(),
 128+ 'question_id': $input.data( 'question-id' )
 129+ } );
 130+ }
115131
116 -
117132 return JSON.stringify( answers );
118133 };
119134
120135 this.submitSurvey = function( surveyId, callback ) {
 136+ var requestArgs = {
 137+ 'action': 'submitsurvey',
 138+ 'format': 'json',
 139+ 'token': $( this ).attr( 'survey-data-token' ),
 140+ 'answers': this.getAnswers( surveyId )
 141+ };
 142+
 143+ requestArgs[this.identifierType] = this.identifier;
 144+
121145 $.post(
122146 wgScriptPath + '/api.php',
123 - {
124 - 'action': 'submitsurvey',
125 - 'format': 'json',
126 - 'ids': options.id,
127 - 'token': options.token,
128 - 'answers': this.getAnswers( surveyId )
129 - },
 147+ requestArgs,
130148 function( data ) {
131149 callback();
132150 // TODO
@@ -217,26 +235,19 @@
218236
219237 this.init = function() {
220238 var $this = $( this );
221 - var identifier = false;
222 - var type;
223239
224240 if ( $this.attr( 'survey-data-id' ) ) {
225 - identifier = $this.attr( 'survey-data-id' );
226 - type = 'suids';
 241+ this.identifier = $this.attr( 'survey-data-id' );
 242+ this.identifierType = 'id';
227243 }
228244 else if ( $this.attr( 'survey-data-name' ) ) {
229 - identifier = $this.attr( 'survey-data-name' );
230 - type = 'sunames';
 245+ this.identifier = $this.attr( 'survey-data-name' );
 246+ this.identifierType = 'name';
231247 }
232248
233 - if ( identifier !== false ) {
234 - var requestArgs = {};
235 - requestArgs[type] = identifier;
236 -
 249+ if ( this.identifier !== null ) {
237250 this.getSurveyData(
238 - {
239 - 'requestArgs': requestArgs
240 - },
 251+ {},
241252 function( surveyData ) {
242253 for ( i in surveyData ) {
243254 _this.initSurvey( surveyData[i] );

Status & tagging log