You are here

function _webform_report_get_report_data in Webform Report 7

Same name and namespace in other branches
  1. 6.2 webform_report.inc \_webform_report_get_report_data()

Get webform report data

Parameters

node : the current node object

Return value

report array - keys 'headers' = report headers, 'rows' = report rows

2 calls to _webform_report_get_report_data()
webform_report_export_form_submit in ./webform_report.inc
Implementation of hook_submit for the report export form
_webform_report_get_body_content in ./webform_report.inc
Get node body content for the specified webform report.

File

./webform_report.inc, line 331
This file contains common functions and functions required to ouput a report for the webform report module

Code

function _webform_report_get_report_data($node) {
  $report = array();

  // get report criteria
  $components = _webform_report_get_components($node->wnid);
  $columns = _webform_report_get_columns($node, $components);

  // if any columns
  if (count($columns) > 0) {
    $headers = array();
    $fields = array();

    // set column headers and field info
    foreach ($columns as $index => $col) {
      if ($col['hidden'] != TRUE) {

        // set report header - also save type for later
        $headers[] = array(
          'data' => $col['name'],
          'field' => $col['cid'],
          'type' => $col['type'],
        );
      }

      // fields by cid for quick lookup
      $fields[$col['cid']] = $col['name'];

      // Get mapping for select lists (Start)
      if ($col['type'] == 'select') {

        // get component info
        $result = db_query("SELECT c.extra FROM {webform_component} c WHERE c.nid = :nid AND c.cid = :cid", array(
          ':nid' => $node->wnid,
          ':cid' => $col['cid'],
        ));
        $r = $result
          ->fetchObject();
        $extra = unserialize($r->extra);

        // add missing key to fix php notice
        if (!array_key_exists('options_source', $extra)) {
          $extra['options_source'] = '';
        }

        // load the webform select component handler
        module_load_include('inc', 'webform', 'components/select');

        // detect webforms 3.x and handle
        if (function_exists('_webform_select_options_callback')) {

          // make a webform "component"
          $wfcomp['extra'] = $extra;

          // get select options
          $columns[$index]['pairs'] = _webform_select_options($wfcomp);
        }
        else {
          if (strpos($extra['items'], '|')) {
            $columns[$index]['pairs'] = array();
            foreach (explode("\n", $extra['items']) as $pair) {
              list($k, $v) = explode('|', $pair);
              $columns[$index]['pairs'][$k] = $v;
            }
          }
        }
      }

      // end - if ($col['type'] == 'select')...
      // Get mapping for select lists (End)
    }

    // end - foreach ($columns as $index => $col)...
    // query submissions
    $rs = _webform_report_get_submissions($node);
    if ($rs) {

      // get other report criteria
      $filters = _webform_report_get_filters($node, $components);
      $sorton = _webform_report_get_sorton($node, $components);

      // init values
      $rows = array();
      $csid = 0;

      // add filter fields to lookup
      foreach ($filters as $index => $filter) {

        // fields by cid for quick lookup
        $fields[$filter['cid']] = $filter['name'];
      }

      // submission counter
      $sc = 0;

      // get column data
      while (TRUE) {

        // get next submission data
        $row = $rs
          ->fetchObject();

        // check for end of data or end of submission
        if (!$row || $row->sid != $csid) {

          // process data for this submission, if any
          if ($csid != 0) {

            // test submission against filters
            if (_webform_report_test_filters($data, $filters, $columns)) {

              // output row if filters pass
              $rows[] = _webform_report_output($data, $columns);
            }
          }

          // if end of submission data, exit
          if (!$row) {
            break;
          }

          // count submission
          $sc++;

          // set current submission id
          $csid = $row->sid;

          // empty raw data row
          $data = array();

          // save submitter uid and node nid
          $data['uid'] = $row->uid;
          $data['nid'] = $row->nid;

          // fill in meta fields
          if (array_key_exists(-1, $fields)) {
            $data[-1] = array(
              l($row->user, 'user/' . $row->uid),
            );
          }
          if (array_key_exists(-2, $fields)) {
            $data[-2] = array(
              $row->submitted,
            );
          }
          if (array_key_exists(-3, $fields)) {
            $data[-3] = array(
              $row->submitted,
            );
          }
          if (array_key_exists(-4, $fields)) {
            $data[-4] = array(
              $row->remote_addr,
            );
          }
          if (array_key_exists(-5, $fields)) {
            $data[-5] = array(
              l(t('edit'), 'node/' . $row->nid . '/submission/' . $row->sid . '/edit'),
            );
          }
          if (array_key_exists(-6, $fields)) {
            $data[-6] = array(
              l(t('view'), 'node/' . $row->nid . '/submission/' . $row->sid),
            );
          }
          if (array_key_exists(-7, $fields)) {
            $data[-7] = array(
              l(t('delete'), 'node/' . $row->nid . '/submission/' . $row->sid . '/delete'),
            );
          }
        }

        // if component is on report
        if (array_key_exists($row->cid, $fields)) {

          // add raw data
          $data[$row->cid][] = $row->data;
        }
      }

      // end - while (TRUE)...
      // see if any rows are available
      if (count($rows) > 0) {

        // sort
        _webform_report_sort($headers, $rows, $sorton, $columns);

        // execute php code, if given
        if (isset($node->options['php_code'])) {
          eval('?>' . $node->options['php_code']);
        }
      }

      // save rows if any submissions were returned
      if ($sc > 0) {
        $report['rows'] = $rows;
      }
    }

    // end - if (!empty($rs))...
    $report['headers'] = $headers;
  }

  // end -   if (count($columns) > 0)...
  return $report;
}