Skip to content

Commit

Permalink
add phpdoc & return types
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Jan 7, 2018
1 parent f2846d7 commit 55cb049
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions application/models/SurveyActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ public function __construct($survey)
$this->survey = $survey;
}

/**
* @return array
* @throws CException
*/
public function activate(){

$this->event = new PluginEvent('beforeSurveyActivate');
$this->event->set('surveyId', $this->survey->primaryKey);
$this->event->set('simulate', $this->isSimulation);
App()->getPluginManager()->dispatchEvent($this->event);

$this->showEventMessages();
if(!empty($this->error)){
if(!$this->showEventMessages()){
return ['error'=>$this->error];
}

Expand All @@ -48,11 +51,14 @@ public function activate(){
return array('dbengine'=>Yii::app()->db->getDriverName(), 'dbtype'=>Yii::app()->db->driverName, 'fields'=>$this->tableDefinition);
}

// If last question is of type MCABCEFHP^QKJR let's get rid of the ending coma in createsurvey
$this->createParticipantsTable();
if(!$this->createParticipantsTable()){
return ['error'=>$this->error];
}

if ($this->survey->isSaveTimings) {
$this->createTimingsTable();
if(!$this->createTimingsTable()){
return ['error'=>$this->error];
}
}

if(!empty($this->error)){
Expand All @@ -79,6 +85,7 @@ public function activate(){

/**
* For each question, create the appropriate field(s)
* @return void
*/
private function prepareTableDefinition(){
$sFieldMap = $this->fieldMap;
Expand Down Expand Up @@ -185,7 +192,7 @@ private function prepareTableDefinition(){
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
} elseif (intval($oQuestionAttribute->value) < 1) {
// Fix it if invalid : disallow 0, but need a sub question minimum for EM
// Fix it if invalid : disallow 0, but need a sub question minimum for EM
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
}
Expand All @@ -202,6 +209,9 @@ private function prepareTableDefinition(){

}

/**
* @return void
*/
private function prepareTimingsTable(){
$timingsfieldmap = createTimingsFieldMap($this->survey->primaryKey, "full", false, false, $this->survey->language);
$aTimingTableDefinition = array();
Expand All @@ -214,6 +224,9 @@ private function prepareTimingsTable(){



/**
* @return void
*/
private function prepareCollation(){
// Specify case sensitive collations for the token
$this->collation = '';
Expand All @@ -226,6 +239,9 @@ private function prepareCollation(){
}


/**
* @return void
*/
private function prepareSimulateQuery(){
if ($this->isSimulation) {
$tempTrim = trim($this->tableDefinition);
Expand All @@ -242,6 +258,9 @@ private function prepareSimulateQuery(){
}


/**
* @return void
*/
private function prepareResponsesTable(){
$this->prepareCollation();
//Check for any additional fields for this survey and create necessary fields (token and datestamp)
Expand All @@ -255,7 +274,7 @@ private function prepareResponsesTable(){


/**
* @return array
* @return boolean
*/
private function createParticipantsTable(){
$sTableName = $this->survey->responsesTableName;
Expand All @@ -268,6 +287,7 @@ private function createParticipantsTable(){
$this->error = $e->getMessage();
} else {
$this->error = 'surveytablecreation';
return false;
}
}
try {
Expand All @@ -278,23 +298,34 @@ private function createParticipantsTable(){
}

$this->createParticipantsTableKeys();
return true;

}


/**
* @return boolean
*/
private function showEventMessages(){
$success = $this->event->get('success');
$message = $this->event->get('message');

if ($success === false) {
Yii::app()->user->setFlash('error', $message);
$this->error = 'plugin';
return false;
} else if (!empty($message)) {
Yii::app()->user->setFlash('info', $message);
}
return true;

}

/**
* @return void
* @throws CDbException
* @throws CException
*/
private function createParticipantsTableKeys(){
$sQuery = "SELECT autonumber_start FROM {{surveys}} WHERE sid={$this->survey->primaryKey}";
$iAutoNumberStart = Yii::app()->db->createCommand($sQuery)->queryScalar();
Expand All @@ -321,18 +352,26 @@ private function createParticipantsTableKeys(){

}

/**
* @return boolean
*/
private function createTimingsTable(){
$this->prepareTimingsTable();
$sTableName = $this->survey->timingsTableName;
try {
Yii::app()->db->createCommand()->createTable($sTableName, $this->timingsTableDefinition);
Yii::app()->db->schema->getTable($sTableName, true); // Refresh schema cache just in case the table existed in the past
return true;
} catch (\Exception $e) {
$this->error = 'timingstablecreation';
return false;
}
}


/**
* @return bool
*/
private function createSurveyDirectory(){
$iSurveyID = $this->survey->primaryKey;
// create the survey directory where the uploaded files can be saved
Expand All @@ -349,5 +388,4 @@ private function createSurveyDirectory(){

}


}

0 comments on commit 55cb049

Please sign in to comment.