• 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: Checked

    So 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

Viewing 15 replies - 61 through 75 (of 94 total)
  • Plugin Author xnau webdesign

    (@xnau)

    About saving twice automatically: no way to do that.

    You first question about which time the filter is applied: it will be applied every time the field is updated.

    Did you try my suggestion of re-ordering the fields?

    Yes, I responded to your suggestion. The points are already in the Points field group at the end of all my other fields, but before Field Groups and Help. And total points is after the other 3 points it adds.

    Is there any way to hard code in the updating of the 4 Points fields?

    Regarding my question about when the filter is applied, and you said it’s applied every time the field is updated, well, I pressed the button once, and each field got 4 error message with that unidentified template (before I had the dummies in the field). One of those error messages happened after the record was updated. So I’m wondering if the filter hook is at the wrong moment of the Record Update. If that’s so, it can explain why the record is not updated with the points, but the calculations are there in the background for the next Save.

    I really have no idea how else I can finish the project. Explain to participants that they need to press the Save button twice? What do you suggest?

    Thanks, Ivy

    Hi Roland,

    I’ve put a message in there for now to have people press Save twice.

    I wonder if you can set up a numeric calculation field in the filter that’s based on the calculation of two other numeric calculation fields, and see if you get the same issue. Thanks!

    Plugin Author xnau webdesign

    (@xnau)

    I’ve got some time to night to try this out, see if I can make it work.

    Plugin Author xnau webdesign

    (@xnau)

    I just posted an update to Participants Database that should help your situation. Changed how the values are brought in for calculated fields. You have to make sure the fields are in the right order so the calculations that need to be done first are higher in the list.

    Thank you so much, Roland! I’ll look out for the update.

    I don’t see the update yet. It takes a while to come through?

    Plugin Author xnau webdesign

    (@xnau)

    I got a little ahead of myself, it will be up momentarily.

    It didn’t make any difference. Sorry!

    I also tried changing total_points to a non-calculation field, so that its calculation won’t affect anything else. And just as I thought, it didn’t affect the need to Save twice to update the database with the other points. I don’t think my problem has anything to do with the total points being based on the other calculation points. It’s not like it’s not calculating the correct value.

    You said you can update your calculation fields immediately using
    pdb-custom-calc-field.php with
    add_filter( ‘pdb-calculated_field_calc_value’, ‘pdb_calculate_value’, 10, 3 );
    right? If so, how can it be that you can update the Record immediately and I can’t? What are we doing differently?

    Thanks for helping, Ivy

    Plugin Author xnau webdesign

    (@xnau)

    The pdb-calculated_field_calc_value filter is applied every time the calculated value is updated. Typically, this means when the record is updated. What it does is provides an oppotunity to give a calculated field a value (it ignores the calculation template), which is saved to the record when the the record is updated. You can see this in action if you have plugin debugging turned on.

    I have not seen your code, but the function on pdb-calculated_field_calc_value filter is expected to return the new value for the field. That’s all it has to do, you don’t need to save the new value.

    I just tested this and it works as expected. You can also use values from other calculated fields as long as those fields are higher in the list of fields and so will be calculated before the field you’re using the filter on.

    You can see this demonstrated in this sample code: https://gist.github.com/xnau/9bc6bfa1046e0f2927d25a10ce185ed6

    I have no idea why mine doesn’t work. I don’t have any error messages related to it in my debugging log.

    Do you mind looking at my code to see if you see anything wrong with it?
    It seems like everything is returned correctly, just not updating the database immediately. Could there be another switch somewhere else that you have on but I don’t, or vice versa?
    ——————————————————————-

    <?php
    
    /**
     * Plugin Name: PDB Custom Calculated Field
     * Description: demonstrates how to implement a custom calculation for a calculated field
     */
    
    add_filter( 'pdb-calculated_field_calc_value', 'pdb_calculate_value', 10, 3 );
      
    /**
     * supplies the calculated value
     * 
     * @param bool $result
     * @param array $replacement_data
     * @param \PDb_Field_Item $field
     * @return mixed bool false to use built-in calculation or result of the calculation
     */
    function pdb_calculate_value( $result, $replacement_data, $field )
    {
      /* this is the field that we will be calculating the value for
       * 
       * this must be the name of a Numeric Calculation or Date Caclculation field
       *
       */
    
        $record_data = Participants_Db::get_participant( $field->record_id );
       
       if ( $field->name() == 'role_points' || $field->name() == 'total_points') {
    
        if($record_data['role'] === 'Team Captain'){
           $role_points = 25;}
        elseif($record_data['role'] === 'Teammate'){
           $role_points = 5;}
        else{
           $role_points = 0;}
           
        if ( $field->name() == 'role_points'){
           $result = $role_points;
           }  
        }
        
       if ( $field->name() == 'one_time_points' || $field->name() == 'total_points') {
       
        // this value is set to 0 if it is empty to make sure the value is a number
        $value_44 = empty( $record_data['change_led'] ) ? 0 : $record_data['change_led'];
        $value_45 = empty( $record_data['test_drive'] ) ? 0 : $record_data['test_drive'];
        $value_46 = empty( $record_data['plant_tree'] ) ? 0 : $record_data['plant_tree'];
        $value_47 = empty( $record_data['local_csa'] ) ? 0 : $record_data['local_csa'];
        $value_48 = empty( $record_data['volunteer'] ) ? 0 : $record_data['volunteer'];
        $value_49 = empty( $record_data['paperless'] ) ? 0 : $record_data['paperless'];
        $value_50 = empty( $record_data['rain_barrels'] ) ? 0 : $record_data['rain_barrels'];
        $value_51 = empty( $record_data['aerators'] ) ? 0 : $record_data['aerators'];
        $value_52 = empty( $record_data['microplastics'] ) ? 0 : $record_data['microplastics'];
        $value_53 = empty( $record_data['shower_heads'] ) ? 0 : $record_data['shower_heads'];
        $value_54 = empty( $record_data['running_toilets'] ) ? 0 : $record_data['running_toilets'];
        
        $one_time_points = ($value_44 + $value_45 + $value_46 + $value_47 + $value_48 + $value_49 + $value_50 + $value_51 + $value_52 + $value_53 + $value_54) * 100;
        
        if ( $field->name() == 'one_time_points'){
           $result = $one_time_points;
           }
        }
        
       if ( $field->name() == 'regular_points' || $field->name() == 'total_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'];
        $value_4 = empty( $record_data['screen_time'] ) ? 0 : $record_data['screen_time'];
        $value_5 = empty( $record_data['power_strip'] ) ? 0 : $record_data['power_strip'];
        $value_6 = empty( $record_data['carbon_free_transport'] ) ? 0 : $record_data['carbon_free_transport'];
        $value_7 = empty( $record_data['car_trips'] ) ? 0 : $record_data['car_trips'];
        $value_8 = empty( $record_data['idling'] ) ? 0 : $record_data['idling'];
        $value_9 = empty( $record_data['public_transit'] ) ? 0 : $record_data['public_transit'];
        $value_10 = empty( $record_data['carpool'] ) ? 0 : $record_data['carpool'];
        $value_11 = empty( $record_data['carbon_offsets'] ) ? 0 : $record_data['carbon_offsets'];
        $value_12 = empty( $record_data['grow_flowers'] ) ? 0 : $record_data['grow_flowers'];
        $value_13 = empty( $record_data['herbicides'] ) ? 0 : $record_data['herbicides'];
        $value_14 = empty( $record_data['walk'] ) ? 0 : $record_data['walk'];
        $value_15 = empty( $record_data['play_outside'] ) ? 0 : $record_data['play_outside'];
        $value_16 = empty( $record_data['meeting_outside'] ) ? 0 : $record_data['meeting_outside'];
        $value_17 = empty( $record_data['trash_outside'] ) ? 0 : $record_data['trash_outside'];
        $value_18 = empty( $record_data['local_products'] ) ? 0 : $record_data['local_products'];
        $value_19 = empty( $record_data['local_businesses'] ) ? 0 : $record_data['local_businesses'];
        $value_20 = empty( $record_data['plant_based_diet'] ) ? 0 : $record_data['plant_based_diet'];
        $value_21 = empty( $record_data['refined_sugar'] ) ? 0 : $record_data['refined_sugar'];
        $value_22 = empty( $record_data['smoking'] ) ? 0 : $record_data['smoking'];
        $value_23 = empty( $record_data['organic_ingredients'] ) ? 0 : $record_data['organic_ingredients'];
        $value_24 = empty( $record_data['sustainable_food'] ) ? 0 : $record_data['sustainable_food'];
        $value_25 = empty( $record_data['think_twice'] ) ? 0 : $record_data['think_twice'];
        $value_26 = empty( $record_data['food_waste'] ) ? 0 : $record_data['food_waste'];
        $value_27 = empty( $record_data['eco_friendly'] ) ? 0 : $record_data['eco_friendly'];
        $value_28 = empty( $record_data['plastic_bags'] ) ? 0 : $record_data['plastic_bags'];
        $value_29 = empty( $record_data['produce_in_plastic'] ) ? 0 : $record_data['produce_in_plastic'];
        $value_30 = empty( $record_data['reuse_or_repair'] ) ? 0 : $record_data['reuse_or_repair'];
        $value_31 = empty( $record_data['recycle'] ) ? 0 : $record_data['recycle'];
        $value_32 = empty( $record_data['reuse_boxes'] ) ? 0 : $record_data['reuse_boxes'];
        $value_33 = empty( $record_data['give_away'] ) ? 0 : $record_data['give_away'];
        $value_34 = empty( $record_data['zero_waste'] ) ? 0 : $record_data['zero_waste'];
        $value_35 = empty( $record_data['reusable_cups'] ) ? 0 : $record_data['reusable_cups'];
        $value_36 = empty( $record_data['mulch_setting'] ) ? 0 : $record_data['mulch_setting'];
        $value_37 = empty( $record_data['litter'] ) ? 0 : $record_data['litter'];
        $value_38 = empty( $record_data['compost'] ) ? 0 : $record_data['compost'];
        $value_39 = empty( $record_data['shorter_shower'] ) ? 0 : $record_data['shorter_shower'];
        $value_40 = empty( $record_data['water_usage_teeth'] ) ? 0 : $record_data['water_usage_teeth'];
        $value_41 = empty( $record_data['water_usage_dishes'] ) ? 0 : $record_data['water_usage_dishes'];
        $value_42 = empty( $record_data['water_usage_clothes'] ) ? 0 : $record_data['water_usage_clothes'];
        $value_43 = empty( $record_data['cold_water'] ) ? 0 : $record_data['cold_water'];
           
        $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) * 10;
        
        if ( $field->name() == 'regular_points'){
           $result = $regular_points;
           }
        }
        
       if ( $field->name() == 'total_points' ) {
           $total_points = $role_points + $one_time_points + $regular_points;
           $result = $total_points;
        }
        return $result;
    }
    Plugin Author xnau webdesign

    (@xnau)

    Well, without going too deeply into this (I can’t debug your code), you’re combining the processing of several fields into a single function, makes it way more complex than it needs to be and harder to debug.

    To debug this, you could start by adding error_log statements at various points in the function to make sure each step is completed correctly.

    I’d also suggest you find ways to simplify the code.

    What kind of error_log do you suggest? As I mentioned before, I had echo statements in there for the $result originally, and it’s calculating correctly. It’s just that what was in the echo was not posted to the database immediately. Back then I can step back a page, and the echo would be there, like they’re sitting in the back waiting to be updated.

    I have to say that total points are always in sync with the other 3 points. It posts the total of role points, one time points and regular points, whether those are from a prior time or this time.

    How do you mean to simplify the program? Like this?
    if ( $field->name() == ‘role_points’){}
    elseif ( $field->name() == ‘one_time_points’){}
    elseif ( $field->name() == ‘regular_points’){}
    elseif ( $field->name() == ‘total_points’){}

    That’s how I had it originally, but my non-php programmer friend thought that it’s not good because the total points section has to repeat all the calculations for the other 3 points, adding to the number of lines in the program. If you think my original way is better, I’ll switch back. I don’t think that’s the problem though.

    Incidentally, if I don’t redo all the calculations for role points, one time points and regular points in the program, the total points would be zero. That tells me that even though each of those 3 calculation $result is returned before calculating total points, it is not saved to the database right away. They all sit somewhere and get updated together. So the question is what is the signal that allows them to be updated. I think that signal has to be outside my program.

Viewing 15 replies - 61 through 75 (of 94 total)
  • The topic ‘Numeric Calculation’ is closed to new replies.