Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -150,7 +150,7 @@ |
151 | 151 | $this->name = $name; |
152 | 152 | $this->enabled = $enabled; |
153 | 153 | |
154 | | - $this->questions = $questions; |
| 154 | + $this->setQuestions( $questions ); |
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
— | — | @@ -178,6 +178,7 @@ |
179 | 179 | $dbw->begin(); |
180 | 180 | |
181 | 181 | foreach ( $this->questions as /* SurveyQuestion */ $question ) { |
| 182 | + $question->setSurveyId( $this->getId() ); |
182 | 183 | $success = $question->writeToDB() && $success; |
183 | 184 | } |
184 | 185 | |
— | — | @@ -199,6 +200,39 @@ |
200 | 201 | } |
201 | 202 | |
202 | 203 | /** |
| 204 | + * Sets the survey name. |
| 205 | + * |
| 206 | + * @since 0.1 |
| 207 | + * |
| 208 | + * @param string $name |
| 209 | + */ |
| 210 | + public function setName( $name ) { |
| 211 | + $this->name = $name; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns if the survey is enabled. |
| 216 | + * |
| 217 | + * @since 0.1 |
| 218 | + * |
| 219 | + * @return boolean |
| 220 | + */ |
| 221 | + public function isEnabled() { |
| 222 | + return $this->enabled; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Sets if the survey is enabled or not. |
| 227 | + * |
| 228 | + * @since 0.1 |
| 229 | + * |
| 230 | + * @param boolean $enabled |
| 231 | + */ |
| 232 | + public function setEnabled( $enabled ) { |
| 233 | + $this->enabled = $enabled; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
203 | 237 | * Returns the surveys questions. |
204 | 238 | * |
205 | 239 | * @since 0.1 |
— | — | @@ -210,6 +244,17 @@ |
211 | 245 | } |
212 | 246 | |
213 | 247 | /** |
| 248 | + * Sets the surveys questions. |
| 249 | + * |
| 250 | + * @since 0.1 |
| 251 | + * |
| 252 | + * @param array $questions list of SurveyQuestion |
| 253 | + */ |
| 254 | + public function setQuestions( array /* of SurveyQuestion */ $questions ) { |
| 255 | + $this->questions = $questions; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
214 | 259 | * Serializes the survey to an associative array which |
215 | 260 | * can then easily be converted into JSON or similar. |
216 | 261 | * |
Index: trunk/extensions/Survey/includes/SurveyQuestion.php |
— | — | @@ -20,9 +20,11 @@ |
21 | 21 | |
22 | 22 | /** |
23 | 23 | * The ID of the survey this question belongs to. |
| 24 | + * This can be null. When written to the db via Survey::writeToDB of |
| 25 | + * a Survey holding this question, the survey ID will first be set. |
24 | 26 | * |
25 | 27 | * @since 0.1 |
26 | | - * @var integer |
| 28 | + * @var integer|null |
27 | 29 | */ |
28 | 30 | protected $surveyId; |
29 | 31 | |
— | — | @@ -298,6 +300,17 @@ |
299 | 301 | } |
300 | 302 | |
301 | 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 | + /** |
302 | 315 | * Gets if the question was removed. |
303 | 316 | * This means it should not be shown in the UI, |
304 | 317 | * and is only kept to make sense of old answers liked to it. |
Index: trunk/extensions/Survey/resources/ext.survey.special.survey.js |
— | — | @@ -54,13 +54,27 @@ |
55 | 55 | |
56 | 56 | $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html( |
57 | 57 | getQuestionInput( question ) |
| 58 | + .append( '<br />' ) |
| 59 | + .append( $( '<button />' ).button() |
| 60 | + .text( mw.msg( 'survey-special-remove' ) ) |
| 61 | + .click( function() { |
| 62 | + if ( confirm( mw.msg( 'survey-special-remove-confirm' ) ) ) { |
| 63 | + removeQuestion( question ); |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | + } ) |
| 68 | + ) |
58 | 69 | ) ); |
59 | 70 | |
60 | 71 | $table.append( $tr ); |
61 | 72 | }; |
62 | 73 | |
63 | 74 | function getQuestionInput( question ) { |
64 | | - var $input = $( '<div />' ).attr( { 'border': '1px solid black', 'id': 'survey-question-div-' + question.id } ); |
| 75 | + var $input = $( '<div />' ).attr( { |
| 76 | + 'border': '1px solid black', |
| 77 | + 'id': 'survey-question-div-' + question.id |
| 78 | + } ); |
65 | 79 | |
66 | 80 | $input.append( $( '<label />' ).attr( { |
67 | 81 | 'for': 'survey-question-text-' + question.id |
— | — | @@ -71,7 +85,8 @@ |
72 | 86 | $input.append( $( '<textarea />' ).attr( { |
73 | 87 | 'rows': 2, |
74 | 88 | 'cols': 80, |
75 | | - 'id': 'survey-question-text-' + question.id |
| 89 | + 'id': 'survey-question-text-' + question.id, |
| 90 | + 'name': 'survey-question-text-' + question.id |
76 | 91 | } ).val( question.text ) ); |
77 | 92 | |
78 | 93 | $input.append( '<br />' ); |
— | — | @@ -81,11 +96,13 @@ |
82 | 97 | } ).text( mw.msg( 'survey-special-label-type' ) ) ); |
83 | 98 | |
84 | 99 | $input.append( survey.htmlSelect( questionTypes, question.type, { |
85 | | - 'id': 'survey-question-type-' + question.id |
| 100 | + 'id': 'survey-question-type-' + question.id, |
| 101 | + 'name': 'survey-question-type-' + question.id |
86 | 102 | } ) ); |
87 | 103 | |
88 | 104 | $required = $( '<input />' ).attr( { |
89 | 105 | 'id': 'survey-question-required-' + question.id, |
| 106 | + 'name': 'survey-question-required-' + question.id, |
90 | 107 | 'type': 'checkbox', |
91 | 108 | } ).text( mw.msg( 'survey-special-label-type' ) ); |
92 | 109 | |
— | — | @@ -103,7 +120,7 @@ |
104 | 121 | }; |
105 | 122 | |
106 | 123 | function removeQuestion( question ) { |
107 | | - $( 'survey-question-div-' + question.id ).slideUp( 'fast', function() { $( this ).remove(); } ) |
| 124 | + $( '#survey-question-div-' + question.id ).closest( 'tr' ).slideUp( 'fast', function() { $( this ).remove(); } ) |
108 | 125 | }; |
109 | 126 | |
110 | 127 | function onAddQuestionRequest() { |
Index: trunk/extensions/Survey/Survey.php |
— | — | @@ -122,7 +122,8 @@ |
123 | 123 | ), |
124 | 124 | 'dependencies' => array( 'ext.survey' ), |
125 | 125 | 'messages' => array( |
126 | | - 'surveys-special-confirm-delete' |
| 126 | + 'surveys-special-confirm-delete', |
| 127 | + 'surveys-special-delete-failed', |
127 | 128 | ) |
128 | 129 | ); |
129 | 130 | |
— | — | @@ -145,6 +146,8 @@ |
146 | 147 | 'survey-special-label-text', |
147 | 148 | 'survey-special-label-addquestion', |
148 | 149 | 'survey-special-label-button', |
| 150 | + 'survey-special-remove', |
| 151 | + 'survey-special-remove-confirm', |
149 | 152 | ) |
150 | 153 | ); |
151 | 154 | |
Index: trunk/extensions/Survey/Survey.i18n.php |
— | — | @@ -69,4 +69,6 @@ |
70 | 70 | 'survey-special-label-required' => 'Question is required', |
71 | 71 | 'survey-special-label-type' => 'Question type', |
72 | 72 | 'survey-special-label-text' => 'Question text', |
| 73 | + 'survey-special-remove' => 'Remove question', |
| 74 | + 'survey-special-remove-confirm' => 'Are you sure you want to remove this question?', |
73 | 75 | ); |
Index: trunk/extensions/Survey/specials/SpecialSurvey.php |
— | — | @@ -32,18 +32,117 @@ |
33 | 33 | public function execute( $subPage ) { |
34 | 34 | parent::execute( $subPage ); |
35 | 35 | |
36 | | - $survey = Survey::newFromName( $subPage, true ); |
| 36 | + global $wgRequest, $wgUser; |
37 | 37 | |
38 | | - if ( $survey === false ) { |
39 | | - $this->showNameError(); |
| 38 | + if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) { |
| 39 | + $this->handleSubmission(); |
40 | 40 | } |
41 | 41 | else { |
42 | | - $this->showSurvey( $survey ); |
43 | | - $this->addModules( 'ext.survey.special.survey' ); |
| 42 | + if ( is_null( $subPage ) ) { |
| 43 | + $survey = new Survey( null ); |
| 44 | + } |
| 45 | + else { |
| 46 | + $survey = Survey::newFromName( $subPage, true ); |
| 47 | + } |
| 48 | + |
| 49 | + if ( $survey === false ) { |
| 50 | + $this->showNameError(); |
| 51 | + } |
| 52 | + else { |
| 53 | + $this->showSurvey( $survey ); |
| 54 | + $this->addModules( 'ext.survey.special.survey' ); |
| 55 | + } |
44 | 56 | } |
45 | 57 | } |
46 | 58 | |
47 | 59 | /** |
| 60 | + * Handle submission of a survey. |
| 61 | + * This conists of finding the posted survey data, constructing the |
| 62 | + * corresponding objects, writing these to the db and then redirecting |
| 63 | + * the user back to the surveys list. |
| 64 | + * |
| 65 | + * @since 0.1 |
| 66 | + */ |
| 67 | + protected function handleSubmission() { |
| 68 | + global $wgRequest; |
| 69 | + |
| 70 | + $values = $wgRequest->getValues(); |
| 71 | + |
| 72 | + if ( $wgRequest->getInt( 'survey-id' ) == 0 ) { |
| 73 | + $survey = new Survey( null ); |
| 74 | + } |
| 75 | + else { |
| 76 | + $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), false ); |
| 77 | + } |
| 78 | + |
| 79 | + $survey->setName( $wgRequest->getText( 'survey-name' ) ); |
| 80 | + $survey->setEnabled( $wgRequest->getCheck( 'survey-enabled' ) ); |
| 81 | + |
| 82 | + $survey->setQuestions( $this->getSubmittedQuestions() ); |
| 83 | + |
| 84 | + $survey->writeToDB(); |
| 85 | + |
| 86 | + $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Surveys' )->getLocalURL() ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets a list of submitted surveys. |
| 91 | + * |
| 92 | + * @return array of SurveyQuestion |
| 93 | + */ |
| 94 | + protected function getSubmittedQuestions() { |
| 95 | + $questions = array(); |
| 96 | + |
| 97 | +// foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) { |
| 98 | +// |
| 99 | +// } |
| 100 | + |
| 101 | + foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) { |
| 102 | + $matches = array(); |
| 103 | + |
| 104 | + if ( preg_match( '/survey-question-text-(\d)+/', $name, $matches ) ) { |
| 105 | + $questions[] = $this->getSubmittedQuestion( $matches[1] ); |
| 106 | + } |
| 107 | + else if ( preg_match( '/survey-question-text-new-(\d)+/', $name, $matches ) ) { |
| 108 | + $questions[] = $this->getSubmittedQuestion( $matches[1], true ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return $questions; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Create a |
| 117 | + * |
| 118 | + * @since 0.1 |
| 119 | + * |
| 120 | + * @param integer|null $questionId |
| 121 | + * |
| 122 | + * @return SurveyQuestion |
| 123 | + */ |
| 124 | + protected function getSubmittedQuestion( $questionId, $isNewQuestion = false ) { |
| 125 | + global $wgRequest; |
| 126 | + |
| 127 | + if ( $isNewQuestion ) { |
| 128 | + $questionDbId = null; |
| 129 | + $questionId = "new-$questionId"; |
| 130 | + } |
| 131 | + else { |
| 132 | + $questionId = $questionId; |
| 133 | + } |
| 134 | + |
| 135 | + $question = new SurveyQuestion( |
| 136 | + $questionId, |
| 137 | + 0, |
| 138 | + $wgRequest->getText( "survey-question-text-$questionId" ), |
| 139 | + $wgRequest->getInt( "survey-question-type-$questionId" ), |
| 140 | + $wgRequest->getCheck( "survey-question-required-$questionDbId" ) |
| 141 | + ); |
| 142 | + |
| 143 | + return $question; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
48 | 147 | * Show error when requesting a non-existing survey. |
49 | 148 | * |
50 | 149 | * @since 0.1 |
— | — | @@ -78,6 +177,7 @@ |
79 | 178 | 'label-message' => 'survey-special-label-name', |
80 | 179 | 'required' => true, |
81 | 180 | 'id' => 'survey-name', |
| 181 | + 'name' => 'survey-name', |
82 | 182 | ); |
83 | 183 | |
84 | 184 | $fields[] = array( |
— | — | @@ -87,6 +187,7 @@ |
88 | 188 | 'label-message' => 'survey-special-label-enabled', |
89 | 189 | 'required' => true, |
90 | 190 | 'id' => 'survey-enabled', |
| 191 | + 'name' => 'survey-enabled', |
91 | 192 | ); |
92 | 193 | |
93 | 194 | foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) { |