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) && 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) && 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', 'https://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) && 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.