Skip to content

Commit

Permalink
Fixed issue: Quota methods not correct on frontend
Browse files Browse the repository at this point in the history
Dev: rewritten parts of the frontend_helper to correctly use the Model Methods
  • Loading branch information
lacrioque committed Mar 27, 2017
1 parent b4de45c commit 633e156
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
47 changes: 33 additions & 14 deletions application/helpers/frontend_helper.php
Expand Up @@ -2110,35 +2110,47 @@ function checkCompletedQuota($surveyid,$return=false)
if(!$aMatchedQuotas)
{
$aMatchedQuotas=array();
$quota_info=$aQuotasInfo = getQuotaInformation($surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);
if(!$aQuotasInfo || empty($aQuotasInfo)) {
// $aQuotasInfos = getQuotaInformation($surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);
$aQuotas = Quota::model()->findAllByAttributes(array('sid' => $surveyid));
// if(!$aQuotasInfo || empty($aQuotaInfos)) {
if(!$aQuotas || empty($aQuotas)) {
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 ($aQuotasInfo as $aQuotaInfo)
// foreach ($aQuotasInfos as $aQuotaInfo)
foreach ($aQuotas as $oQuota)
{
if(!$aQuotaInfo['active']) {
// if(!$aQuotaInfo['active']) {
if(!$oQuota->active) {
continue;
}
if(count($aQuotaInfo['members'])===0) {
// if(count($aQuotaInfo['members'])===0) {
if(count($oQuota->quotaMembers)===0) {
continue;
}
$iMatchedAnswers=0;
$bPostedField=false;

////Create filtering
// 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();
// To count number of hidden questions
$aQuotaQid=array();
foreach ($aQuotaInfo['members'] as $aQuotaMember)
//Fill the necessary filter arrays
foreach ($oQuota->quotaMembers as $oQuotaMember)
{
$aQuotaMember = $oQuotaMember->memberInfo;
$aQuotaFields[$aQuotaMember['fieldname']][] = $aQuotaMember['value'];
$aQuotaRelevantFieldnames[$aQuotaMember['fieldname']]=isset($_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']]) && $_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']];
$aQuotaRelevantFieldnames[$aQuotaMember['fieldname']]= isset($_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']]) && $_SESSION['survey_'.$surveyid]['relevanceStatus'][$aQuotaMember['qid']];
$aQuotaQid[]=$aQuotaMember['qid'];
}
$aQuotaQid=array_unique($aQuotaQid);

////Filter
// For each field : test if actual responses is in quota (and is relevant)
foreach ($aQuotaFields as $sFieldName=>$aValues)
{
Expand All @@ -2149,16 +2161,22 @@ function checkCompletedQuota($surveyid,$return=false)
if(in_array($sFieldName,$aPostedFields))// Need only one posted value
$bPostedField=true;
}
// Condition to count quota : Answers are the same in quota + an answer is submitted at this time (bPostedField) OR all questions is hidden (bAllHidden)
$bAllHidden=QuestionAttribute::model()->countByAttributes(array('qid'=>$aQuotaQid),'attribute=:attribute',array(':attribute'=>'hidden'))==count($aQuotaQid);


// Condition to count quota :
// Answers are the same in quota + an answer is submitted at this time (bPostedField)
// OR all questions is hidden (bAllHidden)
$bAllHidden = QuestionAttribute::model()
->countByAttributes(array('qid'=>$aQuotaQid),'attribute=:attribute',array(':attribute'=>'hidden')) == count($aQuotaQid);

if($iMatchedAnswers==count($aQuotaFields) && ( $bPostedField || $bAllHidden) )
{
if($aQuotaInfo['qlimit'] == 0) { // Always add the quota if qlimit==0
$aMatchedQuotas[]=$aQuotaInfo;
if($oQuota->qlimit == 0) { // Always add the quota if qlimit==0
$aMatchedQuotas[]=$oQuota->viewArray;
} else {
$iCompleted=getQuotaCompletedCount($surveyid, $aQuotaInfo['id']);
if(!is_null($iCompleted) && ((int)$iCompleted >= (int)$aQuotaInfo['qlimit'])) // This remove invalid quota and not completed
$aMatchedQuotas[]=$aQuotaInfo;
$iCompleted=getQuotaCompletedCount($surveyid, $oQuota->id);
if(!is_null($iCompleted) && ((int)$iCompleted >= (int)$oQuota->qlimit )) // This remove invalid quota and not completed
$aMatchedQuotas[]=$oQuota->viewArray;
}
}
}
Expand Down Expand Up @@ -2201,6 +2219,7 @@ function checkCompletedQuota($surveyid,$return=false)
/* @var $blockData PluginEventContent */
$blocks[] = CHtml::tag('div', array('id' => $blockData->getCssId(), 'class' => $blockData->getCssClass()), $blockData->getContent());
}

// Allow plugin to update message, url, url description and action
$sMessage=$event->get('message',$aMatchedQuota['quotals_message']);
$sUrl=$event->get('url',$aMatchedQuota['quotals_url']);
Expand Down
13 changes: 12 additions & 1 deletion application/models/Quota.php
Expand Up @@ -190,14 +190,25 @@ public function getCompleteCount(){
}
}

public function getViewArray(){
$languageSettings = $this->currentLanguageSetting;
$members = array();
foreach($this->quotaMembers as $quotaMember){
$members[] = $quotaMember->memberInfo;
}
$attributes = $this->attributes;

return array_merge(array(), $languageSettings->attributes, array('members' => $members), $attributes);
}

/**
* Get the QuotaLanguageSetting for current language
* @return QuotaLanguageSetting
*/
public function getCurrentLanguageSetting(){
$oQuotaLanguageSettings=QuotaLanguageSetting::model()
->with(array('quota' => array('condition' => 'sid="'.$this->survey->primaryKey.'"')))
->findByAttributes(array(
'quotals_quota_id' => $this->id,
'quotals_language'=>Yii::app()->getLanguage(),
));
if($oQuotaLanguageSettings){
Expand Down

0 comments on commit 633e156

Please sign in to comment.