Skip to content

Commit

Permalink
Dev: Pre-populate some fields in the widget (CintLink)
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Jul 14, 2016
1 parent d63e22f commit c85ec76
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 46 deletions.
68 changes: 49 additions & 19 deletions application/core/plugins/CintLink/CintLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function actionIndex($surveyId)
);

$data['pluginBaseUrl'] = $pluginBaseUrl;
$data['surveyId'] = $surveyId;

Yii::setPathOfAlias('cintLink', dirname(__FILE__));
$content = Yii::app()->controller->renderPartial('cintLink.views.index', $data, true);
Expand All @@ -106,20 +107,6 @@ public function actionIndex($surveyId)
App()->clientScript->registerScriptFile("$assetsUrl/cintlink.js");
App()->clientScript->registerScriptFile("http://" . $this->cintApiKey . ".cds.cintworks.net/assets/cint-link-1-0-0.js");

/*
$curl = new Curl();
$response = $curl->post(
$this->baseURL,
array(
'app' => 'cintlinklimesurveyrestapi',
'format' => 'raw',
'resource' => 'test',
'key' => 'fbc4607979c0292465185aae3422c93b'
)
);
var_dump($response->body);
*/

return $content;
}

Expand All @@ -131,7 +118,8 @@ public function actionIndex($surveyId)
*/
public function checkIfUserIsLoggedInOnLimesurveyorg(LSHttpRequest $request)
{
if (empty($this->limesurveyOrgKey))
$limesurveyOrgKey = Yii::app()->user->getState('limesurveyOrgKey');
if (empty($limesurveyOrgKey))
{
return json_encode(array('result' => false));
}
Expand All @@ -144,11 +132,12 @@ public function checkIfUserIsLoggedInOnLimesurveyorg(LSHttpRequest $request)
'app' => 'cintlinklimesurveyrestapi',
'format' => 'raw',
'resource' => 'test',
'key' => $this->limesurveyOrgKey
'key' => $limesurveyOrgKey
)
);
$response = json_decode($response);

if ($response->body == "post ok")
if ($response == "post ok")
{
return json_encode(array('result' => true));
}
Expand All @@ -174,7 +163,10 @@ public function getLoginForm(LSHttpRequest $request)
}

/**
* Login to limesurvey.org
* Login to limesurvey.org using com_api
*
* @param LSHttpRequest $request
* @return string JSON
*/
public function login(LSHttpRequest $request)
{
Expand All @@ -200,6 +192,9 @@ public function login(LSHttpRequest $request)
}
else if ($result->code == 200)
{
Yii::app()->user->setState('limesurveyOrgKey', $result->auth);
$this->limesurveyOrgKey = $result->auth;

return json_encode(array('result' => true));
}
else
Expand All @@ -208,6 +203,39 @@ public function login(LSHttpRequest $request)
}
}

/**
* When user click "Place order" in the widget,
* this function is called to contact limesurvey.org.
*
* @param LSHttpRequest $request
* @return string JSON
*/
public function purchaseRequest(LSHttpRequest $request)
{
return json_encode(array('result' => 'hello'));
}

/**
* Get survey information
*/
public function getSurvey(LSHttpRequest $request)
{
$surveyId = $request->getParam('surveyId');
$survey = Survey::model()->findByPk($surveyId);
$data = $survey->getAttributes();
$surveyLanguage = SurveyLanguageSetting::model()->findByPk(array(
'surveyls_survey_id' => $surveyId,
'surveyls_language' => $survey->language
));
$data = array_merge($data, $surveyLanguage->getAttributes());
$user = $this->api->getCurrentUser();
return json_encode(array(
'result' => json_encode($data),
'name' => $user->full_name,
'email' => $user->email
));
}

public function newDirectRequest()
{
$event = $this->event;
Expand All @@ -222,7 +250,9 @@ public function newDirectRequest()
}
else if ($functionToCall == 'checkIfUserIsLoggedInOnLimesurveyorg'
|| $functionToCall == 'getLoginForm'
|| $functionToCall == "login")
|| $functionToCall == "login"
|| $functionToCall == "purchaseRequest"
|| $functionToCall == "getSurvey")
{
echo $this->$functionToCall($request);
}
Expand Down
88 changes: 61 additions & 27 deletions application/core/plugins/CintLink/js/cintlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ $(document).ready(function() {
});
}

/**
* CintLink widget supports a couple of languages.
* Get one of those.
*
* @param {string} surveyLanguage
* @return {string}
*/
function getWidgetLanguage(surveyLanguage)
{
var supportedLanguages = [
];
}

/**
* Show the CintLink widget
*
Expand All @@ -63,36 +76,57 @@ $(document).ready(function() {
function showWidget()
{
console.log("showWidget");
var options = {
locale: "en",
introText: "My Survey Company Name",
surveyLink: {
value: "http://mysurveycompany.example.com/takesurvey/15",
readOnly: true
},
surveyTitle: {
value: "My Survey",
readOnly: true
}
};

CintLink.show(options, function(hold, release) {
// A purchase was made, and we're going to POST the hold URL back to ourselves
console.log("purchase was made");
/*
$.ajax("<?php echo addslashes(htmlentities($_SERVER['PHP_SELF'])); ?>", {
data: {purchaseRequest: hold},
type: "POST",
dataType: "json",
success: function(data) {
$('#order').text(data.text);
orderUrl = data.id;
$('#release-order').show();
CintLink.close();

$.ajax({
method: 'POST',
url: LS.plugin.cintlink.pluginBaseUrl + '&function=getSurvey&surveyId=' + LS.plugin.cintlink.surveyId
}).done(function(response) {
console.log(response);
var response = JSON.parse(response);
console.log('response', response);
var survey = JSON.parse(response.result);
console.log('survey', survey);

var options = {
locale: "en",
introText: "My Survey Company Name",
surveyLink: {
value: "http://mysurveycompany.example.com/takesurvey/15",
readOnly: true
},
surveyTitle: {
value: survey.surveyls_title,
readOnly: true
},
contactName: {
value: response.name,
readOnly: false
},
contactEmail: {
value: response.email,
readOnly: false
}
};

CintLink.show(options, function(hold, release) {
// A purchase was made, and we're going to POST the hold URL back to ourselves
console.log("purchase was made");
$.ajax({
url: LS.plugin.cintlink.pluginBaseUrl + '&function=purchaseRequest',
data: {purchaseRequest: hold},
type: "POST",
dataType: "json",
success: function(data) {
$('#order').text(data.text);
orderUrl = data.id;
$('#release-order').show();
CintLink.close();
}
});
});
*/

});

}

// Check if user is logged in on limesurvey.org
Expand Down
1 change: 1 addition & 0 deletions application/core/plugins/CintLink/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
LS.plugin.cintlink = LS.plugin.cintlink || {};

LS.plugin.cintlink.pluginBaseUrl = '<?php echo $pluginBaseUrl; ?>';
LS.plugin.cintlink.surveyId = '<?php echo $surveyId; ?>';

</script>

0 comments on commit c85ec76

Please sign in to comment.