Skip to content

Commit

Permalink
Fixed issue #12394: Order questions in quota->addQuestion (#696)
Browse files Browse the repository at this point in the history
Fixed issue #12394: Adding question to quota shoult order questions in display order
Dev: add quoteColumnName for order
Dev: Put the grouping criteria in separate method (reusable)
  • Loading branch information
TonisOrmisson authored and LouisGac committed Jun 23, 2017
1 parent 0495d24 commit 8a59df4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
16 changes: 8 additions & 8 deletions application/controllers/admin/quotas.php
Expand Up @@ -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'];
Expand All @@ -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){
Expand Down
5 changes: 5 additions & 0 deletions application/models/Question.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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', '!');
}

}
42 changes: 41 additions & 1 deletion application/models/Survey.php
Expand Up @@ -81,6 +81,7 @@
* @property User $owner
* @property QuestionGroup[] $groups
* @property Quota[] $quotas
* @property Question[] $quotableQuestions
*
* @property array $fullAnswers
* @property array $partialAnswers
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

}

}

0 comments on commit 8a59df4

Please sign in to comment.