-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #456 Added survey code book exports
- Loading branch information
1 parent
3565957
commit 1bf38ee
Showing
6 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
|
||
class Gems_Default_SurveyCodeBookAction extends \Gems_Controller_ModelSnippetActionAbstract | ||
{ | ||
|
||
protected $surveyId; | ||
|
||
protected function createModel($detailed, $action) | ||
{ | ||
$model = $this->loader->getModels()->getSurveyCodeBookModel($this->surveyId); | ||
|
||
//$test = $model->load(); | ||
|
||
return $model; | ||
} | ||
|
||
public function getTopic($count = 1) | ||
{ | ||
return $this->_('Codebook'); | ||
} | ||
|
||
public function getTopicTitle() | ||
{ | ||
return $this->_('Codebook'); | ||
} | ||
|
||
public function exportAction() | ||
{ | ||
$this->surveyId = $this->request->getParam(\MUtil_Model::REQUEST_ID); | ||
if ($this->surveyId == false) { | ||
throw new \Exception('No Survey ID set'); | ||
} | ||
|
||
parent::exportAction(); | ||
} | ||
|
||
public function exportMultipleAction() | ||
{ | ||
|
||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
classes/Gems/Default/SurveyCodeBookMultiExportAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
|
||
|
||
class Gems_Default_SurveyCodeBookMultiExportAction extends \Gems_Default_ExportSurveyActionAbstract | ||
{ | ||
/** | ||
* The parameters used for the autofilter action. | ||
* | ||
* When the value is a function name of that object, then that functions is executed | ||
* with the array key as single parameter and the return value is set as the used value | ||
* - unless the key is an integer in which case the code is executed but the return value | ||
* is not stored. | ||
* | ||
* @var array Mixed key => value array for snippet initialisation | ||
*/ | ||
protected $autofilterParameters = array( | ||
'containingId' => null, | ||
'exportModelSource' => 'getExportModelSource', | ||
'extraSort' => 'gto_start_time ASC', | ||
); | ||
|
||
/** | ||
* The snippets used for the autofilter action. | ||
* | ||
* @var mixed String or array of snippets name | ||
*/ | ||
protected $autofilterSnippets = array('Export\\SurveyCodeBookMultiExportSnippet'); | ||
|
||
/** | ||
* The snippets used for the index action, before those in autofilter | ||
* | ||
* @var mixed String or array of snippets name | ||
*/ | ||
protected $indexStartSnippets = array('Generic\\ContentTitleSnippet'); | ||
|
||
/** | ||
* Performs the export step | ||
*/ | ||
protected function exportBatch($data) | ||
{ | ||
$filter = $this->getSearchFilter(); | ||
|
||
if ($data) { | ||
$batch = $this->loader->getTaskRunnerBatch('export_surveys'); | ||
|
||
$models = $this->getExportModels($data['gto_id_survey'], $filter, $data); | ||
|
||
$batch->setVariable('model', $models); | ||
|
||
if (!$batch->count()) { | ||
$batch->minimalStepDurationMs = 2000; | ||
$batch->finishUrl = $this->view->url(array('step' => 'download')); | ||
|
||
$batch->setSessionVariable('files', array()); | ||
|
||
foreach ($data['gto_id_survey'] as $surveyId) { | ||
$batch->addTask('Export_ExportCommand', $data['type'], 'addExport', $data, $surveyId); | ||
} | ||
|
||
$batch->addTask('addTask', 'Export_ExportCommand', $data['type'], 'finalizeFiles'); | ||
|
||
$batch->autoStart = true; | ||
} | ||
|
||
|
||
|
||
if ($batch->run($this->request)) { | ||
exit; | ||
} else { | ||
$controller = $this; | ||
|
||
if ($batch->isFinished()) { | ||
} else { | ||
if ($batch->count()) { | ||
$controller->html->append($batch->getPanel($controller->view, $batch->getProgressPercentage() . '%')); | ||
} else { | ||
$controller->html->pInfo($controller->_('Nothing to do.')); | ||
} | ||
$controller->html->pInfo()->a( | ||
\MUtil_Html_UrlArrayAttribute::rerouteUrl($this->getRequest(), array('action'=>'index', 'step' => false)), | ||
array('class'=>'actionlink'), | ||
$this->_('Back') | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Performs the download step | ||
*/ | ||
protected function exportDownload() | ||
{ | ||
$this->view->layout()->disableLayout(); | ||
$this->_helper->viewRenderer->setNoRender(true); | ||
$batch = $this->loader->getTaskRunnerBatch('export_surveys'); | ||
$file = $batch->getSessionVariable('file'); | ||
foreach($file['headers'] as $header) { | ||
header($header); | ||
} | ||
while (ob_get_level()) { | ||
ob_end_clean(); | ||
} | ||
readfile($file['file']); | ||
// Now clean up the file | ||
unlink($file['file']); | ||
|
||
exit; | ||
} | ||
|
||
/** | ||
* | ||
* @param array $surveys | ||
* @param array $filter | ||
* @param array $data | ||
* @return array | ||
*/ | ||
protected function getExportModels(array $surveys, array $filter, array $data) | ||
{ | ||
$models = array(); | ||
|
||
foreach($surveys as $surveyId) { | ||
$models[$surveyId] = $this->getSurveyCodeBookModel($surveyId); | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
protected function getSurveyCodeBookModel($surveyId) | ||
{ | ||
return $this->loader->getModels()->getSurveyCodeBookModel($surveyId); | ||
|
||
} | ||
|
||
/** | ||
* Helper function to get the title for the index action. | ||
* | ||
* @return $string | ||
*/ | ||
public function getIndexTitle() | ||
{ | ||
return $this->_('Export codebooks from multiple surveys'); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function indexAction() | ||
{ | ||
$step = $this->getParam('step') ?: ($this->request->isXmlHttpRequest() ? 'batch' : 'form'); | ||
|
||
// \MUtil_Echo::track($step, $this->request->getParams()); | ||
if ($step == 'form') { | ||
$batch = $this->loader->getTaskRunnerBatch('export_surveys'); | ||
$batch->reset(); | ||
parent::indexAction(); | ||
|
||
} elseif ($step != 'download') { | ||
$data = $this->getSearchData(); | ||
if ($data) { | ||
if (!isset($data['gto_id_survey']) || empty($data['gto_id_survey'])) { | ||
$this->addMessage($this->_('Please select a survey to start the export'), 'danger'); | ||
parent::indexAction(); | ||
|
||
} else { | ||
$this->exportBatch($data); | ||
} | ||
} else { | ||
$this->exportBatch($data); | ||
} | ||
} elseif ($step == 'download') { | ||
$this->exportDownload(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
classes/Gems/Snippets/Export/SurveyCodeBookMultiExportSnippet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
|
||
namespace Gems\Snippets\Export; | ||
|
||
|
||
class SurveyCodeBookMultiExportSnippet extends \Gems_Snippets_AutosearchFormSnippet | ||
{ | ||
|
||
public $loader; | ||
|
||
/** | ||
* | ||
* @var \Gems_Util_BasePath | ||
*/ | ||
protected $basepath; | ||
|
||
/** | ||
* Returns a text element for autosearch. Can be overruled. | ||
* | ||
* The form / html elements to search on. Elements can be grouped by inserting null's between them. | ||
* That creates a distinct group of elements | ||
* | ||
* @param array $data The $form field values (can be usefull, but no need to set them) | ||
* @return array Of (possible nested) \Zend_Form_Element's or static text to add to the html or null for group breaks. | ||
*/ | ||
protected function getAutoSearchElements(array $data) | ||
{ | ||
$dbLookup = $this->util->getDbLookup(); | ||
$surveys = $dbLookup->getSurveysForExport(null, null, true); | ||
|
||
$elements['gto_id_survey'] = $this->_createMultiCheckBoxElements('gto_id_survey', $surveys, '<br>'); | ||
$elements[] = null; | ||
|
||
$elements = $elements + $this->getExportTypeElements($data); | ||
$elements[] = null; | ||
|
||
return $elements; | ||
} | ||
|
||
|
||
/** | ||
* Creates a submit button | ||
* | ||
* @return \Zend_Form_Element_Submit | ||
*/ | ||
protected function getAutoSearchSubmit() | ||
{ | ||
return $this->form->createElement('submit', 'step', array('label' => $this->_('Export'), 'class' => 'button small')); | ||
} | ||
|
||
/** | ||
* Returns export field elements for auto search. | ||
* | ||
* The form / html elements to search on. Elements can be grouped by inserting null's between them. | ||
* That creates a distinct group of elements | ||
* | ||
* @param array $data The $form field values (can be usefull, but no need to set them) | ||
* @return array Of \Zend_Form_Element's or static tekst to add to the html or null for group breaks. | ||
*/ | ||
protected function getExportTypeElements(array $data) | ||
{ | ||
$export = $this->loader->getExport(); | ||
$exportTypes = $export->getExportClasses(); | ||
|
||
if (isset($data['type'])) { | ||
$currentType = $data['type']; | ||
} else { | ||
reset($exportTypes); | ||
$currentType = key($exportTypes); | ||
} | ||
|
||
$elements['type_label'] = $this->_('Export to'); | ||
|
||
$elements['type'] = $this->_createSelectElement( 'type', $exportTypes); | ||
$elements['type']->setAttrib('onchange', 'this.form.submit();'); | ||
// $elements['step'] = $this->form->createElement('hidden', 'step');; | ||
|
||
$exportClass = $export->getExport($currentType); | ||
$exportName = $exportClass->getName(); | ||
$exportFormElements = $exportClass->getFormElements($this->form, $data); | ||
|
||
if ($exportFormElements) { | ||
$elements['firstCheck'] = $this->form->createElement('hidden', $currentType); | ||
foreach ($exportFormElements as $key => $formElement) { | ||
$elements['type_br_' . $key] = \MUtil_Html::create('br'); | ||
$elements['type_el_' . $key] = $formElement; | ||
} | ||
} | ||
|
||
// if (!isset($data[$currentType])) { | ||
// $data[$exportName] = $exportClass->getDefaultFormValues(); | ||
// } | ||
|
||
return $elements; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
|
||
class SurveyCodeBookController extends \Gems_Default_SurveyCodeBookAction | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
class SurveyCodeBookMultiExportController extends \Gems_Default_SurveyCodeBookMultiExportAction | ||
{ | ||
} |