Skip to content

Commit

Permalink
Several syntax fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed May 22, 2015
1 parent 202e51d commit eda81ce
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 67 deletions.
14 changes: 9 additions & 5 deletions application/controllers/QuestionsController.php
@@ -1,6 +1,7 @@
<?php
namespace ls\controllers;
use ls\models\forms\SubQuestions;
use ls\models\questions\SubQuestion;
use \Yii;
class QuestionsController extends Controller
{
Expand Down Expand Up @@ -51,18 +52,21 @@ public function actionUpdate($id) {
$question->answers = $answers;
}
$subQuestions = [];
if ($question->hasSubQuestions && App()->request->getParam('SubQuestion', false) !== false) {
if ($question->hasSubQuestions && App()->request->getParam(\CHtml::modelName(SubQuestion::class), false) !== false) {

// Remove all subquestions.
// Create new ones.
$codes = [];
foreach(App()->request->getParam('SubQuestion') as $i => $data) {
$subQuestion = new \Question();
$subQuestion->question_id = $question->qid;
foreach(App()->request->getParam(\CHtml::modelName(SubQuestion::class)) as $i => $data) {
$subQuestion = new SubQuestion();
$subQuestion->parent_qid = $question->qid;
$subQuestion->gid = $question->gid;
$subQuestion->sid = $question->sid;
$subQuestion->setAttributes($data);
$subQuestion->sortorder = $i;
$subQuestion->question_order = $i;
$subQuestions[] = $subQuestion;
$error = $error || !$subQuestion->validate();

/**
* @todo Find a better solution for this manual validation.
*/
Expand Down
46 changes: 23 additions & 23 deletions application/helpers/common_helper.php
Expand Up @@ -1828,7 +1828,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
], ['order' => 'group_order']);

/** @var QuestionGroup $group */
$i = 0;
$questionSeq = 0;
foreach($groups as $groupSeq => $group) {
foreach ($group->questions as $question) {
$fieldname = $question->getSgqa();
Expand Down Expand Up @@ -1869,7 +1869,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
$fieldmap[$fieldname]['mandatory'] = $question->mandatory;
$fieldmap[$fieldname]['hasconditions'] = $conditions;
$fieldmap[$fieldname]['usedinconditions'] = $usedinconditions;
$fieldmap[$fieldname]['questionSeq'] = $i;
$fieldmap[$fieldname]['questionSeq'] = $questionSeq;
$fieldmap[$fieldname]['groupSeq'] = $groupSeq;
if (isset($defaultValues[$question->primaryKey . '~0'])) {
$fieldmap[$fieldname]['defaultvalue'] = $defaultValues[$question->primaryKey . '~0'];
Expand Down Expand Up @@ -1940,7 +1940,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
$fieldmap[$fieldname]['mandatory'] = $question->mandatory;
$fieldmap[$fieldname]['hasconditions'] = $conditions;
$fieldmap[$fieldname]['usedinconditions'] = $usedinconditions;
$fieldmap[$fieldname]['questionSeq'] = $i;
$fieldmap[$fieldname]['questionSeq'] = $questionSeq;
$fieldmap[$fieldname]['groupSeq'] = $groupSeq;
}
break;
Expand Down Expand Up @@ -2074,7 +2074,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
$data = count($data);
$slots = $data;
for ($i = 1; $i <= $slots; $i++) {
$fieldname = "{$question->sid}X{$arow['gid']}X{$question->qid}$i";
$fieldname = "{$question->sgqa}$i";
if (isset($fieldmap[$fieldname])) {
$aDuplicateQIDs[$question->qid] = array(
'fieldname' => $fieldname,
Expand All @@ -2086,8 +2086,8 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
"fieldname" => $fieldname,
'type' => $question->type,
'sid' => $surveyid,
"gid" => $arow['gid'],
"qid" =>$question->qid,
"gid" => $question->gid,
"qid" => $question->qid,
"aid" => $i
);
if ($style == "full") {
Expand All @@ -2103,7 +2103,7 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
}
}
} elseif ($question->type == "|") {
$qidattributes = getQuestionAttributeValues($arow['qid']);
$qidattributes = getQuestionAttributeValues($question->qid);
$fieldname = "{$arow['sid']}X{$arow['gid']}X{$arow['qid']}";
$fieldmap[$fieldname] = array(
"fieldname" => $fieldname,
Expand Down Expand Up @@ -2148,39 +2148,39 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
//MULTI ENTRY
$abrows = getSubQuestions($surveyid,$question->qid, $sLanguage);
foreach ($abrows as $abrow) {
$fieldname = "{$arow['sid']}X{$arow['gid']}X{$arow['qid']}{$abrow['title']}";
$fieldname = "{$question->sgqa}{$abrow['title']}";

if (isset($fieldmap[$fieldname])) {
$aDuplicateQIDs[$arow['qid']] = array(
$aDuplicateQIDs[$question->qid] = array(
'fieldname' => $fieldname,
'question' => $arow['question'],
'gid' => $arow['gid']
'question' => $question->question,
'gid' => $question->gid
);
}
$fieldmap[$fieldname] = array(
"fieldname" => $fieldname,
'type' => $question->type,
'sid' => $surveyid,
'gid' => $arow['gid'],
'qid' =>$question->qid,
'sid' => $question->sid,
'gid' => $question->gid,
'qid' => $question->qid,
'aid' => $abrow['title'],
'sqid' => $abrow['qid']
);
if ($style == "full") {
$fieldmap[$fieldname]['title'] = $arow['title'];
$fieldmap[$fieldname]['question'] = $arow['question'];
$fieldmap[$fieldname]['title'] = $question->title;
$fieldmap[$fieldname]['question'] = $question->question;
$fieldmap[$fieldname]['subquestion'] = $abrow['question'];
$fieldmap[$fieldname]['group_name'] = $arow['group_name'];
$fieldmap[$fieldname]['mandatory'] = $arow['mandatory'];
$fieldmap[$fieldname]['group_name'] = $question->group->title;
$fieldmap[$fieldname]['mandatory'] = $question->mandatory;
$fieldmap[$fieldname]['hasconditions'] = $conditions;
$fieldmap[$fieldname]['usedinconditions'] = $usedinconditions;
$fieldmap[$fieldname]['questionSeq'] = $questionSeq;
$fieldmap[$fieldname]['groupSeq'] = $groupSeq;
$fieldmap[$fieldname]['preg'] = $arow['preg'];
$fieldmap[$fieldname]['preg'] = $question->preg;
// get SQrelevance from DB
$fieldmap[$fieldname]['SQrelevance'] = $abrow['relevance'];
if (isset($defaultValues[$arow['qid'] . '~' . $abrow['qid']])) {
$fieldmap[$fieldname]['defaultvalue'] = $defaultValues[$arow['qid'] . '~' . $abrow['qid']];
if (isset($defaultValues[$question->qid . '~' . $abrow['qid']])) {
$fieldmap[$fieldname]['defaultvalue'] = $defaultValues[$question->qid . '~' . $abrow['qid']];
}
}
if ($question->type == "P") {
Expand Down Expand Up @@ -2278,13 +2278,13 @@ function createFieldMap($surveyid, $style='short', $force_refresh=false, $questi
//set question relevance (uses last SQ's relevance field for question relevance)
$fieldmap[$fieldname]['relevance'] = $question->relevance;
$fieldmap[$fieldname]['grelevance'] = $group->grelevance;
$fieldmap[$fieldname]['questionSeq'] = $i;
$fieldmap[$fieldname]['questionSeq'] = $questionSeq;
$fieldmap[$fieldname]['groupSeq'] = $groupSeq;
$fieldmap[$fieldname]['preg'] = $question->preg;
$fieldmap[$fieldname]['other'] = $question->other;
$fieldmap[$fieldname]['help'] = $question->help;
}
$i++;
$questionSeq++;

}
}
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/expressions/em_manager_helper.php
Expand Up @@ -5905,7 +5905,7 @@ function _ValidateQuestion($questionSeq,$force=false)
$LEM =& $this;
$qInfo = $LEM->questionSeq2relevance[$questionSeq]; // this array is by group and question sequence

$qrel=true; // assume relevant unless discover otherwise
$qrel=true; // assume relevant unless discover otherwise
$prettyPrintRelEqn=''; // assume no relevance eqn by default
$qid=$qInfo['qid'];

Expand Down
61 changes: 34 additions & 27 deletions application/models/Question.php
Expand Up @@ -181,7 +181,6 @@ public function rules()
['question_order', 'numerical', 'integerOnly' => true, 'allowEmpty' => true],
['scale_id','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
['same_default','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
['question', 'length', 'min' => 1, 'allowEmpty' => false]
];

$aRules[] = ['title', 'match', 'pattern' => '/^[a-z0-9]*$/i',
Expand Down Expand Up @@ -689,6 +688,7 @@ public function getColumns() {
case "O": //DROPDOWN LIST WITH COMMENT
$result = [$this->sgqa => "string(5)", "{$this->sgqa}comment" => "text"];
break;
case "F": // Array
case "R": // Ranking
case "M": //Multiple choice
case "Q": //Multiple short text
Expand Down Expand Up @@ -759,34 +759,41 @@ protected function instantiate($attributes) {
if (!isset($attributes['type'])) {
throw new \Exception('noo');
}
switch($attributes['type']) {
case 'N':
$class = \ls\models\questions\NumericalQuestion::class;
break;
case 'U': // Huge free text
case 'S': // Short free text
case 'T':
$class = \ls\models\questions\TextQuestion::class;
break;
case 'O': // Single choice with comments.
case '!': // Single choice dropdown.
case 'L': // Single choice (Radio);
$class = \ls\models\questions\SingleChoiceQuestion::class;
break;
case 'Q': // Multiple (short) text.
$class = \ls\models\questions\MultipleTextQuestion::class;
break;
case 'R': // Ranking
$class = \ls\models\questions\RankingQuestion::class;
break;
case '|':

if (!empty($attributes['parent_qid'])) {
$class = \ls\models\questions\SubQuestion::class;
} else {
switch ($attributes['type']) {
case 'N':
$class = \ls\models\questions\NumericalQuestion::class;
break;
case 'U': // Huge free text
case 'S': // Short free text
case 'T':
$class = \ls\models\questions\TextQuestion::class;
break;
case 'O': // Single choice with comments.
case '!': // Single choice dropdown.
case 'L': // Single choice (Radio);
$class = \ls\models\questions\SingleChoiceQuestion::class;
break;
case 'Q': // Multiple (short) text.
$class = \ls\models\questions\MultipleTextQuestion::class;
break;
case 'R': // Ranking
$class = \ls\models\questions\RankingQuestion::class;
break;
case 'F': // Array
$class = \ls\models\questions\ArrayQuestion::class;
break;
case '5': // 5 point choice
case '|':
$class = get_class($this);
break;
default:
die("noo class for type {$attributes['type']}");

default:
// die("noo class for type {$attributes['type']}");
$class = get_class($this);
}
}

return new $class(null);
}

Expand Down
30 changes: 30 additions & 0 deletions application/models/questions/ArrayQuestion.php
@@ -0,0 +1,30 @@
<?php
namespace ls\models\questions;

/**
* Class ArrayQuestion
* @package ls\models\questions
*/
class ArrayQuestion extends \Question
{
public function getHasAnswers()
{
return true;
}

public function getHasSubQuestions()
{
return true;
}


public function relations()
{
return array_merge(parent::relations(), [
'answers' => [self::HAS_MANY, \Answer::class, 'question_id', 'order' => 'sortorder', 'index' => 'code']
]);
}



}
14 changes: 14 additions & 0 deletions application/models/questions/SubQuestion.php
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: sam
* Date: 5/22/15
* Time: 4:44 PM
*/

namespace ls\models\questions;


class SubQuestion extends \Question {

}
29 changes: 19 additions & 10 deletions application/views/questions/create.php
Expand Up @@ -3,15 +3,24 @@
/**
* @var Question $question;
*/
// This is an update view so we use PUT.
echo TbHtml::beginFormTb(TbHtml::FORM_LAYOUT_HORIZONTAL, ['questions/create', 'groupId' => $question->gid], 'post', []);
$options = ['formLayout' => TbHtml::FORM_LAYOUT_HORIZONTAL];
// This is a create view so we use POST.
/** @var TbActiveForm $form */
$form = $this->beginWidget(TbActiveForm::class, [
'enableAjaxValidation' => false,
'enableClientValidation' => true,
'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL,
'action' => ['questions/create', 'groupId' => $question->gid],
'method' => 'post',
'htmlOptions' => [
'validateOnSubmit' => true
]
]);
echo TbHtml::openTag('fieldset', []);
echo TbHtml::activeTextFieldControlGroup($question, 'title', array_merge($options, [
echo $form->textFieldControlGroup($question, 'title', [
'help' => "This is a suggestion based on the previous question title, feel free to change it."
]));
]);
if (!empty($question->group->questions)) {
echo TbHtml::customActiveControlGroup($this->widget(WhSelect2::class, [
echo $form->customControlGroup($this->widget(WhSelect2::class, [
'data' => CHtml::listData($question->group->questions, 'qid', function (Question $question) {
return gT("Before") . ' ' . $question->displayLabel;
}),
Expand All @@ -20,7 +29,7 @@
'htmlOptions' => [
'empty' => 'At end'
]
], true), $question, 'after', $options);
], true), $question, 'after');
}
$questionTypeOptions = CHtml::listData($question->typeList(), 'type', function($details) {
return [
Expand All @@ -31,15 +40,15 @@
'*' => 'EQUATION'
]) . '.png'];
});
echo TbHtml::customActiveControlGroup($this->widget(WhSelect2::class, [
echo $form->customControlGroup($this->widget(WhSelect2::class, [
'data' => CHtml::listData($question->typeList(), 'type', 'description', 'group'),
'model' => $question,
'htmlOptions' => [
'key' => 'type',
'options' => $questionTypeOptions,
],
'attribute' => 'type'
], true), $question, 'type', $options);
], true), $question, 'type');
App()->clientScript->registerScript('test', "
$('#Question_type').on('select2-close', function() {
$('#preview').attr('src', $(this).find(':selected').data('preview'));
Expand All @@ -55,7 +64,7 @@
]);
echo TbHtml::closeTag('div');
echo TbHtml::closeTag('fieldset');
echo TbHtml::endForm();
$this->endWidget();
?>
</div>
<img class="col-md-6" id="preview" src="<?= $questionTypeOptions[$question->type]['data-preview']?>">
Expand Down
3 changes: 2 additions & 1 deletion application/views/questions/update/subQuestionTab.php
Expand Up @@ -19,7 +19,8 @@
echo TbHtml::openTag('div', ['class' => 'sortable']);
$i = 0;
if (empty($question->subQuestions)) {
$subQuestion = new Question();
$subQuestion = new \ls\models\questions\SubQuestion();
$subQuestion->title = "SQ001";
$subQuestion->parent_qid = $question->primaryKey;
$subQuestions = [$subQuestion];
} else {
Expand Down

0 comments on commit eda81ce

Please sign in to comment.