Skip to content

Commit

Permalink
New feature: Export printable questionnaires (#639)
Browse files Browse the repository at this point in the history
* add _exportPrintableHtml to exports

* add _exportPrintableHtml to exports - all languages

* add _exportPrintableHtml to exports - include the printable css files

* add _exportPrintableHtml to exports -get all template assets instead of just css

* add _exportPrintableHtml to exports -PHPDoc

* add _exportPrintableHtml to exports - add proper action exportprintables

* add _exportPrintableHtml to exports -Add link to menu

* add _exportPrintableHtml to exports -clean up

* add _exportPrintableHtml to exports -clean up 2

* add _exportPrintableHtml to exports - add  survey title to filenames

* add _exportPrintableHtml to exports - move in menu

* add _exportPrintableHtml to exports - fix constants redefined in printablesurvey if looped

* add _exportPrintableHtml to exports - remove survey title from zip contents : utf-8 special characters get nasty

* add _exportPrintableHtml to exports - fix imports

* add _exportPrintableHtml to exports Massive  - add initial action

* add _exportPrintableHtml to exports Massive  - add working code

* add _exportPrintableHtml to exports  -remove checking if languages is empty

* Remove unused $oTemplate

* Fixed issue #12132: Survey List Status Filter 'Active and running' does not list expiring surveys

* reverse - wrong branch?

* reverse - wrong branch?

* reverse - wrong branch?

*  Fixed issue #12132: Survey List Status Filter 'Active and running' does not list expiring surveys
  • Loading branch information
TonisOrmisson authored and LouisGac committed Mar 2, 2017
1 parent 982e639 commit 1932ddc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
106 changes: 106 additions & 0 deletions application/controllers/admin/export.php
Expand Up @@ -27,6 +27,7 @@ function __construct($controller, $id)
parent::__construct($controller, $id);

Yii::app()->loadHelper('export');
Yii::import('application.controllers.admin.printablesurvey',1);
}

public function survey()
Expand Down Expand Up @@ -914,6 +915,15 @@ public function exportMultipleStructureSurveys()
Yii::app()->getController()->renderPartial('ext.admin.survey.ListSurveysWidget.views.massive_actions._export_archive_results', array('aResults'=>$exportResult['aResults'], 'sZip'=>$exportResult['sZip'], 'bArchiveIsEmpty'=>$exportResult['bArchiveIsEmpty']));
}

/**
* Export multiple surveys structure. Called via ajax from surveys list massive action
*/
public function exportMultiplePrintableSurveys()
{
$sSurveys = $_POST['sItems'];
$exportResult = $this->exportMultipleSurveys($sSurveys, 'printable');
Yii::app()->getController()->renderPartial('ext.admin.survey.ListSurveysWidget.views.massive_actions._export_archive_results', array('aResults'=>$exportResult['aResults'], 'sZip'=>$exportResult['sZip'], 'bArchiveIsEmpty'=>$exportResult['bArchiveIsEmpty']));
}
/**
* Export multiple surveys archives. Called via ajax from surveys list massive action
*/
Expand Down Expand Up @@ -981,6 +991,24 @@ public function exportMultipleSurveys($sSurveys, $sExportType)
$aResults[$iSurveyID]['error'] = gT("Not active.");
}
break;
// Export printable archives for all selected surveys
case 'printable':
$archiveName = $this->_exportPrintableHtmls($iSurveyID,false);
if (is_file($archiveName))
{
$aResults[$iSurveyID]['result'] = true;
$aResults[$iSurveyID]['file'] = $archiveName;
$bArchiveIsEmpty = false;
$archiveFile = $archiveName;
$newArchiveFileFullName = 'survey_printables_'.$iSurveyID.'.zip';
$this->_addToZip($zip, $archiveFile, $newArchiveFileFullName);
unlink($archiveFile);
}
else
{
$aResults[$iSurveyID]['error'] = gT("Unknown error");
}
break;

// Export structure for survey
default:
Expand Down Expand Up @@ -1165,6 +1193,10 @@ private function _surveyexport($action, $iSurveyID)
{
$this->_exportarchive($iSurveyID);
}
elseif ( $action == "exportprintables" )
{
$this->_exportPrintableHtmls($iSurveyID);
}
}

/**
Expand Down Expand Up @@ -1327,6 +1359,80 @@ public function quexml($iSurveyID)
}
}


/**
* Get a Zipped version of survey printable questionnaires in all languages
* (including the template html assets)
*
* @param integer $iSurveyID Survey ID
* @param bool $readFile Whether we read the file for direct download (or not as in massive actions)
* @return string
*/
private function _exportPrintableHtmls($iSurveyID,$readFile = true){
$oSurvey = Survey::model()->findByPk($iSurveyID);
$assetsDir = Template::getTemplateURL($oSurvey->template);
$fullAssetsDir = Template::getTemplatePath($oSurvey->template);
$aLanguages = $oSurvey->getAllLanguages();

$aSurveyInfo = $oSurvey->getSurveyinfo();

$tempdir = Yii::app()->getConfig("tempdir");
$zipdir = $this->_tempdir($tempdir);

$fn = "printable_questionnaires_".CHtml::encode($aSurveyInfo['surveyls_title'])."_{$oSurvey->primaryKey}.zip";
$zipfile = "$tempdir/".$fn;

Yii::app()->loadLibrary('admin.pclzip');
$z = new PclZip($zipfile);
$z->create($zipdir,PCLZIP_OPT_REMOVE_PATH,$zipdir);
$z->add($fullAssetsDir,PCLZIP_OPT_REMOVE_PATH,$fullAssetsDir,PCLZIP_OPT_ADD_PATH,$assetsDir);

foreach ($aLanguages as $language){
$file = $this->_exportPrintableHtml($oSurvey,$language,$tempdir);
$z->add($file,PCLZIP_OPT_REMOVE_PATH,$tempdir);
unlink($file);
}

$this->_addHeaders($fn,"application/zip",0);
if($readFile){
header('Content-Transfer-Encoding: binary');
header("Content-disposition: attachment; filename=\"".$fn."\"");
readfile($zipfile);
unlink($zipfile);
}
return $zipfile;

}

/**
* Get a the printable html questionnaire in specified language and store
* the file in the specified directory
*
* @param Survey $oSurvey
* @param string $language
* @param string $tempdir the directory the file will be stored in
* @return string File name where the data is stored
*/
private function _exportPrintableHtml($oSurvey, $language, $tempdir){
$printableSurvey = new printablesurvey();

ob_start(); //Start output buffer
$printableSurvey->index($oSurvey->primaryKey,$language);
$response = ob_get_contents(); //Grab output
ob_end_clean(); //Discard output buffer
$aSurveyInfo = $oSurvey->getSurveyinfo();

$file = "$tempdir/questionnaire_{$oSurvey->getPrimaryKey()}_{$language}.html";

// remove first slash to get local path for local storage for template assets
$templateDir = Template::getTemplateURL($oSurvey->template);
$response = str_replace($templateDir,substr($templateDir,1),$response);

file_put_contents($file,$response);
return $file;

}

/**
* Generate an TSV (tab-separated value) file for the survey structure
* @param type $surveyid
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/admin/printablesurvey.php
Expand Up @@ -95,8 +95,8 @@ function index($surveyid, $lang = null)
}
$sFullTemplatePath = $oTemplate->path . DIRECTORY_SEPARATOR;
$sFullTemplateUrl = Template::model()->getTemplateURL($templatename)."/";
define('PRINT_TEMPLATE_DIR' , $sFullTemplatePath , true);
define('PRINT_TEMPLATE_URL' , $sFullTemplateUrl , true);
if (!defined('PRINT_TEMPLATE_DIR')) define('PRINT_TEMPLATE_DIR' , $sFullTemplatePath , true);
if (!defined('PRINT_TEMPLATE_URL')) define('PRINT_TEMPLATE_URL' , $sFullTemplateUrl , true);

LimeExpressionManager::StartSurvey($surveyid, 'survey',NULL,false,LEM_PRETTY_PRINT_ALL_SYNTAX);
$moveResult = LimeExpressionManager::NavigateForwards();
Expand Down
Expand Up @@ -82,7 +82,21 @@
'htmlModalBody' => gT('This will export the survey structure (.lss) for all selected active surveys. They will be provided in a single ZIP archive.').' '.gT('Continue?'),

),

// Export multiple printable
array(
// li element
'type' => 'action',
'action' => 'export',
'url' => App()->createUrl('/admin/export/sa/exportMultiplePrintableSurveys/'),
'iconClasses' => 'icon-export',
'text' => gT("Printable survey (.html)"),
// modal
'actionType' => 'modal',
'modalType' => 'yes-no',
'keepopen' => 'yes',
'sModalTitle' => gT('Export printable survey'),
'htmlModalBody' => gT('This will export printable survey.').' '.gT('Continue?'),
),
),

));
Expand Down
6 changes: 6 additions & 0 deletions application/models/Survey.php
Expand Up @@ -732,6 +732,9 @@ public function setAllowjumps($value)
}
}

/**
* @return array
*/
public function getSurveyinfo()
{
$iSurveyID = $this->sid;
Expand Down Expand Up @@ -1105,6 +1108,7 @@ public function search()
$criteria->compare("t.active",'Y');
$criteria->addCondition("t.startdate >'$sNow'");
}

if($this->active == "R")
{
$now = new CDbExpression("NOW()");
Expand All @@ -1118,6 +1122,8 @@ public function search()
$subCriteria2->addCondition('t.startdate IS NULL', "OR");
$criteria->mergeWith($subCriteria1);
$criteria->mergeWith($subCriteria2);


}
}
}
Expand Down
8 changes: 8 additions & 0 deletions application/views/admin/survey/surveybar_view.php
Expand Up @@ -448,6 +448,14 @@
<?php eT("Tab-separated-values format (*.txt)");?>
</a>
</li>

<!-- Survey printable questionnaires -->
<li>
<a href='<?php echo $this->createUrl("admin/export/sa/survey/action/exportprintables/surveyid/$surveyid");?>' >
<span class="icon-export" ></span>
<?php eT("Printable questionnaires (*.html)");?>
</a>
</li>
<?php endif; ?>

<?php endif;?>
Expand Down

0 comments on commit 1932ddc

Please sign in to comment.