How to use Surveys in CakePHP with very little development..

How to use Surveys in CakePHP with very little development hours.

CakePHP is one of the most favorable PHP frameworks out there and we understand that PHP developer want to be enabled to extend it to use Survey. In this article we will go through how to use CakePHP and Surveys in a way so that you can automatically let your users answer surveys from your Examinare account. Of course some steps can be different from what you would like todo in your application. We are here to help of course, just send an email to support@examinare.com and we will help you along the way.

 

We assume that you have already setup an CakePHP environment and have some PHP coding skills. If you don’t then we can help you with custom projects. Ask for a quote at support@examinare.com

 

Step 1: Create a new Examinare account or a Developer Account and activate the API functions.

phone-surveys-question-example

In your Examinare account you will need to activate the API functionality inside Settings -> Advanced Settings. You will then be presented with an API Key and a CompanyID. Save these for later. If you prefer to use a different to use a special API key for this solution then see our Mobile API key functionality.

 

Step 2: Build your Survey inside Examinare.

Build your survey inside the Examinare platform. Make sure you deploy it as a private survey.

 

Step 3: Build a registration form.

We start by creating a new form for this. In that form we will add the name and the email of the person who will get the survey.

So we start with this code:


PHP Example



echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'Enter your name:'));
echo $this->Form->input('email', array('label' => 'Enter your email address:'));
echo $this->Form->end('Take the Survey');

We can of course if possible skip this step if you have already authorized the person who already loggedin. To retrieve that information depends on your CakePHP enviroment.

So when this form has been created we also need to catch the details and create the recipient and redirect the person to the form.

So add this into your controller:


PHP Example

if($this->request->is('post')){
        
	include_once("examinare-api.php");
	$key["apicompany"]="XXXX";						
	$key["apikey"]="YYYYYY";	

	$apicreate = new ExaminareAPIConnector($key["apicompany"],$key["apikey"],"");
	$apicreate->setCommand("addRecipient");
	$recipient_r = Array();
                        
	$recipient_r["name"]=$this->request->data['MyModel']['name'];
	$recipient_r["email"]=$this->request->data['MyModel']['email'];	$recipient_r["group"]="My Survey Group";
                        
	$apicreate->Data($recipient_r);	

	$person=$apicreate->Execute();
	// Above code will create the recipient, now we have to mark the person for the survey. 
	if($person["contacts"]["contact"]["contactID"]!=""){
		
		$sendcontactid = $person["contacts"]["contact"]["contactID"];
		
		$apimark = new ExaminareAPIConnector($key["apicompany"],$key["apikey"],"");
		$apimark ->setCommand("markRecipientsToSurvey");
		$apimark ->Data(array('surveyID' => “SURVEY ID FOR YOUR SURVEY”, 'user_1' => $sendcontactid));
		$done=$callapi->Execute();

		// Ok so now we get the Survey ID from the system with the help of the recipients ContactID.
			$apisurveylink = new ExaminareAPIConnector($key["apicompany"],$key["apikey"],"");
			$apisurveylink->setCommand("listSurveysByUser");
	 
			$apisurveylink->Data(array("contactID"=>"Contact ID in Examinare"));
						
			$contactcard_r=$apisurveylink->Execute();		
			
			$urltoredirect = $contactcard_r[“contacts”][“contact”][“surveys”][“surveyInfo”][“ surveyLinkWeb”];

			header(“Location: ”. $urltoredirect);exit();

	}else{
		//The was an error.
	}
}

The above code will use 3 functions inside the Examinare API to create a recipient, make the recipient to redirect to the survey. You will of course need to have the ID for the survey that you previously created inside Examinare. The Survey needs to be deployed as a Private survey to return the correct API return.

 

Step 4: Create a customized thank you page inside the CakePHP (optional).

If you want to create a customized thank you page inside your CakePHP and also parse the data from the survey then this can be done inside the last call. Edit the

header(“Location: ”. $urltoredirect);exit();

And add the following code to redirect

header(“Location: ”. $urltoredirect.”&redirect_url=”.encode_url(“YOUR REDIRECT URL”));exit();

This will redirect the recipient after completion to a thank you page. Also in that URL there is a way of getting the ContactID and Inforreg to get the data. Check out the sample code inside the Zendesk Article here to see how to catch these details. Then you can process exactly how you want it.

Also check out the following API calls for reference:

https://www.examinare.io/apidocs/addrecipient/

https://www.examinare.io/apidocs/markrecipientstosurvey/

https://www.examinare.io/apidocs/listsurveysbyuser/

Also make sure you download the PHP Wrapper Library.

https://www.examinare.io/php-wrapper-library/