Forum Replies Created

Viewing 15 replies - 31 through 45 (of 89 total)
  • 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;
    }

    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

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

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

    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!

    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

    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?

    New idea, Roland.

    To get around my problem of the calculated fields not saving, perhaps because of the total points being based on other calculated points, is there a way it can save twice when someone updates their record? That would solve my problem.

    Thanks!!!

    By the way, according to my debugging log, the 4 points fields are processed 4 times:
    1st time – before one is logged in
    LOGGED IN
    2nd and 3rd time – after login and before record is updated
    RECORD UPDATED
    4th time – after record is updated

    Which is the time processed by the custom calc program? I wonder if it’s that 4th pass, as the updated record does not show the points updated.

    The only calculated field that’s based on other calculated fields is the total points field. And that field is after the 3 other points calculations. In the Manage Database Fields page, the 4 Points calculation fields are in its own field group called Points. What’s after the Points field group are Field Groups and Help. In my mind they are already the last thing to process.

    ok, I put a dummy calculation template in them. No more template unformatted message.

    The other possibility is that since these have to numeric calculation fields, the template interferes with the custom code.

    With the custom code, after I update the Earth Day Challenge actions in the record and press Save, the actions are saved, but not the points right away. But if I press Save one more time (with or without changes), then those last points would show up. Leaving the record and logging back in has no effect on the points being updated any sooner. That means the participant would have to press Save twice to get the correct points.

    The good thing is the 3 kinds of calculated points and the total points get updated at the same time. The 3 calculated fields based on updated actions seem to be calculating fine. So if there’s anything wrong with the custom code, it may be where the hook is being placed.

    No, I have a blank calculation template. That’s why I’m getting this message in my debugging log, which you said I can ignore.
    calculation template format not recognized for template: =[?unformatted]

    And because using the template takes 2 passes to update the value, I am using
    pdb-custom-calc-field.php and that’s what I’m expecting to update the data immediately.

    I am. I’m using version 2.0.5 but it’s still not saving the current updates. I’ve cleared my cache too.

    Hi Roland,

    Any luck figuring out why the calculated value is not updating the record immediately?

    Thanks!

Viewing 15 replies - 31 through 45 (of 89 total)