You are here

user_activity.rules.inc in Heartbeat 6.2

File

user_activity/user_activity.rules.inc
View source
<?php

// by Jochen Stals - ONE-agency - www.one-agency.be

# DATA TYPES

/**
* Implementation of hook_rules_data_type_info()
*/
function activity_log_rules_data_type_info() {
  return array(
    'message' => array(
      'label' => t('logged heartbeat message in the most basic form'),
      'class' => 'rules_data_type_heartbeat_message',
      'identifiable' => FALSE,
    ),
  );
}

/**
* Rules heartbeat activity message data type
*/
class rules_data_type_heartbeat_message extends rules_data_type {

}

# ACTIONS

/**
 * Implementation of hook_rules_action_info().
 * @ingroup rules
 */
function user_activity_rules_action_info() {
  $info = array(
    'user_activity_rules_default_action' => array(
      'label' => t('Logs relational user activity'),
      'eval input' => array(
        'event_param',
        'uid_param',
        'uid_target_param',
        'nid_target_param',
        'message_param',
        'message_concat_param',
        'variables_param',
      ),
      'help' => 'Create a relation between a user and others when handling a node.',
      'module' => 'User activity',
    ),
  );
  return $info;
}

/**
 * Action drupal message configuration form.
 */
function user_activity_rules_default_action_form($settings, &$form) {

  //dsm($settings);
  $settings += array(
    'event_param' => '',
    'uid_param' => '',
    'uid_target_param' => '',
    'nid_target_param' => '',
    'message_param' => '',
    'message_concat_param' => '',
    'variables_param' => '',
  );
  $result = db_query("SELECT event FROM {heartbeat_messages}");
  $options = array();
  while ($row = db_fetch_object($result)) {
    $options[$row->event] = $row->event;
  }
  $form['settings']['event_param'] = array(
    '#type' => 'select',
    '#title' => t('The event'),
    '#options' => $options,
    '#default_value' => $settings['event_param'],
    '#description' => t('The event'),
  );
  $form['settings']['uid_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('User'),
    '#default_value' => $settings['uid_param'],
    '#description' => t('The user doing the activity'),
  );
  $form['settings']['uid_target_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('User target'),
    '#default_value' => $settings['uid_target_param'],
    '#description' => t('The user target of the activity'),
  );
  $form['settings']['nid_target_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('Node target'),
    '#default_value' => $settings['nid_target_param'],
    '#description' => t('The node target of the activity'),
  );
  $form['settings']['message_param'] = array(
    '#type' => 'textarea',
    '#rows' => 1,
    '#title' => t('The message'),
    '#default_value' => $settings['message_param'],
    '#description' => t('The message'),
  );
  $form['settings']['message_concat_param'] = array(
    '#type' => 'textarea',
    '#rows' => 1,
    '#title' => t('The message if it needs to be concatenated in general heartbeat views.'),
    '#default_value' => $settings['message_concat_param'],
    '#description' => t('The message concatenation string'),
  );
  $form['settings']['variables_param'] = array(
    '#type' => 'textarea',
    '#title' => t('The variables area.'),
    '#default_value' => $settings['variables_param'],
    '#description' => t('The variables, leave empty or use with care! <br /> Extra modular stuff goes here with the synthax:<br /><b>var1:value1;var2:value2;va</b> ...'),
  );
}

/**
 * Action drupal message configuration form. 
 * 
 * @desc Update heartbeat_messages to synchronize,
 * and avoid separate versions of messages and message_concats
 */
function user_activity_rules_default_action_submit(&$settings, $form, $form_state) {
  $params = $form_state['values']['settings'];
  db_query("UPDATE {heartbeat_messages} SET message ='%s', message_concat = '%s', variables = '%s' \n    WHERE event = '%s'", $params['message_param'], $params['message_concat_param'], $params['variables_param'], $params['event_param']);
}

/**
 * Action Implementation: Log an activity
 *
 * @param array $settings
 * @param array form
 * @param array with two arrays (set_info and variables)
 * @ToDo  figure out what params mean / can do
 */
function user_activity_rules_default_action($settings, $form = array(), $event_info = array()) {

  // dsm(func_get_args());
  $data = array();
  if (is_array($settings)) {
    $data['uid'] = $settings['uid_param'];
    $data['uid_target'] = $settings['uid_target_param'];
    $data['nid_target'] = $settings['nid_target_param'];
    $data['message'] = t($settings['message_param']);
    $data['message_concat'] = t($settings['message_concat_param']);
    $data['variables'] = $settings['variables_param'];
    $data['event'] = $settings['event_param'];
    $data['hid'] = heartbeat_event_id($settings['event_param']);
  }
  user_activity_log($data);
}

# CONDITIONS

/**
 * Implentation of hook_rules_conditions_info().
 */
function user_activity_rules_condition_info() {

  // example code here
}

# TOKENS

/**
 * Implementation of hook_token_list().
 */

//Taken from friendlist as example
function user_activity_token_list($type = 'all') {

  //dsm('user_activity_token_list called with '.$type);
  if ($type == 'boolean') {
    $tokens['boolean']['1-or-0'] = t("1 for true, 0 for false");
  }
  if ($type == 'node') {
    $tokens['node']['title-link'] = t("The node's title with a link to it");
  }
  if ($type == 'user') {
    $tokens['node']['user-name-url'] = t("The users name with a link to it");
    $tokens['user']['user-profile-url'] = t("The users name with a link to his/her profile page. <strong>USE ONLY when url-path is set to profile/username </strong>");
  }
  return $tokens;
}

/**
 * Implementation of hook_token_values().
 */
function user_activity_token_values($type, $object = NULL, $options = array()) {

  //dsm('user_activity_token_values called with '.$type);

  //dsm($object);

  //dsm($options);
  $values = array();
  switch ($type) {
    case 'heartbeat_message':
      $values['raw'] = $object;
      break;
    case 'boolean':
      $values['1-or-0'] = $object ? '1' : '0';
      break;
    case 'node':
      $values['title-link'] = l($object->title, 'node/' . $object->nid);
      break;
    case 'user':
      $values['user-name-url'] = l($object->name, 'user/' . $object->uid);
      $values['user-profile-url'] = l($object->name, 'profile/' . $object->name);
      break;
  }

  //dsm($values);
  return $values;
}

Functions

Namesort descending Description
activity_log_rules_data_type_info Implementation of hook_rules_data_type_info()
user_activity_rules_action_info Implementation of hook_rules_action_info().
user_activity_rules_condition_info Implentation of hook_rules_conditions_info().
user_activity_rules_default_action Action Implementation: Log an activity
user_activity_rules_default_action_form Action drupal message configuration form.
user_activity_rules_default_action_submit Action drupal message configuration form.
user_activity_token_list
user_activity_token_values Implementation of hook_token_values().

Classes

Namesort descending Description
rules_data_type_heartbeat_message Rules heartbeat activity message data type