Index: trunk/extensions/Survey/Survey.sql |
— | — | @@ -6,7 +6,10 @@ |
7 | 7 | CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/surveys ( |
8 | 8 | survey_id SMALLINT unsigned NOT NULL auto_increment PRIMARY KEY, |
9 | 9 | survey_name VARCHAR(255) NOT NULL, |
10 | | - survey_enabled TINYINT NOT NULL default '0' |
| 10 | + survey_enabled TINYINT NOT NULL default '0', |
| 11 | + survey_header TEXT NOT NULL, |
| 12 | + survey_footer TEXT NOT NULL, |
| 13 | + survey_thanks TEXT NOT NULL |
11 | 14 | ) /*$wgDBTableOptions*/; |
12 | 15 | |
13 | 16 | -- Questions |
Index: trunk/extensions/Survey/specials/SpecialSurvey.php |
— | — | @@ -185,6 +185,30 @@ |
186 | 186 | 'name' => 'survey-enabled', |
187 | 187 | ); |
188 | 188 | |
| 189 | + $fields[] = array( |
| 190 | + 'type' => 'text', |
| 191 | + 'default' => $survey->getHeader(), |
| 192 | + 'label-message' => 'survey-special-label-header', |
| 193 | + 'id' => 'survey-header', |
| 194 | + 'name' => 'survey-header', |
| 195 | + ); |
| 196 | + |
| 197 | + $fields[] = array( |
| 198 | + 'type' => 'text', |
| 199 | + 'default' => $survey->getFooter(), |
| 200 | + 'label-message' => 'survey-special-label-footer', |
| 201 | + 'id' => 'survey-footer', |
| 202 | + 'name' => 'survey-footer', |
| 203 | + ); |
| 204 | + |
| 205 | + $fields[] = array( |
| 206 | + 'type' => 'text', |
| 207 | + 'default' => $survey->getThanks(), |
| 208 | + 'label-message' => 'survey-special-label-thanks', |
| 209 | + 'id' => 'survey-thanks', |
| 210 | + 'name' => 'survey-thanks', |
| 211 | + ); |
| 212 | + |
189 | 213 | foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) { |
190 | 214 | $fields[] = array( |
191 | 215 | 'class' => 'SurveyQuestionField', |
Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -13,7 +13,24 @@ |
14 | 14 | */ |
15 | 15 | class Survey extends SurveyDBClass { |
16 | 16 | |
| 17 | + protected static $fields = array( |
| 18 | + 'id' => 'int', |
| 19 | + 'name' => 'str', |
| 20 | + 'enabled' => 'bool', |
| 21 | + 'header' => 'str', |
| 22 | + 'footer' => 'str', |
| 23 | + 'thanks' => 'str', |
| 24 | + ); |
| 25 | + |
17 | 26 | /** |
| 27 | + * |
| 28 | + * |
| 29 | + * @since 0.1 |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected static $fieldPrefix = 'survey_'; |
| 33 | + |
| 34 | + /** |
18 | 35 | * The name of the survey. |
19 | 36 | * |
20 | 37 | * @since 0.1 |
— | — | @@ -38,6 +55,30 @@ |
39 | 56 | protected $questions; |
40 | 57 | |
41 | 58 | /** |
| 59 | + * Header text to display above the questions. |
| 60 | + * |
| 61 | + * @since 0.1 |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + protected $header; |
| 65 | + |
| 66 | + /** |
| 67 | + * Footer text to display below the questions. |
| 68 | + * |
| 69 | + * @since 0.1 |
| 70 | + * @var string |
| 71 | + */ |
| 72 | + protected $footer; |
| 73 | + |
| 74 | + /** |
| 75 | + * Thanks message to display after submission of the survey. |
| 76 | + * |
| 77 | + * @since 0.1 |
| 78 | + * @var string |
| 79 | + */ |
| 80 | + protected $thanksMessage; |
| 81 | + |
| 82 | + /** |
42 | 83 | * @see SurveyDBClass::getDBTable() |
43 | 84 | */ |
44 | 85 | protected function getDBTable() { |
— | — | @@ -71,12 +112,13 @@ |
72 | 113 | * @since 0.1 |
73 | 114 | * |
74 | 115 | * @param string $surveyName |
| 116 | + * @param array|null $fields |
75 | 117 | * @param boolean $loadQuestions |
76 | 118 | * |
77 | 119 | * @return Survey or false |
78 | 120 | */ |
79 | | - public static function newFromName( $surveyName, $loadQuestions = true ) { |
80 | | - return self::newFromDB( array( 'survey_name' => $surveyName ), $loadQuestions ); |
| 121 | + public static function newFromName( $surveyName, $fields = null, $loadQuestions = true ) { |
| 122 | + return self::newFromDB( array( 'survey_name' => $surveyName ), $fields, $loadQuestions ); |
81 | 123 | } |
82 | 124 | |
83 | 125 | /** |
— | — | @@ -85,12 +127,13 @@ |
86 | 128 | * @since 0.1 |
87 | 129 | * |
88 | 130 | * @param integer surveyId |
| 131 | + * @param array|null $fields |
89 | 132 | * @param boolean $loadQuestions |
90 | 133 | * |
91 | 134 | * @return Survey or false |
92 | 135 | */ |
93 | | - public static function newFromId( $surveyId, $loadQuestions = true ) { |
94 | | - return self::newFromDB( array( 'survey_id' => $surveyId ), $loadQuestions ); |
| 136 | + public static function newFromId( $surveyId, $fields = null, $loadQuestions = true ) { |
| 137 | + return self::newFromDB( array( 'survey_id' => $surveyId ), $fields, $loadQuestions ); |
95 | 138 | } |
96 | 139 | |
97 | 140 | /** |
— | — | @@ -101,20 +144,25 @@ |
102 | 145 | * @since 0.1 |
103 | 146 | * |
104 | 147 | * @param array $conditions |
| 148 | + * @param array|null $fields |
105 | 149 | * @param boolean $loadQuestions |
106 | 150 | * |
107 | 151 | * @return Survey or false |
108 | 152 | */ |
109 | | - protected static function newFromDB( array $conditions, $loadQuestions = true ) { |
| 153 | + protected static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) { |
110 | 154 | $dbr = wfGetDB( DB_SLAVE ); |
111 | 155 | |
| 156 | + if ( is_null( $fields ) ) { |
| 157 | + $fields = array_keys( self::$fields ); |
| 158 | + } |
| 159 | + |
| 160 | + foreach ( $fields as &$field ) { |
| 161 | + $field = self::$fieldPrefix . $field; |
| 162 | + } |
| 163 | + |
112 | 164 | $survey = $dbr->selectRow( |
113 | 165 | 'surveys', |
114 | | - array( |
115 | | - 'survey_id', |
116 | | - 'survey_name', |
117 | | - 'survey_enabled', |
118 | | - ), |
| 166 | + $fields, |
119 | 167 | $conditions |
120 | 168 | ); |
121 | 169 | |
— | — | @@ -122,11 +170,7 @@ |
123 | 171 | return false; |
124 | 172 | } |
125 | 173 | |
126 | | - $survey = new self( |
127 | | - (int)$survey->survey_id, |
128 | | - $survey->survey_name, |
129 | | - (int)$survey->survey_enabled == 1 |
130 | | - ); |
| 174 | + $survey = self::newFromDBResult( $survey ); |
131 | 175 | |
132 | 176 | if ( $loadQuestions ) { |
133 | 177 | $survey->loadQuestionsFromDB(); |
— | — | @@ -135,7 +179,38 @@ |
136 | 180 | return $survey; |
137 | 181 | } |
138 | 182 | |
| 183 | + protected static function newFromDBResult( $result ) { |
| 184 | + $result = (array)$result; |
| 185 | + $data = array(); |
| 186 | + |
| 187 | + foreach ( $result as $name => $value ) { |
| 188 | + $data[substr( $name, strlen( self::$fieldPrefix ) )] = $value; |
| 189 | + } |
| 190 | + |
| 191 | + return self::newFromArray( $data ); |
| 192 | + } |
| 193 | + |
| 194 | + protected static function newFromArray( array $data ) { |
| 195 | + $survey = new Survey( array_key_exists( 'id', $data ) ? $data['id'] : null ); |
| 196 | + |
| 197 | + $survey->setProps( $data ); |
| 198 | + |
| 199 | + return $survey; |
| 200 | + } |
| 201 | + |
139 | 202 | /** |
| 203 | + * Returns the survey fields. |
| 204 | + * Field name => db field without prefix |
| 205 | + * |
| 206 | + * @since 0.1 |
| 207 | + * |
| 208 | + * @return array |
| 209 | + */ |
| 210 | + public static function getSurveyProps() { |
| 211 | + return array_keys( self::$fields ); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
140 | 215 | * Constructor. |
141 | 216 | * |
142 | 217 | * @since 0.1 |
— | — | @@ -244,6 +319,39 @@ |
245 | 320 | } |
246 | 321 | |
247 | 322 | /** |
| 323 | + * Returns the surveys header. |
| 324 | + * |
| 325 | + * @since 0.1 |
| 326 | + * |
| 327 | + * @return string |
| 328 | + */ |
| 329 | + public function getHeader() { |
| 330 | + return $this->header; |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Returns the surveys footer. |
| 335 | + * |
| 336 | + * @since 0.1 |
| 337 | + * |
| 338 | + * @return string |
| 339 | + */ |
| 340 | + public function getFooter() { |
| 341 | + return $this->footer; |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * Returns the surveys thanks message. |
| 346 | + * |
| 347 | + * @since 0.1 |
| 348 | + * |
| 349 | + * @return string |
| 350 | + */ |
| 351 | + public function getThanks() { |
| 352 | + return $this->thanksMessage; |
| 353 | + } |
| 354 | + |
| 355 | + /** |
248 | 356 | * Sets the surveys questions. |
249 | 357 | * |
250 | 358 | * @since 0.1 |
— | — | @@ -260,12 +368,12 @@ |
261 | 369 | * |
262 | 370 | * @since 0.1 |
263 | 371 | * |
| 372 | + * @param null|array $props |
| 373 | + * |
264 | 374 | * @return array |
265 | 375 | */ |
266 | | - public function toArray() { |
| 376 | + public function toArray( $props = null ) { |
267 | 377 | $data = array( |
268 | | - 'enabled' => $this->enabled, |
269 | | - 'name' => $this->name, |
270 | 378 | 'questions' => array(), |
271 | 379 | ); |
272 | 380 | |
— | — | @@ -273,13 +381,31 @@ |
274 | 382 | $data['questions'][] = $question->toArray(); |
275 | 383 | } |
276 | 384 | |
277 | | - if ( !is_null( $this->id ) ) { |
278 | | - $data['id'] = $this->id; |
| 385 | + if ( !is_array( $props ) ) { |
| 386 | + $props = array_keys( self::$fields ); |
279 | 387 | } |
280 | 388 | |
| 389 | + foreach ( $props as $prop ) { |
| 390 | + if ( !( $prop == 'id' && is_null( $this->{ $prop } ) ) ) { |
| 391 | + $data[$prop] = $this->getProp( $prop ); |
| 392 | + } |
| 393 | + } |
| 394 | + |
281 | 395 | return $data; |
282 | 396 | } |
283 | 397 | |
| 398 | + public static function fromArray( array $data ) { |
| 399 | + $survey = new Survey( array_key_exists( 'id', $data ) ? $data['id'] : null ); |
| 400 | + |
| 401 | + foreach ( $data as $name => $value ) { |
| 402 | + if ( $name != 'id' && array_key_exists( $name, self::$fields ) ) { |
| 403 | + $survey->setProp( $name, $value ); |
| 404 | + } |
| 405 | + } |
| 406 | + |
| 407 | + return $survey; |
| 408 | + } |
| 409 | + |
284 | 410 | /** |
285 | 411 | * Removes the object from the database. |
286 | 412 | * |
— | — | @@ -329,4 +455,38 @@ |
330 | 456 | return $sucecss; |
331 | 457 | } |
332 | 458 | |
| 459 | + public function setProp( $name, $value ) { |
| 460 | + if ( array_key_exists( $name, self::$fields ) ) { |
| 461 | + switch ( self::$fields[$name] ) { |
| 462 | + case 'int': |
| 463 | + $value = (int)$value; |
| 464 | + case 'bool': |
| 465 | + if ( is_string( $value ) ) { |
| 466 | + $value = $value != '0'; |
| 467 | + } |
| 468 | + } |
| 469 | + |
| 470 | + $this->{ $name } = $value; |
| 471 | + } |
| 472 | + } |
| 473 | + |
| 474 | + public function getProp( $name ) { |
| 475 | + if ( array_key_exists( $name, self::$fields ) ) { |
| 476 | + return $this->{ $name }; |
| 477 | + } |
| 478 | + else { |
| 479 | + // TODO: exception |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + public function setProps( array $props ) { |
| 484 | + if ( array_key_exists( 'id', $props ) ) { |
| 485 | + unset( $props['id'] ); |
| 486 | + } |
| 487 | + |
| 488 | + foreach ( $props as $name => $value ) { |
| 489 | + $this->setProp( $name, $value ); |
| 490 | + } |
| 491 | + } |
| 492 | + |
333 | 493 | } |
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php |
— | — | @@ -31,15 +31,18 @@ |
32 | 32 | // Get the requests parameters. |
33 | 33 | $params = $this->extractRequestParams(); |
34 | 34 | |
35 | | - if ( !( isset( $params['ids'] ) XOR isset( $params['names'] ) ) ) { |
| 35 | + if ( !( ( isset( $params['ids'] ) && count( $params['ids'] ) > 0 ) |
| 36 | + XOR ( isset( $params['names'] ) && count( $params['names'] ) > 0 ) |
| 37 | + ) ) { |
36 | 38 | $this->dieUsage( wfMsgExt( 'survey-err-ids-xor-names' ), 'ids-xor-names' ); |
37 | 39 | } |
38 | 40 | |
39 | | - |
40 | 41 | $this->addTables( 'surveys' ); |
41 | 42 | |
42 | | - $this->addFields( 'survey_id' ); |
| 43 | + $fields = array_merge( array( 'id' ), $params['props'] ); |
43 | 44 | |
| 45 | + $this->addFields( $this->getSurveyFields( $fields ) ); |
| 46 | + |
44 | 47 | if ( isset( $params['ids'] ) ) { |
45 | 48 | $this->addWhere( array( 'survey_id' => $params['ids'] ) ); |
46 | 49 | } |
— | — | @@ -70,10 +73,17 @@ |
71 | 74 | break; |
72 | 75 | } |
73 | 76 | |
74 | | - $resultSurveys[] = $this->getSurveyData( Survey::newFromId( |
75 | | - $survey->survey_id, |
76 | | - $params['incquestions'] == 1 |
77 | | - ) ); |
| 77 | + $surveyObject = new Survey( $survey->survey_id ); |
| 78 | + |
| 79 | + foreach ( $fields as $prop ) { |
| 80 | + $surveyObject->setProp( $prop, $survey->{ 'survey_' . $prop } ); |
| 81 | + } |
| 82 | + |
| 83 | + if ( $params['incquestions'] ) { |
| 84 | + $surveyObject->loadQuestionsFromDB(); |
| 85 | + } |
| 86 | + |
| 87 | + $resultSurveys[] = $this->getSurveyData( $surveyObject->toArray( $fields ) ); |
78 | 88 | } |
79 | 89 | |
80 | 90 | $this->getResult()->setIndexedTagName( $resultSurveys, 'survey' ); |
— | — | @@ -85,9 +95,20 @@ |
86 | 96 | ); |
87 | 97 | } |
88 | 98 | |
89 | | - function getSurveyData( Survey $survey ) { |
90 | | - $survey = $survey->toArray(); |
| 99 | + protected function getSurveyFields( array $props ) { |
| 100 | + $fields = array(); |
| 101 | + $allowedFields = Survey::getSurveyProps(); |
91 | 102 | |
| 103 | + foreach ( $props as $prop ) { |
| 104 | + if ( in_array( $prop, $allowedFields ) ) { |
| 105 | + $fields[] = "survey_$prop"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $fields; |
| 110 | + } |
| 111 | + |
| 112 | + protected function getSurveyData( array $survey ) { |
92 | 113 | foreach ( $survey['questions'] as $nr => $question ) { |
93 | 114 | $this->getResult()->setIndexedTagName( $survey['questions'][$nr], 'answer' ); |
94 | 115 | } |
— | — | @@ -111,6 +132,11 @@ |
112 | 133 | ApiBase::PARAM_TYPE => 'string', |
113 | 134 | ApiBase::PARAM_ISMULTI => true, |
114 | 135 | ), |
| 136 | + 'props' => array( |
| 137 | + ApiBase::PARAM_TYPE => Survey::getSurveyProps(), |
| 138 | + ApiBase::PARAM_ISMULTI => true, |
| 139 | + ApiBase::PARAM_DFLT => 'id|name|enabled' |
| 140 | + ), |
115 | 141 | 'incquestions' => array( |
116 | 142 | ApiBase::PARAM_TYPE => 'integer', |
117 | 143 | ApiBase::PARAM_DFLT => '0', |
— | — | @@ -141,6 +167,7 @@ |
142 | 168 | 'names' => 'The names of the surveys to return', |
143 | 169 | 'incquestions' => 'Include the questions of the surveys or not', |
144 | 170 | 'enabled' => 'Enabled state to filter on', |
| 171 | + 'props' => 'Survey data to query', |
145 | 172 | 'continue' => 'Offset number from where to continue the query', |
146 | 173 | 'limit' => 'Max amount of words to return', |
147 | 174 | 'token' => 'Edit token. You can get one of these through prop=info.', |