mcp_field_report()
Description
Aggregate visible values of an applicable field, counting distinct resources per value.Parameters
| Column | Type | Default | Description |
|---|---|---|---|
| $name | string | ||
| $args | array | ||
| $population: | array |
Location
include/mcp_report_functions.php lines 81 to 132
Definition
function mcp_field_report(string $name, array $args, array $population): array
{
$field = $args['field'];
$entry = mcp_field_catalogue($args['resource_type'] ?? null)[$field] ?? null;
if (!$entry) {
throw new DomainException('Metadata field unavailable.');
}
$definition = get_resource_type_field($field);
$refs = [];
foreach ($population as $id => $row) {
resource_type_config_override($row['resource_type']);
if (
in_array($row['resource_type'], $entry['resource_types']) && metadata_field_view_access($field)
&& (!$definition['hide_when_restricted'] || $row['access'] === RESOURCE_ACCESS_FULL)
) {
$refs[] = $id;
}
}
$values = [];
$populated = $distinct = 0;
if ($refs) {
$where = ' FROM resource_node rn JOIN node n ON n.ref = rn.node WHERE n.resource_type_field = ?
AND rn.resource IN (' . ps_param_insert(count($refs)) . ") AND TRIM(n.name) <> '' AND n.active = 1";
$params = ['i', $field, ...ps_param_fill($refs, 'i')];
if ($name === 'get_field_value_counts') {
$fixed = in_array($definition['type'], $GLOBALS['FIXED_LIST_FIELD_TYPES']);
$option_columns = $fixed ? ', n.ref AS node, n.parent' : '';
$group = $fixed ? 'n.ref, n.name, n.parent' : 'n.name';
$order = ($args['sort_by'] ?? '') === 'value' ? 'n.name ASC' : 'resources DESC, n.name ASC';
$order .= $fixed ? ', n.ref ASC' : '';
$values = ps_query('SELECT n.name AS value, COUNT(DISTINCT rn.resource) AS resources' . $option_columns . $where
. " GROUP BY $group ORDER BY $order LIMIT ?, ?", [...$params, 'i', $args['offset'] ?? 0, 'i', $args['limit'] ?? 25]);
foreach ($values as &$value) {
$value['value'] = i18n_get_translated($value['value']);
$value['resources'] = (int) $value['resources'];
}
} else {
$distinct_column = in_array($definition['type'], $GLOBALS['FIXED_LIST_FIELD_TYPES']) ? 'n.ref' : 'n.name';
$stats = ps_query("SELECT COUNT(DISTINCT rn.resource) AS populated, COUNT(DISTINCT $distinct_column) AS distinct_values"
. $where, $params)[0];
$populated = (int) $stats['populated'];
$distinct = (int) $stats['distinct_values'];
}
}
if ($name === 'get_field_value_counts') {
return mcp_result(['field' => $field, 'values' => $values]);
}
$applicable = count($refs);
return mcp_result(['field' => $field, 'applicable' => $applicable, 'populated' => $populated,
'empty' => $applicable - $populated, 'distinct_values' => $distinct,
'percentage_populated' => $applicable ? round(100 * $populated / $applicable, 2) : null]);
}
This article was last updated 3rd December 2026 16:35 Europe/London time based on the source file dated 3rd December 2026 16:30 Europe/London time.