r96806 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96805‎ | r96806 | r96807 >
Date:19:52, 11 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
fixed radio inputs and implemented cookies (omnomom)
Modified paths:
  • /trunk/extensions/Survey/Survey.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.tag.js (modified) (history)
  • /trunk/extensions/Survey/resources/jquery.survey.js (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialTakeSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.php
@@ -192,7 +192,7 @@
193193 'scripts' => array(
194194 'ext.survey.tag.js'
195195 ),
196 - 'dependencies' => array( 'ext.survey.jquery' ),
 196+ 'dependencies' => array( 'ext.survey.jquery', 'jquery.cookie' ),
197197 );
198198
199199 unset( $moduleTemplate );
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -83,6 +83,8 @@
8484 $survey->setField( $field, $wgRequest->getInt( 'survey-' . $field ) );
8585 }
8686
 87+ $survey->setField( 'namespaces', array() );
 88+
8789 $survey->setQuestions( $this->getSubmittedQuestions() );
8890
8991 $survey->writeToDB();
@@ -101,9 +103,9 @@
102104 foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) {
103105 $matches = array();
104106
105 - if ( preg_match( '/survey-question-text-(\d)+/', $name, $matches ) ) {
 107+ if ( preg_match( '/survey-question-text-(\d+)/', $name, $matches ) ) {
106108 $questions[] = $this->getSubmittedQuestion( $matches[1] );
107 - } elseif ( preg_match( '/survey-question-text-new-(\d)+/', $name, $matches ) ) {
 109+ } elseif ( preg_match( '/survey-question-text-new-(\d+)/', $name, $matches ) ) {
108110 $questions[] = $this->getSubmittedQuestion( $matches[1], true );
109111 }
110112 }
Index: trunk/extensions/Survey/specials/SpecialTakeSurvey.php
@@ -66,7 +66,8 @@
6767 'survey',
6868 array(
6969 'name' => $subPage,
70 - 'require-enabled' => $GLOBALS['wgUser']->isAllowed( 'surveyadmin' ) ? '0' : '1'
 70+ 'require-enabled' => $GLOBALS['wgUser']->isAllowed( 'surveyadmin' ) ? '0' : '1',
 71+ 'cookie' => 'no'
7172 ),
7273 wfMsg( 'surveys-takesurvey-loading' )
7374 ) );
Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -91,6 +91,7 @@
9292 return array(
9393 'id' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 1 ) ),
9494 'name' => array(),
 95+ 'cookie' => array(),
9596 'title' => array(),
9697 'require-enabled' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 1 ) ),
9798 );
Index: trunk/extensions/Survey/resources/ext.survey.tag.js
@@ -6,10 +6,60 @@
77 * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
88 */
99
10 -(function( $ ) { $( document ).ready( function() {
 10+(function( $, survey ) {
1111
12 - $( '.surveytag' ).each( function( index, domElement ) {
13 - $( domElement ).mwSurvey();
 12+ function getCookieName( options ) {
 13+ return ( typeof options.id !== 'undefined' ) ?
 14+ 'survey-id-' + options.id
 15+ : 'survey-name-' + options.name
 16+ }
 17+
 18+ function shouldShowSurvey( options ) {
 19+ var shouldShow = $.cookie( getCookieName( options ) ) != 'done';
 20+ survey.log( 'shouldShowSurvey ' + getCookieName( options ) + ': ' + shouldShow.toString() );
 21+ return shouldShow;
 22+ }
 23+
 24+ function onSurveyShown( options ) {
 25+ var expirySeconds = ( typeof options.expiry !== 'undefined' ) ? options.expiry : 60 * 60 * 24 * 30;
 26+ var date = new Date();
 27+ date.setTime( date.getTime() + expirySeconds * 1000 );
 28+ $.cookie( getCookieName( options ), 'done', { 'expires': date, 'path': '/' } );
 29+ }
 30+
 31+ function initTag( $tag ) {
 32+ var options = {};
 33+
 34+ if ( $tag.attr( 'survey-data-id' ) ) {
 35+ options['id'] = $tag.attr( 'survey-data-id' );
 36+ } else if ( $tag.attr( 'survey-data-name' ) ) {
 37+ options['name'] = $tag.attr( 'survey-data-name' );
 38+ }
 39+ else {
 40+ // TODO
 41+ return;
 42+ }
 43+
 44+ if ( $tag.attr( 'survey-data-cookie' ) === 'no' ) {
 45+ $tag.mwSurvey( options );
 46+ }
 47+ else {
 48+ if ( shouldShowSurvey( options ) ) {
 49+ options['onShow'] = function( eventArgs ) {
 50+ onSurveyShown( options );
 51+ };
 52+
 53+ $tag.mwSurvey( options );
 54+ }
 55+ }
 56+ }
 57+
 58+ $( document ).ready( function() {
 59+
 60+ $( '.surveytag' ).each( function( index, domElement ) {
 61+ initTag( $( domElement ) );
 62+ } );
 63+
1464 } );
1565
16 -} ); })( jQuery );
\ No newline at end of file
 66+})( jQuery, window.survey );
\ No newline at end of file
Index: trunk/extensions/Survey/resources/jquery.survey.js
@@ -9,6 +9,8 @@
1010 ( function ( $ ) { $.fn.mwSurvey = function( options ) {
1111
1212 var _this = this;
 13+ this.options = options;
 14+
1315 this.inputs = [];
1416
1517 this.identifier = null;
@@ -116,11 +118,11 @@
117119
118120 $input.data( 'question-id', question.id );
119121
120 - this.inputs.push( $input );
 122+ this.inputs.push( { 'input': $input, 'type': question.type } );
121123
122124 $q = $( '<div />' ).html( $input );
123125
124 - if ( question.type == type.CHECK ) {
 126+ if ( question.type === type.CHECK ) {
125127 $q.append( $( '<label />' ).text( question.text ).attr( 'for', id ) );
126128 }
127129 else {
@@ -155,11 +157,19 @@
156158 var answers = [];
157159
158160 for ( i in this.inputs ) {
159 - var $input = this.inputs[i];
 161+ var $input = this.inputs[i].input;
 162+ var id = $input.data( 'question-id' );
160163
 164+ if ( this.inputs[i].type === survey.question.type.RADIO ) {
 165+ var value = $( 'input:radio[name=survey-question-' + id + ']:checked' ).val();
 166+ }
 167+ else {
 168+ var value = $input.val();
 169+ }
 170+
161171 answers.push( {
162 - 'text': $input.val(),
163 - 'question_id': $input.data( 'question-id' )
 172+ 'text': value,
 173+ 'question_id': id
164174 } );
165175 }
166176
@@ -264,6 +274,10 @@
265275 } );
266276
267277 $link.click();
 278+
 279+ if ( typeof this.options.onShow === 'function' ) {
 280+ this.options.onShow( { 'id': surveyData.id } );
 281+ }
268282 };
269283
270284 this.init = function() {

Status & tagging log