I have implemented the data described here and now want to create a view that will work out the handicap for a particular player. I have so far created my view which list players and using a relationship to results for the player, and then aggregating these fields, can display things like the players best score and their average score.
Next I want to display the players "Handicap", which is a complex aggregation. Namely, it's the average of the best 8 of the last 20 results multiplied by 0.93. Can you write custom aggregation functions? Or should I create a new field on the result? I'm not sure of the easiest way to go about this.
Thanks!
EDIT: Thanks to @Jimajamma I was pointed in the right direction. You can do what he suggested purely from a module. I used the Fields UI to create a field that I can then manipulate to keep it simple. In the module I've used hook_theme to register a tpl.php file. I struggled to get my preprocess function to be recognised because of the base_hook
directive which seems to then cause views/core to ignore the preprocess functions
directive. The following is the simplest module you can create:
function golf_ladder_views_api() {
return array('api' => 3.0);
}
function golf_ladder_theme($existing) {
return array(
'views_view_field__field_handicap' => array (
'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
'template' => 'views-view-field--field-handicap',
/* 'base hook' => 'views_view_field',
* Don't do this if you want to use a preprocess that you control.
* If you define base hook views seems to want to ignore the
* 'preprocess functions' directive below
*/
'path' => drupal_get_path('module', 'golf_ladder'),
'preprocess functions' => array(
'template_preprocess',
'template_preprocess_views_view_field',
'golf_ladder_preprocess_views_view_field__field_handicap',
),
),
);
}
function golf_ladder_preprocess_views_view_field__field_handicap(&$variables) {
$variables['output'] = '4'; // TODO: calculate proper handicap
}
After that create a template file views-view-filed--field-handicap.tpl.php
which is simply:
print $output;