Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Why are my Plugin Settings NULL?

  • mhladun
  • mhladun's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 5 hours ago - 2 years 5 hours ago #227697 by mhladun
Why are my Plugin Settings NULL? was created by mhladun
Your LimeSurvey version: 5.3.10
Own server or LimeSurvey hosting: Self hosting
Survey theme/template: Custom


Hi there,

I am developing a Limesurvey plugin. I am trying to access a boolean survey setting from a method inside my plugin class that extends PluginBase.

Below I am including some code. Where I comment `// nothing is printed`, I expect these boolean values to be either 0 or 1, but always receive NULL, even though my default values are set to 0.

<?php
class SurveyApproval extends PluginBase {
  protected $storage = 'DbStorage';
  static protected $description = 'This plugin prevents surveys from being published without approval.';
  static protected $name = 'SurveyApproval';
  protected $surveyId;
 
  // global level settings for plugin
  protected $settings = array();
  public function init()
  {
    $this->subscribe('beforeSurveyActivate');
    $this->subscribe('beforeSurveySettings'); // add new survey settings
    $this->subscribe('newSurveySettings'); // save new survey settings
  }
  // survey level settings for plugin
  public function beforeSurveySettings()
  {
    $this->log('beforeSurveySettings');
    $oEvent = $this->event;
    $oEvent->set("surveysettings.{$this->id}", array(
      'name' => get_class($this),
      'settings' => array(
        'bSurveyIsApproved' => array(
          'type' => 'boolean',
          'label' => 'Survey approval received?',
          'default' => 0,
          'help' => 'Has this survey\'s questions, translations, accessibility, etc. been approved for publishing?',
          'current' => $this->get('bSurveyIsApproved', 'Survey', $oEvent->get('survey'))
        ),
        'bDebugMode' => array(
          'type' => 'boolean',
          'default' => 0,
          'label' => 'Debug Mode',
          'help' => 'This should be `Off` for live surveys.',
          'current' => $this->get('bDebugMode', 'Survey', $oEvent->get('survey')),
        )
      )
    ));
  }
  public function newSurveySettings()
  {
    $this->log('newSurveySettings');
    $oEvent = $this->event;
    foreach ($oEvent->get('settings') as $name => $value)
    {
        $this->set($name, $value, 'Survey', $oEvent->get('survey'));
    }
  }
  // ensure the survey is approved before users can activate it
  public function beforeSurveyActivate()
  {
    $this->log('beforeSurveyActivate');
    $this->log($this->get('bSurveyIsApproved'));// nothing is printed
    $this->log($this->get('bDebugMode'));// nothing is printed
    $this->log(is_null($this->get('bDebugMode')));// 1 is printed meaning the settings I get are NULL
    if ($this->get('bSurveyIsApproved') == 0)
    {
      $oEvent = $this->getEvent();
      $oEvent->set('success', false);
      $oEvent->set('message', 'This survey cannot be activated. Before publishing a survey, it must be approved.');
    }
  }
}



I've also tried using
Code:
$this->get('bDebugMode', 'Survey', $oEvent->get('survey')
to retrieve settings, but that returns NULL as well.
Last edit: 2 years 5 hours ago by mhladun.

Please Log in to join the conversation.

  • mhladun
  • mhladun's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 5 hours ago - 2 years 5 hours ago #227698 by mhladun
Replied by mhladun on topic Why are my Plugin Settings NULL?
Some of my attempts. 

Note that adding a default argument works, but it's mandatory in order to get something that isn't NULL.
Last edit: 2 years 5 hours ago by mhladun.

Please Log in to join the conversation.

  • mhladun
  • mhladun's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
1 year 11 months ago #227725 by mhladun
Replied by mhladun on topic Why are my Plugin Settings NULL?
In order to access survey level plugin settings, I needed to use:

$oEvent = $this->event;
$my_setting = $this->get('bSurveyIsApproved', 'Survey', $oEvent->get('surveyId'));

I was using the key `survey` when I needed to use `surveyId`.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 11 months ago #228111 by DenisChenu
Replied by DenisChenu on topic Why are my Plugin Settings NULL?
Yes : with $this->get('bSurveyIsApproved') : you have on ly the "global" one.

I use it for default in general.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose