Skip to content

Commit

Permalink
Fixed #7088: When creating quota, I cannot add answer
Browse files Browse the repository at this point in the history
dev: added rules to model, when save not successful redirect to edit screen. also made sure existing row check is always done
  • Loading branch information
mennodekker committed Dec 23, 2012
1 parent e62496f commit 3267b4e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 111 deletions.
125 changes: 51 additions & 74 deletions application/controllers/admin/quotas.php
Expand Up @@ -273,24 +273,30 @@ function insertquotaanswer($iSurveyId)
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'create');

$oQuotaMembers = new Quota_members;
$oQuotaMembers = new Quota_members('create'); // Trigger the 'create' rules
$oQuotaMembers->sid = $iSurveyId;
$oQuotaMembers->qid = Yii::app()->request->getPost('quota_qid');
$oQuotaMembers->quota_id = Yii::app()->request->getPost('quota_id');
$oQuotaMembers->code = Yii::app()->request->getPost('quota_anscode');
$oQuotaMembers->save();

if (!empty($_POST['createanother']))
{
if ($oQuotaMembers->save()) {
if (!empty($_POST['createanother']))
{
$_POST['action'] = "quotas";
$_POST['subaction'] = "new_answer";
$sSubAction = "new_answer";
self::new_answer($iSurveyId, $sSubAction);
}
else
{
self::_redirectToIndex($iSurveyId);
}
} else {
// Save was not successful, redirect back
$_POST['action'] = "quotas";
$_POST['subaction'] = "new_answer";
$sSubAction = "new_answer";
$sSubAction = "new_answer_two";
self::new_answer($iSurveyId, $sSubAction);
}
else
{
self::_redirectToIndex($iSurveyId);
}
}

function delans($iSurveyId)
Expand Down Expand Up @@ -422,12 +428,21 @@ function newquota($iSurveyId)
$this->_renderWrappedTemplate('quotas', 'newquota_view', $aData);
}

/**
*
* @param type $iQuestionId
* @param type $iSurveyId
* @param type $iQuotaId
* @return array
*/
function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
{
$iSurveyId = sanitize_int($iSurveyId);
$aData = $this->_getData($iSurveyId);
$sBaseLang = $aData['sBaseLang'];
$clang = $aData['clang'];
$iQuestionId = sanitize_int($iQuestionId);
$iSurveyId = sanitize_int($iSurveyId);
$iQuotaId = sanitize_int($iQuotaId);
$aData = $this->_getData($iSurveyId);
$sBaseLang = $aData['sBaseLang'];
$clang = $aData['clang'];

$aQuestion = Questions::model()->findByPk(array('qid' => $iQuestionId, 'language' => $sBaseLang));
$aQuestionType = $aQuestion['type'];
Expand All @@ -442,44 +457,22 @@ function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['question'], 0, 40), 'code' => $aDbAnsList['title']);
$aAnswerList[$aDbAnsList['title']] = $tmparrayans;
}

$aResults = Quota_members::model()->findAllByAttributes(array('sid' => $iSurveyId, 'qid' => $iQuestionId, 'quota_id' => $iQuotaId));
foreach($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}
else
} elseif ($aQuestionType == 'G')
{
$aResults = Quota_members::model()->findAllByAttributes(array('sid' => $iSurveyId, 'qid' => $iQuestionId, 'quota_id' => $iQuotaId));
}

if ($aQuestionType == 'G')
{
$aAnswerList = array('M' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("Male"), 'code' => 'M'),
$aAnswerList = array(
'M' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("Male"), 'code' => 'M'),
'F' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("Female"), 'code' => 'F'));

foreach ($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}

if ($aQuestionType == 'L' || $aQuestionType == 'O' || $aQuestionType == '!')
} elseif ($aQuestionType == 'L' || $aQuestionType == 'O' || $aQuestionType == '!')
{
$aAnsResults = Answers::model()->findAllByAttributes(array('qid' => $iQuestionId));

$aAnswerList = array();

foreach ($aAnsResults as $aDbAnsList)
foreach ($aAnsResults as $aDbAnsList)
{
$aAnswerList[$aDbAnsList['code']] = array('Title' => $aQuestion['title'],
'Display' => substr($aDbAnsList['answer'], 0, 40),
'code' => $aDbAnsList['code']);
$aAnswerList[$aDbAnsList['code']] = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['answer'], 0, 40), 'code' => $aDbAnsList['code']);
}
}

if ($aQuestionType == 'A')
} elseif ($aQuestionType == 'A')
{
$aAnsResults = Questions::model()->findAllByAttributes(array('parent_qid' => $iQuestionId));

Expand All @@ -493,14 +486,7 @@ function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
$aAnswerList[$aDbAnsList['title'] . "-" . $x] = $tmparrayans;
}
}

foreach ($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}

if ($aQuestionType == 'B')
} elseif ($aQuestionType == 'B')
{
$aAnsResults = Answers::model()->findAllByAttributes(array('qid' => $iQuestionId));

Expand All @@ -514,25 +500,12 @@ function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
$aAnswerList[$aDbAnsList['code'] . "-" . $x] = $tmparrayans;
}
}

foreach ($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}

if ($aQuestionType == 'Y')
} elseif ($aQuestionType == 'Y')
{
$aAnswerList = array('Y' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("Yes"), 'code' => 'Y'),
$aAnswerList = array(
'Y' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("Yes"), 'code' => 'Y'),
'N' => array('Title' => $aQuestion['title'], 'Display' => $clang->gT("No"), 'code' => 'N'));

foreach ($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}

if ($aQuestionType == 'I')
} elseif ($aQuestionType == 'I')
{
$slangs = Survey::model()->findByPk($iSurveyId)->additionalLanguages;
array_unshift($slangs, $sBaseLang);
Expand All @@ -542,19 +515,23 @@ function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => getLanguageNameFromCode($value, false), $value);
$aAnswerList[$value] = $tmparrayans;
}

foreach ($aResults as $aQuotaList)
{
$aAnswerList[$aQuotaList['code']]['rowexists'] = '1';
}
}

if (empty($aAnswerList))
{
return array();
}
}
else
{
// Now we mark answers already used in this quota as such
$aExistsingAnswers = Quota_members::model()->findAllByAttributes(array('sid' => $iSurveyId, 'qid' => $iQuestionId, 'quota_id' => $iQuotaId));
foreach ($aExistsingAnswers as $aAnswerRow)
{
if (array_key_exists($aAnswerRow['code'], $aAnswerList))
{
$aAnswerList[$aAnswerRow['code']]['rowexists'] = '1';
}
}
return $aAnswerList;
}
}
Expand Down
80 changes: 43 additions & 37 deletions application/models/Quota_members.php
Expand Up @@ -16,47 +16,53 @@

class Quota_members extends CActiveRecord
{
/**
* Returns the static model of Settings table
*
* @static
* @access public
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}
* @return Quota_members
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}

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

/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{quota_members}}';
}
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{quota_members}}';
}

/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'id';
}
/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'id';
}

function insertRecords($data)
function insertRecords($data)
{
$members = new self;
foreach ($data as $k => $v)
$members->$k = $v;
return $members->save();
foreach ($data as $k => $v)
$members->$k = $v;
return $members->save();
}
}
?>
}

0 comments on commit 3267b4e

Please sign in to comment.