You are here

function activity_record in Activity 6.2

Same name and namespace in other branches
  1. 7 activity.module \activity_record()

Implementation of a configurable Drupal action. Tokenize and record an activity message.

Parameters

$object:

$context: holds the messages

1 call to activity_record()
activity_batch_regenerate_step in ./activity.admin.inc
Batch regeneration step.
2 string references to 'activity_record'
ActivityWebTestCase::testActivityActionCreate in tests/activity.test
Test the creation of a new configurable activity action.
activity_form_submit in ./activity.admin.inc
Form submit controller.

File

./activity.module, line 578
Primarily Drupal hooks and global API functions to manipulate activity.

Code

function activity_record($object, $context, $a1 = NULL, $a2 = NULL) {
  $context += array(
    'actor' => $GLOBALS['user']->uid,
    'created' => time(),
    'argument1' => $a1,
    'argument2' => $a2,
  );

  // find what the type is so we can do the tokenizing
  $activity_object = FALSE;
  foreach (activity_get_module_info() as $module => $info) {
    if (in_array($context['hook'], array_keys($info->hooks))) {

      // we found the activity we wanted
      $activity_object = $info;
      break;
    }
  }
  if (!$activity_object) {

    // not a valid trigger
    return;
  }
  $type = $activity_object->object_type;

  // bring in all the types so that we can replace_multiple
  foreach ($activity_object->objects as $object_key) {
    if (!empty($context[$object_key])) {
      $objects[$object_key] = drupal_clone($context[$object_key]);
    }
  }

  // types is the array for the token replace
  // it uses key 'user' not 'account'
  if (isset($objects['account'])) {
    $objects['user'] = $objects['account'];
  }
  if (!isset($objects[$type]) && isset($context[$type])) {
    $objects[$type] = drupal_clone($context[$type]);
  }
  if (!empty($context['actor']) && $context['actor'] != $objects[$type]->uid) {
    $objects[$type]->uid = $context['actor'];
  }
  if (empty($objects['user'])) {
    $objects['user'] = user_load($context['actor']);
  }
  drupal_alter('activity_objects', $objects, $type);

  // check make sure we should record this activity
  // activity trigger modules can implement the hook_activity_type_check
  // to tell activity which triggers it should ignore
  if (!activity_record_check($object, $context, $objects, $activity_object)) {
    return;
  }

  // grab the targeted params
  foreach (activity_enabled_languages() as $id => $language) {
    foreach ($activity_object->objects as $object_name) {
      $patterns[$id][$object_name] = $context["{$object_name}-pattern-{$id}"];
    }
  }

  // $messages is keyed by language -> uid
  $messages = array();

  // Flush the token cache as the old tokens are no longer relevant
  // given the new event.
  token_get_values('global', NULL, TRUE);

  // foreach pattern in the context, token_replace_multiple() with types
  foreach ($patterns as $language_id => $language_patterns) {
    foreach ($language_patterns as $target_key => $message) {
      if (!empty($objects[$target_key])) {
        $message = token_replace_multiple($message, $objects, '[', ']', array(), TRUE);
        if (!empty($message)) {
          $messages[$language_id][intval($objects[$target_key]->uid)] = $message;
        }
      }
    }

    // and now do the everyone else pattern
    $everyone_message = token_replace_multiple($context['everyone-pattern-' . $language_id], $objects, '[', ']', array(), TRUE);
    if (!empty($everyone_message)) {
      $messages[$language_id][0] = $everyone_message;
    }
  }

  // create a record
  $nid = NULL;

  // Anon user is blocked by default and cannot be unblocked. i.e their status
  // can never and will never change, always being 0. Thus, anon
  // gets a pass through.
  if ($context['actor'] != 0) {
    $published = db_result(db_query("SELECT status FROM {users} WHERE uid = %d", $context['actor'])) == 1;
  }
  else {
    $published = TRUE;
  }
  if (isset($context['node']->nid)) {
    $nid = $context['node']->nid;

    // if the trigger op = 'presave' and you're creating a new node, $nid = null
    // because the record hasn't yet been saved to the node table
    $published = $published && $context['node']->status == 1;
  }

  // NOTICE: no elseif(). This is because if the comment is part of the context
  // we want that nid as its the lowest level player. Doubt that will ever be an issue
  if (isset($context['comment']->nid)) {
    $nid = $context['comment']->nid;
    $published = $published && $context['comment']->status == COMMENT_PUBLISHED;
  }
  $record = new stdClass();
  $record->uid = $context['actor'];
  $record->op = $context['op'];
  $record->type = $activity_object->name;
  $record->nid = $nid;
  $record->created = $context['created'];
  $record->actions_id = $context['aid'];
  $record->status = $published ? 1 : 0;

  // handle the entity id
  if (!empty($info->eid_field) && isset($context[$type]->{$info->eid_field})) {
    $record->eid = $context[$type]->{$info->eid_field};
  }
  else {
    $record->eid = NULL;
  }

  // allow other modules to manipulate the record before insertion
  drupal_alter('activity_record', $record, $context);
  drupal_write_record('activity', $record);

  // allow other modules to change the messages based on the activity type and
  // objects.
  drupal_alter('activity_messages', $messages, $activity_object->name, $objects);

  // write the messages here
  foreach ($messages as $language_id => $language_messages) {
    foreach ($language_messages as $uid => $message) {

      // write the message away first so we have the amid
      $message_record = new stdClass();
      $message_record->message = $message;
      drupal_write_record('activity_messages', $message_record);

      // now save the target with the amid from above ^^
      $target_record = new stdClass();
      $target_record->aid = $record->aid;
      $target_record->uid = $uid;
      $target_record->language = $language_id;
      $target_record->amid = $message_record->amid;
      drupal_write_record('activity_targets', $target_record);
    }
  }

  // after this is recorded, another module might want the aid
  module_invoke_all('activity_message_recorded', $record, $context);
  $grants = activity_get_grants($record);
  foreach ($grants as $realm => $values) {
    foreach ($values as $value) {

      // insert one by one. In D7 we can use the DBTNG to insert multiple
      $perm = new stdClass();
      $perm->aid = $record->aid;
      $perm->realm = $realm;
      $perm->value = $value;
      drupal_write_record('activity_access', $perm);
    }
  }
}