thexerteproject / xerteonlinetoolkits

Xerte Online Toolkits
www.xerte.org.uk
Apache License 2.0
62 stars 61 forks source link

Questions about use of conditional variables and perhaps json structure... #1043

Open ronm123 opened 2 years ago

ronm123 commented 2 years ago

I've been asked about the following:

Do you know if there is a simple way to build variables in Xerte that have a number of properties associated with them? For example

Name =sodium (melting point =56, group =1, valency=1) Name=potassium (melting point =88, group=1, valency=1) Name =chlorine (melting point =-99, group =7, valency =1) Name =oxygen (melting point -208, group =6, valency =2)

The idea would be that the name was the variable and then all of the other properties would be picked up and could be used in the question ( eg What is the melting point and valency of sodium? What is the melting point and valency of potassium?). I appreciate that it could probably be done with nested if loops or maybe case statements but it would be a pain (and error prone) to do. It would be useful to be able to write chemical calculation questions where the reagents reacting are random but the values of certain properties are correct for the chemical (e.g. calculate the enthalpy of combustion of xx given that its enthalpy of formation is yy )

In NUMBAS they have a JSON data structure that's perfect but the front end is very ugly. I wondered whether there was something similar in Xerte? I couldn't see anything in the guidance... I suppose its also similar to a class in C++ and other OOP languages.

JSON data structure example: [ { "name": "methane (CH4)", "combustion enthalpy": -890.4, "formation enthalpy": -79.4, "no_of_H":4, "no_of_C":1, "no_of_O":0, "entropy": 186 }, { "name": "ethane (C2H6)", "combustion enthalpy": -1560, "formation enthalpy": -84.7, "no_of_H":6, "no_of_C":2, "no_of_O":0, "entropy": 230 }, ]

I'd seen that advice about variables. However these all of that info seems to be essentially about single independent variables e.g. a=10,20, or 30 . b is independent and can be 1,2, or 5. What I'm after is something where the value of one variable determines the values of the other variables. I think its often done as lists, or dictionaries but I guess an array could be made to work. I can't see this mentioned anywhere in the variables documentation .

e.g. if a=1 then b="fred", c=34 if a=2 then b="joe", c=43 if a=3 then b="bill" , c=98

I'm not sure if this is already achievable in some way or not and if not if it's something that can be enabled or easily achieved with some custom code?

FayCross commented 2 years ago

There's no easy way to do this just with variables but you can use variables and some custom script to get this working. See this example https://xot.xerte.org.uk/play_278 - refresh the page a few times and you should see the data change.

Add variables to project containing placeholder vars for all the info you want to get:

vars

Add Script optional property to project and add object:

var json = [
  {
    "name": "methane (CH4)",
    "combustion": -890.4,
    "formation": -79.4,
    "H":4,
    "C":1,
    "O":0,
    "entropy": 186
  },
  {
    "name": "ethane (C2H6)",
    "combustion": -1560,
    "formation": -84.7,
    "H":6,
    "C":2,
    "O":0,
    "entropy": 230
  },
];

Also in script optional property, add code to replace the placeholder var values with random data from json:

var randomNum = Math.floor(Math.random() * json.length);
XENITH.VARIABLES.setVariable('name', json[randomNum].name);
XENITH.VARIABLES.setVariable('combustion', json[randomNum].combustion);
XENITH.VARIABLES.setVariable('formation', json[randomNum].formation);
XENITH.VARIABLES.setVariable('H', json[randomNum].H);
XENITH.VARIABLES.setVariable('C', json[randomNum].C);
XENITH.VARIABLES.setVariable('O', json[randomNum].O);
XENITH.VARIABLES.setVariable('entropy', json[randomNum].entropy);

Then on the page you can add the variables using the normal variable markup: vars2

I know most of this is done via script rather than variables alone but using both together mean you can still use the convenient [varName] markup to add the data to pages.

FayCross commented 2 years ago

You can use these in the script fields to get and set variable values:

XENITH.VARIABLES.setVariable('varName', value);
XENITH.VARIABLES.getVariable('varName');
ronm123 commented 2 years ago

Thanks @FayCross I'll pass the info on. Shall we leave the issue open and discuss on the next dev day?

FayCross commented 2 years ago

Yes, it feels like there should be a simpler way of doing this with just variables but the problem is getting the info into the editor. The way I posted above should work though - from the way your question is worded I guess the user isn’t unfamiliar with code to attempt it like this.

On 5 Aug 2021, at 12:08, Ron Mitchell @.***> wrote:

 Thanks @FayCross I'll pass the info on. Shall we leave the issue open and discuss on the next dev day?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

ronm123 commented 2 years ago

@FayCross I don't know the user as I was asked about this by someone else on behalf of the user. But yes I came to the same conclusion so will pass your suggestions on and see how they respond. I'm aware of how some other tools try to make this kind of thing easier for authors rather than just pure code so yes let's add as an agenda item for the next dev day. Thanks Ron