/** * RPC routine to to initialise the survey's collection of tokens where new participant tokens may be later added. * * @access public * @param string $sSessionKey Auth credentials * @param integer $iSurveyID ID of the survey where a token table will be created for * @param array $aAttributeFields An array of integer describing any additional attribute fields * @return array Status=>OK when successfull, otherwise the error description */ public function activate_tokens($sSessionKey, $iSurveyID, $aAttributeFields = array()) { if (!$this->_checkSessionKey($sSessionKey)) { return array('status' => 'Invalid session key'); } if (Permission::model()->hasGlobalPermission('surveys', 'create')) { $oSurvey = Survey::model()->findByPk($iSurveyID); if (is_null($oSurvey)) { return array('status' => 'Error: Invalid survey ID'); } if (is_array($aAttributeFields) && count($aAttributeFields) > 0) { foreach ($aAttributeFields as &$sField) { $sField = intval($sField); $sField = 'attribute_' . $sField; } $aAttributeFields = array_unique($aAttributeFields); } Yii::app()->loadHelper('admin/token'); if (Token::createTable($iSurveyID, $aAttributeFields)) { return array('status' => 'OK'); } else { return array('status' => 'Token table could not be created'); } } else { return array('status' => 'No permission'); } }
/** * Show dialogs and create a new tokens table */ public function _newtokentable($iSurveyId) { $aSurveyInfo = getSurveyInfo($iSurveyId); if (!Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'update') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) { Yii::app()->session['flashmessage'] = gT("Survey participants have not been initialised for this survey."); $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } $bTokenExists = tableExists('{{tokens_' . $iSurveyId . '}}'); //The token table already exist ? if ($bTokenExists) { Yii::app()->session['flashmessage'] = gT("Tokens already exist for this survey."); $this->getController()->redirect(array("/admin/survey/sa/view/surveyid/{$iSurveyId}")); } // The user have rigth to create token, then don't test right after Yii::import('application.helpers.admin.token_helper', true); // Update table, must be CRSF controlled if (Yii::app()->request->getPost('createtable') == "Y") { Token::createTable($iSurveyId); $aData['sidemenu']['state'] = false; LimeExpressionManager::SetDirtyFlag(); // LimeExpressionManager needs to know about the new token table $this->_renderWrappedTemplate('token', array('message' => array('title' => gT("Survey participants"), 'message' => gT("A participant table has been created for this survey.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_{$iSurveyId}\")<br /><br />\n" . "<input type='submit' class='btn btn-default' value='" . gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/sa/index/surveyid/{$iSurveyId}") . "', '_top')\" />\n"))); } elseif (returnGlobal('restoretable') == "Y" && Yii::app()->request->getPost('oldtable')) { //Rebuild attributedescription value for the surveys table $table = Yii::app()->db->schema->getTable(Yii::app()->request->getPost('oldtable')); $fields = array_filter(array_keys($table->columns), 'filterForAttributes'); $fieldcontents = $aSurveyInfo['attributedescriptions']; if (!is_array($fieldcontents)) { $fieldcontents = array(); } foreach ($fields as $fieldname) { $name = $fieldname; if ($fieldname[10] == 'c') { //This belongs to a cpdb attribute $cpdbattid = substr($fieldname, 15); $data = ParticipantAttributeName::model()->getAttributeName($cpdbattid, Yii::app()->session['adminlang']); $name = $data['attribute_name']; } if (!isset($fieldcontents[$fieldname])) { $fieldcontents[$fieldname] = array('description' => $name, 'mandatory' => 'N', 'show_register' => 'N'); } } Survey::model()->updateByPk($iSurveyId, array('attributedescriptions' => json_encode($fieldcontents))); Yii::app()->db->createCommand()->renameTable(Yii::app()->request->getPost('oldtable'), Yii::app()->db->tablePrefix . "tokens_" . intval($iSurveyId)); Yii::app()->db->schema->getTable(Yii::app()->db->tablePrefix . "tokens_" . intval($iSurveyId), true); // Refresh schema cache just in case the table existed in the past //Add any survey_links from the renamed table SurveyLink::model()->rebuildLinksFromTokenTable($iSurveyId); $aData = array(); $aData['sidemenu']['state'] = false; $this->_renderWrappedTemplate('token', array('message' => array('title' => gT("Import old tokens"), 'message' => gT("A token table has been created for this survey and the old tokens were imported.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_{$iSurveyId}" . "\")<br /><br />\n" . "<input type='submit' class='btn btn-default' value='" . gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/sa/index/surveyid/{$iSurveyId}") . "', '_top')\" />\n"))); LimeExpressionManager::SetDirtyFlag(); // so that knows that token tables have changed } else { $this->getController()->loadHelper('database'); $result = Yii::app()->db->createCommand(dbSelectTablesLike("{{old_tokens_" . intval($iSurveyId) . "_%}}"))->queryAll(); $tcount = count($result); if ($tcount > 0) { foreach ($result as $rows) { $oldlist[] = reset($rows); } $aData['oldlist'] = $oldlist; } $thissurvey = getSurveyInfo($iSurveyId); $aData['thissurvey'] = $thissurvey; $aData['surveyid'] = $iSurveyId; $aData['tcount'] = $tcount; $aData['databasetype'] = Yii::app()->db->getDriverName(); ///////////////////////////// $surveyinfo = Survey::model()->findByPk($iSurveyId)->surveyinfo; $aData['sidemenu']['state'] = false; $aData["surveyinfo"] = $surveyinfo; $aData['title_bar']['title'] = $surveyinfo['surveyls_title'] . "(" . gT("ID") . ":" . $iSurveyId . ")"; $aData['sidemenu']["token_menu"] = TRUE; $this->_renderWrappedTemplate('token', 'tokenwarning', $aData); } Yii::app()->end(); }
function importSurveyFile($sFullFilePath, $bTranslateLinksFields, $sNewSurveyName = NULL, $DestSurveyID = NULL) { $aPathInfo = pathinfo($sFullFilePath); if (isset($aPathInfo['extension'])) { $sExtension = strtolower($aPathInfo['extension']); } else { $sExtension = ""; } if ($sExtension == 'lss') { return XMLImportSurvey($sFullFilePath, null, $sNewSurveyName, $DestSurveyID, $bTranslateLinksFields); } elseif ($sExtension == 'txt') { return TSVImportSurvey($sFullFilePath); } elseif ($sExtension == 'lsa') { Yii::import("application.libraries.admin.pclzip.pclzip", true); $pclzip = new PclZip(array('p_zipname' => $sFullFilePath)); $aFiles = $pclzip->listContent(); if ($pclzip->extract(PCLZIP_OPT_PATH, Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR, PCLZIP_OPT_BY_EREG, '/(lss|lsr|lsi|lst)$/') == 0) { unset($pclzip); } // Step 1 - import the LSS file and activate the survey foreach ($aFiles as $aFile) { if (pathinfo($aFile['filename'], PATHINFO_EXTENSION) == 'lss') { //Import the LSS file $aImportResults = XMLImportSurvey(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename'], null, null, null, true, false); // Activate the survey Yii::app()->loadHelper("admin/activate"); $activateoutput = activateSurvey($aImportResults['newsid']); unlink(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename']); break; } } // Step 2 - import the responses file foreach ($aFiles as $aFile) { if (pathinfo($aFile['filename'], PATHINFO_EXTENSION) == 'lsr') { //Import the LSS file $aResponseImportResults = XMLImportResponses(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename'], $aImportResults['newsid'], $aImportResults['FieldReMap']); $aImportResults = array_merge($aResponseImportResults, $aImportResults); unlink(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename']); break; } } // Step 3 - import the tokens file - if exists foreach ($aFiles as $aFile) { if (pathinfo($aFile['filename'], PATHINFO_EXTENSION) == 'lst') { Yii::app()->loadHelper("admin/token"); if (Token::createTable($aImportResults['newsid'])) { $aTokenCreateResults = array('tokentablecreated' => true); $aImportResults = array_merge($aTokenCreateResults, $aImportResults); $aTokenImportResults = XMLImportTokens(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename'], $aImportResults['newsid']); } else { $aTokenCreateResults = array('tokentablecreated' => false); $aTokenImportResults['warnings'][] = gT("Unable to create token table"); } $aImportResults = array_merge_recursive($aTokenImportResults, $aImportResults); $aImportResults['importwarnings'] = array_merge($aImportResults['importwarnings'], $aImportResults['warnings']); unlink(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename']); break; } } // Step 4 - import the timings file - if exists Yii::app()->db->schema->refresh(); foreach ($aFiles as $aFile) { if (pathinfo($aFile['filename'], PATHINFO_EXTENSION) == 'lsi' && tableExists("survey_{$aImportResults['newsid']}_timings")) { $aTimingsImportResults = XMLImportTimings(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename'], $aImportResults['newsid'], $aImportResults['FieldReMap']); $aImportResults = array_merge($aTimingsImportResults, $aImportResults); unlink(Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $aFile['filename']); break; } } return $aImportResults; } else { return null; } }
public function newSurveySettings() { $oEvent = $this->getEvent(); $iSurveyId = $oEvent->get('survey'); $aSettings = $oEvent->get('settings'); //check tokens table when YES if ($aSettings['bUse'] == 1 && !tableExists("tokens_{$iSurveyId}")) { Token::createTable($iSurveyId); } $this->set('bUse', array_shift($aSettings), 'Survey', $oEvent->get('survey')); $this->set('surveyIdpAttributes', json_encode($aSettings), 'Survey', $oEvent->get('survey')); }