You are here

recommender.views.inc in Recommender API 6.2

recommender.views.inc Views support for recommender tables Initially contributed by JoeMcGuire at Drupal.org. Thanks!

File

recommender.views.inc
View source
<?php

// $Id: recommender.views.inc,v 1.1 2010/06/03 17:52:22 danithaca Exp $

/**
 * @file recommender.views.inc
 * Views support for recommender tables
 * Initially contributed by JoeMcGuire at Drupal.org. Thanks!
 */
function recommender_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'recommender'),
    ),
    'handlers' => array(),
  );
}
function recommender_views_data() {
  $views_data = array();
  $views_data['recommender_similarity'] = array(
    'table' => array(
      'group' => t('Recommender'),
      'base' => array(
        'field' => 'pair_id',
        'title' => t('Similarity'),
        'help' => t('Recommendation based on similarity between the same type of nodes.'),
        'weight' => 10,
      ),
    ),
    'pair_id' => array(
      'title' => t('ID'),
      'help' => t('Unique ID for each recommendation pair.'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'app_id' => array(
      'title' => t('Application ID'),
      'help' => t('Application used provide data'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'mouse1_id' => array(
      'title' => t('Node ID'),
      'help' => t('Node ID'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'relationship' => array(
        'base' => 'node',
        'base field' => 'nid',
        'handler' => 'views_handler_relationship',
        'label' => t('Node'),
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'mouse2_id' => array(
      'title' => t('Similar Node ID'),
      'help' => t('Node ID for recommended content'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'relationship' => array(
        'base' => 'node',
        'base field' => 'nid',
        'handler' => 'views_handler_relationship',
        'label' => t('Node'),
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'similarity' => array(
      'title' => t('Similarity'),
      'help' => t('Score of how similar Node ID and Similar Node ID are.'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'created' => array(
      'title' => t('Created'),
      'help' => t('Date when the similarity was calculated.'),
      'field' => array(
        'handler' => 'views_handler_field_date',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_date',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort_date',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_date',
      ),
    ),
  );
  $views_data['recommender_prediction'] = array(
    'table' => array(
      'group' => t('Recommender'),
      'base' => array(
        'field' => 'link_id',
        'title' => t('Recommender predictions'),
        'help' => t('Predictions of content users may like from similar users.'),
        'weight' => 11,
      ),
    ),
    'link_id' => array(
      'title' => t('ID'),
      'help' => t('Unique ID for each prediction.'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'app_id' => array(
      'title' => t('Application ID'),
      'help' => t('Application used provide data'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'mouse_id' => array(
      'title' => t('User ID'),
      'help' => t('User ID'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'relationship' => array(
        'handler' => 'views_handler_relationship',
        'base' => 'users',
        'base field' => 'uid',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_user_uid',
        'name field' => 'name',
      ),
    ),
    'cheese_id' => array(
      'title' => t('Predicted Node ID'),
      'help' => t('Node ID for predicted content'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'relationship' => array(
        'base' => 'node',
        'base field' => 'nid',
        'handler' => 'views_handler_relationship',
        'label' => t('Node'),
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'prediction' => array(
      'title' => t('Predicition confidence'),
      'help' => t('Score of how confidence the user will like the Node ID.'),
      'field' => array(
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric',
      ),
    ),
    'created' => array(
      'title' => t('Created'),
      'help' => t('Date when the prediction was calculated.'),
      'field' => array(
        'handler' => 'views_handler_field_date',
        'click sortable' => TRUE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_date',
      ),
      'sort' => array(
        'handler' => 'views_handler_sort_date',
      ),
      'argument' => array(
        'handler' => 'views_handler_argument_date',
      ),
    ),
  );
  return $views_data;
}

/**
 * Implementation of hook_views_plugins_alter().
 * Allow recommender tables to use the node view row style
 * @see recommender_views_query_alter()
 */
function recommender_views_plugins_alter(&$plugins) {

  // Add recommender modules to the base of the node row plugin.
  $plugins['row']['node']['base'][] = 'recommender_similarity';
  $plugins['row']['node']['base'][] = 'recommender_prediction';
}

/**
 * Implementation of hook_views_query_alter().
 * We'd like to make use of the Node view row style. But currently the base table
 * field is being passed through to be used with node_load(). in the case of the
 * recommender tables we need store the node id as cheese_id
 */
function recommender_views_query_alter(&$view, &$query) {

  // We only need to tweak queries which intend to display as node views
  if ($view->style_plugin->row_plugin instanceof views_plugin_row_node_view) {

    // makes changes for the recommender module tables and for everything else do nothing
    switch ($view->base_table) {
      case 'recommender_prediction':

        // add an additional field to the SELECT
        $query->fields['cheese_id'] = array(
          'table' => 'recommender_prediction',
          'field' => 'cheese_id',
          'alias' => 'cheese_id',
        );

        // set the row plugin field_alias to our node id field. This is used by
        // template_preprocess_views_view_row_node().
        $view->style_plugin->row_plugin->field_alias = 'cheese_id';
        break;
      case 'recommender_similarity':

        // Same approach as above see comments
        $query->fields['mouse2_id'] = array(
          'table' => 'recommender_similarity',
          'field' => 'mouse2_id',
          'alias' => 'mouse2_id',
        );
        $view->style_plugin->row_plugin->field_alias = 'mouse2_id';
        break;
      default:

        // do nothing
        break;
    }
  }
}

Functions

Namesort descending Description
recommender_views_data
recommender_views_handlers @file recommender.views.inc Views support for recommender tables Initially contributed by JoeMcGuire at Drupal.org. Thanks!
recommender_views_plugins_alter Implementation of hook_views_plugins_alter(). Allow recommender tables to use the node view row style
recommender_views_query_alter Implementation of hook_views_query_alter(). We'd like to make use of the Node view row style. But currently the base table field is being passed through to be used with node_load(). in the case of the recommender tables we need store the node id…