http://www.mediawiki.org/wiki/Special:Code/MediaWiki/96001

Revision: 96001
Author:   jeroendedauw
Date:     2011-09-01 15:45:30 +0000 (Thu, 01 Sep 2011)
Log Message:
-----------
work on survey admin

Modified Paths:
--------------
    trunk/extensions/Survey/Survey.i18n.php
    trunk/extensions/Survey/Survey.php
    trunk/extensions/Survey/includes/Survey.class.php
    trunk/extensions/Survey/includes/SurveyQuestion.php
    trunk/extensions/Survey/resources/ext.survey.special.survey.js
    trunk/extensions/Survey/specials/SpecialSurvey.php

Modified: trunk/extensions/Survey/Survey.i18n.php
===================================================================
--- trunk/extensions/Survey/Survey.i18n.php     2011-09-01 15:31:41 UTC (rev 
96000)
+++ trunk/extensions/Survey/Survey.i18n.php     2011-09-01 15:45:30 UTC (rev 
96001)
@@ -69,4 +69,6 @@
        'survey-special-label-required' => 'Question is required',
        'survey-special-label-type' => 'Question type',
        'survey-special-label-text' => 'Question text',
+       'survey-special-remove' => 'Remove question',
+       'survey-special-remove-confirm' => 'Are you sure you want to remove 
this question?',
 );

Modified: trunk/extensions/Survey/Survey.php
===================================================================
--- trunk/extensions/Survey/Survey.php  2011-09-01 15:31:41 UTC (rev 96000)
+++ trunk/extensions/Survey/Survey.php  2011-09-01 15:45:30 UTC (rev 96001)
@@ -122,7 +122,8 @@
        ),
        'dependencies' => array( 'ext.survey' ),
        'messages' => array(
-               'surveys-special-confirm-delete'
+               'surveys-special-confirm-delete',
+               'surveys-special-delete-failed',
        )
 );
 
@@ -145,6 +146,8 @@
                'survey-special-label-text',
                'survey-special-label-addquestion',
                'survey-special-label-button',
+               'survey-special-remove',
+               'survey-special-remove-confirm',
        )
 );
 

Modified: trunk/extensions/Survey/includes/Survey.class.php
===================================================================
--- trunk/extensions/Survey/includes/Survey.class.php   2011-09-01 15:31:41 UTC 
(rev 96000)
+++ trunk/extensions/Survey/includes/Survey.class.php   2011-09-01 15:45:30 UTC 
(rev 96001)
@@ -150,7 +150,7 @@
                $this->name = $name;
                $this->enabled = $enabled;
                
-               $this->questions = $questions;
+               $this->setQuestions( $questions );
        }
        
        /**
@@ -178,6 +178,7 @@
                $dbw->begin();
                
                foreach ( $this->questions as /* SurveyQuestion */ $question ) {
+                       $question->setSurveyId( $this->getId() );
                        $success = $question->writeToDB() && $success;
                }
                
@@ -199,6 +200,39 @@
        }
        
        /**
+        * Sets the survey name.
+        * 
+        * @since 0.1
+        * 
+        * @param string $name
+        */
+       public function setName( $name ) {
+               $this->name = $name;
+       }
+       
+       /**
+        * Returns if the survey is enabled.
+        * 
+        * @since 0.1
+        * 
+        * @return boolean
+        */
+       public function isEnabled() {
+               return $this->enabled;
+       }
+       
+       /**
+        * Sets if the survey is enabled or not.
+        * 
+        * @since 0.1
+        * 
+        * @param boolean $enabled
+        */
+       public function setEnabled( $enabled ) {
+               $this->enabled = $enabled;
+       }
+       
+       /**
         * Returns the surveys questions.
         * 
         * @since 0.1
@@ -210,6 +244,17 @@
        }
        
        /**
+        * Sets the surveys questions.
+        * 
+        * @since 0.1
+        * 
+        * @param array $questions list of SurveyQuestion
+        */
+       public function setQuestions( array /* of SurveyQuestion */ $questions 
) {
+               $this->questions = $questions;
+       }
+       
+       /**
         * Serializes the survey to an associative array which
         * can then easily be converted into JSON or similar.
         * 

Modified: trunk/extensions/Survey/includes/SurveyQuestion.php
===================================================================
--- trunk/extensions/Survey/includes/SurveyQuestion.php 2011-09-01 15:31:41 UTC 
(rev 96000)
+++ trunk/extensions/Survey/includes/SurveyQuestion.php 2011-09-01 15:45:30 UTC 
(rev 96001)
@@ -20,9 +20,11 @@
        
        /**
         * The ID of the survey this question belongs to.
+        * This can be null. When written to the db via Survey::writeToDB of
+        * a Survey holding this question, the survey ID will first be set.
         * 
         * @since 0.1
-        * @var integer
+        * @var integer|null
         */
        protected $surveyId;
        
@@ -298,6 +300,17 @@
        }
        
        /**
+        * 
+        * 
+        * @since 0.1
+        * 
+        * @param integer|null $id
+        */
+       public function setSurveyId( $id ) {
+               $this->surveyId = $id;
+       }
+       
+       /**
         * Gets if the question was removed.
         * This means it should not be shown in the UI,
         * and is only kept to make sense of old answers liked to it. 

Modified: trunk/extensions/Survey/resources/ext.survey.special.survey.js
===================================================================
--- trunk/extensions/Survey/resources/ext.survey.special.survey.js      
2011-09-01 15:31:41 UTC (rev 96000)
+++ trunk/extensions/Survey/resources/ext.survey.special.survey.js      
2011-09-01 15:45:30 UTC (rev 96001)
@@ -54,13 +54,27 @@
                
                $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html(
                        getQuestionInput( question )
+                               .append( '<br />' )
+                               .append( $( '<button />' ).button()
+                                       .text( mw.msg( 'survey-special-remove' 
) )
+                                       .click( function() { 
+                                               if ( confirm( mw.msg( 
'survey-special-remove-confirm' ) ) ) {
+                                                       removeQuestion( 
question );
+                                               }
+                                               
+                                               return false;
+                                       } )
+                               ) 
                ) );
                
                $table.append( $tr );
        };
        
        function getQuestionInput( question ) {
-               var $input = $( '<div />' ).attr( { 'border': '1px solid 
black', 'id': 'survey-question-div-' + question.id } );
+               var $input = $( '<div />' ).attr( {
+                       'border': '1px solid black',
+                       'id': 'survey-question-div-' + question.id
+               } );
                
                $input.append( $( '<label />' ).attr( {
                        'for': 'survey-question-text-' + question.id
@@ -71,7 +85,8 @@
                $input.append( $( '<textarea />' ).attr( {
                        'rows': 2,
                        'cols': 80,
-                       'id': 'survey-question-text-' + question.id
+                       'id': 'survey-question-text-' + question.id,
+                       'name': 'survey-question-text-' + question.id
                } ).val( question.text ) );
                
                $input.append( '<br />' );
@@ -81,11 +96,13 @@
                } ).text( mw.msg( 'survey-special-label-type' ) ) );
 
                $input.append( survey.htmlSelect( questionTypes, question.type, 
{
-                       'id': 'survey-question-type-' + question.id
+                       'id': 'survey-question-type-' + question.id,
+                       'name': 'survey-question-type-' + question.id
                } ) );
                
                $required = $( '<input />' ).attr( {
                        'id': 'survey-question-required-' + question.id,
+                       'name': 'survey-question-required-' + question.id,
                        'type': 'checkbox',
                } ).text( mw.msg( 'survey-special-label-type' ) );
                
@@ -103,7 +120,7 @@
        };
        
        function removeQuestion( question ) {
-               $( 'survey-question-div-' + question.id ).slideUp( 'fast', 
function() { $( this ).remove(); } )
+               $( '#survey-question-div-' + question.id ).closest( 'tr' 
).slideUp( 'fast', function() { $( this ).remove(); } )
        };
        
        function onAddQuestionRequest() {

Modified: trunk/extensions/Survey/specials/SpecialSurvey.php
===================================================================
--- trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-01 15:31:41 UTC 
(rev 96000)
+++ trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-01 15:45:30 UTC 
(rev 96001)
@@ -32,18 +32,117 @@
        public function execute( $subPage ) {
                parent::execute( $subPage );
                
-               $survey = Survey::newFromName( $subPage, true );
+               global $wgRequest, $wgUser;
                
-               if ( $survey === false ) {
-                       $this->showNameError();
+               if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( 
$wgRequest->getVal( 'wpEditToken' ) ) ) {
+                       $this->handleSubmission();
                }
                else {
-                       $this->showSurvey( $survey );
-                       $this->addModules( 'ext.survey.special.survey' );
+                       if ( is_null( $subPage ) ) {
+                               $survey = new Survey( null );
+                       }
+                       else {
+                               $survey = Survey::newFromName( $subPage, true );
+                       }
+                       
+                       if ( $survey === false ) {
+                               $this->showNameError();
+                       }
+                       else {
+                               $this->showSurvey( $survey );
+                               $this->addModules( 'ext.survey.special.survey' 
);
+                       }
                }
        }
        
        /**
+        * Handle submission of a survey.
+        * This conists of finding the posted survey data, constructing the
+        * corresponding objects, writing these to the db and then redirecting
+        * the user back to the surveys list.
+        * 
+        * @since 0.1
+        */
+       protected function handleSubmission() {
+               global $wgRequest;
+               
+               $values = $wgRequest->getValues();
+               
+               if ( $wgRequest->getInt( 'survey-id' ) == 0 ) {
+                       $survey = new Survey( null );
+               }
+               else {
+                       $survey = Survey::newFromId( $wgRequest->getInt( 
'survey-id' ), false );
+               }
+               
+               $survey->setName( $wgRequest->getText( 'survey-name' ) );
+               $survey->setEnabled( $wgRequest->getCheck( 'survey-enabled' ) );
+               
+               $survey->setQuestions( $this->getSubmittedQuestions() );
+               
+               $survey->writeToDB();
+               
+               $this->getOutput()->redirect( SpecialPage::getTitleFor( 
'Surveys' )->getLocalURL() );
+       }
+       
+       /**
+        * Gets a list of submitted surveys.
+        * 
+        * @return array of SurveyQuestion
+        */
+       protected function getSubmittedQuestions() {
+               $questions = array();
+               
+//             foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value 
) {
+//                     
+//             }
+               
+               foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value 
) {
+                       $matches = array();
+                       
+                       if ( preg_match( '/survey-question-text-(\d)+/', $name, 
$matches ) ) {
+                               $questions[] = $this->getSubmittedQuestion( 
$matches[1] );
+                       }
+                       else if ( preg_match( 
'/survey-question-text-new-(\d)+/', $name, $matches ) ) {
+                               $questions[] = $this->getSubmittedQuestion( 
$matches[1], true );
+                       }
+               }
+               
+               return $questions;
+       }
+       
+       /**
+        * Create a 
+        * 
+        * @since 0.1
+        * 
+        * @param integer|null $questionId
+        * 
+        * @return SurveyQuestion
+        */
+       protected function getSubmittedQuestion( $questionId, $isNewQuestion = 
false ) {
+               global $wgRequest;
+               
+               if ( $isNewQuestion ) {
+                       $questionDbId = null;
+                       $questionId = "new-$questionId";
+               }
+               else {
+                       $questionId = $questionId;
+               }
+               
+               $question = new SurveyQuestion(
+                       $questionId,
+                       0,
+                       $wgRequest->getText( "survey-question-text-$questionId" 
),
+                       $wgRequest->getInt( "survey-question-type-$questionId" 
),
+                       $wgRequest->getCheck( 
"survey-question-required-$questionDbId" )
+               );
+               
+               return $question;
+       }
+       
+       /**
         * Show error when requesting a non-existing survey.
         * 
         * @since 0.1
@@ -78,6 +177,7 @@
                        'label-message' => 'survey-special-label-name',
                        'required' => true,
                        'id' => 'survey-name',
+                       'name' => 'survey-name',
                );
                
                $fields[] = array(
@@ -87,6 +187,7 @@
                        'label-message' => 'survey-special-label-enabled',
                        'required' => true,
                        'id' => 'survey-enabled',
+                       'name' => 'survey-enabled',
                );
                
                foreach ( $survey->getQuestions() as /* SurveyQuestion */ 
$question ) {


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to