6

Wondering how to do this, it clearly would need an extension (or, as I have done for other things, a Drupal module) but just looking for any tips really.

I would like to be able to specify a default status when defining an activity type. When case workers are with a client they take the opportunity to do whatever they can at the time, whatever presents itself, e.g. refer the client to another organisation, or make copies of documents they brought in. These activities are normally logged at the same time that they are completed. It is annoying for the worker to have to manually set the activity to completed when they log it, they often don't, which results in "scheduled" activities lying around that are actually completed. But other types of activities are genuinely scheduled for the future, e.g. "review" meetings that are automatically generated for certain lengths of time in the future when a client is taken on.

So it would be good if, when defining an activity type, we could state whether it is default completed or default scheduled.

4
  • Naomi, i concur. reminds me it would also be nice if one could offer different Status for different Activity Types sinc sometimes one wants a specific Status that is only applicable to a particular Activity Type. just wishing out loud but i guess it relates in the sense of having some configurability on a per Activity Type basis.
    – petednz - fuzion
    Nov 3, 2016 at 19:14
  • See civicrm.stackexchange.com/questions/15074/… which is a way to do what you want by directly updating the database. Your use case is exactly the same as mine for 2 clients. So would be good to have this available in the UI
    – Andy Clark
    Nov 3, 2016 at 21:34
  • Thanks Andy, interesting stuff following those links. I will probably go with the buildForm method. It would be nice to add something to the form when creating a new activity type, too, in order to pick which default status it should have. Which I guess is what you want too. If I have time I'll do it and let you know! Big if!
    – naomi
    Nov 4, 2016 at 8:54
  • Actually, updating the database directly looks a lot easier...
    – naomi
    Nov 4, 2016 at 8:59

1 Answer 1

3

Not an answer but rather a kind of half temporary workaround, posting in case someone finds it useful, and beacuse you metioned you are familiar with creating an extension/module.

You can use hook_civicrm_buildForm to alter the form status id based on activity type_id/name, this soultion is not user friendly and it won't work from standalone context, ie when using Contacts -> New Activity, but it can do the trick if you can live with that and don't have a ton of Activities Types.

Hope it helps, and let me know if there's anything unclear.

// Implement myextension_civicrm_buildForm 
function myextension_civicrm_buildForm( $formName, &$form ){
    if ( $formName == 'CRM_Activity_Form_Activity' ) {
        // Hook only when adding an Activity and the context is not standalone
        if ( $form->getAction() == CRM_Core_Action::ADD && $form->_context != 'standalone' ) {
        
            // The array that holds the Status and Activities that should have that status by default
            // and the only bit that you'll have to change/upadte when you add more activities and statuses
            $activities = array( 
                'Completed' => array( 'Meeting', 'Phone Call' ),
                'No-Show' => array( 'Custom Activity' ),
                'My Custom Status' => ( 'Another Custom Activity', 'Yet Another Custom Activity' )
            );

            foreach ($activities as $status => $activity_types) {
                if ( in_array( $form->_activityTypeName, $activity_types ) ) {
                    // Get status_id by Label
                    $result = civicrm_api3('OptionValue', 'getsingle', array(
                        'sequential' => 1,
                        'option_group_id' => 'activity_status',
                        'label' => $status,
                    ));
                    // Set the default value in the form
                    $defaults['status_id'] = $result['value'];
                    $form->setDefaults($defaults);
                }
            }
        }
    } 
}

EDIT If you want to do the same thing when in standalone context you could add some jQuery/JavaScript to the form, see below updated snippet and JS snippet.

// Implement myextension_civicrm_buildForm 
function myextension_civicrm_buildForm( $formName, &$form ){
    if ( $formName == 'CRM_Activity_Form_Activity' ) {
        // Hook only when adding an Activity and the context is not standalone
        if ( $form->getAction() == CRM_Core_Action::ADD ) {
        
            // The array that holds the Status and Activities that should have that status by default
            // and the only bit that you'll have to change/upadte when you add more activities and statuses
            $activities = array( 
                'Completed' => array( 'Meeting', 'Phone Call' ),
                'No-Show' => array( 'Custom Activity' ),
                'My Custom Status' => ( 'Another Custom Activity', 'Yet Another Custom Activity' )
            );
            
            if( $form->_context != 'standalone' ){
                foreach ($activities as $status => $activity_types) {
                    if ( in_array( $form->_activityTypeName, $activity_types ) ) {
                        // Get status_id by Label
                        $result = civicrm_api3('OptionValue', 'getsingle', array(
                            'sequential' => 1,
                            'option_group_id' => 'activity_status',
                            'label' => $status,
                        ));
                        // Set the default value in the form
                        $defaults['status_id'] = $result['value'];
                        $form->setDefaults($defaults);
                    }
                }
            } else {
                // Make $activites available in JS
                CRM_Core_Resources::singleton()->addVars( 'myextension', array( 'defaultedActivites' => $activities ) );
                // Add JS script
                CRM_Core_Resources::singleton()->addScriptFile( 'myextension', 'defaultedActivites.js' );
            }
        }
    } 
}

// in your JS script => defaultedActivites.js
// Same principle as above, there might a better way to do this JS-wise but that should do it
CRM.$(function($) {
    $( '#activity_type_id' ).change( function(){
        var defaultedActivites = CRM.vars.myextension.defaultedActivites; // Store defaultedActivites
        var activity = $( this ).select2( 'data' ); // Get the select2 Activity object, ie Activty type
        $.each( defaultedActivites, function( status, activityTypes ){
            activityTypes.map( function( currentActivity ){
                if( currentActivity == activity.text ){
                    // Get status_id by label
                    CRM.api3( 'OptionValue', 'getsingle', {
                        'sequential': 1,
                        'option_group_id': 'activity_status',
                        'label': status
                    }).done( function( statusId ) {
                        // Set default value
                        $("#status_id").val( statusId.value ).trigger("change");
                    });
                }
            })
        })
    });
});
2
  • Thanks, I will probably use this. Just had a play with standalone context and custom fields, and when you pick the activity type the custom fields appear so there is some process going on there that could be hijacked - if standalone context is important. I think in our workflow it's not
    – naomi
    Nov 4, 2016 at 9:05
  • 1
    @naomi I've updated the answer to cover also the standalone context using a bit of javascript
    – Andrei
    Nov 4, 2016 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.