Skip to content

Commit

Permalink
Dev: ArrayQuestionObject submit works
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Oct 7, 2016
1 parent e2690cb commit 8948b10
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 33 deletions.
132 changes: 132 additions & 0 deletions application/core/questions/ArrayQuestionObject/ArrayQuestionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,138 @@

Yii::import('application.core.questions.QuestionObjectBase', true);

/**
* Basic array question type
* @since 2016-10-06
* @author Olle Haerstedt
*/
class ArrayQuestionObject extends QuestionObjectBase
{
/**
* @return
*/
public function getFieldMap($fieldname)
{
$subQuestions = Question::model()->getSubQuestions($this->data['qid'])->readAll();
tracevar($fieldname);

$fieldnames = array();
foreach ($subQuestions as $subQuestion) {
$fieldnames[$fieldname . '_' . $subQuestion['title']] = array(
'fieldname' => $fieldname . '_' . $subQuestion['title'],
'sid' => $this->data['sid'],
'gid' => $this->data['gid'],
'qid' => $this->data['qid'],
'aid' => null,
'title' => $subQuestion['title'],
'question' => $subQuestion['question'],
'group_name' => 'group name',
'mandatory' => 'N',
'hasconditions' => 'N',
'usedinconditions' => 'N',
'questionSeq' => 0,
'groupSeq' => 0,
'relevance' => 1,
'grelevance' => 1,
'preg' => null,
'other' => 'N',
'help' => 'question help',
'type' => '?',
'extended_type' => 'ArrayQuestionObject',
'database' => 'string(5)'
);
}

return $fieldnames;
}

/**
*
* @return
*/
public function getAttributeNames()
{
// The 'hidden' attribute is mandatory, won't work without.
$attributeNames = array();
$attributeNames[] = ls\helpers\questionHelper::$attributes['hidden'];
$attributeNames[0]['i18n'] = false; // TODO: Why needed?
$attributeNames[0]['name'] = 'hidden'; // TODO: Why needed?
$attributeNames[0]['default'] = 0; // TODO: Why needed?
return $attributeNames;
}

/**
*
* @return
*/
public function getQuestionText()
{
$questionText = array(
'all' => '', // All has been added for backwards compatibility with templates that use question_start.pstpl (now redundant)
'text' => 'some text', // Question text (not answer)
'code' => $this->ia[2], // Question code
'number' => '', // ?
'help' => 'some help', // Not used? Question help is set in getQuestionReplacement and _ValidateQuestion.
'mandatory' => '', // HTML content of mandatory sign (*)
'man_message' => '', // HTML of mandatory message
'valid_message' => '', // HTML when question is not valid
'file_valid_message' => '', // Only for file upload?
'class' => '', // ?
'man_class' => '', // ?
'input_error_class' => '', // 'input-error' will show a red border
'essentials' => '' // ?
);
return $questionText;
}

/**
* @return string html
*/
public function getAnswer()
{
// List with comment 'O'
$answerOptions = $this->questionModel->getOrderedAnswers(0, 0);
$subQuestions = $this->questionModel->getSubQuestions()->readAll();

$answer = '<table>';
foreach ($subQuestions as $subQuestion) {
$answer .= '<tr>';
$answer .= '<td>';
$answer .= $subQuestion['question'];
$answer .= '</td>';
foreach ($answerOptions as $answerOption) {
$answer .= '<td>';
$answer .= $answerOption['answer'];
$answer .= '<input type="radio" name="' . $this->ia[1] . '_' . $subQuestion['title']. '" value="' . $answerOption['code'] . '" />';
$answer .= '</td>';
}
$answer .= '</tr>';
}
$answer .= '</table>';

return $answer;
}

/**
* All question codes for this question
* Called from qanda_helper.
* @return array
*/
public function getQuestionCodes()
{
$subQuestions = $this->questionModel->getSubQuestions()->readAll();
$codes = array();
foreach ($subQuestions as $subQuestion) {
$codes[] = $this->ia[1] . '_' . $subQuestion['title'];
}
return $codes;
}

public function getDatabaseFieldTypes(array $row)
{
return array(
$row['fieldname'] => $row['database']
);
}

}
30 changes: 25 additions & 5 deletions application/core/questions/QuestionObjectBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ abstract class QuestionObjectBase
* @var array
* @todo Rename to a more descriptive name
*/
private $ia;
protected $ia;

/**
* List of attributes is in questionHelper.
* @var array
*/
private $questionAttributes;
protected $questionAttributes;

/**
* @var Question
*/
private $questionModel;
protected $questionModel;

/**
* Data used to create fieldmap
* @var array
*/
private $data;
protected $data;

/**
* @var TestQuestionObject
*/
static private $instance = null;
static protected $instance = null;

/**
* @return TestQuestionObject
Expand Down Expand Up @@ -64,4 +64,24 @@ public function setIa(array $ia)
{
$this->ia = $ia;
}

/**
* Called from qanda_helper.
* @param array $questionAttributes
* @return void
*/
public function setQuestionAttributes(array $questionAttributes)
{
$this->questionAttributes = $questionAttributes;
}

/**
* Called from qanda_helper.
* @param Question $questionModel
* @return void
*/
public function setQuestionModel(Question $questionModel)
{
$this->questionModel = $questionModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,6 @@ public function getDatabaseFieldTypes(array $row)
);
}

/**
* Called from qanda_helper.
* @param array $questionAttributes
* @return void
*/
public function setQuestionAttributes(array $questionAttributes)
{
$this->questionAttributes = $questionAttributes;
}

/**
* Called from qanda_helper.
* @param Question $questionModel
* @return void
*/
public function setQuestionModel(Question $questionModel)
{
$this->questionModel = $questionModel;
}
/**
* Called from createFieldMap
* Array
Expand Down
4 changes: 2 additions & 2 deletions application/helpers/qanda_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ function retrieveAnswers($ia)
$ia[1] // Fieldname
);

traceVar($ia);
traceVar($question_text);
//traceVar($ia);
//traceVar($question_text);
//traceVar(array($qanda, $inputnames));

return array($qanda, $inputnames);
Expand Down
20 changes: 13 additions & 7 deletions application/models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,22 @@ function getQuestions($sid, $gid, $language)
/**
* This function is only called from database.php
* TODO : create a relation to self called subquestion
* @param int|null $parent_qid
* @return
*/
function getSubQuestions($parent_qid)
function getSubQuestions($parent_qid = null)
{
if ($parent_qid === null) {
$parent_qid = $this->qid;
}

return Yii::app()->db->createCommand()
->select()
->from(self::tableName())
->where('parent_qid=:parent_qid')
->bindParam(":parent_qid", $parent_qid, PDO::PARAM_INT)
->order('question_order asc')
->query();
->select()
->from(self::tableName())
->where('parent_qid=:parent_qid')
->bindParam(":parent_qid", $parent_qid, PDO::PARAM_INT)
->order('question_order asc')
->query();
}

/**
Expand Down

0 comments on commit 8948b10

Please sign in to comment.