Skip to content

Commit

Permalink
DEV: use Survey model as input for createFieldMap. Fix all calls. (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson authored and Shnoulle committed Jul 7, 2017
1 parent feb4eeb commit 214ca21
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 63 deletions.
2 changes: 1 addition & 1 deletion application/controllers/UploaderController.php
Expand Up @@ -133,7 +133,7 @@ function run($actionID)
//$filename = sanitize_filename($_FILES['uploadfile']['name']);// This remove all non alpha numeric characters and replaced by _ . Leave only one dot .
$size = 0.001 * $_FILES['uploadfile']['size'];
$preview = Yii::app()->session['preview'];
$aFieldMap = createFieldMap($surveyid,'short',false,false,$sLanguage);
$aFieldMap = createFieldMap($oSurvey,'short',false,false,$sLanguage);
if (!isset($aFieldMap[$sFieldName]))
{
throw new CHttpException(400);// See for debug > 1
Expand Down
8 changes: 4 additions & 4 deletions application/controllers/admin/dataentry.php
Expand Up @@ -539,7 +539,7 @@ public function editdata($subaction, $id, $surveyid, $language='')

$fnames['completed'] = array('fieldname'=>"completed", 'question'=>gT("Completed"), 'type'=>'completed');

$fnames=array_merge($fnames,createFieldMap($surveyid,'full',false,false,$sDataEntryLanguage));
$fnames=array_merge($fnames,createFieldMap($survey,'full',false,false,$sDataEntryLanguage));
// Fix private if disallowed to view token
if(!Permission::model()->hasSurveyPermission($surveyid,'tokens','read'))
unset($fnames['token']);
Expand Down Expand Up @@ -588,7 +588,7 @@ public function editdata($subaction, $id, $surveyid, $language='')
$responses[$svrow['fieldname']] = $svrow['value'];
} // while

$fieldmap = createFieldMap($surveyid,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);
$results1 = array();
foreach($fieldmap as $fm)
{
Expand Down Expand Up @@ -1459,7 +1459,7 @@ public function update()

Yii::app()->loadHelper("database");
$surveytable = "{{survey_".$surveyid.'}}';
$fieldmap = createFieldMap($surveyid,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);
// restet token if user is not allowed to update
if(!Permission::model()->hasSurveyPermission($surveyid,'tokens','update')) // If not allowed to read: remove it
{
Expand Down Expand Up @@ -1702,7 +1702,7 @@ public function insert()

//BUILD THE SQL TO INSERT RESPONSES
$baselang = $survey->language;
$fieldmap = createFieldMap($surveyid,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);
$insert_data = array();

$_POST['startlanguage'] = $survey->language;
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/admin/export.php
Expand Up @@ -681,7 +681,7 @@ public function vvexport()
$aData['selectincansstate']=incompleteAnsFilterState();
$aData['surveyid'] = $iSurveyId;
$aData['display']['menu_bars']['browse'] = gT("Export VV file");
$fieldmap = createFieldMap($iSurveyId,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);

$surveytable = "{{survey_$iSurveyId}}";
// Control if fieldcode are unique
Expand Down Expand Up @@ -715,7 +715,7 @@ public function vvexport()

$s="\t";

$fieldmap = createFieldMap($iSurveyId,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);
$surveytable = "{{survey_$iSurveyId}}";


Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/limereplacementfields.php
Expand Up @@ -29,7 +29,7 @@ public function index()
list($replacementFields, $isInsertAnswerEnabled) = $this->_getReplacementFields($fieldtype, $surveyid);
if ($isInsertAnswerEnabled === true) {
//2: Get all other questions that occur before this question that are pre-determined answer types
$fieldmap = createFieldMap($surveyid,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);

$surveyInfo = getSurveyInfo($surveyid);
$surveyformat = $surveyInfo['format']; // S, G, A
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/admin/printablesurvey.php
Expand Up @@ -28,6 +28,7 @@ class printablesurvey extends Survey_Common_Action
function index($surveyid, $lang = null)
{
$surveyid = sanitize_int($surveyid);
$survey = Survey::model()->findByPk($surveyid);
if(!Permission::model()->hasSurveyPermission($surveyid,'surveycontent','read'))
{
$aData['surveyid'] = $surveyid;
Expand Down Expand Up @@ -160,7 +161,7 @@ function index($surveyid, $lang = null)
$mapquestionsNumbers=Array();
$answertext = ''; // otherwise can throw an error on line 1617

$fieldmap = createFieldMap($surveyid,'full',false,false,$sLanguageCode);
$fieldmap = createFieldMap($survey,'full',false,false,$sLanguageCode);

// =========================================================
// START doin the business:
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/admin/questions.php
Expand Up @@ -1886,6 +1886,7 @@ public function ajaxchecklabel()
public function preview($surveyid, $qid, $lang = null)
{
$surveyid = sanitize_int($surveyid);
$survey = Survey::model()->findByPk($surveyid);
$qid = sanitize_int($qid);
$LEMdebugLevel=0;

Expand All @@ -1908,7 +1909,7 @@ public function preview($surveyid, $qid, $lang = null)

// Use $_SESSION instead of $this->session for frontend features.
$_SESSION['survey_'.$surveyid]['s_lang'] = $language;
$_SESSION['survey_'.$surveyid]['fieldmap'] = createFieldMap($surveyid, 'full', true, $qid, $language);
$_SESSION['survey_'.$surveyid]['fieldmap'] = createFieldMap($survey, 'full', true, $qid, $language);


// Prefill question/answer from defaultvalues
Expand Down
7 changes: 5 additions & 2 deletions application/controllers/admin/responses.php
Expand Up @@ -132,6 +132,8 @@ public function viewbytoken($iSurveyID, $token, $sBrowseLang = '')
*/
public function view($iSurveyID, $iId, $sBrowseLang = '')
{
$survey = Survey::model()->findByPk($iSurveyID);

if(Permission::model()->hasSurveyPermission($iSurveyID,'responses','read'))
{
$aData = $this->_getData(array('iId' => $iId, 'iSurveyId' => $iSurveyID, 'browselang' => $sBrowseLang));
Expand All @@ -141,7 +143,7 @@ public function view($iSurveyID, $iId, $sBrowseLang = '')

$aViewUrls = array();

$fieldmap = createFieldMap($iSurveyID, 'full', false, false, $aData['language']);
$fieldmap = createFieldMap($survey, 'full', false, false, $aData['language']);
$bHaveToken=$aData['surveyinfo']['anonymized'] == "N" && tableExists('tokens_' . $iSurveyID);// Boolean : show (or not) the token
if(!Permission::model()->hasSurveyPermission($iSurveyID,'tokens','read')) // If not allowed to read: remove it
{
Expand Down Expand Up @@ -391,6 +393,7 @@ public function set_grid_display()
*/
public function browse($iSurveyId)
{
$survey = Survey::model()->findByPk($iSurveyId);
if(Permission::model()->hasSurveyPermission($iSurveyId,'responses','read'))
{
App()->getClientScript()->registerScriptFile( App()->getConfig('adminscripts') . 'listresponse.js');
Expand All @@ -403,7 +406,7 @@ public function browse($iSurveyId)
$aData['sidemenu']['state'] = false;
$aData['issuperadmin'] = Permission::model()->hasGlobalPermission('superadmin');
$aData['hasUpload'] = hasFileUploadQuestion($iSurveyId);
$aData['fieldmap'] = createFieldMap($iSurveyId, 'full', true, false, $aData['language']);
$aData['fieldmap'] = createFieldMap($survey, 'full', true, false, $aData['language']);
$aData['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat']);

////////////////////
Expand Down
4 changes: 3 additions & 1 deletion application/controllers/admin/statistics.php
Expand Up @@ -568,7 +568,9 @@ function graph()
$sQCode=substr($sQCode,1);
}
list($qsid, $qgid, $qqid) = explode("X", substr($sQCode, 0), 3);
$aFieldmap=createFieldMap($qsid,'full',false,false,$sStatisticsLanguage);
$survey = Survey::model()->findByPk($qsid);

$aFieldmap=createFieldMap($survey,'full',false,false,$sStatisticsLanguage);
$qtype=$aFieldmap[$sQCode]['type'];
$qqid=$aFieldmap[$sQCode]['qid'];
$aattr = QuestionAttribute::model()->getQuestionAttributes($qqid);
Expand Down
4 changes: 2 additions & 2 deletions application/helpers/admin/activate_helper.php
Expand Up @@ -229,7 +229,7 @@ function checkQuestions($postsid, $iSurveyID, $qtypes)
}

//CHECK THAT ALL THE CREATED FIELDS WILL BE UNIQUE
$fieldmap = createFieldMap($iSurveyID,'full',true,false,$survey->language,$aDuplicateQIDs);
$fieldmap = createFieldMap($survey,'full',true,false,$survey->language,$aDuplicateQIDs);
if (count($aDuplicateQIDs)) {
foreach ($aDuplicateQIDs as $iQID=>$aDuplicate) {
$sFixLink = "[<a href='".Yii::app()->getController()->createUrl("/admin/survey/sa/activate/surveyid/{$iSurveyID}/fixnumbering/{$iQID}")."'>Click here to fix</a>]";
Expand Down Expand Up @@ -281,7 +281,7 @@ function activateSurvey($iSurveyID, $simulate = false)
$oSurvey = Survey::model()->findByPk($iSurveyID);
$oSurvey->fixInvalidQuestions();
//Get list of questions for the base language
$sFieldMap = createFieldMap($iSurveyID,'full',true,false,$oSurvey->language);
$sFieldMap = createFieldMap($oSurvey,'full',true,false,$oSurvey->language);
//For each question, create the appropriate field(s)
foreach ($sFieldMap as $j=>$aRow)
{
Expand Down
8 changes: 5 additions & 3 deletions application/helpers/admin/export/SurveyDao.php
Expand Up @@ -21,14 +21,16 @@ public function loadSurveyById($id, $lang = null)
$intId = sanitize_int($id);
$survey->id = $intId;
$survey->info = getSurveyInfo($survey->id);
$availableLanguages = Survey::model()->findByPk($intId)->getAllLanguages();
$oSurvey = Survey::model()->findByPk($intId);

$availableLanguages = $oSurvey->allLanguages;

if (is_null($lang) || in_array($lang, $availableLanguages) === false) {
// use base language when requested language is not found or no specific language is requested
$lang = Survey::model()->findByPk($intId)->language;
$lang = $oSurvey->language;
}
App()->setLanguage($lang);
$survey->fieldMap = createFieldMap($intId,'full',true,false,$lang);
$survey->fieldMap = createFieldMap($oSurvey,'full',true,false,$lang);
// Check to see if timings are present and add to fieldmap if needed
if ($survey->info['savetimings']=="Y") {
$survey->fieldMap = $survey->fieldMap + createTimingsFieldMap($intId,'full',true,false,$lang);
Expand Down
37 changes: 20 additions & 17 deletions application/helpers/admin/statistics_helper.php
Expand Up @@ -342,8 +342,9 @@ function buildSelects($allfields, $surveyid, $language) {
//Create required variables
$selects=array();
$aQuestionMap=array();
$survey = Survey::model()->findByPk($surveyid);

$fieldmap=createFieldMap($surveyid, "full", false, false, $language);
$fieldmap=createFieldMap($survey, "full", false, false, $language);
foreach ($fieldmap as $field)
{
if(isset($field['qid']) && $field['qid']!='')
Expand Down Expand Up @@ -605,30 +606,32 @@ class statistics_helper {
protected $xlsRow = 0;

/**
* Builds an array containing information about this particular question/answer combination
*
* @param string $rt The code passed from the statistics form listing the field/answer (SGQA) combination to be displayed
* @param mixed $language The language to present output in
* @param mixed $surveyid The survey id
* @param string $outputType
* @param boolean $browse
*
* @output array $output An array containing "alist"=>A list of answers to the question in the form of an array ($alist array
* contains an array for every field to be displayed - with the Actual Question Code/Title, The text (flattened)
* of the question, and the fieldname where the data is stored.
* "qtitle"=>The title of the question,
* "qquestion"=>The description of the question,
* "qtype"=>The question type code
*/
* Builds an array containing information about this particular question/answer combination
*
* @param string $rt The code passed from the statistics form listing the field/answer (SGQA) combination to be displayed
* @param mixed $language The language to present output in
* @param mixed $surveyid The survey id
* @param string $outputType
* @param boolean $browse
*
* @output array $output An array containing "alist"=>A list of answers to the question in the form of an array ($alist array
* contains an array for every field to be displayed - with the Actual Question Code/Title, The text (flattened)
* of the question, and the fieldname where the data is stored.
* "qtitle"=>The title of the question,
* "qquestion"=>The description of the question,
* "qtype"=>The question type code
* @return array
*/
protected function buildOutputList($rt, $language, $surveyid, $outputType, $sql, $oLanguage,$browse=true)
{
//Set up required variables
$survey = Survey::model()->findByPk($surveyid);
$alist=array();
$qtitle="";
$qquestion="";
$qtype="";
$firstletter = substr($rt, 0, 1);
$fieldmap=createFieldMap($surveyid, "full", false, false, $language);
$fieldmap=createFieldMap($survey, "full", false, false, $language);
$sDatabaseType = Yii::app()->db->getDriverName();
$statisticsoutput="";
$qqid = "";
Expand Down
24 changes: 14 additions & 10 deletions application/helpers/common_helper.php
Expand Up @@ -1234,12 +1234,14 @@ function sendCacheHeaders()
*/
function getExtendedAnswer($iSurveyID, $sFieldCode, $sValue, $sLanguage)
{

if ($sValue==null || $sValue=='') return '';
$survey = Survey::model()->findByPk($iSurveyID);
//Fieldcode used to determine question, $sValue used to match against answer code
//Returns NULL if question type does not suit
if (strpos($sFieldCode, "{$iSurveyID}X")===0) //Only check if it looks like a real fieldcode
{
$fieldmap = createFieldMap($iSurveyID,'short',false,false,$sLanguage);
$fieldmap = createFieldMap($survey,'short',false,false,$sLanguage);
if (isset($fieldmap[$sFieldCode]))
{
$fields = $fieldmap[$sFieldCode];
Expand Down Expand Up @@ -1562,19 +1564,18 @@ function createCompleteSGQA($iSurveyID,$aFilters,$sLanguage) {
/**
* This function generates an array containing the fieldcode, and matching data in the same order as the activate script
*
* @param string $surveyid The Survey ID
* @param Survey $survey
* @param string $style 'short' (default) or 'full' - full creates extra information like default values
* @param boolean $force_refresh - Forces to really refresh the array, not just take the session copy
* @param false|int $questionid Limit to a certain qid only (for question preview) - default is false
* @param bool|int $questionid Limit to a certain qid only (for question preview) - default is false
* @param string $sLanguage The language to use
* @param array $aDuplicateQIDs
* @return array
*/
function createFieldMap($surveyid, $style='short', $force_refresh=false, $questionid=false, $sLanguage='', &$aDuplicateQIDs=array()) {
function createFieldMap($survey, $style='short', $force_refresh=false, $questionid=false, $sLanguage='', &$aDuplicateQIDs=array()) {

$sLanguage = sanitize_languagecode($sLanguage);
$surveyid = sanitize_int($surveyid);
$survey = Survey::model()->findByPk($surveyid);
$surveyid = $survey->sid;
//checks to see if fieldmap has already been built for this page.
if (isset(Yii::app()->session['fieldmap-' . $surveyid . $sLanguage]) && !$force_refresh && $questionid == false) {
return Yii::app()->session['fieldmap-' . $surveyid . $sLanguage];
Expand Down Expand Up @@ -2192,6 +2193,8 @@ function createTimingsFieldMap($surveyid, $style='full', $force_refresh=false, $

$sLanguage = sanitize_languagecode($sQuestionLanguage);
$surveyid = sanitize_int($surveyid);
$survey = Survey::model()->findByPk($surveyid);

$sOldLanguage=App()->language;
App()->setLanguage($sLanguage);

Expand All @@ -2201,7 +2204,7 @@ function createTimingsFieldMap($surveyid, $style='full', $force_refresh=false, $
}

//do something
$fields = createFieldMap($surveyid, $style, $force_refresh, $questionid, $sQuestionLanguage);
$fields = createFieldMap($survey, $style, $force_refresh, $questionid, $sQuestionLanguage);
$fieldmap['interviewtime']=array('fieldname'=>'interviewtime','type'=>'interview_time','sid'=>$surveyid, 'gid'=>'', 'qid'=>'', 'aid'=>'', 'question'=>gT('Total time'), 'title'=>'interviewtime');
foreach ($fields as $field) {
if (!empty($field['gid'])) {
Expand Down Expand Up @@ -2639,7 +2642,7 @@ function getArrayFilterExcludesCascadesForGroup($surveyid, $gid="", $output="qid
$cascaded=array();
$sources=array();
$qidtotitle=array();
$fieldmap = createFieldMap($surveyid,'full',false,false,$survey->language);
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);

if($gid != "") {
$qrows = arraySearchByKey($gid, $fieldmap, 'gid');
Expand Down Expand Up @@ -3066,7 +3069,7 @@ function reverseTranslateFieldNames($iOldSID,$iNewSID,$aGIDReplacements,$aQIDRep
else {
$forceRefresh=false;
}
$aFieldMap = createFieldMap($iNewSID,'short',$forceRefresh,false,$oNewSurvey->language);
$aFieldMap = createFieldMap($oNewSurvey,'short',$forceRefresh,false,$oNewSurvey->language);

$aFieldMappings=array();
foreach ($aFieldMap as $sFieldname=>$aFieldinfo)
Expand Down Expand Up @@ -3613,7 +3616,8 @@ function enforceSSLMode()
*/
function getFullResponseTable($iSurveyID, $iResponseID, $sLanguageCode, $bHonorConditions=true)
{
$aFieldMap = createFieldMap($iSurveyID,'full',false,false,$sLanguageCode);
$survey = Survey::model()->findByPk($iSurveyID);
$aFieldMap = createFieldMap($survey,'full',false,false,$sLanguageCode);

//Get response data
$idrow = SurveyDynamic::model($iSurveyID)->findByAttributes(array('id'=>$iResponseID));
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/export_helper.php
Expand Up @@ -365,7 +365,7 @@ function SPSSFieldMap($iSurveyID, $prefix = 'V', $sLanguage='')
if (empty($sLanguage)){
$sLanguage=$survey->language;
}
$fieldmap = createFieldMap($iSurveyID,'full',false,false,$sLanguage);
$fieldmap = createFieldMap($survey,'full',false,false,$sLanguage);

#See if tokens are being used
$bTokenTableExists = tableExists('tokens_'.$iSurveyID);
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/expressions/em_manager_helper.php
Expand Up @@ -3598,7 +3598,7 @@ private function setVariableAndTokenMappingsForExpressionManager($surveyid,$forc
$survey = Survey::model()->findByPk($surveyid);

// TODO - do I need to force refresh, or trust that createFieldMap will cache langauges properly?
$fieldmap=createFieldMap($surveyid,$style='full',$forceRefresh,false,$_SESSION['LEMlang']);
$fieldmap=createFieldMap($survey,$style='full',$forceRefresh,false,$_SESSION['LEMlang']);
$this->sid= $surveyid;
$this->sessid = 'survey_' . $this->sid;
$this->runtimeTimings[] = array(__METHOD__ . '.createFieldMap',(microtime(true) - $now));
Expand Down

0 comments on commit 214ca21

Please sign in to comment.