Skip to content

Commit

Permalink
New feature: New RemoteControl 2 XML-RPC function "add_participants" …
Browse files Browse the repository at this point in the history
…to add one or more entries to the token table

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_ci@11299 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
c-schmitz committed Oct 29, 2011
1 parent 60f2967 commit 87ad65d
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 206 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/dataentry.php
Expand Up @@ -2089,7 +2089,7 @@ function insert()
"sent"=>date_shift(date("Y-m-d H:i:s"), "Y-m-d H:i", $timeadjust),
"completed"=>"N");
$this->load->model('tokens_dynamic_model');
$this->tokens_dynamic_model->insertTokens($surveyid,$tokendata);
$this->tokens_dynamic_model->insertToken($surveyid,$tokendata);
//$connect->AutoExecute(db_table_name("tokens_".$surveyid), $tokendata,'INSERT');
$dataentryoutput .= "<font class='successtitle'>".$clang->gT("A token entry for the saved survey has been created too.")."</font><br />\n";

Expand Down
117 changes: 110 additions & 7 deletions application/controllers/admin/remotecontrol.php
Expand Up @@ -20,7 +20,8 @@ function index()
$config['functions']['get_session_key'] = array('function' => 'remotecontrol.getSessionKey');
$config['functions']['release_session_key'] = array('function' => 'remotecontrol.releaseSessionKey');
$config['functions']['delete_survey'] = array('function' => 'remotecontrol.deleteSurvey');
// $config['functions']['create_survey'] = array('function' => 'remotecontrol.createSurvey');
$config['functions']['add_participants'] = array('function' => 'remotecontrol.addParticipants');
// $config['functions']['create_survey'] = array('function' => 'remotecontrol.createSurvey');

$this->xmlrpcs->initialize($config);
$this->xmlrpcs->serve();
Expand Down Expand Up @@ -48,7 +49,7 @@ function getSessionKey($request)
'created'=>date( 'Y-m-d H:i:s'),
'modified'=>date( 'Y-m-d H:i:s'),
'sessdata'=>$sUserName));
return $this->xmlrpc->send_response(array($sSessionKey,'array'));
return $this->xmlrpc->send_response(array($sSessionKey,'string'));
}
else
{
Expand Down Expand Up @@ -95,10 +96,87 @@ function deleteSurvey($request)
return $this->xmlrpc->send_response($response);
}
else
return $this->xmlrpc->send_error_message('2', 'No permission');
return $this->xmlrpc->send_error_message('2', 'No permission');
}
}

/**
* XML-RPC routine to add a participant to a token table
* Returns the inserted data including additional new information like the Token entry ID and the token
*
* @param array $request Array containing the following elements (in that order):
* - Session key (string)
* - Survey ID (integer)
* - ParticipantData (array)
* - CreateToken (boolean) Sets if a token should be created for each ParticipantData record
*
*
*/
function addParticipants($request)
{
if (!is_object($request)) die();
$aParameters = $request->output_parameters();

if (!isset($aParameters['0'],$aParameters['1'],$aParameters['2'],$aParameters['3']))
{
return $this->xmlrpc->send_error_message('3', 'Missing parameters');
}
$sSessionKey=$aParameters['0'];
$iSurveyID=(int)$aParameters['1'];
$aParticipantData=$aParameters['2'];
$bCreateTokenKey=$aParameters['3'];

if($this->_checkSessionKey($sSessionKey))
{
if(bHasSurveyPermission($iSurveyID,'tokens','create'))
{
if (!$this->db->table_exists('tokens_'.$iSurveyID))
{
return $this->xmlrpc->send_error_message('11', 'No token table');
}
$aFieldnames=$this->db->list_fields('tokens_'.$iSurveyID);
$aFieldnames=array_flip($aFieldnames);
$this->load->model("tokens_dynamic_model");
foreach ($aParticipantData as &$aParticipant)
{
Foreach ($aParticipant as $sFieldname=>$sValue)
{
if (!isset($aFieldnames[$sFieldname])) unset($aParticipant[$sFieldname]);
}
if ($this->tokens_dynamic_model->insertToken($iSurveyID,$aParticipant))
{
$iNewTokenEntryID=$this->db->insert_id();
$aParticipant=array_merge($aParticipant, array('tid'=>(string)$iNewTokenEntryID,
'token'=>$this->tokens_dynamic_model->createToken($iSurveyID,$iNewTokenEntryID))
);
};
}
$iTokensInserted=$this->db->affected_rows();
$aOutArray=array(array(array($this->array_to_xml_rpc_struct($aParticipantData),'struct')),'struct');
return $this->xmlrpc->send_response($aOutArray);
}
else
return $this->xmlrpc->send_error_message('2', 'No permission');
}
}

/**
* Converts a result_array() response set to a XLMRPC struct array
*
* @param mixed $array Array to convert
*/
function array_to_xml_rpc_struct($array)
{

$xml_rpc_rows=array();
for ($i=0;$i<count($array);++$i)
{
$xml_rpc_rows[$i]=array($array[$i],'struct');
}
return $xml_rpc_rows;
}


/**
* Tries to login with username and password
*
Expand Down Expand Up @@ -188,20 +266,45 @@ function _checkSessionKey($sSessionKey)
*/
function test()
{
$iSurveyID=552489;
$aParticipantsData=array(
array(
array(array('firstname'=>'firstname1','lastname'=>'lastname1','dummy'=>'lastname1'),'struct'),
array(array('firstname'=>'firstname2','lastname'=>'lastname2'),'struct'),
)
,'array');



$this->load->library('xmlrpc');
// $this->xmlrpc->set_debug(TRUE);
// $this->xmlrpc->set_debug(TRUE);
$this->xmlrpc->server(site_url('admin/remotecontrol'), 80);
$this->xmlrpc->method('release_session_key');

$request = array('zgn4phgzybs7j92rn89ayhvwrxu6pxp72acjgc9e8xe92fb95pvy72khfaffmtvv','12345');

$this->xmlrpc->method('get_session_key');
$request = array('admin','password');
$this->xmlrpc->request($request);

if ( ! $this->xmlrpc->send_request())
{
echo $this->xmlrpc->display_error();
}
else
print_r($this->xmlrpc->display_response());
{
$sSessionKey=($this->xmlrpc->display_response());
}

$this->xmlrpc->method('add_participants');
$request = array(array($sSessionKey,'string'),array($iSurveyID,'integer'),$aParticipantsData, array(true,'boolean'));
$this->xmlrpc->request($request,'struct');

if ( ! $this->xmlrpc->send_request())
{
echo $this->xmlrpc->display_error();
}
else
var_dump($this->xmlrpc->display_response());

}

}
Expand Down
52 changes: 10 additions & 42 deletions application/controllers/admin/tokens.php
Expand Up @@ -258,7 +258,7 @@ function browse($surveyid,$limit=50,$start=0,$order=false,$searchstring=false)

$clang=$this->limesurvey_lang;
$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);

self::_js_admin_includes(base_url()."scripts/admin/tokens.js");

Expand Down Expand Up @@ -399,7 +399,7 @@ function addnew($surveyid)
{
// AutoExecute
//$inresult = $connect->AutoExecute($tblInsert, $data, 'INSERT') or safe_die ("Add new record failed:<br />\n$inquery<br />\n".$connect->ErrorMsg());
$inresult = $this->tokens_dynamic_model->insertTokens($surveyid,$data);
$inresult = $this->tokens_dynamic_model->insertToken($surveyid,$data);
$data['success']=true;
}
else
Expand Down Expand Up @@ -556,7 +556,7 @@ function delete($surveyid, $tokenid=null,$limit=50,$start=0,$order=false,$search
}
if(isset($tokenids) && count($tokenids)>0) {
if(implode(", ", $tokenids) != "") {
$this->tokens_dynamic_model->deleteTokens($surveyid,$tokenids);
$this->tokens_dynamic_model->deleteRecords($surveyid,$tokenids);
$tokenoutput = $clang->gT("Marked tokens have been deleted.");
} else {
$tokenoutput = $clang->gT("No tokens were selected for deletion");
Expand Down Expand Up @@ -660,7 +660,7 @@ function adddummys($surveyid)
$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->insertTokens($surveyid,$dataToInsert);
$inresult = $this->tokens_dynamic_model->insertToken($surveyid,$dataToInsert);
}

self::_getAdminHeader();
Expand All @@ -671,7 +671,7 @@ function adddummys($surveyid)

} else {
$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");
//get token length from survey settings
$this->load->model("surveys_model");
Expand Down Expand Up @@ -713,7 +713,7 @@ function managetokenattributes($surveyid)
}

$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");

$this->load->model("surveys_model");
Expand Down Expand Up @@ -836,7 +836,7 @@ function email($surveyid,$tokenids=null)
}

$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");

$this->load->model("surveys_model");
Expand Down Expand Up @@ -1091,7 +1091,7 @@ function remind($surveyid)
}

$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");

$this->load->model("surveys_model");
Expand Down Expand Up @@ -1444,40 +1444,8 @@ function tokenify($surveyid)
else
{
//get token length from survey settings
$this->load->model("surveys_model");
$tlresult = $this->surveys_model->getSomeRecords(array("tokenlength"),array("sid"=>$surveyid));
$tlrow = $tlresult->row_array();
$tokenlength = $tlrow['tokenlength'];

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

// select all existing tokens
$this->load->model("tokens_dynamic_model");
$ntresult = $this->tokens_dynamic_model->getSomeRecords(array("token"),$surveyid,FALSE,"token");
foreach ($ntresult->result_array() as $tkrow)
{
$existingtokens[$tkrow['token']]=null;
}
$newtokencount = 0;
$tkresult = $this->tokens_dynamic_model->selectEmptyTokens($surveyid);
foreach ($tkresult->result_array() as $tkrow)
{
$isvalidtoken = false;
while ($isvalidtoken == false)
{
$newtoken = sRandomChars($tokenlength);
if (!isset($existingtokens[$newtoken])) {
$isvalidtoken = true;
$existingtokens[$newtoken]=null;
}
}
$itresult = $this->tokens_dynamic_model->updateToken($surveyid,$tkrow['tid'],$newtoken);
$newtokencount++;
}
$newtokencount=$his->tokens_dynamic_model->createTokens($surveyid);
$message=str_replace("{TOKENCOUNT}", $newtokencount, $clang->gT("{TOKENCOUNT} tokens have been created"));
self::_getAdminHeader();
$this->load->view("admin/token/tokenbar",$data);
Expand Down Expand Up @@ -1602,7 +1570,7 @@ function _handletokenform($surveyid,$subaction,$tokenid="")
{
$clang=$this->limesurvey_lang;
$this->load->model("tokens_dynamic_model");
$tkcount=$this->tokens_dynamic_model->totalTokens($surveyid);
$tkcount=$this->tokens_dynamic_model->totalRecords($surveyid);
$this->load->helper("surveytranslator");

if ($subaction == "edit")
Expand Down
2 changes: 1 addition & 1 deletion application/helpers/admin/import_helper.php
Expand Up @@ -4024,7 +4024,7 @@ function XMLImportTokens($sFullFilepath,$iSurveyID,$sCreateMissingAttributeField

//$query=$connect->GetInsertSQL($tablename,$insertdata);

$result = $CI->tokens_dynamic_model->insertTokens($iSurveyID,$insertdata) or show_error($clang->gT("Error").": Failed to insert data<br />");
$result = $CI->tokens_dynamic_model->insertToken($iSurveyID,$insertdata) or show_error($clang->gT("Error").": Failed to insert data<br />");

$results['tokens']++;
}
Expand Down
1 change: 1 addition & 0 deletions application/helpers/common_helper.php
Expand Up @@ -451,6 +451,7 @@ function bHasSurveyPermission($iSID, $sPermission, $sCRUD, $iUID=null)
if (!in_array($sCRUD,array('create','read','update','delete','import','export'))) return false;
$sCRUD=$sCRUD.'_p';
$iSID = (int)$iSID;
if ($iSID==0) return false;
$aSurveyPermissionCache = $CI->config->item("aSurveyPermissionCache");

if (is_null($iUID))
Expand Down
2 changes: 1 addition & 1 deletion application/models/failed_login_attempts_model.php
Expand Up @@ -66,7 +66,7 @@ function addAttempt($sIp)

$timestamp = date("Y-m-d H:m:s");
$this->db->where('ip', $ip);
$oData=$this->db->select('failed_login_attempts');
$oData=$this->db->get('failed_login_attempts');
if ($oData->num_rows()>0)
{
//$query = "UPDATE ".db_table_name('failed_login_attempts')
Expand Down

0 comments on commit 87ad65d

Please sign in to comment.