Automatically pass a token to and execute a Python script

More
4 months 1 week ago #155737 by horseshrink
horseshrink created the topic: Automatically pass a token to and execute a Python script
Have developed / operationalized a clinical interview and use Python / RC2 to extract the results and post-process them into a plain text format that I then copy / paste into an electronic health record. Works reasonably well. Useful.

Is there a way to automatically execute my Python post-processing script when the patient completes the survey? The patient's token number would have to be passed to the script.

Please Log in to join the conversation.

More
4 months 1 week ago #155747 by Ben_V
Ben_V replied the topic: Automatically pass a token to and execute a Python script
Hi David,

What about the built-in "endurl" function to trigger a redirection to your python script (or any intermediate php script if necessary)?

manual.limesurvey.org/URL_fields#End_URL_field

Note that you can dynamically access {SAVEDID} & {TOKEN} values in the completed message/page , so you can use several other redirect methods like AJAX calls or meta refresh redirection:
<meta http-equiv="refresh" content="0;url=http://example.org/myscript.php?savedid{SAVEDID}&token={TOKEN}">

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: LouisGac, horseshrink

Please Log in to join the conversation.

More
4 months 6 days ago #155766 by horseshrink
horseshrink replied the topic: Automatically pass a token to and execute a Python script
Will let you know how it works.

Please Log in to join the conversation.

More
2 months 3 weeks ago #157128 by horseshrink
horseshrink replied the topic: Automatically pass a token to and execute a Python script
Am a fair novice still with website design / management (knowledge has exploded much faster over the decades than I can learn it as a clinical professional), but got the "End URL" Python script to work via CGI.

"http://localhost:8080/cgi-bin/Psychiatric%20Diagnostic%20Interview%20Print.py?urltoken={TOKEN}"

Had to read up on how to configure the Python scripts to run via CGI (URLs below.) I now know there are options other than CGI, and I don't know all the security ramifications, but this will all be run only within the walls of a LAN. No WAN use. Am just trying to make it work.

I set the following:
  • Survey properties -> Edit survey text elements and settings -> Presentation & navigation -> Automatically load URL when survey complete = "On"

Links helpful to me for this:
The following user(s) said Thank You: DenisChenu, Ben_V

Please Log in to join the conversation.

More
2 months 3 weeks ago #157139 by Ben_V
Ben_V replied the topic: Automatically pass a token to and execute a Python script
Hi David,

thank you for your feedback & links. I'm glad to see that you get all your system working now.

Really nice and useful LS use case!

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

More
2 months 2 weeks ago - 2 months 2 weeks ago #157237 by DenisChenu
DenisChenu replied the topic: Automatically pass a token to and execute a Python script
Hi David,

Did you know if PHP have another way to 'knock' a python script ? PHP exec is always disable for security purpose but maybe another solution.

Else : you can call the same line directly in PHP using manual.limesurvey.org/AfterSurveyComplete and curl.

Some example of php script : framagit.org/SondagePro-LimeSurvey-plugi...enByResponse.php#L58
You just need the public function afterSurveyComplete()
and put a curl

Quickly done (not tested)
public function afterSurveyComplete() {
  $surveyId =$this->getEvent()->get('surveyId');
  $token = isset($_SESSION['survey_'.$surveyId]['token'] ? $_SESSION['survey_'.$surveyId]['token'] : null;
  if($token) {
    // There are surely another way to call a py script ???
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/cgi-bin/Psychiatric%20Diagnostic%20Interview%20Print.py?surveyid=".$surveyId."&urltoken=".$token);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
  }
}

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .
Last Edit: 2 months 2 weeks ago by DenisChenu.
The following user(s) said Thank You: Ben_V, horseshrink

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now