Welcome, Guest
Username: Password: Remember me

TOPIC: Javascript expression not updating in survey

Javascript expression not updating in survey 2 years 6 months ago #79587

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:
The administrator has disabled public write access.

Re: Javascript expression not updating in survey 2 years 6 months ago #79591

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Javascript expression not updating in survey 2 years 6 months ago #79596

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
The administrator has disabled public write access.

Re: Javascript expression not updating in survey 2 years 6 months ago #79598

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.
The administrator has disabled public write access.

Re: Javascript expression not updating in survey 2 years 6 months ago #79600

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
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: 2 years 6 months ago by TMSWhite.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.151 seconds
Donation Image