r96890 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96889‎ | r96890 | r96891 >
Date:19:46, 12 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
follow up to r96855; adding js for min_pages setting
Modified paths:
  • /trunk/extensions/Survey/Survey.hooks.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyTag.php (modified) (history)
  • /trunk/extensions/Survey/resources/ext.survey.tag.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.hooks.php
@@ -136,7 +136,7 @@
137137
138138 $surveys = Survey::select(
139139 array(
140 - 'id', 'namespaces', 'ratio', 'expiry'
 140+ 'id', 'namespaces', 'ratio', 'expiry', 'min_pages'
141141 ),
142142 array(
143143 'enabled' => 1,
@@ -160,6 +160,7 @@
161161 'id' => $survey->getId(),
162162 'ratio' => $survey->getField( 'ratio' ),
163163 'expiry' => $survey->getField( 'expiry' ),
 164+ 'min-pages' => $survey->getField( 'min_pages' ),
164165 )
165166 ) );
166167 }
Index: trunk/extensions/Survey/includes/SurveyTag.php
@@ -95,6 +95,7 @@
9696 'title' => array(),
9797 'require-enabled' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 1 ) ),
9898 'expiry' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0 ) ),
 99+ 'min-pages' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0 ) ),
99100 'ratio' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => 0, 'max_range' => 100 ) ),
100101 );
101102 }
Index: trunk/extensions/Survey/resources/ext.survey.tag.js
@@ -10,33 +10,56 @@
1111
1212 function getCookieName( options ) {
1313 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
1616 }
1717
1818 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+ }
2227 }
2328
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 ) {
2536 if ( options.cookie ) {
2637 var date = new Date();
2738 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 ) );
3041 }
3142 }
3243
 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+
3354 function initTag( $tag ) {
3455 var ratioAttr = $tag.attr( 'survey-data-ratio' );
3556 var expiryAttr = $tag.attr( 'survey-data-expiry' );
 57+ var pagesAttr = $tag.attr( 'survey-data-min-pages' );
3658
3759 var options = {
3860 'ratio': typeof ratioAttr === 'undefined' ? 1 : parseFloat( ratioAttr ) / 100,
3961 '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 )
4164 };
4265
4366 if ( $tag.attr( 'survey-data-id' ) ) {
@@ -49,17 +72,15 @@
5073 return;
5174 }
5275
53 - var rand = Math.random();
54 - survey.log( rand + ' < ' + options.ratio );
55 -
56 - if ( rand < options.ratio ) {
 76+ if ( hasCookie( options ) || winsLottery( options ) ) {
5777 if ( shouldShowSurvey( options ) ) {
58 - options['onShow'] = function( eventArgs ) {
59 - onSurveyDone( options );
60 - };
61 -
6278 $tag.mwSurvey( options );
 79+ setCookie( options, 'done' );
6380 }
 81+ else if ( options.pages !== 0 ) {
 82+ var nr = parseInt( getCookie( options ) );
 83+ setCookie( options, ( isNaN( nr ) ? 0 : nr ) + 1 )
 84+ }
6485 }
6586 else {
6687 onSurveyDone( options );

Follow-up revisions

RevisionCommit summaryAuthorDate
r96895Follow up to r96890; fixed some logic failsjeroendedauw20:13, 12 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r96855added min_pages settingjeroendedauw15:28, 12 September 2011

Status & tagging log