You are here

spaces_dashboard.module in Spaces 5.2

File

spaces_dashboard/spaces_dashboard.module
View source
<?php

/*
 * Implementation of hook_menu()
 */
function spaces_dashboard_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'group_dashboard',
      'title' => t('Group Dashboard'),
      'description' => t('Allows a user in multiple groups to get a birds-eye view of her groups.'),
      'callback' => 'spaces_dashboard_group_dashboard',
      'access' => user_access('access content'),
      'type' => MENU_NORMAL_ITEM,
    );
    if (module_exists('ucreate')) {
      $items[] = array(
        'path' => 'group_dashboard/team',
        'title' => t('Users'),
        'description' => t('A full listing of users on the spaces.'),
        'callback' => 'spaces_dashboard_team',
        'access' => user_access('create users'),
        'type' => MENU_NORMAL_ITEM,
      );
    }
  }
  return $items;
}

/**
 * Implementation of hook_help()
 */
function spaces_dashboard_help($page) {
  switch ($page) {
    case 'group_dashboard':
    case 'group_dashboard/dashboard':
      return "<p>" . t('The group dashboard shows you the latest content across all of your groups. You can limit the listing by using the dropdown filter.') . "</p>";
    case 'group_dashboard/group_directory':
      return "<p>" . t('The directory lists all groups you have access to. You may join groups that interest you if they allow others to join or request membership.') . "</p>";
    case 'group_dashboard/team':
      return "<p>" . t('You can manage user accounts and group membership from this page. Select multiple users to add or remove them from a group.') . "</p>";
      break;
  }
}

/*
 * Implementation of hook_form_alter()
 */
function spaces_dashboard_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'ucreate_user_form':
      context_set('spaces', 'feature', 'group_dashboard');
      break;
    case 'views_filters':

      // Switch nid text filter to a select
      if ($form['view']['#value']->name == 'spaces_group_dashboard') {
        global $user;
        $options = array(
          '**ALL**' => '-- ' . t('All of my groups') . ' --',
        );
        foreach ($user->og_groups as $gid => $group) {
          $options[$gid] = truncate_utf8($group['title'], 30, false, true);
        }
        $form['filter0'] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $_GET['filter0'] ? $_GET['filter0'] : '**ALL**',
        );
        $form['#action'] = 'group_dashboard';
      }
      break;
  }
}

/**
 * Implementation of hook_context_define()
 */
function spaces_dashboard_context_define() {
  global $user;
  $items = array();
  $items[] = array(
    'namespace' => 'spaces',
    'attribute' => 'feature',
    'value' => 'group_dashboard',
    'node' => og_get_types('group'),
    'views' => array(
      'spaces_group_dashboard',
      'spaces_group_directory',
      'spaces_group_my',
    ),
    'block' => array(
      array(
        'module' => 'views',
        'delta' => 'spaces_group_my',
        'region' => 'right',
        'weight' => -11,
      ),
    ),
    'menu' => array(
      'group_dashboard',
    ),
  );
  return $items;
}

/**
 *  Implementation of hook_default_views
 */
function spaces_dashboard_views_default_views() {
  $default_views = array(
    '_spaces_dashboard_views_group_dashboard',
    '_spaces_dashboard_views_group_directory',
    '_spaces_dashboard_views_group_my',
  );
  foreach ($default_views as $v) {
    $view = call_user_func($v);
    if (is_object($view) && $view->name) {
      $views[$view->name] = $view;
    }
  }
  return $views;
}

/**
 * Implements a group dashboard for users in multiple groups
 * TODO: implement "fires" listing
 */
function spaces_dashboard_group_dashboard() {
  global $user;
  if ($space = spaces_get_space()) {
    $space
      ->redirect('home');
  }
  else {
    if ($user->uid != 0) {

      // User is a member of many groups, show the dashboard.
      if (count($user->og_groups)) {
        return views_build_view('page', views_get_view('spaces_group_dashboard'), null, true, 25);
      }
      else {

        // Set the context as if the view were built
        context_ui_set('views', 'spaces_group_dashboard');

        // Check whether any 'joinable' groups exist
        $group_count = db_result(db_query("SELECT count(n.nid) FROM {node} n JOIN {og} og ON n.nid = og.nid WHERE n.status = 1 AND og.selective = 0"));

        // There are groups to join
        if ($group_count) {
          $message = t("You are currently not a member of any groups. You can browse the group directory and join groups that interest you.");
          $buttons = l(t('Group Directory'), 'group_dashboard/group_directory', array(
            'class' => 'button',
          ));
        }
        else {

          // User can make groups
          if (node_access('create', $group_types[0])) {
            $message = t("There are currently no groups available for you to join. Please create your first group to get started.");
            $buttons = spaces_node_links();
          }
          else {
            $message = t("You are currently not a member of any groups. Please ask a site administrator to add your account to one of the site's groups.");
          }
        }
        drupal_set_message("<p>{$message}</p><div class='buttons'>{$buttons}</div>");
        return '';
      }
    }
    else {

      // User shouldn't be here.
      drupal_access_denied();
      exit;
    }
  }
}

/*
 * A user listing page
 */
function spaces_dashboard_team($gid = null) {
  context_set('spaces', 'feature', 'group_dashboard');

  // Add contextual button for adding users
  if (user_access('create users')) {
    $links = array();
    $links[] = array(
      'title' => t('User account'),
      'href' => 'user/add',
    );
    context_set('spaces', 'links', $links);
  }
  drupal_add_css(drupal_get_path('module', 'spaces_dashboard') . '/spaces_dashboard.css');
  drupal_add_js(drupal_get_path('module', 'spaces_dashboard') . '/spaces_dashboard.js');
  $output .= drupal_get_form('spaces_dashboard_users');
  return $output;
}
function spaces_views_handler_field_nodelink($fieldinfo, $fielddata, $value, $data) {
  if ($fielddata['options'] == 'nolink') {
    return check_plain($value);
  }
  return l($value, "node/{$data->nid}");
}
function spaces_dashboard_users() {
  $form = array();
  $form['groups'] = array(
    '#title' => t('Group'),
    '#type' => 'select',
    '#options' => array(
      '---',
    ) + og_all_groups_options(),
  );
  $form['actions'] = array(
    '#title' => t('Actions'),
    '#type' => 'select',
    '#disabled' => true,
    '#options' => array(
      0 => '---',
      t('Groups') => array(
        'join' => t('Add to Group'),
        'yank' => t('Remove from Group'),
      ),
      t('Status') => array(
        'active' => t('Make Active'),
        'block' => t('Block Users'),
      ),
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  $team = spaces_og_get_users(true, false, true, 20);
  foreach ($team as $account) {
    $account = (object) $account;
    $account = user_load($account);
    $form['username'][$account->uid] = array(
      '#type' => 'markup',
      '#value' => theme('username', $account),
    );
    $form['email'][$account->uid] = array(
      '#type' => 'markup',
      '#value' => l($account->mail, 'mailto:' . $account->mail),
    );
    $form['#accounts'][$account->uid] = $account;
    $users[$account->uid] = '';
  }
  $form['users'] = array(
    '#type' => 'checkboxes',
    '#options' => $users,
  );
  $form['pager'] = array(
    '#value' => theme('pager', NULL, 20, 0),
  );
  $form['#theme'] = 'spaces_dashboard_users';
  return $form;
}
function spaces_dashboard_users_submit($form_id, $form_values) {
  $gid = $form_values['groups'];
  $action = $form_values['actions'];
  $users = array();
  foreach ($form_values['users'] as $uid => $selected) {
    if ($selected) {
      $users[] = $uid;
    }
  }
  _spaces_dashboard_user_batch($action, $users, $gid);
  return 'group_dashboard/team';
}
function _spaces_dashboard_user_batch($op = 'active', $users, $gid = null) {
  switch ($op) {
    case 'active':
    case 'block':
      $status = $op == 'active' ? 1 : 0;
      foreach ($users as $uid) {
        $account = user_load(array(
          'uid' => $uid,
        ));
        user_save($account, array(
          'status' => $status,
        ));
      }
      return true;
    case 'join':

      // check gid for validity
      if (is_numeric($gid) && ($node = node_load($gid))) {
        if (og_is_group_type($node->type)) {
          foreach ($users as $uid) {
            $account = user_load(array(
              'uid' => $uid,
            ));
            if (!isset($account->og_groups[$gid])) {
              og_save_subscription($gid, $uid, array(
                'is_active' => 1,
              ));
            }
          }
          return true;
        }
      }
      return false;
    case 'yank':

      // check gid for validity
      if (is_numeric($gid) && ($node = node_load($gid))) {
        if (og_is_group_type($node->type)) {
          foreach ($users as $uid) {
            $account = user_load(array(
              'uid' => $uid,
            ));
            if (isset($account->og_groups[$gid])) {
              og_delete_subscription($gid, $uid);
            }
          }
          return true;
        }
      }
      return false;
  }
}
function theme_spaces_dashboard_users($form) {
  $row = array();
  $labels[] = $form['groups']['#title'];
  $labels[] = $form['actions']['#title'];
  $labels[] = '';
  unset($form['groups']['#title']);
  unset($form['actions']['#title']);
  $row[] = drupal_render($form['groups']);
  $row[] = drupal_render($form['actions']);
  $row[] = drupal_render($form['submit']);
  $output .= theme('table', $labels, array(
    $row,
  ));

  // Overview table:
  $header = array(
    theme('table_select_header_cell'),
    t('Name'),
    t('User Info'),
  );
  if (isset($form['username']) && is_array($form['username'])) {
    foreach (element_children($form['username']) as $key) {
      $groups = $groupnames = array();
      foreach ($form['#accounts'][$key]->og_groups as $gid => $node) {
        $groupnames[] = $node['title'];
        $groups[] = 'og-' . $gid;
      }
      $groups = implode(' ', $groups);
      $groupnames = "<div class='groupnames'>" . implode(', ', $groupnames) . "</div>";
      $row = array();
      $row[] = drupal_render($form['users'][$key]);
      $row[] = array(
        'data' => drupal_render($form['username'][$key]),
        'class' => 'name',
      );
      $row[] = drupal_render($form['email'][$key]) . $groupnames;

      // $row[] = $form['#accounts'][$key]->status ? t('Active') : t('Blocked');
      $rows[] = array(
        'data' => $row,
        'class' => $groups . ($form['#accounts'][$key]->status ? '' : ' blocked'),
      );
    }
  }
  else {
    $rows[] = array(
      array(
        'data' => t('No users found.'),
        'colspan' => '4',
      ),
    );
  }
  $output .= theme('table', $header, $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }
  $output .= drupal_render($form);
  return $output;
}

/**
 * Provides a links array suitable for use with theme_links() to the
 * dashboard pages.
 */
function spaces_dashboard_nav_links() {
  $links = array();
  $links[] = array(
    'title' => t('Dashboard'),
    'href' => 'group_dashboard',
  );
  $links[] = array(
    'title' => t('Directory'),
    'href' => 'group_dashboard/group_directory',
  );
  if (module_exists('ucreate') && user_access('create users')) {
    $links[] = array(
      'title' => t('Users'),
      'href' => 'group_dashboard/team',
    );
  }
  return $links;
}

/*
 * EXPORTED VIEWS ======================================================
 */
function _spaces_dashboard_views_group_dashboard() {
  $view = new stdClass();
  $view->name = 'spaces_group_dashboard';
  $view->description = '';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Group Dashboard';
  $view->page_empty = '<p>' . t('No posts found in your groups.') . '</p>';
  $view->page_empty_format = '4';
  $view->page_type = 'table';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '25';
  $view->sort = array(
    array(
      'tablename' => 'node_comment_statistics',
      'field' => 'last_changed',
      'sortorder' => 'DESC',
      'options' => 'normal',
    ),
  );
  $view->argument = array();
  $view->field = array(
    array(
      'tablename' => 'og_node_data',
      'field' => 'title',
      'label' => 'Group',
      'handler' => 'spaces_views_handler_crayon_name',
      'options' => 'og',
    ),
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Title',
      'handler' => 'spaces_views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'label' => 'Type',
    ),
    array(
      'tablename' => 'node_comment_statistics',
      'field' => 'last_comment_name',
      'label' => 'Last post',
    ),
    array(
      'tablename' => 'node_comment_statistics',
      'field' => 'last_changed',
      'label' => 'On',
      'handler' => 'views_handler_field_since',
      'options' => 1,
    ),
  );
  if (variable_get('spaces_calendar_feed_itemtype', '')) {
    $excluded = array(
      'shout',
      variable_get('spaces_calendar_feed_itemtype', ''),
    );
  }
  else {
    $excluded = array(
      'shout',
    );
  }
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'NOR',
      'options' => '',
      'value' => array_merge(og_get_types('group'), $excluded),
    ),
    array(
      'tablename' => 'og_uid_node',
      'field' => 'currentuid',
      'operator' => '=',
      'options' => '',
      'value' => '***CURRENT_USER***',
    ),
    array(
      'tablename' => 'node',
      'field' => 'changed',
      'operator' => '>',
      'options' => -1 * SPACES_ARCHIVE_TIMESTAMP,
      'value' => 'now',
    ),
    array(
      'tablename' => 'og_ancestry',
      'field' => 'gid',
      'operator' => '=',
      'options' => '',
      'value' => '',
    ),
  );
  $view->exposed_filter = array(
    array(
      'tablename' => 'og_ancestry',
      'field' => 'gid',
      'label' => 'Posts in my groups',
      'optional' => '1',
      'is_default' => '0',
      'operator' => '1',
      'single' => '1',
    ),
  );
  $view->requires = array(
    node_comment_statistics,
    og_node_data,
    node,
    og_uid_node,
    og_ancestry,
  );
  return $view;
}
function _spaces_dashboard_views_group_my() {
  $view = new stdClass();
  $view->name = 'spaces_group_my';
  $view->description = '';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = FALSE;
  $view->block = TRUE;
  $view->block_title = 'My Groups';
  $view->block_empty = '<p>' . t('No groups found') . '</p>';
  $view->block_empty_format = '2';
  $view->block_type = 'spaces_datetitle';
  $view->nodes_per_block = '99';
  $view->sort = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array();
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => '_spaces_dashboard_views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'og',
      'field' => 'private',
      'label' => '',
    ),
  );
  $view->filter = array(
    array(
      'tablename' => 'og_uid',
      'field' => 'currentuidsimple',
      'operator' => '=',
      'options' => '',
      'value' => '***CURRENT_USER***',
    ),
  );
  $view->exposed_filter = array();
  $view->requires = array(
    node,
    og_uid,
  );
  return $view;
}
function _spaces_dashboard_views_group_directory() {
  $view = new stdClass();
  $view->name = 'spaces_group_directory';
  $view->description = '';
  $view->access = array();
  $view->view_args_php = '';
  $view->menu = TRUE;
  $view->menu_title = t('Directory');
  $view->page = TRUE;
  $view->page_title = t('Group Directory');
  $view->page_type = 'table';
  $view->url = 'group_dashboard/group_directory';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '25';
  $view->sort = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array();
  $view->field = array(
    array(
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Group',
      'handler' => '_spaces_dashboard_views_handler_field_nodelink',
      'options' => 'link',
    ),
    array(
      'tablename' => 'og',
      'field' => 'private',
      'handler' => '_spaces_dashboard_views_handler_field_private',
      'label' => 'Privacy',
    ),
    array(
      'tablename' => 'og',
      'field' => 'count',
      'label' => 'Members',
    ),
  );
  $view->filter = array(
    array(
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array(
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => og_get_types('group'),
    ),
  );
  $view->exposed_filter = array();
  $view->requires = array(
    node,
    og,
  );
  return $view;
}

/**
 * Custom views field handler for adding a class to distinguish
 * between public/private group nodes. Requires a field for
 * og_private in the view.
 */
function _spaces_dashboard_views_handler_field_nodelink($fieldinfo, $fielddata, $value, $data) {
  if ($fielddata['options'] == 'nolink') {
    return check_plain($value);
  }
  return $data->og_private ? l($value, "node/{$data->nid}", array(
    'class' => 'private',
  )) : l($value, "node/{$data->nid}", array(
    'class' => 'public',
  ));
}

/**
 * Handler to display public/private text.
 */
function _spaces_dashboard_views_handler_field_private($fieldinfo, $fielddata, $value, $data) {
  return $data->og_private ? t('Private') : t('Public');
}

Functions

Namesort descending Description
spaces_dashboard_context_define Implementation of hook_context_define()
spaces_dashboard_form_alter
spaces_dashboard_group_dashboard Implements a group dashboard for users in multiple groups TODO: implement "fires" listing
spaces_dashboard_help Implementation of hook_help()
spaces_dashboard_menu
spaces_dashboard_nav_links Provides a links array suitable for use with theme_links() to the dashboard pages.
spaces_dashboard_team
spaces_dashboard_users
spaces_dashboard_users_submit
spaces_dashboard_views_default_views Implementation of hook_default_views
spaces_views_handler_field_nodelink
theme_spaces_dashboard_users
_spaces_dashboard_user_batch
_spaces_dashboard_views_group_dashboard
_spaces_dashboard_views_group_directory
_spaces_dashboard_views_group_my
_spaces_dashboard_views_handler_field_nodelink Custom views field handler for adding a class to distinguish between public/private group nodes. Requires a field for og_private in the view.
_spaces_dashboard_views_handler_field_private Handler to display public/private text.