r95373 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95372‎ | r95373 | r95374 >
Date:04:37, 24 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey questions
Modified paths:
  • /trunk/extensions/Survey/Survey.php (modified) (history)
  • /trunk/extensions/Survey/Survey.sql (modified) (history)
  • /trunk/extensions/Survey/api/ApiAddSurvey.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiEditSurvey.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/Survey.php
@@ -54,6 +54,7 @@
5555 $wgAutoloadClasses['ApiSubmitSurvey'] = dirname( __FILE__ ) . '/api/ApiSubmitSurvey.php';
5656
5757 $wgAutoloadClasses['Survey'] = dirname( __FILE__ ) . '/includes/Survey.class.php';
 58+$wgAutoloadClasses['SurveyQuestion'] = dirname( __FILE__ ) . '/includes/SurveyQuestion.php';
5859
5960 $wgAutoloadClasses['SpecialSurveys'] = dirname( __FILE__ ) . '/specials/SpecialSurveys.php';
6061 $wgAutoloadClasses['SpecialSurveyStats'] = dirname( __FILE__ ) . '/specials/SpecialSurveyStats.php';
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -102,18 +102,19 @@
103103 return false;
104104 }
105105
106 - $questions = $loadQuestions ? self::getQuestionsFromDB( $dbr, $survey->survey_id ) : array();
107 -
108 - return new self(
 106+ $survey = new self(
109107 $survey->survey_id,
110108 $survey->survey_name,
111 - $survey->survey_enabled,
112 - $questions
 109+ $survey->survey_enabled
113110 );
 111+
 112+ if ( $loadQuestions ) {
 113+ $survey->loadQuestionsFromDB();
 114+ }
 115+
 116+ return $survey;
114117 }
115118
116 -
117 -
118119 /**
119120 * Constructor.
120121 *
@@ -124,7 +125,7 @@
125126 * @param boolean $enabled
126127 * @param array $questions
127128 */
128 - public function __construct( $id, $name, $enabled, array $questions ) {
 129+ public function __construct( $id, $name, $enabled, array $questions = array() ) {
129130 $this->id = $id;
130131 $this->name = $name;
131132 $this->enabled = $enabled;
@@ -133,6 +134,15 @@
134135 }
135136
136137 /**
 138+ * Load the surveys questions from the database.
 139+ *
 140+ * @since 0.1
 141+ */
 142+ public function loadQuestionsFromDB() {
 143+ $this->questions = SurveyQuestion::getQuestionsForSurvey( $this->id );
 144+ }
 145+
 146+ /**
137147 * Writes the survey to the database, either updating it
138148 * when it already exists, or inserting it when it doesn't.
139149 *
@@ -142,11 +152,23 @@
143153 */
144154 public function writeToDB() {
145155 if ( is_null( $this->id ) ) {
146 - return $this->insertIntoDB();
 156+ $success = $this->insertIntoDB();
147157 }
148158 else {
149 - return $this->updateInDB();
 159+ $success = $this->updateInDB();
150160 }
 161+
 162+ $dbw = wfGetDB( DB_MASTER );
 163+
 164+ $dbw->begin();
 165+
 166+ foreach ( $this->questions as /* SurveyQuestion */ $question ) {
 167+ $success = $question->writeToDB( $this->id ) && $success;
 168+ }
 169+
 170+ $dbw->commit();
 171+
 172+ return $success;
151173 }
152174
153175 /**
@@ -157,13 +179,15 @@
158180 * @return boolean Success indicator
159181 */
160182 protected function updateInDB() {
161 - $dbr = wfGetDB( DB_MASTER );
 183+ $dbw = wfGetDB( DB_MASTER );
162184
163 - return $dbr->update(
 185+ $success = $dbw->update(
164186 'surveys',
165187 $this->getWriteValues(),
166188 array( 'survey_id' => $this->id )
167189 );
 190+
 191+ return $success;
168192 }
169193
170194 /**
@@ -174,16 +198,16 @@
175199 * @return boolean Success indicator
176200 */
177201 protected function insertIntoDB() {
178 - $dbr = wfGetDB( DB_MASTER );
 202+ $dbw = wfGetDB( DB_MASTER );
179203
180 - $result = $dbr->insert(
 204+ $success = $dbw->insert(
181205 'surveys',
182206 $this->getWriteValues()
183207 );
184208
185 - $this->id = $dbr->insertId();
 209+ $this->id = $dbw->insertId();
186210
187 - return $result;
 211+ return $success;
188212 }
189213
190214 /**
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -56,6 +56,16 @@
5757 protected $answers;
5858
5959 /**
 60+ * Indicated if the question was removed.
 61+ * Removed questions are kept in the db so their answers can
 62+ * still be used untill the survey itself gets removed.
 63+ *
 64+ * @since 0.1
 65+ * @var boolean
 66+ */
 67+ protected $removed;
 68+
 69+ /**
6070 * Constructor.
6171 *
6272 * @since 0.1
@@ -65,16 +75,65 @@
6676 * @param integer $type
6777 * @param boolean $required
6878 * @param array $answers
 79+ * @param boolean $removed
6980 */
70 - public function __construct( $id, $text, $type, $required, array $answers = array() ) {
 81+ public function __construct( $id, $text, $type, $required, array $answers = array(), $removed = false ) {
7182 $this->id = $id;
7283 $this->text = $text;
7384 $this->type = $type;
7485 $this->required = $required;
7586 $this->answers = $answers;
 87+ $this->removed = $removed;
7688 }
7789
7890 /**
 91+ * Unserialization method for survey question data passed as a multi-value API parameter.
 92+ * Uses base64 and replaces padding = by !, so the values does not contain any = or |.
 93+ *
 94+ * @since 0.1
 95+ *
 96+ * @param string $args
 97+ *
 98+ * @return SurveyQuestion
 99+ */
 100+ public static function newFromUrlData( $args ) {
 101+ $args = (array)FormatJson::decode( base64_decode( str_replace( '!', '=', $args ) ) );
 102+
 103+ return new self(
 104+ array_key_exists( 'id', $args ) ? $args['id'] : null,
 105+ $args['text'],
 106+ $args['type'],
 107+ $args['required'],
 108+ $args['answers'],
 109+ $args['removed']
 110+ );
 111+ }
 112+
 113+ /**
 114+ * Serialization method for survey questions that need to be passed via multi-value API parameter.
 115+ * Uses base64 and replaces padding = by !, so the values does not contain any = or |.
 116+ *
 117+ * @since 0.1
 118+ *
 119+ * @return string
 120+ */
 121+ public function toUrlData() {
 122+ $args = array(
 123+ 'text' => $this->text,
 124+ 'type' => $this->type,
 125+ 'required' => $this->required,
 126+ 'answers' => $this->answers,
 127+ 'removed' => $this->removed,
 128+ );
 129+
 130+ if ( !is_null( $this->id ) ) {
 131+ $args['id'] = $this->id;
 132+ }
 133+
 134+ return str_replace( '=', '!', base64_encode( FormatJson::encode( $args ) ) );
 135+ }
 136+
 137+ /**
79138 * Returns the questions for the specified survey.
80139 *
81140 * @since 0.1
@@ -134,14 +193,16 @@
135194 *
136195 * @since 0.1
137196 *
 197+ * @param integer $surveyId
 198+ *
138199 * @return boolean Success indicator
139200 */
140 - public function writeToDB() {
 201+ public function writeToDB( $surveyId ) {
141202 if ( is_null( $this->id ) ) {
142 - return $this->insertIntoDB();
 203+ return $this->insertIntoDB( $surveyId );
143204 }
144205 else {
145 - return $this->updateInDB();
 206+ return $this->updateInDB( $surveyId );
146207 }
147208 }
148209
@@ -150,14 +211,16 @@
151212 *
152213 * @since 0.1
153214 *
 215+ * @param integer $surveyId
 216+ *
154217 * @return boolean Success indicator
155218 */
156 - protected function updateInDB() {
 219+ protected function updateInDB( $surveyId ) {
157220 $dbr = wfGetDB( DB_MASTER );
158221
159222 return $dbr->update(
160223 'survey_questions',
161 - $this->getWriteValues(),
 224+ $this->getWriteValues( $surveyId ),
162225 array( 'question_id' => $this->id )
163226 );
164227 }
@@ -167,14 +230,16 @@
168231 *
169232 * @since 0.1
170233 *
 234+ * @param integer $surveyId
 235+ *
171236 * @return boolean Success indicator
172237 */
173 - protected function insertIntoDB() {
 238+ protected function insertIntoDB( $surveyId ) {
174239 $dbr = wfGetDB( DB_MASTER );
175240
176241 $result = $dbr->insert(
177242 'survey_questions',
178 - $this->getWriteValues()
 243+ $this->getWriteValues( $surveyId )
179244 );
180245
181246 $this->id = $dbr->insertId();
@@ -187,10 +252,13 @@
188253 *
189254 * @since 0.1
190255 *
 256+ * @param integer $surveyId
 257+ *
191258 * @return array
192259 */
193 - protected function getWriteValues() {
 260+ protected function getWriteValues( $surveyId ) {
194261 return array(
 262+ 'question_survey_id' => $surveyId,
195263 'question_text' => $this->text,
196264 'question_type' => $this->type,
197265 'question_required' => $this->required,
Index: trunk/extensions/Survey/api/ApiEditSurvey.php
@@ -27,26 +27,83 @@
2828
2929 $params = $this->extractRequestParams();
3030
 31+// $q = new SurveyQuestion(null, 'new q', 1, false);
 32+// var_dump($q->toUrlData());exit;
 33+ // eyJ0ZXh0IjoibmV3IHEiLCJ0eXBlIjoxLCJyZXF1aXJlZCI6ZmFsc2UsImFuc3dlcnMiOltdfQ!!
 34+
 35+ foreach ( $params['questions'] as &$question ) {
 36+ $question = SurveyQuestion::newFromUrlData( $question );
 37+ }
 38+
 39+ $survey = new Survey(
 40+ $params['id'],
 41+ $params['name'],
 42+ $params['enabled'] == 1,
 43+ $params['questions']
 44+ );
 45+
 46+ $this->getResult()->addValue(
 47+ null,
 48+ 'success',
 49+ $survey->writeToDB()
 50+ );
 51+
 52+ $this->getResult()->addValue(
 53+ 'survey',
 54+ 'id',
 55+ $survey->getId()
 56+ );
 57+
 58+ $this->getResult()->addValue(
 59+ 'survey',
 60+ 'name',
 61+ $survey->getName()
 62+ );
3163 }
3264
3365 public function getAllowedParams() {
3466 return array(
 67+ 'id' => array(
 68+ ApiBase::PARAM_TYPE => 'integer',
 69+ ApiBase::PARAM_REQUIRED => true,
 70+ ),
 71+ 'name' => array(
 72+ ApiBase::PARAM_TYPE => 'string',
 73+ ApiBase::PARAM_REQUIRED => true,
 74+ ),
 75+ 'enabled' => array(
 76+ ApiBase::PARAM_TYPE => 'integer',
 77+ ApiBase::PARAM_REQUIRED => true,
 78+ ),
 79+ 'questions' => array(
 80+ ApiBase::PARAM_TYPE => 'string',
 81+ ApiBase::PARAM_ISMULTI => true,
 82+ ApiBase::PARAM_REQUIRED => true,
 83+ ),
3584 );
3685 }
3786
3887 public function getParamDescription() {
3988 return array(
 89+ 'id' => 'The ID of the survey to modify',
 90+ 'name' => 'The name of the survey',
 91+ 'enabled' => 'Enable the survey or not',
 92+ 'questions' => 'The questions that make up the survey',
4093 );
4194 }
4295
4396 public function getDescription() {
4497 return array(
45 - ''
 98+ 'API module for editing a survey.'
4699 );
47100 }
48101
49102 public function getPossibleErrors() {
50103 return array_merge( parent::getPossibleErrors(), array(
 104+ array( 'missingparam', 'id' ),
 105+ array( 'missingparam', 'name' ),
 106+ array( 'missingparam', 'enabled' ),
 107+ array( 'missingparam', 'questions' ),
51108 ) );
52109 }
53110
Index: trunk/extensions/Survey/api/ApiAddSurvey.php
@@ -27,11 +27,15 @@
2828
2929 $params = $this->extractRequestParams();
3030
 31+ foreach ( $params['questions'] as &$question ) {
 32+ $question = SurveyQuestion::newFromUrlData( $question );
 33+ }
 34+
3135 $survey = new Survey(
3236 null,
3337 $params['name'],
3438 $params['enabled'] == 1,
35 - $params['questions'] // TODO
 39+ $params['questions']
3640 );
3741
3842 $this->getResult()->addValue(
Index: trunk/extensions/Survey/Survey.sql
@@ -16,7 +16,8 @@
1717 question_text TEXT NOT NULL,
1818 question_type INT(2) unsigned NOT NULL,
1919 question_required INT(2) unsigned NOT NULL,
20 - question_answers BLOB NOT NULL
 20+ question_answers BLOB NOT NULL,
 21+ question_removed TINYINT NOT NULL default '0'
2122 ) /*$wgDBTableOptions*/;
2223
2324 -- Submissions

Sign-offs

UserFlagDate
Nikerabbitinspected05:59, 24 August 2011

Status & tagging log