public function exportresults() { $iSurveyID = sanitize_int(Yii::app()->request->getParam('surveyid')); if (!isset($imageurl)) { $imageurl = "./images"; } if (!isset($iSurveyID)) { $iSurveyID = returnGlobal('sid'); } if (!isset($exportstyle)) { $exportstyle = returnGlobal('exportstyle'); } if (!isset($answers)) { $answers = returnGlobal('answers'); } if (!isset($type)) { $type = returnGlobal('type'); } if (!isset($convertyto1)) { $convertyto1 = returnGlobal('convertyto1'); } if (!isset($convertnto2)) { $convertnto2 = returnGlobal('convertnto2'); } if (!isset($convertyto)) { $convertyto = returnGlobal('convertyto'); } if (!isset($convertnto)) { $convertnto = returnGlobal('convertnto'); } if (!isset($convertspacetous)) { $convertspacetous = returnGlobal('convertspacetous'); } $clang = Yii::app()->lang; if (!hasSurveyPermission($iSurveyID, 'responses', 'export')) { exit; } Yii::app()->loadHelper("admin/exportresults"); $surveybaselang = Survey::model()->findByPk($iSurveyID)->language; $exportoutput = ""; // Get info about the survey $thissurvey = getSurveyInfo($iSurveyID); if (!$exportstyle) { //FIND OUT HOW MANY FIELDS WILL BE NEEDED - FOR 255 COLUMN LIMIT $aFieldMap = createFieldMap($iSurveyID, 'full', false, false, getBaseLanguageFromSurveyID($iSurveyID)); if ($thissurvey['savetimings'] === "Y") { //Append survey timings to the fieldmap array $aFieldMap = $aFieldMap + createTimingsFieldMap($iSurveyID, 'full', false, false, getBaseLanguageFromSurveyID($iSurveyID)); } $iFieldCount = count($aFieldMap); $selecthide = ""; $selectshow = ""; $selectinc = ""; if (incompleteAnsFilterState() == "complete") { $selecthide = "selected='selected'"; } elseif (incompleteAnsFilterState() == "incomplete") { $selectinc = "selected='selected'"; } else { $selectshow = "selected='selected'"; } $data['SingleResponse'] = (int) returnGlobal('id'); $data['selecthide'] = $selecthide; $data['selectshow'] = $selectshow; $data['selectinc'] = $selectinc; $data['afieldcount'] = $iFieldCount; $data['excesscols'] = $aFieldMap; //get max number of datasets $iMaximum = Yii::app()->db->createCommand("SELECT count(id) FROM {{survey_" . intval($iSurveyID) . "}}")->queryScalar(); $data['max_datasets'] = $iMaximum; $data['surveyid'] = $iSurveyID; $data['imageurl'] = Yii::app()->getConfig('imageurl'); $data['thissurvey'] = $thissurvey; $data['display']['menu_bars']['browse'] = $clang->gT("Export results"); $this->_renderWrappedTemplate('export', 'exportresults_view', $data); return; } // Export Language is set by default to surveybaselang // * the explang language code is used in SQL queries // * the alang object is used to translate headers and hardcoded answers // In the future it might be possible to 'post' the 'export language' from // the exportresults form $explang = $surveybaselang; $elang = new limesurvey_lang($explang); //Get together our FormattingOptions and then call into the exportSurvey //function. $options = new FormattingOptions(); $options->selectedColumns = Yii::app()->request->getPost('colselect'); $options->responseMinRecord = sanitize_int(Yii::app()->request->getPost('export_from')); $options->responseMaxRecord = sanitize_int(Yii::app()->request->getPost('export_to')); $options->answerFormat = $answers; $options->convertN = $convertnto2; $options->output = 'display'; if ($options->convertN) { $options->nValue = $convertnto; } $options->convertY = $convertyto1; if ($options->convertY) { $options->yValue = $convertyto; } $options->headerSpacesToUnderscores = $convertspacetous; $options->headingFormat = $exportstyle; $options->responseCompletionState = incompleteAnsFilterState(); // Replace token information by the column name if (in_array('first_name', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "firstname"; } if (in_array('last_name', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "lastname"; } if (in_array('email_address', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "email"; } $attributeFields = array_keys(getTokenFieldsAndNames($iSurveyID, TRUE)); foreach ($attributeFields as $attr_name) { if (in_array($attr_name, Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = $attr_name; } } if (Yii::app()->request->getPost('response_id')) { $sFilter = 'id=' . (int) Yii::app()->request->getPost('response_id'); } else { $sFilter = ''; } $resultsService = new ExportSurveyResultsService(); $resultsService->exportSurvey($iSurveyID, $explang, $type, $options, $sFilter); exit; }
/** * RPC Routine to export token response in a survey. * Returns the requested file as base64 encoded string * * @access public * @param string $sSessionKey Auth credentials * @param int $iSurveyID Id of the Survey * @param string $sDocumentType pdf,csv,xls,doc,json * @param string $sToken The token for which responses needed * @param string $sLanguageCode The language to be used * @param string $sCompletionStatus Optional 'complete','incomplete' or 'all' - defaults to 'all' * @param string $sHeadingType 'code','full' or 'abbreviated' Optional defaults to 'code' * @param string $sResponseType 'short' or 'long' Optional defaults to 'short' * @param array $aFields Optional Selected fields * @return array|string On success: Requested file as base 64-encoded string. On failure array with error information * */ public function export_responses_by_token($sSessionKey, $iSurveyID, $sDocumentType, $sToken, $sLanguageCode = null, $sCompletionStatus = 'all', $sHeadingType = 'code', $sResponseType = 'short', $aFields = null) { if (!$this->_checkSessionKey($sSessionKey)) { return array('status' => 'Invalid session key'); } Yii::app()->loadHelper('admin/exportresults'); if (!tableExists('{{survey_' . $iSurveyID . '}}')) { return array('status' => 'No Data, survey table does not exist.'); } if (!($maxId = SurveyDynamic::model($iSurveyID)->getMaxId())) { return array('status' => 'No Data, could not get max id.'); } if (!empty($sLanguageCode) && !in_array($sLanguageCode, Survey::model()->findByPk($iSurveyID)->getAllLanguages())) { return array('status' => 'Language code not found for this survey.'); } if (!SurveyDynamic::model($iSurveyID)->findByAttributes(array('token' => $sToken))) { return array('status' => 'No Response found for Token'); } if (!Permission::model()->hasSurveyPermission($iSurveyID, 'responses', 'export')) { return array('status' => 'No permission'); } if (empty($sLanguageCode)) { $sLanguageCode = getBaseLanguageFromSurveyID($iSurveyID); } if (is_null($aFields)) { $aFields = array_keys(createFieldMap($iSurveyID, 'full', true, false, $sLanguageCode)); } if ($sDocumentType == 'xls') { // Cut down to the first 255 fields $aFields = array_slice($aFields, 0, 255); } $oFormattingOptions = new FormattingOptions(); $oFormattingOptions->responseMinRecord = 1; $oFormattingOptions->responseMaxRecord = $maxId; $oFormattingOptions->selectedColumns = $aFields; $oFormattingOptions->responseCompletionState = $sCompletionStatus; $oFormattingOptions->headingFormat = $sHeadingType; $oFormattingOptions->answerFormat = $sResponseType; $oFormattingOptions->output = 'file'; $oExport = new ExportSurveyResultsService(); $sTableName = Yii::app()->db->tablePrefix . 'survey_' . $iSurveyID; $sTempFile = $oExport->exportSurvey($iSurveyID, $sLanguageCode, $sDocumentType, $oFormattingOptions, "{$sTableName}.token='{$sToken}'"); return new BigFile($sTempFile, true, 'base64'); }
public function exportresults() { $iSurveyID = sanitize_int(Yii::app()->request->getParam('surveyid')); if (!isset($imageurl)) { $imageurl = "./images"; } if (!isset($iSurveyID)) { $iSurveyID = returnGlobal('sid'); } if (!isset($convertyto1)) { $convertyto1 = returnGlobal('convertyto1'); } if (!isset($convertnto2)) { $convertnto2 = returnGlobal('convertnto2'); } if (!isset($convertyto)) { $convertyto = returnGlobal('convertyto'); } if (!isset($convertnto)) { $convertnto = returnGlobal('convertnto'); } if (!Permission::model()->hasSurveyPermission($iSurveyID, 'responses', 'export')) { $this->getController()->error('Access denied!'); } Yii::app()->loadHelper("admin/exportresults"); App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts') . "expressions/em_javascript.js"); App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . '/exportresults.js'); $sExportType = Yii::app()->request->getPost('type'); $sHeadingFormat = Yii::app()->request->getPost('headstyle'); $sAnswerFormat = Yii::app()->request->getPost('answers'); $bHeaderSpacesToUnderscores = Yii::app()->request->getPost('headspacetounderscores'); $bConvertY = Yii::app()->request->getPost('converty'); $bConvertN = Yii::app()->request->getPost('convertn'); $sYValue = Yii::app()->request->getPost('convertyto'); $sNValue = Yii::app()->request->getPost('convertnto'); $surveybaselang = Survey::model()->findByPk($iSurveyID)->language; $exportoutput = ""; // Get info about the survey $thissurvey = getSurveyInfo($iSurveyID); // Load ExportSurveyResultsService so we know what exports are available $resultsService = new ExportSurveyResultsService(); $exports = $resultsService->getExports(); if (!$sExportType) { //FIND OUT HOW MANY FIELDS WILL BE NEEDED - FOR 255 COLUMN LIMIT $aFieldMap = createFieldMap($iSurveyID, 'full', false, false, getBaseLanguageFromSurveyID($iSurveyID)); if ($thissurvey['savetimings'] === "Y") { //Append survey timings to the fieldmap array $aFieldMap = $aFieldMap + createTimingsFieldMap($iSurveyID, 'full', false, false, getBaseLanguageFromSurveyID($iSurveyID)); } $iFieldCount = count($aFieldMap); $selecthide = ""; $selectshow = ""; $selectinc = ""; if (incompleteAnsFilterState() == "complete") { $selecthide = "selected='selected'"; } elseif (incompleteAnsFilterState() == "incomplete") { $selectinc = "selected='selected'"; } else { $selectshow = "selected='selected'"; } $aFields = array(); foreach ($aFieldMap as $sFieldName => $fieldinfo) { $sCode = viewHelper::getFieldCode($fieldinfo); $aFields[$sFieldName] = $sCode . ' - ' . htmlspecialchars(ellipsize(html_entity_decode(viewHelper::getFieldText($fieldinfo)), 30, 0.6, '...')); $aFieldsOptions[$sFieldName] = array('title' => viewHelper::getFieldText($fieldinfo), 'data-fieldname' => $fieldinfo['fieldname'], 'data-emcode' => viewHelper::getFieldCode($fieldinfo, array('LEMcompat' => true))); // No need to filter title : Yii do it (remove all tag) } $data['SingleResponse'] = (int) returnGlobal('id'); $data['selecthide'] = $selecthide; $data['selectshow'] = $selectshow; $data['selectinc'] = $selectinc; $data['afieldcount'] = $iFieldCount; $data['aFields'] = $aFields; $data['aFieldsOptions'] = $aFieldsOptions; //get max number of datasets $iMaximum = SurveyDynamic::model($iSurveyID)->getMaxId(); $data['max_datasets'] = $iMaximum; $data['surveyid'] = $iSurveyID; $data['imageurl'] = Yii::app()->getConfig('imageurl'); $data['thissurvey'] = $thissurvey; $data['display']['menu_bars']['browse'] = gT("Export results"); // Export plugins, leave out all entries that are not plugin $exports = array_filter($exports); $exportData = array(); foreach ($exports as $key => $plugin) { $event = new PluginEvent('listExportOptions'); $event->set('type', $key); $oPluginManager = App()->getPluginManager(); $oPluginManager->dispatchEvent($event, $plugin); $exportData[$key] = array('onclick' => $event->get('onclick'), 'label' => $event->get('label'), 'checked' => $event->get('default', false), 'tooltip' => $event->get('tooltip', null)); } $data['exports'] = $exportData; // Pass available exports $data['headexports'] = array('code' => array('label' => gT("Question code"), 'help' => null, 'checked' => false), 'abbreviated' => array('label' => gT("Abbreviated question text"), 'help' => null, 'checked' => false), 'full' => array('label' => gT("Full question text"), 'help' => null, 'checked' => true), 'codetext' => array('label' => gT("Question code and question text"), 'help' => null, 'checked' => false)); // Add a plugin for adding headexports : a public function getRegistereddPlugins($event) can help here. $aLanguagesCode = Survey::model()->findByPk($iSurveyID)->getAllLanguages(); $aLanguages = array(); foreach ($aLanguagesCode as $sLanguage) { $aLanguages[$sLanguage] = getLanguageNameFromCode($sLanguage, false); } $data['aLanguages'] = $aLanguages; // Pass available exports $this->_renderWrappedTemplate('export', 'exportresults_view', $data); return; } // Export Language is set by default to surveybaselang // * the explang language code is used in SQL queries // * the alang object is used to translate headers and hardcoded answers // In the future it might be possible to 'post' the 'export language' from // the exportresults form $explang = Yii::app()->request->getPost('exportlang', $surveybaselang); //Get together our FormattingOptions and then call into the exportSurvey //function. $options = new FormattingOptions(); $options->selectedColumns = Yii::app()->request->getPost('colselect'); $options->responseMinRecord = sanitize_int(Yii::app()->request->getPost('export_from')); $options->responseMaxRecord = sanitize_int(Yii::app()->request->getPost('export_to')); $options->answerFormat = $sAnswerFormat; $options->convertY = $bConvertY; $options->yValue = $bConvertY ? $sYValue : null; $options->convertN = $bConvertN; $options->nValue = $bConvertN ? $sNValue : null; $options->headingTextLength = Yii::app()->request->getPost('abbreviatedtext') ? (int) Yii::app()->request->getPost('abbreviatedtextto') : null; $options->useEMCode = Yii::app()->request->getPost('emcode'); $options->headCodeTextSeparator = Yii::app()->request->getPost('codetextseparator'); $options->headerSpacesToUnderscores = $bHeaderSpacesToUnderscores; $options->headingFormat = $sHeadingFormat; $options->responseCompletionState = incompleteAnsFilterState(); $options->output = 'display'; // Replace token information by the column name if (in_array('first_name', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "firstname"; } if (in_array('last_name', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "lastname"; } if (in_array('email_address', Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = "email"; } $attributeFields = array_keys(getTokenFieldsAndNames($iSurveyID, TRUE)); foreach ($attributeFields as $attr_name) { if (in_array($attr_name, Yii::app()->request->getPost('attribute_select', array()))) { $options->selectedColumns[] = $attr_name; } } if (Yii::app()->request->getPost('response_id')) { $sFilter = "{{survey_{$iSurveyID}}}.id=" . (int) Yii::app()->request->getPost('response_id'); } else { $sFilter = ''; } viewHelper::disableHtmlLogging(); $resultsService->exportSurvey($iSurveyID, $explang, $sExportType, $options, $sFilter); exit; }
/** * RPC Routine to export responses. * Returns the requested file as base64 encoded string * * @access public * @param string $sSessionKey Auth credentials * @param int $iSurveyID Id of the Survey * @param string $sDocumentType pdf,csv,xls,doc * @param string $sLanguageCode The language to be used * @param string $sCompletionStatus Optional 'complete','incomplete' or 'all' - defaults to complete * @param string $sHeadingType 'code','full' or 'abbreviated' Optional defaults to 'code' * @param string $sResponseType 'short' or 'long' Optional defaults to 'short' * @param integer $iFromResponseID Optional * @param integer $iToResponseID Optional * @param array $aFields Optional Selected fields * @return array|string On success: Requested file as base 64-encoded string. On failure array with error information **/ public function export_responses($sSessionKey, $iSurveyID, $sDocumentType, $sLanguageCode = null, $sCompletionStatus = 'all', $sHeadingType = 'code', $sResponseType = 'short', $iFromResponseID = null, $iToResponseID = null, $aFields = null) { if (!$this->_checkSessionKey($sSessionKey)) { return array('status' => 'Invalid session key'); } Yii::app()->loadHelper('admin/exportresults'); if (!hasSurveyPermission($iSurveyID, 'responses', 'export')) { return array('status' => 'No permission'); } if (is_null($sLanguageCode)) { $sLanguageCode = getBaseLanguageFromSurveyID($iSurveyID); } if (is_null($aFields)) { $aFields = array_keys(createFieldMap($iSurveyID, 'full', true, false, $sLanguageCode)); } if ($sDocumentType == 'xls') { // Cut down to the first 255 fields $aFields = array_slice($aFields, 0, 255); } $oFomattingOptions = new FormattingOptions(); $oFomattingOptions->format = $sDocumentType; $oFomattingOptions->responseMinRecord = $iFromResponseID; $oFomattingOptions->responseMaxRecord = $iToResponseID; $oFomattingOptions->selectedColumns = $aFields; $oFomattingOptions->responseCompletionState = $sCompletionStatus; $oFomattingOptions->headingFormat = $sHeadingType; $oFomattingOptions->answerFormat = $sResponseType; $oExport = new ExportSurveyResultsService(); $sFileData = $oExport->exportSurvey($iSurveyID, $sLanguageCode, $oFomattingOptions, 'return'); return base64_encode($sFileData); }
/** * RPC Routine to export responses. * Returns the requested file as base64 encoded string * * @access public * @param string $sSessionKey Auth credentials * @param int $iSurveyID Id of the Survey * @param string $sDocumentType pdf,csv,xls,doc,json * @param string $sLanguageCode The language to be used * @param string $sCompletionStatus Optional 'complete','incomplete' or 'all' - defaults to 'all' * @param string $sHeadingType 'code','full' or 'abbreviated' Optional defaults to 'code' * @param string $sResponseType 'short' or 'long' Optional defaults to 'short' * @param integer $iFromResponseID Optional * @param integer $iToResponseID Optional * @param array $aFields Optional Selected fields * @return array|string On success: Requested file as base 64-encoded string. On failure array with error information * */ public function export_responses($sSessionKey, $iSurveyID, $sDocumentType, $sLanguageCode = null, $sCompletionStatus = 'all', $sHeadingType = 'code', $sResponseType = 'short', $iFromResponseID = null, $iToResponseID = null, $aFields = null) { if (!$this->_checkSessionKey($sSessionKey)) { return array('status' => 'Invalid session key'); } Yii::app()->loadHelper('admin/exportresults'); if (!tableExists('{{survey_' . $iSurveyID . '}}')) { return array('status' => 'No Data, survey table does not exist.'); } if (!($maxId = SurveyDynamic::model($iSurveyID)->getMaxId())) { return array('status' => 'No Data, could not get max id.'); } if (!Permission::model()->hasSurveyPermission($iSurveyID, 'responses', 'export')) { return array('status' => 'No permission'); } if (is_null($sLanguageCode)) { $sLanguageCode = getBaseLanguageFromSurveyID($iSurveyID); } if (is_null($aFields)) { $aFields = array_keys(createFieldMap($iSurveyID, 'full', true, false, $sLanguageCode)); } if ($sDocumentType == 'xls') { // Cut down to the first 255 fields $aFields = array_slice($aFields, 0, 255); } $oFomattingOptions = new FormattingOptions(); if ($iFromResponseID != null) { $oFomattingOptions->responseMinRecord = $iFromResponseID; } else { $oFomattingOptions->responseMinRecord = 1; } if ($iToResponseID != null) { $oFomattingOptions->responseMaxRecord = $iToResponseID; } else { $oFomattingOptions->responseMaxRecord = $maxId; } $oFomattingOptions->selectedColumns = $aFields; $oFomattingOptions->responseCompletionState = $sCompletionStatus; $oFomattingOptions->headingFormat = $sHeadingType; $oFomattingOptions->answerFormat = $sResponseType; $oFomattingOptions->output = 'file'; $oExport = new ExportSurveyResultsService(); $sTempFile = $oExport->exportSurvey($iSurveyID, $sLanguageCode, $sDocumentType, $oFomattingOptions, ''); return new BigFile($sTempFile, true, 'base64'); }