You are here

webform_report.module in Webform Report 7

This module allows users to create simple, dynamic reports based on data collected by the webform module. It adds a new node type that contains the report criteria, and the data displayed is updated automatically as webforms are date. The data can be sorted according to the viewer's preference without altering the report criteria.

The module is written by Kristian Lance for Communications Research Centre Canada (CRC), an agency of Industry Canada.

File

webform_report.module
View source
<?php

/**
 * @file
 * This module allows users to create simple, dynamic reports based on data collected
 * by the webform module. It adds a new node type that contains the report criteria,
 * and the data displayed is updated automatically as webforms are date. The data
 * can be sorted according to the viewer's preference without altering the report criteria.
 *
 * The module is written by Kristian Lance for Communications Research Centre Canada (CRC),
 * an agency of Industry Canada.
 *
 */

/**
 * Implementation of hook_help
 */
function webform_report_help($path) {
  switch ($path) {
    case 'admin/modules#description':
      return t('Allows users to create reports from Webform data.');
      break;
    case 'node/add#webform_report':
      return t('A Webform report is a dynamic page that contains Webform data. The data can be sorted by a particular field, and fields can be included/excluded from the report as needed.');
      break;
  }
}

/**
 * Implementation of hook_permission
 */
function webform_report_permission() {
  return array(
    'access webform reports' => array(
      'title' => t('Access Webform Reports'),
    ),
    'create webform reports' => array(
      'title' => t('Create Webform Reports'),
    ),
    'edit webform reports' => array(
      'title' => t('Edit Webform Reports'),
    ),
    'edit own webform reports' => array(
      'title' => t('Edit Own Webform Reports'),
    ),
    'use php code' => array(
      'title' => t('Use PHP Code in Webform Reports'),
    ),
  );
}

/**
 * Implementation of hook_node_access
 */
function webform_report_node_access($node, $op, $account) {
  $result = NODE_ACCESS_IGNORE;

  // get node values
  if (is_string($node)) {
    $node_type = $node;
    $node_status = FALSE;
    $node_uid = 0;
  }
  else {
    $node_type = $node->type;
    $node_status = $node->status;
    $node_uid = $node->uid;
  }

  // if a webform_report content type is requested
  if ($node_type == 'webform_report') {
    $result = NODE_ACCESS_DENY;
    switch ($op) {
      case 'view':

        // if node is published, return view access
        if ($node_status) {
          if (user_access('access webform reports', $account)) {
            $result = NODE_ACCESS_ALLOW;
          }
        }
        else {
          if (node_access('update', $node, $account)) {
            $result = NODE_ACCESS_ALLOW;
          }
        }
        break;
      case 'create':
        if (user_access('create webform reports', $account)) {
          $result = NODE_ACCESS_ALLOW;
        }
        break;
      case 'update':
      case 'delete':
        if (user_access('edit webform reports', $account) || user_access('edit own webform reports', $account) && $account->uid == $node_uid) {
          $result = NODE_ACCESS_ALLOW;
        }
        break;
    }
  }
  return $result;
}

/**
 * Implementation of hook_menu()
 */
function webform_report_menu() {
  $items = array();
  $items['admin/content/webform_report'] = array(
    'title' => t('Webform Reports'),
    'description' => t('View and edit Webform reports.'),
    'page callback' => '_webform_report_page',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'webform_report.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['node/%webform_report_menu/edit/webform_report/configuration'] = array(
    'title' => t('Configuration'),
    'page callback' => 'node_page_edit',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'node_access',
    'access arguments' => array(
      'update',
      1,
    ),
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['node/%webform_report_menu/edit/webform_report/criteria'] = array(
    'title' => t('Report Criteria'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'webform_report_criteria_form',
      1,
    ),
    'access callback' => 'node_access',
    'access arguments' => array(
      'update',
      1,
    ),
    'file' => 'webform_report_criteria.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['node/%webform_report_menu/csv'] = array(
    'title' => t('Report CSV Export'),
    'page callback' => 'webform_report_csv',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'node_access',
    'access arguments' => array(
      'view',
      1,
    ),
    'file' => 'webform_report.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_admin_paths()
 */
function webform_report_admin_paths() {
  $paths = array(
    'node/*/edit/webform_report/criteria' => TRUE,
  );
  return $paths;
}

/**
 * Menu loader callback; Load a webform report node if the given nid is a webform report
 */
function webform_report_menu_load($nid) {
  if (!is_numeric($nid)) {
    return FALSE;
  }
  $node = node_load($nid);
  if (empty($node->type) || $node->type != 'webform_report') {
    return FALSE;
  }
  return $node;
}

/**
 * Implementation of hook_node_info
 */
function webform_report_node_info() {
  return array(
    'webform_report' => array(
      'name' => t('Webform report'),
      'base' => 'webform_report',
      'description' => t('A webform report is a dynamic page that contains user-specified data collected by the Webform module. The data can be sorted by a particular field, and fields can be included/excluded as needed.'),
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function webform_report_theme() {
  $theme = array(
    'webform_report_view' => array(
      'variables' => array(
        'node' => NULL,
        'view_mode' => NULL,
      ),
    ),
    'webform_report_criteria_form' => array(
      'render element' => 'form',
    ),
  );
  return $theme;
}

/**
 * Implementation of hook_load
 */
function webform_report_load($nodes) {
  foreach ($nodes as $node) {
    $report = db_query("SELECT * FROM {webform_report} WHERE nid = :nid", array(
      ':nid' => $node->nid,
    ))
      ->fetchObject();
    if (empty($report)) {
      $report = stdClass();
      $report->wnid = 0;
      $report->columns = array();
      $report->sorton = array();
      $report->filters = array();
      $report->options = array();
    }
    else {
      $report->columns = unserialize($report->columns);
      $report->sorton = unserialize($report->sorton);
      $report->filters = unserialize($report->filters);
      $report->options = unserialize($report->options);
    }
    foreach ($report as $key => $value) {
      $nodes[$node->nid]->{$key} = $value;
    }
  }
}

/**
 * Implementation of hook_view
 */
function webform_report_view($node, $view_mode) {

  // no teasers for now
  if ($view_mode == 'teaser') {
    return $node;
  }
  $node->content['webform_report'] = array(
    '#markup' => theme('webform_report_view', array(
      'node' => $node,
      'view_mode' => $view_mode,
    )),
    '#weight' => 10,
  );
  return $node;
}

/**
 * Theming function for webform_report_view
 */
function theme_webform_report_view($variables) {
  $node = $variables['node'];
  $view_mode = $variables['view_mode'];
  if ($node->type != 'webform_report' || !isset($node->wnid) || $node->wnid == 0) {
    drupal_set_message(t('Unable to display webform report - possibly damaged node.'), 'error');
    return '';
  }
  module_load_include('inc', 'webform_report', 'webform_report');
  module_load_include('inc', 'webform_report', 'webform_report_criteria');
  $output = _webform_report_get_body_content($node, $view_mode);
  return $output;
}

/**
 * Implementation of hook_insert
 */
function webform_report_insert($node) {
  $node->columns = array();
  $node->sorton = array();
  $node->filters = array();
  $node->options = array();
  db_insert('webform_report')
    ->fields(array(
    'nid' => $node->nid,
    'description' => $node->description,
    'wnid' => $node->wnid,
    'columns' => serialize($node->columns),
    'sorton' => serialize($node->sorton),
    'filters' => serialize($node->filters),
    'options' => serialize($node->options),
  ))
    ->execute();
}

/**
 * Implementation of hook_update
 */
function webform_report_update($node) {
  db_update('webform_report')
    ->fields(array(
    'description' => $node->description,
    'columns' => serialize($node->columns),
    'sorton' => serialize($node->sorton),
    'filters' => serialize($node->filters),
    'options' => serialize($node->options),
  ))
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implementation of hook_delete
 */
function webform_report_delete($node) {
  db_delete('webform_report')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implementation of hook_form
 */
function webform_report_form($node) {
  module_load_include('inc', 'webform_report', 'webform_report');
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => 1,
    '#description' => t('Enter a title for your Webform report page'),
  );
  $default = '';
  if (isset($node->description)) {
    $default = $node->description;
  }
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('The description will be displayed at the top of the report page'),
    '#default_value' => $default,
    '#required' => FALSE,
    '#weight' => 2,
  );

  // if adding a new report, ask for the webform
  if (!isset($node->nid)) {
    $form['wnid'] = array(
      '#type' => 'select',
      '#title' => t('Webform'),
      '#options' => array(
        '' => t('Select a Webform'),
      ) + _webform_report_get_webforms(),
      '#required' => TRUE,
      '#weight' => 3,
      '#description' => t('Select a webform for this report.'),
    );
  }
  else {
    $default = '';
    if (isset($node->wnid)) {
      $default = check_plain(db_query("SELECT n.title FROM {node} n WHERE n.nid = :nid", array(
        ':nid' => $node->wnid,
      ))
        ->fetchField());
    }
    else {
      drupal_set_message(t('Error - Webform is not set for this report - cannot proceed.  Please delete and re-create this report.'), 'error');
    }
    $form['wnid'] = array(
      '#type' => 'textfield',
      '#title' => t('Webform'),
      '#default_value' => $default,
      '#weight' => 3,
      '#attributes' => array(
        'readonly' => 'readonly',
      ),
      '#description' => t('The webform for this report - this cannot be updated.'),
    );
  }
  return $form;
}

Functions

Namesort descending Description
theme_webform_report_view Theming function for webform_report_view
webform_report_admin_paths Implementation of hook_admin_paths()
webform_report_delete Implementation of hook_delete
webform_report_form Implementation of hook_form
webform_report_help Implementation of hook_help
webform_report_insert Implementation of hook_insert
webform_report_load Implementation of hook_load
webform_report_menu Implementation of hook_menu()
webform_report_menu_load Menu loader callback; Load a webform report node if the given nid is a webform report
webform_report_node_access Implementation of hook_node_access
webform_report_node_info Implementation of hook_node_info
webform_report_permission Implementation of hook_permission
webform_report_theme Implementation of hook_theme().
webform_report_update Implementation of hook_update
webform_report_view Implementation of hook_view