Index: trunk/extensions/Survey/includes/SurveySubmission.php |
— | — | @@ -61,4 +61,50 @@ |
62 | 62 | ); |
63 | 63 | } |
64 | 64 | |
| 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 | + |
65 | 111 | } |
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php |
— | — | @@ -37,11 +37,27 @@ |
38 | 38 | if ( $survey === false ) { |
39 | 39 | $this->dieUsage( wfMsgExt( 'survey-err-survey-name-unknown', 'parsemag', $params['name'] ), 'survey-name-unknown' ); |
40 | 40 | } |
| 41 | + } |
| 42 | + else { |
| 43 | + $survey = Survey::newFromId( $params['id'], null, false ); |
41 | 44 | |
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 | + } |
43 | 48 | } |
44 | 49 | |
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(); |
46 | 62 | } |
47 | 63 | |
48 | 64 | public function needsToken() { |
— | — | @@ -52,6 +68,10 @@ |
53 | 69 | return 'submitsurvey'; |
54 | 70 | } |
55 | 71 | |
| 72 | + public function mustBePosted() { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
56 | 76 | public function getAllowedParams() { |
57 | 77 | return array( |
58 | 78 | 'id' => array( |
— | — | @@ -60,6 +80,9 @@ |
61 | 81 | 'name' => array( |
62 | 82 | ApiBase::PARAM_TYPE => 'string', |
63 | 83 | ), |
| 84 | + 'answers' => array( |
| 85 | + ApiBase::PARAM_TYPE => 'string', |
| 86 | + ), |
64 | 87 | 'token' => null, |
65 | 88 | ); |
66 | 89 | } |
Index: trunk/extensions/Survey/resources/jquery.survey.js |
— | — | @@ -6,22 +6,25 @@ |
7 | 7 | * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
8 | 8 | */ |
9 | 9 | |
10 | | -(function( $ ) { $( document ).ready( function() { |
| 10 | +( function ( $ ) { $.fn.mwSurvey = function( options ) { |
11 | 11 | |
12 | 12 | var _this = this; |
13 | 13 | |
14 | 14 | 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 | + |
15 | 26 | $.getJSON( |
16 | 27 | 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, |
26 | 29 | function( data ) { |
27 | 30 | if ( data.surveys ) { |
28 | 31 | callback( data.surveys ); |
— | — | @@ -105,12 +108,31 @@ |
106 | 109 | return $questions; |
107 | 110 | }; |
108 | 111 | |
109 | | - this.submitSurvey = function( surveyId, callback ) { |
110 | | - // TODO |
| 112 | + this.getAnswers = function( surveyId ) { |
| 113 | + var answers = []; |
111 | 114 | |
112 | | - callback(); |
| 115 | + |
| 116 | + |
| 117 | + return JSON.stringify( answers ); |
113 | 118 | }; |
114 | 119 | |
| 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 | + |
115 | 137 | this.doCompletion = function() { |
116 | 138 | $.fancybox.close(); |
117 | 139 | }; |
— | — | @@ -168,7 +190,7 @@ |
169 | 191 | return $survey; |
170 | 192 | }; |
171 | 193 | |
172 | | - this.initSurvey = function( surveyElement, surveyData ) { |
| 194 | + this.initSurvey = function( surveyData ) { |
173 | 195 | $div = $( '<div />' ).attr( { |
174 | 196 | 'style': 'display:none' |
175 | 197 | } ).html( $( '<div />' ).attr( { 'id': 'survey-' + surveyData.id } ).html( this.getSurveyBody( surveyData ) ) ); |
— | — | @@ -177,7 +199,7 @@ |
178 | 200 | 'href': '#survey-' + surveyData.id, |
179 | 201 | } ).html( $div ); |
180 | 202 | |
181 | | - surveyElement.html( $link ); |
| 203 | + $( this ).html( $link ); |
182 | 204 | |
183 | 205 | $link.fancybox( { |
184 | 206 | // 'width' : '75%', |
— | — | @@ -194,29 +216,44 @@ |
195 | 217 | }; |
196 | 218 | |
197 | 219 | this.init = function() { |
198 | | - var surveyNames = []; |
199 | | - var surveys = []; |
| 220 | + var $this = $( this ); |
| 221 | + var identifier = false; |
| 222 | + var type; |
200 | 223 | |
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 | + } |
206 | 232 | |
207 | | - if ( surveyNames.length > 0 ) { |
| 233 | + if ( identifier !== false ) { |
| 234 | + var requestArgs = {}; |
| 235 | + requestArgs[type] = identifier; |
| 236 | + |
208 | 237 | this.getSurveyData( |
209 | 238 | { |
210 | | - 'names': surveyNames |
| 239 | + 'requestArgs': requestArgs |
211 | 240 | }, |
212 | 241 | function( surveyData ) { |
213 | 242 | for ( i in surveyData ) { |
214 | | - _this.initSurvey( surveys[surveyData[i].name], surveyData[i] ); |
| 243 | + _this.initSurvey( surveyData[i] ); |
215 | 244 | } |
216 | 245 | } |
217 | | - ); |
| 246 | + ); |
218 | 247 | } |
219 | 248 | }; |
220 | 249 | |
221 | 250 | this.init(); |
222 | 251 | |
| 252 | +}; } )( jQuery ); |
| 253 | + |
| 254 | +(function( $ ) { $( document ).ready( function() { |
| 255 | + |
| 256 | + $( '.surveytag' ).each( function( index, domElement ) { |
| 257 | + $( domElement ).mwSurvey(); |
| 258 | + } ); |
| 259 | + |
223 | 260 | } ); })( jQuery ); |
\ No newline at end of file |