Skip to content

Commit

Permalink
Dev Updated function signatures to typehint against interface instead…
Browse files Browse the repository at this point in the history
… of \Response class.
  • Loading branch information
SamMousa committed Sep 1, 2015
1 parent 6c3630f commit e52c2cc
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 17 deletions.
2 changes: 1 addition & 1 deletion application/components/SurveySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property int $step;
* @property mixed $format;
* @property int $maxStep;
* @property Response $response;
* @property \ls\interfaces\iResponse $response;
* @property string $templateDir;
*/
class SurveySession extends CComponent {
Expand Down
3 changes: 2 additions & 1 deletion application/components/SurveySessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public function newSession($surveyId, \ls\interfaces\iResponse $response)
}
// Doesn't really need to be random
$sessionId = rand(1, 1000);
$this->_current = $this->sessions[$sessionId] = new SurveySession($surveyId, $response, $sessionId);
$this->_current = new SurveySession($surveyId, $response, $sessionId);
$this->sessions->add($sessionId, $this->_current);
return $this->current;
}

Expand Down
10 changes: 9 additions & 1 deletion application/controllers/SurveysController.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ public function actionDeactivateMultiple(array $ids) {

/**
* Renders the relevance and tailoring javascript for a survey.
* @todo Add caching headers.
*/
public function actionScript($id) {
$survey = $this->loadModel($id);
Expand All @@ -380,6 +379,15 @@ public function actionScript($id) {
echo $result;
}

public function actionPreview($id) {
$survey = $this->loadModel($id);
$dummy = new \DummyResponse($survey);
$session = App()->surveySessionManager->newSession($survey->primaryKey, $dummy);
return $this->redirect(['survey/index', 'SSM' => $session->getId()]);


}




Expand Down
5 changes: 4 additions & 1 deletion application/helpers/SurveyRuntimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,10 @@ protected function renderGroup(SurveySession $session, QuestionGroup $group) {
echo "\n\n<!-- PRESENT THE QUESTIONS -->\n";
if ($session->format != Survey::FORMAT_QUESTION) {
foreach ($group->questions as $question) {
echo $this->renderQuestion($session, $question);
vd($question->bool_hidden);
if (!$question->bool_hidden) {
echo $this->renderQuestion($session, $question);
}
}
} else {
echo $this->renderQuestion($session, $session->getQuestionByIndex($session->step));
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/expressions/em_manager_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4190,7 +4190,7 @@ public static function registerScripts(SurveySession $session)
$clientScript->registerScriptFile(SettingGlobal::get('generalscripts', '/scripts') . "/expressions/em_javascript.js");
$clientScript->registerScriptFile(App()->createUrl('surveys/script', ['id' => $session->surveyId]));
$values = [];
foreach ($session->response->attributes as $name => $value) {
foreach ($session->response->getAttributes() as $name => $value) {
if (isset($value) && strpos($name, 'X') !== false) {
$values[$name] = $value;
}
Expand Down
13 changes: 13 additions & 0 deletions application/interfaces/iResponse.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
namespace ls\interfaces;


/**
* Interface iResponse
* @package ls\interfaces
* Objects implementing this interface can be used to store response data.
* The default implementation store it in database (for live surveys)
* Or in session (for previews).
*/
interface iResponse {

/**
Expand All @@ -20,4 +28,9 @@ public function getToken();
*/
public static function loadById($id);


/**
* @return [] An array containing the response data.
*/
public function getAttributes();
}
13 changes: 12 additions & 1 deletion application/models/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* Extensions to the CActiveRecord class
*/
class ActiveRecord extends CActiveRecord
class ActiveRecord extends CActiveRecord implements JsonSerializable
{

/**
Expand Down Expand Up @@ -225,4 +225,15 @@ public function __sleep()
// TODO: Implement serialize() method.
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
function jsonSerialize()
{
return $this->attributes;
}
}
4 changes: 4 additions & 0 deletions application/models/DummyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public function save() {
public function getToken() {
return null;
}

public function getAttributes() {
return $this->fields;
}
}
25 changes: 21 additions & 4 deletions application/models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ public function rules()
['same_default','numerical', 'integerOnly'=>true,'allowEmpty'=>true],
/** @todo Create EM validator that validates syntax only. */
['relevance', 'safe'],
[['bool_mandatory', 'bool_other'], 'boolean'],
[['bool_mandatory', 'bool_other', 'bool_hidden'], 'boolean'],

];

$aRules[] = ['title', 'match', 'pattern' => '/^[a-z0-9]*$/i',
Expand Down Expand Up @@ -1066,7 +1067,7 @@ public function getVarName() {
* By default a question passes this if any of it's fields have been filled.
* @return boolean
*/
final public function validateResponse(Response $response) {
final public function validateResponse(\ls\interfaces\iResponse $response) {

$em = $this->getExpressionManager($response);
$result = new QuestionValidationResult($this);
Expand Down Expand Up @@ -1130,10 +1131,10 @@ public function getRelevanceScript() {
}
/**
* Checks if the question is relevant for the current response.
* @param Response $response
* @param \ls\interfaces\iResponse $response
* @return boolean
*/
public function isRelevant(Response $response) {
public function isRelevant(\ls\interfaces\iResponse $response) {
// Check if the group is relevant first.
if (!$this->group->isRelevant($response)) {
$result = false;
Expand Down Expand Up @@ -1329,6 +1330,22 @@ public function getArrayFilterStyleOptions()
];

}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
function jsonSerialize()
{
$result = parent::jsonSerialize();
$this->loadCustomAttributes();
// Merge, custom first so "real" attributes always override custom attributes.
return array_merge($this->customAttributes, $result);

}
}


2 changes: 1 addition & 1 deletion application/models/QuestionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function deleteDependent() {
}
}

public function isRelevant(Response $response) {
public function isRelevant(\ls\interfaces\iResponse $response) {
if (empty($this->grelevance)) {
$result = true;
} else {
Expand Down
5 changes: 3 additions & 2 deletions application/models/questions/FixedArrayQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function render(iResponse$response, \SurveySession $session)

foreach ($this->subQuestions as $subQuestion)
{
$relevance = strtr($this->getFilterExpression(), ['{VALUE}' => $subQuestion->question]);
// We explode to make sure we only get the left part.
$relevance = strtr($this->getFilterExpression(), ['{VALUE}' => explode('|', $subQuestion->question)[0]]);
$tableContent .= $this->renderSubQuestion($subQuestion, $response, $relevance, count($rightQuestions) > 0);
}

Expand All @@ -102,7 +103,7 @@ public function render(iResponse$response, \SurveySession $session)
return $result;
}

protected function renderSubQuestion(\Question $subQuestion, \Response $response, $relevance) {
protected function renderSubQuestion(\Question $subQuestion, iResponse $response, $relevance) {
bP();
$result = [];
$em = $this->getExpressionManager($response);
Expand Down
2 changes: 1 addition & 1 deletion application/models/questions/LanguageQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getAnswers($scale = null)
* @param Response $response
* @return boolean
*/
public function isRelevant(Response $response)
public function isRelevant(\ls\interfaces\iResponse $response)
{

$result = (count($this->survey->allLanguages) > 1) ? parent::isRelevant($response) : false;
Expand Down
5 changes: 4 additions & 1 deletion application/models/questions/MultipleChoiceQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public function renderSubQuestion(\Question $question, iResponse $response, \Sur
// Render a line in the multiple choice question.
$result = '';
$field = $this->sgqa . $question->title;
$result .= \TbHtml::checkBox($field, $response->$field == 'Y', ['id' => "answer$field"]);
$result .= \TbHtml::checkBox($field, $response->$field == 'Y', [
'id' => "answer$field",
'value' => 'Y'
]);
$result .= \TbHtml::label($question->question, "answer$field");
return $result;

Expand Down
15 changes: 14 additions & 1 deletion application/models/questions/SingleChoiceQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ public function getClasses()

}


/**
* 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 \ls\interfaces\Response $response
* @param \SurveySession $session
* @return \RenderedQuestion
*/
public function render(\ls\interfaces\iResponse $response, \SurveySession $session)
{
$result = parent::render($response, $session);
$result->setHtml('TODO');
return $result;
}


}
9 changes: 8 additions & 1 deletion application/views/global/surveyMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
'icon' => 'stop',
'disabled' => !$model->isActive
], [
'title' => gT('Execute survey.'),
'title' => gT('Execute survey'),
'icon' => 'certificate',
'disabled' => !$model->isActive || $model->isExpired || $model->bool_usetokens,
'linkOptions' => ['target' => '_blank'],
'url' => !$model->isActive || $model->isExpired ? '#' : ["surveys/start", 'id' => $model->sid]
], [
'title' => gT('Preview survey'),
'icon' => 'eye-open',
'linkOptions' => [
'target' => '_blank'
],
'url' => ["surveys/preview", 'id' => $model->primaryKey]
],[
'title' => gT('Survey settings'),
'icon' => 'wrench',
// 'disabled' => $model->responseCount == 0,
Expand Down
1 change: 1 addition & 0 deletions application/views/questions/update/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
echo $form->textFieldControlGroup($question, 'random_group');
}
echo $form->checkBoxControlGroup($question, 'bool_mandatory');
echo $form->checkBoxControlGroup($question, 'bool_hidden');
if ($question->hasAttribute('other_comment_mandatory')) {
echo $form->checkBoxControlGroup($question, 'bool_other_comment_mandatory');
}
Expand Down

0 comments on commit e52c2cc

Please sign in to comment.