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

Revision: 96674
Author:   jeroendedauw
Date:     2011-09-09 17:59:57 +0000 (Fri, 09 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/SurveyQuestion.php
    trunk/extensions/Survey/resources/ext.survey.js
    trunk/extensions/Survey/resources/ext.survey.special.survey.js
    trunk/extensions/Survey/resources/jquery.survey.js
    trunk/extensions/Survey/specials/SpecialSurvey.php

Modified: trunk/extensions/Survey/Survey.i18n.php
===================================================================
--- trunk/extensions/Survey/Survey.i18n.php     2011-09-09 17:34:47 UTC (rev 
96673)
+++ trunk/extensions/Survey/Survey.i18n.php     2011-09-09 17:59:57 UTC (rev 
96674)
@@ -83,6 +83,7 @@
        'survey-special-label-header' => 'Text to display above the survey',
        'survey-special-label-footer' => 'Text to display below the survey',
        'survey-special-label-thanks' => 'Thanks message to display after 
submission of the survey',
+       'survey-special-label-answers' => 'Available answers, one per line.',
 
        // Survey jQuery
        'survey-jquery-submit' => 'Submit',

Modified: trunk/extensions/Survey/Survey.php
===================================================================
--- trunk/extensions/Survey/Survey.php  2011-09-09 17:34:47 UTC (rev 96673)
+++ trunk/extensions/Survey/Survey.php  2011-09-09 17:59:57 UTC (rev 96674)
@@ -149,6 +149,7 @@
                'survey-special-label-button',
                'survey-special-remove',
                'survey-special-remove-confirm',
+               'survey-special-label-answers'
        )
 );
 

Modified: trunk/extensions/Survey/includes/SurveyQuestion.php
===================================================================
--- trunk/extensions/Survey/includes/SurveyQuestion.php 2011-09-09 17:34:47 UTC 
(rev 96673)
+++ trunk/extensions/Survey/includes/SurveyQuestion.php 2011-09-09 17:59:57 UTC 
(rev 96674)
@@ -83,6 +83,24 @@
        }
        
        /**
+        * Returns a list of default field values.
+        * field name => field value
+        * 
+        * @since 0.1
+        * 
+        * @return array
+        */
+       public static function getDefaults() {
+               return array(
+                       'text' => '',
+                       'type' => self::$TYPE_TEXT,
+                       'required' => false,
+                       'answers' => array(),
+                       'removed' => false,
+               );
+       }
+       
+       /**
         * Unserialization method for survey question data passed as a 
multi-value API parameter.
         * Uses base64 and replaces padding = by !, so the values does not 
contain any = or |. 
         * 

Modified: trunk/extensions/Survey/resources/ext.survey.js
===================================================================
--- trunk/extensions/Survey/resources/ext.survey.js     2011-09-09 17:34:47 UTC 
(rev 96673)
+++ trunk/extensions/Survey/resources/ext.survey.js     2011-09-09 17:59:57 UTC 
(rev 96674)
@@ -34,7 +34,7 @@
                }
        };
        
-       this.htmlSelect = function( options, value, attributes ) {
+       this.htmlSelect = function( options, value, attributes, 
onChangeCallback ) {
                $select = $( '<select />' ).attr( attributes );
                
                for ( message in options ) {
@@ -47,9 +47,40 @@
                        $select.append( $( '<option />' ).text( message ).attr( 
attribs ) );
                }
                
+               if ( typeof onChangeCallback !== 'undefined' ) {
+                       $select.change( function() { onChangeCallback( $( this 
).val() ) } );
+               }
+               
                return $select;
        };
        
+       this.htmlRadio = function( options, value, name, attributes ) {
+               var $radio = $( '<div />' ).attr( attributes );
+               $radio.html( '' );
+               
+               for ( message in options ) {
+                       var value = options[message];
+                       var id = name + value;
+                       
+                       $input = $( '<input />' ).attr( {
+                               'id': id,
+                               'type': 'radio',
+                               'name': name,
+                               'value': value
+                       } );
+                       
+                       if ( value === options[message] ) {
+                               $input.attr( 'checked', 'checked' );
+                       }
+                       
+                       $radio.append( $input );
+                       $radio.append( $( '<label />' ).attr( 'for', id ).text( 
message ) );
+                       $radio.append( $( '<br />' ) );
+               }
+               
+               return $radio;
+       };
+       
        this.question = new( function() {
                
                this.type = new( function() {
@@ -61,7 +92,11 @@
                        this.CHECK = 5;
                } );
                
-               this.getTypeSelector = function( value, attributes ) {
+               this.typeHasAnswers = function( t ) {
+                       return $.inArray( t, [ survey.question.type.RADIO, 
survey.question.type.SELECT ] ) !== -1;
+               };
+               
+               this.getTypeSelector = function( value, attributes, 
onChangeCallback ) {
                        var options = [];
                        
                        var types = {
@@ -77,7 +112,7 @@
                                options[survey.msg( 'survey-question-type-' + 
msg )] = types[msg];
                        }
                        
-                       return survey.htmlSelect( options, parseInt( value ), 
attributes );
+                       return survey.htmlSelect( options, parseInt( value ), 
attributes, onChangeCallback );
                };
                
        } );

Modified: trunk/extensions/Survey/resources/ext.survey.special.survey.js
===================================================================
--- trunk/extensions/Survey/resources/ext.survey.special.survey.js      
2011-09-09 17:34:47 UTC (rev 96673)
+++ trunk/extensions/Survey/resources/ext.survey.special.survey.js      
2011-09-09 17:59:57 UTC (rev 96674)
@@ -5,8 +5,57 @@
  * @licence GNU GPL v3 or later
  * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
  */
-(function( $, mw ) { $( document ).ready( function() {
 
+(function( $, mw, survey ) {
+       
+       survey.answerSelector = function( options ) {
+               var _this = this;
+               
+               var defaults = {
+                       'visible': true,
+                       'answers': []
+               };
+               
+               options = $.extend( defaults, options );
+               
+               this.$div = $( '<div />' ).html( '' );
+               
+               this.$div.append( $( '<p />' ).text( mw.msg( 
'survey-special-label-answers' ) ) );
+
+               this.$div.append( $( '<textarea />' ).attr( options.attr ).val( 
options.answers.join( '\n' ) ) );
+               
+               this.setVisible( options.visible );
+       };
+
+       survey.answerSelector.prototype = {
+               
+               getHtml: function() {
+                       return this.$div;
+               },
+                       
+               show: function() {
+                       this.$div.show();
+               },
+               
+               hide: function() {
+                       this.$div.hide();
+               },
+               
+               setVisible: function( visible ) {
+                       if ( visible ) {
+                               this.show();
+                       }
+                       else {
+                               this.hide();
+                       }
+               }
+                       
+       };
+       
+} )( jQuery, window.mediaWiki, window.survey );
+
+(function( $, mw, survey ) { $( document ).ready( function() {
+
        var _this = this;
 
        var $table = null;
@@ -25,7 +74,7 @@
                ) );
                
                $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html(
-                       getQuestionInput( { 'id': 'new' } )
+                       getQuestionInput( { 'id': 'new', 'answers': [], 'type': 
0 } )
                ).append( $( '<button />' ).button( { 'label': mw.msg( 
'survey-special-label-button' ) } )
                        .click( function() { onAddQuestionRequest(); return 
false; } )
                ) );
@@ -69,6 +118,17 @@
                        'border': '1px solid black',
                        'id': 'survey-question-div-' + question.id
                } );
+
+               var answerSelector = new survey.answerSelector( {
+                       'visible': survey.question.typeHasAnswers( parseInt( 
question.type ) ),
+                       'attr': {
+                               'rows': 2,
+                               'cols': 80,
+                               'id': 'survey-question-answers-' + question.id,
+                               'name': 'survey-question-answers-' + question.id
+                       },
+                       'answers': question.answers
+               } );
                
                $input.append( $( '<label />' ).attr( {
                        'for': 'survey-question-text-' + question.id
@@ -89,10 +149,16 @@
                        'for': 'survey-question-type-' + question.id
                } ).text( mw.msg( 'survey-special-label-type' ) ) );
 
-               $input.append( survey.question.getTypeSelector( question.type, {
-                       'id': 'survey-question-type-' + question.id,
-                       'name': 'survey-question-type-' + question.id
-               } ) );
+               $input.append( survey.question.getTypeSelector(
+                       question.type,
+                       {
+                               'id': 'survey-question-type-' + question.id,
+                               'name': 'survey-question-type-' + question.id
+                       },
+                       function( newValue ) {
+                               answerSelector.setVisible( 
survey.question.typeHasAnswers( parseInt( newValue ) ) );
+                       }
+               ) );
                
                $required = $( '<input />' ).attr( {
                        'id': 'survey-question-required-' + question.id,
@@ -110,6 +176,8 @@
                        'for': 'survey-question-required-' + question.id
                } ).text( mw.msg( 'survey-special-label-required' ) ) );
                
+               $input.append( answerSelector.getHtml() );
+               
                return $input;
        };
        
@@ -122,7 +190,8 @@
                        'text': $( '#survey-question-text-new' ).val(),
                        'required': !!$( '#survey-question-required-new' 
).attr( 'checked' ),
                        'type': $( '#survey-question-type-new' ).val(),
-                       'id': 'new-' + newQuestionNr++
+                       'id': 'new-' + newQuestionNr++,
+                       'answers': $( '#survey-question-answers-new' 
).val().split( '\n' )
                } );
                $( '#survey-question-text-new' ).focus().select();
                $( 'html' ).animate( { scrollTop: $( document ).height() }, 
'fast' );
@@ -146,6 +215,7 @@
                                'required': $this.attr( 'data-required' ) == 
'1',
                                'id': $this.attr( 'data-id' ),
                                'type': $this.attr( 'data-type' ),
+                               'answers': eval( $this.attr( 'data-answers' ) ),
                        } );
                } );
                
@@ -156,4 +226,4 @@
        
        setup();
        
-} ); })( jQuery, window.mediaWiki );
\ No newline at end of file
+} ); })( jQuery, window.mediaWiki, window.survey );
\ No newline at end of file

Modified: trunk/extensions/Survey/resources/jquery.survey.js
===================================================================
--- trunk/extensions/Survey/resources/jquery.survey.js  2011-09-09 17:34:47 UTC 
(rev 96673)
+++ trunk/extensions/Survey/resources/jquery.survey.js  2011-09-09 17:59:57 UTC 
(rev 96674)
@@ -49,61 +49,83 @@
                var type = survey.question.type;
                
                var $input;
+               var id = 'survey-question-' + question.id;
                
                switch ( question.type ) {
                        case type.TEXT: default:
                                $input = $( '<input />' ).attr( {
-                                       'id': 'survey-question-' + question.id,
+                                       'id': id,
                                        'class': 'survey-question survey-text'
                                } );
                                break;
                        case type.NUMBER:
                                $input = $( '<input />' ).numeric().attr( {
-                                       'id': 'survey-question-' + question.id,
+                                       'id': id,
                                        'class': 'survey-question 
survey-number',
                                        'size': 7
                                } );
                                break;
                        case type.SELECT:
                                $input = survey.htmlSelect( question.answers, 
0, { 
-                                       'id': 'survey-question-' + question.id,
+                                       'id': id,
                                        'class': 'survey-question survey-select'
                                } );
                                break;
                        case type.RADIO:
-                               // TODO
-                               $input = $( '<input />' ).attr( {
-                                       'id': 'survey-question-' + question.id,
-                                       'class': 'survey-question'
-                               } );
+                               $input = survey.htmlRadio(
+                                       question.answers,
+                                       0,
+                                       id,
+                                       {
+                                               'id': id,
+                                               'class': 'survey-question 
survey-radio'
+                                       }
+                               );
                                break;
                        case type.TEXTAREA:
                                $input = $( '<textarea />' ).attr( {
-                                       'id': 'survey-question-' + question.id,
+                                       'id': id,
                                        'class': 'survey-question 
survey-textarea',
                                        'cols': 80,
                                        'rows': 2
                                } );
                                break;
+                       case type.CHECK:
+                               $input = $( '<input />' ).attr( {
+                                       'id': id,
+                                       'type': 'checkbox',
+                                       'class': 'survey-question survey-check',
+                               } );
+                               break;
                }
                
                $input.data( 'question-id', question.id );
                
                this.inputs.push( $input );
                
-               return $input;
-       };
-       
-       this.getSurveyQuestion = function( question ) {
-               $q = $( '<div />' );
+               $q = $( '<div />' ).html( $input );
                
-               $q.append( $( '<p />' ).text( question.text ) );
+               if ( question.type == type.CHECK ) {
+                       $q.append( $( '<label />' ).text( question.text ).attr( 
'for', id ) );
+               }
+               else {
+                       $q.prepend( $( '<p />' ).text( question.text ) );
+               }
                
-               $q.append( this.getQuestionInput( question ) )
-               
                return $q;
        };
        
+       this.getSurveyQuestion = function( question ) {
+               if ( survey.question.typeHasAnswers( question.type )
+                       && question.answers.length == 0 ) {
+                       survey.log( 'getSurveyQuestion: no answers for: ' + 
question.id );
+                       return '';
+               }
+               else {
+                       return this.getQuestionInput( question );
+               }
+       };
+       
        this.getSurveyQuestions = function( questions ) {
                $questions = $( '<div />' );
                

Modified: trunk/extensions/Survey/specials/SpecialSurvey.php
===================================================================
--- trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-09 17:34:47 UTC 
(rev 96673)
+++ trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-09 17:59:57 UTC 
(rev 96674)
@@ -126,12 +126,20 @@
                        $questionDbId = $questionId;
                }
                
+               $answers = array_filter(
+                       explode( "\n", $wgRequest->getText( 
"survey-question-answers-$questionId" ) ),
+                       function( $line ) {
+                               return trim( $line ) != '';
+                       }
+               );
+               
                $question = new SurveyQuestion( array(
                        'id' => $questionDbId,
                        'removed' => 0,
                        'text' => $wgRequest->getText( 
"survey-question-text-$questionId" ),
                        'type' => $wgRequest->getInt( 
"survey-question-type-$questionId" ),
-                       'required' => $wgRequest->getCheck( 
"survey-question-required-$questionId" )
+                       'required' => $wgRequest->getCheck( 
"survey-question-required-$questionId" ),
+                       'answers' => $answers
                ) );
                
                return $question;
@@ -224,12 +232,7 @@
                foreach ( $survey->getQuestions() as /* SurveyQuestion */ 
$question ) {
                        $fields[] = array(
                                'class' => 'SurveyQuestionField',
-                               'options' => array(
-                                       'required' => $question->getField( 
'required' ),
-                                       'text' => $question->getField( 'text' ),
-                                       'type' => $question->getField( 'type' ),
-                                       'id' => $question->getId(),
-                               )
+                               'options' => $question->toArray()
                        );
                }
                


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

Reply via email to