Skip to content

Instantly share code, notes, and snippets.

@thyseus
Created June 10, 2013 16:27
Show Gist options
  • Select an option

  • Save thyseus/5750112 to your computer and use it in GitHub Desktop.

Select an option

Save thyseus/5750112 to your computer and use it in GitHub Desktop.
public function actionCreate()
{
$model=new Survey;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Survey']))
{
$model->attributes=$_POST['Survey'];
if($model->save()) {
$model->processQuestions($_POST);
$this->redirect(array('view','id'=>$model->id));
}
}
if(!$model->questions)
$model->addDefaultQuestions();
$this->render('create',array(
'model'=>$model,
));
}
public function processAnswers($answers) {
foreach($answers as $key => $value)
if(substr($key, 0, 6) == 'answer') {
$question = explode('_', $key);
$question = $question[1];
$answer = new Answer();
$answer->attend_id = $_POST['Answer']['attend_id'];
$answer->question_id = $question;
$answer->answer = $value;
$answer->save();
}
}
? $questions = $model->questions; ?>
<table id="questions" class="table table-striped">
<tr> <th> Frage </th> <th> Typ </th> <th> Optionen </th> </tr>
<? for($i = 0; $i <= 20; $i++) { ?>
<tr> <td style="width:70%">
<?= CHtml::textField('question_'.$i,
isset($questions[$i]) ? $questions[$i]->title : '', array(
'class'=>'span12',
'maxlength'=>255)); ?>
</td> <td>
<?= CHtml::dropDownList('type_'.$i,
isset($questions[$i]) ? $questions[$i]->type : '', array(
0 => 'Ja/Nein',
1 => 'Freitexteingabe',
2 => 'Wertigkeit')); ?>
</td> <td>
<?= CHtml::textField('options_'.$i,
isset($questions[$i]) ? $questions[$i]->option : '', array(
'class'=>'span12',
'maxlength'=>255)); ?>
</td></tr>
<? } ?>
</table>
@thyseus

thyseus commented Jun 10, 2013

Copy link
Copy Markdown
Author
public function processQuestions($questions) {
    foreach(Question::model()->findAll('survey_id = :id', array(
                    ':id' => $this->id)) as $question)
        $question->delete();

    foreach($questions as $key => $value) {
        if(substr($key, 0, 8) == 'question' && $value) {
            $pos = explode('_', $key);
            $pos = $pos[1];
            $question = new Question();
            $question->survey_id = $this->id;
            $question->title = $questions['question_'.$pos];
            $question->type = $questions['type_'.$pos];
            $question->option = $questions['options_'.$pos];
            $question->save();
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment