You are here

function theme_makemeeting_answers in Make Meeting Scheduler 7.2

Function used to render answers' table

File

./makemeeting.theme.inc, line 78

Code

function theme_makemeeting_answers($vars) {
  global $user;
  $form = $vars['form'];
  $item = $form['#item'];

  // Add appropriate CSS.
  drupal_add_css(drupal_get_path('module', 'makemeeting') . '/makemeeting.css');

  // Initialize variables
  $header = $day_cell = $row_form = $tree = $rows = array();

  // First construct tree of suggestions for headers
  foreach ($item['choices'] as $key => $choice) {
    $original = _makemeeting_date_timestamp($choice['chdate']);
    $month = $original
      ->format('F Y');
    $day = $original
      ->format('D. j');
    $count = 0;
    foreach ($choice['chsuggestions'] as $text) {
      if (!$text && isset($tree[$month][$day]) && $count) {
        continue;
      }
      elseif (!$text) {
        $text = "-";
      }

      // Try to convert the hour to convert timezone
      $dateToParse = $key . ' ' . $text;
      $tz = new DateTimeZone($item['timezone']);
      $userTz = new DateTimeZone(drupal_get_user_timezone());
      if ($date = DateTime::createFromFormat('d-m-Y H\\h', $dateToParse, $tz)) {
        $text = $date
          ->setTimezone($userTz)
          ->format('H\\h');
      }
      elseif ($date = DateTime::createFromFormat('d-m-Y H:i', $dateToParse, $tz)) {
        $text = $date
          ->setTimezone($userTz)
          ->format('H:i');
      }
      elseif ($date = DateTime::createFromFormat('d-m-Y H a', $dateToParse, $tz)) {
        $text = $date
          ->setTimezone($userTz)
          ->format('H a');
      }
      if ($date && $date
        ->format('F Y') != $month) {
        $month = $date
          ->format('F Y');
      }
      if ($date && $date
        ->format('D. j') != $day) {
        $day = $date
          ->format('D. j');
      }
      $tree[$month][$day][] = $text;
      $count++;
    }
  }

  // Now construct headers based on tree
  $headers = $row_days = $row_suggestions = array(
    '',
  );

  // Add month-related classes
  $months_total = count($tree);
  $months_count = 0;
  foreach ($tree as $month => $days) {
    $header['colspan'] = 0;
    $header['data'] = $month;
    $header['class'] = array(
      'date-month',
    );
    _makemeeting_define_class_suffix($header, $months_count, $months_total, 'date-month');
    $months_count++;

    // Add day-related classes
    $days_total = count($days);
    $days_count = 0;
    foreach ($days as $day => $suggestions) {
      $day_cell['colspan'] = 0;
      $day_cell['data'] = $day;
      $day_cell['class'] = array(
        'date',
      );
      _makemeeting_define_class_suffix($day_cell, $days_count, $days_total, 'date');
      $days_count++;

      // Add suggestion-related classes
      $suggestions_total = count($suggestions);
      $suggestion_count = 0;
      foreach ($suggestions as $text) {
        $suggestion = array(
          'data' => $text,
          'class' => array(
            'suggestion',
          ),
        );
        _makemeeting_define_class_suffix($suggestion, $suggestion_count, $suggestions_total, 'suggestion');
        $suggestion_count++;
        $row_suggestions[] = $suggestion;

        // Set colspan
        $header['colspan']++;
        $day_cell['colspan']++;
      }
      $row_days[] = $day_cell;
    }
    $headers[] = $header;
  }
  array_push($rows, $row_days, $row_suggestions);

  // Initialize totals labels
  $totals = array();
  $options = _makemeeting_options($item['yesnomaybe']);
  foreach ($options as $key => $label) {
    $totals[$key]['table'] = array(
      array(
        'data' => t('Totals ' . $label),
        'class' => 'total-title',
      ),
    );
    foreach ($item['choices'] as $choice) {
      $count = 0;
      foreach ($choice['chsuggestions'] as $text) {
        if ($text || !$text && !$count) {

          // Initialize totals count
          $totals[$key]['count'][] = 0;
        }
        $count++;
      }
    }
  }

  // Display already submitted answers
  $select = db_select('makemeeting_answers', 'ma')
    ->fields('ma');
  foreach (array(
    'field_name',
    'entity_type',
    'deleted',
    'entity_id',
    'language',
    'delta',
  ) as $info) {
    $select
      ->condition($info, $form[$info]['#value']);
  }

  // Filter out answer being edited
  if (!empty($form['answer_edited'])) {
    $select
      ->condition('answer_id', $form['answer_edited']['#value']->answer_id, '!=');
  }
  $results = $select
    ->execute()
    ->fetchAllAssoc('answer_id');
  $uids = array();
  foreach ($results as $result) {
    $uids[] = $result->uid;
    if (!$item['is_hidden']) {
      $row = array();

      // Display name of the person that has answered
      $cell = _makemeeting_render_cell_name($result->uid, $result->name, $result->answer_id);
      $row[] = drupal_render($cell);

      // Display his/her answers
      $answers = unserialize($result->value);
      foreach (element_children($form['answers']) as $answer_key) {
        $row_count = count($row);

        // Determine choice based on choice type
        if ($item['one_option']) {
          $key = $answers == $answer_key ? MAKEMEETING_YES : MAKEMEETING_NO;
        }
        else {
          $key = isset($answers[$answer_key]) ? intval($answers[$answer_key]) : FALSE;
        }
        $label = isset($options[$key]) ? $options[$key] : FALSE;
        if ($key !== FALSE && $label !== FALSE) {

          // Add corresponding classes for the answer
          $classes = array(
            'answer',
            'answer-' . $key,
          );
          foreach (array(
            'suggestion-first',
            'suggestion-medium',
            'suggestion-last',
          ) as $class) {
            if (in_array($class, $row_suggestions[$row_count]['class'])) {
              $classes[] = str_replace('suggestion', 'answer', $class);
            }
          }

          // Store answer (will an background image according to class)
          $row[] = array(
            'data' => '<span>' . $label . '</span>',
            'class' => $classes,
          );

          // Increment key answer counter
          $totals[$key]['count'][$row_count - 1]++;
        }
        else {
          $row[] = t('No answer');
        }
      }
      $rows[] = array(
        'data' => $row,
        'id' => 'answer-' . $result->answer_id,
        'class' => array(
          'in' . (empty($cell['#editable']) ? '' : ' editable'),
        ),
      );
    }
  }
  if (!$item['is_hidden']) {

    // Add participant count
    $rows[1][0] = format_plural(count($results), '@count participant', '@count participants');
  }

  // Display answer form if ... not limited,
  $limited = $item['limit'] > 0 && $item['limit'] * count($row_suggestions) - 1 <= count($results);

  // ... not closed,
  $closed = $item['closed'] || $limited;
  $submitted = user_is_logged_in() && in_array($user->uid, array_values($uids));

  // ... user has access and has not already submitted
  $show_form = user_access(MAKEMEETING_ANSWER_PERM) && !$closed && !$submitted;
  if ($show_form) {
    $row_form[] = drupal_render($form['name']);
    foreach (element_children($form['answers']) as $answer_key) {
      $row_form[] = array(
        'data' => drupal_render($form['answers'][$answer_key]),
        'class' => $form['answers'][$answer_key]['#type'],
      );
    }
    $rows[] = array(
      'data' => $row_form,
      'class' => array(
        'answer-form-row',
      ),
    );
  }

  // Get total maxima
  $totals_max = array();
  foreach ($totals as $key => &$table) {
    if (empty($table['count'])) {
      $table['count'][] = 0;
    }
    $totals_max[$key] = max($table['count']);
  }

  // Display totals
  foreach ($options as $key => $label) {
    foreach ($totals[$key]['count'] as $choice => $count) {
      $classes = array(
        'total-count',
        'total-' . $key,
      );
      if ($count) {

        // Indicate first / last suggestion item
        foreach (array(
          'suggestion-first',
          'suggestion-medium',
          'suggestion-last',
        ) as $class) {
          if (in_array($class, $row_suggestions[$choice + 1]['class'])) {
            $classes[] = str_replace('suggestion', 'total', $class);
          }
        }
      }

      // Indicate max count
      if (!empty($totals_max[$key]) && $count == $totals_max[$key]) {
        $classes[] = 'total-max';
      }
      $totals[$key]['table'][] = array(
        'data' => $count,
        'class' => $classes,
      );
    }
    $classes = array(
      'total-row',
    );

    // Change labels for two choices
    if (!$item['yesnomaybe']) {
      $totals[$key]['table'][0]['data'] = t('Totals');
    }

    // Do not display other totals for two choices
    if ($item['yesnomaybe'] || !$item['yesnomaybe'] && $key == MAKEMEETING_YES) {
      $classes[] = 'total-row-' . ($key ? $key == MAKEMEETING_YES ? 'last' : 'medium' : 'first');
      $rows[] = array(
        'data' => $totals[$key]['table'],
        'class' => $classes,
      );
    }
  }

  // Apply classes to the two first rows
  $rows[0] = array(
    'data' => $rows[0],
    'class' => array(
      'dates',
    ),
  );
  $rows[1] = array(
    'data' => $rows[1],
    'class' => array(
      'suggestions',
    ),
  );
  $suggestion = array(
    '#theme' => 'table',
    '#header' => $headers,
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'makemeeting-table',
      ),
    ),
  );
  if ($show_form) {
    return drupal_render($suggestion) . drupal_render_children($form);
  }
  return drupal_render($suggestion);
}