Skip to content

Commit

Permalink
Fixed #456 Added survey code book exports
Browse files Browse the repository at this point in the history
  • Loading branch information
jvangestel committed Jul 4, 2019
1 parent 3565957 commit 1bf38ee
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 0 deletions.
42 changes: 42 additions & 0 deletions classes/Gems/Default/SurveyCodeBookAction.php
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 classes/Gems/Default/SurveyCodeBookMultiExportAction.php
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();
}
}
}
8 changes: 8 additions & 0 deletions classes/Gems/Menu/MenuAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ public function addExportContainer($label)
// EXPORT TO HTML
$export->addPage($this->_('Respondent archives'), 'pr.export-html', 'respondent-export', 'index');

$export->addPage($this->_('Survey codebooks'), 'pr.survey-maintenance.code-book-export', 'survey-code-book-multi-export', 'index');


return $export;
}
Expand Down Expand Up @@ -802,6 +804,12 @@ public function addTrackBuilderMenu($label, array $other = array())
$showPage->addPdfButton($this->_('PDF'), 'pr.survey-maintenance')
->addParameters(\MUtil_Model::REQUEST_ID)
->setParameterFilter('gsu_has_pdf', 1);

$codePage = $showPage->addPage($this->_('Export codebook'), 'pr.survey-maintenance.code-book-export', 'survey-code-book', 'export')
->addParameters(\MUtil_Model::REQUEST_ID)
->setParameterFilter('gsu_active', 1);


// Multi survey
$page->addAction($this->_('Check all is answered'), 'pr.survey-maintenance.check-all', 'check-all');
$page->addAction($this->_('Import answers'), 'pr.survey-maintenance.answer-import', 'answer-imports');
Expand Down
97 changes: 97 additions & 0 deletions classes/Gems/Snippets/Export/SurveyCodeBookMultiExportSnippet.php
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;
}
}
6 changes: 6 additions & 0 deletions controllers/SurveyCodeBookController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php


class SurveyCodeBookController extends \Gems_Default_SurveyCodeBookAction
{
}
5 changes: 5 additions & 0 deletions controllers/SurveyCodeBookMultiExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class SurveyCodeBookMultiExportController extends \Gems_Default_SurveyCodeBookMultiExportAction
{
}

0 comments on commit 1bf38ee

Please sign in to comment.