Skip to content

Commit

Permalink
Fixed issue: Quotas were wrongly set in the backend.
Browse files Browse the repository at this point in the history
Dev: Moved methods from common_helper into the model. Kept compatibility link.
  • Loading branch information
lacrioque committed Mar 27, 2017
1 parent a24951b commit b4de45c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 91 deletions.
1 change: 1 addition & 0 deletions application/controllers/admin/quotas.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function index($iSurveyId, $quickreport = false)
foreach ($oSurvey->quotas as $oQuota)
{
$totalquotas += $oQuota->qlimit;
$completed = 0;

This comment has been minimized.

Copy link
@LouisGac

LouisGac Mar 27, 2017

Contributor

did you checked if this was making any problem when completed != 0 ?

This comment has been minimized.

Copy link
@lacrioque

lacrioque Mar 27, 2017

Author Contributor

Just reset the variable, it gets set anew each time in line 190

$completed = getQuotaCompletedCount($iSurveyId, $oQuota->primaryKey);
$totalcompleted = $totalcompleted + $completed;
$csvoutput[] = $oQuota->name . "," . $oQuota->qlimit . "," . $completed . "," . ($oQuota->qlimit - $completed) . "\r\n";
Expand Down
84 changes: 8 additions & 76 deletions application/helpers/common_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4118,48 +4118,19 @@ function enforceSSLMode()
};

/**
* @deprecated
* Returns the number of answers matching the quota
* THIS METHOD IS DEPRECATED AND IS LEFT ONLY FOR COMPATIBILITY REASONS
* USE THE METHOD ON THE QUOTA CLASS INSTEAD
*
* @param int $iSurveyId - Survey identification number
* @param int $iSurveyId - Survey identification number //deprecated
* @param int $quotaid - quota id for which you want to compute the completed field
* @return mixed - value of matching entries in the result DB or null
*/
function getQuotaCompletedCount($iSurveyId, $quotaid)
{
if(!tableExists("survey_{$iSurveyId}")) // Yii::app()->db->schema->getTable('{{survey_' . $iSurveyId . '}}' are not updated even after Yii::app()->db->schema->refresh();
return;
$aColumnName=SurveyDynamic::model($iSurveyId)->getTableSchema()->getColumnNames();
$aQuotas = getQuotaInformation($iSurveyId, Survey::model()->findByPk($iSurveyId)->language, $quotaid);
$aQuota = $aQuotas[0];
if (Yii::app()->db->schema->getTable('{{survey_' . $iSurveyId . '}}') &&
count($aQuota['members']) > 0)
{
// Keep a list of fields for easy reference
$aQuotaColumns = array();

foreach ($aQuota['members'] as $member)
{
if(in_array($member['fieldname'],$aColumnName))
$aQuotaColumns[$member['fieldname']][] = $member['value'];
else
return;
}

$oCriteria = new CDbCriteria;
$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
}
else
{
$oCriteria->addInCondition(Yii::app()->db->quoteColumnName($sColumn),$aValue); // NO need params : addInCondition bind
}
}
return SurveyDynamic::model($iSurveyId)->count($oCriteria);
}
$oQuota = Quota::model()->findByPk($quotaid);
return $oQuota->completeCount;
}

/**
Expand Down Expand Up @@ -4365,50 +4336,11 @@ function getQuotaInformation($surveyid,$deprecated=null,$iQuotaID=null)
// Array for each quota
$aQuotaInfo = array_merge($oQuota->attributes,$oQuota->currentLanguageSetting->attributes);
$aQuotaMembers = QuotaMember::model()->findAllByAttributes(array('quota_id'=>$oQuota->id));
$aQuotaInfo['members'] = array();
if (count($aQuotaMembers) > 0)
//$aQuotaInfo['members'] = $aQuotaMembers->memberInfo;
{
foreach ($aQuotaMembers as $oQuotaMember)
{
$oMemberQuestion=Question::model()->findByAttributes(array('qid'=>$oQuotaMember->qid, 'language'=>$baselang));
if($oMemberQuestion)
{
$sFieldName = "0";

if ($oMemberQuestion->type == "I" || $oMemberQuestion->type == "G" || $oMemberQuestion->type == "Y")
{
$sFieldName=$surveyid.'X'.$oMemberQuestion->gid.'X'.$oQuotaMember->qid;
$sValue = $oQuotaMember->code;
}

if($oMemberQuestion->type == "L" || $oMemberQuestion->type == "O" || $oMemberQuestion->type =="!")
{
$sFieldName=$surveyid.'X'.$oMemberQuestion->gid.'X'.$oQuotaMember->qid;
$sValue = $oQuotaMember->code;
}

if($oMemberQuestion->type == "M")
{
$sFieldName=$surveyid.'X'.$oMemberQuestion->gid.'X'.$oQuotaMember->qid.$oQuotaMember->code;
$sValue = "Y";
}

if($oMemberQuestion->type == "A" || $oMemberQuestion->type == "B")
{
$temp = explode('-',$oQuotaMember->code);
$sFieldName=$surveyid.'X'.$oMemberQuestion->gid.'X'.$oQuotaMember->qid.$temp[0];
$sValue = $temp[1];
}

$aQuotaInfo['members'][]=array(
'title' => $oMemberQuestion->title,
'type' => $oMemberQuestion->type,
'code' => $oQuotaMember->code,
'value' => $sValue,
'qid' => $oQuotaMember->qid,
'fieldname' => $sFieldName,
);
}
$aQuotaInfo['members'][]=$oQuotaMember->memberInfo;
}
}
// Push this quota Information to all survey quota
Expand Down
39 changes: 34 additions & 5 deletions application/models/Quota.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,40 @@ function deleteQuota($condition = false, $recursive = true)
* @return QuotaLanguageSetting
*/
public function getMainLanguagesetting(){
return QuotaLanguageSetting::model()
->with(array('quota' => array('condition' => 'sid="'.$this->survey->primaryKey.'"')))
->findByAttributes(array(
'quotals_language'=>$this->survey->language,
));
return $this->languagesettings[ $this->survey->language ];

}

public function getCompleteCount(){
// if(!tableExists("survey_{$this->survey->id}")) // Yii::app()->db->schema->getTable('{{survey_' . $iSurveyId . '}}' are not updated even after Yii::app()->db->schema->refresh();
// return;

if (count($this->quotaMembers) > 0)
{
// Keep a list of fields for easy reference
$aQuotaColumns = array();
foreach ($this->quotaMembers as $member)
{
$aQuotaColumns[$member->memberInfo['fieldname']][] = $member->memberInfo['value'];
}

$oCriteria = new CDbCriteria;
$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

This comment has been minimized.

Copy link
@LouisGac

LouisGac Mar 27, 2017

Contributor

compare need to be tested on MSSQL (often makes problems)... and postgresql just in case...

This comment has been minimized.

Copy link
@lacrioque

lacrioque Mar 27, 2017

Author Contributor

This method was only moved.
See common_helper.php line 4154

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Mar 27, 2017

Collaborator

mssql : it's the reason why added db->quoteColumnName, at the time this part was updated Carsten test it in mssql.

And method tested in pgsql.

}
else
{
$oCriteria->addInCondition(Yii::app()->db->quoteColumnName($sColumn),$aValue); // NO need params : addInCondition bind
}
}
return SurveyDynamic::model($this->survey->sid)->count($oCriteria);
} else {
return 0;
}
}

/**
Expand Down
57 changes: 54 additions & 3 deletions application/models/QuotaMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ public static function model($class = __CLASS__)
{
return parent::model($class);
}

public function rules()
{
return array(
array('code', 'required', 'on'=>array('create'))
);
}

/**
* Returns the relations
*
* @access public
* @return array
*/
public function relations()
{
return array(
'survey' => array(self::BELONGS_TO, 'Survey', 'sid'),
'question' => array(self::BELONGS_TO, 'Question', 'qid'),
'quota' => array(self::BELONGS_TO, 'Quota', 'quota_id'),
);
}
/**
* Returns the setting's table name to be used by the model
*
Expand All @@ -57,11 +70,49 @@ public function primaryKey()
return 'id';
}

public function getMemberInfo()
{
$sFieldName = "0";

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Mar 27, 2017

Collaborator

? better null here , no . null is more clear than 0 (for me)

if ($this->question->type == "I" || $this->question->type == "G" || $this->question->type == "Y")
{
$sFieldName=$this->sid.'X'.$this->question->gid.'X'.$this->qid;
$sValue = $this->code;
}

if($this->question->type == "L" || $this->question->type == "O" || $this->question->type =="!")
{
$sFieldName=$this->sid.'X'.$this->question->gid.'X'.$this->qid;
$sValue = $this->code;
}

if($this->question->type == "M")
{
$sFieldName=$this->sid.'X'.$this->question->gid.'X'.$this->qid.$this->code;
$sValue = "Y";
}

if($this->question->type == "A" || $this->question->type == "B")
{
$temp = explode('-',$this->code);
$sFieldName=$this->sid->sid.'X'.$this->question->gid.'X'.$this->qid.$temp[0];
$sValue = $temp[1];
}

return array(
'title' => $this->question->title,
'type' => $this->question->type,
'code' => $this->code,
'value' => $sValue,
'qid' => $this->qid,
'fieldname' => $sFieldName,
);
}

function insertRecords($data)
{
$members = new self;
foreach ($data as $k => $v)
$members->$k = $v;
return $members->save();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}
?>

<?php /*echo "<pre>".print_r($oQuota->mainLanguagesetting->attributes,true)."</pre>";*/ ?>
<div class="panel panel-<?php echo ($oQuota->active==1 ? 'primary' : 'default') ?>">
<div class="panel-heading">
<div class="pull-right small">
Expand Down
14 changes: 7 additions & 7 deletions application/views/admin/quotas/viewquotas_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@
},
),
array(
'name'=>'completed',
'name'=>'completeCount',
'type'=>'raw',
'htmlOptions'=>array('style'=>'vertical-align:top'),
'value'=>function($oQuota)use($oSurvey){
$completerCount =getQuotaCompletedCount($oSurvey->sid, $oQuota->id);
$class = ($completerCount <= $oQuota->qlimit ? 'text-warning':null);
$span = CHtml::tag('span',array('class'=>$class),$completerCount);
return $span;
},
// 'value'=>function($oQuota)use($oSurvey){
// $completerCount =getQuotaCompletedCount($oSurvey->sid, $oQuota->id);
// $class = ($completerCount <= $oQuota->qlimit ? 'text-warning':null);
// $span = CHtml::tag('span',array('class'=>$class),$completerCount);
// return $span;
// },
'footer'=>$totalcompleted,
),
array(
Expand Down

0 comments on commit b4de45c

Please sign in to comment.