Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Automated / Web Service Based Token Generation

More
12 years 6 months ago #65695 by brian
Hello friends -

This looks like a really neat tool! I am very impressed with my basic playing around with it. That being said, I'm wondering if anyone has crafted a solution to my problem.

I have a need to periodically (daily) generate tokens in an automated fashion. I'm may need to send out my emails with tokens from a source other than lime for a variety of valid and otherwise imposed reasons. What I can't do is manually load a .csv up every day that defines the recipients I'd like to toggle with a request to take a survey. The idea is that as another system generates specific pieces of data (customer X had work Y performed at location Z), and I'd like to toggle that customer on taking a survey. The volume is up and down, but most importantly, happens throughout the day and week and I need notifications to go out without me doing anything manually.

That being said, it looks like I need guidance on a few things:

1) Creating tokens in an automated fashion. I'm competent enough on the jdbc end to write inserts into lime_tokens_##### if necessary, but thought there might be capacities within the tool that would allow me to bypass this step. I'd also love the have the tool generate the token hash instead of providing one on my end, though I could make one up.

2) Automate notifications to token holders that haven't gotten emailed yet. Is there any way to handle this from a data level? Would a HTTP POST to a php page make this work? I'm new to PHP, could could generate a GET or POST from java to make it happen.

Any insight would be greatly appreciated.

brian
The topic has been locked.
More
12 years 6 months ago #65697 by timbee
to your first point, how do you expect limesurvey to handle automatic creation of tokens when it doesn't know when they should be updated?

I'm in a similar situation right now. We have our own web application that has a customer table. When we make updates to the customer record (let's say we have to change their name that had a typo) the application makes an additional call to the lime_tokens_# table. Hypothetically speaking that is :) still implementing. I think the trick also is to make our web app aware of which lime survey # (and thus lime_tokens_#) should be linked with our customer data.
The topic has been locked.
More
12 years 6 months ago #65698 by brian
Hi Timbee -

Well, I'd be willing to make a call, http get / post from java land to the appropriate .php page to create the tokens if I have to. My problem is that I don't know much about php, I was hoping there was a cleaner solution besides psuedo-crawling into the application to simulate someone clicking on a button. And as I think about this some more, I can make a hash tag just as easily at insert time.

Similarly with initiating the email to un-notified parties; I can handle that externally if necessary, but would rather leverage what is already there. It just seems like the last step is missing (or I am missing it), namely, a way to make the functions fire without actually being there.

brian
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
12 years 5 months ago #65847 by Mazi
@1: When do you want to create tokens? Which event should trigger the creation of the tokens and the insert of token data like First name, last name and email (which are the required fields)?

@2: The easiest way for automatically sending invitations/reminders is to create a cronjob which is run daily and pics the according users which meet certain criteria (survey not filled out yet, no invitation/reminder sent yet, email address exists, ...). These will then receive an invitation/reminder automatically.

For issue #1 a cronjob might also do the trick. Simply connect a customized PHP script to your DB and read out all user which were added today and then add them to the token table of a predefined survey (it could also send the invitation right away).

As I see it there are no inbuild feature whcih could help you out. Some custom coding is needed anyway to connect the systems and automate certain processes.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
12 years 5 months ago #65971 by timbee

brian wrote: I'd be willing to make a call, http get / post from java land to the appropriate .php page to create the tokens if I have to. My problem is that I don't know much about php,


You don't really need to make any php-originated http calls. As long as your java has access to the same database, all you should have to keep track of is which unique token id is associated to which unique user id on your system. So let's say you have a survey (lime_survey_353), and you initially enable tokens, so it creates the lime_tokens_353 table. Ten minutes later your java program gets a request to initiate a survey for user_id 21, so all the java app has to do is an SQL insert statement on the lime_tokens_353 table. Generate a random token yourself, and then in your java user table make sure that user_id 21 also has a token_id field so you can link the two. Then as Mazi suggests, use a cron job to send mail out every 5 minutes or something like that.

What sucks about this is that there is a separate token table for each survey that you have to keep track of. This was resolved with the "central participants database" in version 2, but we're sticking with the 1.9x branch for stability. Not sure how tested the CI version is...
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
12 years 5 months ago #66042 by Mazi

timbee wrote: What sucks about this is that there is a separate token table for each survey that you have to keep track of. This was resolved with the "central participants database" in version 2, but we're sticking with the 1.9x branch for stability. Not sure how tested the CI version is...

You have just volunteered to test and report bugs: www.limesurvey.org/en/component/content/...is-ready-for-testing

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The following user(s) said Thank You: timbee
The topic has been locked.
More
12 years 5 months ago #66062 by timbee

Mazi wrote: You have just volunteered to test and report bugs: www.limesurvey.org/en/component/content/...is-ready-for-testing


Lol, I like your style Mazi. Well, I can tell you it install's fine, haha. We have too much going on right now for a thorough testing ... I am interested in digging under the hood of the new CI architecture though, maybe on my personal time.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
12 years 5 months ago #66068 by Mazi

timbee wrote:

Mazi wrote: You have just volunteered to test and report bugs: www.limesurvey.org/en/component/content/...is-ready-for-testing


Lol, I like your style Mazi. Well, I can tell you it install's fine, haha. We have too much going on right now for a thorough testing ... I am interested in digging under the hood of the new CI architecture though, maybe on my personal time.

That would be great. We really need some help testing new features. There are probably quite a few bugs. You'll get a cookie for each ticket you create ;-)

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
11 years 4 months ago - 11 years 4 months ago #87147 by michaeljean
Replied by michaeljean on topic Automated / Web Service Based Token Generation
Hi all,

I had a similar problem. I wanted to automatically register users for a second survey once they finished the first one. Here is the script I used to solve it. Change all the stuff that looks like <this> with your stuff. Doing it this way means that the invitation is sent automatically once the users are registered.

export-users.php:
Code:
<?php
/*
 * Gets survey participants that have completed survey 1 and automatically registers them for survey 2.
 * Also saves a CSV file called "export-users.csv" on the web host in the survey application folder.
 * This file can be used to manually import the user tokens in case something goes wrong with the 
 * automation.
 */
 
/*
 *
 * Set the database objects
 *
 */
/* Database name */
$db_name = '<my_db_name>';
 
/* Database username */
$db_user = '<my_db_user>';
 
/* Database password */
$db_password = '<my_db_password>';
 
/* Database hostname */
$db_host = '<my_db_host>';
 
$table_name_survey_1_tokens = "lime_tokens_<my_survey_id>"; /* Database survey 1 user tokens table name */
 
/* Database where clause to get users who have completed survey 1 and their closing date is past */
$where_clause = " where date(a.completed) < now()";
 
$field_list = "a.tid,a.firstname,a.lastname,a.email,a.emailstatus,a.token,a.language"; /* The field list as text. */
 
$field_list_array = explode(',',str_replace('a.', '', $field_list) ); /* Field list as an array; required by the fputcsv function. */
 
$csv_filename = 'export-users.csv'; /* Output CSV filename */
 
 
/***** Start of processing *****/
 
/*
 *
 * Establish database connection
 *
 */
$conn = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(nl2br(mysql_error($conn) . "\n"));
echo nl2br("Established database connection.\n");
 
 
/*
 *
 * Execute SQL query to find survey users that have completed survey 1
 *
 */
$query = "SELECT " . $field_list . " FROM " . $table_name_survey_1_tokens . " as a" . $where_clause;
$result = mysql_query($query, $conn) or die(mysql_error($conn));
echo nl2br("Found survey users that have completed survey 1.\n");
 
 
/*
 *
 * Output data rows (if atleast one row exists)
 *
 */
$row = mysql_fetch_assoc($result);
$fp = fopen($csv_filename, 'w');
fputcsv($fp,$field_list_array);
while ($row) {
    fputcsv($fp,$row,',','"');
    register($row[firstname],$row[lastname],$row[email]);
    echo nl2br("User " . $row['firstname'] . " " . $row['lastname'] . " at " . $row['email'] . " was registered.\n");
    $row = mysql_fetch_assoc($result);
}
fclose($fp);
echo nl2br("CSV file export-users.csv created.");
 
 
/*
 *
 * Function to register users.
 *
 */
function register($firstname,$lastname,$email)
{
 
$data = array(
    'lang' => 'en',
    'sid' => '<my_survey_id>',
    'register_firstname' => $firstname,
    'register_lastname' => $lastname,
    'register_email' => $email 
);
 
$post_str = '';
foreach($data as $key=>$val) {
    $post_str .= $key.'='.urlencode($val).'&amp;';
}
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '<my_URL>/index.php/register/index/sid/<my_survey_id>' );
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
 
}
 
 
?>
Last edit: 11 years 4 months ago by michaeljean.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 3 months ago #89397 by Mazi
Instead of reading data from a CSV file, why don't you just modify the script and call it at the end of survey 1 to automatically create a new token for the current user at survey 2 and then automatically redirect him to that survey and attaching the generated token so they can start with survey 2 right away?

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
11 years 3 months ago #89442 by michaeljean
Replied by michaeljean on topic Automated / Web Service Based Token Generation
From a technical perspective, yes of course, however my client has a product with a closing date. At the time of purchase the client's customer is asked to fill in survey 1 and provide their closing date in one of the survey questions. I needed a way to send the invitation only after this closing date has occurred (run as a cron job on a weekly basis to check it), so I wrote the script with that in mind, as a way to accommodate their business process. Also, an administrator can re-run the user import manually using the CSV if there was a problem.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose