Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -45,10 +45,75 @@ |
46 | 46 | */ |
47 | 47 | protected $questions; |
48 | 48 | |
49 | | - public static function newFromDBResult() { |
| 49 | + /** |
| 50 | + * Returns the Survey with specified name, or false if there is no such survey. |
| 51 | + * |
| 52 | + * @since 0.1 |
| 53 | + * |
| 54 | + * @param string $surveyName |
| 55 | + * @param boolean $loadQuestions |
| 56 | + * |
| 57 | + * @return Survey or false |
| 58 | + */ |
| 59 | + public static function newFromName( $surveyName, $loadQuestions = true ) { |
| 60 | + return self::newFromDB( array( 'survey_name' => $surveyName ), $loadQuestions ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the Survey with specified ID, or false if there is no such survey. |
| 65 | + * |
| 66 | + * @since 0.1 |
| 67 | + * |
| 68 | + * @param integer surveyId |
| 69 | + * @param boolean $loadQuestions |
| 70 | + * |
| 71 | + * @return Survey or false |
| 72 | + */ |
| 73 | + public static function newFromId( $surveyId, $loadQuestions = true ) { |
| 74 | + return self::newFromDB( array( 'survey_id' => surveyId ), $loadQuestions ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns a new instance of Survey build from a database result |
| 79 | + * obtained by doing a select with the porvided conditions on the surveys table. |
| 80 | + * If no survey matches the conditions, false will be returned. |
| 81 | + * |
| 82 | + * @since 0.1 |
| 83 | + * |
| 84 | + * @param array $conditions |
| 85 | + * @param boolean $loadQuestions |
| 86 | + * |
| 87 | + * @return Survey or false |
| 88 | + */ |
| 89 | + protected static function newFromDB( array $conditions, $loadQuestions = true ) { |
| 90 | + $dbr = wfGetDB( DB_SLAVE ); |
50 | 91 | |
| 92 | + $survey = $dbr->selectRow( |
| 93 | + 'surveys', |
| 94 | + array( |
| 95 | + 'survey_id', |
| 96 | + 'survey_name', |
| 97 | + 'survey_enabled', |
| 98 | + ), |
| 99 | + $conditions |
| 100 | + ); |
| 101 | + |
| 102 | + if ( !$survey ) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + $questions = $loadQuestions ? self::getQuestionsFromDB( $dbr, $survey->survey_id ) : array(); |
| 107 | + |
| 108 | + return new self( |
| 109 | + $survey->survey_id, |
| 110 | + $survey->survey_name, |
| 111 | + $survey->survey_enabled, |
| 112 | + $questions |
| 113 | + ); |
51 | 114 | } |
52 | 115 | |
| 116 | + |
| 117 | + |
53 | 118 | /** |
54 | 119 | * Constructor. |
55 | 120 | * |
— | — | @@ -63,6 +128,7 @@ |
64 | 129 | $this->id = $id; |
65 | 130 | $this->name = $name; |
66 | 131 | $this->enabled = $enabled; |
| 132 | + |
67 | 133 | $this->questions = $questions; |
68 | 134 | } |
69 | 135 | |
— | — | @@ -84,7 +150,7 @@ |
85 | 151 | } |
86 | 152 | |
87 | 153 | /** |
88 | | - * Updates the group in the database. |
| 154 | + * Updates the survey in the database. |
89 | 155 | * |
90 | 156 | * @since 0.1 |
91 | 157 | * |
— | — | @@ -101,7 +167,7 @@ |
102 | 168 | } |
103 | 169 | |
104 | 170 | /** |
105 | | - * Inserts the group into the database. |
| 171 | + * Inserts the survey into the database. |
106 | 172 | * |
107 | 173 | * @since 0.1 |
108 | 174 | * |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -13,10 +13,46 @@ |
14 | 14 | */ |
15 | 15 | class SurveyQuestion { |
16 | 16 | |
| 17 | + /** |
| 18 | + * The ID of the question DB record, or null if not inserted yet. |
| 19 | + * |
| 20 | + * @since 0.1 |
| 21 | + * @var integer|null |
| 22 | + */ |
17 | 23 | protected $id; |
| 24 | + |
| 25 | + /** |
| 26 | + * The question text. |
| 27 | + * |
| 28 | + * @since 0.1 |
| 29 | + * @var string |
| 30 | + */ |
18 | 31 | protected $text; |
| 32 | + |
| 33 | + /** |
| 34 | + * The question type. |
| 35 | + * |
| 36 | + * @since 0.1 |
| 37 | + * @var integer |
| 38 | + */ |
19 | 39 | protected $type; |
| 40 | + |
| 41 | + /** |
| 42 | + * Indicated if the question is required, |
| 43 | + * ie if the user can not submit the survey without answering it. |
| 44 | + * |
| 45 | + * @since 0.1 |
| 46 | + * @var boolean |
| 47 | + */ |
20 | 48 | protected $required; |
| 49 | + |
| 50 | + /** |
| 51 | + * List of allowed values for the question. |
| 52 | + * Empty list for no restrictions. |
| 53 | + * |
| 54 | + * @since 0.1 |
| 55 | + * @var array of string|number |
| 56 | + */ |
21 | 57 | protected $answers; |
22 | 58 | |
23 | 59 | /** |
— | — | @@ -31,7 +67,146 @@ |
32 | 68 | * @param array $answers |
33 | 69 | */ |
34 | 70 | public function __construct( $id, $text, $type, $required, array $answers = array() ) { |
| 71 | + $this->id = $id; |
| 72 | + $this->text = $text; |
| 73 | + $this->type = $type; |
| 74 | + $this->required = $required; |
| 75 | + $this->answers = $answers; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the questions for the specified survey. |
| 80 | + * |
| 81 | + * @since 0.1 |
| 82 | + * |
| 83 | + * @param integer $surveyId |
| 84 | + * |
| 85 | + * @return array of SurveyQuestion |
| 86 | + */ |
| 87 | + protected static function getQuestionsForSurvey( $surveyId ) { |
| 88 | + return self::getQuestionsFromDB( array( 'question_survey_id' => $surveyId ) ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the questions matching the specified conditions. |
| 93 | + * |
| 94 | + * @since 0.1 |
| 95 | + * |
| 96 | + * @param array $conditions |
| 97 | + * |
| 98 | + * @return array of SurveyQuestion |
| 99 | + */ |
| 100 | + protected static function getQuestionsFromDB( array $conditions ) { |
| 101 | + $dbr = wfgetDB( DB_SLAVE ); |
35 | 102 | |
| 103 | + $questions = $dbr->select( |
| 104 | + 'survey_questions', |
| 105 | + array( |
| 106 | + 'question_id', |
| 107 | + 'question_text', |
| 108 | + 'question_type', |
| 109 | + 'question_required', |
| 110 | + 'question_answers', |
| 111 | + ), |
| 112 | + $conditions |
| 113 | + ); |
| 114 | + |
| 115 | + if ( !$questions ) { |
| 116 | + return array(); |
| 117 | + } |
| 118 | + |
| 119 | + foreach ( $questions as &$question ) { |
| 120 | + $question = new SurveyQuestion( |
| 121 | + $question->question_id, |
| 122 | + $question->question_text, |
| 123 | + $question->question_type, |
| 124 | + $question->question_required, |
| 125 | + unserialize( $question->question_answers ) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + return $questions; |
36 | 130 | } |
37 | 131 | |
38 | | -} |
\ No newline at end of file |
| 132 | + /** |
| 133 | + * Writes the question to the database, either updating it |
| 134 | + * when it already exists, or inserting it when it doesn't. |
| 135 | + * |
| 136 | + * @since 0.1 |
| 137 | + * |
| 138 | + * @return boolean Success indicator |
| 139 | + */ |
| 140 | + public function writeToDB() { |
| 141 | + if ( is_null( $this->id ) ) { |
| 142 | + return $this->insertIntoDB(); |
| 143 | + } |
| 144 | + else { |
| 145 | + return $this->updateInDB(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Updates the question in the database. |
| 151 | + * |
| 152 | + * @since 0.1 |
| 153 | + * |
| 154 | + * @return boolean Success indicator |
| 155 | + */ |
| 156 | + protected function updateInDB() { |
| 157 | + $dbr = wfGetDB( DB_MASTER ); |
| 158 | + |
| 159 | + return $dbr->update( |
| 160 | + 'survey_questions', |
| 161 | + $this->getWriteValues(), |
| 162 | + array( 'question_id' => $this->id ) |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Inserts the question into the database. |
| 168 | + * |
| 169 | + * @since 0.1 |
| 170 | + * |
| 171 | + * @return boolean Success indicator |
| 172 | + */ |
| 173 | + protected function insertIntoDB() { |
| 174 | + $dbr = wfGetDB( DB_MASTER ); |
| 175 | + |
| 176 | + $result = $dbr->insert( |
| 177 | + 'survey_questions', |
| 178 | + $this->getWriteValues() |
| 179 | + ); |
| 180 | + |
| 181 | + $this->id = $dbr->insertId(); |
| 182 | + |
| 183 | + return $result; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Gets the fields => values to write to the survey_questions table. |
| 188 | + * |
| 189 | + * @since 0.1 |
| 190 | + * |
| 191 | + * @return array |
| 192 | + */ |
| 193 | + protected function getWriteValues() { |
| 194 | + return array( |
| 195 | + 'question_text' => $this->text, |
| 196 | + 'question_type' => $this->type, |
| 197 | + 'question_required' => $this->required, |
| 198 | + 'question_answers' => serialize( $this->answers ), |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Returns the question database id. |
| 204 | + * |
| 205 | + * @since 0.1 |
| 206 | + * |
| 207 | + * @return integer|null |
| 208 | + */ |
| 209 | + public function getId() { |
| 210 | + return $this->id; |
| 211 | + } |
| 212 | + |
| 213 | +} |
Index: trunk/extensions/Survey/api/ApiAddSurvey.php |
— | — | @@ -31,7 +31,7 @@ |
32 | 32 | null, |
33 | 33 | $params['name'], |
34 | 34 | $params['enabled'] == 1, |
35 | | - $params['questions'] |
| 35 | + $params['questions'] // TODO |
36 | 36 | ); |
37 | 37 | |
38 | 38 | $this->getResult()->addValue( |