Skip to content

Commit

Permalink
Porting the functionality 'Add dummy tokens' to the Yii PHP framework…
Browse files Browse the repository at this point in the history
… - GCI Task #7127236 by gautamgupta

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_yii@11591 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
GautamGupta committed Dec 5, 2011
1 parent a9e9612 commit e7ffe1b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 64 deletions.
12 changes: 6 additions & 6 deletions application/controllers/admin/browse.php
Expand Up @@ -23,10 +23,10 @@
* @subpackage Backend
*/
class browse extends Survey_Common_Action {

private $yii;
private $controller;

public function run()
{
$this->yii = Yii::app();
Expand All @@ -53,11 +53,11 @@ function action($surveyid = null, $subaction = null, $var1 = null, $var2 = null,
// Some test in response table
if (!isset($surveyid) && !isset($subaction)) //NO SID OR ACTION PROVIDED
{
show_error("\t<div class='messagebox ui-corner-all'><div class='header ui-widget-header'>"
die("\t<div class='messagebox ui-corner-all'><div class='header ui-widget-header'>"
. $clang->gT("Browse Responses")."</div><div class='warningheader'>"
. $clang->gT("Error")."\t</div>\n"
. $clang->gT("You have not selected a survey to browse.")."<br />\n"
."<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('$scriptname', '_top')\" /><br />\n"
."<input type='submit' value='".$clang->gT("Main Admin Screen")."' onclick=\"window.open('".$this->getController()->createUrl("/admin")."', '_top')\" /><br />\n"
."</div>");
return;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ function action($surveyid = null, $subaction = null, $var1 = null, $var2 = null,
if (isset($rlanguage)) {$data['rlanguage'] = $rlanguage;}
$data['next'] = $next;
$data['last'] = $last;

$this->controller->render("/admin/browse/browseidheader_view", $data);

$idresult = db_execute_assoc($idquery) or safe_die ("Couldn't get entry<br />$idquery<br />".$connect->ErrorMsg());
Expand Down Expand Up @@ -696,7 +696,7 @@ function action($surveyid = null, $subaction = null, $var1 = null, $var2 = null,
$data['last'] = $last;
$data['next'] = $next;
$data['end'] = $end;

$this->controller->render("/admin/browse/browseallheader_view", $data);

foreach ($dtresult->readAll() as $dtrow)
Expand Down
93 changes: 43 additions & 50 deletions application/controllers/admin/tokens.php
Expand Up @@ -46,6 +46,8 @@ public function run($sa)
$this->route('import', array('surveyid'));
elseif ($sa == 'importldap')
$this->route('importldap', array('surveyid'));
elseif ($sa == 'adddummys')
$this->route('adddummys', array('surveyid', 'subaction'));
}

/**
Expand Down Expand Up @@ -627,34 +629,34 @@ function delete($surveyid, $tokenid=null,$limit=50,$start=0,$order=false,$search
/**
* Add dummy tokens form
*/
function adddummys($surveyid)
function adddummys($surveyid, $subaction = '')
{
$surveyid = sanitize_int($surveyid);
$clang=$this->limesurvey_lang;
$clang = $this->getController()->lang;
if(!bHasSurveyPermission($surveyid, 'tokens','create'))
{
show_error("no permissions"); // TODO Replace
die("No permissions."); // TODO Replace
}

if ($this->input->post("subaction"))
$this->getController()->loadHelper("surveytranslator");

if (!empty($subaction) && $subaction == 'add')
{
$this->load->model("tokens_dynamic_model");
$_POST=$this->input->post();
$this->getController()->loadLibrary('Date_Time_Converter');
$dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']);

//Fix up dates and match to database format
if (trim($_POST['validfrom'])=='') {
$_POST['validfrom']=null;
}

else
{
$datetimeobj = new Date_Time_Converter(trim($_POST['validfrom']), $dateformatdetails['phpdate'].' H:i');
$_POST['validfrom'] =$datetimeobj->convert('Y-m-d H:i:s');
$_POST['validfrom'] = null;
} else {
$datetimeobj = new Date_Time_Converter(array(trim($_POST['validfrom']), $dateformatdetails['phpdate'].' H:i'));
$_POST['validfrom'] = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim($_POST['validuntil'])=='') {$_POST['validuntil']=null;}
else
{
$datetimeobj = new Date_Time_Converter(trim($_POST['validuntil']), $dateformatdetails['phpdate'].' H:i');
$_POST['validuntil'] =$datetimeobj->convert('Y-m-d H:i:s');
$datetimeobj = new Date_Time_Converter(array(trim($_POST['validuntil']), $dateformatdetails['phpdate'].' H:i'));
$_POST['validuntil'] = $datetimeobj->convert('Y-m-d H:i:s');
}

$santitizedtoken='';
Expand All @@ -668,28 +670,28 @@ function adddummys($surveyid)
'emailstatus' => 'OK',
'token' => $santitizedtoken,
'language' => sanitize_languagecode($_POST['language']),
'sent' => 'N',
'sent' => 'N',
'remindersent' => 'N',
'completed' => 'N',
'usesleft' => $_POST['usesleft'],
'validfrom' => $_POST['validfrom'],
'validuntil' => $_POST['validuntil']);

// add attributes
$attrfieldnames=GetAttributeFieldnames($surveyid);
$attrfieldnames = GetAttributeFieldnames($surveyid);
foreach ($attrfieldnames as $attr_name)
{
$data[$attr_name]=$_POST[$attr_name];
$data[$attr_name] = $_POST[$attr_name];
}
//$tblInsert=db_table_name('tokens_'.$surveyid);
$amount = sanitize_int($_POST['amount']);
$tokenlength = sanitize_int($_POST['tokenlen']);

for ($i=0; $i<$amount;$i++){
$dataToInsert = $data;
$dataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['firstname']);
$dataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['lastname']);
$dataToInsert['email'] = str_replace('{TOKEN_COUNTER}',"$i",$dataToInsert['email']);
$dataToInsert['firstname'] = str_replace('{TOKEN_COUNTER}', $i, $dataToInsert['firstname']);
$dataToInsert['lastname'] = str_replace('{TOKEN_COUNTER}', $i, $dataToInsert['lastname']);
$dataToInsert['email'] = str_replace('{TOKEN_COUNTER}', $i, $dataToInsert['email']);

$isvalidtoken = false;
while ($isvalidtoken == false)
Expand All @@ -700,46 +702,37 @@ function adddummys($surveyid)
$existingtokens[$newtoken]=null;
}
}

$dataToInsert['token'] = $newtoken;
//$tblInsert=db_table_name('tokens_'.$surveyid);
//$inresult = $connect->AutoExecute($tblInsert, $dataToInsert, 'INSERT') or safe_die ("Add new record failed:<br />\n$inquery<br />\n".$connect->ErrorMsg());
$inresult = $this->tokens_dynamic_model->insertToken($surveyid,$dataToInsert);
Tokens_dynamic::insertToken($surveyid, $dataToInsert);
}

self::_getAdminHeader();
self::_showMessageBox($clang->gT("Success"),
$this->getController()->_getAdminHeader();
$this->getController()->_showMessageBox($clang->gT("Success"),
$clang->gT("New dummy tokens were added.")."<br /><br />\n<input type='button' value='"
.$clang->gT("Display tokens")."' onclick=\"window.open('".site_url("admin/tokens/browse/$surveyid")."', '_top')\" />\n");
self::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
.$clang->gT("Display tokens")."' onclick=\"window.open('".$this->getController()->createUrl("admin/tokens/sa/browse/surveyid/$surveyid")."', '_top')\" />\n");
$this->getController()->_getAdminFooter("http://docs.limesurvey.org", $clang->gT("LimeSurvey online manual"));

} else {
$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");
//get token length from survey settings
$this->load->model("surveys_model");
$query = $this->surveys_model->getSomeRecords(array("tokenlength"),array("sid"=>$surveyid));
$row = $query->row_array();
$tokenlength = $row['tokenlength'];
$tkcount = Tokens_dynamic::totalRecords($surveyid);
$tokenlength = Yii::app()->db->createCommand()->select('tokenlength')->from('{{surveys}}')->where('sid='.$surveyid)->query()->readColumn(0);

//if tokenlength is not set or there are other problems use the default value (15)
if(!isset($tokenlength) || $tokenlength == '')
{
if(empty($tokenlength)) {
$tokenlength = 15;
}

$data['clang']=$clang;
$thissurvey=getSurveyInfo($surveyid);
$data['thissurvey']=$thissurvey;
$data['imageurl'] = $this->config->item('imageurl');
$data['surveyid']=$surveyid;
$data['tokenlength']=$tokenlength;
$data['dateformatdetails']=getDateFormatData($this->session->userdata('dateformat'));
$data['clang'] = $clang;
$thissurvey = getSurveyInfo($surveyid);
$data['thissurvey'] = $thissurvey;
$data['imageurl'] = Yii::app()->getConfig('imageurl');
$data['surveyid'] = $surveyid;
$data['tokenlength'] = $tokenlength;
$data['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat']);

self::_getAdminHeader();
$this->load->view("admin/token/tokenbar",$data);
$this->load->view("admin/token/dummytokenform",$data);
self::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
$this->getController()->_getAdminHeader();
$this->getController()->render("/admin/token/tokenbar",$data);
$this->getController()->render("/admin/token/dummytokenform",$data);
$this->getController()->_getAdminFooter("http://docs.limesurvey.org", $clang->gT("LimeSurvey online manual"));
}
}

Expand Down
11 changes: 6 additions & 5 deletions application/models/Tokens_dynamic.php
Expand Up @@ -104,15 +104,15 @@ public function summary()

return $data;
}

public function totalRecords($iSurveyID)
{
$tksq = "SELECT count(tid) FROM {{tokens_{$iSurveyID}}}";
$tksr = Yii::app()->db->createCommand($tksq)->query();
$tkr = $tksr->read();
return $tkr["count(tid)"];
}

public function ctquery($iSurveyID,$SQLemailstatuscondition,$tokenid=false,$tokenids=false)
{
$ctquery = "SELECT * FROM {{tokens_{$iSurveyID}}} WHERE ((completed ='N') or (completed='')) AND ((sent ='N') or (sent='')) AND token !='' AND email != '' $SQLemailstatuscondition";
Expand All @@ -132,10 +132,11 @@ public function emquery($iSurveyID,$SQLemailstatuscondition,$maxemails,$tokenid=
Yii::app()->loadHelper("database");
return db_select_limit_assoc($emquery,$maxemails);
}
function insertToken($iSurveyID,$data)

function insertToken($iSurveyID, $data)
{
return Yii::app()->db->createCommand()->insert("{{tokens_".$iSurveyID.'}}', $data)->query();
self::sid($iSurveyID);
return Yii::app()->db->createCommand()->insert(self::tableName(), $data);
}
}
?>
4 changes: 1 addition & 3 deletions application/views/admin/token/dummytokenform.php
@@ -1,7 +1,7 @@
<div class='header ui-widget-header'>
<?php echo $clang->gT("Create dummy tokens"); ?>
</div>
<form id='edittoken' class='form30' method='post' action='<?php echo site_url("admin/tokens/adddummys/$surveyid");?>'>
<form id='edittoken' class='form30' method='post' action='<?php echo $this->createUrl("admin/tokens/sa/adddummys/surveyid/$surveyid/subaction/add");?>'>
<ul>
<li><label>ID:</label>
<?php echo $clang->gT("Auto");?>
Expand Down Expand Up @@ -48,7 +48,5 @@
?>
</ul><p>
<input type='submit' value='<?php echo $clang->gT("Add dummy tokens");?>' />
<input type='hidden' name='subaction' value='insertdummys' />
<input type='hidden' name='sid' value='$surveyid' /></p>
</form>

0 comments on commit e7ffe1b

Please sign in to comment.