Skip to content

Commit

Permalink
Dev Implemented exporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Jun 5, 2015
1 parent 8b0efeb commit a992747
Show file tree
Hide file tree
Showing 48 changed files with 1,306 additions and 9,886 deletions.
3 changes: 2 additions & 1 deletion application/config/internal.php
Expand Up @@ -85,11 +85,12 @@
'CWebLogRoute' => array( // Use an associative array allow update in config
'class' => 'CWebLogRoute',
'levels'=>'error, warning, trace, info',
'enabled' => false
),
'trace' => array(
'class' => 'CWebLogRoute', // you can include more levels separated by commas... trace is shown on debug only
'levels' => 'trace', // you can include more separated by commas
'enabled' => YII_DEBUG
'enabled' => YII_DEBUG && false
),
'profile' => [
'class' => 'CProfileLogRoute'
Expand Down
Empty file.
1 change: 1 addition & 0 deletions application/config/routes.php
Expand Up @@ -43,4 +43,5 @@
'verb' => 'GET',
];
$route['<_controller:\w+>/<_action:\w+>'] = '<_controller>/<_action>';

return $route;
4 changes: 3 additions & 1 deletion application/controllers/Controller.php
Expand Up @@ -188,7 +188,9 @@ protected function loadModel($id) {

public function getActionParams()
{
return App()->request->psr7->getParsedBody();
$psr7 = App()->request->psr7;

return array_merge($psr7->getQueryParams(), $psr7->getParsedBody());
}


Expand Down
50 changes: 47 additions & 3 deletions application/controllers/ParticipantsController.php
Expand Up @@ -35,6 +35,10 @@ public function actionSummary() {
$this->render('summary', ['data' => $data]);
}

/**
*
* @param $id
*/
public function actionAttributes($id) {
$searchModel = \ParticipantAttribute::model();
$searchModel->dbCriteria->addColumnCondition(['participant_id' => $id]);
Expand All @@ -47,16 +51,21 @@ public function actionAttributes($id) {
}
}

public function actionManageAttributes() {
$dataProvider = new \CActiveDataProvider(\ParticipantAttributeName::class);
$this->render('manageAttributes', ['dataProvider' => $dataProvider]);
}

public function actionImport()
{
$this->render('import');
}


public function actionAjaxImport(array $items, $querySize = 1000)
public function actionAjaxImport(array $items, array $map, $querySize = 1000)
{
header('Content-Type: application/json');

// Set response code so on errors (max execution time, memory limit) we don't get http 200.
http_response_code(501);
set_time_limit(20);
Expand Down Expand Up @@ -84,11 +93,32 @@ public function actionAjaxImport(array $items, $querySize = 1000)


$participant = new Participant();
$regularFields = $participant->safeAttributeNames;
$tableName = $participant->tableName();
$attributeTableName = \ParticipantAttribute::model()->tableName();

$fields = array_flip($regularFields);
$participant->getSafeAttributeNames();
$fields = array_flip($participant->safeAttributeNames);

foreach($map as $csvName => $targetName) {
if (!isset($fields[$targetName])) {
// Create it.
$model = new \ParticipantAttributeName();
$model->name = $targetName;
if (!$model->save()) {
var_dump($model->errors);
} else {
unset($participant);
}
}
}
if (!isset($participant)) {
$participant = new Participant();
$participant->customAttributeNames(true);
}

$fields = array_flip($participant->safeAttributeNames);


$batchInserter = new \Batch(function(array $batch, $category = null) {
if (!empty($batch)) {
\Yii::beginProfile('query');
Expand Down Expand Up @@ -152,4 +182,18 @@ public function actionSettings() {
}
$this->render('settings', ['settings' => $settings]);
}


public function actionUpdate($id)
{
$participant = Participant::model()->with('customAttributes')->findByPk($id);
if (strcasecmp(App()->request->psr7->getMethod(), 'put') === 0 && isset(App()->request->psr7->getParsedBody()['Participant'])) {
$participant->setAttributes(App()->request->psr7->getParsedBody()['Participant']);
if ($participant->save()) {
App()->user->setFlash('success', gT("Participant information updated."));
$this->redirect(['participants/index']);
}
}
$this->render('update', ['participant' => $participant]);
}
}
3 changes: 2 additions & 1 deletion application/controllers/PluginsController.php
Expand Up @@ -77,7 +77,7 @@ public function actionDeactivate($id)
{
if ($id === App()->authManager->authorizationPlugin->id) {
App()->user->setFlash('error', "Cannot disable currently active authorization plugin.");
} elseif (in_array($id, SettingGlobal::get('authenticationPlugins', []))) {
} elseif (in_array($id, \SettingGlobal::get('authenticationPlugins', []))) {
App()->user->setFlash('error', "Cannot disable currently active authentication plugin.");
}

Expand Down Expand Up @@ -137,6 +137,7 @@ public function actionConfigureAuth() {
public function actionIndex()
{
$pm = App()->pluginManager;
$pm->scanPlugins();
$plugins = new CArrayDataProvider(array_values($pm->scanPlugins()));
return $this->render('index', [
'plugins' => $plugins,
Expand Down
12 changes: 6 additions & 6 deletions application/controllers/QuestionsController.php
Expand Up @@ -7,18 +7,18 @@ class QuestionsController extends Controller
{
public $layout = 'survey';
public function actionView($id) {
$this->models['question'] = $question = $this->loadModel($id);
$this->models['group'] = $question->group;
$this->models['survey'] = $question->survey;
$this->menus['question'] = $question = $this->loadModel($id);
$this->menus['group'] = $question->group;
$this->menus['survey'] = $question->survey;


$this->render('view', ['question' => $question]);
}

public function actionUpdate($id) {
$this->models['question'] = $question = $this->loadModel($id);
$this->models['group'] = $question->group;
$this->models['survey'] = $question->survey;
$this->menus['question'] = $question = $this->loadModel($id);
$this->menus['group'] = $question->group;
$this->menus['survey'] = $question->survey;


if (App()->request->isPutRequest) {
Expand Down
59 changes: 54 additions & 5 deletions application/controllers/ResponsesController.php
@@ -1,6 +1,10 @@
<?php
namespace ls\controllers;
use ls\models\forms\FormattingOptions;
use Survey;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

/**
* This class will handle survey creation and manipulation.
*/
Expand All @@ -13,14 +17,14 @@ public function actionIndex($id) {
* @todo Add permission check.
*/
$survey = Survey::model()->findByPk($id);
$this->survey = $survey;
$this->menus['survey'] = $survey;

$dataProvider = new \CActiveDataProvider(\Response::model($id), [
'pagination' => [
'pageSize' => 20
]
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
return $this->render('index', ['dataProvider' => $dataProvider, 'survey' => $survey]);
}

public function actionDelete($id, $surveyId) {
Expand All @@ -34,18 +38,63 @@ public function actionDelete($id, $surveyId) {
}
}

public function actionExport($id)
{
/* @var Survey $survey */
$this->menus['survey'] = $survey = Survey::model()->findByPk($id);

$options = new FormattingOptions();
$options->surveyId = $survey->sid;

/**
* Use PSR-7 request for easier future migrations.
*/
$psr7 = App()->request->psr7;
if (strcasecmp($psr7->getMethod(), 'post') == 0
&& ($options->setAttributes($psr7->getParsedBody()[\Html::modelName($options)]) || true) // SetAttributes returns null so we use || true.
&& $options->validate()
) {
/**
* Write returns a stream, we can optionally pass a stream to force it to use that stream instead.
* This would allow us to have it write directly to the browser.
*/
$writerClass = $options->getWriter();
/** @var \IWriter $writer */
$writer = new $writerClass($options);
$survey->language = App()->language;

$headers = [
'Content-Type' => $writer->getMimeType(),
'Content-Disposition' => "inline; filename='{$writer->getFileName($survey, App()->language)}'"
];
$response = new Response($writer->write($survey, App()->language), 200, $headers);


(new Response\SapiEmitter())->emit($response);
// Disable weblogroutes.
foreach(App()->log->routes as $route) {
if ($route instanceof \CWebLogRoute) {
$route->enabled = false;
}
}

return;
}
$this->render('export', ['options' => $options]);
}

public function actionView($id, $surveyId)
{
$response = \Response::model($surveyId)->findByPk($id);
$this->survey = $response->survey;
$this->menus['survey'] = $response->survey;
return $this->render('view', [
'response' => $response
]);
}

/**
* This function appends a new response to the series of the response id given.
* If the current series_id is set to null it's initialized to 0.
* If the current seriwe hees_id is set to null it's initialized to 0.
*
* @param int $surveyId
* @param string $id
Expand All @@ -62,6 +111,6 @@ public function actionAppend($surveyId, $id, $copy = false)

public function actionUpdate($id, $surveyId) {
$_SESSION['survey_'.$surveyId]['srid'] = $id;
$this->redirect(['survey/index', 'sid' => 652359]);
$this->redirect(['survey/index', 'sid' => $surveyId]);
}
}
8 changes: 4 additions & 4 deletions application/controllers/SurveysController.php
Expand Up @@ -52,7 +52,7 @@ public function actionUpdate($id) {
}
}
$this->layout = 'survey';
$this->models['survey'] = $survey;
$this->menus['survey'] = $survey;
$this->render('update', ['survey' => $survey]);
}

Expand All @@ -77,7 +77,7 @@ public function actionDeactivate($id) {
$this->redirect(['surveys/update', 'id' => $survey->sid]);
}

$this->models['survey'] = $survey;
$this->menus['survey'] = $survey;
$this->render('deactivate', ['survey' => $survey]);
}
public function filters()
Expand All @@ -100,7 +100,7 @@ protected function loadModel($id) {
}

if ($this->layout == 'survey') {
$this->models['survey'] = $survey;
$this->menus['survey'] = $survey;
}
return $survey;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ public function actionExpire($id)

}
$this->layout = 'survey';
$this->models['survey'] = $survey;
$this->menus['survey'] = $survey;
$this->render('expire', ['survey' => $survey]);
}

Expand Down
5 changes: 5 additions & 0 deletions application/core/HttpRequest.php
Expand Up @@ -21,6 +21,11 @@ public function __construct() {

protected function initRequest() {
$this->_request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
// Support _method.
if (isset($this->_request->getParsedBody()['_method'])) {
$this->_request = $this->_request->withMethod($this->_request->getParsedBody()['_method']);
}


// Add support for JSON requests.
if ($this->_request->getHeaderLine('Content-Type') == 'application/json'
Expand Down

0 comments on commit a992747

Please sign in to comment.