Skip to content

Commit

Permalink
Dev Implemented first renderable questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Aug 21, 2015
1 parent 1cac3ca commit 5b90968
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 476 deletions.
2 changes: 1 addition & 1 deletion application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -854,7 +854,7 @@ protected function renderQuestion(SurveySession $session, Question $question) {
$aReplacement = [];
$question_template = file_get_contents($session->templateDir .'question.pstpl');

list($plus_qanda, $plus_inputnames) = retrieveAnswers($question);
$plus_qanda = retrieveAnswers($question);
$plus_qanda[] = $question->type;
$plus_qanda[] = $question->bool_mandatory;
$plus_qanda['finalgroup'] = $question->gid;
Expand Down
773 changes: 323 additions & 450 deletions application/helpers/qanda_helper.php

Large diffs are not rendered by default.

57 changes: 46 additions & 11 deletions application/models/Question.php
Expand Up @@ -69,7 +69,7 @@ class Question extends ActiveRecord
* @var ResponseField[]
*/
protected $_fields = [];
protected $customAttributes = [];
protected $customAttributes;
protected $customLocalizedAttributes = [];

/**
Expand All @@ -78,24 +78,37 @@ class Question extends ActiveRecord
*/
public $before;

protected function loadCustomAttributes() {
if (!isset($this->customAttributeNames)) {
// Fill the question attributes.
foreach($this->questionAttributes as $questionAttribute) {
if (!isset($questionAttribute->language)) {
$this->customAttributes[$questionAttribute->attribute] = $questionAttribute->value;
}
}
}
}
protected function getCustomAttribute($name) {
$this->loadCustomAttributes();
if (array_key_exists($name, $this->customAttributes)) {
return $this->customAttributes[$name];
}

// Fill the question attributes.
foreach($this->questionAttributes as $questionAttribute) {
if (!isset($questionAttribute->language) && $questionAttribute->attribute === $name) {
$this->customAttributes[$name] = $questionAttribute->value;
return $questionAttribute->value;
}
}

// Get default value.
$config = questionAttributes(true)[$name];
return isset($config['default']) ? $config['default'] : null;
}

protected function setCustomAttribute($name, $value) {
$this->loadCustomAttributes();
$this->customAttributes[$name] = $value;
}

protected function issetCustomAttribute($name) {
$this->loadCustomAttributes();
return isset($this->customAttributes[$name]);
}

protected function getCustomLocalizedAttribute($name) {
if (array_key_exists($name, $this->customLocalizedAttributes)) {
return $this->customLocalizedAttributes[$name];
Expand All @@ -122,6 +135,11 @@ protected function afterSave()

}

/**
* Save the advanced question attributes.
* @throws CDbException
* @throws Exception
*/
protected function updateAttributes() {
// Save the question attributes that do not use i18n.
$db = self::getDbConnection();
Expand Down Expand Up @@ -272,12 +290,23 @@ public function __get($name)
return $result;
}

public function __isset($name)
{
if(in_array($name, $this->customAttributeNames())) {
$result = $this->issetCustomAttribute($name);
} else {
$result = parent::__isset($name);
}
return $result;
}


public function __set($name, $value)
{
if (substr($name, 0, 5) == 'bool_') {
$this->__set(substr($name, 5), $value ? 'Y' : 'N');
} elseif (in_array($name, $this->customAttributeNames())) {
$this->customAttributes[$name] = $value;
$this->setCustomAttribute($name, $value);

} else {
parent::__set($name, $value);
Expand Down Expand Up @@ -828,8 +857,14 @@ public static function resolveClass($type) {
$class = \ls\models\questions\DateTimeQuestion::class;
break;
case self::TYPE_HUGE_TEXT:
case self::TYPE_SHORT_TEXT:
$class = \ls\models\questions\HugeTextQuestion::class;
break;
case self::TYPE_LONG_TEXT:
$class = \ls\models\questions\LongTextQuestion::class;
break;

case self::TYPE_SHORT_TEXT:

$class = \ls\models\questions\TextQuestion::class;
break;
case self::TYPE_LIST_WITH_COMMENT:
Expand Down
37 changes: 37 additions & 0 deletions application/models/questions/HugeTextQuestion.php
@@ -0,0 +1,37 @@
<?php
/**
* Created by PhpStorm.
* User: sam
* Date: 8/21/15
* Time: 10:13 AM
*/

namespace ls\models\questions;


use ls\interfaces\iRenderable;

class HugeTextQuestion extends LongTextQuestion implements iRenderable
{
/**
* This function renders the object.
* It MUST NOT produce any output.
* It should return a string or an object that can be converted to string.
* @param Response $response
* @param \SurveySession $session
* @return string
*/
public function render(\Response $response, \SurveySession $session)
{
if (!isset($this->text_input_width)) {
$this->text_input_width = 70;
}

if (!isset($this->display_rows)) {
$this->display_rows = 70;
}
return parent::render($response, $session);
}


}
58 changes: 58 additions & 0 deletions application/models/questions/LongTextQuestion.php
@@ -0,0 +1,58 @@
<?php
/**
* Created by PhpStorm.
* User: sam
* Date: 8/21/15
* Time: 10:15 AM
*/

namespace ls\models\questions;


use ls\interfaces\iRenderable;

class LongTextQuestion extends TextQuestion implements iRenderable
{
/**
* This function renders the object.
* It MUST NOT produce any output.
* It should return a string or an object that can be converted to string.
* @param Response $response
* @param \SurveySession $session
* @return string
*/
public function render(\Response $response, \SurveySession $session)
{
$classes = ['question', 'answer-item', 'text-item'];
if ($this->maximum_chars > 0 )
{
// Only maxlength attribute, use textarea[maxlength] jquery selector for textarea
$maxlength= "maxlength='{$this->maximum_chars}' ";
$classes[] = "maxchars";
$classes[] = "maxchars-" . $this->maximum_chars;
}

$rows = isset($this->display_rows) ? $this->display_rows : 5;

if (isset($this->text_input_width)) {
$classes[] = "inputwidth-" . $this->text_input_width;
$width = $this->text_input_width;
} else {
$width = 40;
}

$html = \TbHtml::tag('p', ['class' => implode(' ', $classes)],
\TbHtml::label(gT('Your answer'), "answer{$this->sgqa}")
. \TbHtml::textArea($this->sgqa, $response->{$this->sgqa}, [
'class' => 'textarea',
'rows' => $rows,
'cols' => $width,
'maxlength' => isset($this->maximum_chars) ? $this->maximum_chars : 0,
'id' => "answer{$this->sgqa}"

])
);

return $html;
}
}
2 changes: 1 addition & 1 deletion application/models/questions/TextQuestion.php
Expand Up @@ -6,7 +6,7 @@
* "Long free text"
* @package ls\models\questions
*/
class TextQuestion extends \Question {
class TextQuestion extends \Question{
/**
* This function return the class by question type
* @param string question type
Expand Down
2 changes: 2 additions & 0 deletions application/views/questions/update/answerTab.php
Expand Up @@ -32,6 +32,7 @@
if (empty($answers)) {
$answer = new Answer();
$answer->code = $question->answerScales == 1 ? "A1" : ($scale == 0 ? "L1" : "R1");
$answer->scale_id = $scale;
$answer->question_id = $question->primaryKey;
$answers = [$answer];
}
Expand All @@ -49,6 +50,7 @@
* @todo Develop something proper that uses a collection model and validates that model.
*/
$validators[] = "for (var key in values) { if (values[key] !== elem && values[key].value == value) return '$message'; } return true;";
echo $form->hiddenField($answer, "[{$i}]scale_id");
echo $form->textField($answer, "[{$i}]code", array_merge([
'class' => 'col-sm-1 code',
], \SamIT\Form\FormHelper::createAttributesForInput($validators)));
Expand Down
1 change: 1 addition & 0 deletions application/views/questions/update/subQuestionTab.php
Expand Up @@ -37,6 +37,7 @@
}
// Take scales into account.
foreach ($subQuestions as $subQuestion) {
// vdd($subQuestion);
$subQuestion->language = $language;
$attribute = "[{$i}]title";
echo TbHtml::openTag('div', array_merge(['class' => 'form-group', 'data-index' => $i, 'data-scale' => $scale],
Expand Down
13 changes: 8 additions & 5 deletions application/views/questions/update/subquestions.php
Expand Up @@ -24,8 +24,8 @@
if ($question->subQuestionScales == 1) {
echo TbHtml::button('Add question', ['class' => 'addquestion', 'data-scale' => 0]);
} else {
echo TbHtml::button('Add X question', ['class' => 'addquestion', 'data-scale' => 0]);
echo TbHtml::button('Add Y question', ['class' => 'addquestion', 'data-scale' => 1]);
echo TbHtml::button('Add Y question', ['class' => 'addquestion', 'data-scale' => 0]);
echo TbHtml::button('Add X question', ['class' => 'addquestion', 'data-scale' => 1]);
}

/**
Expand Down Expand Up @@ -106,8 +106,9 @@ function renumber(elements) {
var $subQuestionTab = $('#subQuestionTab');
$subQuestionTab.on('change', '.code', function (e) {
var i = $(this).closest('.form-group').attr('data-index');
var scale =$(this).closest('.form-group').attr('data-scale');
// Update the other inputs.
$subQuestionTab.find('.form-group[data-index=' + i + '] .code').val($(this).val());
$subQuestionTab.find('.form-group[data-index=' + i + '][data-scale=' + scale + '] .code').val($(this).val());
});

/**
Expand All @@ -118,7 +119,8 @@ function renumber(elements) {
$this = $(this);
$this.find('.form-group').each(function(i, elem) {
var index = $(elem).attr('data-index');
$subQuestionTab.find('.sortable').not($this).find('.form-group[data-index=' + index + ']').each(function(j, group) {
var scale = $(elem).attr('data-scale');
$subQuestionTab.find('.sortable').not($this).find('.form-group[data-index=' + index + '][data-scale=' + scale + ']').each(function(j, group) {
var $group = $(group);
$group.appendTo($group.parent());
});
Expand All @@ -136,7 +138,8 @@ function renumber(elements) {
*/
$subQuestionTab.on('click', 'a.remove', function(e) {
var index = $(this).closest('.form-group').attr('data-index');
$subQuestionTab.find('.form-group[data-index="' + index + '"]').remove();
var scale = $(this).closest('.form-group').attr('data-scale');
$subQuestionTab.find('.form-group[data-index="' + index + '"][data-scale=' + scale + ']').remove();
renumber($subQuestionTab.find('.sortable'));
});
});
Expand Down
10 changes: 2 additions & 8 deletions composer.json
Expand Up @@ -48,25 +48,19 @@
"application/helpers"
],
"psr-4" : {
"ls\\migrations\\" : [
"application/migrations"
"ls\\" : [
"application"
],
"ls\\cli\\" : [
"application/commands"
],
"ls\\controllers\\" : [
"application/controllers"
],
"ls\\expressionmanager\\" : [
"application/libraries/ExpressionManager"
],
"ls\\pluginmanager\\" : [
"application/libraries/PluginManager",
"application/libraries/PluginManager/Storage"
],
"ls\\models\\" : [
"application/models"
],
"ls\\import\\" : [
"application/helpers/import"
]
Expand Down

0 comments on commit 5b90968

Please sign in to comment.