r96001 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96000‎ | r96001 | r96002 >
Date:15:45, 1 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on survey admin
Modified paths:
  • /trunk/extensions/Survey/Survey.i18n.php (modified) (history)
  • /trunk/extensions/Survey/Survey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.special.survey.js (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -150,7 +150,7 @@
151151 $this->name = $name;
152152 $this->enabled = $enabled;
153153
154 - $this->questions = $questions;
 154+ $this->setQuestions( $questions );
155155 }
156156
157157 /**
@@ -178,6 +178,7 @@
179179 $dbw->begin();
180180
181181 foreach ( $this->questions as /* SurveyQuestion */ $question ) {
 182+ $question->setSurveyId( $this->getId() );
182183 $success = $question->writeToDB() && $success;
183184 }
184185
@@ -199,6 +200,39 @@
200201 }
201202
202203 /**
 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+ /**
203237 * Returns the surveys questions.
204238 *
205239 * @since 0.1
@@ -210,6 +244,17 @@
211245 }
212246
213247 /**
 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+ /**
214259 * Serializes the survey to an associative array which
215260 * can then easily be converted into JSON or similar.
216261 *
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -20,9 +20,11 @@
2121
2222 /**
2323 * 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.
2426 *
2527 * @since 0.1
26 - * @var integer
 28+ * @var integer|null
2729 */
2830 protected $surveyId;
2931
@@ -298,6 +300,17 @@
299301 }
300302
301303 /**
 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+ /**
302315 * Gets if the question was removed.
303316 * This means it should not be shown in the UI,
304317 * 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 @@
5555
5656 $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html(
5757 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+ )
5869 ) );
5970
6071 $table.append( $tr );
6172 };
6273
6374 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+ } );
6579
6680 $input.append( $( '<label />' ).attr( {
6781 'for': 'survey-question-text-' + question.id
@@ -71,7 +85,8 @@
7286 $input.append( $( '<textarea />' ).attr( {
7387 'rows': 2,
7488 'cols': 80,
75 - 'id': 'survey-question-text-' + question.id
 89+ 'id': 'survey-question-text-' + question.id,
 90+ 'name': 'survey-question-text-' + question.id
7691 } ).val( question.text ) );
7792
7893 $input.append( '<br />' );
@@ -81,11 +96,13 @@
8297 } ).text( mw.msg( 'survey-special-label-type' ) ) );
8398
8499 $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
86102 } ) );
87103
88104 $required = $( '<input />' ).attr( {
89105 'id': 'survey-question-required-' + question.id,
 106+ 'name': 'survey-question-required-' + question.id,
90107 'type': 'checkbox',
91108 } ).text( mw.msg( 'survey-special-label-type' ) );
92109
@@ -103,7 +120,7 @@
104121 };
105122
106123 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(); } )
108125 };
109126
110127 function onAddQuestionRequest() {
Index: trunk/extensions/Survey/Survey.php
@@ -122,7 +122,8 @@
123123 ),
124124 'dependencies' => array( 'ext.survey' ),
125125 'messages' => array(
126 - 'surveys-special-confirm-delete'
 126+ 'surveys-special-confirm-delete',
 127+ 'surveys-special-delete-failed',
127128 )
128129 );
129130
@@ -145,6 +146,8 @@
146147 'survey-special-label-text',
147148 'survey-special-label-addquestion',
148149 'survey-special-label-button',
 150+ 'survey-special-remove',
 151+ 'survey-special-remove-confirm',
149152 )
150153 );
151154
Index: trunk/extensions/Survey/Survey.i18n.php
@@ -69,4 +69,6 @@
7070 'survey-special-label-required' => 'Question is required',
7171 'survey-special-label-type' => 'Question type',
7272 '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?',
7375 );
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -32,18 +32,117 @@
3333 public function execute( $subPage ) {
3434 parent::execute( $subPage );
3535
36 - $survey = Survey::newFromName( $subPage, true );
 36+ global $wgRequest, $wgUser;
3737
38 - if ( $survey === false ) {
39 - $this->showNameError();
 38+ if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
 39+ $this->handleSubmission();
4040 }
4141 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+ }
4456 }
4557 }
4658
4759 /**
 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+ /**
48147 * Show error when requesting a non-existing survey.
49148 *
50149 * @since 0.1
@@ -78,6 +177,7 @@
79178 'label-message' => 'survey-special-label-name',
80179 'required' => true,
81180 'id' => 'survey-name',
 181+ 'name' => 'survey-name',
82182 );
83183
84184 $fields[] = array(
@@ -87,6 +187,7 @@
88188 'label-message' => 'survey-special-label-enabled',
89189 'required' => true,
90190 'id' => 'survey-enabled',
 191+ 'name' => 'survey-enabled',
91192 );
92193
93194 foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) {

Status & tagging log