Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -70,7 +70,7 @@ |
71 | 71 | * @return Survey or false |
72 | 72 | */ |
73 | 73 | public static function newFromId( $surveyId, $loadQuestions = true ) { |
74 | | - return self::newFromDB( array( 'survey_id' => surveyId ), $loadQuestions ); |
| 74 | + return self::newFromDB( array( 'survey_id' => $surveyId ), $loadQuestions ); |
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
— | — | @@ -257,4 +257,30 @@ |
258 | 258 | return $this->questions; |
259 | 259 | } |
260 | 260 | |
| 261 | + /** |
| 262 | + * Serializes the survey to an associative array which |
| 263 | + * can then easily be converted into JSON or similar. |
| 264 | + * |
| 265 | + * @since 0.1 |
| 266 | + * |
| 267 | + * @return array |
| 268 | + */ |
| 269 | + public function toArray() { |
| 270 | + $data = array( |
| 271 | + 'enabled' => $this->enabled, |
| 272 | + 'name' => $this->name, |
| 273 | + 'questions' => array(), |
| 274 | + ); |
| 275 | + |
| 276 | + foreach ( $this->questions as /* SurveyQuestion */ $question ) { |
| 277 | + $data['questions'][] = $question->toArray(); |
| 278 | + } |
| 279 | + |
| 280 | + if ( !is_null( $this->id ) ) { |
| 281 | + $data['id'] = $this->id; |
| 282 | + } |
| 283 | + |
| 284 | + return $data; |
| 285 | + } |
| 286 | + |
261 | 287 | } |
\ No newline at end of file |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -13,6 +13,11 @@ |
14 | 14 | */ |
15 | 15 | class SurveyQuestion { |
16 | 16 | |
| 17 | + public static $TYPE_TEXT = 0; |
| 18 | + public static $TYPE_NUMBER = 1; |
| 19 | + public static $TYPE_SELECT = 2; |
| 20 | + public static $TYPE_RADIO = 3; |
| 21 | + |
17 | 22 | /** |
18 | 23 | * The ID of the question DB record, or null if not inserted yet. |
19 | 24 | * |
— | — | @@ -98,7 +103,10 @@ |
99 | 104 | */ |
100 | 105 | public static function newFromUrlData( $args ) { |
101 | 106 | $args = (array)FormatJson::decode( base64_decode( str_replace( '!', '=', $args ) ) ); |
102 | | - |
| 107 | + return self::newFromArray( $args ); |
| 108 | + } |
| 109 | + |
| 110 | + public static function newFromArray( array $args ) { |
103 | 111 | return new self( |
104 | 112 | array_key_exists( 'id', $args ) ? $args['id'] : null, |
105 | 113 | $args['text'], |
— | — | @@ -118,6 +126,10 @@ |
119 | 127 | * @return string |
120 | 128 | */ |
121 | 129 | public function toUrlData() { |
| 130 | + return str_replace( '=', '!', base64_encode( FormatJson::encode( $this->toArray() ) ) ); |
| 131 | + } |
| 132 | + |
| 133 | + public function toArray() { |
122 | 134 | $args = array( |
123 | 135 | 'text' => $this->text, |
124 | 136 | 'type' => $this->type, |
— | — | @@ -130,7 +142,7 @@ |
131 | 143 | $args['id'] = $this->id; |
132 | 144 | } |
133 | 145 | |
134 | | - return str_replace( '=', '!', base64_encode( FormatJson::encode( $args ) ) ); |
| 146 | + return $args; |
135 | 147 | } |
136 | 148 | |
137 | 149 | /** |
— | — | @@ -142,7 +154,7 @@ |
143 | 155 | * |
144 | 156 | * @return array of SurveyQuestion |
145 | 157 | */ |
146 | | - protected static function getQuestionsForSurvey( $surveyId ) { |
| 158 | + public static function getQuestionsForSurvey( $surveyId ) { |
147 | 159 | return self::getQuestionsFromDB( array( 'question_survey_id' => $surveyId ) ); |
148 | 160 | } |
149 | 161 | |
— | — | @@ -155,7 +167,7 @@ |
156 | 168 | * |
157 | 169 | * @return array of SurveyQuestion |
158 | 170 | */ |
159 | | - protected static function getQuestionsFromDB( array $conditions ) { |
| 171 | + public static function getQuestionsFromDB( array $conditions ) { |
160 | 172 | $dbr = wfgetDB( DB_SLAVE ); |
161 | 173 | |
162 | 174 | $questions = $dbr->select( |
— | — | @@ -174,8 +186,10 @@ |
175 | 187 | return array(); |
176 | 188 | } |
177 | 189 | |
178 | | - foreach ( $questions as &$question ) { |
179 | | - $question = new SurveyQuestion( |
| 190 | + $sQuestions = array(); |
| 191 | + |
| 192 | + foreach ( $questions as $question ) { |
| 193 | + $sQuestions[] = new SurveyQuestion( |
180 | 194 | $question->question_id, |
181 | 195 | $question->question_text, |
182 | 196 | $question->question_type, |
— | — | @@ -184,7 +198,7 @@ |
185 | 199 | ); |
186 | 200 | } |
187 | 201 | |
188 | | - return $questions; |
| 202 | + return $sQuestions; |
189 | 203 | } |
190 | 204 | |
191 | 205 | /** |
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php |
— | — | @@ -31,6 +31,21 @@ |
32 | 32 | // Get the requests parameters. |
33 | 33 | $params = $this->extractRequestParams(); |
34 | 34 | |
| 35 | + $surveys = array(); |
| 36 | + |
| 37 | + foreach ( $params['ids'] as $surveyId ) { |
| 38 | + $survey = Survey::newFromId( $surveyId, $params['incquestions'] == 1 )->toArray(); |
| 39 | + $this->getResult()->setIndexedTagName( $survey['questions'], 'question' ); |
| 40 | + $surveys[] = $survey; |
| 41 | + } |
| 42 | + |
| 43 | + $this->getResult()->setIndexedTagName( $surveys, 'survey' ); |
| 44 | + |
| 45 | + $this->getResult()->addValue( |
| 46 | + null, |
| 47 | + 'surveys', |
| 48 | + $surveys |
| 49 | + ); |
35 | 50 | } |
36 | 51 | |
37 | 52 | /** |
— | — | @@ -39,9 +54,15 @@ |
40 | 55 | */ |
41 | 56 | public function getAllowedParams() { |
42 | 57 | return array ( |
43 | | - 'surveyids' => array( |
| 58 | + 'ids' => array( |
44 | 59 | ApiBase::PARAM_TYPE => 'integer', |
| 60 | + ApiBase::PARAM_REQUIRED => true, |
| 61 | + ApiBase::PARAM_ISMULTI => true, |
45 | 62 | ), |
| 63 | + 'incquestions' => array( |
| 64 | + ApiBase::PARAM_TYPE => 'integer', |
| 65 | + ApiBase::PARAM_DFLT => '0', |
| 66 | + ), |
46 | 67 | 'limit' => array( |
47 | 68 | ApiBase :: PARAM_DFLT => 20, |
48 | 69 | ApiBase :: PARAM_TYPE => 'limit', |
— | — | @@ -60,7 +81,8 @@ |
61 | 82 | */ |
62 | 83 | public function getParamDescription() { |
63 | 84 | return array ( |
64 | | - 'surveyids' => 'The IDs of the surveys to return', |
| 85 | + 'ids' => 'The IDs of the surveys to return', |
| 86 | + 'incquestions' => 'Include the questions of the surveys or not', |
65 | 87 | 'continue' => 'Offset number from where to continue the query', |
66 | 88 | 'limit' => 'Max amount of words to return', |
67 | 89 | ); |
— | — | @@ -71,7 +93,7 @@ |
72 | 94 | * @see includes/api/ApiBase#getDescription() |
73 | 95 | */ |
74 | 96 | public function getDescription() { |
75 | | - return ''; |
| 97 | + return 'API module for obatining surveys and optionaly their questions'; |
76 | 98 | } |
77 | 99 | |
78 | 100 | /** |
— | — | @@ -80,7 +102,7 @@ |
81 | 103 | */ |
82 | 104 | public function getPossibleErrors() { |
83 | 105 | return array_merge( parent::getPossibleErrors(), array( |
84 | | - |
| 106 | + array( 'surveyids', 'name' ), |
85 | 107 | ) ); |
86 | 108 | } |
87 | 109 | |