r96447 MediaWiki - Code Review archive

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

Diff [purge]

Index: trunk/extensions/Survey/includes/SurveySubmission.php
@@ -61,4 +61,50 @@
6262 );
6363 }
6464
 65+ /**
 66+ * List of answers.
 67+ *
 68+ * @since 0.1
 69+ * @var array of SurveyAnswer
 70+ */
 71+ protected $answers;
 72+
 73+
 74+ public function addAnswer( SurveyAnswer $answer ) {
 75+ $this->answers[] = $answer;
 76+ }
 77+
 78+ public function setAnswers( array $answers ) {
 79+ $this->answers = $answers;
 80+ }
 81+
 82+ public function getAnswers() {
 83+ return $this->answers;
 84+ }
 85+
 86+ /**
 87+ * Writes the answer to the database, either updating it
 88+ * when it already exists, or inserting it when it doesn't.
 89+ *
 90+ * @since 0.1
 91+ *
 92+ * @return boolean Success indicator
 93+ */
 94+ public function writeToDB() {
 95+ $success = parent::writeToDB();
 96+
 97+ if ( $success ) {
 98+ $this->writeAnswersToDB();
 99+ }
 100+
 101+ return $success;
 102+ }
 103+
 104+ public function writeAnswersToDB() {
 105+ foreach ( $this->answers as /* SurveyAnswer */ $answer ) {
 106+ $answer->setField( 'submission_id', $this->getId() );
 107+ $answer->writeToDB();
 108+ }
 109+ }
 110+
65111 }
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php
@@ -37,11 +37,27 @@
3838 if ( $survey === false ) {
3939 $this->dieUsage( wfMsgExt( 'survey-err-survey-name-unknown', 'parsemag', $params['name'] ), 'survey-name-unknown' );
4040 }
 41+ }
 42+ else {
 43+ $survey = Survey::newFromId( $params['id'], null, false );
4144
42 - $params['id'] = $survey->getId();
 45+ if ( $survey === false ) {
 46+ $this->dieUsage( wfMsgExt( 'survey-err-survey-id-unknown', 'parsemag', $params['id'] ), 'survey-id-unknown' );
 47+ }
4348 }
4449
45 - $submission = new SurveySubmission();
 50+ $submission = new SurveySubmission( array(
 51+ 'survey_id' => $survey->getId(),
 52+ 'page_id' => 0, // TODO
 53+ 'user_name' => $GLOBALS['wgUser']->getName(),
 54+ 'time' => wfTimestampNow()
 55+ ) );
 56+
 57+ foreach ( FormatJson::decode( $params['answers'] ) as $answer ) {
 58+ $submission->addAnswer( SurveyAnswer::newFromArray( $answer ) );
 59+ }
 60+
 61+ $submission->writeToDB();
4662 }
4763
4864 public function needsToken() {
@@ -52,6 +68,10 @@
5369 return 'submitsurvey';
5470 }
5571
 72+ public function mustBePosted() {
 73+ return true;
 74+ }
 75+
5676 public function getAllowedParams() {
5777 return array(
5878 'id' => array(
@@ -60,6 +80,9 @@
6181 'name' => array(
6282 ApiBase::PARAM_TYPE => 'string',
6383 ),
 84+ 'answers' => array(
 85+ ApiBase::PARAM_TYPE => 'string',
 86+ ),
6487 'token' => null,
6588 );
6689 }
Index: trunk/extensions/Survey/resources/jquery.survey.js
@@ -6,22 +6,25 @@
77 * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
88 */
99
10 -(function( $ ) { $( document ).ready( function() {
 10+( function ( $ ) { $.fn.mwSurvey = function( options ) {
1111
1212 var _this = this;
1313
1414 this.getSurveyData = function( options, callback ) {
 15+ var requestArgs = {
 16+ 'action': 'query',
 17+ 'list': 'surveys',
 18+ 'format': 'json',
 19+ 'suincquestions': 1,
 20+ 'suenabled': 1,
 21+ 'suprops': '*'
 22+ };
 23+
 24+ requestArgs = $.extend( requestArgs, options.requestArgs )
 25+
1526 $.getJSON(
1627 wgScriptPath + '/api.php',
17 - {
18 - 'action': 'query',
19 - 'list': 'surveys',
20 - 'format': 'json',
21 - 'sunames': options.names.join( '|' ),
22 - 'suincquestions': 1,
23 - 'suenabled': 1,
24 - 'suprops': '*'
25 - },
 28+ requestArgs,
2629 function( data ) {
2730 if ( data.surveys ) {
2831 callback( data.surveys );
@@ -105,12 +108,31 @@
106109 return $questions;
107110 };
108111
109 - this.submitSurvey = function( surveyId, callback ) {
110 - // TODO
 112+ this.getAnswers = function( surveyId ) {
 113+ var answers = [];
111114
112 - callback();
 115+
 116+
 117+ return JSON.stringify( answers );
113118 };
114119
 120+ this.submitSurvey = function( surveyId, callback ) {
 121+ $.post(
 122+ wgScriptPath + '/api.php',
 123+ {
 124+ 'action': 'submitsurvey',
 125+ 'format': 'json',
 126+ 'ids': options.id,
 127+ 'token': options.token,
 128+ 'answers': this.getAnswers( surveyId )
 129+ },
 130+ function( data ) {
 131+ callback();
 132+ // TODO
 133+ }
 134+ );
 135+ };
 136+
115137 this.doCompletion = function() {
116138 $.fancybox.close();
117139 };
@@ -168,7 +190,7 @@
169191 return $survey;
170192 };
171193
172 - this.initSurvey = function( surveyElement, surveyData ) {
 194+ this.initSurvey = function( surveyData ) {
173195 $div = $( '<div />' ).attr( {
174196 'style': 'display:none'
175197 } ).html( $( '<div />' ).attr( { 'id': 'survey-' + surveyData.id } ).html( this.getSurveyBody( surveyData ) ) );
@@ -177,7 +199,7 @@
178200 'href': '#survey-' + surveyData.id,
179201 } ).html( $div );
180202
181 - surveyElement.html( $link );
 203+ $( this ).html( $link );
182204
183205 $link.fancybox( {
184206 // 'width' : '75%',
@@ -194,29 +216,44 @@
195217 };
196218
197219 this.init = function() {
198 - var surveyNames = [];
199 - var surveys = [];
 220+ var $this = $( this );
 221+ var identifier = false;
 222+ var type;
200223
201 - $( '.surveytag' ).each( function( index, domElement ) {
202 - $survey = $( domElement );
203 - surveyNames.push( $survey.attr( 'survey-data-name' ) );
204 - surveys[$survey.attr( 'survey-data-name' )] = $survey;
205 - } );
 224+ if ( $this.attr( 'survey-data-id' ) ) {
 225+ identifier = $this.attr( 'survey-data-id' );
 226+ type = 'suids';
 227+ }
 228+ else if ( $this.attr( 'survey-data-name' ) ) {
 229+ identifier = $this.attr( 'survey-data-name' );
 230+ type = 'sunames';
 231+ }
206232
207 - if ( surveyNames.length > 0 ) {
 233+ if ( identifier !== false ) {
 234+ var requestArgs = {};
 235+ requestArgs[type] = identifier;
 236+
208237 this.getSurveyData(
209238 {
210 - 'names': surveyNames
 239+ 'requestArgs': requestArgs
211240 },
212241 function( surveyData ) {
213242 for ( i in surveyData ) {
214 - _this.initSurvey( surveys[surveyData[i].name], surveyData[i] );
 243+ _this.initSurvey( surveyData[i] );
215244 }
216245 }
217 - );
 246+ );
218247 }
219248 };
220249
221250 this.init();
222251
 252+}; } )( jQuery );
 253+
 254+(function( $ ) { $( document ).ready( function() {
 255+
 256+ $( '.surveytag' ).each( function( index, domElement ) {
 257+ $( domElement ).mwSurvey();
 258+ } );
 259+
223260 } ); })( jQuery );
\ No newline at end of file

Status & tagging log