Skip to content

Commit

Permalink
Dev Major refactoring of em_manager_helper and related code.
Browse files Browse the repository at this point in the history
Dev Removed a lot of bad pratices and "caching". Code is currently a lot slower but cleaner.
Dev Currently still some bugs in survey taking, this push is primarily so the work is save.
  • Loading branch information
SamMousa committed Jul 16, 2015
1 parent ce0bb95 commit 8696136
Show file tree
Hide file tree
Showing 24 changed files with 5,757 additions and 6,415 deletions.
147 changes: 134 additions & 13 deletions application/components/SurveySession.php
Expand Up @@ -14,8 +14,10 @@
* @property string $language
* @property Survey $survey;
* @property int $step;
* @property mixed $format;
* @property int $maxStep;
* @property Response $response;
* @property string $templateDir;
*/
class SurveySession extends CComponent {
/**
Expand All @@ -37,10 +39,14 @@ class SurveySession extends CComponent {
protected $id;
protected $responseId;
protected $finished = false;
protected $language = 'en';

protected $_language = 'en';
protected $_prevStep = 0;
protected $_step = 0;
protected $_maxStep = 0;
protected $postKey;
protected $_templateDir;
protected $_postKey;
protected $_token;
/**
* @param int $surveyId
* @param int $responseId
Expand All @@ -50,6 +56,7 @@ public function __construct($surveyId, $responseId, $id)
$this->surveyId = $surveyId;
$this->responseId = $responseId;
$this->id = $id;
$this->token = isset($this->response->token) ? $this->response->token : null;
}

public function getSurveyId() {
Expand All @@ -64,8 +71,19 @@ public function getIsFinished() {
return $this->finished;
}

public function getToken() {
return $this->_token;
}

public function setToken($value) {
$this->_token = $value;
}
public function getLanguage() {
return $this->language;
return $this->_language;
}

public function setLanguage($value) {
$this->_language = $value;
}
/**
* Returns the session id for this session.
Expand All @@ -89,25 +107,84 @@ public function getResponse() {
*/
public function getSurvey() {
if (!isset($this->_survey)) {
$this->_survey = $survey = Survey::model()->with('groups.questions')->findByPk($this->surveyId);
/** @var Survey $survey */
\Yii::trace('ok123');
/**
* We greedily load questions via groups.
*/
$this->_survey = $survey = Survey::model()->with([
'groups' => [
'with' => [
'questions' => [
'with' => [
'answers',
'questionAttributes'
]
]
]
]
])->findByPk($this->surveyId);
/**
* We manually set the questions in survey to the same objects as those in groups.
* Note that the $survey->questions relation is redundant.
*/
$questions = [];
foreach($survey->groups as $group) {
foreach($group->questions as $question) {
$questions[$question->qid] = $question;
foreach($group->questions as $key => $question) {
$questions[$key] = $question;
}
}

$survey->questions = $questions;

/**
* Manually set the group count.
*/
$survey->groupCount = count($survey->groups);
}
return $this->_survey;
}

/**
* Wrapper function that returns the question given by qid to make sure we always get the same object.
* @param int $id
* @param int $id The primary key of the question.
* @return Question
*/
public function getQuestion($id) {
return $this->_survey->questions[$id];

return isset($this->survey->questions[$id]) ? $this->survey->questions[$id] : null;
}

public function getQuestionIndex($id) {
\Yii::beginProfile(__CLASS__ . "::" . __FUNCTION__);
$questions = $this->survey->questions;
$question = $questions[$id];
$result = array_search($question, array_values($questions), true);
\Yii::endProfile(__CLASS__ . "::" . __FUNCTION__);
return $result;
}

public function getQuestionByIndex($index) {
return array_values($this->survey->questions)[$index];
}

public function getStepCount() {
switch ($this->format) {
case Survey::FORMAT_ALL_IN_ONE:
$result = 1;
break;
case Survey::FORMAT_GROUP:
$result = count($this->getGroups());
break;
case Survey::FORMAT_QUESTION:
$result = array_sum(array_map(function(QuestionGroup $group) {
return count($this->getQuestions($group));
}));
break;
default:
throw new \Exception("Unknown survey format.");
}
return $result;
}
/**
* @return int
Expand All @@ -121,6 +198,7 @@ public function setStep($value) {
throw new \BadMethodCallException('Parameter $value must be an integer.');
}
$this->_step = $value > 0 ? $value : 0;
$this->_prevStep = $this->_step;
$this->_maxStep = max($this->_step, $this->_maxStep);
}

Expand All @@ -130,7 +208,11 @@ public function getMaxStep() {
}

public function getPrevStep() {
return $this->_step > 1 ? $this->_step - 1 : 1; // 0 ?
return $this->_prevStep;
}

public function setPrevStep($value) {
$this->_prevStep = $value;
}

public function __sleep() {
Expand All @@ -139,16 +221,45 @@ public function __sleep() {
'id',
'_step',
'_maxStep',
'_prevStep',
'responseId',
'finished',
'language',
'postKey'
'_language',
'_postKey',
'_token'
];
}

/**
* Sets the template dir to use for this session.
* @param string $value
*/
public function setTemplateDir($value) {
$this->_templateDir = $value;
}

public function getTemplateDir() {
if (!isset($this->_templateDir)) {
$this->_templateDir = \Template::getTemplatePath($this->survey->template) . '/';
};
return $this->_templateDir;
}

public function getGroup($id) {
return $this->survey->groups[$id];
}
public function getGroupIndex($id) {
\Yii::beginProfile(__CLASS__ . "::" . __FUNCTION__);
$groups = $this->groups;
$group = $this->getGroup($id);
$result = array_search($group, array_values($groups), true);
\Yii::endProfile(__CLASS__ . "::" . __FUNCTION__);
return $result;
}
/**
* Returns the list of question groups.
* Ordered according to the randomization groups.
* @return QuestionGroup[]
*/
public function getGroups()
{
Expand Down Expand Up @@ -197,6 +308,13 @@ public function getFormat() {
return $this->survey->format;
}



/**
* Returns the questions in group $group, indexed by primary key.
* @param QuestionGroup $group
* @return Question[]
*/
public function getQuestions(QuestionGroup $group) {
return $group->questions;

Expand Down Expand Up @@ -314,10 +432,13 @@ public function getFieldArray() {
}

public function getPostKey() {
return $this->postKey;
if (!isset($this->_postKey)) {
$this->_postKey = \Cake\Utility\Text::uuid();
}
return $this->_postKey;
}
public function setPostKey($value) {
$this->postKey = $value;
$this->_postKey = $value;
}


Expand Down
2 changes: 1 addition & 1 deletion application/config/third_party.php
Expand Up @@ -139,7 +139,7 @@
)
),
'jqueryui-timepicker' => array(
'baseUrl' => $bowerAssetPath . '/jqueryui-timepicker-addon/',
'baseUrl' => $bowerAssetPath . '/jqueryui-timepicker-addon/dist/',
'js' => array(
'jquery-ui-timepicker-addon.js'
),
Expand Down
16 changes: 16 additions & 0 deletions application/controllers/ResponsesController.php
Expand Up @@ -36,6 +36,22 @@ public function actionDelete($id, $surveyId) {
if (App()->request->isPostRequest || App()->request->isDeleteRequest) {
return \Response::model($surveyId)->deleteByPk($id);
}

$this->redirect(['responses/index', 'id' => $surveyId]);
}

/**
* @todo Add permission check.
*/

public function actionDeleteMultiple(array $ids, $surveyId) {
if (App()->request->isDeleteRequest
&& 0 < $count = \Response::model($surveyId)->deleteAllByAttributes(['id' => $ids]) ) {
App()->user->setFlash('success', gT("Responses deleted"));
} else {
App()->user->setFlash('danger', gT("Responses not deleted"));
}
$this->redirect(['responses/index', 'id' => $surveyId]);
}

public function actionExport($id)
Expand Down
7 changes: 7 additions & 0 deletions application/controllers/SurveyController.php
Expand Up @@ -59,6 +59,13 @@ protected function _sessioncontrol()
* @access public
* @return array
*/
public function accessRules()
{
return array_merge([
['allow', 'actions' => ['index']],
], parent::accessRules());
}

public function actions()
{
return array(
Expand Down
14 changes: 9 additions & 5 deletions application/controllers/SurveysController.php
Expand Up @@ -59,7 +59,7 @@ public function actionPublicList($sLanguage = null)
}

public function actionUpdate($id) {
$survey = $this->loadModel($id);
$survey = $this->loadModel($id, 'groups.questions');
if (App()->request->isPostRequest && isset($survey)) {
$survey->setAttributes($_POST['Survey']);
if ($survey->save(true)) {
Expand Down Expand Up @@ -107,8 +107,8 @@ public function filters()
* @throws CHttpException
* @throws \CHttpException
*/
protected function loadModel($id) {
$survey = Survey::model()->findByPk($id);
protected function loadModel($id, $with = null) {
$survey = Survey::model()->with($with)->findByPk($id);
if (!isset($survey)) {
throw new \CHttpException(404, "Survey not found.");
} elseif (!App()->user->checkAccess('survey', ['crud' => 'read', 'entity' => 'survey', 'entity_id' => $id])) {
Expand Down Expand Up @@ -171,9 +171,13 @@ public function actionStart($id, $token = null, array $pre = [])
$response->save();

$session = App()->surveySessionManager->newSession($survey->primaryKey, $response->id);
$this->redirect(['survey/index', 'sid' => $id, 'SSM' => $session->getId()]);
if (isset($token)) {
$session->setToken($token);
}

$this->redirect(['survey/index', 'SSM' => $session->getId()]);

$this->redirect(['surveys/run', 'sessionId' => $session->id]);
// $this->redirect(['surveys/run', 'sessionId' => $session->id]);
} else {
$this->render('execute/welcome', ['survey' => $survey]);
}
Expand Down

0 comments on commit 8696136

Please sign in to comment.