Check out the LimeSurvey source code on GitHub!

Javascript expression not updating in survey

More
4 years 8 months ago #79587 by colleenatrsc
I am running version 1.92+ Build 120425. I have created a custom expression manager function - udfchoose. Equations that use udfchoose are not updating when a value changes.

I have defined the following PHP function in classes/expressions/ExpressionManager.php

function udf_choose($args) {
if (!@is_array($args)) return "79";
if (@count($args)<2) return "78";
if (!is_numeric($args[0])) return "77";


$ind = @intval($args[0]);

if ($ind>=@count($args) || $ind<=0) {
return "76";
} else {
return $args[$ind];
}
}

and the following javascript function to scripts/em_javascript.js

function UDFChoose() {
if (!(arguments instanceof Array)) { return "99"; }
if (arguments.length<2) { return "98"; }
if (isNaN(arguments[0])) { return "97";}

index = parseInt(arguments[0]);
if (index<=0 || index>=arguments.length) {
return "96";
} else {
return arguments[index];
}
}

and added the following function definition to the RDP_ValidFunctions array in ExpressionManager.php:

'udfchoose' => array('udf_choose', 'UDFchoose', $this->gT('Returns a value from a list based on the value of the first argument'), 'number udfchoose(index, arg1, arg2, ... argN)', '', -3)

See attached survey section.

Colleen
Attachments:

Please Log in to join the conversation.

More
4 years 8 months ago #79591 by Mazi
Sorry, but we can't help much when it comes to customized coding.

Can't you use the on-board EM features for implementing this, see docs.limesurvey.org/Expression+Manager&s...#Access_to_Functions

What do you plan to use that code for?


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
4 years 8 months ago #79596 by colleenatrsc
Thanks for responding

The function returns one of the input parameters depending on the numerical value of the first parameter, similar to the VBA choose function.

I have added the UDFChoose javascript function to scripts/em_javascript.js, and defined the PHP and javascript functions in RDP_ValidFunctions in classes/expressions/ExpressionManager.php. The PHP function works. Is there anything else I need to do in order to add a new function to expression manager?

Colleen

Please Log in to join the conversation.

More
4 years 8 months ago #79598 by colleenatrsc
I took out the array check from the UDFChoose function and the javascript is updating the output now. I am pretty sure I tried this before, maybe I needed to clear my cache.

Thanks for your help.

Please Log in to join the conversation.

More
4 years 8 months ago - 4 years 8 months ago #79600 by TMSWhite
Glad it worked. Yes, you need to update your cache (or use F5 to reload the page) every time you change em_javascript.js

Also, I think your function definition should be:
'udfchoose' => array('udf_choose', 'UDFchoose', $this->gT('Returns a value from a list based on the value of the first argument'), 'number udfchoose(index, arg1, arg2, ... argN)', '', -2)

Instead of -3, since it looks like you only take one mandatory argument.

As per the documentation on adding functions , it is worth adding test cases to Expression Manager to verify that your new function is working as desired.

That also looks like a helpful function. If you are using GitHub, you should consider opening a pull request so the team could have the option of incorporating that function into the base. The is a current work-around, which is to use nested if() statements, but that can get cumbersome; and extensions to udfchoose() could take a delimited syntax for the second argument so that users could implement more generic array lookup functionality (e.g. by having udfchoose() split the second parameter on the delimiter and then return the desired sub-element).
Last Edit: 4 years 8 months ago by TMSWhite.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form