Numeric Calculation
-
Hello again,
Sorry to keep asking lots of questions, but once I have the main setup done, I will be good to go…so thank you for your patience.
Is there a way with the current version, or possibly with your upgraded version to count entries in a “Multiselect Checkbox” – so for example, if the multiselect was as follows:
Car Design:
– Leather seats: Checked
– Alloy wheels: Checked
– Metallic paint: Unchecked
– Satnav: Unchecked
– Diesel: CheckedSo in this case all the “Checked” items would equate to 1 and all the “Unchecked” items would equate to 0, which means the total would equal 3 in the above example.
Thank you
-
Help, Roland! I’m at a loss again.
The debugging log doesn’t show any activity whatsoever for the calculation but I know it’s being accessed cuz I’ve crashed my site many times by fiddling with the program.
[02/12/22 5:33am UTC] PDb_submission\main_query\base_query::execute_query storing record: UPDATE wp_participants_database SET <code>date_updated</code> = NOW(), <code>role</code> = 'Team Captain', <code>team</code> = 'Old Friends', <code>organization</code> = 'Self', <code>change_led</code> = '0', <code>test_drive</code> = '0', <code>plant_tree</code> = '0', <code>local_csa</code> = '1', <code>volunteer</code> = '1', <code>paperless</code> = '1', <code>rain_barrels</code> = '1', <code>aerators</code> = '1', <code>microplastics</code> = '1', <code>shower_heads</code> = '1', <code>running_toilets</code> = '1', <code>turn_off_lights</code> = '22', <code>small_appliances</code> = '22', <code>indoor_temp</code> = '22', <code>screen_time</code> = '22', <code>power_strip</code> = NULL, <code>carbon_free_transport</code> = NULL, <code>car_trips</code> = NULL, <code>idling</code> = NULL, <code>public_transit</code> = '22', <code>carpool</code> = NULL, <code>carbon_offsets</code> = NULL, <code>grow_flowers</code> = NULL, <code>herbicides</code> = NULL, <code>walk</code> = '22', <code>play_outside</code> = NULL, <code>meeting_outside</code> = NULL, <code>trash_outside</code> = NULL, <code>local_products</code> = NULL, <code>local_businesses</code> = NULL, <code>plant_based_diet</code> = NULL, <code>refined_sugar</code> = NULL, <code>smoking</code> = NULL, <code>organic_ingredients</code> = NULL, <code>sustainable_food</code> = NULL, <code>think_twice</code> = NULL, <code>food_waste</code> = NULL, <code>eco_friendly</code> = NULL, <code>plastic_bags</code> = NULL, <code>produce_in_plastic</code> = NULL, <code>reuse_or_repair</code> = NULL, <code>recycle</code> = NULL, <code>reuse_boxes</code> = NULL, <code>give_away</code> = NULL, <code>zero_waste</code> = NULL, <code>reusable_cups</code> = NULL, <code>mulch_setting</code> = NULL, <code>litter</code> = '22', <code>compost</code> = '22', <code>shorter_shower</code> = '22', <code>water_usage_teeth</code> = '22', <code>water_usage_dishes</code> = NULL, <code>water_usage_clothes</code> = NULL, <code>cold_water</code> = '6', <code>role_points</code> = '0', <code>one_time_points</code> = '100', <code>regular_points</code> = '590', <code>total_points</code> = NULL, <code>members</code> = '0', <code>team_points</code> = '0' WHERE id = 8
The beginning of my modified calc php is as follows.
The role field is a radio button field. The one-time points fields are checkbox fields, and the regular points fields are numeric. Do you see anything wrong?$record_data = Participants_Db::get_participant( $field->record_id ); if ( $field->name() == 'role_points' ) { $field_name = 'role_points'; // shows a way to convert a text value to a number if($record_data['role'] === 'Team Captain'){ $role_points = 25;} elseif($record_data['role'] === 'Teammate'){ $role_points = 5;} else{ $role_points = 0;} } elseif ( $field->name() == 'one_time_points' ) { $field_name = 'one_time_points'; // this value is set to 0 if it is empty to make sure the value is a number $value_1 = empty( $record_data['change_led'] ) ? 0 : $record_data['change_led']; $value_2 = empty( $record_data['test_drive'] ) ? 0 : $record_data['test_drive']; $value_3 = empty( $record_data['plant_tree'] ) ? 0 : $record_data['plant_tree']; $value_4 = empty( $record_data['local_csa'] ) ? 0 : $record_data['local_csa']; $value_5 = empty( $record_data['volunteer'] ) ? 0 : $record_data['volunteer']; $value_6 = empty( $record_data['paperless'] ) ? 0 : $record_data['paperless']; $value_7 = empty( $record_data['rain_barrels'] ) ? 0 : $record_data['rain_barrels']; $value_8 = empty( $record_data['aerators'] ) ? 0 : $record_data['aerators']; $value_9 = empty( $record_data['microfibers'] ) ? 0 : $record_data['microfibers']; $value_10 = empty( $record_data['shower_heads'] ) ? 0 : $record_data['shower_heads']; $value_11 = empty( $record_data['running_toilets'] ) ? 0 : $record_data['running_toilets']; $one_time_points = $value_1 * $value_2 * $value_3 * $value_4 * $value_5 * $value_6 * $value_7 * $value_8 * $value_9 * $value_10 * $value_11; } elseif ( $field->name() == 'regular_points' ) { $field_name = 'regular_points'; // this value is set to 0 if it is empty to make sure the value is a number $value_1 = empty( $record_data['turn_off_lights'] ) ? 0 : $record_data['turn_off_lights']; $value_2 = empty( $record_data['small_appliances'] ) ? 0 : $record_data['small_appliances']; $value_3 = empty( $record_data['indoor_temp'] ) ? 0 : $record_data['indoor_temp'];
The end of the program is as follows:
$regular_points = $value_1 * $value_2 * $value_3 * $value_4 * $value_5 * $value_6 * $value_7 * $value_8 * $value_9 * $value_10 * $value_11 * $value_12 * $value_13 * $value_14 * $value_15 * $value_16 * $value_17 * $value_18 * $value_19 * $value_20 * $value_21 * $value_22 * $value_23 * $value_24 * $value_25 * $value_26 * $value_27 * $value_28 * $value_29 * $value_30 * $value_31 * $value_32 * $value_33 * $value_34 * $value_35 * $value_36 * $value_37 * $value_38 * $value_39 * $value_40 * $value_41 * $value_42 * $value_43; } $total_points = $role_points + $one_time_points + $regular_points; $result=array($role_points, $one_time_points, $regular_points, $total_points); return $result; }
Now I probably have one other problem. Eventually these points fields need to be read only fields, cuz people can’t be allowed to change their points. So then they need to go into a signup program?
Whoops! Just found one stupid mistake. The operations should all be + and not *, but that didn’t make any difference to the outcome.
Guess what? I figured out what was partly the problem.
I had changed the 4 points fields from numeric calculation fields back to numeric fields, thinking that the numeric calculation fields won’t work for me. Then I suddenly saw that you wrote the filter for the numeric calculation field. Once I switched back, it worked.
Well, at least it’s showing up in the debugging log. One of the errors in the log is:
calculation template format not recognized for template: =[?unformatted]
Is that something you can fix? It doesn’t sound like something I can fix in the program.Now I’m still not sure if it’s ok that these are read-only fields.
Thanks again for your help!
IvyHi Roland,
I figured out that the numeric calculation fields are read only fields, so that’s ok. It seems like I have the same problem as before. The calculation is looking at fields in the database, rather than what’s being updated. When the user updates the record and presses Save, he’s still in his record after the Save, and he can see that his points are not updated. It seems like he has to get out of his Record, then log back in and Save again to get the last updates. How to get around that?
That template error is still there. I don’t know if that has anything to do with the lag.
Thank you!
Hi Roland,
Some very encouraging news! I added echo into the program, and the echo shows up with all the correct points in my Record after I press Save, then <- to go back one step.
Now the Points fields themselves are wrong, but I can see from the echo that all the calculations are right there. Just not in the right place. Again, hopefully that template issue would correct this. It’s so close!!!
Thanks!
@crowwoods1 I’m having a hard time following what the current status of your support request is, can you summarize the problem you are sill facing? Thanks.
Hi Roland,
I’m at the same place as the last message. My 4 Points fields are not updating with the new changes, but if I do <- to go back one screen, the echo statements I put in the program show up with all the right calculations. I just need those right calculations to be posted to the database.
My debugging log still shows 16 lines of
calculation template format not recognized for template: =[?unformatted]Maybe the two problems are related?
Thanks, IvyWhat is the exact calculation template you are using in the field definition?
The other issue is with your own code, I can’t say what the problem is. Normally, the field will be updated with the new values when the record update is submitted, the new calculated value will be seen right after the update.
I’m just modifying your PDB Custom Calculated Field php plugin.
Where does it indicate a template? That first argument – the filter hook name? I don’t think I downloaded that file with the plugin. Where would I find it?
add_filter( ‘pdb-calculated_field_calc_value’, ‘pdb_calculate_value’, 10, 3 );Or in the function? I didn’t modify either one.
function pdb_calculate_value( $result, $replacement_data, $field )And each $result shows the right value. At the very end, I have the following, which is also unmodified from the original.
return $result;
}The calculation template I’m asking about is in your numeric calculation field configuration. That is the source of the “template format not recognized” message.
As to the rest, I don’t know…you’re doing something with echoing values which is not in the code provided in the tutorial.
I don’t get what you’re saying about the numeric calculation field configuration. In the program? Or in Manage Participant Database? Both seem very straightforward.
I removed all the echos and the result remains the same. Basically, there’s a lag in the writing to the database. When someone presses Save, it saves the changes they make, but the points are from the time before the changes. To get it to update to reflect their current changes, they would have to leave the Record and log back in, not make any changes, and the points would be right.
OH, I just noticed a box in the numeric calculation fields called Template. That’s what’s generating the error. Since I’m no longer using that field directly to calculate points, what should I put there?
It should work to leave it blank.
ok, thanks!
What about the lag in writing to the database? That the points are not posted to the database until the participant next logs back into his Record?
I wonder if the lag is because these are Numeric Calculation fields, which gave me the lag before. So maybe that is overwriting what the program is returning.
- The topic ‘Numeric Calculation’ is closed to new replies.