From 633e1563ffdb311296def95251a12a05cf351304 Mon Sep 17 00:00:00 2001 From: markusfluer Date: Mon, 27 Mar 2017 12:37:17 +0200 Subject: [PATCH] Fixed issue: Quota methods not correct on frontend Dev: rewritten parts of the frontend_helper to correctly use the Model Methods --- application/helpers/frontend_helper.php | 47 +++++++++++++++++-------- application/models/Quota.php | 13 ++++++- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/application/helpers/frontend_helper.php b/application/helpers/frontend_helper.php index 64c51ef3559..51d8f1cebbf 100644 --- a/application/helpers/frontend_helper.php +++ b/application/helpers/frontend_helper.php @@ -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) { @@ -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; } } } @@ -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']); diff --git a/application/models/Quota.php b/application/models/Quota.php index 5ec5faeb03d..13989bd7137 100644 --- a/application/models/Quota.php +++ b/application/models/Quota.php @@ -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){