r95374 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95373‎ | r95374 | r95375 >
Date:05:07, 24 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey questions
Modified paths:
  • /trunk/extensions/Survey/api/ApiQuerySurveys.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -70,7 +70,7 @@
7171 * @return Survey or false
7272 */
7373 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 );
7575 }
7676
7777 /**
@@ -257,4 +257,30 @@
258258 return $this->questions;
259259 }
260260
 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+
261287 }
\ No newline at end of file
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -13,6 +13,11 @@
1414 */
1515 class SurveyQuestion {
1616
 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+
1722 /**
1823 * The ID of the question DB record, or null if not inserted yet.
1924 *
@@ -98,7 +103,10 @@
99104 */
100105 public static function newFromUrlData( $args ) {
101106 $args = (array)FormatJson::decode( base64_decode( str_replace( '!', '=', $args ) ) );
102 -
 107+ return self::newFromArray( $args );
 108+ }
 109+
 110+ public static function newFromArray( array $args ) {
103111 return new self(
104112 array_key_exists( 'id', $args ) ? $args['id'] : null,
105113 $args['text'],
@@ -118,6 +126,10 @@
119127 * @return string
120128 */
121129 public function toUrlData() {
 130+ return str_replace( '=', '!', base64_encode( FormatJson::encode( $this->toArray() ) ) );
 131+ }
 132+
 133+ public function toArray() {
122134 $args = array(
123135 'text' => $this->text,
124136 'type' => $this->type,
@@ -130,7 +142,7 @@
131143 $args['id'] = $this->id;
132144 }
133145
134 - return str_replace( '=', '!', base64_encode( FormatJson::encode( $args ) ) );
 146+ return $args;
135147 }
136148
137149 /**
@@ -142,7 +154,7 @@
143155 *
144156 * @return array of SurveyQuestion
145157 */
146 - protected static function getQuestionsForSurvey( $surveyId ) {
 158+ public static function getQuestionsForSurvey( $surveyId ) {
147159 return self::getQuestionsFromDB( array( 'question_survey_id' => $surveyId ) );
148160 }
149161
@@ -155,7 +167,7 @@
156168 *
157169 * @return array of SurveyQuestion
158170 */
159 - protected static function getQuestionsFromDB( array $conditions ) {
 171+ public static function getQuestionsFromDB( array $conditions ) {
160172 $dbr = wfgetDB( DB_SLAVE );
161173
162174 $questions = $dbr->select(
@@ -174,8 +186,10 @@
175187 return array();
176188 }
177189
178 - foreach ( $questions as &$question ) {
179 - $question = new SurveyQuestion(
 190+ $sQuestions = array();
 191+
 192+ foreach ( $questions as $question ) {
 193+ $sQuestions[] = new SurveyQuestion(
180194 $question->question_id,
181195 $question->question_text,
182196 $question->question_type,
@@ -184,7 +198,7 @@
185199 );
186200 }
187201
188 - return $questions;
 202+ return $sQuestions;
189203 }
190204
191205 /**
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php
@@ -31,6 +31,21 @@
3232 // Get the requests parameters.
3333 $params = $this->extractRequestParams();
3434
 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+ );
3550 }
3651
3752 /**
@@ -39,9 +54,15 @@
4055 */
4156 public function getAllowedParams() {
4257 return array (
43 - 'surveyids' => array(
 58+ 'ids' => array(
4459 ApiBase::PARAM_TYPE => 'integer',
 60+ ApiBase::PARAM_REQUIRED => true,
 61+ ApiBase::PARAM_ISMULTI => true,
4562 ),
 63+ 'incquestions' => array(
 64+ ApiBase::PARAM_TYPE => 'integer',
 65+ ApiBase::PARAM_DFLT => '0',
 66+ ),
4667 'limit' => array(
4768 ApiBase :: PARAM_DFLT => 20,
4869 ApiBase :: PARAM_TYPE => 'limit',
@@ -60,7 +81,8 @@
6182 */
6283 public function getParamDescription() {
6384 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',
6587 'continue' => 'Offset number from where to continue the query',
6688 'limit' => 'Max amount of words to return',
6789 );
@@ -71,7 +93,7 @@
7294 * @see includes/api/ApiBase#getDescription()
7395 */
7496 public function getDescription() {
75 - return '';
 97+ return 'API module for obatining surveys and optionaly their questions';
7698 }
7799
78100 /**
@@ -80,7 +102,7 @@
81103 */
82104 public function getPossibleErrors() {
83105 return array_merge( parent::getPossibleErrors(), array(
84 -
 106+ array( 'surveyids', 'name' ),
85107 ) );
86108 }
87109

Sign-offs

UserFlagDate
Nikerabbitinspected06:00, 24 August 2011

Status & tagging log