Commit 50f0e863 authored by Chenu Denis's avatar Chenu Denis
Browse files

[feature] Expression Script: make answer option text available.

parent 4dd519ce
<?php
/**
* This file is part of ExpressionAnswerOptions plugin
* @version 0.1.0
*/
namespace ExpressionAnswerOptionsExt;
use LimeExpressionManager;
use Question;
use Answer;
class AnswerOptionsFunctions
{
/**
* Return the answer text related to a question
* @param integer|string $qid : id or code of question, get parent question if needed
* @param string $code : code of the answer text to return
* @return null|string
*/
public static function getAnswerOptionText($qidortitle, $code, $scale = 0)
{
$surveyId = LimeExpressionManager::getLEMsurveyId();
$oQuestion = null;
if (is_int($qidortitle) || ctype_digit($qidortitle)) { // self.qid is not an int …
$oQuestion = Question::model()->find("qid = :qid and sid = :sid", array(":qid"=>$qidortitle, ":sid" => $surveyId));
if ($oQuestion && $oQuestion->parent_qid) {
$oQuestion = Question::model()->find("qid = :qid and sid = :sid", array(":qid"=>$oQuestion->parent_qid, ":sid" => $surveyId));
}
}
if(empty($oQuestion)) {
$oQuestion = Question::model()->find("title = :title and sid = :sid", array(":title"=>$qidortitle, ":sid" => $surveyId));
}
if(empty($oQuestion)) {
return null;
}
/* Don't check the question type : we know it's a question (not a subquetsion) */
$language = LimeExpressionManager::getEMlanguage(); // Or by App()->getLanguage(), em for expression file view ?
return Answer::model()->getAnswerFromCode($oQuestion->qid, $code, $language, $scale);
}
}
<?php
/**
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2021 Respondage <https://www.respondage.nl/>
* @copyright 2021 Denis Chenu <https://www.sondages.pro>
* @license GPL version 3
* @version 0.1.0
*
* 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
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
class ExpressionAnswerOptionsExt extends PluginBase
{
protected static $description = 'Expression Script: make answer option text available.';
protected static $name = 'ExpressionAnswerOptionsExt';
/** @inheritdoc, this plugin didn't have any public method */
public $allowedPublicMethods = array();
public function init()
{
$this->subscribe('ExpressionManagerStart', 'newValidFunctions');
}
public function newValidFunctions()
{
Yii::setPathOfAlias("ExpressionAnswerOptionsExt", dirname(__FILE__));
$newFunctions = array(
'getAnswerOptionText' => array(
'\ExpressionAnswerOptionsExt\AnswerOptionsFunctions::getAnswerOptionText',
null, // No javascript function : set as static function
$this->gT("Return the answer text related to a question by answer code"), // Description for admin
'string getAnswerOptionText(QuestionCode, code[, scale = 0])', // Extra description
'https://www.respondage.nl', // Help url
2, // Number of argument : minimum 2, allow 3
3
),
);
$this->getEvent()->append('functions', $newFunctions);
}
}
# ExpressionAnswerOptionsExt
Expression Script: make answer option text available.
\ No newline at end of file
Expression Script: make answer option text available inside survey by expression function.
## Installation
### Via GIT
- Go to your LimeSurvey Directory
- Clone in plugins/ExpressionAnswerOptionsExt directory : `git clone https://gitlab.com/SondagesPro/ExpressionManager/ExpressionAnswerOptionsExt.git`
Plugin was tested on Version 5.2.2 , can work with version 4.0 and up.
## Usage
The plugin can be used on single choice question and array question , all question with realted answers editable by administrator.
The function getAnswerOptionText get 3 parameters
1. The question id or title
2. The answer code to get
3. The scale (for array dual scale question)
Some sample `getAnswerOptionText(self.qid,"A1")`, `getAnswerOptionText("Qcode","A1")` or `getAnswerOptionText(Qcode_SQ01.qid,"A1")`.
## Contribute and issue
Contribution are welcome, for patch and issue : use [gitlab](https://gitlab.com/SondagesPro/ExpressionManager/ExpressionAnswerOptionsExt).
## Home page & Copyright
- Copyright © 2021 Respondage <https://respondage.nl>
- Copyright © 2021 Denis Chenu <https://sondages.pro>
- Licence : GNU General Public License <https://www.gnu.org/licenses/gpl-3.0.html>
<?xml version="1.0" encoding="UTF-8"?>
<config>
<metadata>
<name>ExpressionAnswerOptionsExt</name>
<type>plugin</type>
<creationDate>2021-11-18</creationDate>
<last_update>2021-11-18</last_update>
<author>Denis Chenu (for Respondage)</author>
<authorUrl>https://www.respondage.nl</authorUrl>
<supportUrl>https://www.respondage.nl</supportUrl>
<version>0.1.0</version>
<license>GNU General Public License version 3 or later</license>
<description><![CDATA[Expression Script: make answer option text available.]]></description>
</metadata>
<compatibility>
<version>5.0</version>
</compatibility>
<updaters disabled="disabled">
</updaters>
</config>
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