r96674 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96673‎ | r96674 | r96675 >
Date:17:59, 9 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/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.js (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.special.survey.js (modified) (history)
  • /trunk/extensions/Survey/resources/jquery.survey.js (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.php
@@ -149,6 +149,7 @@
150150 'survey-special-label-button',
151151 'survey-special-remove',
152152 'survey-special-remove-confirm',
 153+ 'survey-special-label-answers'
153154 )
154155 );
155156
Index: trunk/extensions/Survey/Survey.i18n.php
@@ -83,6 +83,7 @@
8484 'survey-special-label-header' => 'Text to display above the survey',
8585 'survey-special-label-footer' => 'Text to display below the survey',
8686 'survey-special-label-thanks' => 'Thanks message to display after submission of the survey',
 87+ 'survey-special-label-answers' => 'Available answers, one per line.',
8788
8889 // Survey jQuery
8990 'survey-jquery-submit' => 'Submit',
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -126,12 +126,20 @@
127127 $questionDbId = $questionId;
128128 }
129129
 130+ $answers = array_filter(
 131+ explode( "\n", $wgRequest->getText( "survey-question-answers-$questionId" ) ),
 132+ function( $line ) {
 133+ return trim( $line ) != '';
 134+ }
 135+ );
 136+
130137 $question = new SurveyQuestion( array(
131138 'id' => $questionDbId,
132139 'removed' => 0,
133140 'text' => $wgRequest->getText( "survey-question-text-$questionId" ),
134141 'type' => $wgRequest->getInt( "survey-question-type-$questionId" ),
135 - 'required' => $wgRequest->getCheck( "survey-question-required-$questionId" )
 142+ 'required' => $wgRequest->getCheck( "survey-question-required-$questionId" ),
 143+ 'answers' => $answers
136144 ) );
137145
138146 return $question;
@@ -224,12 +232,7 @@
225233 foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) {
226234 $fields[] = array(
227235 'class' => 'SurveyQuestionField',
228 - 'options' => array(
229 - 'required' => $question->getField( 'required' ),
230 - 'text' => $question->getField( 'text' ),
231 - 'type' => $question->getField( 'type' ),
232 - 'id' => $question->getId(),
233 - )
 236+ 'options' => $question->toArray()
234237 );
235238 }
236239
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -83,6 +83,24 @@
8484 }
8585
8686 /**
 87+ * Returns a list of default field values.
 88+ * field name => field value
 89+ *
 90+ * @since 0.1
 91+ *
 92+ * @return array
 93+ */
 94+ public static function getDefaults() {
 95+ return array(
 96+ 'text' => '',
 97+ 'type' => self::$TYPE_TEXT,
 98+ 'required' => false,
 99+ 'answers' => array(),
 100+ 'removed' => false,
 101+ );
 102+ }
 103+
 104+ /**
87105 * Unserialization method for survey question data passed as a multi-value API parameter.
88106 * Uses base64 and replaces padding = by !, so the values does not contain any = or |.
89107 *
Index: trunk/extensions/Survey/resources/ext.survey.special.survey.js
@@ -5,8 +5,57 @@
66 * @licence GNU GPL v3 or later
77 * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
88 */
9 -(function( $, mw ) { $( document ).ready( function() {
109
 10+(function( $, mw, survey ) {
 11+
 12+ survey.answerSelector = function( options ) {
 13+ var _this = this;
 14+
 15+ var defaults = {
 16+ 'visible': true,
 17+ 'answers': []
 18+ };
 19+
 20+ options = $.extend( defaults, options );
 21+
 22+ this.$div = $( '<div />' ).html( '' );
 23+
 24+ this.$div.append( $( '<p />' ).text( mw.msg( 'survey-special-label-answers' ) ) );
 25+
 26+ this.$div.append( $( '<textarea />' ).attr( options.attr ).val( options.answers.join( '\n' ) ) );
 27+
 28+ this.setVisible( options.visible );
 29+ };
 30+
 31+ survey.answerSelector.prototype = {
 32+
 33+ getHtml: function() {
 34+ return this.$div;
 35+ },
 36+
 37+ show: function() {
 38+ this.$div.show();
 39+ },
 40+
 41+ hide: function() {
 42+ this.$div.hide();
 43+ },
 44+
 45+ setVisible: function( visible ) {
 46+ if ( visible ) {
 47+ this.show();
 48+ }
 49+ else {
 50+ this.hide();
 51+ }
 52+ }
 53+
 54+ };
 55+
 56+} )( jQuery, window.mediaWiki, window.survey );
 57+
 58+(function( $, mw, survey ) { $( document ).ready( function() {
 59+
1160 var _this = this;
1261
1362 var $table = null;
@@ -25,7 +74,7 @@
2675 ) );
2776
2877 $tr.append( $( '<td />' ).attr( { 'class': 'mw-input' } ).html(
29 - getQuestionInput( { 'id': 'new' } )
 78+ getQuestionInput( { 'id': 'new', 'answers': [], 'type': 0 } )
3079 ).append( $( '<button />' ).button( { 'label': mw.msg( 'survey-special-label-button' ) } )
3180 .click( function() { onAddQuestionRequest(); return false; } )
3281 ) );
@@ -69,6 +118,17 @@
70119 'border': '1px solid black',
71120 'id': 'survey-question-div-' + question.id
72121 } );
 122+
 123+ var answerSelector = new survey.answerSelector( {
 124+ 'visible': survey.question.typeHasAnswers( parseInt( question.type ) ),
 125+ 'attr': {
 126+ 'rows': 2,
 127+ 'cols': 80,
 128+ 'id': 'survey-question-answers-' + question.id,
 129+ 'name': 'survey-question-answers-' + question.id
 130+ },
 131+ 'answers': question.answers
 132+ } );
73133
74134 $input.append( $( '<label />' ).attr( {
75135 'for': 'survey-question-text-' + question.id
@@ -89,10 +149,16 @@
90150 'for': 'survey-question-type-' + question.id
91151 } ).text( mw.msg( 'survey-special-label-type' ) ) );
92152
93 - $input.append( survey.question.getTypeSelector( question.type, {
94 - 'id': 'survey-question-type-' + question.id,
95 - 'name': 'survey-question-type-' + question.id
96 - } ) );
 153+ $input.append( survey.question.getTypeSelector(
 154+ question.type,
 155+ {
 156+ 'id': 'survey-question-type-' + question.id,
 157+ 'name': 'survey-question-type-' + question.id
 158+ },
 159+ function( newValue ) {
 160+ answerSelector.setVisible( survey.question.typeHasAnswers( parseInt( newValue ) ) );
 161+ }
 162+ ) );
97163
98164 $required = $( '<input />' ).attr( {
99165 'id': 'survey-question-required-' + question.id,
@@ -110,6 +176,8 @@
111177 'for': 'survey-question-required-' + question.id
112178 } ).text( mw.msg( 'survey-special-label-required' ) ) );
113179
 180+ $input.append( answerSelector.getHtml() );
 181+
114182 return $input;
115183 };
116184
@@ -122,7 +190,8 @@
123191 'text': $( '#survey-question-text-new' ).val(),
124192 'required': !!$( '#survey-question-required-new' ).attr( 'checked' ),
125193 'type': $( '#survey-question-type-new' ).val(),
126 - 'id': 'new-' + newQuestionNr++
 194+ 'id': 'new-' + newQuestionNr++,
 195+ 'answers': $( '#survey-question-answers-new' ).val().split( '\n' )
127196 } );
128197 $( '#survey-question-text-new' ).focus().select();
129198 $( 'html' ).animate( { scrollTop: $( document ).height() }, 'fast' );
@@ -146,6 +215,7 @@
147216 'required': $this.attr( 'data-required' ) == '1',
148217 'id': $this.attr( 'data-id' ),
149218 'type': $this.attr( 'data-type' ),
 219+ 'answers': eval( $this.attr( 'data-answers' ) ),
150220 } );
151221 } );
152222
@@ -156,4 +226,4 @@
157227
158228 setup();
159229
160 -} ); })( jQuery, window.mediaWiki );
\ No newline at end of file
 230+} ); })( jQuery, window.mediaWiki, window.survey );
\ No newline at end of file
Index: trunk/extensions/Survey/resources/jquery.survey.js
@@ -49,61 +49,83 @@
5050 var type = survey.question.type;
5151
5252 var $input;
 53+ var id = 'survey-question-' + question.id;
5354
5455 switch ( question.type ) {
5556 case type.TEXT: default:
5657 $input = $( '<input />' ).attr( {
57 - 'id': 'survey-question-' + question.id,
 58+ 'id': id,
5859 'class': 'survey-question survey-text'
5960 } );
6061 break;
6162 case type.NUMBER:
6263 $input = $( '<input />' ).numeric().attr( {
63 - 'id': 'survey-question-' + question.id,
 64+ 'id': id,
6465 'class': 'survey-question survey-number',
6566 'size': 7
6667 } );
6768 break;
6869 case type.SELECT:
6970 $input = survey.htmlSelect( question.answers, 0, {
70 - 'id': 'survey-question-' + question.id,
 71+ 'id': id,
7172 'class': 'survey-question survey-select'
7273 } );
7374 break;
7475 case type.RADIO:
75 - // TODO
76 - $input = $( '<input />' ).attr( {
77 - 'id': 'survey-question-' + question.id,
78 - 'class': 'survey-question'
79 - } );
 76+ $input = survey.htmlRadio(
 77+ question.answers,
 78+ 0,
 79+ id,
 80+ {
 81+ 'id': id,
 82+ 'class': 'survey-question survey-radio'
 83+ }
 84+ );
8085 break;
8186 case type.TEXTAREA:
8287 $input = $( '<textarea />' ).attr( {
83 - 'id': 'survey-question-' + question.id,
 88+ 'id': id,
8489 'class': 'survey-question survey-textarea',
8590 'cols': 80,
8691 'rows': 2
8792 } );
8893 break;
 94+ case type.CHECK:
 95+ $input = $( '<input />' ).attr( {
 96+ 'id': id,
 97+ 'type': 'checkbox',
 98+ 'class': 'survey-question survey-check',
 99+ } );
 100+ break;
89101 }
90102
91103 $input.data( 'question-id', question.id );
92104
93105 this.inputs.push( $input );
94106
95 - return $input;
96 - };
97 -
98 - this.getSurveyQuestion = function( question ) {
99 - $q = $( '<div />' );
 107+ $q = $( '<div />' ).html( $input );
100108
101 - $q.append( $( '<p />' ).text( question.text ) );
 109+ if ( question.type == type.CHECK ) {
 110+ $q.append( $( '<label />' ).text( question.text ).attr( 'for', id ) );
 111+ }
 112+ else {
 113+ $q.prepend( $( '<p />' ).text( question.text ) );
 114+ }
102115
103 - $q.append( this.getQuestionInput( question ) )
104 -
105116 return $q;
106117 };
107118
 119+ this.getSurveyQuestion = function( question ) {
 120+ if ( survey.question.typeHasAnswers( question.type )
 121+ && question.answers.length == 0 ) {
 122+ survey.log( 'getSurveyQuestion: no answers for: ' + question.id );
 123+ return '';
 124+ }
 125+ else {
 126+ return this.getQuestionInput( question );
 127+ }
 128+ };
 129+
108130 this.getSurveyQuestions = function( questions ) {
109131 $questions = $( '<div />' );
110132
Index: trunk/extensions/Survey/resources/ext.survey.js
@@ -34,7 +34,7 @@
3535 }
3636 };
3737
38 - this.htmlSelect = function( options, value, attributes ) {
 38+ this.htmlSelect = function( options, value, attributes, onChangeCallback ) {
3939 $select = $( '<select />' ).attr( attributes );
4040
4141 for ( message in options ) {
@@ -47,9 +47,40 @@
4848 $select.append( $( '<option />' ).text( message ).attr( attribs ) );
4949 }
5050
 51+ if ( typeof onChangeCallback !== 'undefined' ) {
 52+ $select.change( function() { onChangeCallback( $( this ).val() ) } );
 53+ }
 54+
5155 return $select;
5256 };
5357
 58+ this.htmlRadio = function( options, value, name, attributes ) {
 59+ var $radio = $( '<div />' ).attr( attributes );
 60+ $radio.html( '' );
 61+
 62+ for ( message in options ) {
 63+ var value = options[message];
 64+ var id = name + value;
 65+
 66+ $input = $( '<input />' ).attr( {
 67+ 'id': id,
 68+ 'type': 'radio',
 69+ 'name': name,
 70+ 'value': value
 71+ } );
 72+
 73+ if ( value === options[message] ) {
 74+ $input.attr( 'checked', 'checked' );
 75+ }
 76+
 77+ $radio.append( $input );
 78+ $radio.append( $( '<label />' ).attr( 'for', id ).text( message ) );
 79+ $radio.append( $( '<br />' ) );
 80+ }
 81+
 82+ return $radio;
 83+ };
 84+
5485 this.question = new( function() {
5586
5687 this.type = new( function() {
@@ -61,7 +92,11 @@
6293 this.CHECK = 5;
6394 } );
6495
65 - this.getTypeSelector = function( value, attributes ) {
 96+ this.typeHasAnswers = function( t ) {
 97+ return $.inArray( t, [ survey.question.type.RADIO, survey.question.type.SELECT ] ) !== -1;
 98+ };
 99+
 100+ this.getTypeSelector = function( value, attributes, onChangeCallback ) {
66101 var options = [];
67102
68103 var types = {
@@ -77,7 +112,7 @@
78113 options[survey.msg( 'survey-question-type-' + msg )] = types[msg];
79114 }
80115
81 - return survey.htmlSelect( options, parseInt( value ), attributes );
 116+ return survey.htmlSelect( options, parseInt( value ), attributes, onChangeCallback );
82117 };
83118
84119 } );

Status & tagging log