From 807241033faaefa0db7e8366a81d5af761a11a54 Mon Sep 17 00:00:00 2001 From: Sam Mousa Date: Mon, 2 Sep 2013 16:33:39 +0200 Subject: [PATCH] Dev Created better dynamic models Dev Created (new) dynamic token model and replaced most usages of the old one. --- application/config/internal.php | 11 +- application/controllers/OptinController.php | 12 +- application/controllers/OptoutController.php | 26 ++--- .../controllers/RegisterController.php | 8 +- application/controllers/admin/dataentry.php | 2 - application/controllers/admin/responses.php | 2 +- application/controllers/admin/tokens.php | 109 ++++++++---------- application/helpers/admin/import_helper.php | 4 +- application/helpers/admin/token_helper.php | 2 +- application/helpers/common_helper.php | 23 +--- application/helpers/export_helper.php | 2 +- application/helpers/frontend_helper.php | 50 ++++---- .../remotecontrol/remotecontrol_handle.php | 89 ++++++-------- application/models/TokenDynamic.php | 26 ----- application/tests/staticCalls.php | 1 - .../admin/responses/browseindex_view.php | 8 +- .../views/admin/token/tokensummary.php | 10 +- 17 files changed, 159 insertions(+), 226 deletions(-) diff --git a/application/config/internal.php b/application/config/internal.php index aa1f52d537b..54da6e3d9b4 100644 --- a/application/config/internal.php +++ b/application/config/internal.php @@ -48,7 +48,16 @@ 'user' => array( 'class' => 'LSWebUser', ), - + 'log' => array( + 'class'=>'CLogRouter', + 'routes'=>array( + array( + 'class' => 'CWebLogRoute', + //'enabled' => , + 'categories' => 'system.db.*', + ), + ) + ) ) ); diff --git a/application/controllers/OptinController.php b/application/controllers/OptinController.php index cb4d304c5f7..e3c07c423da 100644 --- a/application/controllers/OptinController.php +++ b/application/controllers/OptinController.php @@ -64,21 +64,21 @@ function actiontokens($surveyid, $token, $langcode = '') } else { - $row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken); + $tokenInstance = Token::model(null, $iSurveyID)->findByAttributes(array('token' => $token)); - if ($row == false) + if (!isset($tokenInstance)) { $html = $clang->gT('You are not a participant in this survey.'); } else { - $usresult = $row['emailstatus']; - if ($usresult=='OptOut') + if ($tokenInstance->emailstatus =='OptOut') { - $usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OK'); + $tokenInstance->emailstatus = 'OK'; + $tokenInstance->save(); $html = $clang->gT('You have been successfully added back to this survey.'); } - else if ($usresult=='OK') + elseif ($tokenInstance->emailstatus == 'OK') { $html = $clang->gT('You are already a part of this survey.'); } diff --git a/application/controllers/OptoutController.php b/application/controllers/OptoutController.php index fb0e7a10e39..657a36b2d52 100644 --- a/application/controllers/OptoutController.php +++ b/application/controllers/OptoutController.php @@ -70,18 +70,18 @@ function actiontokens() } else { - $row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken); + $tokenInstance = Token::model(null, $iSurveyID)->findByAttributes($sToken); - if ($row == false) + if (!isset($tokenInstance)) { $html = $clang->gT('You are not a participant in this survey.'); } else { - $usresult = $row['emailstatus']; - if ($usresult == 'OK') + if ($tokenInstance->emailstatus == 'OK') { - $usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OptOut'); + $tokenInstance->emailstatus = 'OptOut'; + $tokenInstance->save(); $html = $clang->gT('You have been successfully removed from this survey.'); } else @@ -153,29 +153,27 @@ function actionparticipants() } else { - $row = TokenDynamic::model($iSurveyID)->getEmailStatus($sToken); - $datas = TokenDynamic::model($iSurveyID)->find('token = :token', array(":token"=>$sToken)); - - if ($row == false) + $tokenInstance = Token::model(null, $iSurveyID)->findByAttributes(array('token' => $sToken)); + if (!isset($tokenInstance)) { $html = $clang->gT('You are not a participant in this survey.'); } else { - $usresult = $row['emailstatus']; - if ($usresult == 'OK') + if ($tokenInstance->emailstatus == 'OK') { - $usresult = TokenDynamic::model($iSurveyID)->updateEmailStatus($sToken, 'OptOut'); + $tokenInstance->emailstatus = 'OptOut'; + $tokenInstance->save(); $html = $clang->gT('You have been successfully removed from this survey.'); } else { $html = $clang->gT('You have been already removed from this survey.'); } - if(!empty($datas->participant_id) && $datas->participant_id != "") + if(!empty($tokenInstance->participant_id)) { //Participant also exists in central db - $cpdb = Participant::model()->find('participant_id = :participant_id', array(":participant_id"=>$datas->participant_id)); + $cpdb = Participant::model()->findByPk($tokenInstance->participant_id); if($cpdb->blacklisted=="Y") { $html .= "
"; diff --git a/application/controllers/RegisterController.php b/application/controllers/RegisterController.php index b2dacfa58dd..55f3ac995ee 100644 --- a/application/controllers/RegisterController.php +++ b/application/controllers/RegisterController.php @@ -159,8 +159,7 @@ function actionIndex($surveyid = null) $postattribute2=sanitize_xss_string(strip_tags(returnGlobal('register_attribute2'))); */ // Insert new entry into tokens db - TokenDynamic::sid($thissurvey['sid']); - $token = new TokenDynamic; + $token = new Token('insert', $thissurvey['sid']); $token->firstname = $postfirstname; $token->lastname = $postlastname; $token->email = Yii::app()->request->getPost('register_email'); @@ -171,8 +170,7 @@ function actionIndex($surveyid = null) $token->validfrom = $starttime; $token->validuntil = $endtime; } - foreach ($attributeinsertdata as $k => $v) - $token->$k = $v; + $token->setAttributes($attributeinsertdata); $result = $token->save(); /** @@ -186,8 +184,6 @@ function actionIndex($surveyid = null) ) or safeDie ($query."
".$connect->ErrorMsg()); //Checked - According to adodb docs the bound variables are quoted automatically */ $tid = getLastInsertID($token->tableName());; - $token=$token->token; - $fieldsarray["{ADMINNAME}"]=$thissurvey['adminname']; $fieldsarray["{ADMINEMAIL}"]=$thissurvey['adminemail']; $fieldsarray["{SURVEYNAME}"]=$thissurvey['name']; diff --git a/application/controllers/admin/dataentry.php b/application/controllers/admin/dataentry.php index c8d4114802c..6a61c029c23 100644 --- a/application/controllers/admin/dataentry.php +++ b/application/controllers/admin/dataentry.php @@ -1810,9 +1810,7 @@ public function insert() (".implode(',',$columns).") VALUES (".implode(',',$values).")"; - //$this->TokenDynamic_model->insertToken($surveyid,$tokendata); dbExecuteAssoc($SQL); - //Yii::app()->db->AutoExecute(db_table_name("tokens_".$surveyid), $tokendata,'INSERT'); $aDataentrymsgs[] = CHtml::tag('font', array('class'=>'successtitle'), $clang->gT("A token entry for the saved survey has been created too.")); //$aDataentryoutput .= "
\n"; } diff --git a/application/controllers/admin/responses.php b/application/controllers/admin/responses.php index 07b3487c5ed..b9f89079e73 100644 --- a/application/controllers/admin/responses.php +++ b/application/controllers/admin/responses.php @@ -308,7 +308,7 @@ public function index($iSurveyID) if (tableExists('{{tokens_' . $iSurveyID . '}}') && Permission::model()->hasSurveyPermission($iSurveyID,'tokens','read')) { $aData['with_token']= Yii::app()->db->schema->getTable('{{tokens_' . $iSurveyID . '}}'); - $aData['tokeninfo'] = TokenDynamic::model($iSurveyID)->summary(); + $aData['tokeninfo'] = Token::model(null, $iSurveyID)->summary(); } $aViewUrls[] = 'browseindex_view'; diff --git a/application/controllers/admin/tokens.php b/application/controllers/admin/tokens.php index 4fbec5e8ac3..fa513aaef46 100644 --- a/application/controllers/admin/tokens.php +++ b/application/controllers/admin/tokens.php @@ -52,12 +52,14 @@ function index($iSurveyId) else { //Check that the tokens table has the required fields + /** + * @todo is this still needed? What versions of token tables require this?? + */ TokenDynamic::model($iSurveyId)->checkColumns(); $aData['thissurvey'] = $thissurvey; $aData['surveyid'] = $iSurveyId; - $aData['queries'] = TokenDynamic::model($iSurveyId)->summary(); - - $this->_renderWrappedTemplate('token', array('tokenbar', 'tokensummary'), $aData); + $aData['queries'] = Token::model(null, $iSurveyId)->summary(); + $this->_renderWrappedTemplate('token', array('tokenbar', 'tokensummary'), $aData); } } @@ -200,7 +202,7 @@ function bounceprocessing($iSurveyId) ); $condn = array('token' => $tokenBounce[1]); - $record = TokenDynamic::model($iSurveyId)->findByAttributes($condn); + $record = Token::model(null,$iSurveyId)->findByAttributes($condn); foreach ($aData as $k => $v) $record->$k = $v; $record->save(); @@ -283,7 +285,8 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri $limit = (int) $limit; $start = (int) $start; - $tkcount = TokenDynamic::model($iSurveyId)->count(); + + $tkcount = Token::model(null, $iSurveyId)->count(); $next = $start + $limit; $last = $start - $limit; $end = $tkcount - $limit; @@ -329,7 +332,7 @@ function browse($iSurveyId, $limit = 50, $start = 0, $order = false, $searchstri $iquery = '(' . implode(' OR ', $iquery) . ')'; } - $tokens = TokenDynamic::model($iSurveyId)->findAll(array('condition' => $iquery, 'limit' => $limit, 'offset' => $start, 'order' => $order)); + $tokens = Token::model(null, $iSurveyId)->findAll(array('condition' => $iquery, 'limit' => $limit, 'offset' => $start, 'order' => $order)); $aData['bresult'] = array(); foreach ($tokens as $token) { @@ -388,11 +391,11 @@ function getTokens_json($iSurveyId, $search = null) $condition->order = $sidx. " ". $sord; $condition->offset = ($page - 1) * $limit; $condition->limit = $limit; - $tokens = TokenDynamic::model($iSurveyId)->findAll($condition); + $tokens = Token::model(null, $iSurveyId)->findAll($condition); $condition->offset=0; $condition->limit=0; - $aData->records = TokenDynamic::model($iSurveyId)->count($condition); + $aData->records = Token::model(null, $iSurveyId)->count($condition); if ($limit>$aData->records) { @@ -544,9 +547,7 @@ function editToken($iSurveyId) // Used ? 2013-01-29 // { // $sLang = Yii::app()->request->getPost('language'); // } - TokenDynamic::model($iSurveyId); - echo $from . ',' . $until; $aData = array( 'firstname' => Yii::app()->request->getPost('firstname'), @@ -570,7 +571,7 @@ function editToken($iSurveyId) // Used ? 2013-01-29 $this->getController()->error(sprintf($this->controller->lang->gT('%s cannot be left empty'), $desc['description'])); $aData[$attr_name] = Yii::app()->request->getPost($attr_name); } - $token = TokenDynamic::model()->find('tid=' . Yii::app()->getRequest()->getPost('id')); + $token = Token::model(null, $iSurveyId)->find('tid=' . Yii::app()->getRequest()->getPost('id')); foreach ($aData as $k => $v) $token->$k = $v; @@ -602,9 +603,8 @@ function editToken($iSurveyId) // Used ? 2013-01-29 $aData[$attr_name] = Yii::app()->request->getPost($attr_name); } echo ls_json_encode(var_export($aData)); - $token = new TokenDynamic; - foreach ($aData as $k => $v) - $token->$k = $v; + $token = new Token('insert', $iSurveyId); + $token->setAttributes($aData); echo $token->save(); } elseif ($sOperation == 'del' && Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')) @@ -716,13 +716,12 @@ function addnew($iSurveyId) $aData[$attr_name] = Yii::app()->getRequest()->getPost($attr_name); } - $udresult = TokenDynamic::model($iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'"); + $udresult = Token::model(null, $iSurveyId)->findAll("token <> '' and token = '$sanitizedtoken'"); if (count($udresult) == 0) { // AutoExecute - $token = new TokenDynamic; - foreach ($aData as $k => $v) - $token->$k = $v; + $token = new Token('insert', $iSurveyId); + $token->setAttributes($aData); $inresult = $token->save(); $aData['success'] = true; } @@ -806,7 +805,7 @@ function edit($iSurveyId, $iTokenId) $aTokenData['remindersent'] = Yii::app()->request->getPost('remindersent'); $aTokenData['remindercount'] = intval(Yii::app()->request->getPost('remindercount')); - $udresult = TokenDynamic::model($iSurveyId)->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'"); + $udresult = Token::model(null, $iSurveyId)->findAll("tid <> '$iTokenId' and token <> '' and token = '$santitizedtoken'"); if (count($udresult) == 0) { @@ -821,7 +820,7 @@ function edit($iSurveyId, $iTokenId) $aTokenData[$attr_name] = Yii::app()->request->getPost($attr_name); } - $token = TokenDynamic::model($iSurveyId)->findByPk($iTokenId); + $token = Token::model(null, $iSurveyId)->findByPk($iTokenId); foreach ($aTokenData as $k => $v) $token->$k = $v; $token->save(); @@ -876,7 +875,7 @@ function delete($iSurveyID) SurveyLink::model()->deleteTokenLink($aTokenIds, $iSurveyID); //Then delete the tokens - TokenDynamic::model($iSurveyID)->deleteRecords($aTokenIds); + Token::model(null, $iSurveyID)->deleteByPk($aTokenIds); } } @@ -954,9 +953,9 @@ function addDummies($iSurveyId, $subaction = '') $tokenlength = sanitize_int(Yii::app()->request->getPost('tokenlen')); // Fill an array with all existing tokens - $criteria = TokenDynamic::model($iSurveyId)->getDbCriteria(); + $criteria = Token::model(null, $iSurveyId)->getDbCriteria(); $criteria->select = 'token'; - $ntresult = TokenDynamic::model($iSurveyId)->findAllAsArray($criteria); //Use AsArray to skip active record creation + $ntresult = Token::model(null, $iSurveyId)->findAll($criteria); $existingtokens=array(); foreach ($ntresult as $tkrow) { @@ -964,36 +963,30 @@ function addDummies($iSurveyId, $subaction = '') } $invalidtokencount=0; $newDummyToken=0; - while ($newDummyToken<$amount && $invalidtokencount<50) + while ($newDummyToken < $amount && $invalidtokencount < 50) { - $aDataToInsert = $aData; - $aDataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['firstname']); - $aDataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['lastname']); - $aDataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $newDummyToken, $aDataToInsert['email']); - - $isvalidtoken = false; - while ($isvalidtoken == false && $invalidtokencount<50) - { - $newtoken = randomChars($tokenlength); - if (!isset($existingtokens[$newtoken])) - { - $isvalidtoken = true; - $existingtokens[$newtoken] = true; - $invalidtokencount=0; - } - else - { - $invalidtokencount ++; - } - } - if($isvalidtoken) - { - $aDataToInsert['token'] = $newtoken; - TokenDynamic::model()->insertToken($iSurveyId, $aDataToInsert); - $newDummyToken ++; - } - - } + $token = new Token('insert', $iSurveyId); + $token->setAttributes($aData); + + $token->firstname = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->firstname); + $token->lastname = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->lastname); + $token->email = str_replace('{TOKEN_COUNTER}', $newDummyToken, $token->email); + + $attempts = 0; + do { + $token->token = randomChars($tokenlength); + $attempts++; + } while (isset($existingtokens[$token->token]) && $attempts < 50); + + if ($attempts == 50) + { + throw new Exception('Something is wrong with your random generator.'); + } + + $existingtokens[$token->token] = true; + var_dump($token->save()); + $newDummyToken++; + } $aData['thissurvey'] = getSurveyInfo($iSurveyId); $aData['surveyid'] = $iSurveyId; if(!$invalidtokencount) @@ -1020,7 +1013,7 @@ function addDummies($iSurveyId, $subaction = '') } else { - $tkcount = TokenDynamic::model($iSurveyId)->count(); + $tkcount = Token::model(null, $iSurveyId)->count(); $tokenlength = Yii::app()->db->createCommand()->select('tokenlength')->from('{{surveys}}')->where('sid=:sid')->bindParam(":sid", $iSurveyId, PDO::PARAM_INT)->query()->readColumn(0); if (empty($tokenlength)) @@ -1472,19 +1465,19 @@ function email($iSurveyId, $tokenids = null) if (SendEmailMessage($modmessage, $modsubject, $to, $from, Yii::app()->getConfig("sitename"), $bHtml, getBounceEmail($iSurveyId), $aRelevantAttachments, $customheaders)) { // Put date into sent - $udequery = TokenDynamic::model($iSurveyId)->findByPk($emrow['tid']); + $token = Token::model(null, $iSurveyId)->findByPk($emrow['tid']); if ($bEmail) { $tokenoutput .= $clang->gT("Invitation sent to:"); - $udequery->sent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")); + $token->sent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")); } else { $tokenoutput .= $clang->gT("Reminder sent to:"); - $udequery->remindersent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")); - $udequery->remindercount = $udequery->remindercount + 1; + $token->remindersent = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")); + $token->remindercount++; } - $udequery->save(); + $token->save(); //Update central participant survey_links if(!empty($emrow['participant_id'])) { diff --git a/application/helpers/admin/import_helper.php b/application/helpers/admin/import_helper.php index 9ab1e367066..67152f58514 100644 --- a/application/helpers/admin/import_helper.php +++ b/application/helpers/admin/import_helper.php @@ -4067,7 +4067,9 @@ function XMLImportTokens($sFullFilepath,$iSurveyID,$sCreateMissingAttributeField $insertdata[(string)$key]=(string)$value; } - $result = TokenDynamic::model($iSurveyID)->insertToken($iSurveyID,$insertdata) or safeDie($clang->gT("Error").": Failed to insert data[15]
"); + $token = new Token('insert', $iSurveyID); + $token->setAttributes($insertdata); + $result = $token->save() or safeDie($clang->gT("Error").": Failed to insert data[15]
"); $results['tokens']++; } diff --git a/application/helpers/admin/token_helper.php b/application/helpers/admin/token_helper.php index 7b75d47eb35..b98d8bdc995 100644 --- a/application/helpers/admin/token_helper.php +++ b/application/helpers/admin/token_helper.php @@ -78,7 +78,7 @@ function emailTokens($iSurveyID,$aResultTokens,$sType) $attributes = array_keys(getTokenFieldsAndNames($iSurveyID)); $oSurveyLocale=SurveyLanguageSetting::model()->findAllByAttributes(array('surveyls_survey_id' => $iSurveyID)); - $oTokens = TokenDynamic::model($iSurveyID); + $oTokens = Token::model(null, $iSurveyID); $aSurveyLangs = $oSurvey->additionalLanguages; array_unshift($aSurveyLangs, $oSurvey->language); diff --git a/application/helpers/common_helper.php b/application/helpers/common_helper.php index 3a0c4c8e440..aaba1a0d2bc 100644 --- a/application/helpers/common_helper.php +++ b/application/helpers/common_helper.php @@ -5544,8 +5544,7 @@ function getAttributeValue($surveyid,$attrName,$token) } $surveyid=sanitize_int($surveyid); - TokenDynamic::sid($surveyid); - $query=TokenDynamic::model()->find(array("token"=>$token)); + $query= Token::model(null, $surveyid)->findByAttributes(array("token"=>$token)); $count=$query->count(); // OK - AR count if ($count != 1) @@ -5746,14 +5745,8 @@ function getNumericalFormat($lang = 'en', $integer = false, $negative = true) { */ function getTokenData($surveyid, $token) { - $thistoken = TokenDynamic::model($surveyid)->find('token = :token',array(':token' => $token)); - $thistokenarray=array(); // so has default value - if($thistoken) - { - $thistokenarray =$thistoken->attributes; - }// Did we fill with empty string if not exist ? - - return $thistokenarray; + $token = Token::model(null, $surveyid)->findByAttributes(array('token' => $token)); + return isset($token) ? $token->attributes : array(); } /** @@ -5882,13 +5875,9 @@ function getXMLWriter() { */ function usedTokens($token, $surveyid) { - $utresult = true; - $query=TokenDynamic::model($surveyid)->findAllByAttributes(array("token"=>$token)); - if (count($query) > 0) { - $row = $query[0]; - if ($row->usesleft > 0) $utresult = false; - } - return $utresult; + return Token::model(null, $surveyid)->usable()->countByAttributes(array( + 'token' => $token + )) > 0; } /** diff --git a/application/helpers/export_helper.php b/application/helpers/export_helper.php index 8b8730f3c17..a4697e17118 100644 --- a/application/helpers/export_helper.php +++ b/application/helpers/export_helper.php @@ -1771,7 +1771,7 @@ function tokensExport($iSurveyID) if (Yii::app()->request->getPost('tokendeleteexported') && !empty($aExportedTokens)) { - TokenDynamic::model($iSurveyID)->deleteByPk($aExportedTokens); + Token::model(null, $iSurveyID)->deleteByPk($aExportedTokens); } } diff --git a/application/helpers/frontend_helper.php b/application/helpers/frontend_helper.php index b939d66a7c9..60272e98bcb 100644 --- a/application/helpers/frontend_helper.php +++ b/application/helpers/frontend_helper.php @@ -435,32 +435,27 @@ function submittokens($quotaexit=false) $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")); // check how many uses the token has left - $oTokenInformation = TokenDynamic::model($surveyid)->findByAttributes(array('token' => $clienttoken)); - if ($oTokenInformation) - { - $usesleft = $oTokenInformation->usesleft; - $participant_id = isset($oTokenInformation->participant_id) ? $oTokenInformation->participant_id : ''; - } + $token = Token::model(null, $surveyid)->findByAttributes(array('token' => $clienttoken)); - if ($quotaexit==true) + if ($quotaexit==true) { - $oTokenInformation->completed = 'Q'; - $oTokenInformation->usesleft = $oTokenInformation->usesleft-1; + $token->completed = 'Q'; + $token->usesleft--; } else { - if (isset($usesleft) && $usesleft<=1) + if ($token->usesleft <= 1) { // Finish the token if (isTokenCompletedDatestamped($thissurvey)) { - $oTokenInformation->completed = $today; + $token->completed = $today; } else { - $oTokenInformation->completed = 'Y'; + $token->completed = 'Y'; } - if(!empty($participant_id)) + if(isset($token->participant_id)) { - $slquery = SurveyLink::model()->find('participant_id = :pid AND survey_id = :sid AND token_id = :tid', array(':pid'=>$participant_id, ':sid'=>$surveyid, ':tid'=>$oTokenInformation->tid)); + $slquery = SurveyLink::model()->find('participant_id = :pid AND survey_id = :sid AND token_id = :tid', array(':pid'=> $token->participant_id, ':sid'=>$surveyid, ':tid'=>$token->tid)); if (isTokenCompletedDatestamped($thissurvey)) { @@ -472,17 +467,18 @@ function submittokens($quotaexit=false) $slquery->save(); } } - $oTokenInformation->usesleft = $oTokenInformation->usesleft-1; + $token->usesleft--; } - $oTokenInformation->save(); + $token->save(); + if ($quotaexit==false) { - if ($oTokenInformation && trim(strip_tags($thissurvey['email_confirm'])) != "" && $thissurvey['sendconfirmation'] == "Y") + if ($token && trim(strip_tags($thissurvey['email_confirm'])) != "" && $thissurvey['sendconfirmation'] == "Y") { - if($oTokenInformation->completed == "Y" || $oTokenInformation->completed == $today) + if($token->completed == "Y" || $token->completed == $today) { $from = "{$thissurvey['adminname']} <{$thissurvey['adminemail']}>"; - $to = $oTokenInformation->email; + $to = $token->email; $subject=$thissurvey['email_confirm_subj']; $aReplacementVars=array(); @@ -490,8 +486,8 @@ function submittokens($quotaexit=false) $aReplacementVars["ADMINEMAIL"]=$thissurvey['adminemail']; $aReplacementVars['ADMINEMAIL'] = $thissurvey['adminemail']; //Fill with token info, because user can have his information with anonimity control - $aReplacementVars["FIRSTNAME"]=$oTokenInformation->firstname; - $aReplacementVars["LASTNAME"]=$oTokenInformation->lastname; + $aReplacementVars["FIRSTNAME"]=$token->firstname; + $aReplacementVars["LASTNAME"]=$token->lastname; $aReplacementVars["TOKEN"]=$clienttoken; // added survey url in replacement vars $surveylink = Yii::app()->createAbsoluteUrl("/survey/index/sid/{$surveyid}",array('lang'=>$_SESSION['survey_'.$surveyid]['s_lang'],'token'=>$clienttoken)); @@ -500,7 +496,7 @@ function submittokens($quotaexit=false) $attrfieldnames=getAttributeFieldNames($surveyid); foreach ($attrfieldnames as $attr_name) { - $aReplacementVars[strtoupper($attr_name)]=$oTokenInformation->$attr_name; + $aReplacementVars[strtoupper($attr_name)] = $token->$attr_name; } $dateformatdatat=getDateFormatData($thissurvey['surveyls_dateformat']); @@ -984,9 +980,9 @@ function buildsurveysession($surveyid,$preview=false) //check if token actually does exist // check also if it is allowed to change survey after completion if ($thissurvey['alloweditaftercompletion'] == 'Y' ) { - $oTokenEntry = TokenDynamic::model($surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); + $oTokenEntry = Token::model(null, $surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); } else { - $oTokenEntry = TokenDynamic::model($surveyid)->find("token=:token AND (completed = 'N' or completed='')", array(':token'=>trim(strip_tags($clienttoken)))); + $oTokenEntry = Token::model(null, $surveyid)->find("token=:token AND (completed = 'N' or completed='')", array(':token'=>trim(strip_tags($clienttoken)))); } if (is_null($oTokenEntry) || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y') ) @@ -1024,15 +1020,15 @@ function buildsurveysession($surveyid,$preview=false) //check if tokens actually haven't been already used $areTokensUsed = usedTokens(trim(strip_tags($clienttoken)),$surveyid); //check if token actually does exist - $oTokenEntry = TokenDynamic::model($surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); + $oTokenEntry = Token::model(null, $surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); if ($thissurvey['alloweditaftercompletion'] == 'Y' ) { - $oTokenEntry = TokenDynamic::model($surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); + $oTokenEntry = Token::model(null, $surveyid)->find('token=:token', array(':token'=>trim(strip_tags($clienttoken)))); } else { - $oTokenEntry = TokenDynamic::model($surveyid)->find("token=:token AND (completed = 'N' or completed='')", array(':token'=>trim(strip_tags($clienttoken)))); + $oTokenEntry = Token::model(null, $surveyid)->find("token=:token AND (completed = 'N' or completed='')", array(':token'=>trim(strip_tags($clienttoken)))); } if (is_null($oTokenEntry) || ($areTokensUsed && $thissurvey['alloweditaftercompletion'] != 'Y') ) { diff --git a/application/helpers/remotecontrol/remotecontrol_handle.php b/application/helpers/remotecontrol/remotecontrol_handle.php index 1b24301dd44..9abcacf8a1f 100644 --- a/application/helpers/remotecontrol/remotecontrol_handle.php +++ b/application/helpers/remotecontrol/remotecontrol_handle.php @@ -588,7 +588,7 @@ public function get_summary($sSessionKey,$iSurveyID, $sStatName) if(in_array($sStatName, $aPermittedTokenStats)) { if (tableExists('{{tokens_' . $iSurveyID . '}}')) - $summary = TokenDynamic::model($iSurveyID)->summary(); + $summary = Token::model(null, $iSurveyID)->summary(); else return array('status' => 'No available data'); } @@ -604,23 +604,23 @@ public function get_summary($sSessionKey,$iSurveyID, $sStatName) { case 'token_count': if (isset($summary)) - return $summary['tkcount']; + return $summary['count']; break; case 'token_invalid': if (isset($summary)) - return $summary['tkinvalid']; + return $summary['invalid']; break; case 'token_sent': if (isset($summary)) - return $summary['tksent']; + return $summary['sent']; break; case 'token_opted_out': if (isset($summary)) - return $summary['tkoptout']; + return $summary['optout']; break; case 'token_completed'; if (isset($summary)) - return $summary['tkcompleted']; + return $summary['completed']; break; case 'completed_responses': return SurveyDynamic::model($iSurveyID)->count('submitdate IS NOT NULL'); @@ -1707,30 +1707,22 @@ public function add_participants($sSessionKey, $iSurveyID, $aParticipantData, $b if (!Yii::app()->db->schema->getTable('{{tokens_' . $iSurveyID . '}}')) return array('status' => 'No token table'); - $aDestinationFields = Yii::app()->db->schema->getTable('{{tokens_' . $iSurveyID . '}}')->getColumnNames(); - $aDestinationFields = array_flip($aDestinationFields); - - foreach ($aParticipantData as &$aParticipant) + $aDestinationFields = array_flip(Token::model(null, $iSurveyID)->getMetaData()->tableSchema->columnNames); + foreach ($aParticipantData as &$aParticipant) { - $aParticipant=array_intersect_key($aParticipant,$aDestinationFields); - TokenDynamic::sid($iSurveyID); - $token = new TokenDynamic; - - if ($new_token_id=$token->insertParticipant($aParticipant)) - { - if ($bCreateToken) - $token_string = TokenDynamic::model()->createToken($new_token_id); - else - $token_string = ''; - - $aParticipant = array_merge($aParticipant, array( - 'tid' => $new_token_id, - 'token' => $token_string, - )); - } - else - { - $aParticipant=false; + $token = new Token('insert', $iSurveyID); + $token->setAttributes(array_intersect_key($aParticipant,$aDestinationFields)); + if ($bCreateToken) + { + $token->generateToken(); + } + if ($token->save()) + { + $aParticipant = $token->getAttributes(); + } + else + { + $aParticipant = false; } } return $aParticipantData; @@ -1767,13 +1759,13 @@ public function delete_participants($sSessionKey, $iSurveyID, $aTokenIDs) $aResult=array(); foreach($aTokenIDs as $iTokenID) { - $tokenidExists = TokenDynamic::model($iSurveyID)->findByPk($iTokenID); - if (!isset($tokenidExists)) + $token = Token::model(null, $iSurveyID)->findByPk($iTokenID); + if (!isset($token)) $aResult[$iTokenID]='Invalid token ID'; else { SurveyLink::deleteTokenLink(array($iTokenID), $iSurveyID); - if(TokenDynamic::model($iSurveyID)->deleteRecords(array($iTokenID))) + if($token->delete()) $aResult[$iTokenID]='Deleted'; else $aResult[$iTokenID]='Deletion went wrong'; @@ -1860,7 +1852,7 @@ public function set_participant_properties($sSessionKey, $iSurveyID, $iTokenID, if(!tableExists("{{tokens_$iSurveyID}}")) return array('status' => 'Error: No token table'); - $oToken = TokenDynamic::model($iSurveyID)->findByPk($iTokenID); + $oToken = Token::model(null, $iSurveyID)->findByPk($iTokenID); if (!isset($oToken)) return array('status' => 'Error: Invalid tokenid'); @@ -1868,30 +1860,17 @@ public function set_participant_properties($sSessionKey, $iSurveyID, $iTokenID, // Remove fields that may not be modified unset($aTokenData['tid']); - $aBasicDestinationFields=array_flip(TokenDynamic::model()->tableSchema->columnNames); - $aTokenData=array_intersect_key($aTokenData,$aBasicDestinationFields); - $aTokenAttributes = $oToken->getAttributes(); + $aBasicDestinationFields = array_flip($oToken->getTableSchema()->columnNames); + $aTokenData = array_intersect_key($aTokenData,$aBasicDestinationFields); if (empty($aTokenData)) return array('status' => 'No valid Data'); - foreach($aTokenData as $sFieldName=>$sValue) - { - $oToken->$sFieldName=$sValue; - try - { - $bSaveResult=$oToken->save(); - $aResult[$sFieldName]=$bSaveResult; - //unset fields that failed - if (!$bSaveResult) - $oToken->$sFieldName=$aTokenAttributes[$sFieldName]; - } - catch(Exception $e) - { - $oToken->$sFieldName=$aTokenAttributes[$sFieldName]; - } - } - return $aResult; + $oToken->setAttributes($aTokenData); + if ($oToken->save()) + { + return $oToken->attributes; + } } else return array('status' => 'No permission'); @@ -1929,9 +1908,9 @@ public function list_participants($sSessionKey, $iSurveyID, $iStart=0, $iLimit=1 return array('status' => 'Error: No token table'); if($bUnused) - $oTokens = TokenDynamic::model($iSurveyID)->findAll(array('condition'=>"completed = 'N'", 'limit' => $iLimit, 'offset' => $iStart)); + $oTokens = Token::model(null, $iSurveyID)->incomplete()->findAll(array('limit' => $iLimit, 'offset' => $iStart)); else - $oTokens = TokenDynamic::model($iSurveyID)->findAll(array('limit' => $iLimit, 'offset' => $iStart)); + $oTokens = Token::model(null, $iSurveyID)->findAll(array('limit' => $iLimit, 'offset' => $iStart)); if(count($oTokens)==0) return array('status' => 'No Tokens found'); diff --git a/application/models/TokenDynamic.php b/application/models/TokenDynamic.php index 62264573868..75ce5a0aa5a 100644 --- a/application/models/TokenDynamic.php +++ b/application/models/TokenDynamic.php @@ -96,33 +96,7 @@ public function rules() } - /** - * Returns summary information of this token table - * - * @access public - * @return array - */ - public function summary() - { - $sid = self::$sid; - if(Yii::app()->db->schema->getTable("{{tokens_$sid}}")){ - $data=Yii::app()->db->createCommand() - ->select("COUNT(*) as tkcount, - SUM(CASE WHEN (token IS NULL OR token='') THEN 1 ELSE 0 END) as tkinvalid, - SUM(CASE WHEN (sent!='N' AND sent<>'') THEN 1 ELSE 0 END) as tksent, - SUM(CASE WHEN (emailstatus LIKE 'OptOut%') THEN 1 ELSE 0 END) as tkoptout, - SUM(CASE WHEN (completed!='N' and completed<>'') THEN 1 ELSE 0 END) as tkcompleted - ") - ->from("{{tokens_$sid}}") - ->queryRow(); - } - else - { - $data=false; - } - return $data; - } /** * Checks to make sure that all required columns exist in this tokens table diff --git a/application/tests/staticCalls.php b/application/tests/staticCalls.php index 6564858275b..16689a8ffee 100644 --- a/application/tests/staticCalls.php +++ b/application/tests/staticCalls.php @@ -67,7 +67,6 @@ function checkStatic($line) 'ParticipantAttribute::', 'Tokens::', 'UserGroup::', -'TokenDynamic::', 'Condition::', 'Survey_Common_Action::', 'Quota::', diff --git a/application/views/admin/responses/browseindex_view.php b/application/views/admin/responses/browseindex_view.php index c8c0ef22403..370c3027a14 100644 --- a/application/views/admin/responses/browseindex_view.php +++ b/application/views/admin/responses/browseindex_view.php @@ -9,11 +9,11 @@ - + - - - + + +
eT("Token summary"); ?>
eT("Total records in this token table"); ?>
eT("Total records in this token table"); ?>
eT("Total invitations sent"); ?>
eT("Total surveys completed"); ?>
eT("Total with no unique Token"); ?>
eT("Total invitations sent"); ?>
eT("Total surveys completed"); ?>
eT("Total with no unique Token"); ?>
diff --git a/application/views/admin/token/tokensummary.php b/application/views/admin/token/tokensummary.php index baad3c01d04..21cf89b3cba 100644 --- a/application/views/admin/token/tokensummary.php +++ b/application/views/admin/token/tokensummary.php @@ -8,7 +8,7 @@ eT("Total records in this token table"); ?> - + @@ -16,7 +16,7 @@ eT("Total with no unique Token"); ?> - + @@ -24,7 +24,7 @@ eT("Total invitations sent"); ?> - + @@ -32,7 +32,7 @@ eT("Total opted out"); ?> - + @@ -40,7 +40,7 @@ eT("Total surveys completed"); ?> - +