Index: trunk/extensions/Survey/Survey.i18n.php |
— | — | @@ -71,4 +71,7 @@ |
72 | 72 | 'survey-special-label-text' => 'Question text', |
73 | 73 | 'survey-special-remove' => 'Remove question', |
74 | 74 | 'survey-special-remove-confirm' => 'Are you sure you want to remove this question?', |
| 75 | + 'survey-special-label-header' => 'Text to display above the survey', |
| 76 | + 'survey-special-label-footer' => 'Text to display below the survey', |
| 77 | + 'survey-special-label-thanks' => 'Thanks message to display after submission of the survey', |
75 | 78 | ); |
Index: trunk/extensions/Survey/specials/SpecialSurvey.php |
— | — | @@ -42,7 +42,7 @@ |
43 | 43 | $survey = new Survey( null ); |
44 | 44 | } |
45 | 45 | else { |
46 | | - $survey = Survey::newFromName( $subPage, true ); |
| 46 | + $survey = Survey::newFromName( $subPage, null, true ); |
47 | 47 | } |
48 | 48 | |
49 | 49 | if ( $survey === false ) { |
— | — | @@ -71,7 +71,7 @@ |
72 | 72 | $survey = new Survey( null ); |
73 | 73 | } |
74 | 74 | else { |
75 | | - $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), false ); |
| 75 | + $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), null, false ); |
76 | 76 | } |
77 | 77 | |
78 | 78 | $survey->setName( $wgRequest->getText( 'survey-name' ) ); |
— | — | @@ -171,7 +171,7 @@ |
172 | 172 | |
173 | 173 | $fields[] = array( |
174 | 174 | 'type' => 'text', |
175 | | - 'default' => $survey->getName(), |
| 175 | + 'default' => $survey->getField( 'name' ), |
176 | 176 | 'label-message' => 'survey-special-label-name', |
177 | 177 | 'id' => 'survey-name', |
178 | 178 | 'name' => 'survey-name', |
— | — | @@ -179,7 +179,7 @@ |
180 | 180 | |
181 | 181 | $fields[] = array( |
182 | 182 | 'type' => 'check', |
183 | | - 'default' => $survey->isEnabled() ? '1' : '0', |
| 183 | + 'default' => $survey->getField( 'enabled' ) ? '1' : '0', |
184 | 184 | 'label-message' => 'survey-special-label-enabled', |
185 | 185 | 'id' => 'survey-enabled', |
186 | 186 | 'name' => 'survey-enabled', |
— | — | @@ -187,7 +187,7 @@ |
188 | 188 | |
189 | 189 | $fields[] = array( |
190 | 190 | 'type' => 'text', |
191 | | - 'default' => $survey->getHeader(), |
| 191 | + 'default' => $survey->getField( 'header' ), |
192 | 192 | 'label-message' => 'survey-special-label-header', |
193 | 193 | 'id' => 'survey-header', |
194 | 194 | 'name' => 'survey-header', |
— | — | @@ -195,7 +195,7 @@ |
196 | 196 | |
197 | 197 | $fields[] = array( |
198 | 198 | 'type' => 'text', |
199 | | - 'default' => $survey->getFooter(), |
| 199 | + 'default' => $survey->getField( 'footer' ), |
200 | 200 | 'label-message' => 'survey-special-label-footer', |
201 | 201 | 'id' => 'survey-footer', |
202 | 202 | 'name' => 'survey-footer', |
— | — | @@ -203,7 +203,7 @@ |
204 | 204 | |
205 | 205 | $fields[] = array( |
206 | 206 | 'type' => 'text', |
207 | | - 'default' => $survey->getThanks(), |
| 207 | + 'default' => $survey->getField( 'thanks' ), |
208 | 208 | 'label-message' => 'survey-special-label-thanks', |
209 | 209 | 'id' => 'survey-thanks', |
210 | 210 | 'name' => 'survey-thanks', |
— | — | @@ -213,9 +213,9 @@ |
214 | 214 | $fields[] = array( |
215 | 215 | 'class' => 'SurveyQuestionField', |
216 | 216 | 'options' => array( |
217 | | - 'required' => $question->isRequired(), |
218 | | - 'text' => $question->getText(), |
219 | | - 'type' => $question->getType(), |
| 217 | + 'required' => $question->getField( 'required' ), |
| 218 | + 'text' => $question->getField( 'text' ), |
| 219 | + 'type' => $question->getField( 'type' ), |
220 | 220 | 'id' => $question->getId(), |
221 | 221 | ) |
222 | 222 | ); |
Index: trunk/extensions/Survey/INSTALL |
— | — | @@ -9,7 +9,7 @@ |
10 | 10 | Survey requires: |
11 | 11 | |
12 | 12 | * MediaWiki 1.17 or above |
13 | | -* PHP 5.2 or above |
| 13 | +* PHP 5.3 or above |
14 | 14 | |
15 | 15 | == Download == |
16 | 16 | |
Index: trunk/extensions/Survey/includes/SurveyDBClass.php |
— | — | @@ -14,41 +14,187 @@ |
15 | 15 | abstract class SurveyDBClass { |
16 | 16 | |
17 | 17 | /** |
| 18 | + * The fields of the object. |
| 19 | + * field name (w/o prefix) => value |
| 20 | + * |
| 21 | + * @since 0.1 |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $fields; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor. |
| 28 | + * |
| 29 | + * @since 0.1 |
| 30 | + * |
| 31 | + * @param array|null $fields |
| 32 | + */ |
| 33 | + public function __construct( $fields ) { |
| 34 | + if ( !is_null( $fields ) ) { |
| 35 | + $this->setFields( $fields ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns an array with the fields and their types this object contains. |
| 41 | + * This corresponds directly to the fields in the database, without prefix. |
| 42 | + * |
| 43 | + * field name => type |
| 44 | + * |
| 45 | + * Allowed types: |
| 46 | + * * str |
| 47 | + * * int |
| 48 | + * * bool |
| 49 | + * |
| 50 | + * @since 0.1 |
| 51 | + * |
| 52 | + * @return array |
| 53 | + */ |
| 54 | + protected static function getFieldTypes() { |
| 55 | + return array(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
18 | 59 | * Returns the name of the database table objects of this type are stored in. |
19 | 60 | * |
20 | 61 | * @since 0.1 |
21 | 62 | * |
| 63 | + * @throws MWException |
22 | 64 | * @return string |
23 | 65 | */ |
24 | | - protected abstract function getDBTable(); |
| 66 | + protected static function getDBTable() { |
| 67 | + throw new MWException( 'Class did not implement getDBTable' ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the db field prefix. |
| 72 | + * |
| 73 | + * @since 0.1 |
| 74 | + * |
| 75 | + * @throws MWException |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + protected static function getFieldPrefix() { |
| 79 | + throw new MWException( 'Class did not implement getFieldPrefix' ); |
| 80 | + } |
25 | 81 | |
26 | 82 | /** |
27 | | - * Returns the name of the id field. |
| 83 | + * Returns the name of the id db field, without prefix. |
28 | 84 | * |
29 | 85 | * @since 0.1 |
30 | 86 | * |
31 | 87 | * @return string |
32 | 88 | */ |
33 | | - protected abstract function getIDField(); |
| 89 | + protected static function getIDField() { |
| 90 | + return 'id'; |
| 91 | + } |
34 | 92 | |
35 | 93 | /** |
36 | | - * Gets the fields => values to write to the database table. |
| 94 | + * Get a new instance of the class from a database result. |
37 | 95 | * |
38 | 96 | * @since 0.1 |
39 | 97 | * |
40 | | - * @return array |
| 98 | + * @param object $result |
| 99 | + * |
| 100 | + * @return SurveyDBClass |
41 | 101 | */ |
42 | | - protected abstract function getWriteValues(); |
| 102 | + public static function newFromDBResult( $result ) { |
| 103 | + $result = (array)$result; |
| 104 | + $data = array(); |
| 105 | + $idFieldLength = strlen( static::getFieldPrefix() ); |
| 106 | + |
| 107 | + foreach ( $result as $name => $value ) { |
| 108 | + $data[substr( $name, $idFieldLength )] = $value; |
| 109 | + } |
| 110 | + |
| 111 | + return static::newFromArray( $data ); |
| 112 | + } |
43 | 113 | |
44 | 114 | /** |
45 | | - * The ID of the question DB record, or null if not inserted yet. |
| 115 | + * Get a new instance of the class from an array. |
46 | 116 | * |
47 | 117 | * @since 0.1 |
48 | | - * @var integer|null |
| 118 | + * |
| 119 | + * @param array $data |
| 120 | + * |
| 121 | + * @return SurveyDBClass |
| 122 | + */ |
| 123 | + public static function newFromArray( array $data ) { |
| 124 | + return new static( $data ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Selects the the specified fields of the records matching the provided |
| 129 | + * conditions. |
| 130 | + * |
| 131 | + * @since 0.1 |
| 132 | + * |
| 133 | + * @param array|null $fields |
| 134 | + * @param array $conditions |
| 135 | + * @param array $options |
| 136 | + * |
| 137 | + * @return array of self |
49 | 138 | */ |
50 | | - protected $id; |
| 139 | + public static function select( $fields = null, array $conditions = array(), array $options = array() ) { |
| 140 | + $result = static::rawSelect( $fields, $conditions, $options ); |
| 141 | + |
| 142 | + $objects = array(); |
| 143 | + |
| 144 | + foreach ( $result as $record ) { |
| 145 | + $objects[] = static::newFromDBResult( $record ); |
| 146 | + } |
| 147 | + |
| 148 | + return $objects; |
| 149 | + } |
51 | 150 | |
52 | 151 | /** |
| 152 | + * Selects the the specified fields of the first matching record. |
| 153 | + * |
| 154 | + * @since 0.1 |
| 155 | + * |
| 156 | + * @param array|null $fields |
| 157 | + * @param array $conditions |
| 158 | + * @param array $options |
| 159 | + * |
| 160 | + * @return self|false |
| 161 | + */ |
| 162 | + public static function selectRow( $fields = null, array $conditions = array(), array $options = array() ) { |
| 163 | + $options['LIMIT'] = 1; |
| 164 | + |
| 165 | + $objects = static::select( $fields, $conditions, $options ); |
| 166 | + |
| 167 | + return count( $objects ) > 0 ? $objects[0] : false; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Selects the the specified fields of the records matching the provided |
| 172 | + * conditions. |
| 173 | + * |
| 174 | + * @since 0.1 |
| 175 | + * |
| 176 | + * @param array|null $fields |
| 177 | + * @param array $conditions |
| 178 | + * @param array $options |
| 179 | + * |
| 180 | + * @return ResultWrapper |
| 181 | + */ |
| 182 | + public static function rawSelect( $fields = null, array $conditions = array(), array $options = array() ) { |
| 183 | + if ( is_null( $fields ) ) { |
| 184 | + $fields = array_keys( static::getFieldTypes() ); |
| 185 | + } |
| 186 | + |
| 187 | + $dbr = wfgetDB( DB_SLAVE ); |
| 188 | + |
| 189 | + return $dbr->select( |
| 190 | + static::getDBTable(), |
| 191 | + static::getPrefixedFields( $fields ), |
| 192 | + count( $conditions ) == 0 ? '' : static::getPrefixedValues( $conditions ), |
| 193 | + '', |
| 194 | + $options |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
53 | 199 | * Writes the answer to the database, either updating it |
54 | 200 | * when it already exists, or inserting it when it doesn't. |
55 | 201 | * |
— | — | @@ -57,11 +203,11 @@ |
58 | 204 | * @return boolean Success indicator |
59 | 205 | */ |
60 | 206 | public function writeToDB() { |
61 | | - if ( is_null( $this->id ) ) { |
| 207 | + if ( $this->hadIdField() ) { |
62 | 208 | return $this->insertIntoDB(); |
63 | 209 | } |
64 | 210 | else { |
65 | | - return $this->updateInDB(); |
| 211 | + return $this->updateInDB(); |
66 | 212 | } |
67 | 213 | } |
68 | 214 | |
— | — | @@ -78,7 +224,7 @@ |
79 | 225 | return $dbw->update( |
80 | 226 | $this->getDBTable(), |
81 | 227 | $this->getWriteValues(), |
82 | | - array( $this->getIDField() => $this->id ) |
| 228 | + array( static::getFieldPrefix() . static::getIDField() => $this->getId() ) |
83 | 229 | ); |
84 | 230 | } |
85 | 231 | |
— | — | @@ -93,11 +239,11 @@ |
94 | 240 | $dbw = wfGetDB( DB_MASTER ); |
95 | 241 | |
96 | 242 | $result = $dbw->insert( |
97 | | - $this->getDBTable(), |
98 | | - $this->getWriteValues() |
| 243 | + static::getDBTable(), |
| 244 | + static::getWriteValues() |
99 | 245 | ); |
100 | 246 | |
101 | | - $this->id = $dbw->insertId(); |
| 247 | + $this->setField( static::getIDField(), $dbw->insertId() ); |
102 | 248 | |
103 | 249 | return $result; |
104 | 250 | } |
— | — | @@ -113,18 +259,117 @@ |
114 | 260 | $dbw = wfGetDB( DB_MASTER ); |
115 | 261 | |
116 | 262 | $sucecss = $dbw->delete( |
117 | | - $this->getDBTable(), |
118 | | - array( $this->getIDField() => $this->id ) |
| 263 | + static::getDBTable(), |
| 264 | + array( static::getFieldPrefix() . static::getIDField() => $this->getId() ) |
119 | 265 | ); |
120 | 266 | |
121 | 267 | if ( $sucecss ) { |
122 | | - $this->id = null; |
| 268 | + $this->removeField( static::getIDField() ); |
123 | 269 | } |
124 | 270 | |
125 | 271 | return $sucecss; |
126 | 272 | } |
127 | 273 | |
128 | 274 | /** |
| 275 | + * Return the names of the fields. |
| 276 | + * |
| 277 | + * @since 0.1 |
| 278 | + * |
| 279 | + * @return array |
| 280 | + */ |
| 281 | + public function getFields() { |
| 282 | + return $this->fields; |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Return the names of the fields. |
| 287 | + * |
| 288 | + * @since 0.1 |
| 289 | + * |
| 290 | + * @return array |
| 291 | + */ |
| 292 | + public static function getFieldNames() { |
| 293 | + return array_keys( static::getFieldTypes() ); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Return the names of the fields. |
| 298 | + * |
| 299 | + * @since 0.1 |
| 300 | + * |
| 301 | + * @return array |
| 302 | + */ |
| 303 | + public function getSetFieldNames() { |
| 304 | + return array_keys( $this->fields ); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Sets the value of a field. |
| 309 | + * Strings can be provided for other types, |
| 310 | + * so this method can be called from unserialization handlers. |
| 311 | + * |
| 312 | + * @since 0.1 |
| 313 | + * |
| 314 | + * @param string $name |
| 315 | + * @param mixed $value |
| 316 | + * |
| 317 | + * @throws MWException |
| 318 | + */ |
| 319 | + public function setField( $name, $value ) { |
| 320 | + $fields = static::getFieldTypes(); |
| 321 | + |
| 322 | + if ( array_key_exists( $name, $fields ) ) { |
| 323 | + switch ( $fields[$name] ) { |
| 324 | + case 'int': |
| 325 | + $value = (int)$value; |
| 326 | + case 'bool': |
| 327 | + if ( is_string( $value ) ) { |
| 328 | + $value = $value != '0'; |
| 329 | + } |
| 330 | + case 'array': |
| 331 | + if ( is_string( $value ) ) { |
| 332 | + $value = unserialize( $value ); |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + $this->fields[$name] = $value; |
| 337 | + } |
| 338 | + else { |
| 339 | + throw new MWException( 'Attempted to set unknonw field ' . $name ); |
| 340 | + } |
| 341 | + } |
| 342 | + |
| 343 | + /** |
| 344 | + * Gets the value of a field. |
| 345 | + * |
| 346 | + * @since 0.1 |
| 347 | + * |
| 348 | + * @param string $name |
| 349 | + * |
| 350 | + * @throws MWException |
| 351 | + * @return mixed |
| 352 | + */ |
| 353 | + public function getField( $name ) { |
| 354 | + if ( array_key_exists( $name, static::getFieldTypes() ) ) { |
| 355 | + return $this->fields[$name]; |
| 356 | + } |
| 357 | + else { |
| 358 | + throw new MWException( 'Attempted to get unknonw field ' . $name ); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + /** |
| 363 | + * Remove a field. |
| 364 | + * |
| 365 | + * @since 0.1 |
| 366 | + * |
| 367 | + * @param string $name |
| 368 | + */ |
| 369 | + public function removeField( $name ) { |
| 370 | + unset( $this->fields[$name] ); |
| 371 | + } |
| 372 | + |
| 373 | + /** |
129 | 374 | * Returns the objects database id. |
130 | 375 | * |
131 | 376 | * @since 0.1 |
— | — | @@ -132,7 +377,176 @@ |
133 | 378 | * @return integer|null |
134 | 379 | */ |
135 | 380 | public function getId() { |
136 | | - return $this->id; |
| 381 | + return $this->getField( static::getIDField() ); |
137 | 382 | } |
138 | 383 | |
| 384 | + /** |
| 385 | + * Gets if a certain field is set. |
| 386 | + * |
| 387 | + * @since 0.1 |
| 388 | + * |
| 389 | + * @param string $name |
| 390 | + * |
| 391 | + * @return boolean |
| 392 | + */ |
| 393 | + public function hasField( $name ) { |
| 394 | + return array_key_exists( $name, $this->fields ); |
| 395 | + } |
| 396 | + |
| 397 | + /** |
| 398 | + * Gets if the object can take a certain field. |
| 399 | + * |
| 400 | + * @since 0.1 |
| 401 | + * |
| 402 | + * @param string $name |
| 403 | + * |
| 404 | + * @return boolean |
| 405 | + */ |
| 406 | + public static function canHasField( $name ) { |
| 407 | + return array_key_exists( $name, static::getFieldTypes() ); |
| 408 | + } |
| 409 | + |
| 410 | + /** |
| 411 | + * Gets if the id field is set. |
| 412 | + * |
| 413 | + * @since 0.1 |
| 414 | + * |
| 415 | + * @return boolean |
| 416 | + */ |
| 417 | + public function hasIdField() { |
| 418 | + return $this->hasField( static::getIDField() ) |
| 419 | + && !is_null( $this->getField( static::getIDField() ) ); |
| 420 | + } |
| 421 | + |
| 422 | + /** |
| 423 | + * Sets multiple fields. |
| 424 | + * |
| 425 | + * @since 0.1 |
| 426 | + * |
| 427 | + * @param array $fields |
| 428 | + */ |
| 429 | + public function setFields( array $fields ) { |
| 430 | + foreach ( $fields as $name => $value ) { |
| 431 | + $this->setField( $name, $value ); |
| 432 | + } |
| 433 | + } |
| 434 | + |
| 435 | + /** |
| 436 | + * Gets the fields => values to write to the surveys table. |
| 437 | + * |
| 438 | + * @since 0.1 |
| 439 | + * |
| 440 | + * @return array |
| 441 | + */ |
| 442 | + protected function getWriteValues() { |
| 443 | + $values = array(); |
| 444 | + |
| 445 | + foreach ( static::getFieldTypes() as $name => $type ) { |
| 446 | + $value = $this->fields[$name]; |
| 447 | + |
| 448 | + switch ( $type ) { |
| 449 | + case 'array': |
| 450 | + $value = serialize( (array)$value ); |
| 451 | + } |
| 452 | + |
| 453 | + $values[static::getFieldPrefix() . $name] = $value; |
| 454 | + } |
| 455 | + |
| 456 | + return $values; |
| 457 | + } |
| 458 | + |
| 459 | + /** |
| 460 | + * Takes in a field or array of fields and returns an |
| 461 | + * array with their prefixed versions, ready for db usage. |
| 462 | + * |
| 463 | + * @since 0.1 |
| 464 | + * |
| 465 | + * @param array|string $fields |
| 466 | + * |
| 467 | + * @return array |
| 468 | + */ |
| 469 | + public static function getPrefixedFields( $fields ) { |
| 470 | + $fields = (array)$fields; |
| 471 | + |
| 472 | + foreach ( $fields as &$field ) { |
| 473 | + $field = static::getFieldPrefix() . $field; |
| 474 | + } |
| 475 | + |
| 476 | + return $fields; |
| 477 | + } |
| 478 | + |
| 479 | + /** |
| 480 | + * Takes in an associative array with field names as keys and |
| 481 | + * their values as value. The field names are prefixed with the |
| 482 | + * db field prefix. |
| 483 | + * |
| 484 | + * @since 0.1 |
| 485 | + * |
| 486 | + * @param array $values |
| 487 | + * |
| 488 | + * @return array |
| 489 | + */ |
| 490 | + public static function getPrefixedValues( array $values ) { |
| 491 | + $prefixedValues = array(); |
| 492 | + |
| 493 | + foreach ( $values as $field => $value ) { |
| 494 | + $prefixedValues[static::getFieldPrefix() . $field] = $value; |
| 495 | + } |
| 496 | + |
| 497 | + return $prefixedValues; |
| 498 | + } |
| 499 | + |
| 500 | + /** |
| 501 | + * Serializes the survey to an associative array which |
| 502 | + * can then easily be converted into JSON or similar. |
| 503 | + * |
| 504 | + * @since 0.1 |
| 505 | + * |
| 506 | + * @param null|array $props |
| 507 | + * |
| 508 | + * @return array |
| 509 | + */ |
| 510 | + public function toArray( $fields = null ) { |
| 511 | + $data = array(); |
| 512 | + $setFields = array(); |
| 513 | + |
| 514 | + if ( !is_array( $fields ) ) { |
| 515 | + $setFields = $this->getSetFieldNames(); |
| 516 | + } |
| 517 | + else { |
| 518 | + foreach ( $fields as $field ) { |
| 519 | + if ( $this->hasField( $field ) ) { |
| 520 | + $setFields[] = $field; |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + |
| 525 | + foreach ( $setFields as $field ) { |
| 526 | + $data[$field] = $this->getField( $field ); |
| 527 | + } |
| 528 | + |
| 529 | + return $data; |
| 530 | + } |
| 531 | + |
| 532 | + /** |
| 533 | + * Creates and returns a new instance from an array. |
| 534 | + * |
| 535 | + * @since 0.1 |
| 536 | + * |
| 537 | + * @param array $data |
| 538 | + * |
| 539 | + * @return SurveyDBClass |
| 540 | + */ |
| 541 | + public static function fromArray( array $data ) { |
| 542 | + $validData = array(); |
| 543 | + |
| 544 | + foreach ( $data as $name => $value ) { |
| 545 | + if ( static::canHasField( $name ) ) { |
| 546 | + $validData[$name] = $value; |
| 547 | + } |
| 548 | + } |
| 549 | + |
| 550 | + return new static( $validData ); |
| 551 | + } |
| 552 | + |
139 | 553 | } |
Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -13,99 +13,36 @@ |
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 | | - |
26 | 17 | /** |
27 | | - * |
28 | | - * |
29 | | - * @since 0.1 |
30 | | - * @var string |
31 | | - */ |
32 | | - protected static $fieldPrefix = 'survey_'; |
33 | | - |
34 | | - /** |
35 | | - * The name of the survey. |
36 | | - * |
37 | | - * @since 0.1 |
38 | | - * @var string |
39 | | - */ |
40 | | - protected $name; |
41 | | - |
42 | | - /** |
43 | | - * If the survey is enabled or not. |
44 | | - * |
45 | | - * @since 0.1 |
46 | | - * @var boolean |
47 | | - */ |
48 | | - protected $enabled; |
49 | | - |
50 | | - /** |
51 | | - * The questions that go with this survey. |
52 | | - * |
53 | | - * @since 0.1 |
54 | | - * @var array of SurveyQuestion |
55 | | - */ |
56 | | - protected $questions; |
57 | | - |
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 | | - /** |
83 | 18 | * @see SurveyDBClass::getDBTable() |
84 | 19 | */ |
85 | | - protected function getDBTable() { |
| 20 | + protected static function getDBTable() { |
86 | 21 | return 'surveys'; |
87 | 22 | } |
88 | 23 | |
89 | 24 | /** |
90 | | - * @see SurveyDBClass::getIDField() |
91 | | - */ |
92 | | - protected function getIDField() { |
93 | | - return 'survey_id'; |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Gets the fields => values to write to the surveys table. |
| 25 | + * Returns an array with the fields and their types this object contains. |
| 26 | + * This corresponds directly to the fields in the database, without prefix. |
98 | 27 | * |
99 | 28 | * @since 0.1 |
100 | 29 | * |
101 | 30 | * @return array |
102 | 31 | */ |
103 | | - protected function getWriteValues() { |
| 32 | + protected static function getFieldTypes() { |
104 | 33 | return array( |
105 | | - 'survey_enabled' => $this->enabled, |
106 | | - 'survey_name' => $this->name, |
| 34 | + 'id' => 'int', |
| 35 | + 'name' => 'str', |
| 36 | + 'enabled' => 'bool', |
| 37 | + 'header' => 'str', |
| 38 | + 'footer' => 'str', |
| 39 | + 'thanks' => 'str', |
107 | 40 | ); |
108 | 41 | } |
109 | 42 | |
| 43 | + protected static function getFieldPrefix() { |
| 44 | + return 'survey_'; |
| 45 | + } |
| 46 | + |
110 | 47 | /** |
111 | 48 | * Returns the Survey with specified name, or false if there is no such survey. |
112 | 49 | * |
— | — | @@ -118,7 +55,7 @@ |
119 | 56 | * @return Survey or false |
120 | 57 | */ |
121 | 58 | public static function newFromName( $surveyName, $fields = null, $loadQuestions = true ) { |
122 | | - return self::newFromDB( array( 'survey_name' => $surveyName ), $fields, $loadQuestions ); |
| 59 | + return self::newFromDB( array( 'name' => $surveyName ), $fields, $loadQuestions ); |
123 | 60 | } |
124 | 61 | |
125 | 62 | /** |
— | — | @@ -133,7 +70,7 @@ |
134 | 71 | * @return Survey or false |
135 | 72 | */ |
136 | 73 | public static function newFromId( $surveyId, $fields = null, $loadQuestions = true ) { |
137 | | - return self::newFromDB( array( 'survey_id' => $surveyId ), $fields, $loadQuestions ); |
| 74 | + return self::newFromDB( array( 'id' => $surveyId ), $fields, $loadQuestions ); |
138 | 75 | } |
139 | 76 | |
140 | 77 | /** |
— | — | @@ -149,29 +86,9 @@ |
150 | 87 | * |
151 | 88 | * @return Survey or false |
152 | 89 | */ |
153 | | - protected static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) { |
154 | | - $dbr = wfGetDB( DB_SLAVE ); |
| 90 | + public static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) { |
| 91 | + $survey = self::selectRow( $fields, $conditions ); |
155 | 92 | |
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 | | - |
164 | | - $survey = $dbr->selectRow( |
165 | | - 'surveys', |
166 | | - $fields, |
167 | | - $conditions |
168 | | - ); |
169 | | - |
170 | | - if ( !$survey ) { |
171 | | - return false; |
172 | | - } |
173 | | - |
174 | | - $survey = self::newFromDBResult( $survey ); |
175 | | - |
176 | 93 | if ( $loadQuestions ) { |
177 | 94 | $survey->loadQuestionsFromDB(); |
178 | 95 | } |
— | — | @@ -179,52 +96,16 @@ |
180 | 97 | return $survey; |
181 | 98 | } |
182 | 99 | |
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 | | - |
202 | 100 | /** |
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 | | - /** |
215 | 101 | * Constructor. |
216 | 102 | * |
217 | 103 | * @since 0.1 |
218 | 104 | * |
219 | | - * @param integer|null $id |
220 | | - * @param string $name |
221 | | - * @param boolean $enabled |
| 105 | + * @param array|null $fields |
222 | 106 | * @param array $questions |
223 | 107 | */ |
224 | | - public function __construct( $id, $name = '', $enabled = false, array $questions = array() ) { |
225 | | - $this->id = $id; |
226 | | - $this->name = $name; |
227 | | - $this->enabled = $enabled; |
228 | | - |
| 108 | + public function __construct( $fields, array $questions = array() ) { |
| 109 | + parent::__construct( $fields ); |
229 | 110 | $this->setQuestions( $questions ); |
230 | 111 | } |
231 | 112 | |
— | — | @@ -234,7 +115,7 @@ |
235 | 116 | * @since 0.1 |
236 | 117 | */ |
237 | 118 | public function loadQuestionsFromDB() { |
238 | | - $this->questions = SurveyQuestion::getQuestionsForSurvey( $this->id ); |
| 119 | + $this->questions = SurveyQuestion::getQuestionsForSurvey( $this->getId() ); |
239 | 120 | } |
240 | 121 | |
241 | 122 | /** |
— | — | @@ -262,52 +143,7 @@ |
263 | 144 | return $success; |
264 | 145 | } |
265 | 146 | |
266 | | - |
267 | 147 | /** |
268 | | - * Returns the survey name. |
269 | | - * |
270 | | - * @since 0.1 |
271 | | - * |
272 | | - * @return string |
273 | | - */ |
274 | | - public function getName() { |
275 | | - return $this->name; |
276 | | - } |
277 | | - |
278 | | - /** |
279 | | - * Sets the survey name. |
280 | | - * |
281 | | - * @since 0.1 |
282 | | - * |
283 | | - * @param string $name |
284 | | - */ |
285 | | - public function setName( $name ) { |
286 | | - $this->name = $name; |
287 | | - } |
288 | | - |
289 | | - /** |
290 | | - * Returns if the survey is enabled. |
291 | | - * |
292 | | - * @since 0.1 |
293 | | - * |
294 | | - * @return boolean |
295 | | - */ |
296 | | - public function isEnabled() { |
297 | | - return $this->enabled; |
298 | | - } |
299 | | - |
300 | | - /** |
301 | | - * Sets if the survey is enabled or not. |
302 | | - * |
303 | | - * @since 0.1 |
304 | | - * |
305 | | - * @param boolean $enabled |
306 | | - */ |
307 | | - public function setEnabled( $enabled ) { |
308 | | - $this->enabled = $enabled; |
309 | | - } |
310 | | - |
311 | | - /** |
312 | 148 | * Returns the surveys questions. |
313 | 149 | * |
314 | 150 | * @since 0.1 |
— | — | @@ -319,39 +155,6 @@ |
320 | 156 | } |
321 | 157 | |
322 | 158 | /** |
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 | | - /** |
356 | 159 | * Sets the surveys questions. |
357 | 160 | * |
358 | 161 | * @since 0.1 |
— | — | @@ -368,44 +171,22 @@ |
369 | 172 | * |
370 | 173 | * @since 0.1 |
371 | 174 | * |
372 | | - * @param null|array $props |
| 175 | + * @param null|array $fields |
373 | 176 | * |
374 | 177 | * @return array |
375 | 178 | */ |
376 | | - public function toArray( $props = null ) { |
377 | | - $data = array( |
378 | | - 'questions' => array(), |
379 | | - ); |
| 179 | + public function toArray( $fields = null ) { |
| 180 | + $data = parent::toArray( $fields ); |
380 | 181 | |
| 182 | + $data['questions'] = array(); |
| 183 | + |
381 | 184 | foreach ( $this->questions as /* SurveyQuestion */ $question ) { |
382 | 185 | $data['questions'][] = $question->toArray(); |
383 | 186 | } |
384 | 187 | |
385 | | - if ( !is_array( $props ) ) { |
386 | | - $props = array_keys( self::$fields ); |
387 | | - } |
388 | | - |
389 | | - foreach ( $props as $prop ) { |
390 | | - if ( !( $prop == 'id' && is_null( $this->{ $prop } ) ) ) { |
391 | | - $data[$prop] = $this->getProp( $prop ); |
392 | | - } |
393 | | - } |
394 | | - |
395 | 188 | return $data; |
396 | 189 | } |
397 | 190 | |
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 | | - |
410 | 191 | /** |
411 | 192 | * Removes the object from the database. |
412 | 193 | * |
— | — | @@ -455,38 +236,4 @@ |
456 | 237 | return $sucecss; |
457 | 238 | } |
458 | 239 | |
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 | | - |
493 | 240 | } |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -19,80 +19,47 @@ |
20 | 20 | public static $TYPE_RADIO = 3; |
21 | 21 | |
22 | 22 | /** |
| 23 | + * Returns an array with the fields and their types this object contains. |
| 24 | + * This corresponds directly to the fields in the database, without prefix. |
| 25 | + * |
| 26 | + * survey_id: |
23 | 27 | * The ID of the survey this question belongs to. |
24 | 28 | * This can be null. When written to the db via Survey::writeToDB of |
25 | 29 | * a Survey holding this question, the survey ID will first be set. |
26 | 30 | * |
27 | | - * @since 0.1 |
28 | | - * @var integer|null |
29 | | - */ |
30 | | - protected $surveyId; |
31 | | - |
32 | | - /** |
| 31 | + * text: |
33 | 32 | * The question text. |
34 | 33 | * |
35 | | - * @since 0.1 |
36 | | - * @var string |
37 | | - */ |
38 | | - protected $text; |
39 | | - |
40 | | - /** |
| 34 | + * type: |
41 | 35 | * The question type. |
42 | 36 | * |
43 | | - * @since 0.1 |
44 | | - * @var integer |
45 | | - */ |
46 | | - protected $type; |
47 | | - |
48 | | - /** |
| 37 | + * required: |
49 | 38 | * Indicated if the question is required, |
50 | 39 | * ie if the user can not submit the survey without answering it. |
51 | 40 | * |
52 | | - * @since 0.1 |
53 | | - * @var boolean |
54 | | - */ |
55 | | - protected $required; |
56 | | - |
57 | | - /** |
| 41 | + * answers: |
58 | 42 | * List of allowed values for the question. |
59 | 43 | * Empty list for no restrictions. |
60 | 44 | * |
61 | | - * @since 0.1 |
62 | | - * @var array of string|number |
63 | | - */ |
64 | | - protected $answers; |
65 | | - |
66 | | - /** |
| 45 | + * removed: |
67 | 46 | * Indicated if the question was removed. |
68 | 47 | * Removed questions are kept in the db so their answers can |
69 | 48 | * still be used untill the survey itself gets removed. |
70 | 49 | * |
71 | 50 | * @since 0.1 |
72 | | - * @var boolean |
73 | | - */ |
74 | | - protected $removed; |
75 | | - |
76 | | - /** |
77 | | - * Constructor. |
78 | 51 | * |
79 | | - * @since 0.1 |
80 | | - * |
81 | | - * @param integer|null $id |
82 | | - * @param integer $surveyId |
83 | | - * @param string $text |
84 | | - * @param integer $type |
85 | | - * @param boolean $required |
86 | | - * @param array $answers |
87 | | - * @param boolean $removed |
| 52 | + * @return array |
88 | 53 | */ |
89 | | - public function __construct( $id, $surveyId, $text, $type, $required, array $answers = array(), $removed = false ) { |
90 | | - $this->id = is_null( $id ) ? $id : (int)$id; |
91 | | - $this->surveyId = (int)$surveyId; |
92 | | - $this->text = $text; |
93 | | - $this->type = $type; |
94 | | - $this->required = (boolean)$required; |
95 | | - $this->answers = $answers; |
96 | | - $this->removed = $removed; |
| 54 | + protected static function getFieldTypes() { |
| 55 | + return array( |
| 56 | + 'id' => 'int', |
| 57 | + 'survey_id' => 'int', |
| 58 | + 'text' => 'str', |
| 59 | + 'type' => 'int', |
| 60 | + 'required' => 'bool', |
| 61 | + 'answers' => 'array', |
| 62 | + 'removed' => 'bool', |
| 63 | + ); |
97 | 64 | } |
98 | 65 | |
99 | 66 | /** |
— | — | @@ -111,27 +78,6 @@ |
112 | 79 | } |
113 | 80 | |
114 | 81 | /** |
115 | | - * Unserializes a survey question in the form of an associative array. |
116 | | - * |
117 | | - * @since 0.1 |
118 | | - * |
119 | | - * @param array $args |
120 | | - * |
121 | | - * @return SurveyQuestion |
122 | | - */ |
123 | | - public static function newFromArray( array $args ) { |
124 | | - return new self( |
125 | | - array_key_exists( 'id', $args ) ? $args['id'] : null, |
126 | | - array_key_exists( 'surveyId', $args ) ? $args['surveyId'] : null, |
127 | | - $args['text'], |
128 | | - $args['type'], |
129 | | - $args['required'], |
130 | | - $args['answers'], |
131 | | - $args['removed'] |
132 | | - ); |
133 | | - } |
134 | | - |
135 | | - /** |
136 | 82 | * Serialization method for survey questions that need to be passed via multi-value API parameter. |
137 | 83 | * Uses base64 and replaces padding = by !, so the values does not contain any = or |. |
138 | 84 | * |
— | — | @@ -144,31 +90,6 @@ |
145 | 91 | } |
146 | 92 | |
147 | 93 | /** |
148 | | - * Serializes the survey question to an associative array which |
149 | | - * can then easily be converted into JSON or similar. |
150 | | - * |
151 | | - * @since 0.1 |
152 | | - * |
153 | | - * @return array |
154 | | - */ |
155 | | - public function toArray() { |
156 | | - $args = array( |
157 | | - 'surveyId' => $this->surveyId, |
158 | | - 'text' => $this->text, |
159 | | - 'type' => $this->type, |
160 | | - 'required' => $this->required, |
161 | | - 'answers' => $this->answers, |
162 | | - 'removed' => $this->removed, |
163 | | - ); |
164 | | - |
165 | | - if ( !is_null( $this->id ) ) { |
166 | | - $args['id'] = $this->id; |
167 | | - } |
168 | | - |
169 | | - return $args; |
170 | | - } |
171 | | - |
172 | | - /** |
173 | 94 | * Returns the questions for the specified survey. |
174 | 95 | * |
175 | 96 | * @since 0.1 |
— | — | @@ -179,148 +100,24 @@ |
180 | 101 | * @return array of SurveyQuestion |
181 | 102 | */ |
182 | 103 | public static function getQuestionsForSurvey( $surveyId, $incRemoved = false ) { |
183 | | - $conditions = array( 'question_survey_id' => $surveyId ); |
| 104 | + $conditions = array( 'survey_id' => $surveyId ); |
184 | 105 | |
185 | 106 | if ( $incRemoved === false ) { |
186 | | - $conditions['question_removed'] = 0; |
| 107 | + $conditions['removed'] = 0; |
187 | 108 | } |
188 | 109 | |
189 | | - return self::getQuestionsFromDB( $conditions ); |
| 110 | + return self::select( null, $conditions ); |
190 | 111 | } |
191 | 112 | |
192 | 113 | /** |
193 | | - * Returns the questions matching the specified conditions. |
194 | | - * |
195 | | - * @since 0.1 |
196 | | - * |
197 | | - * @param array $conditions |
198 | | - * |
199 | | - * @return array of SurveyQuestion |
200 | | - */ |
201 | | - public static function getQuestionsFromDB( array $conditions ) { |
202 | | - $dbr = wfgetDB( DB_SLAVE ); |
203 | | - |
204 | | - $questions = $dbr->select( |
205 | | - 'survey_questions', |
206 | | - array( |
207 | | - 'question_id', |
208 | | - 'question_survey_id', |
209 | | - 'question_text', |
210 | | - 'question_type', |
211 | | - 'question_required', |
212 | | - 'question_answers', |
213 | | - ), |
214 | | - $conditions |
215 | | - ); |
216 | | - |
217 | | - if ( !$questions ) { |
218 | | - return array(); |
219 | | - } |
220 | | - |
221 | | - $sQuestions = array(); |
222 | | - |
223 | | - foreach ( $questions as $question ) { |
224 | | - $sQuestions[] = new SurveyQuestion( |
225 | | - $question->question_id, |
226 | | - $question->question_survey_id, |
227 | | - $question->question_text, |
228 | | - $question->question_type, |
229 | | - $question->question_required, |
230 | | - unserialize( $question->question_answers ) |
231 | | - ); |
232 | | - } |
233 | | - |
234 | | - return $sQuestions; |
235 | | - } |
236 | | - |
237 | | - /** |
238 | 114 | * @see SurveyDBClass::getDBTable() |
239 | 115 | */ |
240 | | - protected function getDBTable() { |
| 116 | + protected static function getDBTable() { |
241 | 117 | return 'survey_questions'; |
242 | 118 | } |
243 | 119 | |
244 | | - /** |
245 | | - * @see SurveyDBClass::getIDField() |
246 | | - */ |
247 | | - protected function getIDField() { |
248 | | - return 'question_id'; |
| 120 | + protected static function getFieldPrefix() { |
| 121 | + return 'question_'; |
249 | 122 | } |
250 | 123 | |
251 | | - /** |
252 | | - * Gets the fields => values to write to the survey_questions table. |
253 | | - * |
254 | | - * @since 0.1 |
255 | | - * |
256 | | - * @param integer $surveyId |
257 | | - * |
258 | | - * @return array |
259 | | - */ |
260 | | - protected function getWriteValues() { |
261 | | - return array( |
262 | | - 'question_survey_id' => $this->surveyId, |
263 | | - 'question_text' => $this->text, |
264 | | - 'question_type' => $this->type, |
265 | | - 'question_required' => $this->required, |
266 | | - 'question_answers' => serialize( $this->answers ), |
267 | | - ); |
268 | | - } |
269 | | - |
270 | | - /** |
271 | | - * Gets the question text. |
272 | | - * |
273 | | - * @since 0.1 |
274 | | - * |
275 | | - * @return string |
276 | | - */ |
277 | | - public function getText() { |
278 | | - return $this->text; |
279 | | - } |
280 | | - |
281 | | - /** |
282 | | - * Gets the questions type. |
283 | | - * |
284 | | - * @since 0.1 |
285 | | - * |
286 | | - * @return integer |
287 | | - */ |
288 | | - public function getType() { |
289 | | - return $this->type; |
290 | | - } |
291 | | - |
292 | | - /** |
293 | | - * Gets if the question is required. |
294 | | - * |
295 | | - * @since 0.1 |
296 | | - * |
297 | | - * @return boolean |
298 | | - */ |
299 | | - public function isRequired() { |
300 | | - return $this->required; |
301 | | - } |
302 | | - |
303 | | - /** |
304 | | - * |
305 | | - * |
306 | | - * @since 0.1 |
307 | | - * |
308 | | - * @param integer|null $id |
309 | | - */ |
310 | | - public function setSurveyId( $id ) { |
311 | | - $this->surveyId = $id; |
312 | | - } |
313 | | - |
314 | | - /** |
315 | | - * Gets if the question was removed. |
316 | | - * This means it should not be shown in the UI, |
317 | | - * and is only kept to make sense of old answers liked to it. |
318 | | - * |
319 | | - * @since 0.1 |
320 | | - * |
321 | | - * @return boolean |
322 | | - */ |
323 | | - public function wasRemoved() { |
324 | | - return $this->removed; |
325 | | - } |
326 | | - |
327 | 124 | } |
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php |
— | — | @@ -32,7 +32,7 @@ |
33 | 33 | } |
34 | 34 | |
35 | 35 | if ( isset( $params['name'] ) ) { |
36 | | - $survey = Survey::newFromName( $params['name'], false ); |
| 36 | + $survey = Survey::newFromName( $params['name'], null, false ); |
37 | 37 | |
38 | 38 | if ( $survey === false ) { |
39 | 39 | $this->dieUsage( wfMsgExt( 'survey-err-survey-name-unknown', 'parsemag', $params['name'] ), 'survey-name-unknown' ); |
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | |
43 | 43 | $fields = array_merge( array( 'id' ), $params['props'] ); |
44 | 44 | |
45 | | - $this->addFields( $this->getSurveyFields( $fields ) ); |
| 45 | + $this->addFields( Survey::getPrefixedFields( $fields ) ); |
46 | 46 | |
47 | 47 | if ( isset( $params['ids'] ) ) { |
48 | 48 | $this->addWhere( array( 'survey_id' => $params['ids'] ) ); |
— | — | @@ -73,12 +73,8 @@ |
74 | 74 | break; |
75 | 75 | } |
76 | 76 | |
77 | | - $surveyObject = new Survey( $survey->survey_id ); |
| 77 | + $surveyObject = Survey::newFromDBResult( $survey ); |
78 | 78 | |
79 | | - foreach ( $fields as $prop ) { |
80 | | - $surveyObject->setProp( $prop, $survey->{ 'survey_' . $prop } ); |
81 | | - } |
82 | | - |
83 | 79 | if ( $params['incquestions'] ) { |
84 | 80 | $surveyObject->loadQuestionsFromDB(); |
85 | 81 | } |
— | — | @@ -95,19 +91,15 @@ |
96 | 92 | ); |
97 | 93 | } |
98 | 94 | |
99 | | - protected function getSurveyFields( array $props ) { |
100 | | - $fields = array(); |
101 | | - $allowedFields = Survey::getSurveyProps(); |
102 | | - |
103 | | - foreach ( $props as $prop ) { |
104 | | - if ( in_array( $prop, $allowedFields ) ) { |
105 | | - $fields[] = "survey_$prop"; |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - return $fields; |
110 | | - } |
111 | | - |
| 95 | + /** |
| 96 | + * |
| 97 | + * |
| 98 | + * @since 0.1 |
| 99 | + * |
| 100 | + * @param array $survey |
| 101 | + * |
| 102 | + * @return $survey |
| 103 | + */ |
112 | 104 | protected function getSurveyData( array $survey ) { |
113 | 105 | foreach ( $survey['questions'] as $nr => $question ) { |
114 | 106 | $this->getResult()->setIndexedTagName( $survey['questions'][$nr], 'answer' ); |
— | — | @@ -133,7 +125,7 @@ |
134 | 126 | ApiBase::PARAM_ISMULTI => true, |
135 | 127 | ), |
136 | 128 | 'props' => array( |
137 | | - ApiBase::PARAM_TYPE => Survey::getSurveyProps(), |
| 129 | + ApiBase::PARAM_TYPE => Survey::getFieldNames(), |
138 | 130 | ApiBase::PARAM_ISMULTI => true, |
139 | 131 | ApiBase::PARAM_DFLT => 'id|name|enabled' |
140 | 132 | ), |