Skip to content

Commit

Permalink
Dev: Check if all Cint orders are completed; show notification is yes
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 17, 2016
1 parent 80bc680 commit 1104645
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
63 changes: 39 additions & 24 deletions application/core/plugins/CintLink/CintLink.php
Expand Up @@ -11,6 +11,10 @@
* CintLink integration to be able to buy respondents
* from within LimeSurvey.
*
* About the Cint active flag: The flag is set to true
* when user clicks "Pay now" on a Cint order. It is
* deactivated when all orders are completed or cancelled.
*
* @since 2016-07-13
* @author Olle Härstedt
*/
Expand Down Expand Up @@ -270,6 +274,7 @@ public function beforeControllerAction() {
$surveyId = Yii::app()->request->getParam('surveyId');
$surveyId = empty($surveyId) ? Yii::app()->request->getParam('surveyid') : $surveyId;
$this->checkCintActive($surveyId);
$this->checkCintCompleted($surveyId);

// Disable all tokens if user has any Cint order
$this->disableTokens($surveyId);
Expand Down Expand Up @@ -305,12 +310,7 @@ protected function checkCintActive($surveyId)
App()->clientScript->registerScriptFile("$assetsUrl/checkOrders.js");

$survey = Survey::model()->findByPk($surveyId);
$surveyIsActive = $survey->active == 'Y'; // TODO: Not enough! Expired etc.
$orders = $this->getOrders(array(
'sid' => $surveyId,
'deleted' => 0
));

$orders = CintLinkOrder::getOrders($surveyId);
if (empty($orders))
{
// Possible?
Expand All @@ -321,6 +321,7 @@ protected function checkCintActive($surveyId)

// Check if any order is paid and/or live
$anyOrderIsActive = CintLinkOrder::anyOrderHasStatus($orders, array('new', 'live', 'hold'));
$surveyIsActive = $survey->active == 'Y'; // TODO: Not enough! Expired etc.
$this->log('anyOrderIsActive = ' . $anyOrderIsActive);
if (!$surveyIsActive && $anyOrderIsActive)
{
Expand All @@ -342,6 +343,33 @@ protected function checkCintActive($surveyId)

}

/**
* Check if all Cint orders are completed. If yes,
* show a notification to user that survey can be
* deactivated.
* @param int $surveyId
* @return void
*/
protected function checkCintCompleted($surveyId)
{
// Check flag
$cintActive = $this->get('cint_active_' . $surveyId);
if ($cintActive &&
CintLinkOrder::allOrdersAreCompleted($surveyId))
{
$not = new Notification(array(
'survey_id' => $surveyId,
'importance' => Notification::NORMAL_IMPORTANCE,
'title' => $this->gT('Cint'),
'message' => $this->gT('All Cint orders are completed.')
));
$not->save();

// Disable flag
$this->set('cint_active_' . $surveyId, false);
}
}

/**
* Returns true if user is on survey activation page OR the controller is notification
* @return bool
Expand Down Expand Up @@ -374,15 +402,10 @@ protected function disableTokens($surveyId)
$isTokenAction = $contr == 'admin' && $action == 'tokens';

// If user has any Cint order, forbid access to participants
if ($isTokenAction)
// Only if survey has blocking Cint orders (hold, live, new/review)
if ($isTokenAction &&
CintLinkOrder::hasAnyBlockingOrders($surveyId))
{

// End if survey has no blocking Cint orders
if (!CintLinkOrder::hasAnyBlockingOrders($surveyId))
{
return;
}

$not = new Notification(array(
'user_id' => Yii::app()->user->id,
'title' => $this->gT('Participants disabled'),
Expand Down Expand Up @@ -421,16 +444,8 @@ public function beforeSurveyDeactivate()
$event = $this->getEvent();
$surveyId = $event->get('surveyId');

$orders = $this->getOrders(array(
'sid' => $surveyId,
'deleted' => 0
));

if (empty($orders))
{
// Do nothing
}
else
$orders = CintLinkOrder::getOrders($surveyId);
if (!empty($orders))
{
$orders = CintLinkOrder::updateOrders($orders);

Expand Down
35 changes: 29 additions & 6 deletions application/core/plugins/CintLink/model/CintLinkOrder.php
Expand Up @@ -440,12 +440,6 @@ public static function updateOrders($orders)
}

$newOrders = array();
$limesurveyOrgKey = Yii::app()->user->getState('limesurveyOrgKey');

if (empty($limesurveyOrgKey))
{
throw new Exception('Not logged in on limesurvey.org');
}

// Loop through orders and get updated info from Cint
foreach ($orders as $order)
Expand Down Expand Up @@ -501,6 +495,35 @@ public static function hasAnyBlockingOrders($surveyId)
return $count > 0;
}

/**
* Returns true if ALL orders are completed OR cancelled.
* Returns false if there are no orders.
*
* All orders are complete if:
* there are any orders
* at least one order is completed.
* no order is blocking (meaning all orders are 'completed' or 'cancelled')
*
* @param int $surveyId
* @return boolean
* @todo Can this be done in one query?
*/
public function allOrdersAreCompleted($surveyId)
{
$orders = self::getOrders($surveyId);

if (self::hasAnyOrders($surveyId) &&
self::anyOrderHasStatus($orders, 'completed') &&
!self::hasAnyBlockingOrders($surveyId))
{
return true;
}
else
{
return false;
}
}

/**
* Returns true if this survey has ANY Cint order related to it, in any state.
* @param int $surveyId
Expand Down

0 comments on commit 1104645

Please sign in to comment.