Skip to content

Commit

Permalink
Dev: Calculate number of survey questions for Cint
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 18, 2016
1 parent b377dfc commit 74aff83
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
43 changes: 33 additions & 10 deletions application/core/plugins/CintLink/CintLink.php
Expand Up @@ -995,7 +995,7 @@ public function getSurvey(LSHttpRequest $request)
'result' => json_encode($data),
'name' => $user->full_name,
'email' => $user->email,
'nrOfQuestions' => $this->getNrOfQuestions($survey),
'numberOfQuestions' => $this->calculateNumberOfQuestions($surveyId),
'link' => $link,
'language' => $survey->language
));
Expand Down Expand Up @@ -1222,6 +1222,38 @@ protected function hideNaggingNotication($surveyId)
*/
}

/**
* Cint need to know how many questions there are in the
* survey. I use base questions + (sub questions / 3).
* Approx is 3 questions each minute.
* @param int $surveyId
* @return int
*/
protected function calculateNumberOfQuestions($surveyId)
{
$survey = Survey::model()->findByPk($surveyId);

$criteria = new CDbCriteria();
$criteria->addCondition('sid = ' . $surveyId);
$criteria->addCondition('parent_qid = 0');
$criteria->addCondition('language = \'' . $survey->language . '\'');
$baseQuestions = Question::model()->count($criteria);

// Note: An array questions with one sub question is fetched as 1 base question + 1 sub question
$criteria = new CDbCriteria();
$criteria->addCondition('sid = ' . $surveyId);
$criteria->addCondition('parent_qid != 0');
$criteria->addCondition('language = \'' . $survey->language . '\'');
$subQuestions = Question::model()->count($criteria);

// Subquestions are worth less "time" than base questions
$subQuestions = intval(($subQuestions - $baseQuestions) / 2);
$subQuestions = $subQuestions < 0 ? 0 : $subQuestions;

return $subQuestions + $baseQuestions;

}

/**
* Echoes Javascript code that is common for all scripts
* Only runs if it's NOT an Ajax call
Expand Down Expand Up @@ -1273,15 +1305,6 @@ protected function renderCommonJs($surveyId = null)
}
}

/**
* Calculate how many questions the survey contains.
* Used by Cint widget.
* @param Survey $survey
*/
protected function getNrOfQuestions(Survey $survey)
{
}

/**
* Return true if this survey has a token table
* @param int $surveyId
Expand Down
5 changes: 5 additions & 0 deletions application/core/plugins/CintLink/js/cintlink.js
Expand Up @@ -146,6 +146,7 @@ $(document).ready(function() {
completeLink = response.link + '&lang=' + lang;
}
console.log('completeLink', completeLink);
console.log('response.numberOfQuestions', response.numberOfQuestions);

var options = {
locale: getWidgetLanguage(survey.surveyls_language),
Expand All @@ -165,6 +166,10 @@ $(document).ready(function() {
contactEmail: {
value: response.email,
readOnly: false
},
numberOfQuestions: {
value: response.numberOfQuestions,
readOnly: true
}
};

Expand Down

0 comments on commit 74aff83

Please sign in to comment.