View source
<?php
class user_activity {
private $m_uid = 0;
private $m_uid_target = 0;
private $m_nid_target = 0;
private $m_hid = 0;
private $m_karma_index = 0;
private $m_description = '';
private $m_message = '';
private $m_message_concat = '';
private $m_variables_array = array();
private $m_variables_string = '';
public $event = '';
function __construct($data = null) {
if (isset($data)) {
$this
->set_data($data);
}
}
public function set_data($data) {
foreach ($data as $key => $value) {
if (isset($this->{$key})) {
$this->{$key} = $value;
}
if (isset($this->{'m_' . $key})) {
$this->{'m_' . $key} = $value;
}
}
if (isset($data['variables'])) {
$this->m_variables_string = $data['variables'];
}
if ($this->m_variables_array == array() && $this->m_variables_string != '') {
$this
->variables2array();
}
}
public function __get($variable) {
$var = null;
if (isset($this->{'m_' . $variable})) {
$var = $this->{'m_' . $variable};
}
else {
if (array_key_exists($variable, $this->m_variables_array)) {
$var = $this->m_variables_array[$variable];
}
}
return $var;
}
public function variables2array() {
$this->m_variables_array = heartbeat_decode_message_variables($this->m_variables_string);
return $this->m_variables_array;
}
public function save($raw_args = array()) {
if (module_exists('locale')) {
$this
->save_locale($raw_args);
}
else {
$this
->_save($raw_args);
}
}
private function save_locale($raw_args = array()) {
$args = $this
->rebuild_arguments($raw_args, true);
$locale = $args['locale'];
unset($args['locale']);
$languages = locale_language_list();
foreach ($languages as $language => $human_language) {
foreach ($this->m_variables_array as $key => $value) {
if (isset($locale[$key])) {
$amp_token = str_replace("#", "!", $key);
$args[$amp_token] = t($locale[$key], array(), $language);
}
}
$this
->log_message($args, $language);
}
}
private function _save($raw_args = array()) {
$args = $this
->rebuild_arguments($raw_args);
$this
->log_message($args);
}
private function log_message($args, $lang = '') {
if ($lang == '') {
global $language;
$lang = $language->language;
}
db_query("INSERT INTO user_activity SET uid=%d, uid_target=%d, nid_target=%d, hid=%d, language='%s', \n message='%s', timestamp=%d, event='%s', variables='%s'", $this->m_uid, $this->m_uid_target, $this->m_nid_target, $this->m_hid, $lang, t($this->m_message, $args, $lang), $_SERVER['REQUEST_TIME'], $this->event, $this->m_variables_string);
}
private function rebuild_arguments($raw_input, $locale = false) {
$args = array();
if ($locale) {
$args['locale'] = array();
}
foreach ($this->m_variables_array as $key => $value) {
if ($key[0] != "@" && $key[0] != "#") {
continue;
}
$oldkey = $key;
if ($key[0] == "@") {
$key[0] = "!";
}
if ($key[0] == "#") {
dsm($key . ' ' . $value);
if ($locale) {
$args['locale'][$key] = $value;
}
$key[0] = "!";
}
if (isset($raw_args[$oldkey])) {
$args[$key] = $raw_args[$oldkey];
continue;
}
$args[$key] = $value;
}
return $args;
}
}
function heartbeat_perm() {
return array(
'configure heartbeat',
'configure heartbeat messages',
'heartbeat view messages',
);
}
function heartbeat_menu() {
$items = array();
$items['admin/settings/heartbeat'] = array(
'title' => t('heartbeat settings'),
'description' => t('Administer settings for heartbeat.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'heartbeat_admin_settings',
),
'access arguments' => array(
'configure heartbeat',
),
'file' => 'heartbeat.admin.inc',
);
$items['admin/settings/heartbeat/settings'] = array(
'title' => t('Heartbeat settings'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/settings/heartbeat/messages'] = array(
'title' => t('Heartbeat messages'),
'description' => t('Administer messages for heartbeat.'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'page callback' => 'heartbeat_messages_overview',
'access arguments' => array(
'configure heartbeat messages',
),
'file' => 'heartbeat.admin.inc',
);
$items['admin/settings/heartbeat/messages/%'] = array(
'title' => t('Heartbeat message editer'),
'description' => t('Administer message for heartbeat.'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'heartbeat_messages_edit',
4,
),
'access arguments' => array(
'configure heartbeat messages',
),
'file' => 'heartbeat.admin.inc',
);
return $items;
}
function heartbeat_theme() {
return array(
'heartbeat_block' => array(
'arguments' => array(
'messages' => array(),
),
),
);
}
function heartbeat_block($op = 'list', $delta = 0) {
if (user_access('heartbeat view messages')) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Personal heartbeat');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
if (variable_get('heartbeat_enabled', 1)) {
global $user, $language;
$max_gap = variable_get('user_activity_grouping_seconds', 3600);
$end_time = $_SERVER['REQUEST_TIME'] - (int) $max_gap;
$messages = array();
$sql = "SELECT * FROM {user_activity} WHERE \n\t (user_activity.uid = %d) AND (user_activity.language = '%s') AND timestamp > %d ORDER BY timestamp DESC";
$result = db_query($sql, $user->uid, $language->language, $end_time);
while ($heartbeat = db_fetch_object($result)) {
$messages[] = $heartbeat;
}
$block['subject'] = t('Recent heartbeat');
$messages = heartbeat_group_messages($messages);
$block['content'] = theme('heartbeat_block', $messages);
return $block;
}
default:
}
}
}
function heartbeat_gather_messages() {
$info_default = module_invoke_all('heartbeat_message_info');
$info_cache = heartbeat_messages('all', false, false);
$info = array_diff($info_default, $info_cache);
$info = heartbeat_messages_install($info);
return $info;
}
function theme_heartbeat_block($messages) {
if (empty($messages)) {
return t('<p>No activity yet</p>');
}
$content = '';
foreach ($messages as $key => $message) {
$content .= '<span class="heartbeat-message-block ' . (($key + 1) % 2 ? 'odd' : 'even') . '">' . $message->message . '</span>';
}
return $content;
}
function heartbeat_group_messages($messages) {
return $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}");
}
else {
$result = db_query("SELECT * FROM {heartbeat_messages} WHERE module = '%s'", $module);
}
while ($row = db_fetch_array($result)) {
if ($objects) {
$messages[] = new user_activity($row);
}
else {
$messages[] = $row;
}
}
}
return $messages;
}
function heartbeat_event_message($event) {
$result = db_query("SELECT message from {heartbeat_messages} WHERE event = '%s' LIMIT 1", $event);
$message = db_fetch_object($result);
return $message->message;
}
function heartbeat_event_messages($event, $field) {
static $messages;
if (!isset($messages[$event])) {
if (count($messages) > 0) {
$result = db_query("SELECT event, message, message_concat, variables from {heartbeat_messages} WHERE event = '%s' LIMIT 1", $event);
}
else {
$result = db_query("SELECT event, message, message_concat, variables from {heartbeat_messages}");
}
while ($message = db_fetch_object($result)) {
$messages[$message->event] = $message;
}
}
return $messages[$event]->{$field};
}
function heartbeat_event_id($action) {
$result = db_query("SELECT hid from {heartbeat_messages} WHERE event = '%s' LIMIT 1", $action);
$res = db_fetch_object($result);
return $res->hid;
}
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);
}
db_query("INSERT INTO {heartbeat_messages} (message, message_concat, concat_args, event, karma_index, description, module, variables) \n VALUES ('%s', '%s','%s','%s',%d,'%s','%s','%s') ", $record->message, $record->message_concat, $record->concat_args, $record->event, $record->karma_index, $record->description, $record->module, $record->variables);
}
return $objects;
}
function heartbeat_messages_uninstall($module) {
db_query("DELETE FROM {heartbeat_messages} WHERE module = '%s'", $module);
}
function heartbeat_decode_message_variables($string, $object = false) {
$array = explode("-|-", $string);
$variables = array();
if (!empty($array)) {
foreach ($array as $varvalue) {
$parts = explode("=|=", $varvalue);
if (isset($parts[0]) && !empty($parts[0])) {
$variables[$parts[0]] = (string) $parts[1];
}
}
}
return $object ? (object) $variables : (array) $variables;
}
function heartbeat_encode_message_variables($array) {
$string = '';
foreach ($array as $key => $value) {
$string .= $key . '=|=' . $value . '-|-';
}
return $string;
}