コード例 #1
0
/**
* Function to activate a survey
* @param int $iSurveyID The Survey ID
* @param bool $simulate
* @return string
*/
function activateSurvey($iSurveyID, $simulate = false)
{
    $createsurvey = '';
    $activateoutput = '';
    $createsurveytimings = '';
    $fieldstiming = array();
    $createsurveydirectory = false;
    //Check for any additional fields for this survey and create necessary fields (token and datestamp)
    $prow = Survey::model()->findByAttributes(array('sid' => $iSurveyID));
    //Get list of questions for the base language
    $fieldmap = createFieldMap($iSurveyID, 'full', true, false, getBaseLanguageFromSurveyID($iSurveyID));
    $createsurvey = array();
    foreach ($fieldmap as $j => $arow) {
        switch ($arow['type']) {
            case 'startlanguage':
                $createsurvey[$arow['fieldname']] = "VARCHAR(20) NOT NULL";
                break;
            case 'id':
                $createsurvey[$arow['fieldname']] = "pk";
                break;
            case "startdate":
            case "datestamp":
                $createsurvey[$arow['fieldname']] = "datetime NOT NULL";
                break;
            case "submitdate":
                $createsurvey[$arow['fieldname']] = "datetime";
                break;
            case "lastpage":
                $createsurvey[$arow['fieldname']] = "integer";
                break;
            case "N":
                //NUMERICAL
                $createsurvey[$arow['fieldname']] = "decimal (30,10)";
                break;
            case "S":
                //SHORT TEXT
                if (Yii::app()->db->driverName == 'mysql' || Yii::app()->db->driverName == 'mysqli') {
                    $createsurvey[$arow['fieldname']] = "text";
                } else {
                    $createsurvey[$arow['fieldname']] = "string";
                }
                break;
            case "L":
                //LIST (RADIO)
            //LIST (RADIO)
            case "!":
                //LIST (DROPDOWN)
            //LIST (DROPDOWN)
            case "M":
                //Multiple choice
            //Multiple choice
            case "P":
                //Multiple choice with comment
            //Multiple choice with comment
            case "O":
                //DROPDOWN LIST WITH COMMENT
                if ($arow['aid'] != 'other' && strpos($arow['aid'], 'comment') === false && strpos($arow['aid'], 'othercomment') === false) {
                    $createsurvey[$arow['fieldname']] = "VARCHAR(5)";
                } else {
                    $createsurvey[$arow['fieldname']] = "text";
                }
                break;
            case "K":
                // Multiple Numerical
                $createsurvey[$arow['fieldname']] = "float";
                break;
            case "U":
                //Huge text
            //Huge text
            case "Q":
                //Multiple short text
            //Multiple short text
            case "T":
                //LONG TEXT
            //LONG TEXT
            case ";":
                //Multi Flexi
            //Multi Flexi
            case ":":
                //Multi Flexi
                $createsurvey[$arow['fieldname']] = "text";
                break;
            case "D":
                //DATE
                $createsurvey[$arow['fieldname']] = "datetime";
                break;
            case "5":
                //5 Point Choice
            //5 Point Choice
            case "G":
                //Gender
            //Gender
            case "Y":
                //YesNo
            //YesNo
            case "X":
                //Boilerplate
                $createsurvey[$arow['fieldname']] = "VARCHAR(1)";
                break;
            case "I":
                //Language switch
                $createsurvey[$arow['fieldname']] = "VARCHAR(20)";
                break;
            case "|":
                $createsurveydirectory = true;
                if (strpos($arow['fieldname'], "_")) {
                    $createsurvey[$arow['fieldname']] = "integer";
                } else {
                    $createsurvey[$arow['fieldname']] = "text";
                }
                break;
            case "ipaddress":
                if ($prow->ipaddr == "Y") {
                    $createsurvey[$arow['fieldname']] = "text";
                }
                break;
            case "url":
                if ($prow->refurl == "Y") {
                    $createsurvey[$arow['fieldname']] = "text";
                }
                break;
            case "token":
                if ($prow->anonymized == "N") {
                    $createsurvey[$arow['fieldname']] = "VARCHAR(36)";
                }
                break;
            case '*':
                // Equation
                $createsurvey[$arow['fieldname']] = "text";
                break;
            default:
                $createsurvey[$arow['fieldname']] = "VARCHAR(5)";
        }
        if ($simulate) {
            $tempTrim = trim($createsurvey);
            $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' => $CI->db->databasetabletype, '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
    //$createsurvey = rtrim($createsurvey, ",\n")."\n"; // Does nothing if not ending with a comma
    $tabname = "{{survey_{$iSurveyID}}}";
    $command = new CDbCommand(Yii::app()->db);
    try {
        $execresult = $command->createTable($tabname, $createsurvey);
    } catch (CDbException $e) {
        return array('error' => 'surveytablecreation');
    }
    $anquery = "SELECT autonumber_start FROM {{surveys}} WHERE sid={$iSurveyID}";
    if ($anresult = Yii::app()->db->createCommand($anquery)->query()->readAll()) {
        //if there is an autonumber_start field, start auto numbering here
        foreach ($anresult as $row) {
            if ($row['autonumber_start'] > 0) {
                if (Yii::app()->db->driverName == 'mssql' || Yii::app()->db->driverName == 'sqlsrv') {
                    mssql_drop_primary_index('survey_' . $iSurveyID);
                    mssql_drop_constraint('id', 'survey_' . $iSurveyID);
                    $autonumberquery = "alter table {{survey_{$iSurveyID}}} drop column id ";
                    Yii::app()->db->createCommand($autonumberquery)->execute();
                    $autonumberquery = "alter table {{survey_{$iSurveyID}}} add [id] int identity({$row['autonumber_start']},1)";
                    Yii::app()->db->createCommand($autonumberquery)->execute();
                } elseif (Yii::app()->db->driverName == 'pgsql') {
                } else {
                    $autonumberquery = "ALTER TABLE {{survey_{$iSurveyID}}} AUTO_INCREMENT = " . $row['autonumber_start'];
                    $result = @Yii::app()->db->createCommand($autonumberquery)->execute();
                }
            }
        }
    }
    if ($prow->savetimings == "Y") {
        $timingsfieldmap = createTimingsFieldMap($iSurveyID, "full", false, false, getBaseLanguageFromSurveyID($iSurveyID));
        $column = array();
        $column['id'] = $createsurvey['id'];
        foreach ($timingsfieldmap as $field => $fielddata) {
            $column[$field] = 'FLOAT';
        }
        $command = new CDbCommand(Yii::app()->db);
        $tabname = "{{survey_{$iSurveyID}}}_timings";
        try {
            $execresult = $command->createTable($tabname, $column);
        } catch (CDbException $e) {
            return array('error' => 'timingstablecreation');
        }
    }
    $aResult = array('status' => 'OK');
    // create the survey directory where the uploaded files can be saved
    if ($createsurveydirectory) {
        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>');
            }
        }
    }
    $acquery = "UPDATE {{surveys}} SET active='Y' WHERE sid=" . $iSurveyID;
    $acresult = Yii::app()->db->createCommand($acquery)->query();
    return $aResult;
}
コード例 #2
0
 /**
  * Adds `id`, `dateCreated`, `date_update`, and `uid` columns to $columns, packages up the column definitions into
  * strings, and then passes it back to CDbCommand->createTable().
  *
  * @param string $table
  * @param array  $columns
  * @param null   $options
  * @param bool   $addIdColumn
  * @param bool   $addAuditColumns
  *
  * @return int
  */
 public function createTable($table, $columns, $options = null, $addIdColumn = true, $addAuditColumns = true)
 {
     $table = $this->getConnection()->addTablePrefix($table);
     $columns = array_merge($addIdColumn ? array('id' => ColumnType::PK) : array(), $columns, $addAuditColumns ? DbHelper::getAuditColumnConfig() : array());
     foreach ($columns as $col => $settings) {
         $columns[$col] = DbHelper::generateColumnDefinition($settings);
     }
     // Create the table
     return parent::createTable($table, $columns, $options);
 }