Skip to content

Commit

Permalink
dont use activateSurvey()
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Jan 7, 2018
1 parent 782839a commit ba35a7d
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 4 deletions.
4 changes: 3 additions & 1 deletion application/controllers/admin/surveyadmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ public function activate($iSurveyID)

$iSurveyID = (int) $iSurveyID;
$survey = Survey::model()->findByPk($iSurveyID);
$surveyActivator = new SurveyActivator($survey);

Yii::app()->user->setState('sql_'.$iSurveyID, ''); // If user has set some filters for responses from statistics on a previous activation, it must be wiped out
$aData = array();
Expand Down Expand Up @@ -782,7 +783,8 @@ public function activate($iSurveyID)
Survey::model()->resetCache(); // Make sure the saved values will be picked up
}

$aResult = activateSurvey($iSurveyID);
$aResult = $surveyActivator->activate();

$aViewUrls = array();
if ((isset($aResult['error']) && $aResult['error'] == 'plugin')
|| (isset($aResult['blockFeedback']) && $aResult['blockFeedback'])) {
Expand Down
1 change: 1 addition & 0 deletions application/helpers/admin/activate_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ function checkQuestions($postsid, $iSurveyID, $qtypes)
* Function to activate a survey
* @param int $iSurveyID The Survey ID
* @param bool $simulate
* @deprecated use SurveyActivator Class
* @return array
*/
function activateSurvey($iSurveyID, $simulate = false)
Expand Down
4 changes: 3 additions & 1 deletion application/helpers/admin/import_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,9 @@ function importSurveyFile($sFullFilePath, $bTranslateLinksFields, $sNewSurveyNam
$aImportResults = XMLImportSurvey(Yii::app()->getConfig('tempdir').DIRECTORY_SEPARATOR.$aFile['filename'], null, null, null, true, false);
// Activate the survey
Yii::app()->loadHelper("admin/activate");
activateSurvey($aImportResults['newsid']);
$survey = Survey::model()->findByPk($aImportResults['newsid']);
$surveyActivator = new SurveyActivator($survey);
$surveyActivator->activate();
unlink(Yii::app()->getConfig('tempdir').DIRECTORY_SEPARATOR.$aFile['filename']);
break;
}
Expand Down
3 changes: 2 additions & 1 deletion application/helpers/remotecontrol/remotecontrol_handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ public function activate_survey($sSessionKey, $iSurveyID)
}
if (Permission::model()->hasSurveyPermission($iSurveyID, 'surveyactivation', 'update')) {
Yii::app()->loadHelper('admin/activate');
$aActivateResults = activateSurvey($iSurveyID);
$surveyActivator = new SurveyActivator($oSurvey);
$aActivateResults =$surveyActivator->activate();
if (isset($aActivateResults['error'])) {
return array('status' => 'Error: '.$aActivateResults['error']);
} else {
Expand Down
262 changes: 262 additions & 0 deletions application/models/SurveyActivator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
<?php


class SurveyActivator
{
/** @var Survey */
private $survey;
/** @var boolean */
public $isSimulation;

public function __construct($survey)
{
$this->survey = $survey;
}

public function activate(){
$event = new PluginEvent('beforeSurveyActivate');
$iSurveyID = $this->survey->primaryKey;
$simulate = $this->isSimulation;
$oSurvey = $this->survey;

$event->set('surveyId', $iSurveyID);
$event->set('simulate', $simulate);
App()->getPluginManager()->dispatchEvent($event);
$success = $event->get('success');
$message = $event->get('message');
if ($success === false) {
Yii::app()->user->setFlash('error', $message);
return array('error' => 'plugin');
} else if (!empty($message)) {
Yii::app()->user->setFlash('info', $message);
}

$aTableDefinition = array();
$bCreateSurveyDir = false;
// Specify case sensitive collations for the token
$sCollation = '';
if (Yii::app()->db->driverName == 'mysqli' || Yii::app()->db->driverName == 'mysql') {
$sCollation = " COLLATE 'utf8mb4_bin'";
}
if (Yii::app()->db->driverName == 'sqlsrv' || Yii::app()->db->driverName == 'dblib' || Yii::app()->db->driverName == 'mssql') {
$sCollation = " COLLATE SQL_Latin1_General_CP1_CS_AS";
}
//Check for any additional fields for this survey and create necessary fields (token and datestamp)
$oSurvey->fixInvalidQuestions();
//Get list of questions for the base language
$sFieldMap = createFieldMap($oSurvey, 'full', true, false, $oSurvey->language);
//For each question, create the appropriate field(s)
foreach ($sFieldMap as $j=>$aRow) {
switch ($aRow['type']) {
case 'seed':
$aTableDefinition[$aRow['fieldname']] = "string(31)";
break;
case 'startlanguage':
$aTableDefinition[$aRow['fieldname']] = "string(20) NOT NULL";
break;
case 'id':
$aTableDefinition[$aRow['fieldname']] = "pk";
break;
case "startdate":
case "datestamp":
$aTableDefinition[$aRow['fieldname']] = "datetime NOT NULL";
break;
case "submitdate":
$aTableDefinition[$aRow['fieldname']] = "datetime";
break;
case "lastpage":
$aTableDefinition[$aRow['fieldname']] = "integer";
break;
case "N": //Numerical
case "K": //Multiple Numerical
$aTableDefinition[$aRow['fieldname']] = "decimal (30,10)";
break;
case "S": //SHORT TEXT
$aTableDefinition[$aRow['fieldname']] = "text";
break;
case "L": //LIST (RADIO)
case "!": //LIST (DROPDOWN)
case "M": //Multiple choice
case "P": //Multiple choice with comment
case "O": //DROPDOWN LIST WITH COMMENT
if ($aRow['aid'] != 'other' && strpos($aRow['aid'], 'comment') === false && strpos($aRow['aid'], 'othercomment') === false) {
$aTableDefinition[$aRow['fieldname']] = "string(5)";
} else {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "U": //Huge text
case "Q": //Multiple short text
case "T": //LONG TEXT
case ";": //Multi Flexi
case ":": //Multi Flexi
$aTableDefinition[$aRow['fieldname']] = "text";
break;
case "D": //DATE
$aTableDefinition[$aRow['fieldname']] = "datetime";
break;
case "5": //5 Point Choice
case "G": //Gender
case "Y": //YesNo
case "X": //Boilerplate
$aTableDefinition[$aRow['fieldname']] = "string(1)";
break;
case "I": //Language switch
$aTableDefinition[$aRow['fieldname']] = "string(20)";
break;
case "|":
$bCreateSurveyDir = true;
if (strpos($aRow['fieldname'], "_")) {
$aTableDefinition[$aRow['fieldname']] = "integer";
} else {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "ipaddress":
if ($oSurvey->ipaddr == "Y") {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "url":
if ($oSurvey->refurl == "Y") {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "token":
$aTableDefinition[$aRow['fieldname']] = 'string(35)'.$sCollation;
break;
case '*': // Equation
$aTableDefinition[$aRow['fieldname']] = "text";
break;
case 'R':
/**
* See bug #09828: Ranking question : update allowed can broke Survey DB
* If max_subquestions is not set or is invalid : set it to actual answers numbers
*/

$nrOfAnswers = Answer::model()->countByAttributes(
array('qid' => $aRow['qid'], 'language'=>Survey::model()->findByPk($iSurveyID)->language)
);
$oQuestionAttribute = QuestionAttribute::model()->find(
"qid = :qid AND attribute = 'max_subquestions'",
array(':qid' => $aRow['qid'])
);
if (empty($oQuestionAttribute)) {
$oQuestionAttribute = new QuestionAttribute();
$oQuestionAttribute->qid = $aRow['qid'];
$oQuestionAttribute->attribute = 'max_subquestions';
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
} elseif (intval($oQuestionAttribute->value) < 1) {
// Fix it if invalid : disallow 0, but need a sub question minimum for EM
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
}
$aTableDefinition[$aRow['fieldname']] = "string(5)";
break;
default:
$aTableDefinition[$aRow['fieldname']] = "string(5)";
}
if ($oSurvey->anonymized == 'N' && !array_key_exists('token', $aTableDefinition)) {
$aTableDefinition['token'] = 'string(35)'.$sCollation;
}
if ($simulate) {
$tempTrim = trim($aTableDefinition);
$brackets = strpos($tempTrim, "(");
if ($brackets === false) {
$type = substr($tempTrim, 0, 2);
} else {
$type = substr($tempTrim, 0, 2);
}
$arrSim[] = array($type);
}
}

if ($simulate) {
return array('dbengine'=>Yii::app()->db->getDriverName(), 'dbtype'=>Yii::app()->db->driverName, 'fields'=>$arrSim);
}

// If last question is of type MCABCEFHP^QKJR let's get rid of the ending coma in createsurvey

$sTableName = "{{survey_{$iSurveyID}}}";
Yii::app()->loadHelper("database");
try {
Yii::app()->db->createCommand()->createTable($sTableName, $aTableDefinition);
Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache just in case the table existed in the past
} catch (CDbException $e) {
if (App()->getConfig('debug')) {
return array('error'=>$e->getMessage());
} else {
return array('error'=>'surveytablecreation');
}
}
try {
if (isset($aTableDefinition['token'])) {
Yii::app()->db->createCommand()->createIndex("idx_survey_token_{$iSurveyID}_".rand(1, 50000), $sTableName, 'token');
}
} catch (CDbException $e) {
}

$sQuery = "SELECT autonumber_start FROM {{surveys}} WHERE sid={$iSurveyID}";
$iAutoNumberStart = Yii::app()->db->createCommand($sQuery)->queryScalar();
//if there is an autonumber_start field, start auto numbering here
if ($iAutoNumberStart !== false && $iAutoNumberStart > 0) {
if (Yii::app()->db->driverName == 'mssql' || Yii::app()->db->driverName == 'sqlsrv' || Yii::app()->db->driverName == 'dblib') {
mssql_drop_primary_index('survey_'.$iSurveyID);
mssql_drop_constraint('id', 'survey_'.$iSurveyID);
$sQuery = "ALTER TABLE {{survey_{$iSurveyID}}} drop column id ";
Yii::app()->db->createCommand($sQuery)->execute();
$sQuery = "ALTER TABLE {{survey_{$iSurveyID}}} ADD [id] int identity({$iAutoNumberStart},1)";
Yii::app()->db->createCommand($sQuery)->execute();
// Add back the primaryKey

Yii::app()->db->createCommand()->addPrimaryKey('PRIMARY_'.rand(1, 50000), $oSurvey->responsesTableName, 'id');
} elseif (Yii::app()->db->driverName == 'pgsql') {
$sQuery = "SELECT setval(pg_get_serial_sequence('{{survey_{$iSurveyID}}}', 'id'),{$iAutoNumberStart},false);";
@Yii::app()->db->createCommand($sQuery)->execute();
} else {
$sQuery = "ALTER TABLE {{survey_{$iSurveyID}}} AUTO_INCREMENT = {$iAutoNumberStart}";
@Yii::app()->db->createCommand($sQuery)->execute();
}
}

if ($oSurvey->savetimings == "Y") {
$timingsfieldmap = createTimingsFieldMap($iSurveyID, "full", false, false, $oSurvey->language);

$aTimingTableDefinition = array();
$aTimingTableDefinition['id'] = $aTableDefinition['id'];
foreach ($timingsfieldmap as $field=>$fielddata) {
$aTimingTableDefinition[$field] = 'FLOAT';
}

$sTableName = "{{survey_{$iSurveyID}_timings}}";
try {
Yii::app()->db->createCommand()->createTable($sTableName, $aTimingTableDefinition);
Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache just in case the table existed in the past
} catch (CDbException $e) {
return array('error'=>'timingstablecreation');
}

}
$aResult = array(
'status' => 'OK',
'pluginFeedback' => $event->get('pluginFeedback')
);
// create the survey directory where the uploaded files can be saved
if ($bCreateSurveyDir) {
if (!file_exists(Yii::app()->getConfig('uploaddir')."/surveys/".$iSurveyID."/files")) {
if (!(mkdir(Yii::app()->getConfig('uploaddir')."/surveys/".$iSurveyID."/files", 0777, true))) {
$aResult['warning'] = 'nouploadsurveydir';
} else {
file_put_contents(Yii::app()->getConfig('uploaddir')."/surveys/".$iSurveyID."/files/index.html", '<html><head></head><body></body></html>');
}
}
}
$sQuery = "UPDATE {{surveys}} SET active='Y' WHERE sid=".$iSurveyID;
Yii::app()->db->createCommand($sQuery)->query();
return $aResult;

}


}
5 changes: 4 additions & 1 deletion tests/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Exception\NoSuchDriverException;
use SurveyActivator;

class TestHelper extends TestCase
{
Expand Down Expand Up @@ -111,7 +112,9 @@ public function activateSurvey($surveyId)
$survey->save();
\Survey::model()->resetCache(); // Make sure the saved values will be picked up

$result = \activateSurvey($surveyId);
$surveyActivator = new SurveyActivator($survey);
$result = $surveyActivator->activate();

$this->assertEquals(['status' => 'OK', 'pluginFeedback' => null], $result, 'Activate survey is OK');
}

Expand Down

0 comments on commit ba35a7d

Please sign in to comment.