Display responses from my survey in real time via PHP or HTML

Plus d'informations
il y a 4 mois 2 semaines #184862 par h0rr0rOrg
Hello... I'm a long-time user of LimeSurvey, but a new user in the cloud. I'm trying to display the responses to my survey on our website.

I've looked at the Remote Control 2 API page, and I've activated the API/Json.

I'm new to Json though, and so I'm confused about the instructions on that page. I don't see any API functions that simply grab the data/responses from the survey. Am I missing something?

This is how we have displayed the responses in the past: www.horror.org/awards/2018readinglist.php
We used a direct connection to our SQL database through PHP to display the data.

Any help you can give to get me over the hump to understanding how JSon works with PHP would be greatly appreciated!!

Thanks so much!
Angel...
Horror.org webmaster

Connexion ou Créer un compte pour participer à la conversation.

LimeSurvey Partners
Plus d'informations
il y a 4 mois 2 semaines #184863 par h0rr0rOrg
I am stalled right at the beginning. I don't see any indication of how to connect to my Limesurvey other than this:

How to use LSRC2
The basic LSRC2 URL is: http://<your_domain>/<your_limesurvey_dir>/index.php/admin/remotecontrol

But what is my domain? What is my Limesurvey dir? Where do I find that information? Is this for the cloud or just for an installation?

Thanks!
Angel...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184864 par h0rr0rOrg

h0rr0rOrg écrit: I am stalled right at the beginning. I don't see any indication of how to connect to my Limesurvey other than this:

How to use LSRC2
The basic LSRC2 URL is: http://<your_domain>/<your_limesurvey_dir>/index.php/admin/remotecontrol

But what is my domain? What is my Limesurvey dir? Where do I find that information? Is this for the cloud or just for an installation?

Thanks!
Angel...


Okay, I figured this out.


The instructions on this page ( manual.limesurvey.org/RemoteControl_2_API ) seem to indicate that I need to call my RPC client.

require_once 'path/to/your/rpcclient/jsonRPCClient.php';

Is this RPC on my website server or on the Limesurvey cloud?

Thanks!
Angel...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184871 par DenisChenu

h0rr0rOrg écrit: Is this RPC on my website server or on the Limesurvey cloud?

In your website … you can't include a file on another server …

It's clearly noted on manual , no ?
manual.limesurvey.org/RemoteControl_2_API#PHP_Example

To include JSON-RPC in your application, you can write an application using the light-weight jsonRPCClient from the jsonrpcphp Github repository.


Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
An error happen ? Before make a new topic : remind the Debug mode .

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184880 par tpartner
Here is a link to a post describing how to use the API in PHP to get a response by token (I couldn't find any examples of the export_responses() method but it will be similar) - www.limesurvey.org/forum/development/118...urvey-rpc-api#182796

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184890 par h0rr0rOrg
Thanks for all this! I am, however, still fumbling. I have the RPC installed, but I can't figure out how to export the responses. I see a lot of methods I don't need.

By token, do you mean the token assigned a particular respondent or does that also have a different meaning?

My survey does not have tokens. It's not secured in that way.

I just want to pull the data into an array that I can then parse with PHP and display.

Is this possible? Without using tokens?

Thanks!!
Angel...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184891 par h0rr0rOrg
I'm getting the following error:
Fatal error: Class 'jsonRPCClient' not found in /home/content/91/8685291/html/members-only/documents/rec-files/new/recs-simple.php on line 27

This is the line it's referencing:
$myJSONRPCClient = new jsonRPCClient( LS_BASEURL,'index.php/admin/remotecontrol' );

And I input my LS_BASEURL like this:
define( 'LS_BASEURL', ' NAME.limesurvey.org/admin/remotecontrol ');
where NAME is the name of my account. I pulled this URL from the Global Settings page. Should I be using the URL of my survey instead or something else?

Thanks for the help!! Troubleshooting sux.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines - il y a 4 mois 2 semaines #184896 par Joffm
Here the first lines of my php file:
<?php
 
require_once 'jsonrpcphp/JsonRPCClient.php';
 
define( 'LS_BASEURL', 'https://www.limesurvey.de/survey');  // adjust this one to your actual LimeSurvey URL
define( 'LS_USER', 'user' );
define( 'LS_PASSWORD', 'pass' );
 
 
// the survey to process
$survey_id=557357;
 
// instantiate a new client
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
// receive session key
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
print_r($sessionKey );
 
if(is_array($sessionKey))
{
    header("Content-type: application/json");
    echo json_encode($sessionKey);
    die();
}
 
/* Get the responses */
$response = $myJSONRPCClient->export_responses(
    $sessionKey,
    $survey_id,
    'json', // Document type : pdf,csv,xls,doc,json
    null, // Language code : null : default from survey
    'all', // Stautus complete|incomplete|all
    null, // Heading : code|full|abbreviated : question text, default code
    null // answer : short|long , default : long
    ); // See http://api.limesurvey.org/classes/remotecontrol_handle.html#method_export_responses or https://github.com/LimeSurvey/LimeSurvey/blob/master/application/helpers/remotecontrol/remotecontrol_handle.php#L2382 or
 

All from the sample in the manual.

You get an array of answers which you download or save to something with your php functions.

Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Dernière édition: il y a 4 mois 2 semaines par Joffm.
Les utilisateur(s) suivant ont remercié: tpartner

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines - il y a 4 mois 2 semaines #184905 par h0rr0rOrg
That was really helpful Joffm! Thank you so much!

I've managed to get some of the data displaying, but my foreach is stopping after the first entry. I can't get it to continue on no matter what I do! I've been at this for hours!

Here's the code I'm currently using (after many iterations).
...$responseArr is my full array.
...'responses' is the code name of the array inside it.
...I can manually increment the $i, but it doesn't increment on the fly, and it still always only shows one piece of data.
...I have managed to get it to show one data set (for one submission), but only by echoing the $row2 after it's been reset (which makes its array only include one submission's data)
	echo '<table border=1 cellpadding=5>';
	$i = 0;
 
foreach ($responseArr as $value)
	{
	$row2 = reset($responseArr['responses'][$i]);
	$title = $row2['title'];
	echo '<tr><td>';
	echo $title . '</td><td><br>' . $i . '</td></tr>';
	++$i;
	}
	echo '</table>';

I'm at a complete loss. Please, if you have time to help, I'm very grateful.
Dernière édition: il y a 4 mois 2 semaines par h0rr0rOrg. Raison: It hid some of the code. Trying to get it visible.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines - il y a 4 mois 2 semaines #184946 par tpartner
Here is an example of how to use the export_responses() method in PHP that works for me:

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
	define( 'LS_USER', 'admin' );
	define( 'LS_PASSWORD', 'password' );
 
	$iSurveyID = 123456;
 
	// Instantiate a new RPC client
	$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
	// Get a session key
	$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
	if(is_array($sSessionKey)) { // Invalid session
		echo $sSessionKey['status'];
	}
	else if($sSessionKey) { // Valid session
 
		$surveyResponses = $myJSONRPCClient->export_responses($sSessionKey, $iSurveyID, 'json', null, 'completed', 'code', 'short', null, null, null);
 
		if(is_array($surveyResponses)) {
			// Oops, print any errors
			print_r($surveyResponses);
		}
		else {
			// Decode the retuned base-64 string and convert to an array
			$decodedString = base64_decode($surveyResponses);
			$aResponses = json_decode($decodedString, True);
 
			// Find the first response ID				
			$aFirstResponse = reset($aResponses['responses'][0]);
 
			echo '<table style="border-collapse: collapse; text-align: left;">';		
				echo '<tr>';				
					// Insert column headers
					foreach($aFirstResponse as $key => $value) {
						echo '<th style="border: 1px solid #CCC; padding: 2px 7px;">'.$key .'</th>';
					}
				echo '</tr>';
				foreach($aResponses['responses'] as $key => $row) {
					echo '<tr>';
						// Insert the data
						foreach(reset($row) as $key => $item) {
							echo '<td style="border: 1px solid #CCC; padding: 2px 7px;">'.$item .'</td>';
						}
					echo '</tr>';
				}
			echo '</table>';
		}
	}
 
	// Release the session key
	$myJSONRPCClient->release_session_key( $sSessionKey );
 
?>


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Pièces jointes :
Dernière édition: il y a 4 mois 2 semaines par tpartner.
Les utilisateur(s) suivant ont remercié: h0rr0rOrg

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184994 par h0rr0rOrg
That is beautiful, Tony! Thanks so much! It got me to the point where I can display all the data like I want.

Now, I'm trying to figure out how to display only specific data.

I have the main array, then "sub-arrays" within it that are the $row.

I want to display only the rows where one of the data points is "poetry".

Then, I only want to display the data related to specific $keys within that row: 'author,' 'title,' etc.

I've tried using nested if statements, and I've tried (in_array()). None of this is working for me.

I suspect it's because of how the foreach is processing the data. I'm asking it to do something too complex.

Does this make any sense? Here's what I want:

if an $item in the $row == 'poetry'
{
echo the row, but only the data in 'author' and 'title';
}

I spent all day (literally) yesterday trying to trial-and-error my way through this and searching the web for a solution. I don't know if I'm too stupid or if I'm just missing a crucial piece of the puzzle.

Thank you to anyone who can help me! Mucho appreciated!
Angel...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184995 par Joffm
Hi,
Did you read this?
https://api.limesurvey.org/classes/remotecontrol_handle.html

I want to display only the rows where one of the data points is "poetry".

Is "poetry" in a well defined column or somewhere in the dataset?

With the "$aFields"-parameter you can select your desired fields.

Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines - il y a 4 mois 2 semaines #184997 par h0rr0rOrg
Hi, Joffm. Thanks for the quick reply!

One of the JSon pairs in my data is "category=>poetry" where category is the Limesurvey 'code' and poetry is the Limesurvey 'answer' data.

I've gotten this far:
$rawResponses = $myJSONRPCClient->export_responses( $sessionKey, $survey_id,
    'json', // Document type : pdf,csv,xls,doc,json
    null, // Language code : null : default from survey
    'complete', // Stautus complete|incomplete|all
    'code', // Heading : code|full|abbreviated : question text, default code
    'short', // answer : short|long , default : long
	null,
	null,
	null
    ); 
 
	if(is_array($rawResponses))
	{
		// Oops, print any errors
		print_r($rawResponses);
	}
	else {
		// There is a valid response...
		// Decode the retuned base-64 string and convert to an array
		$decodedString = base64_decode($rawResponses); // The decodedString variable turns the data into an array and contains the entire array of data for the survey in English
		$responseArr = json_decode($decodedString, True); // Creates the json-decoded array of data for use in PHP.
 
		echo '<table border=1 text-align="left">';
			echo '<tr>';
				// Insert column headers_list
				foreach($responseArr as $key => $value) // $responseArr is the entire array, $key returns the name of the data set 'responses'; $value returns an array of the jSon data pairs with => between
				{
					foreach($responseArr['responses'] as $rownumber => $row) // $rownumber is a numeric value, not an array; $row is an array of all data in each submission.
					{
						echo '<tr>';
						// Inserts the data
						foreach(reset($row) as $headinng => $item) // $heading returns the heading name (of which 'category' is one), $item returns the data, one item at a time
						{
// THE FOLLOWING line is where I need to echo only specified $item data, and if I can nest those criteria, all the better. I need to filter first by $item (APPROVED), then filter by multiple other $items (AUTHOR and TITLE) to choose which to show.
 
							echo '<td style="border: 1px solid #CCC; padding: 2px 7px;">' . $item . '</td>';
						}
					}
				}

I can use an IF statement to pull the rows with "category=>poetry" but it shows the entire row when I echo, and if I try to use nested IF statements, the $item only returns 'poetry' and I get 'poetry' repeated over and over again.

I'm sorry. I'm learning this on the fly. My PHP knowledge is just enough to get me halfway, but not enough to get me all the way with the JSon data format.

I do greatly appreciate all the help you guys are giving me.
Angel...
Dernière édition: il y a 4 mois 2 semaines par h0rr0rOrg. Raison: updating with new info

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines #184998 par Joffm
Hi,
use the last parameter to select the desired fields.

With this setting:
$response = $myJSONRPCClient->export_responses(
    $sessionKey,
    $survey_id,
    'json', // Document type : pdf,csv,xls,doc,json
    null, // Language code : null : default from survey
    'complete', // Stautus complete|incomplete|all
    NULL, // Heading : code|full|abbreviated : question text, default code
    NULL, // answer : short|long , default : long
    1,
    3,
    ['id','token','submitdate','lastpage']
    ); 

I got


Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Pièces jointes :

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 4 mois 2 semaines - il y a 4 mois 2 semaines #185064 par h0rr0rOrg
Hi, Joffm,
Thanks very much for that info. It works great for the data that is auto-generated by Limesurvey, but any data that my users input isn't showing up.

I can see id, submitdate, and lastpage, for example. But my 'author' and 'title' data is just blank. It makes the table cell for it, but there's nothing in it.

I've double-checked the spelling and capitalization on the codes, and they're correct. All lower case. Here's what I input:
['submitdate','lastpage','id','title','author','publisher','category']

And here's the echo line:
echo '<td style="border: 1px solid #CCC; padding: 2px 7px;">A: ' . $item . '</td>';

The first three show up. The last four are blank, though the table cell is created.

I tried reordering them, and I tried showing only 'title', in case it was a question of order, but that didn't work either.

Any idea why that might be?
Thanks a ton for taking time to help me!

Angel...
Dernière édition: il y a 4 mois 2 semaines par h0rr0rOrg.

Connexion ou Créer un compte pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!