Skip to content

Commit

Permalink
Refactoring, removing direct session access.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Apr 3, 2015
1 parent 22a3e27 commit e487f26
Show file tree
Hide file tree
Showing 25 changed files with 567 additions and 221 deletions.
79 changes: 78 additions & 1 deletion application/components/SurveySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,49 @@

/**
* Class SurveySession
* IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
*
* If you build caches for the getters, make sure to exclude them from serialization (__sleep).
* This class is stored in the session and therefore requires some extra care:
*
* IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
*
* @property bool $isFinished
* @property int $surveyId
* @property string $language
* @property Survey $survey;
*/
class SurveySession extends CComponent {

/**
* These variables are not serialized.
*/
/**
* @var Response
*/
private $_response;
/**
* @var Survey
*/
private $_survey;

/**
* These are serialized
*/
protected $surveyId;
protected $id;
protected $responseId;
protected $finished = false;
protected $language = 'en';
/**
* @param int $surveyId
* @param int $responseId
*/
public function __construct($surveyId, $responseId)
public function __construct($surveyId, $responseId, $id)
{
$this->surveyId = $surveyId;
$this->responseId = $responseId;
$this->id = $id;
}

public function getSurveyId() {
Expand All @@ -29,4 +58,52 @@ public function getResponseId() {
public function getIsFinished() {
return $this->finished;
}

public function getLanguage() {
return $this->language;
}
/**
* Returns the session id for this session.
* The session id is unique per browser session and does not need to be unguessable.
* In fact it is just an auto incremented number.
*/
public function getId() {
return $this->id;
}

public function getResponse() {
if (!isset($this->_response)) {
$this->_response = Response::model($this->surveyId)->findByPk($this->responseId);
}
return $this->_response;
}

public function getSurvey() {
if (!isset($this->_survey)) {
$this->_survey = Survey::model()->findByPk($this->surveyId);
}
return $this->_survey;
}
public function getStep() {
return 1;

}

public function getMaxStep() {
return 5;
}

public function getPrevStep() {
return 1;
}

public function __sleep() {
return [
'surveyId',
'id',
'responseId',
'finished',
'language'
];
}
}
21 changes: 16 additions & 5 deletions application/components/SurveySessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SurveySessionManager extends CApplicationComponent
*/
protected $current;
/*
* @var \CTypedMap
* @var SurveySession[]
*/
protected $sessions;

Expand All @@ -24,7 +24,9 @@ public function init()
if (!isset($this->sessions)) {
$session->add('SSM', $this->sessions = new CTypedMap('SurveySession'));
}
$current = App()->request->getPost('SSM', []);
if ((null !== $current = App()->request->getParam('SSM')) && isset($this->sessions[$current])) {
$this->current = $this->sessions[$current];
}
}

public function getActive() {
Expand All @@ -42,12 +44,21 @@ public function getSessions() {
return $this->sessions;
}

public function getSession($id)
{
return $this->sessions[$id];

}

public function newSession($surveyId, $responseId)
{
if (isset($this->sessions["$surveyId.$responseId"])) {
throw new \Exception("Duplicate session detected.");
/** @var SurveySession $session */
foreach($this->sessions as $session) {
if ($session->getSurveyId() == $surveyId && $session->getResponseId() == $responseId) {
throw new \Exception("Duplicate session detected.");
}
}
return $this->sessions["$surveyId.$responseId"] = new SurveySession($surveyId, $responseId);
return $this->sessions[] = new SurveySession($surveyId, $responseId, count($this->sessions));
}


Expand Down
6 changes: 3 additions & 3 deletions application/controllers/ResponsesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function actionIndex($id) {

$dataProvider = new \CActiveDataProvider(\Response::model($id), [
'pagination' => [
'pageSize' => 1
'pageSize' => 50
]
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
Expand Down Expand Up @@ -55,7 +55,7 @@ public function actionAppend($surveyId, $id, $copy = false)
{
$response = \Response::model($surveyId)->findByPk($id);
$newResponse = $response->append($copy);
var_dump($response->attributes);
var_dump($newResponse->attributes);
$newResponse->save();
$this->redirect(['responses/index', 'id' => $surveyId]);
}
}
27 changes: 13 additions & 14 deletions application/controllers/SurveysController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SurveysController extends Controller
public function accessRules() {
return array_merge([
['allow', 'actions' => ['index'], 'users' => ['@']],
['allow', 'actions' => ['publicList']],
['allow', 'actions' => ['publicList', 'run' ,'start']],

], parent::accessRules());
}
Expand Down Expand Up @@ -48,7 +48,7 @@ public function actionUpdate($id) {
App()->user->setFlash('success', gT("Survey settings updated."));
}


$this->refresh();
}
$this->layout = 'survey';
$this->survey = $survey;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected function loadModel($id) {
*/
public function actionStart($id, $token = null)
{
$survey = $this->loadModel($id);
$survey = Survey::model()->findByPk($id);
$this->layout = 'bare';
if (!$survey->isActive) {
throw new \CHttpException(412, gT("The survey is not active."));
Expand All @@ -125,36 +125,35 @@ public function actionStart($id, $token = null)
'surveys/execute',
'surveyId' => $id,
];

if (App()->request->isPostRequest || $survey->format == 'A' || !$survey->bool_showwelcome) {

// Create response.
/**
* @todo Check if we shoudl resume an existing response instead.
* @todo Check if we should resume an existing response instead.
*/
$response = \Response::create($id);
if (isset($token)) {
/**
* @todo Update token and check for anonymous.
*/
$response->token = $token->token;
if (!$survey->bool_anonymized) {
$response->token = $token->token;
}
}
$response->save();

$this->render('start', ['url' => ['surveys/run', 'id' => $response->id, 'surveyId' => $id]]);
$session = App()->surveySessionManager->newSession($survey->sid, $response->id);
$this->redirect(['surveys/run', 'sessionId' => $session->id]);
} else {
$this->render('welcome', ['survey' => $survey, 'id' => 'test']);
}
}

public function actionRun($id, $surveyId)
public function actionRun($sessionId)
{
$survey = $this->loadModel($surveyId);
$response = \Response::model($survey->sid)->findByPk($id);
if (!isset($response)) {
$session = App()->surveySessionManager->getSession($sessionId);
if (!isset($session->response)) {
throw new \CHttpException(404, gT("Response not found."));
} else {
$this->redirect(['survey/index', 'sid' => $surveyId]);
echo ' running!';
}

}
Expand Down
95 changes: 95 additions & 0 deletions application/controllers/TokensController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,101 @@ public function actions() {
];
}

public function actionResponses($id, $surveyId) {
$this->layout = 'bare';
$survey = \Survey::model()->findByPk($surveyId);
$token = \Token::model($survey->sid)->findByPk($id);
$criteria = new \CDbCriteria();
$criteria->order = 'submitdate DESC';
$criteria->addColumnCondition(['token' => $token->token]);
$dataProvider = new \CActiveDataProvider(\Response::model($survey->sid), [
'criteria' => $criteria,
'pagination' => [
'pageSize' => 50
],
'sort' => false
]);
$this->survey = $survey;

if ($dataProvider->totalItemCount > 0) {
$this->render('/responses/index', [
'dataProvider' => $dataProvider,
'columns' => [
'token',
'submitdate',
'series_id',
],

'wrapper' => 'col-md-10 col-md-offset-2'
]);
} else {
echo "No responses for this token.";
}
}
public function actionCreate($surveyId)
{
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;
if (!$survey->bool_usetokens) {
throw new \CHttpException(412, "The survey you selected does not have tokens enabled.");
}

$token = \Token::create($survey->sid);
if (App()->request->isPostRequest) {
$token->setAttributes(App()->request->getPost(get_class($token)));

// Validate & safe.
if ($token->save()) {
// On success.
App()->user->setFlash('success', 'Token created.');
$this->redirect(['tokens/index', 'surveyId' => $survey->sid]);
}
}
$this->render('create', ['token' => $token, 'survey' => $survey]);
}


public function actionUpdate($surveyId, $id)
{
/**
* @todo Add permission check.
*/
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;

if (!$survey->bool_usetokens) {
throw new \CHttpException(412, "The survey you selected does not have tokens enabled.");
}

$token = \Token::model($survey->sid)->findByPk($id);
if (App()->request->isPostRequest) {
$token->setAttributes(App()->request->getPost(get_class($token)));

// Validate & safe.
if ($token->save()) {
// On success.
App()->user->setFlash('success', 'Token created.');
$this->redirect(['tokens/index', 'surveyId' => $survey->sid]);
}
}
$this->render('create', ['token' => $token, 'survey' => $survey]);
}

public function actionIndex($surveyId)
{
/**
* @todo Add permission check.
*/
$survey = \Survey::model()->findByPk($surveyId);
$this->survey = $survey;

$dataProvider = new \CActiveDataProvider(\Token::model($survey->sid), [
'pagination' => [
'pageSize' => 50
]
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
}
public function actionRegister($surveyId)
{
$this->layout = 'minimal';
Expand Down

0 comments on commit e487f26

Please sign in to comment.