How to incorporate implemented PHP functions such as strip_tags?

More
1 month 3 weeks ago - 1 month 3 weeks ago #215263 by cheeseburger
Hello all. I'm fuzzy on the order of executions of various things in LS and cannot figure out how to implement a strip_tags() string function into the current syntax I am using. 

I am inside of the footer.twig file of a theme which I am editing from within the LS control panel theme editor. 

You can see that I am using a combination of JS and EM (below):

var myVar   = parseInt(   {{ processString('{q1.value}') }}  );  

The value that may be retrieved from q1.value may include a span tag wrapped around it. 

I am trying to insert the strip_tags() function to remove the span tag so that it leaves the value it is wrapping (such as the number 50), I am then parsing into an integer to make sure it's no longer a string. 

What is the syntax to include the strip_tags() function? I know how to do this in pure PHP but am not sure here. 

I am also referencing this: manual.limesurvey.org/ExpressionScript_-...rence_for_Developers 

Thanks!


 
Last edit: 1 month 3 weeks ago by cheeseburger. Reason: typo

Please Log in to join the conversation.

More
1 month 3 weeks ago #215280 by cheeseburger
I may have found my answer. It looks like the manual is possibly incorrect? I am using version 3.26.0 and when viewing the LS_Twig_Extension.php file, it looks like the function of strip_tags is accessed via flatString()?

{{ flatString( processString('{stripTagTest}'),false ) }}

Using the above syntax, I am able to process using the flatString function but it isn't giving the expected results. It either keeps the span tag in place or it converts <> to the equivalent html entities depending on the second argument value of true|false.

Before I create my own function to add to this file (that will remove the span wrapper), am I missing something that can perform this task?

Thanks!

Please Log in to join the conversation.

More
1 month 3 weeks ago - 1 month 3 weeks ago #215310 by DenisChenu
{{ processString('{q1.value}', 1) }}
 /*** Process any string with current page
* @param string to be processed
* @param boolean $static return static string (or not)
* @param integer $numRecursionLevels recursion (max) level to do
* @param array $aReplacement replacement out of EM
* @return string
*/
public static function processString($string, $static = false, $numRecursionLevels = 3, $aReplacement = array())

github.com/LimeSurvey/LimeSurvey/blob/37...ension.php#L505-L513

Really need a manual page for Twig function

PS : static mean without the Expression script system.
If it's on same page : value retuen witgh sapn to be updfated.
If you need updated value : you need to get it when needed.

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.
Last edit: 1 month 3 weeks ago by DenisChenu. Reason: PS

Please Log in to join the conversation.

More
4 weeks 2 days ago #216417 by cheeseburger
Hi and thank you for the last reply. I've found myself in the following scenario:

I ultimately simply need to use {{ processString('{q1}') }} to get the answer of the question with the QID of 'q1'. I can get this fine, and if it has spans or not, that is currently OK too.

However, the project has evolved a bit and the specific requirement at the moment is that I need to dynamically populate the QID portion of {{ processString('{QID}') }} via JS.

Now due to order of execution, this is probably not possible?

Example:

var myQID = 'q1';
var myQAnswer = {{ processString(myQID) }}

Since I've tried this and get errors for not having an actual string, I then made a twig extension that receives my string and then attempts to pass it over to processString()

Example:
dvPassString(myQID)

Once it is in my dvPassString function (extended twig static and public), I attempt to pass the string over to the processString function but it is not seeing the $var passed as a string and is not parsing into the related QID.

Do you know of a syntax or method that can do this?

Out of context it seems silly. But it is part of a larger block that loops through 100 question IDs and prints to a DOM element and requires the JS to do this.

Thanks

Please Log in to join the conversation.

More
4 weeks 2 days ago #216430 by DenisChenu
It's JS … use JS and HTML …
Why try to use Expression when you can update HTML ?

github.com/LimeSurvey/LimeSurvey/blob/1c...t_container.twig#L23

<div class="{{ aSurveyInfo.class.questiontitlecontainer }} col-xs-12 " {{ aSurveyInfo.attr.questiontitlecontainer }} data-questioncode="{{ aQuestion.code }}">

Then
var myQID = 'q1';
var myQAnswer = $("[data-questioncode='" + myQID + "']).find('.answer-item input,.answer-item select').first().val();


 

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.

More
4 weeks 2 days ago #216447 by cheeseburger
haha I've been attempting to reply but the explanation requires way too many sentences to provide context for what we are trying to achieve. Maybe this will help:

In our earlier explorations we had made a test survey with its sole mission to observe the pros/cons of getting and setting values in LS through different approaches. We've successfully used:
- direct EM to get and set values
- JS/Jquery to get and set values from the input field
- direct EM to get values from questions and to print them as html in the DOM
- JS/Jquery to get vals from questions and to print them as html in the DOM
- Hybrid JS/JQuery and EM (using keyUp Event)

All of the above have found their uses within our surveys - this includes the syntax you suggest above (thank you for that). However, this current scenario doesn't quite work for any of these options.

To spare you from the very long boring details of the current mission, the end goal is that we have a JS loop which is pulling QID values from a JS Obj and we are programmatically inserting over 100 elements into the DOM which we are trying to include the values of questions also. The challenge is that when running this very small loop, it simply needs to access the value of the question (the answer) but since it is a JS loop, we are attempting to pass the QID to the string to populate the DOM versus manually writing 100+ <div></div> lines - and this block of code would remain dynamic and self-updating.

One loop inserts the 100+ <div> elements into the DOM.

Another loop assigns values to each.

In short, each iteration in the assignment loop produces something like this:

var thisQID = someObj[someCount]; //where this grabs the QID stored in the JS object

var thisQVal = dvPassString(thisQID); //where this attempts to send to processString() to get the question's answer value

//then through some other code, we simply assign the value to the related div
$('#'+ThisTrg+'xxx').html(thisQVal);

The alternative is that we manually place all of the direct EM calls to the value inside of the manually inserted 100+ divs. And we can do that, but was hopeful the loop and programmatic method was possible. (this is part of a much larger "application" and this approach benefits many other aspects of the app's functions) and the only missing piece is how to send a JS string to the processString() function so that it can be parsed into a question's answer.

I wasn't sure if one of the optional arguments within processString() could be sent as a parameter to make it work. Or if it required a specific syntax.

Thanks

Please Log in to join the conversation.

More
4 weeks 2 days ago #216448 by DenisChenu
Maybe time to think of Plugin ?

Currently : i use Dispaly text question tyo show "what i want to show"

You have a sample here :
gitlab.com/SondagesPro/ExportAndStats/StatQuestionByTwig

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.

Start now!

Just create your account and start using Limesurvey today.

Register now