Skip to content

Commit

Permalink
Token controller now handles table initialization
Browse files Browse the repository at this point in the history
git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_ci@10383 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
dionet committed Jun 30, 2011
1 parent 3ba134d commit c29b1b7
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 91 deletions.
2 changes: 2 additions & 0 deletions application/config/database.php
Expand Up @@ -59,5 +59,7 @@
$db['default']['port'] = 'default';
$db['default']['databasetabletype'] = 'myISAM';

$config['dbdriver'] = $db['default']['dbdriver'];

/* End of file database.php */
/* Location: ./application/config/database.php */
196 changes: 105 additions & 91 deletions application/controllers/admin/tokens.php
Expand Up @@ -80,22 +80,23 @@ function index($surveyid)
$tokenexists=tableExists('tokens_'.$surveyid);
if (!$tokenexists) //If no tokens table exists
{
//_newtokentable($surveyid);
show_error("No token table! TODO: Implement new token table function.");
self::_newtokentable($surveyid);
}
else
{
$data['clang']=$clang;
$data['thissurvey']=$thissurvey;
$data['imageurl'] = $this->config->item('imageurl');
$data['surveyid']=$surveyid;

$this->load->model("tokens_dynamic_model");
$data['queries']=$this->tokens_dynamic_model->tokensSummary($surveyid);

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

$data['clang']=$clang;
$data['thissurvey']=$thissurvey;
$data['imageurl'] = $this->config->item('imageurl');
$data['surveyid']=$surveyid;

$this->load->model("tokens_dynamic_model");
$data['queries']=$this->tokens_dynamic_model->tokensSummary($surveyid);

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

/**
Expand Down Expand Up @@ -879,17 +880,62 @@ function _handletokenform($surveyid,$subaction,$tokenid="")

}

/**
* Show dialogs and create a new tokens table
*/
function _newtokentable($surveyid)
{
/*
//if (isset($_POST['createtable']) && $_POST['createtable']=="Y" && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
$clang=$this->limesurvey_lang;
if($this->input->post("createtable")=="Y" && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{
//Tokens Dynamic
if ($execresult==0 || $execresult==1)
{
$this->load->dbforge();
$this->dbforge->add_field("tid int(11) NOT NULL AUTO_INCREMENT");
$fields = array(
'firstname' => array('type' => 'VARCHAR', 'constraint' => 40),
'lastname' => array('type' => 'VARCHAR', 'constraint' => 40),
'email' => array('type' => 'TEXT'),
'emailstatus' => array('type' => 'TEXT'),
'token' => array('type' => 'VARCHAR', 'constraint' => 36),
'language' => array('type' => 'VARCHAR', 'constraint' => 25),
'sent' => array('type' => 'VARCHAR', 'constraint' => 17, 'default' => 'N'),
'remindersent' => array('type' => 'VARCHAR', 'constraint' => 17, 'default' => 'N'),
'remindercount' => array('type' => 'INT', 'constraint' => 11, 'default' => 0),
'completed' => array('type' => 'VARCHAR', 'constraint' => 17, 'default' => 'N'),
'usesleft' => array('type' => 'INT', 'constraint' => 11, 'default' => 1),
'validfrom' => array('type' => 'DATETIME'),
'validuntil' => array('type' => 'DATETIME'),
'mpid' => array('type' => 'INT', 'constraint' => 11)
);
$this->dbforge->add_field($fields);

//$tabname = "{$dbprefix}tokens_{$surveyid}"; # not using db_table_name as it quotes the table name (as does CreateTableSQL)
/*$taboptarray = array('mysql' => 'ENGINE='.$databasetabletype.' CHARACTER SET utf8 COLLATE utf8_unicode_ci',
'mysqli' => 'ENGINE='.$databasetabletype.' CHARACTER SET utf8 COLLATE utf8_unicode_ci');
$dict = NewDataDictionary($connect);
$sqlarray = $dict->CreateTableSQL($tabname, $createtokentable, $taboptarray);
$execresult=$dict->ExecuteSQLArray($sqlarray, false);
$createtokentableindex = $dict->CreateIndexSQL("{$tabname}_idx", $tabname, array('token'));
$dict->ExecuteSQLArray($createtokentableindex, false) or safe_die ("Failed to create token table index<br />$createtokentableindex<br /><br />".$connect->ErrorMsg());
if ($connect->databaseType == 'mysql' || $connect->databaseType == 'mysqli')
{
$query = 'CREATE INDEX idx_'.$tabname.'_efl ON '.$tabname.' ( email(120), firstname, lastname )';
$result=$connect->Execute($query) or safe_die("Failed Rename!<br />".$query."<br />".$connect->ErrorMsg());
}*/

$this->dbforge->add_key('tid', TRUE);
$this->dbforge->add_key("token");
//$this->dbforge->add_key(array('email (120)', 'firstname', 'lastname'));
$this->dbforge->create_table("tokens_{$surveyid}");

self::_getAdminHeader();
self::_showMessageBox($clang->gT("Token control"),
$clang->gT("A token table has been created for this survey.")." (\"".$this->db->dbprefix("tokens_$surveyid")."\")<br /><br />\n"
."<input type='submit' value='"
.$clang->gT("Continue")."' onclick=\"window.open('".site_url("admin/tokens/index/$surveyid")."', '_top')\" />\n");
self::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
/*if ($execresult==0 || $execresult==1)
{
$tokenoutput .= "\t</div><div class='messagebox ui-corner-all'>\n"
."<font size='1'><strong><center>".$clang->gT("Token table could not be created.")."</center></strong></font>\n"
.$clang->gT("Error").": \n<font color='red'>" . $connect->ErrorMsg() . "</font>\n"
Expand All @@ -901,97 +947,65 @@ function _newtokentable($surveyid)
."</div>\n";
} else {
$createtokentableindex = $dict->CreateIndexSQL("{$tabname}_idx", $tabname, array('token'));
$dict->ExecuteSQLArray($createtokentableindex, false) or safe_die ("Failed to create token table index<br />$createtokentableindex<br /><br />".$connect->ErrorMsg());
if ($connect->databaseType == 'mysql' || $connect->databaseType == 'mysqli')
{
$query = 'CREATE INDEX idx_'.$tabname.'_efl ON '.$tabname.' ( email(120), firstname, lastname )';
$result=$connect->Execute($query) or safe_die("Failed Rename!<br />".$query."<br />".$connect->ErrorMsg());
}
$tokenoutput .= "\t</div><p>\n"
.$clang->gT("A token table has been created for this survey.")." (\"".$dbprefix."tokens_$surveyid\")<br /><br />\n"
."<input type='submit' value='"
.$clang->gT("Continue")."' onclick=\"window.open('$scriptname?action=tokens&amp;sid=$surveyid', '_top')\" />\n";
}
}*/
return;
}
elseif (returnglobal('restoretable') == "Y" && returnglobal('oldtable') && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
elseif (returnglobal('restoretable') == "Y" && $this->input->post("oldtable") && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{
$query = db_rename_table(returnglobal('oldtable') , db_table_name_nq("tokens_$surveyid"));
$result=$connect->Execute($query) or safe_die("Failed Rename!<br />".$query."<br />".$connect->ErrorMsg());
//$query = db_rename_table($this->input->post("oldtable") , $this->db->dbprefix("tokens_$surveyid"));
//$result=$connect->Execute($query) or safe_die("Failed Rename!<br />".$query."<br />".$connect->ErrorMsg());
$this->dbforge->rename_table($this->input->post("oldtable") , $this->db->dbprefix("tokens_$surveyid"));

self::_getAdminHeader();
self::_showMessageBox($clang->gT("Import old tokens"),
$clang->gT("A token table has been created for this survey and the old tokens were imported.")." (\"".$this->db->dbprefix("tokens_$surveyid")."\")<br /><br />\n"
."<input type='submit' value='"
.$clang->gT("Continue")."' onclick=\"window.open('".site_url("admin/tokens/index/$surveyid")."', '_top')\" />\n");
self::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));

$tokenoutput .= "\t</div><div class='messagebox ui-corner-all'>\n"
/*$tokenoutput .= "\t</div><div class='messagebox ui-corner-all'>\n"
."<div class='header ui-widget-header'>".$clang->gT("Import old tokens")."</div>"
."<br />".$clang->gT("A token table has been created for this survey and the old tokens were imported.")." (\"".$dbprefix."tokens_$surveyid\")<br /><br />\n"
."<input type='submit' value='"
.$clang->gT("Continue")."' onclick=\"window.open('$scriptname?action=tokens&amp;sid=$surveyid', '_top')\" />\n"
."</div>\n";
."</div>\n";*/
return;
}
else
{
$query=db_select_tables_like("{$dbprefix}old\_tokens\_".$surveyid."\_%");
$result=db_execute_num($query) or safe_die("Couldn't get old table list<br />".$query."<br />".$connect->ErrorMsg());
$tcount=$result->RecordCount();
$this->load->model("tokens_dynamic_model");
$result=$this->tokens_dynamic_model->getOldTableList($surveyid);
$tcount=$result->num_rows();
if ($tcount > 0)
{
while($rows=$result->FetchRow())
{
$oldlist[]=$rows[0];
}
}
$tokenoutput .= "\t</div><div class='messagebox ui-corner-all'>\n"
."<div class='warningheader'>".$clang->gT("Warning")."</div>\n"
."<br /><strong>".$clang->gT("Tokens have not been initialised for this survey.")."</strong><br /><br />\n";
if (bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{
$tokenoutput .= $clang->gT("If you initialise tokens for this survey then this survey will only be accessible to users who provide a token either manually or by URL.")
."<br /><br />\n";
$thissurvey=getSurveyInfo($surveyid);
if ($thissurvey['anonymized'] == 'Y')
{
$tokenoutput .= "".$clang->gT("Note: If you turn on the -Anonymized responses- option for this survey then LimeSurvey will mark your completed tokens only with a 'Y' instead of date/time to ensure the anonymity of your participants.")
."<br /><br />\n";
}
$tokenoutput .= $clang->gT("Do you want to create a token table for this survey?");
$tokenoutput .= "<br /><br />\n";
$tokenoutput .= "<input type='submit' value='"
.$clang->gT("Initialise tokens")."' onclick=\"".get2post("$scriptname?action=tokens&amp;sid=$surveyid&amp;createtable=Y")."\" />\n";
$tokenoutput .= "<input type='submit' value='"
.$clang->gT("No, thanks.")."' onclick=\"window.open('{$scriptname}?sid=$surveyid', '_top')\" /></div>\n";
}
else
{
$tokenoutput .= $clang->gT("You don't have the permission to activate tokens.");
$tokenoutput .= "<input type='submit' value='"
.$clang->gT("Back to main menu")."' onclick=\"window.open('{$scriptname}?sid=$surveyid', '_top')\" /></div>\n";
}
// Do not offer old postgres token tables for restore since these are having an issue with missing index
if ($tcount>0 && $databasetype!='postgres' && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{
$tokenoutput .= "<br /><div class='header ui-widget-header'>".$clang->gT("Restore options")."</div>\n"
."<div class='messagebox ui-corner-all'>\n"
."<form method='post' action='$scriptname?action=tokens'>\n"
.$clang->gT("The following old token tables could be restored:")."<br /><br />\n"
."<select size='4' name='oldtable' style='width:250px;'>\n";
foreach($oldlist as $ol)
{
$tokenoutput .= "<option>".$ol."</option>\n";
}
$tokenoutput .= "</select><br /><br />\n"
."<input type='submit' value='".$clang->gT("Restore")."' />\n"
."<input type='hidden' name='restoretable' value='Y' />\n"
."<input type='hidden' name='sid' value='$surveyid' />\n"
."</form></div>\n";
foreach ($result->result_array() as $rows)
{
$oldlist[]=$rows[0];
}
$data['oldlist'] = $oldlist;
}

$data['clang']=$clang;
$thissurvey=getSurveyInfo($surveyid);
$data['thissurvey']=$thissurvey;
$data['imageurl'] = $this->config->item('imageurl');
$data['surveyid']=$surveyid;
$data['tcount']=$tcount;
$this->load->config("database");
$data['databasetype']=$this->config->item("dbdriver");

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

return;
}*/
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions application/models/tokens_dynamic_model.php
Expand Up @@ -120,4 +120,10 @@ function insertTokens($surveyid,$data)
{
return $this->db->insert("tokens_".$surveyid, $data);
}

function getOldTableList ($surveyid)
{
$this->load->helper("database");
return $this->db->query(db_select_tables_like($this->db->dbprefix("old\_tokens\_".$surveyid."\_%")));
}
}
84 changes: 84 additions & 0 deletions application/views/admin/Token/tokenwarning.php
@@ -0,0 +1,84 @@
<div class='menubar'>
<div class='menubar-title ui-widget-header'>
<strong><?php echo $clang->gT("Token control");?> </strong> <?php echo htmlspecialchars($thissurvey['surveyls_title']);?>
</div></div><div class='messagebox ui-corner-all'>
<div class='warningheader'><?php echo $clang->gT("Warning");?></div>
<br /><strong><?php echo $clang->gT("Tokens have not been initialised for this survey.");?></strong><br /><br />
<?php if (bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{
echo $clang->gT("If you initialise tokens for this survey then this survey will only be accessible to users who provide a token either manually or by URL.");
?><br /><br />

<?php if ($thissurvey['anonymized'] == 'Y')
{
echo $clang->gT("Note: If you turn on the -Anonymized responses- option for this survey then LimeSurvey will mark your completed tokens only with a 'Y' instead of date/time to ensure the anonymity of your participants.");
?><br /><br />
<?php }
echo $clang->gT("Do you want to create a token table for this survey?");
?>
<br /><br />
<input type='submit' value='<?php echo $clang->gT("Initialise tokens");?>' onclick="<?php echo get2post(site_url("admin/tokens/index/$surveyid")."?action=tokens&amp;sid=$surveyid&amp;createtable=Y");?>" />
<input type='submit' value='<?php echo $clang->gT("No, thanks.");?>' onclick="window.open('<?php echo site_url("admin/survey/view/$surveyid");?>', '_top')" /></div>
<?php }
else
{
echo $clang->gT("You don't have the permission to activate tokens.");?>
<input type='submit' value='<?php echo $clang->gT("Back to main menu");?>' onclick="window.open('<?php echo site_url("admin/survey/view/$surveyid");?>', '_top')" /></div>

<?php }

// Do not offer old postgres token tables for restore since these are having an issue with missing index
if ($tcount>0 && $databasetype!='postgres' && bHasSurveyPermission($surveyid, 'surveyactivation','update'))
{ ?>
<br /><div class='header ui-widget-header'><?php echo $clang->gT("Restore options");?></div>
<div class='messagebox ui-corner-all'>
<form method='post' action='$scriptname?action=tokens'>
<?php echo $clang->gT("The following old token tables could be restored:");?><br /><br />
<select size='4' name='oldtable' style='width:250px;'>
<?php foreach($oldlist as $ol)
{
echo "<option>".$ol."</option>\n";
} ?>
</select><br /><br />
<input type='submit' value='<?php echo $clang->gT("Restore");?>' />
<input type='hidden' name='restoretable' value='Y' />
<input type='hidden' name='sid' value='$surveyid' />
</form></div>
<?php } ?>
<script type="text/javascript">
<!--
for(i=0; i<document.forms.length; i++)
{
var el = document.createElement('input');
el.type = 'hidden';
el.name = 'checksessionbypost';
el.value = 'cvtp4rts86';
document.forms[i].appendChild(el);
}

function addHiddenElement(theform,thename,thevalue)
{
var myel = document.createElement('input');
myel.type = 'hidden';
myel.name = thename;
theform.appendChild(myel);
myel.value = thevalue;
return myel;
}

function sendPost(myaction,checkcode,arrayparam,arrayval)
{
var myform = document.createElement('form');
document.body.appendChild(myform);
myform.action =myaction;
myform.method = 'POST';
for (i=0;i<arrayparam.length;i++)
{
addHiddenElement(myform,arrayparam[i],arrayval[i])
}
addHiddenElement(myform,'checksessionbypost',checkcode)
myform.submit();
}

//-->
</script>

0 comments on commit c29b1b7

Please sign in to comment.