Skip to content

Commit

Permalink
Dev: Quota speed optimisation : SQL only for matched quota in session
Browse files Browse the repository at this point in the history
Dev: Quota system is a good candidate for a core plugin in a future release
  • Loading branch information
Shnoulle committed Mar 24, 2015
1 parent 29a1647 commit d40a371
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
4 changes: 1 addition & 3 deletions application/helpers/common_helper.php
Expand Up @@ -1161,7 +1161,6 @@ function getSurveyInfo($surveyid, $languagecode='')
}

}

return $thissurvey;
}

Expand Down Expand Up @@ -5440,14 +5439,13 @@ function getQuotaCompletedCount($iSurveyId, $quotaid)
$oCriteria->condition="submitdate IS NOT NULL";
foreach ($aQuotaColumns as $sColumn=>$aValue)
{

if(count($aValue)==1)
{
$oCriteria->compare(Yii::app()->db->quoteColumnName($sColumn),$aValue); // NO need params : compare bind automatically
}
else
{
$oCriteria->addInCondition(Yii::app()->db->quoteColumnName($sColumn),$aValue); // NO need params : addInCondition bind automatically
$oCriteria->addInCondition(Yii::app()->db->quoteColumnName($sColumn),$aValue); // NO need params : addInCondition bind
}
}
$result = SurveyDynamic::model($iSurveyId)->count($oCriteria);
Expand Down
39 changes: 12 additions & 27 deletions application/helpers/frontend_helper.php
Expand Up @@ -1914,28 +1914,17 @@ function checkCompletedQuota($surveyid,$return=false)
// $aQuotasInfo have an 'active' key, we don't use it ?
if(!$aQuotasInfo || empty($aQuotasInfo))
return $aMatchedQuotas;
// Test only completed quota, other is not needed
$aQuotasCompleted=array();
foreach($aQuotasInfo as $aQuotaInfo)
{
$iCompleted=getQuotaCompletedCount($surveyid, $aQuotaInfo['id']);// Return a string
if(ctype_digit($iCompleted) && ((int)$iCompleted >= (int)$aQuotaInfo['qlimit'])) // This remove invalid quota and not completed
$aQuotasCompleted[]=$aQuotaInfo;
}

if(empty($aQuotasCompleted))
return $aMatchedQuotas;
// OK, we have some quota, then find if this $_SESSION have some set
$aPostedFields = explode("|",Yii::app()->request->getPost('fieldnames','')); // Needed for quota allowing update
foreach ($aQuotasCompleted as $aQuotaCompleted)
$aPostedFields = explode("|",Yii::app()->request->getPost('fieldnames','')); // Needed for quota allowing update
foreach ($aQuotasInfo as $aQuotaInfo)
{
$iMatchedAnswers=0;
$bPostedField=false;
// Array of field with quota array value
$aQuotaFields=array();
// Array of fieldnames with relevance value : EM fill $_SESSION with default value even is unrelevant (em_manager_helper line 6548)
$aQuotaRelevantFieldnames=array();
foreach ($aQuotaCompleted['members'] as $aQuotaMember)
foreach ($aQuotaInfo['members'] as $aQuotaMember)
{
$aQuotaFields[$aQuotaMember['fieldname']][] = $aQuotaMember['value'];
$aQuotaRelevantFieldnames[$aQuotaMember['fieldname']]=isset($_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']]) && $_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']];
Expand All @@ -1948,26 +1937,22 @@ function checkCompletedQuota($surveyid,$return=false)
{
$iMatchedAnswers++;
}
if(in_array($sFieldName,$aPostedFields))
if(in_array($sFieldName,$aPostedFields))// Need only one posted value
$bPostedField=true;
}
if($iMatchedAnswers==count($aQuotaFields))
// Count only needed quotas
if($iMatchedAnswers==count($aQuotaFields) && ( $aQuotaInfo['action']!=2 || $bPostedField ) )
{
switch ($aQuotaCompleted['action'])
{
case '1':
default:
$aMatchedQuotas[]=$aQuotaCompleted;
break;
case '2':
if($bPostedField)// Action 2 allow to correct last answers, then need to be posted
$aMatchedQuotas[]=$aQuotaCompleted;
break;
if((int)$aQuotaInfo['qlimit'] < 1){
$aMatchedQuotas[]=$aQuotaInfo;
}else{
$iCompleted=getQuotaCompletedCount($surveyid, $aQuotaInfo['id']);// Return a string
if(ctype_digit($iCompleted) && ((int)$iCompleted >= (int)$aQuotaInfo['qlimit'])) // This remove invalid quota and not completed
$aMatchedQuotas[]=$aQuotaInfo;
}
}
}
}

if ($return)
return $aMatchedQuotas;
if(empty($aMatchedQuotas))
Expand Down

0 comments on commit d40a371

Please sign in to comment.