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

Revision: 96806
Author:   jeroendedauw
Date:     2011-09-11 19:52:45 +0000 (Sun, 11 Sep 2011)
Log Message:
-----------
fixed radio inputs and implemented cookies (omnomom)

Modified Paths:
--------------
    trunk/extensions/Survey/Survey.php
    trunk/extensions/Survey/includes/SurveyTag.php
    trunk/extensions/Survey/resources/ext.survey.tag.js
    trunk/extensions/Survey/resources/jquery.survey.js
    trunk/extensions/Survey/specials/SpecialSurvey.php
    trunk/extensions/Survey/specials/SpecialTakeSurvey.php

Modified: trunk/extensions/Survey/Survey.php
===================================================================
--- trunk/extensions/Survey/Survey.php  2011-09-11 19:46:33 UTC (rev 96805)
+++ trunk/extensions/Survey/Survey.php  2011-09-11 19:52:45 UTC (rev 96806)
@@ -192,7 +192,7 @@
        'scripts' => array(
                'ext.survey.tag.js'
        ),
-       'dependencies' => array( 'ext.survey.jquery' ),
+       'dependencies' => array( 'ext.survey.jquery', 'jquery.cookie' ),
 );
 
 unset( $moduleTemplate );

Modified: trunk/extensions/Survey/includes/SurveyTag.php
===================================================================
--- trunk/extensions/Survey/includes/SurveyTag.php      2011-09-11 19:46:33 UTC 
(rev 96805)
+++ trunk/extensions/Survey/includes/SurveyTag.php      2011-09-11 19:52:45 UTC 
(rev 96806)
@@ -91,6 +91,7 @@
                return array(
                        'id' => array( 'filter' => FILTER_VALIDATE_INT, 
'options' => array( 'min_range' => 1 ) ),
                        'name' => array(),
+                       'cookie' => array(),
                        'title' => array(),
                        'require-enabled' => array( 'filter' => 
FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 1 ) ),
                );

Modified: trunk/extensions/Survey/resources/ext.survey.tag.js
===================================================================
--- trunk/extensions/Survey/resources/ext.survey.tag.js 2011-09-11 19:46:33 UTC 
(rev 96805)
+++ trunk/extensions/Survey/resources/ext.survey.tag.js 2011-09-11 19:52:45 UTC 
(rev 96806)
@@ -6,10 +6,60 @@
  * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
  */
 
-(function( $ ) { $( document ).ready( function() {
+(function( $, survey ) {
        
-       $( '.surveytag' ).each( function( index, domElement ) {
-               $( domElement ).mwSurvey();
+       function getCookieName( options ) {
+               return ( typeof options.id !== 'undefined' ) ?
+                       'survey-id-' + options.id
+                       : 'survey-name-' + options.name
+       }
+       
+       function shouldShowSurvey( options ) {
+               var shouldShow = $.cookie( getCookieName( options ) ) != 'done';
+               survey.log( 'shouldShowSurvey ' + getCookieName( options ) + ': 
' + shouldShow.toString() );
+               return shouldShow;
+       }
+       
+       function onSurveyShown( options ) {
+               var expirySeconds = ( typeof options.expiry !== 'undefined' ) ? 
options.expiry : 60 * 60 * 24 * 30;
+               var date = new Date();
+               date.setTime( date.getTime() + expirySeconds * 1000 );
+               $.cookie( getCookieName( options ), 'done', { 'expires': date, 
'path': '/' } );
+       }
+       
+       function initTag( $tag ) {
+               var options = {};
+               
+               if ( $tag.attr( 'survey-data-id' ) ) {
+                       options['id'] = $tag.attr( 'survey-data-id' );
+               } else if ( $tag.attr( 'survey-data-name' ) ) {
+                       options['name'] = $tag.attr( 'survey-data-name' );
+               }
+               else {
+                       // TODO
+                       return;
+               }
+               
+               if ( $tag.attr( 'survey-data-cookie' ) === 'no' ) {
+                       $tag.mwSurvey( options );
+               }
+               else {
+                       if ( shouldShowSurvey( options ) ) {
+                               options['onShow'] = function( eventArgs ) {
+                                       onSurveyShown( options );
+                               };
+                               
+                               $tag.mwSurvey( options );
+                       }
+               }
+       }
+       
+       $( document ).ready( function() {
+       
+               $( '.surveytag' ).each( function( index, domElement ) {
+                       initTag( $( domElement ) );
+               } );
+       
        } );
        
-} ); })( jQuery );
\ No newline at end of file
+})( jQuery, 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-11 19:46:33 UTC 
(rev 96805)
+++ trunk/extensions/Survey/resources/jquery.survey.js  2011-09-11 19:52:45 UTC 
(rev 96806)
@@ -9,6 +9,8 @@
 ( function ( $ ) { $.fn.mwSurvey = function( options ) {
        
        var _this = this;
+       this.options = options;
+       
        this.inputs = [];
        
        this.identifier = null;
@@ -116,11 +118,11 @@
                
                $input.data( 'question-id', question.id );
                
-               this.inputs.push( $input );
+               this.inputs.push( { 'input': $input, 'type': question.type } );
                
                $q = $( '<div />' ).html( $input );
                
-               if ( question.type == type.CHECK ) {
+               if ( question.type === type.CHECK ) {
                        $q.append( $( '<label />' ).text( question.text ).attr( 
'for', id ) );
                }
                else {
@@ -155,11 +157,19 @@
                var answers = [];
                
                for ( i in this.inputs ) {
-                       var $input = this.inputs[i];
+                       var $input = this.inputs[i].input;
+                       var id = $input.data( 'question-id' );
                        
+                       if ( this.inputs[i].type === survey.question.type.RADIO 
) {
+                               var value = $( 
'input:radio[name=survey-question-' + id + ']:checked' ).val();
+                       }
+                       else {
+                               var value = $input.val();
+                       }
+                       
                        answers.push( {
-                               'text': $input.val(),
-                               'question_id': $input.data( 'question-id' )
+                               'text': value,
+                               'question_id': id
                        } );
                }
                
@@ -264,6 +274,10 @@
                } );
                
                $link.click();
+               
+               if ( typeof this.options.onShow === 'function' ) {
+                       this.options.onShow( { 'id': surveyData.id } );
+               }
        };
        
        this.init = function() {

Modified: trunk/extensions/Survey/specials/SpecialSurvey.php
===================================================================
--- trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-11 19:46:33 UTC 
(rev 96805)
+++ trunk/extensions/Survey/specials/SpecialSurvey.php  2011-09-11 19:52:45 UTC 
(rev 96806)
@@ -83,6 +83,8 @@
                        $survey->setField( $field, $wgRequest->getInt( 
'survey-' . $field ) );
                }
                
+               $survey->setField( 'namespaces', array() );
+               
                $survey->setQuestions( $this->getSubmittedQuestions() );
                
                $survey->writeToDB();
@@ -101,9 +103,9 @@
                foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value 
) {
                        $matches = array();
                        
-                       if ( preg_match( '/survey-question-text-(\d)+/', $name, 
$matches ) ) {
+                       if ( preg_match( '/survey-question-text-(\d+)/', $name, 
$matches ) ) {
                                $questions[] = $this->getSubmittedQuestion( 
$matches[1] );
-                       } elseif ( preg_match( 
'/survey-question-text-new-(\d)+/', $name, $matches ) ) {
+                       } elseif ( preg_match( 
'/survey-question-text-new-(\d+)/', $name, $matches ) ) {
                                $questions[] = $this->getSubmittedQuestion( 
$matches[1], true );
                        }
                }

Modified: trunk/extensions/Survey/specials/SpecialTakeSurvey.php
===================================================================
--- trunk/extensions/Survey/specials/SpecialTakeSurvey.php      2011-09-11 
19:46:33 UTC (rev 96805)
+++ trunk/extensions/Survey/specials/SpecialTakeSurvey.php      2011-09-11 
19:52:45 UTC (rev 96806)
@@ -66,7 +66,8 @@
                        'survey',
                        array(
                                'name' => $subPage,
-                               'require-enabled' => 
$GLOBALS['wgUser']->isAllowed( 'surveyadmin' ) ? '0' : '1'
+                               'require-enabled' => 
$GLOBALS['wgUser']->isAllowed( 'surveyadmin' ) ? '0' : '1',
+                               'cookie' => 'no'
                        ),
                        wfMsg( 'surveys-takesurvey-loading' )
                ) );


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

Reply via email to