You are here

public function fcComplete::completeness in Field Complete 7

1 call to fcComplete::completeness()
fcComplete::fetch in ./fc.inc

File

./fc.inc, line 49
Field Complete - Provides field-based completeness for any entity - class.

Class

fcComplete
@file Field Complete - Provides field-based completeness for any entity - class.

Code

public function completeness() {
  $field_info = field_info_fields();
  $instances = field_info_instances($this->entity_type, $this->bundle);

  // Allow other modules to remove instances from the completeness check
  $options = array(
    'bundle' => $this->bundle,
    'context' => 'view',
  );
  drupal_alter('fc_instances', $instances, $this->entity_type, $this->entity, $options);
  $this->completeness = array();
  foreach ($instances as $field_name => $instance) {
    $settings = $instance['settings']['fc'];
    if (!empty($settings['disable'])) {
      continue;
    }

    // We track the completeness of all fields.
    $this->completeness[$field_name] = FALSE;

    // Go through the field items if any are "not empty" then
    // we count that as complete (a bit simplistic but okay).
    $field_items = field_get_items($this->entity_type, $this->entity, $field_name);
    if (empty($field_items)) {
      if (!empty($settings['fc_allow_empty'])) {

        // Complex fields can be set so that they optionally
        // have content, and if they don't they must be
        // counted as complete
        $this->completeness[$field_name] = TRUE;
      }
      continue;
    }
    $field = $field_info[$field_name];

    // Choose the right plugin for the field type.
    $plugin = fc_get_plugin($field['type']);
    if ($function = ctools_plugin_get_function($plugin, 'completeness check')) {

      // Process the field to determine whether it's complete, normally we
      // just compare the number of complete field_items with the cardinality
      // but other fields might be more complex (like matrix fields).
      $cardinality = ctools_plugin_get_function($plugin, 'cardinality check');
      $this->completeness[$field_name] = $cardinality($function, $field_items, $instance, $field);
    }
  }
  $count_fields = count($this->completeness);
  $complete_fields = count(array_filter($this->completeness));
  $this->complete = $count_fields == $complete_fields;
  $this->percentage = $count_fields ? (int) ($complete_fields * 100 / $count_fields) : 100;
}