Commit 069671e8 authored by Chenu Denis's avatar Chenu Denis

[feature] StartUrl->getUrl function

parent 6c0db205
......@@ -75,6 +75,19 @@ Sample usage in beforeSurveyPage for example.
$reloadReponse->startSurvey($step);
````
### StartUrl utilitie : get start url for other plugin
- Allow to get the valid start url for another plugin, if surey is with token : need token except for admin user.
Sample usage
````
$oStartUrl = new \reloadAnyResponse\StartUrl($surveyId, $token);
$startUrl = $oStartUrl->getUrl($srif, array('myparam' => $myValue), true);
if($startUrl) {
echo CHtml::link("reload survey",$startUrl);
}
````
## Contribute and issue
Contribution are welcome, for patch and issue : use [gitlab]( https://gitlab.com/SondagesPro/coreAndTools/reloadAnyResponse).
......
<?php
/**
* Some Utilities
*
* @author Denis Chenu <[email protected]>
* @copyright 2020 Denis Chenu <http://www.sondages.pro>
* @license AGPL v3
* @version 0.0.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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
namespace reloadAnyResponse;
use App;
use Yii;
use CDbCriteria;
use CHttpException;
use Survey;
use Permission;
class StartUrl
{
/* var null|integer $surveyId */
private $surveyId;
/* var null|string $token */
private $token;
/* var boolean is available */
private $available = false;
/* var array settings */
private $currentSettings = array();
/**
* constructor
* @param integer survey id
* @param string token
* @throw Exception
*/
public function __construct($surveyId, $token = null)
{
$oSurvey = \Survey::model()->findByPk($surveyId);
if(empty($oSurvey)) {
throw new Exception(404,gT("The survey in which you are trying to participate does not seem to exist."));
}
$this->surveyId = $surveyId;
$this->token = $token;
}
/**
* Validate if reload is available with current information
* @throw Exception
*/
public function isAvailable()
{
if($this->available) {
return true;
}
if(!$this->surveyId) {
return false;
}
$oSurvey = \Survey::model()->findByPk($this->surveyId);
if($oSurvey->active != "Y") {
return false;
}
if($this->token && $this->getSetting( 'allowTokenUser')) {
$this->available = true;
return $this->available;
}
if($this->getSetting( 'uniqueCodeAccess')) {
$this->available = true;
return $this->available;
}
if(Permission::model()->hasSurveyPermission($this->surveyId, 'responses', 'update') && $this->getSetting( 'allowAdminUser')) {
$this->available = true;
return $this->available;
}
return false;
}
/**
* Get the url for a srid
* @param integer needed srid
* @param string[] $extraParams to be added on url
* @param boolean $createCode create code (if not exist)
* return false|string
*/
public function getUrl($srid, $extraParams = array(), $createCode = false)
{
if(!$this->isAvailable()) {
return false;
}
$params = array(
'sid' => $this->surveyId,
'srid' => $srid,
'lang' => Yii::app()->getLanguage()
);
if($this->token) {
$params['token'] = $this->token;
}
$params = array_merge($params,$extraParams);
/* Get the response information Need only token and srid, response can be big */
$responseCriteria = new CDbCriteria;
$responseCriteria->select = array('id');
$oSurvey = \Survey::model()->findByPk($this->surveyId);
if(!Survey::model()->findByPk($this->surveyId)->getIsAnonymized()) {
$responseCriteria->select = array('id', 'token');
}
$responseCriteria->compare('id',$srid);
$oResponse = \Response::model($this->surveyId)->find($responseCriteria);
if(!$oResponse) {
return false;
}
/* Check if accesscode ? Before token ? : review process ? */
/* Create the specific admin right */
$haveAdminRight = false;
if(Permission::model()->hasSurveyPermission($this->surveyId, 'responses', 'update') && $this->getSetting( 'allowAdminUser')) {
$haveAdminRight = true;
}
if(!$this->token && $haveAdminRight) {
return App()->createUrl("survey/index",$params);
}
/* Check token validaty according to current srid */
if(!$oSurvey->getIsAnonymized() && $oSurvey->getHasTokensTable())
{
/* currently token needed (valid one) */
if(!$this->token) {
return false;
}
$allowTokenUser = $this->getSetting( 'allowTokenUser');
if(!$allowTokenUser && !$haveAdminRight) {
// No rights in plugin settings
return false;
}
if($this->token == $oResponse->token) {
// Same token can return true
return App()->createUrl("survey/index",$params);
}
$allowTokenGroupUser = $this->getSetting( 'allowTokenGroupUser');
if(!$allowTokenGroupUser && !$haveAdminRight) {
// No rights in plugin settings
return false;
}
if(Utilities::checkIsValidToken($this->surveyId, $this->token, $oResponse->token)) {
return App()->createUrl("survey/index",$params);
}
/* What to do for admin with invalid token ? */
return false;
}
$uniqueCodeAccess = $this->getSetting( 'uniqueCodeAccess');
if($uniqueCodeAccess) {
$responseLink = \reloadAnyResponse\models\responseLink::model()->findByPk($this->surveyId, $srid );
if($responseLink && $responseLink->accessCode) {
$params['code'] = $responseLink->accesscode;
return App()->createUrl("survey/index",$params);
}
if($createCode || $this->getSetting( 'uniqueCodeCreate')) {
$responseLink = \reloadAnyResponse\models\responseLink::model()->setResponseLink($this->surveyId, $srid, $this->token);
if($responseLink && $responseLink->accessCode) {
$params['code'] = $responseLink->accesscode;
return App()->createUrl("survey/index",$params);
}
}
return false;
}
return false;
}
public function getSetting($setting)
{
if (isset($this->settings[$setting])) {
return $this->settings[$setting];
}
$this->settings[$setting] = Utilities::getReloadAnyResponseSetting($this->surveyId, $setting);
return $this->settings[$setting];
}
}
......@@ -29,7 +29,10 @@ class Utilities
'allowTokenUser' => 1,
'allowTokenGroupUser' => 1,
'uniqueCodeAccess' => 1,
'uniqueCodeCreate' => 0,
'replaceDefaultSave' => 1,
);
/**
* Create Survey and add current response in $_SESSION
* @param integer $surveydi
......@@ -217,7 +220,8 @@ class Utilities
array(":name" => 'reloadAnyResponse')
);
if(!$oPlugin || !$oPlugin->active) {
return $default;
$this->currentSettings[$surveyId][$sSetting] = null;
return null;
}
$oSetting = \PluginSetting::model()->find(
'plugin_id = :pluginid AND '.App()->getDb()->quoteColumnName('key').' = :key AND model = :model AND model_id = :surveyid',
......@@ -231,6 +235,7 @@ class Utilities
if(!empty($oSetting)) {
$value = json_decode($oSetting->value);
if($value !== '') {
$this->currentSettings[$surveyId][$sSetting] = $value;
return $value;
}
}
......
......@@ -96,6 +96,7 @@ class responseLink extends CActiveRecord
$aRules = array(
array('sid', 'required'),
array('srid', 'required'),
array('accesscode', 'required'),
array('sid,srid', 'numerical', 'integerOnly'=>true),
array('srid', 'unique', 'criteria'=>array(
'condition'=>'sid=:sid',
......
......@@ -222,6 +222,8 @@ class reloadAnyResponse extends PluginBase {
/* Get the survey by srid and code */
/* Save current session */
$this->subscribe('beforeSurveyPage');
/* Some needed action when srid is set */
$this->subscribe('getPluginTwigPath');
/* Replace existing system if srid = new */
$this->subscribe('beforeLoadResponse');
/* Add a checker when multiple tabe is open */
......@@ -229,6 +231,7 @@ class reloadAnyResponse extends PluginBase {
/* Survey settings */
$this->subscribe('beforeSurveySettings');
$this->subscribe('newSurveySettings');
/* delete current session*/
$this->subscribe("afterSurveyComplete",'deleteSurveySession');
$this->subscribe("afterSurveyQuota",'deleteSurveySession');
......@@ -622,6 +625,7 @@ class reloadAnyResponse extends PluginBase {
$oSurvey = Survey::model()->findByPk($surveyid);
$token = App()->getRequest()->getParam('token');
if($srid == "new") {
$this->reloadedSrid = "new";
// Done in beforeLoadResponse, needed only with token related survey
return;
}
......@@ -631,6 +635,16 @@ class reloadAnyResponse extends PluginBase {
}
/**
* @see getPluginTwigPath event
* Do some action when srid is potentially set
*/
public function getPluginTwigPath()
{
if($this->reloadedSrid == "new") {
/* check activation */
}
}
/**
* @see beforeQuestionRender event
* Adding a POST value with current reloaded Srid
......
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