Commit 7e65d083 authored by Chenu Denis's avatar Chenu Denis
Browse files

[feature] 5.X version

parent 5f6531b0
......@@ -8,12 +8,13 @@
<author>Denis Chenu</author>
<authorUrl>https://extensions.sondages.pro/</authorUrl>
<supportUrl>https://support.sondages.pro/</supportUrl>
<version>1.15.0</version>
<version>1.16.0-beta1</version>
<license>GNU Affero General Public License v3.0</license>
<description><![CDATA[Use a question to add survey inside another survey visually like a spreadsheet.]]></description>
</metadata>
<compatibility>
<version>3</version>
<version>5</version>
</compatibility>
<lastSecurityUpdate>1.14.6</lastSecurityUpdate>
</config>
......@@ -5,7 +5,7 @@
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2020-2021 Denis Chenu <http://www.sondages.pro>
* @license AGPL v3
* @version 1.2.2
* @version 1.16.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
......@@ -17,7 +17,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
namespace questionSpreadsheetSurvey\helpers;
namespace questionSpreadsheetSurveyData\helpers;
use Yii;
use CHttpException;
......@@ -28,6 +28,7 @@ use Survey;
use CDbCriteria;
/* Question information */
use Question;
use QuestionL10n;
use QuestionAttribute;
use Answer;
/* Get tha data */
......@@ -142,7 +143,6 @@ class dataHelper
public function setForcedDatas($questionCode, $aValuesToSet, $bDeleteOtherValues = false, $bSubmitted = false) {
$criteria = new CDbCriteria;
$criteria->compare("sid", $this->surveyId);
$criteria->compare("language", $this->language);
$criteria->compare("parent_qid", 0);
$criteria->compare("title", $questionCode);
$criteria->addInCondition("type", self::QTYPE);
......@@ -245,13 +245,12 @@ class dataHelper
public function getColumns($idAsKey = false)
{
$criteria = new CDbCriteria;
$criteria->with = array('groups');
$criteria->order = 'groups.group_order asc, t.question_order asc';
$criteria->with = array('group');
$criteria->order = 'group.group_order asc, t.question_order asc';
$criteria->compare("t.sid", $this->surveyId);
$criteria->compare("t.language", $this->language);
$criteria->compare("t.parent_qid", 0);
$criteria->addInCondition("type", self::QTYPE);
$oValidQuestions = \Question::model()->with('groups')->findAll($criteria);
$oValidQuestions = \Question::model()->findAll($criteria);
$aSurveyInfo = getSurveyInfo($this->surveyId, $this->language); /* A lot of things here ? */
$aColumns = array();
if ($idAsKey) {
......@@ -285,9 +284,21 @@ class dataHelper
);
}
foreach ($oValidQuestions as $oValidQuestion) {
$aValidQuestionAttributes = $oValidQuestion->getAttributes();
/* Language attributes */
$aValidQuestionAttributes['question'] = "";
$aValidQuestionAttributes['help'] = "";
$oQuestionL10N = QuestionL10n::model()->find(
'qid = :qid and language = :language',
array(":qid" => $oValidQuestion->qid, ":language" => $this->language)
);
if ($oQuestionL10N) {
$aValidQuestionAttributes['question'] = $oQuestionL10N->question;
$aValidQuestionAttributes['help'] = $oQuestionL10N->help;
}
$renderData = array(
'aSurveyInfo' => $aSurveyInfo, /* A lot of things here ? */
'aQuestion' => $oValidQuestion->getAttributes(),
'aQuestion' => $aValidQuestionAttributes,
'qid' => $oValidQuestion->qid,
'sgq' => $oValidQuestion->sid.'X'.$oValidQuestion->gid.'X'.$oValidQuestion->qid,
);
......@@ -320,11 +331,15 @@ class dataHelper
switch( $oValidQuestion->type ) {
case 'N' :
$aColumnInfo['type'] = 'numeric';
$aAttribute = \QuestionAttribute::model()->getQuestionAttributes($oValidQuestion->qid);
$aAttributeIntOnly = QuestionAttribute::model()->getQuestionAttributes($oValidQuestion->qid);
$integeronlyAttribute = QuestionAttribute::model()->find(
"qid = :qid and attribute = :attribute",
array(":qid" => $oValidQuestion->qid, ":attribute" => 'num_value_int_only')
);
//$aColumnInfo['editor'] = 'questionSpreadsheetSurvey.numberCell';
//$aColumnInfo['editor'] = 'questionSpreadsheetSurvey.numeric';
$aColumnInfo['mask'] = '';
if($aAttribute['num_value_int_only']) {
if (!empty($integeronlyAttribute) && $integeronlyAttribute->value) {
$aColumnInfo['mask'] = '#';
}
$aColumnInfo['decimal'] = '.';
......@@ -333,16 +348,20 @@ class dataHelper
case 'L' :
case '!' :
$aColumnInfo['type'] = 'dropdown';
$oAnswers = \Answer::model()->findAll(array(
'condition' => "qid = :qid and language = :language",
$oAnswers = \Answer::model()->with('answerl10ns')->findAll(array(
'condition' => "qid = :qid",
'order' => 'sortorder ASC',
'params' => array(":qid" => $oValidQuestion->qid , ":language" => $this->language)
'params' => array(":qid" => $oValidQuestion->qid)
));
$options = array();
$options = array();
foreach ($oAnswers as $oAnswer ) {
$name = "";
if(!empty($oAnswer->answerl10ns[$this->language])) {
$name = $oAnswer->answerl10ns[$this->language]->answer;
}
$options[] = array(
'id' => $oAnswer->code,
'name' => $oAnswer->answer,
'name' => $name,
);
}
$aColumnInfo['source'] = $options;
......@@ -353,7 +372,6 @@ class dataHelper
$aColumnInfo['readOnly'] = true;
break;
case 'S' :
$aAttribute = \QuestionAttribute::model()->getQuestionAttributes($oValidQuestion->qid);
$aColumnInfo['type'] = 'text';
$aColumnInfo['wordWrap'] = false;
$aColumnInfo['readOnly'] = $readonly;
......@@ -455,7 +473,7 @@ class dataHelper
}
/**
* get data
* Get the result after updating data to fille answer part
* @return string
*/
public function getResult($type = 'number')
......@@ -567,7 +585,7 @@ class dataHelper
$oCriteria->compare(Yii::app()->db->quoteColumnName('id'), $srid);
$oResponse = Response::model($this->surveyId)->find($oCriteria);
if(empty($oResponse)) {
/* @todo : log it as error (and understand hpow it happen) */
/* @todo : log it as error (and understand how it happen) */
//throw new \CHttpException(400,sprintf(gT("Invalid response id %s."), $srid));
$srid = null;
$newSrid = $srid;
......@@ -666,10 +684,10 @@ class dataHelper
$oResponse->setAttribute('token', $this->token);
}
$oResponse->save();
$this->_startSurvey($oResponse);
$this->ownStartSurvey($oResponse);
/**/
$aReturn['status'] = $this->_loadAndSubmitSurvey($oResponse);
$aReturn['status'] = $this->ownLoadAndSubmitSurvey($oResponse);
$oResponse = Response::model($this->surveyId)->findByPk($srid);
$aReturn['response'] = $this->getResponseData($oResponse);
$aReturn['responseAttributes'] = $oResponse->getAttributes();
......@@ -679,9 +697,10 @@ class dataHelper
/**
* Wipe all session variables of current survey
* @deprecated
* @return void
*/
private function _resetSession()
private function ownResetSession()
{
/* $_SESSION['survey_'.$surveyid]['s_lang'] or App()->getLanguage() ? */
killSurveySession($this->surveyId);
......@@ -698,7 +717,7 @@ class dataHelper
* @param \Response::model $oResponses
* @return void
*/
private function _startSurvey($oResponses)
private function ownStartSurvey($oResponses)
{
killSurveySession($this->surveyId);
/* What to do when response already exist ? */
......@@ -708,31 +727,58 @@ class dataHelper
's_lang' => $this->language,
'srid' => $oResponses->id,
);
buildsurveysession($this->surveyId);
/* Replace buildsurveysession due toi usage of App()->getController()->sTemplate , and try to ligthen */
/* buildsurveysession in frontend_helper */
$Survey = Survey::model()->findByPk($this->surveyId);
/* resetAllSessionVariables in frontend_helper */
/* Start to fill the session buildsurveysession at 2022-09-21 version */
UpdateGroupList($this->surveyId, $this->language);
$_SESSION['survey_' . $this->surveyId]['totalquestions'] = $Survey->countInputQuestions;
/* Forced survey to be in group by group */
$_SESSION['survey_' . $this->surveyId]['totalsteps'] = count($_SESSION['survey_' . $this->surveyId]['grouplist']);
/* The token ? */
if ($this->token) {
$_SESSION['survey_' . $this->surveyId]['token'] = $this->token;
}
if ($Survey->anonymized == "N") {
$_SESSION['survey_' . $this->surveyId]['insertarray'][] = "token";
}
$fieldmap = $_SESSION['survey_' . $this->surveyId]['fieldmap'] = createFieldMap($Survey, 'full', true, false, $_SESSION['survey_' . $this->surveyId]['s_lang']);
initFieldArray($this->surveyId, $fieldmap);
prefillFromCommandLine($this->surveyId);
if (isset($_SESSION['survey_' . $this->surveyId]['fieldarray'])) {
$_SESSION['survey_' . $this->surveyId]['fieldarray'] = array_values($_SESSION['survey_' . $this->surveyId]['fieldarray']);
}
/* End to fill the session buildsurveysession at 2022-09-21 version */
}
/**
* Start survey session like LS core
* @param \Response::model $oResponses
* @return array
*/
private function _loadAndSubmitSurvey($oResponses)
private function ownLoadAndSubmitSurvey($oResponses)
{
$this->_loadanswers($oResponses);
$this->ownLoadAnswers($oResponses);
$_SESSION['survey_'.$this->surveyId]['step'] = 0;
/* Start the survey as group survey */
\LimeExpressionManager::StartSurvey($this->surveyId,'group',$this->_getSurveyOption(),false);
\LimeExpressionManager::StartSurvey($this->surveyId,'group',$this->ownGetSurveyOption(),false);
\LimeExpressionManager::JumpTo(0,false,false,true); // no preview, no post and force
/* Go to count of all groups +1 */
$numberOfGroups = \QuestionGroup::model()->count("sid = :sid AND language = :language",array(":sid"=>$this->surveyId,":language"=>$this->language));
if($numberOfGroups > 1) {
$aMoveResult = \LimeExpressionManager::JumpTo($numberOfGroups+1, false, false, true); // Force if equation in last group
\LimeExpressionManager::JumpTo(0,false,false,true); // no preview, no post and force
$numberOfGroups = 1;
if(!empty($_SESSION['survey_' . $this->surveyId]['totalsteps'])) {
$numberOfGroups = $_SESSION['survey_' . $this->surveyId]['totalsteps'];
}
$aMoveResult = \LimeExpressionManager::JumpTo($numberOfGroups+1, false, false, true); // Force if equation in last group before redo the survey again
\LimeExpressionManager::JumpTo(0,false,false,true); // no preview, no post and force
Response::model($this->surveyId)->updateByPk($oResponses->id, array('submitdate' => null) );
$aMoveResult = \LimeExpressionManager::JumpTo($numberOfGroups+1, false, false, false);
return $aMoveResult;
}
private function _getSurveyOption()
private function ownGetSurveyOption()
{
$oSurvey = Survey::model()->findByPk($this->surveyId);
return array(
......@@ -761,7 +807,7 @@ class dataHelper
* @param \Response::model()
* @return void
*/
private function _loadanswers($oResponses)
private function ownLoadAnswers($oResponses)
{
$surveyId = $this->surveyId;
$insertarray = $_SESSION['survey_'.$surveyId]['insertarray'];
......@@ -827,8 +873,8 @@ class dataHelper
}
/* Usage of getQuextionCode OR restrict to single element */
$oQuestion = Question::model()->find(
"sid = :sid and language = :language and title = :title",
array(":sid" => $this->surveyId, ":language" => $this->language, ":title" => $title)
"sid = :sid and title = :title",
array(":sid" => $this->surveyId, ":title" => $title)
);
if(!$oQuestion) {
return null;
......@@ -846,7 +892,7 @@ class dataHelper
return;
}
// This needs to be done directly on the PDO object because when using CdbCommand or similar it won't have any effect
Yii::app()->db->pdoInstance->exec('SET IDENTITY_INSERT '.Yii::app()->db->tablePrefix.'survey_'.$surveyId.' ON');
App()->db->pdoInstance->exec('SET IDENTITY_INSERT '.App()->db->tablePrefix.'survey_'.$surveyId.' ON');
}
/**
* Set identity insert OFF for mssql
......@@ -857,6 +903,6 @@ class dataHelper
if (!in_array(Yii::app()->db->getDriverName(), array('mssql', 'sqlsrv', 'dblib'))) {
return;
}
Yii::app()->db->pdoInstance->exec('SET IDENTITY_INSERT '.Yii::app()->db->tablePrefix.'survey_'.$surveyId.' OFF');
App()->db->pdoInstance->exec('SET IDENTITY_INSERT '.App()->db->tablePrefix.'survey_'.$surveyId.' OFF');
}
}
This diff is collapsed.
......@@ -6,7 +6,7 @@
* @copyright 2020-2022 Denis Chenu <www.sondages.pro>
* @copyright 2020-2021 OECD (Organisation for Economic Co-operation and Development ) <www.oecd.org>
* @license AGPL v3
* @version 1.15.0
* @version 1.16.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
......@@ -44,7 +44,10 @@ class questionSpreadsheetSurvey extends PluginBase
public function init()
{
Yii::setPathOfAlias('questionSpreadsheetSurvey', dirname(__FILE__));
Yii::setPathOfAlias('questionSpreadsheetSurveyData', dirname(__FILE__));
if (intval(App()->getConfig('versionnumber')) <= 3) {
Yii::setPathOfAlias('questionSpreadsheetSurveyData', dirname(__FILE__) . 'legacy3LTS');
}
$this->subscribe('beforeSurveyPage');
$this->subscribe('beforeQuestionRender');
......@@ -138,6 +141,7 @@ class questionSpreadsheetSurvey extends PluginBase
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$questionAttributeEvent = $this->getEvent();
$extraAttributes = array(
'spreadsheetSurvey'=>array(
'types'=>'XT',
......@@ -298,12 +302,12 @@ class questionSpreadsheetSurvey extends PluginBase
//~ 'caption'=>$this->translate('Disable direct access to survey.'),
//~ ),
);
if (method_exists($this->getEvent(), 'append')) {
$this->getEvent()->append('questionAttributes', $extraAttributes);
if (method_exists($questionAttributeEvent, 'append')) {
$questionAttributeEvent->append('questionAttributes', $extraAttributes);
} else {
$questionAttributes=(array)$this->event->get('questionAttributes');
$questionAttributes=(array) $questionAttributeEvent->get('questionAttributes');
$questionAttributes=array_merge($questionAttributes, $extraAttributes);
$this->event->set('questionAttributes', $questionAttributes);
$questionAttributeEvent->set('questionAttributes', $questionAttributes);
}
}
......@@ -391,9 +395,9 @@ class questionSpreadsheetSurvey extends PluginBase
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$oEvent=$this->getEvent();
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($oEvent->get('qid'), Yii::app()->getLanguage());
$surveyId=$oEvent->get('surveyId');
$renderEvent = $this->getEvent();
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($renderEvent->get('qid'), Yii::app()->getLanguage());
$surveyId=$renderEvent->get('surveyId');
if (empty($aQuestionAttributes['spreadsheetSurvey']) || trim($aQuestionAttributes['spreadsheetSurvey']) == '') {
return;
}
......@@ -422,32 +426,32 @@ class questionSpreadsheetSurvey extends PluginBase
$extraSurvey=Survey::model()->findByPk($extraSurveyAttribute);
$disableMessage = "";
if (!$extraSurvey) {
$disableMessage = sprintf($this->translate("Invalid survey %s for question %s."), $extraSurveyAttribute, $oEvent->get('qid'));
$disableMessage = sprintf($this->translate("Invalid survey %s for question %s."), $extraSurveyAttribute, $renderEvent->get('qid'));
}
if (!$disableMessage && $extraSurvey->active != "Y") {
$disableMessage = sprintf($this->translate("Survey %s for question %s not activated."), $extraSurveyAttribute, $oEvent->get('qid'));
$disableMessage = sprintf($this->translate("Survey %s for question %s not activated."), $extraSurveyAttribute, $renderEvent->get('qid'));
}
if (!$disableMessage && !$this->accessWithToken($thisSurvey) && $this->accessWithToken($extraSurvey)) {
$disableMessage = sprintf($this->translate("Survey %s for question %s can not be used with a survey without tokens."), $extraSurveyAttribute, $oEvent->get('qid'));
$disableMessage = sprintf($this->translate("Survey %s for question %s can not be used with a survey without tokens."), $extraSurveyAttribute, $renderEvent->get('qid'));
}
if (!$disableMessage && $this->accessWithToken($thisSurvey) && $extraSurvey->anonymized == "Y") {
$disableMessage = sprintf($this->translate("Survey %s for question %s need to be not anonymized."), $extraSurveyAttribute, $oEvent->get('qid'));
$disableMessage = sprintf($this->translate("Survey %s for question %s need to be not anonymized."), $extraSurveyAttribute, $renderEvent->get('qid'));
}
if (!$disableMessage && $this->accessWithToken($extraSurvey)) {
if (!$this->validateToken($extraSurvey, $thisSurvey, $token)) {
$disableMessage = sprintf($this->translate("Survey %s for question %s token can not ne found or created."), $extraSurveyAttribute, $oEvent->get('qid'));
$disableMessage = sprintf($this->translate("Survey %s for question %s token can not ne found or created."), $extraSurveyAttribute, $renderEvent->get('qid'));
}
}
if ($disableMessage) {
$oEvent->set("answers", CHtml::tag("div", array('class'=>'alert alert-warning'), $disableMessage));
$renderEvent->set("answers", CHtml::tag("div", array('class'=>'alert alert-warning'), $disableMessage));
return;
}
if ($this->accessWithToken($extraSurvey)) {
$this->validateToken($extraSurvey, $thisSurvey, $token);
}
$this->setSurveySpreadsheetForAnswer($extraSurvey->sid, $aQuestionAttributes, $token);
$this->setSurveySpreadsheetForAnswer($renderEvent, $extraSurvey->sid, $aQuestionAttributes, $token);
}
/**
......@@ -457,26 +461,24 @@ class questionSpreadsheetSurvey extends PluginBase
* @param string $token
* @return void
*/
private function setSurveySpreadsheetForAnswer($surveyId, $aQuestionAttributes, $token = null)
private function setSurveySpreadsheetForAnswer($renderEvent, $surveyId, $aQuestionAttributes, $token = null)
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$oEvent=$this->getEvent();
$currentSurveyId = $oEvent->get('surveyId');
$qid = $oEvent->get('qid');
$currentSurveyId = $renderEvent->get('surveyId');
$qid = $renderEvent->get('qid');
$srid = isset($_SESSION["survey_{$currentSurveyId}"]['srid']) ? $_SESSION["survey_{$currentSurveyId}"]['srid'] : null;
/* Unset session : force reeavaluation */
$this->unsetSession($qid, $srid);
$oEvent->set("class", $oEvent->get("class")." questionSpreadsheetSurvey");
$renderEvent->set("class", $renderEvent->get("class") . " questionSpreadsheetSurvey");
$aWarnings = array();
/** Get the questions */
Yii::setPathOfAlias('questionSpreadsheetSurvey', dirname(__FILE__));
/** script */
/* Construction of options **/
$jexcelOptions = array();
$this->subscribe('getPluginTwigPath', 'addTwigPath');
$dataHelper = new \questionSpreadsheetSurvey\helpers\dataHelper($surveyId);
$dataHelper = new \questionSpreadsheetSurveyData\helpers\dataHelper($surveyId);
$spreadsheetSurveyQuestionLink = trim($this->getQuestionAttribute($qid, 'spreadsheetSurveyQuestionLink'));
if($spreadsheetSurveyQuestionLink) {
if($dataHelper->setColumnsAsReadonly($spreadsheetSurveyQuestionLink)) {
......@@ -587,7 +589,7 @@ class questionSpreadsheetSurvey extends PluginBase
);
$renderData['spreadsheetOptions'] = $spreadsheetOptions;
$renderData['warnings'] = Permission::model()->hasSurveyPermission($currentSurveyId,'surveycontent','update') ? $aWarnings : array();
$renderData['inputName'] = $inputName = $currentSurveyId."X".$oEvent->get('gid')."X".$oEvent->get('qid');
$renderData['inputName'] = $inputName = $currentSurveyId."X".$renderEvent->get('gid')."X".$renderEvent->get('qid');
$renderData['relatedSurvey'] = $surveyId;
$renderData['token'] = $token;
$renderData['relationalData'] = array(
......@@ -621,7 +623,7 @@ class questionSpreadsheetSurvey extends PluginBase
"$('#answer{$inputName}').trigger('keyup');\nquestionSpreadsheetSurvey.setSpreadsheet('jexcel{$inputName}',{$jsonSpreadSheetOptions});\n",
CClientScript::POS_END
);
$oEvent->set("answers", $answer);
$renderEvent->set("answers", $answer);
$this->unsubscribe('getPluginTwigPath');
}
......@@ -873,7 +875,7 @@ class questionSpreadsheetSurvey extends PluginBase
* Return the data helper with restriction
* This control security via qid (question id) and with session of survey
* @param integer $qid
* @return questionSpreadsheetSurvey\helpers\dataHelper
* @return questionSpreadsheetSurveyData\helpers\dataHelper
* @throw exception
*/
private function getDataHelper($qid, $srid, $token, $debug = false)
......@@ -885,7 +887,7 @@ class questionSpreadsheetSurvey extends PluginBase
$token = null;
}
$this->subscribe('getPluginTwigPath', 'addTwigPath');
$dataHelper = new \questionSpreadsheetSurvey\helpers\dataHelper($extraSurvey);
$dataHelper = new \questionSpreadsheetSurveyData\helpers\dataHelper($extraSurvey);
$sTokenUsage = trim($this->getQuestionAttribute($qid, 'spreadsheetSurveyTokenUsage'));
if($token) {
if($sTokenUsage != "no") {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment