Welcome to the LimeSurvey Community Forum

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

Check Quota via EM or JS

  • Liod_DS
  • Liod_DS's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
8 years 1 month ago #131908 by Liod_DS
Check Quota via EM or JS was created by Liod_DS
Hi all,
I'm using LS for a while now, and I think it's a very nice tool to work with..

I've fixed a lot of issues by reading this forum but now I need the help of the community to solve this problem..

Can I check quotas at runtime?

I'll make an example:
I need to test 300 subject, and I have setup the quotes like this:

1 hidden question "completed" y/n
2 equation question at the end of the survey "{completed = "Y"}"

it works fine... but the respondent has to answer at ALL questions to go QuotaFull (it's very annoying after 10-15 minutes of survey)

I need to check my "completed" value at the beginning of the survey...

My last chance is to query the DB with javascript and Json ... but maybe someone could help me with Limesurveys tools!

Additional Infos:
LS 2.06+ Build 160129

(Ps sorry for the bad grammar)

keep improving
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago #131935 by tpartner
Replied by tpartner on topic Check Quota via EM or JS
I think the only solution is, as you say, an AJAX call to a remote PHP file that queries the database for number of completes.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago #131942 by holch
Replied by holch on topic Check Quota via EM or JS
I think it is worth to make a feature request.

Limesurvey should have something like this - "Close survey when received x completed questionnaires".

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • Liod_DS
  • Liod_DS's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
8 years 1 month ago #131985 by Liod_DS
Replied by Liod_DS on topic Check Quota via EM or JS
Thanks for the answers, i will try the php/js hard way :lol:

Any usefull hint for start ? (i've make a few script with jquery_autocomplete that works with Json and PHP but never something like this..)

If i'll get it to work i will post the script for future use ;D

Thanks All

keep improving
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago #132023 by tpartner
Replied by tpartner on topic Check Quota via EM or JS
I agree with Holch that there seems to be a demand for this so I spent a little time on an AJAX approach. Denis could probably roll it into a plugin but he's got a bigger brain than me.

I would handle the maximum-allowed thing on the welcome screen so, if over maximum, the survey never really gets started.

1) Place something like this in the welcome message:
Code:
<p class="normal-welcome">This is the normal welcome text...<p>
<p class="survey-full-welcome" style="display:none">Sorry, this survey is full.<p>

2) Add this function to template.js:
Code:
function handleCompletes(sid, maxCount) {
  // Identify some stuff...
  var surveyRoot = location.pathname.split('index.php')[0];
  var templateName = $('head link[href*="template.css"]').attr('href').replace(/\/template.css/, '').split('/templates/')[1];
  var templatePath = surveyRoot+'upload/templates/'+templateName+'/';
 
  // AJAX call
  $.ajax({
    url: templatePath+'db_query.php', // The PHP file
    async: true,
    cache : false, // No caching in IE
    data: {
      sid: sid // Send the survey ID to the PHP file
    },
    success: function(results){
      // If we're at  or above the maximum completes
      if($.isNumeric(results) &amp;&amp; results >= maxCount) {
        // Handle the welcome text elements
        $('.normal-welcome').hide();
        $('.survey-full-welcome').show();
        // Remove all of the buttons
        $('#movenextbtn, #loadallbtn, #clearall, #therearexquestions').remove();
 
        // Redirect here if you want
      }
    },
    error: function(){
      alert('Could not connect!');
    }
  }); 
}

3) Call the function from the welcome message (after the text in step 1) like this. In this example, I am limiting to 10 completes:
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){
    handleCompletes({SID}, 10);
  });
</script>

4) Add a PHP file (in this example, called db_query.php) to the template directory. The PHP file will contain one of the two code blocks below. The code blocks are for MySQL and there are probably better ways to do it but no one ever accused me of being a PHP expert. The first block queries the database directly, the second uses the RemoteControl 2 API . If using the API, you will also need to include the jsonRPCClient files in your template directory. In either case, you will need to define the constants at the beginning of the file.

Direct database query:
Code:
<?php
 
  define( 'LS_DB_HOST', 'localhost' );
  define( 'LS_DB_NAME', 'database_name' );
  define( 'LS_DB_USER', 'database_user_name' );
  define( 'LS_DB_PASS', 'database_user_password' );
  define( 'LS_DB_TABLEPREFIX', 'lime_' );
 
  $sid = $_GET["sid"];
 
  if(ctype_alnum($sid) &amp;&amp; strlen($sid) == 6) { // Valid SID format
 
    $link = mysqli_connect(LS_DB_HOST, LS_DB_USER, LS_DB_PASS, LS_DB_NAME);
 
    // Check the connection 
    if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
    }
 
    // Query the survey table for all completed responses
    if ($result = mysqli_query($link, "SELECT * FROM ".LS_DB_TABLEPREFIX."survey_".$sid." WHERE `submitdate` IS NOT NULL")) {
 
      // Count number of rows in the result */
      $completedCount = mysqli_num_rows($result);
 
      // Return that value
      echo $completedCount;
 
      // Close result set 
      mysqli_free_result($result);
    }
 
    // Close the connection 
    mysqli_close($link);
 
  }
  else { // Invalid SID format
    die( 'Invalid format!' );
  }
 
?>

API call:
Code:
<?php
 
  require_once 'jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://path/to/limeSurvey/');  
  define( 'LS_USER', 'limesurvey_user_name' );
  define( 'LS_PASSWORD', 'limesurvey_user_password' );
  define( 'LS_DB_HOST', 'localhost' );
  define( 'LS_DB_NAME', 'database_name' );
  define( 'LS_DB_USER', 'database_user_name' );
  define( 'LS_DB_PASS', 'database_user_password' );
  define( 'LS_DB_TABLEPREFIX', 'lime_' );
 
  $sid = $_GET["sid"];
 
  if(ctype_alnum($sid) &amp;&amp; strlen($sid) == 6) { // Valid SID format
 
    // Instantiate a new RPC client
    $myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
    // Get a session key
    $sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
    // Call the API with the get_summary() method
    $completedCount = $myJSONRPCClient->get_summary($sessionKey, $sid, 'completed_responses');
 
    // Return the completed surveys count
    echo $completedCount;
 
    // Release the session key
    $myJSONRPCClient->release_session_key( $sessionKey );
  }
  else { // Invalid SID format
    die( 'Invalid format!' );
  }
 
?>

Attached below are a template and sample survey. This template uses the direct database query method but everything is included for the API method.

File Attachment:

File Name: test_ajax_...uery.zip
File Size:93 KB


File Attachment:

File Name: limesurvey...4867.lss
File Size:12 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Liod_DS
The topic has been locked.
  • Liod_DS
  • Liod_DS's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
8 years 1 month ago #132027 by Liod_DS
Replied by Liod_DS on topic Check Quota via EM or JS
You have done really too much posting all the code :blush:

Thanks a lot!

I will test and deploy it tomorrow morning..

You guys have been really helpfull

keep improving
The topic has been locked.
  • Liod_DS
  • Liod_DS's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
8 years 1 month ago #132046 by Liod_DS
Replied by Liod_DS on topic Check Quota via EM or JS
The script works great!

Now I just have to fit it in the various request of our surveys, really useful method, it adds a lot of options for survey's setup!

keep improving
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago #132109 by holch
Replied by holch on topic Check Quota via EM or JS
hehehehe, not putting a maximum numbers had costs the company a few bucks when I first started in online market research.

We had ordered a sample of n=300 drivers of premium cars and I thought the panel would stop when we reached 300 and they thought we would close when we reached 300. We started field on a Friday (you should never do that anyway, but that was well at the beginning, we were still learning) and by Monday we had more than 300 respondents. We never expected it to be that quick, because the target group was difficult, but over the weekend we had the full sample and more.

Panel made us pay the extra interviews. My boss was actually quite cool about it back then. He said he thought the same way as I did. We ordered 300 and it is the panels responsibility to deliver 300. If they deliver more, it is would be their problem. But at the end we had to pay anyway. We took it as costs for learning. ;-)

Never made that error again.

But with this feature missing in the Limesurvey core it actually causes that LS can't really be used with panels. Because not everyone can or wants to apply Tpartner's brilliant work arounds.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
More
6 years 4 months ago #161215 by yurigar
Replied by yurigar on topic Check Quota via EM or JS
Hello,

I have a similar problem, what I need to do is to show a table with the quota completed count at the beginning of the survey.

I wonder if this could be done with an equation type question.

The problem is I not only need the report on the total number of completes, but the completes for each quota.

In the quotas admin section there is a column named completeCount which shows the values I need to show at the beginning of the survey with an equation question.

Can anyone help about how to show that value? I tried with the function getQuotaCompletedCount($iSurveyId, $quotaid) but is deprecated now. Does something similar exists?

Thank you in advance, Yuri.
The topic has been locked.
  • Liod_DS
  • Liod_DS's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
6 years 4 months ago #161335 by Liod_DS
Replied by Liod_DS on topic Check Quota via EM or JS
Hi,

in this thread you can see 2 ways to solve this problem, the first one can query directly the Database, you can change the query in the PHP file to get almost anything you want from the survey table, if you can access it you can look around for what you need... then you can display the result of the query in the page....

the second path is the API call, i didn't fidn anything like "getResponseCount" or "getQuotaCount" so I THINK(but i really don't know for sure) you have to export and parse all the response with export_responses .... but it can't be efficent ..

So : i didn't find any function that can be helpfull to build a real quota_checker, and the PHP provided in this thread can only count the completed user in the surveys table (not the one with the real response) but i think it's possible to build up a query that can count the response of an answer .... maybe something like this will be implemented in the v.3.0 of LS...

keep improving
The topic has been locked.
More
6 years 4 months ago #161337 by yurigar
Replied by yurigar on topic Check Quota via EM or JS
Thank you very much for your answer! Yuri.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose