Index: trunk/extensions/Survey/Survey.hooks.php |
— | — | @@ -136,7 +136,7 @@ |
137 | 137 | |
138 | 138 | $surveys = Survey::select( |
139 | 139 | array( |
140 | | - 'id', 'namespaces' |
| 140 | + 'id', 'namespaces', 'ratio', 'expiry' |
141 | 141 | ), |
142 | 142 | array( |
143 | 143 | 'enabled' => 1, |
— | — | @@ -157,7 +157,9 @@ |
158 | 158 | $GLOBALS['wgOut']->addWikiText( Xml::element( |
159 | 159 | 'survey', |
160 | 160 | array( |
161 | | - 'id' => $survey->getId() |
| 161 | + 'id' => $survey->getId(), |
| 162 | + 'ratio' => $survey->getField( 'ratio' ), |
| 163 | + 'expiry' => $survey->getField( 'expiry' ), |
162 | 164 | ) |
163 | 165 | ) ); |
164 | 166 | } |
Index: trunk/extensions/Survey/includes/SurveyTag.php |
— | — | @@ -94,6 +94,8 @@ |
95 | 95 | 'cookie' => array(), |
96 | 96 | 'title' => array(), |
97 | 97 | 'require-enabled' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 1 ) ), |
| 98 | + 'expiry' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0 ) ), |
| 99 | + 'ratio' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 100 ) ), |
98 | 100 | ); |
99 | 101 | } |
100 | 102 | |
Index: trunk/extensions/Survey/resources/ext.survey.tag.js |
— | — | @@ -17,41 +17,53 @@ |
18 | 18 | function shouldShowSurvey( options ) { |
19 | 19 | var shouldShow = $.cookie( getCookieName( options ) ) != 'done'; |
20 | 20 | survey.log( 'shouldShowSurvey ' + getCookieName( options ) + ': ' + shouldShow.toString() ); |
21 | | - return shouldShow; |
| 21 | + return options.cookie || shouldShow; |
22 | 22 | } |
23 | 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': '/' } ); |
| 24 | + function onSurveyDone( options ) { |
| 25 | + if ( options.cookie ) { |
| 26 | + var date = new Date(); |
| 27 | + date.setTime( date.getTime() + options.expiry * 1000 ); |
| 28 | + $.cookie( getCookieName( options ), 'done', { 'expires': date, 'path': '/' } ); |
| 29 | + survey.log( 'wrote done to cookie ' + getCookieName( options ) ); |
| 30 | + } |
29 | 31 | } |
30 | 32 | |
31 | 33 | function initTag( $tag ) { |
32 | | - var options = {}; |
| 34 | + var ratioAttr = $tag.attr( 'survey-data-ratio' ); |
| 35 | + var expiryAttr = $tag.attr( 'survey-data-expiry' ); |
33 | 36 | |
| 37 | + var options = { |
| 38 | + 'ratio': typeof ratioAttr === 'undefined' ? 1 : parseFloat( ratioAttr ) / 100, |
| 39 | + 'cookie': $tag.attr( 'survey-data-cookie' ) !== 'no', |
| 40 | + 'expiry': typeof expiryAttr === 'undefined' ? 60 * 60 * 24 * 30 : expiryAttr |
| 41 | + }; |
| 42 | + |
34 | 43 | if ( $tag.attr( 'survey-data-id' ) ) { |
35 | | - options['id'] = $tag.attr( 'survey-data-id' ); |
| 44 | + options.id = $tag.attr( 'survey-data-id' ); |
36 | 45 | } else if ( $tag.attr( 'survey-data-name' ) ) { |
37 | | - options['name'] = $tag.attr( 'survey-data-name' ); |
| 46 | + options.name = $tag.attr( 'survey-data-name' ); |
38 | 47 | } |
39 | 48 | else { |
40 | 49 | // TODO |
41 | 50 | return; |
42 | 51 | } |
43 | 52 | |
44 | | - if ( $tag.attr( 'survey-data-cookie' ) === 'no' ) { |
45 | | - $tag.mwSurvey( options ); |
46 | | - } |
47 | | - else { |
| 53 | + var rand = Math.random(); |
| 54 | + survey.log( rand + ' < ' + options.ratio ); |
| 55 | + |
| 56 | + if ( rand < options.ratio ) { |
48 | 57 | if ( shouldShowSurvey( options ) ) { |
49 | 58 | options['onShow'] = function( eventArgs ) { |
50 | | - onSurveyShown( options ); |
| 59 | + onSurveyDone( options ); |
51 | 60 | }; |
52 | 61 | |
53 | 62 | $tag.mwSurvey( options ); |
54 | 63 | } |
55 | 64 | } |
| 65 | + else { |
| 66 | + onSurveyDone( options ); |
| 67 | + } |
56 | 68 | } |
57 | 69 | |
58 | 70 | $( document ).ready( function() { |