Index: trunk/extensions/Survey/Survey.hooks.php |
— | — | @@ -136,7 +136,7 @@ |
137 | 137 | |
138 | 138 | $surveys = Survey::select( |
139 | 139 | array( |
140 | | - 'id', 'namespaces', 'ratio', 'expiry' |
| 140 | + 'id', 'namespaces', 'ratio', 'expiry', 'min_pages' |
141 | 141 | ), |
142 | 142 | array( |
143 | 143 | 'enabled' => 1, |
— | — | @@ -160,6 +160,7 @@ |
161 | 161 | 'id' => $survey->getId(), |
162 | 162 | 'ratio' => $survey->getField( 'ratio' ), |
163 | 163 | 'expiry' => $survey->getField( 'expiry' ), |
| 164 | + 'min-pages' => $survey->getField( 'min_pages' ), |
164 | 165 | ) |
165 | 166 | ) ); |
166 | 167 | } |
Index: trunk/extensions/Survey/includes/SurveyTag.php |
— | — | @@ -95,6 +95,7 @@ |
96 | 96 | 'title' => array(), |
97 | 97 | 'require-enabled' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 1 ) ), |
98 | 98 | 'expiry' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0 ) ), |
| 99 | + 'min-pages' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0 ) ), |
99 | 100 | 'ratio' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 100 ) ), |
100 | 101 | ); |
101 | 102 | } |
Index: trunk/extensions/Survey/resources/ext.survey.tag.js |
— | — | @@ -10,33 +10,56 @@ |
11 | 11 | |
12 | 12 | function getCookieName( options ) { |
13 | 13 | return ( typeof options.id !== 'undefined' ) ? |
14 | | - 'survey-id-' + options.id |
15 | | - : 'survey-name-' + options.name |
| 14 | + 'ssurvey-id-' + options.id |
| 15 | + : 'ssurvey-name-' + options.name |
16 | 16 | } |
17 | 17 | |
18 | 18 | function shouldShowSurvey( options ) { |
19 | | - var shouldShow = $.cookie( getCookieName( options ) ) != 'done'; |
20 | | - survey.log( 'shouldShowSurvey ' + getCookieName( options ) + ': ' + shouldShow.toString() ); |
21 | | - return options.cookie || shouldShow; |
| 19 | + if ( !options.cookie ) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + else { |
| 23 | + var cookie = getCookie( options ); |
| 24 | + return ( options.pages === 0 && cookie !== 'done' ) |
| 25 | + || ( options.pages !== 0 && parseInt( cookie ) >= options.pages ); |
| 26 | + } |
22 | 27 | } |
23 | 28 | |
24 | | - function onSurveyDone( options ) { |
| 29 | + function getCookie( options ) { |
| 30 | + var cookie = $.cookie( getCookieName( options ) ); |
| 31 | + survey.log( 'read "' + cookie + '" from cookie ' + getCookieName( options ) ); |
| 32 | + return cookie; |
| 33 | + } |
| 34 | + |
| 35 | + function setCookie( options, cookieValue ) { |
25 | 36 | if ( options.cookie ) { |
26 | 37 | var date = new Date(); |
27 | 38 | date.setTime( date.getTime() + options.expiry * 1000 ); |
28 | | - $.cookie( getCookieName( options ), 'done', { 'expires': date, 'path': '/' } ); |
29 | | - survey.log( 'wrote done to cookie ' + getCookieName( options ) ); |
| 39 | + $.cookie( getCookieName( options ), cookieValue, { 'expires': date, 'path': '/' } ); |
| 40 | + survey.log( 'wrote "' + cookieValue + '" to cookie ' + getCookieName( options ) ); |
30 | 41 | } |
31 | 42 | } |
32 | 43 | |
| 44 | + function hasCookie( options ) { |
| 45 | + return getCookie( options ) !== null; |
| 46 | + } |
| 47 | + |
| 48 | + function winsLottery( options ) { |
| 49 | + var rand = Math.random(); |
| 50 | + survey.log( 'doLottery: ' + rand + ' < ' + options.ratio ); |
| 51 | + return rand < options.ratio; |
| 52 | + } |
| 53 | + |
33 | 54 | function initTag( $tag ) { |
34 | 55 | var ratioAttr = $tag.attr( 'survey-data-ratio' ); |
35 | 56 | var expiryAttr = $tag.attr( 'survey-data-expiry' ); |
| 57 | + var pagesAttr = $tag.attr( 'survey-data-min-pages' ); |
36 | 58 | |
37 | 59 | var options = { |
38 | 60 | 'ratio': typeof ratioAttr === 'undefined' ? 1 : parseFloat( ratioAttr ) / 100, |
39 | 61 | 'cookie': $tag.attr( 'survey-data-cookie' ) !== 'no', |
40 | | - 'expiry': typeof expiryAttr === 'undefined' ? 60 * 60 * 24 * 30 : expiryAttr |
| 62 | + 'expiry': typeof expiryAttr === 'undefined' ? 60 * 60 * 24 * 30 : 60 * 60 * 24 * 30,//parseInt( expiryAttr ), |
| 63 | + 'pages': typeof pagesAttr === 'undefined' ? 0 : parseInt( pagesAttr ) |
41 | 64 | }; |
42 | 65 | |
43 | 66 | if ( $tag.attr( 'survey-data-id' ) ) { |
— | — | @@ -49,17 +72,15 @@ |
50 | 73 | return; |
51 | 74 | } |
52 | 75 | |
53 | | - var rand = Math.random(); |
54 | | - survey.log( rand + ' < ' + options.ratio ); |
55 | | - |
56 | | - if ( rand < options.ratio ) { |
| 76 | + if ( hasCookie( options ) || winsLottery( options ) ) { |
57 | 77 | if ( shouldShowSurvey( options ) ) { |
58 | | - options['onShow'] = function( eventArgs ) { |
59 | | - onSurveyDone( options ); |
60 | | - }; |
61 | | - |
62 | 78 | $tag.mwSurvey( options ); |
| 79 | + setCookie( options, 'done' ); |
63 | 80 | } |
| 81 | + else if ( options.pages !== 0 ) { |
| 82 | + var nr = parseInt( getCookie( options ) ); |
| 83 | + setCookie( options, ( isNaN( nr ) ? 0 : nr ) + 1 ) |
| 84 | + } |
64 | 85 | } |
65 | 86 | else { |
66 | 87 | onSurveyDone( options ); |