You are here

heartbeat.module in Heartbeat 6.3

File

heartbeat.module
View source
<?php

// by Stalski (Jochen Stals) - ONE-agency - www.one-agency.be

/**
 * @file
 *
 * Heartbeat is a module that logs activity for each language to its own table.
 * The second part is the display and there it provides blocks for each heartbeataccess
 * type that is defined.
 *
 * The logging is a call to a log function with parameters that can be build in several
 * ways. The rules module can trigger an event based action that is delegated to the
 * heartbeat logger. This is nice because you can use tokens to replace heartbeat message
 * variables. This way you can customize your message and keep its parts very variable.
 * This logger is also available as an api method to use from your own module.
 *   heartbeat_api_log($message_id, $uid, $uid_target=0, $nid=0, $variables=array())
 *
 * To fully be creative with heartbeat, you have to be digg in the rules ui (and code).
 * Lots of documentation can be found on http://drupal.org/node/298480 for
 * an introduction and tutorial, but http://drupal.org/node/298486 is a lot
 * of handy info for developers.
 *
 * HOOKS
 *
 * hook_heartbeat_message_info
 *   describes heartbeat message exports
 * hook_heartbeat_register_access_types
 *   registers heartbeat access states to define the scope and build the correct sql.
 * hook_heartbeat_messages_alter
 *   Alter messages before they are displayed
 *
 * @developers
 * You can define your own HeartbeatAccess class and register it with the hook
 * heartbeat_register_access_types(). Heartbeat itself, ofcourse implements it.
 * The hook is only called at the heartbeat message overview page, where messages
 * are imported as well from the other hook heartbeat_message_info()
 *
 */
include 'heartbeat.common.inc';

/**
 * Autoloader function for heartbeat classes
 *
 * @param String $class
 */
spl_autoload_extensions('.inc');
function heartbeat_class_loader($class) {
  heartbeat_include(strtolower($class));
}
spl_autoload_register('heartbeat_class_loader');

/**
 * Message access types
 *
 * What people can see and are entitled to see. This permission
 * on messages can be set as default per HeartbeatAccess type but
 * can be overriden in the configuration of a heartbeat message.
 */

// Display only activity messages that are mine or addressed to me
define('HEARTBEAT_PRIVATE', 0);

// Everyone can see this activity message, unless this type of message is set to private
define('HEARTBEAT_PUBLIC_TO_ALL', 1);

// Display activity message of all my user relations, described in contributed modules:
// friendlist, og, ...
define('HEARTBEAT_PUBLIC_TO_CONNECTED', 2);

// Only the person that is chosen by the actor, can see the message
define('HEARTBEAT_PUBLIC_TO_ADDRESSEE', 3);

/**
 * Implementation of hook_init().
 */
function heartbeat_init() {
  global $user;
  if (!isset($user->heartbeat_relations)) {
    $user->heartbeat_relations = array();
  }
}

/**
 * Implementation of hook_perm().
 */
function heartbeat_perm() {
  return array(
    'configure heartbeat',
    'configure heartbeat messages',
    'delete heartbeat activity logs',
    'view heartbeat messages',
  );
}

/**
 *  Implementation of hook_menu().
 */
function heartbeat_menu() {
  $items = array();
  $items['heartbeat/public'] = array(
    'title' => 'public heartbeat page',
    'description' => 'Public heartbeat page',
    'page callback' => 'heartbeat_site_messages',
    'access arguments' => array(
      'view heartbeat messages',
    ),
  );
  $items['heartbeat/activity/personal'] = array(
    'title' => 'Activity',
    'description' => 'All activity from you and your connections',
    'page callback' => 'heartbeat_connected_messages',
    'access arguments' => array(
      'view heartbeat messages',
    ),
  );

  // Administer settings
  $items['admin/settings/heartbeat'] = array(
    'title' => 'heartbeat settings',
    'description' => 'Administer settings for heartbeat.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'heartbeat_admin_settings',
    ),
    'access arguments' => array(
      'configure heartbeat',
    ),
    'file' => 'heartbeat.admin.inc',
    'weight' => -5,
  );

  // Tabs
  $items['admin/settings/heartbeat/settings'] = array(
    'title' => 'Heartbeat settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/settings/heartbeat/delete'] = array(
    'title' => 'Delete activity logs',
    'description' => 'Delete heartbeat activity logs.',
    'page arguments' => array(
      'heartbeat_delete_logs_confirm',
    ),
    'access arguments' => array(
      'delete heartbeat activity logs',
    ),
    'file' => 'heartbeat.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
  );

  // Build menu
  $items['admin/build/heartbeat'] = array(
    'title' => 'Heartbeat messages',
    'description' => 'Administer messages for heartbeat.',
    'weight' => -5,
    'page callback' => 'heartbeat_messages_overview',
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'file' => 'heartbeat.admin.inc',
  );
  $items['admin/build/heartbeat/list'] = array(
    'title' => 'List',
    'description' => 'Overview messages',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/build/heartbeat/add'] = array(
    'title' => 'Add heartbeat message',
    'description' => 'Administer message for heartbeat.',
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'heartbeat_messages_add',
    ),
    'access arguments' => array(
      'add heartbeat message',
    ),
    'file' => 'heartbeat.admin.inc',
  );
  $items['admin/build/heartbeat/export'] = array(
    'title' => 'Export',
    'description' => 'Export messages to use as default.',
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'heartbeat_messages_export',
    ),
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'file' => 'heartbeat.admin.inc',
  );
  $items['admin/build/heartbeat/edit/%'] = array(
    'title' => 'Edit heartbeat message',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'heartbeat_messages_edit',
      4,
    ),
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'file' => 'heartbeat.admin.inc',
  );
  $items['admin/build/heartbeat/delete/%'] = array(
    'title' => 'Delete heartbeat message',
    'description' => 'Administer deletions of messages.',
    'type' => MENU_CALLBACK,
    'page callback' => 'heartbeat_messages_delete',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'file' => 'heartbeat.admin.inc',
  );

  // Callbacks
  $items['heartbeat/heartbeat_activity_rules_default/js'] = array(
    'title' => 'Callback to supply extra action variables as arguments',
    'type' => MENU_CALLBACK,
    'page callback' => 'heartbeat_activity_rules_action_message_id_js',
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'file' => 'heartbeat.admin.inc',
  );
  $items['heartbeat/message_types/js'] = array(
    'title' => 'Callback to supply textarea changes for messages',
    'page callback' => 'heartbeat_message_type_js',
    'access arguments' => array(
      'configure heartbeat messages',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'heartbeat.admin.inc',
  );
  $items['admin/settings/heartbeat/heartbeat_activity'] = array(
    'title' => 'user activity settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'heartbeat_logs_admin_settings',
    ),
    'access arguments' => array(
      'configure user activity',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 6,
    'file' => 'heartbeat.admin.inc',
  );
  $items['heartbeat/ahah/%'] = array(
    'page callback' => 'heartbeat_activity_ahah',
    'page arguments' => array(
      2,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'configure user activity',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'heartbeat.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function heartbeat_theme() {
  return array(
    'heartbeat_list' => array(
      'arguments' => array(
        'messages' => array(),
      ),
    ),
  );
}

/**
 * Implementation of hook_blocks()
 *
 */
function heartbeat_block($op = 'list', $delta = 0, $edit = array()) {
  if (!user_access('view heartbeat messages') || !variable_get('heartbeat_enabled', 1)) {
    return FALSE;
  }
  $access_types = variable_get('heartbeat_access_types', array());
  if ($op == 'list') {

    // A block foreach access type
    foreach ($access_types as $key => $access_type) {
      $blocks[$key]['info'] = ucfirst($access_type['name']);
      $blocks[$key]['cache'] = BLOCK_CACHE_PER_USER;
    }
    return $blocks;
  }
  else {
    if ($op == 'view') {

      // Message streams for each access type
      if (isset($access_types[$delta])) {
        if ($access_types[$delta]['module'] != 'heartbeat') {
          heartbeat_include($access_types[$delta]['class'], $access_types[$delta]['module']);
        }
        $accesstype = $access_types[$delta]['class'];
        $context = new HeartbeatMessageBuilder(new $accesstype());
        if (!$context
          ->hasErrors()) {
          $messages = $context
            ->execute();
          $block['subject'] = t($access_types[$delta]['name']);
          $block['content'] = theme('heartbeat_list', $messages);
          return $block;
        }
      }
    }
    else {
      if ($op == 'configure') {
        $class = strtolower($access_types[$delta]['class']);
        $form['items'] = array(
          '#type' => 'checkbox',
          '#title' => t('Show activity for the displayed user on the user profile page'),
          '#description' => t('By default heartbeat will show activity in relation to the
        currently logged in user.  With this setting enabled and only on the user profile page,
        the messages will be shown in relation to the user profile.'),
          '#default_value' => variable_get('heartbeat_show_user_profile_messages_' . $class, 0),
        );
        return $form;
      }
      else {
        if ($op == 'save') {
          $class = strtolower($access_types[$delta]['class']);
          variable_set('heartbeat_show_user_profile_messages_' . $class, $edit['heartbeat_show_user_profile_messages_' . $class]);
        }
      }
    }
  }
}

/**
 * Theme function for a list of heartbeat activity messages
 */
function theme_heartbeat_list($messages) {
  return _heartbeat_build_block($messages);
}

/**
 * Implementation of hook_heartbeat_message_info
 */
function heartbeat_heartbeat_message_info() {
  $info = array(
    /**
     * Default node messages
     */
    2 => array(
      'message' => '!username has updated !node_type "!node_title"',
      'message_concat' => '!username has updated %node_title%',
      'message_id' => 'heartbeat_edit_node',
      'message_type' => 'normal',
      'concat_args' => array(
        'type' => 'summary',
        'group_by' => 'user',
        'group_target' => 'node_title',
        'merge_separator' => ', ',
        'merge_end_separator' => ' and ',
        'perms' => '0',
      ),
      'module' => 'node',
      'description' => 'When editing a node, log the users activity',
      'variables' => array(
        '@username' => '[node:author-name-url]',
        '@node_type' => '[node:type]',
        '@node_title' => '[node:title-link]',
      ),
    ),
    3 => array(
      'message_id' => 'heartbeat_add_node',
      'message_type' => 'normal',
      'message' => '!username has added !node_type !node_title.',
      'message_concat' => '!username has added the following !nodetypes: %node_title%.',
      'concat_args' => array(
        'type' => 'summary',
        'group_by' => 'user',
        'merge_target' => 'node_title',
        'merge_separator' => ',',
        'merge_end_separator' => 'and ',
        'perms' => '0',
      ),
      'module' => 'node',
      'description' => 'User adds a node, save user activity',
      'variables' => array(
        '@username' => '[user:user-name-url]',
        '@node_type' => '[node:type]',
        '@node_title' => '[node:title-link]',
        '@nodetypes' => '[node:type]s',
      ),
    ),
    /**
     * Default comment messages
     */
    4 => array(
      'message_id' => 'heartbeat_add_comment',
      'message_type' => 'normal',
      'message' => '!username replied on !title.',
      'message_concat' => '!username replied on !title.',
      'concat_args' => array(
        'type' => 'count',
        'merge_target' => 'times',
        'merge_separator' => '',
        'merge_end_separator' => '',
        'perms' => '0',
      ),
      'module' => 'comment',
      'description' => 'user replied on some content',
      'variables' => array(
        '@username' => '[user:user-name-url]  ',
        '@title' => '[node:title-link]',
      ),
    ),
    5 => array(
      'message_id' => 'heartbeat_edit_comment',
      'message_type' => 'normal',
      'message' => '!username changed his comment on !title.',
      'message_concat' => '!username changed his comment on !title several times (%times%).',
      'concat_args' => array(
        'type' => 'count',
        'merge_target' => 'times',
        'merge_separator' => '',
        'merge_end_separator' => '',
        'perms' => '0',
      ),
      'module' => 'comment',
      'description' => 'user changed his comment',
      'variables' => array(
        '@username' => '[user:user-name-url]',
        '@title' => '[node:title-link]',
      ),
    ),
    6 => array(
      'message_id' => 'heartbeat_edit_account',
      'message_type' => 'normal',
      'message' => '!username\'s personal account page has been changed.',
      'message_concat' => '',
      'concat_args' => array(
        'type' => 'single',
        'merge_target' => '',
        'merge_separator' => '',
        'merge_end_separator' => '',
        'perms' => '0',
      ),
      'module' => 'user',
      'description' => 'user changed his/her account',
      'variables' => array(
        '@username' => '[user:user-name-url]',
      ),
    ),
  );
  return $info;
}

/**
 * Implementation of hook_heartbeat_register_access_types()
 */
function heartbeat_heartbeat_register_access_types() {
  return array(
    0 => array(
      'name' => 'Personal Heartbeat',
      'class' => 'PrivateHeartbeat',
      'path' => 'includes/privateheartbeat.inc',
      'module' => 'heartbeat',
    ),
    1 => array(
      'name' => 'Public Heartbeat',
      'class' => 'PublicHeartbeat',
      'path' => 'includes/publicheartbeat.inc',
      'module' => 'heartbeat',
    ),
    2 => array(
      'name' => 'Heartbeat activity for user relations',
      'class' => 'ConnectedHeartbeat',
      'path' => 'includes/connectedheartbeat.inc',
      'module' => 'heartbeat',
    ),
  );
}

/**
 * API function to log a message from custom code
 *
 * @param string $message_id
 *   Id of the message that is known in the message
 * @param integer $uid
 *   Actor or user performing the activity
 * @param integer $uid_target [optional]
 *   user id of the target user if present. Target users can be an addresse or a
 *   user relation transaction with the actor $uid
 * @param integer $nid [optional]
 *   Node id if the activity is content related
 * @param array $variables [optional]
 *   Variables can be used if you used them in the used message. Take care to use
 *   the @-sign for words that are prefix with the question mark sign in the messages
 */
function heartbeat_api_log($message_id, $uid, $uid_target = 0, $nid = 0, $variables = array()) {
  $data = array();

  // Normal form values
  $data['message_id'] = $message_id;
  $data['uid'] = $uid;
  $data['uid_target'] = $uid_target;
  $data['nid_target'] = $nid;

  // Relational message of heartbeat messages
  $message = heartbeat_load_message($data['message_id']);
  $data['message'] = $message->message;
  $data['message_concat'] = $message->message_concat;
  if (!empty($variables) && is_array($variables)) {
    $data['variables'] = heartbeat_encode_message_variables($variables);
  }
  return heartbeat_log($data);
}

/**
 * Implementation of hook_token_list().
 */
function heartbeat_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'boolean') {
    $tokens['boolean']['1-or-0'] = t("1 for true, 0 for false");
  }
  if ($type == 'rules_data_type_heartbeat_message_id') {
    $tokens['heartbeat_message']['message-id-raw'] = t("The message chosen by the user in raw");
    $tokens['heartbeat_message']['message-id'] = t("The message chosen by the user");
  }
  if ($type == 'rules_data_type_heartbeat_access') {
    $tokens['heartbeat_message']['message-access'] = t("The message access chosen by user");
  }
  if ($type == 'heartbeat_message') {
    $tokens['heartbeat']['heartbeat-message-raw'] = t("The logged heartbeat message in raw format");
  }
  if ($type == 'node') {
    $tokens['node']['title-link'] = t("The node's title with a link to it");
    $tokens['node']['author-name-url'] = t("The users name with a link to it");
  }
  if ($type == 'user') {
    $tokens['user']['user-name-url'] = t("The currently logged-in username with a link to it");
    $tokens['user']['user-profile-url'] = t("The currently logged-in username 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 heartbeat_token_values($type, $object = NULL, $options = array()) {

  // dsm('heartbeat_activity_token_values: type: '.$type);
  // dsm($object);
  $values = array();
  switch ($type) {
    case 'boolean':
      $values['1-or-0'] = $object ? '1' : '0';
      break;
    case 'rules_data_type_heartbeat_message_id':
      $values['message-id'] = $object;
      $values['message-id-raw'] = (int) $object;
      break;
    case 'rules_data_type_heartbeat_access':
      $values['message-access'] = (int) $object;
      break;
    case 'heartbeat_message':
      $values['heartbeat-message-raw'] = check_plain($object->message);
      break;
    case 'node':
      $values['title-link'] = l($object->title, 'node/' . $object->nid);
      $values['author-name-url'] = l($object->name, 'user/' . $object->uid, array(
        'attributes' => array(
          'class' => 'user',
        ),
      ));
      break;
    case 'user':
      $values['user-name-url'] = l($object->name, 'user/' . $object->uid, array(
        'attributes' => array(
          'class' => 'user',
        ),
      ));
      $values['user-profile-url'] = l($object->name, 'profile/' . $object->name, array(
        'attributes' => array(
          'class' => 'user',
        ),
      ));
      break;
    case 'global':
      break;
  }

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

/**
 * Check if there no new heartbeat access types available
 */
function heartbeat_check_access_types() {
  $types = module_invoke_all('heartbeat_register_access_types');
  if (!empty($types)) {
    foreach ($types as $key => $type) {
      require_once drupal_get_path('module', $type['module']) . '/' . $type['path'];
      if (!class_exists($type['class'])) {
        watchdog('heartbeat', t('No class found for') . $type['class'], array(), WATCHDOG_ERROR);
        unset($types[$key]);
      }
    }
  }
  variable_set('heartbeat_access_types', $types);
}

/**
 * Function that gathers all messages from all modules
 * New ones and existing ones
 *
 */
function heartbeat_gather_messages() {
  $info_default = module_invoke_all('heartbeat_message_info');
  $defaults_lookup = array();
  foreach ($info_default as $default) {
    $defaults_lookup[$default['message_id']] = TRUE;
  }
  $info_cache = heartbeat_messages('all', false, false);

  // dsm($info_default);dsm($info_cache);dsm($info);
  foreach ($info_cache as $key => $cached) {
    if (!isset($defaults_lookup[$cached['message_id']]) && !$cached['custom']) {
      heartbeat_messages_uninstall($cached['message_id'], 'message');
      unset($info_cache[$key]);
    }
  }
  $info = _heartbeat_array_udiff($info_default, $info_cache);
  $info = heartbeat_messages_install($info);
  return $info;
}

/**
 * User activity logger function
 * @param   The data to add one row
 */
function heartbeat_log($data, $args = array()) {
  $heartbeat_activity = new HeartbeatActivity($data);
  return $heartbeat_activity
    ->save($args);
}

/**
 * function to show private/connected heartbeats
 */
function heartbeat_connected_messages() {
  $context = new HeartbeatMessageBuilder(new ConnectedHeartbeat());
  $messages = $context
    ->execute();
  return theme('heartbeat_block_private', $messages);
}

/**
 * function to show public heartbeats
 */
function heartbeat_site_messages() {
  $context = new HeartbeatMessageBuilder(new PublicHeartbeat());
  $messages = $context
    ->execute();
  return theme('heartbeat_block_private', $messages);
}

/**
 * Include heartbeat .inc class files
 */
function heartbeat_include($file, $module = 'heartbeat') {
  static $loaded = array();
  $file = strtolower($file);
  $path = drupal_get_path('module', $module) . '/includes/' . $file . '.inc';
  if (!isset($loaded[$file]) && is_file($path)) {
    require $path;
    $loaded[$file] = TRUE;
  }
  return $loaded[$file];
}

/**
 * Function to install default records
 *
 * @param string $module to conventionally look
 *        for defined record objects in heartbeat_messages
 */
function heartbeat_messages_install($objects) {
  foreach ($objects as $record) {
    $record = (object) $record;
    if (isset($record->concat_args) && is_array($record->concat_args)) {
      $record->concat_args = heartbeat_encode_message_variables($record->concat_args);
    }
    if (isset($record->variables) && is_array($record->variables)) {
      $record->variables = heartbeat_encode_message_variables($record->variables);
    }

    //watchdog('heartbeat', $module.' is writing a message to heartbeat_messages.', ((array)$record) );

    // drupal_write record does not work when installing all modules together
    // reason is the schema static cannot rebuilt without messing in the core.
    // drupal_write_record('heartbeat_messages', $record);
    db_query("INSERT INTO {heartbeat_messages} (\n    message_id, message_type, message, message_concat, concat_args, perms, description, module, variables)\n    VALUES ('%s', '%s', '%s', '%s','%s', %d, '%s', '%s', '%s') ", $record->message_id, $record->message_type, $record->message, $record->message_concat, $record->concat_args, $record->perms, $record->description, $record->module, $record->variables);
  }
  return $objects;
}

/**
 * Function to uninstall default records
 */
function heartbeat_messages_uninstall($id, $type = 'module') {
  if ($type == 'module') {
    db_query("DELETE FROM {heartbeat_messages} WHERE module = '%s'", $id);
  }
  if ($type == 'message') {
    db_query("DELETE FROM {heartbeat_messages} WHERE message_id = '%s'", $id);
  }
}

/**
 * Helper function that wraps the template functions
 */
function _heartbeat_build_block($messages) {
  if (empty($messages)) {
    return '<p>' . t('<em>No activity yet.</em>') . '</p>';
  }
  $content = '';

  // Add messages in one of the formats
  $content .= _heartbeat_messages_format_interval($messages);

  // TODO Add footer content with other site information
  if (isset($messages->info)) {
    $content .= _heartbeat_activity_get_info();
  }
  return $content;
}

/**
 * Function calculates all related uids
 * The result depends highly on the settings for
 * each private relation
 */
function heartbeat_get_related_uids($uid) {
  $uids = $related_uids = array();
  $uids[$uid] = $uid;

  // all the messages where the current uid is in the friendlist
  // if function exists use it
  $related_uids = module_invoke_all('heartbeat_related_uid_info', $uid);
  if (count($related_uids) > 0) {
    foreach ($related_uids as $rel_uid) {
      $uids[$rel_uid] = $rel_uid;
    }
  }
  return $uids;

  // array_unique($uids);
}

/**
 * get all messages with static cache variable
 *  and reset possibility
 *
 * @param string $module
 * @param boolean $reset
 * @param boolean $objects
 * @return array messages
 */
function heartbeat_messages($module = 'all', $reset = false, $objects = true) {
  static $messages;
  if (empty($messages) || $reset == true) {
    $messages = array();
    if ($module == 'all') {
      $result = db_query("SELECT * FROM {heartbeat_messages} ORDER BY module, description ");
    }
    else {
      $result = db_query("SELECT * FROM {heartbeat_messages} WHERE module = '%s' ", $module);
    }
    while ($row = db_fetch_array($result)) {
      if ($objects) {
        $messages[] = new HeartbeatActivity($row);
      }
      else {
        $messages[] = $row;
      }
    }
  }
  return $messages;
}

/**
 * Get heartbeat messages of the the given
 */
function heartbeat_get_messages($type = 'all', $reset = false) {
  static $messages;
  if ($type == 'all') {
    $messages = heartbeat_messages('all', $reset, true);
    $reset = false;
  }
  if ($type != 'all' && (empty($messages) || $reset)) {
    $messages = array();
    $result = db_query("SELECT * FROM {heartbeat_messages} WHERE message_type = '%s'", $type);
    while ($row = db_fetch_array($result)) {
      $messages[] = new HeartbeatActivity($row);
    }
  }
  return $messages;
}

/**
 * Function to get the message types
 */
function heartbeat_get_message_types() {
  $types = variable_get('message_types', array(
    'normal',
  ));
  return drupal_map_assoc($types);
}

/**
 * Fetches the translatable message for corresponding action
 *
 * @param string $hid
 */
function heartbeat_load_message($hid) {
  if (!is_numeric($hid)) {
    $where = " message_id = '%s' ";
    $hid = (string) $hid;
  }
  else {
    $where = " hid = %d ";
  }
  $result = db_query("SELECT * from {heartbeat_messages} WHERE " . $where . " LIMIT 1", $hid);
  $message = db_fetch_object($result);
  return $message;
}

/**
 * Fetches the translatable message for corresponding action
 *
 * @param string $hid
 */
function heartbeat_load_message_instance($uaid) {
  return db_fetch_object(db_query("SELECT * from {heartbeat_activity} WHERE  uaid = %d  LIMIT 1", $uaid));
}

/**
 * Helper function to get the uid of the private heartbeat request
 */
function _heartbeat_who_is_private_user($op) {

  // Check the requested user
  switch ($op) {
    case 'current':
      global $user;
      $uid = $user->uid;
      break;
    case 'argument':
      $url_uid = args(1);
      $uid = is_numeric($url_uid) ? $url_uid : 0;
      break;
    case 'all':
      $uid = 0;
      break;
    default:
      if (is_numeric($op)) {
        $uid = $op;
      }
  }
  return $uid;
}

/**
 * Helper function to get the options for access types
 *
 * @return array of access types
 */
function _heartbeat_access_type_options() {
  return array(
    HEARTBEAT_PRIVATE => t('Only the activity performed by the logged-in user'),
    HEARTBEAT_PUBLIC_TO_ALL => t('Public to everyone'),
    HEARTBEAT_PUBLIC_TO_ADDRESSEE => t('Public to the actor and the addressee. For specific user-user activity'),
    HEARTBEAT_PUBLIC_TO_CONNECTED => t('friendlist, og, ... connected or related'),
  );
}

/**
 * Helper function to get the extra site activity information
 */
function _heartbeat_activity_get_info() {
  $sitename = ucfirst(variable_get('site_name', 'this site'));
  $num_users = db_result(db_query("SELECT COUNT(uid) AS 'users' FROM {users} WHERE status= 1"));
  $content .= '<div class="heartbeat_info_text">';
  $content .= t('!sitename counts !num_users users', array(
    '!sitename' => $sitename,
    '!num_users' => $num_users,
  ));
  $content .= '</div>';
  return $content;
}

Functions

Namesort descending Description
heartbeat_api_log API function to log a message from custom code
heartbeat_block Implementation of hook_blocks()
heartbeat_check_access_types Check if there no new heartbeat access types available
heartbeat_class_loader
heartbeat_connected_messages function to show private/connected heartbeats
heartbeat_gather_messages Function that gathers all messages from all modules New ones and existing ones
heartbeat_get_messages Get heartbeat messages of the the given
heartbeat_get_message_types Function to get the message types
heartbeat_get_related_uids Function calculates all related uids The result depends highly on the settings for each private relation
heartbeat_heartbeat_message_info Implementation of hook_heartbeat_message_info
heartbeat_heartbeat_register_access_types Implementation of hook_heartbeat_register_access_types()
heartbeat_include Include heartbeat .inc class files
heartbeat_init Implementation of hook_init().
heartbeat_load_message Fetches the translatable message for corresponding action
heartbeat_load_message_instance Fetches the translatable message for corresponding action
heartbeat_log User activity logger function
heartbeat_menu Implementation of hook_menu().
heartbeat_messages get all messages with static cache variable and reset possibility
heartbeat_messages_install Function to install default records
heartbeat_messages_uninstall Function to uninstall default records
heartbeat_perm Implementation of hook_perm().
heartbeat_site_messages function to show public heartbeats
heartbeat_theme Implementation of hook_theme().
heartbeat_token_list Implementation of hook_token_list().
heartbeat_token_values Implementation of hook_token_values().
theme_heartbeat_list Theme function for a list of heartbeat activity messages
_heartbeat_access_type_options Helper function to get the options for access types
_heartbeat_activity_get_info Helper function to get the extra site activity information
_heartbeat_build_block Helper function that wraps the template functions
_heartbeat_who_is_private_user Helper function to get the uid of the private heartbeat request

Constants