Automatically close a project when quota is full using Confirmit APIs (C# .NET) & JSONP – Part2

In the previous post, we’ve covered the .NET application that connects to Confirmit APIs and closes a project.
Let’s see now what we need to do in order to actually call it from a Confirmit survey.

Since the topic is close a project when the quota is reached, we’ll first need a condition node, with a quota check qf(‘quota1’)

In the “then” branch, you continue to use your own scripts, set the respondent status SetStatus(‘quotafull’) and everything else you may have, but not do the redirect yet (if you’re using one)

Add an info question type with the following content. You can use the instruction area as well.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery-jsonp.googlecode.com/files/jquery.jsonp-2.1.4.min.js"></script>

<script>
//hide navigationarea(s)
$(function() {
	$('.navigationarea').hide();
});


function callback(data){
	//if(typeof data.status!="undefined") //you can use the data.status here
	//show navigationarea back...
	$('.navigationarea').show();

	//or redirect them to whatever URL you need
}

$.jsonp({url: "http://your domain to Confirmit Close Project/?projectID=^ProjectID()^",complete: function(data) {}});
</script>

As you can see the only parameter we send is the project ID.

There could be more to add, like showing a div with a message “Please wait!” or an ajax loading gif etc

For a better understanding on how everything works, as soon as the quota is reached, the first respondent who gets in the condition qf(‘quota1’), true now, will trigger the call to the .NET app that closes the study.
After this, it doesn’t mater how many respondents are currently on the link, no one else will made it to the end (of survey). If clicking on the “next” button, the system will answer with the standard message, “Sorry, but this survey has been closed.”

Check an older post, and find out how can you customize it for this particular study (you can also have a redirect right from the theme)

Automatically close a project when quota is full using Confirmit APIs (C# .NET) & JSONP – Part1

Surely by now, if you were in Confirmit world long enough, you must have heard questions about closing studies automatically when a quota is reached.
If you’ve ever asked to do this, I’m pretty sure the answer was Confirmit has nothing to accommodate this.

If you have access to Confirmit APIs means you can say now, I have something!

This solution comprises two parts:

  • An application (done in C# .NET) that connects to the APIs and closes the project
  • The actual call directly from inside the survey (that needs to be closed). For this I’m using a JSONP technique since you never know where the app ends being hosted, plus this offers you a little bit more control over it.

Before starting, make sure you have access to Confirmit APIs (your user has to have some special permission).

Since this is done in .NET you start by creating a new Web site and add (Add web reference) two of the Confirmit WS: LogOn and Authoring, LogOn is needed for authorization reasons.

The links to connect to the APIs are in the following format:
your_confirmit_domain/confirmit/webservices/version/service
where version is your Confirmit version like 15.0 and service is one of the available services Confirmit APIs has here is an example: LogOn.asmx

Ok so now after you’ve added them you may see something similar
Confirmit WS reference

AuthConfirm(): authenticate and get a key back.

    private bool AuthConfirm(){

        Confirmit.LogOn logOn = new Confirmit.LogOn();
        try{
            key = logOn.LogOnUser(ConfigurationManager.AppSettings["username"]
                , ConfigurationManager.AppSettings["password"]);
        }
        catch{}
        if (key.Length == 0){       // Login failed 
            Response.Write("callback({\"status\":\"error\"});"); 
            return false;
        }
        else return true;
    }

CloseProject() sets the SurveyStatus to Closed

    protected void CloseProject(string pid)
    {
        myAuth = new Confirmit.Authoring();
        mySchema = myAuth.GetProjectInfo(key, pid);

        try {
            myAuth.SetSurveyStatus(key, pid, Confirmit.SurveyStatusType.Closed);
            myAuth.Update(key, pid, mySchema);
            Response.Write("callback({\"status\":\"closed\"});");
        }
        catch
        {
            Response.Write("callback({\"status\":\"failed\"});");
        }
        
    }

Let’s put them together now

   protected void Page_Load(object sender, EventArgs e)
    {   
        string projectId = Request.QueryString["projectId"];
        if (projectId != null && projectId != ""){
            if (AuthConfirm()) CloseProject(projectId);
        }
        else
            Response.Write("callback({\"status\":\"projectId\"});");

    }