Commit 5f18cb7a authored by Chenu Denis's avatar Chenu Denis

[fix] Move max step to own DB

- #7
parent 4df983ce
......@@ -415,24 +415,7 @@ class Utilities
*/
public static function setMaxStep($surveyId, $srid) {
if(!empty($_SESSION['survey_'.$surveyId]['maxstep'])) {
\Response::model($surveyId)->updateByPk($srid, array('reload_maxstep' => $_SESSION['survey_'.$surveyId]['maxstep']));
\reloadAnyResponse\models\responseMaxstep::setResponseMaxStep($surveyId, $srid, $_SESSION['survey_'.$surveyId]['maxstep']);
}
}
/**
* Create a column to save max page in DB
* @param integer survey id
* @return boolean if done with success, false if already exist
*/
public static function setMaxStepDb($surveyId) {
$tableName = "{{survey_{$surveyId}}}";
$table = App()->db->schema->getTable($tableName);
if(!isset($table->columns['reload_maxstep'])) {
App()->db->createCommand()->addColumn($tableName, 'reload_maxstep', "integer");
Yii::app()->db->schema->getTable($tableName, true);
return true;
}
return false;
}
}
<?php
/**
* This file is part of reloadAnyResponse plugin
* @version 0.1.0
*/
namespace reloadAnyResponse\models;
use Yii;
use CActiveRecord;
class responseMaxstep extends CActiveRecord
{
/**
* Class reloadAnyResponse\models\responseMaxstep
*
* @property integer $sid survey
* @property integer $srid : response id
* @property string $maxstep : integer
*/
/**
* Set defaults
* @inheritdoc
*/
public function init()
{
$this->maxstep = 0;
}
/** @inheritdoc */
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/** @inheritdoc */
public function tableName()
{
return '{{reloadanyresponse_responseMaxstep}}';
}
/** @inheritdoc */
public function primaryKey()
{
return array('sid', 'srid');
}
/**
* Return (or create) self
* @param integer $sid survey
* @param integer $srid : response id
* @param string $step : optionnal token, if set : always reset
* @return int|null current max step , null if error
*/
public static function setResponseMaxStep($sid, $srid, $step)
{
$oResponseMaxstep = self::model()->findByPk(array('sid'=>$sid,'srid'=>$srid));
if(!$oResponseMaxstep) {
$oResponseMaxstep = new self;
$oResponseMaxstep->sid = $sid;
$oResponseMaxstep->srid = $srid;
}
if($step < $oResponseMaxstep->maxstep) {
return $oResponseMaxstep->maxstep;
}
$oResponseMaxstep->maxstep = $step;
if(!$oResponseMaxstep->save()) {
return null;
}
return $oResponseMaxstep->maxstep;
}
/**
* return the current max step
* @param integer $sid survey
* @param integer $srid : response id
* @return int|null current max step , null if error
*/
public function getMaxstep($sid, $srid)
{
$oResponseMaxstep = self::model()->findByPk(array('sid'=>$sid,'srid'=>$srid));
if(!$oResponseMaxstep) {
return null;
}
return $oResponseMaxstep->maxstep;
}
/**
* @inheritdoc
*/
public function rules()
{
$aRules = array(
array('sid', 'required'),
array('srid', 'required'),
array('maxstep', 'numerical', 'integerOnly'=>true),
array('sid,srid', 'numerical', 'integerOnly'=>true),
array('srid', 'unique', 'criteria'=>array(
'condition'=>'sid=:sid',
'params'=>array(':sid'=>$this->sid)
),
'message'=>sprintf("Srid : '%s' already set for '%s'.",$this->srid,$this->sid),
),
);
return $aRules;
}
}
......@@ -5,7 +5,7 @@
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2018-2020 Denis Chenu <http://www.sondages.pro>
* @license AGPL v3
* @version 3.3.3-beta1
* @version 3.4.0-beta2
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -24,7 +24,7 @@ class reloadAnyResponse extends PluginBase {
static protected $description = 'New class and function allowing to reload any survey.';
static protected $name = 'reloadAnyResponse';
static protected $dbVersion = 3;
static protected $dbVersion = 4;
/* @var null|interger Keep reload srid during all events */
private $reloadedSrid = null;
......@@ -265,8 +265,6 @@ class reloadAnyResponse extends PluginBase {
$this->subscribe('beforeSurveyDeleteMany');
/* Get the survey by srid and code */
/* Upoxdate DB just after activation */
$this->subscribe('afterSurveyActivate');
/* Save current session */
$this->subscribe('beforeSurveyPage');
/* Some needed action when srid is set */
......@@ -320,18 +318,6 @@ class reloadAnyResponse extends PluginBase {
throw new CHttpException(403);
}
parent::saveSettings($settings);
if (!empty($settings['keepMaxStep']) && intval($settings['keepMaxStep'])) {
$criteria = new CDBcriteria;
$criteria->select = array("sid", "active");
$criteria->compare("active","Y");
$oSurveysIds = Survey::model()->findAll($criteria);
if($oSurveysIds) {
$aSurveysIds = CHtml::listData($oSurveysIds, 'sid', 'sid');
foreach($aSurveysIds as $surveyId) {
\reloadAnyResponse\Utilities::setMaxStepDb($surveyId);
}
}
}
}
/** @inheritdoc **/
......@@ -388,7 +374,7 @@ class reloadAnyResponse extends PluginBase {
} else {
$allowTokenUserHelp .= "<li class='text-success'>".$this->_translate("User can create a new response in any conditions.")."</li>";
}
$allowTokenUserHelp .= "</ul>";
$oEvent->set("surveysettings.{$this->id}", array(
'name' => get_class($this),
'settings' => array(
......@@ -406,7 +392,7 @@ class reloadAnyResponse extends PluginBase {
),
'allowTokenUser'=>array(
'type' => 'select',
'label' => $this->_translate("Allow participant with token to reload responses."),
'label' => $this->_translate("Allow participant with token to reload or create responses."),
'help' => $allowTokenUserHelp,
'options'=>array(
1 => gT("Yes"),
......@@ -636,23 +622,6 @@ class reloadAnyResponse extends PluginBase {
foreach ($event->get('settings') as $name => $value) {
$this->set($name, $value, 'Survey', $event->get('survey'));
}
if(\reloadAnyResponse\Utilities::getSetting($event->get('survey'),'keepMaxStep')) {
\reloadAnyResponse\Utilities::setMaxStepDb($event->get('survey'));
}
}
/** @inheritdoc **/
public function afterSurveyActivate()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
if ($this->event->get("simulate")) {
return;
}
if(\reloadAnyResponse\Utilities::getSetting($this->event->get('surveyId'),'keepMaxStep')) {
\reloadAnyResponse\Utilities::setMaxStepDb($this->event->get('surveyId'));
}
}
/** @inheritdoc
......@@ -780,7 +749,7 @@ class reloadAnyResponse extends PluginBase {
$oResponses = $this->getEvent()->get('responses');
$oResponse = $this->getEvent()->get('response');/* Get the current response set */
$token = App()->getRequest()->getParam('token');
if($srid == 'new' && $token && $this->_getIsActivated('allowTokenUser',$surveyId) ) {
if($oSurvey->tokenanswerspersistence != "Y" || $oSurvey->showwelcome != 'Y') {
/* Check if create or not … */
......@@ -788,6 +757,8 @@ class reloadAnyResponse extends PluginBase {
return;
}
}
/* if srid is set : all is already done */
/* need to check maxstep */
if (!$this->_getCurrentSetting('multiAccessTime',$surveyId)) {
return;
}
......@@ -801,13 +772,14 @@ class reloadAnyResponse extends PluginBase {
$oResponse = $oResponses[0];
}
}
if($oResponse) {
if ($since = \reloadAnyResponse\models\surveySession::getIsUsed($surveyId,$oResponse->id, !$this->noactionByUserAgent())) {
$this->_endWithEditionMessage($since);
}
if(!$this->noactionByUserAgent()) {
\reloadAnyResponse\models\surveySession::saveSessionTime($surveyId,$oResponse->id);
}
if(empty($oResponse)) {
return;
}
if ($since = \reloadAnyResponse\models\surveySession::getIsUsed($surveyId,$oResponse->id, !$this->noactionByUserAgent())) {
$this->_endWithEditionMessage($since);
}
if(!$this->noactionByUserAgent()) {
\reloadAnyResponse\models\surveySession::saveSessionTime($surveyId,$oResponse->id);
}
}
......@@ -1181,6 +1153,15 @@ class reloadAnyResponse extends PluginBase {
Yii::app()->getDb()->createCommand()->addPrimaryKey('responselink_sidsrid',$tableName,'sid,srid');
}
}
if($this->get("dbVersion") < 4 ) {
$this->api->createTable($this, 'responseMaxstep', array(
'sid' => 'int not NULL',
'srid' => 'int not NULL',
'maxstep' => 'int not NULL',
));
$tableName = $this->api->getTable($this,'responseMaxstep')->tableName();
App()->getDb()->createCommand()->addPrimaryKey('responsemaxstep_sidsrid',$tableName,'sid,srid');
}
/* all done */
$this->set("dbVersion",self::$dbVersion);
}
......
......@@ -2,7 +2,7 @@
/**
* This file is part of reloadAnyResponse plugin
* Helpers for other plugin : reload or create responses
* @version 0.1.0
* @version 0.2.0
*/
namespace reloadAnyResponse;
use Yii;
......@@ -41,9 +41,6 @@ class reloadResponse
$language = $oSurvey->language;
}
$this->language = $language;
if(Utilities::getSetting($this->surveyId,'keepMaxStep')) {
Utilities::setMaxStepDb($this->surveyId);
}
}
/**
* Start and load a Survey
......@@ -64,9 +61,15 @@ class reloadResponse
killSurveySession($surveyId);
$_SESSION['survey_'.$surveyId]['s_lang'] = $this->language;
$currentStep = $maxStep = $oResponse->lastpage;
if ($keepMaxStep && $oResponse->reload_maxstep > $currentStep) {
$maxStep = $oResponse->reload_maxstep;
if($keepMaxStep) {
$reloadmaxStep = \reloadAnyResponse\models\responseMaxstep::model()->findByPk(
array('sid'=>$surveyId, 'srid'=>$this->srid)
);
if ($reloadmaxStep > $currentStep) {
$maxStep = $reloadmaxStep;
}
}
$submitdate = $oResponse->submitdate;
LimeExpressionManager::SetSurveyId($surveyId);
buildsurveysession($surveyId);
......@@ -155,7 +158,7 @@ class reloadResponse
LimeExpressionManager::JumpTo($currentStep, false, false, true);
}
if(Utilities::getSetting($this->surveyId,'keepMaxStep')) {
\Response::model($this->surveyId)->updateByPk($this->srid, array('reload_maxstep' => $maxStep));
\reloadAnyResponse\models\responseMaxstep::setResponseMaxStep($this->surveyId, $this->srid, $maxStep);
}
if (Survey::model()->findByPk($surveyId)->getIsDateStamp() && $datestamp) {
\Response::model($this->surveyId)->updateByPk($this->srid, array('datestamp' => $datestamp));
......
Markdown is supported
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