diff --git a/application/controllers/admin/quotas.php b/application/controllers/admin/quotas.php index 26c6436c4fb..441b8c4bf3f 100644 --- a/application/controllers/admin/quotas.php +++ b/application/controllers/admin/quotas.php @@ -393,6 +393,10 @@ function editquota($iSurveyId) function new_answer($iSurveyId, $sSubAction = 'new_answer') { $iSurveyId = sanitize_int($iSurveyId); + + /** @var Survey $oSurvey */ + $oSurvey = Survey::model()->findByPk($iSurveyId); + $this->_checkPermissions($iSurveyId, 'update'); $aData = $this->_getData($iSurveyId); $sBaseLang = $aData['sBaseLang']; @@ -406,21 +410,17 @@ function new_answer($iSurveyId, $sSubAction = 'new_answer') $quota_name = $aQuotaDetails['name']; } - $result = Question::model()->findAllByAttributes(array('type' => array('G', 'M', 'Y', 'A', 'B', 'I', 'L', 'O', '!'), 'sid' => $iSurveyId, 'language' => $sBaseLang, 'parent_qid' => 0)); - if (empty($result)) - { + $result = $oSurvey->quotableQuestions; + if (empty($result)) { $aViewUrls[] = 'newanswererror_view'; - } - else - { + } else { $aData['newanswer_result'] = $result; $aData['quota_name'] = $quota_name; $aViewUrls[] = 'newanswer_view'; } } - if ($sSubAction == "new_answer_two" && isset($_POST['quota_qid']) && Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', 'create')) - { + if ($sSubAction == "new_answer_two" && isset($_POST['quota_qid']) && Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', 'create')) { $result = Quota::model()->findAllByPk(Yii::app()->request->getPost('quota_id')); foreach ($result as $aQuotaDetails){ diff --git a/application/models/Question.php b/application/models/Question.php index 17877e1fd9c..881c2ad46e5 100644 --- a/application/models/Question.php +++ b/application/models/Question.php @@ -40,6 +40,7 @@ * @property QuestionGroup $groups //TODO should be singular * @property Question $parents //TODO should be singular * @property Question[] $subquestions + * @property string[] $quotableTypes Question types that can be used for quotas */ class Question extends LSActiveRecord { @@ -1056,5 +1057,9 @@ public function fixSubQuestions(){ $criteria->addNotInCondition('title', CHtml::listData($validSubQuestion,'title','title')); Question::model()->deleteAll($criteria);// Must log count of deleted ? } + /** @return array */ + public static function getQuotableTypes(){ + return array('G', 'M', 'Y', 'A', 'B', 'I', 'L', 'O', '!'); + } } diff --git a/application/models/Survey.php b/application/models/Survey.php index 50f91b394bb..0e0716df3fc 100644 --- a/application/models/Survey.php +++ b/application/models/Survey.php @@ -81,6 +81,7 @@ * @property User $owner * @property QuestionGroup[] $groups * @property Quota[] $quotas + * @property Question[] $quotableQuestions * * @property array $fullAnswers * @property array $partialAnswers @@ -686,7 +687,7 @@ public function resetCache() { /** * Attribute renamed to questionindex in dbversion 169 * Y maps to 1 otherwise 0; - * @param type $value + * @param string $value */ public function setAllowjumps($value) { @@ -1261,4 +1262,43 @@ public function getsSurveyUrl() return $this->sSurveyUrl; } + + /** + * @return Question[] + */ + public function getQuotableQuestions() + { + $criteria = $this->getQuestionOrderCriteria(); + + $criteria->addColumnCondition(array( + 't.sid' => $this->sid, + 't.language' => $this->language, + 'parent_qid' => 0, + + )); + + $criteria->addInCondition('t.type',Question::getQuotableTypes()); + + /** @var Question[] $questions */ + $questions = Question::model()->findAll($criteria); + return $questions; + } + + /** + * Get the DB criteria to get questions as ordered in survey + * @return CDbCriteria + */ + private function getQuestionOrderCriteria(){ + $criteria=new CDbCriteria; + $criteria->select = Yii::app()->db->quoteColumnName('t.*'); + $criteria->with=array( + 'survey.groups', + ); + $criteria->order =Yii::app()->db->quoteColumnName('groups.group_order').',' + .Yii::app()->db->quoteColumnName('t.question_order'); + $criteria->addCondition('`groups`.`gid` =`t`.`gid`','AND'); + return $criteria; + + } + }