You are here

ogactivity.module in Activity 6

Same filename and directory in other branches
  1. 5.4 contrib/ogactivity/ogactivity.module

File

contrib/ogactivity/ogactivity.module
View source
<?php

/**
 * Activity definition file
 *
 * This defines what hooks activity module should use
 */
function ogactivity_activity_info() {
  return array(
    'ops' => array(
      'join' => t('Join'),
      'leave' => t('Leave'),
    ),
    'types' => array(
      'ogaction' => t('Group Action'),
    ),
    'roles' => array(
      'author' => array(
        '#name' => t('Author'),
        '#description' => t('The person who joined or left a group.'),
        '#default' => array(
          'join' => '[author] joined the [node-link] group',
          'leave' => '[author] left the [node-link] group',
        ),
      ),
      // This is what corresponds to ACTIVITY_ALL
      'all' => array(
        '#name' => t('All'),
        '#description' => t('The general public.'),
        '#default' => array(
          'join' => '[author-all] joined the [node-link] group',
          'leave' => '[author-all] left the [node-link] group',
        ),
      ),
    ),
  );
}

/** 
 * Implementation of hook_activityapi().
 */
function ogactivity_activityapi(&$activity, $op) {
  if ($op == 'load') {
    if ($activity['data']['module'] == 'ogactivity' && !node_access('view', node_load($activity['data']['node-id']))) {
      $activity = array();
    }
  }
}

/**
 * Token module integration. Defines available default tokens.
 */
function ogactivity_token_list($type = 'all') {
  if ($type == 'ogactivity') {
    $tokens['ogactivity'] = array(
      'node-id' => t('Id of the node of the group that was joined or left'),
      'node-title' => t('Title of the group that was joined or left'),
      'node-link' => t('Link to the group that was joined or left'),
      'node-type' => t('The node type of the group that was joined or left'),
    );
    return $tokens;
  }
}

/**
 * Token module integration. Defines available default token values.
 */
function ogactivity_token_values($type, $data = NULL, $options = array()) {
  if ($type == 'ogactivity' && !empty($data)) {
    $data['node-type'] = theme('activity_node_type', $data['node-type']);
    $data['node-link'] = l($data['node-title'], 'node/' . $data['node-id']);
    return $data;
  }
}

/**
 * Implementation of hook_og().
 */
function ogactivity_og($op, $gid, $uid, $args) {
  switch ($op) {
    case 'user insert':
    case 'user delete':

      // If the group join action requires approval then the is_active
      // arg will be 0 so in this case we bail out.
      if (array_key_exists('is_active', $args) && $args['is_active'] == 0) {
        return FALSE;
      }
      $node = node_load($gid);
      $type = 'ogaction';
      $op = $op == 'user delete' ? 'leave' : 'join';

      // Check if both type and operation are
      // enabled for activity. If not then stop here
      if (!in_array($type, variable_get('ogactivity_token_types', array(
        $type,
      )), TRUE) || !in_array($op, variable_get('ogactivity_op_types', array(
        $op,
      )), TRUE)) {
        return FALSE;
      }

      // Privacy setting check
      $user = user_load(array(
        'uid' => $uid,
      ));
      if (activity_user_privacy_optout($user)) {
        return FALSE;
      }

      // User hide activity permission check
      if (user_access('hide activity', $user)) {
        return FALSE;
      }
      $data = array(
        'operation' => $op,
        'node-id' => $node->nid,
        'node-title' => $node->title,
        'node-type' => $node->type,
      );
      $target_users_roles = array(
        ACTIVITY_ALL => 'all',
        $user->uid => 'author',
      );
      activity_insert($user->uid, 'ogactivity', $type, $op, $data, $target_users_roles);
      break;
  }
}

/**
 * Implementation of hook_menu().
 */
function ogactivity_menu() {
  $items = array();
  $items['activity/og'] = array(
    'title' => 'My groups activity',
    'page callback' => 'ogactivity_page',
    'type' => MENU_LOCAL_TASK,
    'access callback' => 'user_access',
    'access arguments' => array(
      'view own activity',
    ),
  );
  return $items;
}

/**
 * Menu callback for displaying site or user activity as full page.
 */
function ogactivity_page() {
  global $user;
  $result = db_query("\nSELECT \n  DISTINCT(og.uid) \nFROM \n  {og_uid} og \nINNER JOIN \n  {og_uid} og2 ON og.nid = og2.nid AND \n  og.uid != %d AND \n  og2.uid = %d \nINNER JOIN \n  {activity_targets} at ON og.uid = at.target_uid \nINNER JOIN \n  {activity} a ON at.aid = a.aid \nWHERE \n   at.target_role = 'author'", $user->uid, $user->uid);
  while ($row = db_fetch_object($result)) {
    $users[] = $row->uid;
  }
  $activities = array();
  if ($users) {
    $activities = activity_get_activity($users, NULL, variable_get('activity_page_pager', 20));
  }
  $table = theme('activity_table', $activities);
  return theme('activity_page', $activities, $table);
}

/**
 * create a block for display
 * @param op
 * @param delta
 * @returns block HTML
 */
function ogactivity_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  switch ($op) {
    case 'list':
      $block['og']['info'] = t("Activity - Groups: show activity in user's groups.");
      return $block;
      break;
    case 'configure':
      $form['items'] = array(
        '#type' => 'select',
        '#title' => t('Number of items'),
        '#default_value' => variable_get('activity_block_' . $delta, 5),
        '#options' => drupal_map_assoc(range(1, 50)),
      );
      return $form;
      break;
    case 'save':
      variable_set('activity_block_' . $delta, $edit['items']);
      break;
    case 'view':
      switch ($delta) {
        case 'og':
          $result = db_query("\nSELECT \n  DISTINCT(og.uid) \nFROM \n  {og_uid} og \nINNER JOIN \n  {og_uid} og2 ON og.nid = og2.nid AND \n  og.uid != %d AND \n  og2.uid = %d \nINNER JOIN \n  {activity_targets} at ON og.uid = at.target_uid \nINNER JOIN \n  {activity} a ON at.aid = a.aid \nWHERE \n   at.target_role = 'author'", $user->uid, $user->uid);
          while ($row = db_fetch_object($result)) {
            $users[] = $row->uid;
          }
          $activity = array();
          if ($users) {
            $activity = activity_get_activity($users, NULL, variable_get('activity_block_' . $delta, 5) + 1);
          }
          if ($count = count($activity)) {
            drupal_add_css(drupal_get_path('module', 'activity') . '/activity.css');
            if ($count > variable_get('activity_block_' . $delta, 5)) {
              $more_link = theme('activity_more_link', 'activity/og');
              array_pop($activity);
            }
            $activites = array();
            foreach ($activity as $item) {
              $activities[] = theme('activity', activity_token_replace($item), $item) . activity_delete_link($item);
            }
            return array(
              'subject' => t('My groups activity'),
              'content' => theme('activity_block', $activities, $more_link),
            );
          }
          break;
      }
      break;
  }
}

Functions

Namesort descending Description
ogactivity_activityapi Implementation of hook_activityapi().
ogactivity_activity_info Activity definition file
ogactivity_block create a block for display
ogactivity_menu Implementation of hook_menu().
ogactivity_og Implementation of hook_og().
ogactivity_page Menu callback for displaying site or user activity as full page.
ogactivity_token_list Token module integration. Defines available default tokens.
ogactivity_token_values Token module integration. Defines available default token values.