Skip to content

Commit

Permalink
Fixed issue #17093: Unable to fix invalid survey after import bad TSV (
Browse files Browse the repository at this point in the history
…#1774)

* Fixed issue #17093: Unable to fix invalid survey after import bad TSV
Dev: Start a Survey::model()->fixSurveyConsistency()
Dev:  can be improved

* Dev: move to Services

* Dev: phpdoc oups
  • Loading branch information
Shnoulle committed Mar 11, 2021
1 parent 86c697e commit 687a20d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
9 changes: 6 additions & 3 deletions application/helpers/admin/import_helper.php
Expand Up @@ -1223,14 +1223,16 @@ function importSurveyFile($sFullFilePath, $bTranslateLinksFields, $sNewSurveyNam
case 'lss':
$aImportResults = XMLImportSurvey($sFullFilePath, null, $sNewSurveyName, $DestSurveyID, $bTranslateLinksFields);
if (!empty($aImportResults['newsid'])) {
TemplateConfiguration::checkAndcreateSurveyConfig($aImportResults['newsid']);
$SurveyIntegrity = new LimeSurvey\Models\Services\SurveyIntegrity(Survey::model()->findByPk($aImportResults['newsid']));
$SurveyIntegrity->fixSurveyIntegrity();
}
return $aImportResults;
case 'txt':
case 'tsv':
$aImportResults = TSVImportSurvey($sFullFilePath);
if ($aImportResults && $aImportResults['newsid']) {
TemplateConfiguration::checkAndcreateSurveyConfig($aImportResults['newsid']);
$SurveyIntegrity = new LimeSurvey\Models\Services\SurveyIntegrity(Survey::model()->findByPk($aImportResults['newsid']));
$SurveyIntegrity->fixSurveyIntegrity();
}
return $aImportResults;
case 'lsa':
Expand All @@ -1249,7 +1251,8 @@ function importSurveyFile($sFullFilePath, $bTranslateLinksFields, $sNewSurveyNam
//Import the LSS file
$aImportResults = XMLImportSurvey(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename'], null, null, null, true, false);
if ($aImportResults && $aImportResults['newsid']) {
TemplateConfiguration::checkAndcreateSurveyConfig($aImportResults['newsid']);
$SurveyIntegrity = new LimeSurvey\Models\Services\SurveyIntegrity(Survey::model()->findByPk($aImportResults['newsid']));
$SurveyIntegrity->fixSurveyIntegrity();
}
// Activate the survey
Yii::app()->loadHelper("admin/activate");
Expand Down
3 changes: 2 additions & 1 deletion application/models/Survey.php
Expand Up @@ -1787,6 +1787,7 @@ public static function saveTranscribeCaptchaOptions(Survey $oSurvey = null)

/**
* Method to make an approximation on how long a survey will last
* @deprecated, unused since 3.X
* Approx is 3 questions each minute.
* @return double
*/
Expand Down Expand Up @@ -1826,6 +1827,7 @@ public static function getSurveysWithTokenTable()

/**
* Fix invalid question in this survey
* Delete question that don't exist in primary language
*/
public function fixInvalidQuestions()
{
Expand Down Expand Up @@ -1981,7 +1983,6 @@ public function getCountInputQuestions()
return $this->countTotalQuestions - $this->countNoInputQuestions;
}


/**
* Returns true if this survey has any question of type $type.
* @param string $type Question type, like 'L', 'T', etc.
Expand Down
81 changes: 81 additions & 0 deletions application/models/services/SurveyIntegrity.php
@@ -0,0 +1,81 @@
<?php

namespace LimeSurvey\Models\Services;

use App;
use CDbCriteria;
use Survey;
use SurveyLanguageSetting;
use TemplateConfiguration;

/**
* Service class to fix integrity on a single survey
* @version 0.1.0
*/
class SurveyIntegrity
{

/** @var Survey */
private $survey;

/**
* @param Survey $survey
*/
public function __construct(
Survey $survey
) {
$this->survey = $survey;
}
/**
* Add needed language if needed in related SurveyLanguageSetting
* Remove uneeded language if needed in related SurveyLanguageSetting
* @return void
*/
public function fixSurveyLanguageSetting()
{
/* Check if SurveyLanguageSetting exist, create if not */
foreach ($this->survey->additionalLanguages as $sLang) {
if ($sLang) {
$oLanguageSettings = SurveyLanguageSetting::model()->find(
'surveyls_survey_id=:surveyid AND surveyls_language=:langname',
array(':surveyid' => $this->survey->sid, ':langname' => $sLang)
);
if (!$oLanguageSettings) {
$oLanguageSettings = new SurveyLanguageSetting();
$languagedetails = getLanguageDetails($sLang);
$oLanguageSettings->surveyls_survey_id = $this->survey->sid;
$oLanguageSettings->surveyls_language = $sLang;
$oLanguageSettings->surveyls_title = ''; // Not in default model ?
$oLanguageSettings->surveyls_dateformat = $languagedetails['dateformat'];
if (!$oLanguageSettings->save()) {
App()->setFlashMessage(sprintf(gT("Survey language %s could not be created."), $sLang), "error");
}
}
}
}
/* Delete all unneeded language setting */
$aAvailableLanguage = $this->survey->getAllLanguages();
$oCriteria = new CDbCriteria();
$oCriteria->compare('surveyls_survey_id', $this->survey->sid);
$oCriteria->addNotInCondition('surveyls_language', $aAvailableLanguage);
SurveyLanguageSetting::model()->deleteAll($oCriteria);
}

/**
* Function to find and fix potential issue inside current survey, mpore fix to be added
* - fixes missing groups, questions, answers, quotas & assessments for languages on a survey
* - Remove invalid question in this survey : exist in another la,guage but not in primary
* @return void
*/
public function fixSurveyIntegrity()
{
/* Fix current related language database */
$this->fixSurveyLanguageSetting();
/* Common helper function : missing groups, questions, answers, quotas & assessments for languages on a survey*/
fixLanguageConsistency($this->survey->sid, $this->survey->additional_languages);
/* Invalid question and subquestions */
$this->survey->fixInvalidQuestions();
/* Create an empty theme if needed */
TemplateConfiguration::checkAndcreateSurveyConfig($this->survey->sid);
}
}

0 comments on commit 687a20d

Please sign in to comment.