You are here

function pcp_get_complete_percentage_data in Profile Complete Percent 7

Same name and namespace in other branches
  1. 8 pcp.inc \pcp_get_complete_percentage_data()
  2. 5 pcp.module \pcp_get_complete_percentage_data()
  3. 6.2 pcp.module \pcp_get_complete_percentage_data()
  4. 6 pcp.module \pcp_get_complete_percentage_data()

Get the profile complete percentage data for a given user.

Parameters

$user: User object

Return value

PCP data array.

6 calls to pcp_get_complete_percentage_data()
pcp_block_view in ./pcp.module
Implements hook_block_view().
pcp_handler_field_profile_completeness::render in includes/pcp_handler_field_profile_completeness.inc
Render data.
pcp_handler_field_profile_completeness_core::render in includes/pcp_handler_field_profile_completeness_core.inc
Render data.
pcp_rules_profile_is_completed in ./pcp.rules.inc
Helper function for the condition 'User profile is completed'
pcp_tokens in ./pcp.tokens.inc
Implements hook_tokens().

... See full list

File

./pcp.module, line 176
Allows users with valid permissions to tag profile fields (core fields or Profile2 fields) for a users profile to be considered complete.

Code

function pcp_get_complete_percentage_data($entity_type, $bundle, $user) {
  $entity_fields = field_info_instances($entity_type, $bundle);

  // Determine which fields have been tagged for evaluation.
  $profile_fields = pcp_get_tagged_profile_fields($entity_type, $bundle, NULL);
  $additional_fields = pcp_get_additional_tagged_fields(NULL, $entity_type, $bundle);
  $fields = $profile_fields + $additional_fields;

  // Restrict profile completeness for fields the user don't have access to.
  // Access rules are provided by user role field module.
  if (module_exists('user_role_field')) {
    $user_roles = array_keys($user->roles);
    $fields = _pcp_check_user_field_access($fields, $user_roles);
  }

  // We need to define these values here in case $data is returned early.
  $data = array();
  $data['entity_type'] = $entity_type;
  $data['bundle'] = $bundle;
  $data['uid'] = $user->uid;

  // If no fields have been tagged, indicate that the profile is complete.
  if (!$fields) {
    $data['current_percent'] = 100;
    $data['incomplete'] = 0;
    return $data;
  }

  // Gather all profile values for this user.
  $user_profile_values = pcp_get_user_profile_values($user->uid, $entity_type, $bundle);
  $user_additional_values = pcp_get_user_additional_values($user->uid, $entity_type, $bundle);
  $user_values = $user_profile_values + $user_additional_values;

  // Enumerate empty fields by comparing tagged fields to user profile values.
  $empty_fields = array();
  foreach ($fields as $field_name) {
    if (empty($user_values[$field_name])) {
      $empty_fields[$field_name] = $field_name;
    }
  }

  // If there is one empty field or more.
  if ($empty_fields) {
    $profile_fields_order_value = variable_get('pcp_fields_order', '0');

    // Pick up a random field, we won't use shuffle because it
    // re-indexes the array keys
    if ($profile_fields_order_value == 0) {
      $field_name = array_rand($empty_fields);
    }
    else {

      // Assign any higher value.
      $old_field_instance_weight = '999';
      foreach ($empty_fields as $key => $user_field_name) {
        $field_instance = field_info_instance('user', $user_field_name, 'user');
        $field_instance_weight = $field_instance['widget']['weight'];
        if ($field_instance_weight < $old_field_instance_weight) {
          $field_name = $user_field_name;
          $old_field_instance_weight = $field_instance_weight;
        }
      }
    }
    if ($field_name == 'user_picture') {
      $field = array(
        'fiid' => 'user_picture',
        'title' => 'User Picture',
        'name' => 'picture_upload',
      );
      $field_title = t($field['title']);
    }
    else {
      $field_title = $entity_fields[$field_name]['label'];
      if (module_exists('i18n_field') && !empty($field_title)) {
        $field_title = i18n_field_translate_property($entity_fields[$field_name], 'label');
      }
    }
    $data['nextfield_title'] = $field_title;
    $data['nextfield_name'] = $field_name;
  }
  $fields_count = count($fields);
  $empty_fields_count = count($empty_fields);
  $completed_fields = $fields_count - $empty_fields_count;
  $current_percent = number_format($completed_fields / $fields_count, 2) * 100;
  $next_percent = number_format(($completed_fields + 1) / $fields_count, 2) * 100;
  $data['completed'] = $completed_fields;
  $data['incomplete'] = $empty_fields_count;
  $data['total'] = $fields_count;
  $data['current_percent'] = $current_percent;
  $data['next_percent'] = $next_percent;
  return $data;
}