You are here

function quiz_results_manage_results_form in Quiz 7

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \quiz_results_manage_results_form()
  2. 6.4 quiz.admin.inc \quiz_results_manage_results_form()
  3. 7.4 quiz.admin.inc \quiz_results_manage_results_form()

Form for searching after and manipulating results for a quiz

Parameters

$form_state: FAPI form_state

$quiz: The quiz node

Return value

FAPI-array

1 string reference to 'quiz_results_manage_results_form'
quiz_menu in ./quiz.module
Implements hook_menu().

File

./quiz.admin.inc, line 2091
Administrator interface for Quiz module.

Code

function quiz_results_manage_results_form($form, &$form_state, $quiz) {
  $form = array();
  $pre = 'quiz_results_mr_';
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'quiz') . '/theme/quiz_results_browser.js',
  );

  /* not_in_progress is a filter to filter away questions that are in progress...
     By default we don't want to show questions in progress... */
  if (!isset($_SESSION[$pre . 'not_in_progress'])) {
    $_SESSION[$pre . 'not_in_progress'] = 1;
  }

  // Specify action to avoid redirection issues when using ajax
  $form['#action'] = url('node/' . $quiz->nid . '/results');

  // We hide the update fieldset if we are to delete results
  $display = isset($_GET['del']) || isset($form_state['storage']['del']) ? 'none' : 'block';
  $form['update'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
      'id' => 'quiz-results-update',
      'style' => "display:{$display};",
    ),
  );
  $form['update']['bulk_action'] = array(
    '#type' => 'select',
    '#options' => array(
      'def' => '',
      'del' => t('delete'),
    ),
  );
  $form['update']['update'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  // We show the delete confirmation fieldset if we are to delete results
  $display = isset($_GET['del']) || isset($form_state['storage']['del']) ? 'block' : 'none';
  $form['confirm_delete'] = array(
    '#type' => 'fieldset',
    '#title' => t('Confirm deletion'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#attributes' => array(
      'style' => "display:{$display};",
      'id' => 'quiz-results-confirm-delete',
    ),
  );
  $form['confirm_delete']['help'] = array(
    '#type' => 'item',
    '#value' => t('Are you sure you want to delete all of these results?'),
    '#description' => t('This action cannot be undone'),
  );
  $form['confirm_delete']['confirm_delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete all marked results'),
  );
  $form['confirm_delete']['cancel'] = array(
    '#markup' => l(t('cancel'), $_GET['q'], array(
      'attributes' => array(
        'id' => 'quiz-results-cancel-delete',
      ),
    )),
  );
  $form['special_filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Special filters'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  if (!isset($_SESSION[$pre . 'best_results'])) {
    $_SESSION[$pre . 'best_results'] = 1;
  }
  $form['special_filters']['best_results'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only show each users best result'),
    '#parents' => array(
      'table',
      'filters',
      'best_results',
    ),
    '#default_value' => $_SESSION[$pre . 'best_results'],
  );
  if (!isset($_SESSION[$pre . 'not_in_progress'])) {
    $_SESSION[$pre . 'not_in_progress'] = 1;
  }
  $form['special_filters']['not_in_progress'] = array(
    '#type' => 'checkbox',
    '#title' => t('Do not show quizzes in progress'),
    '#parents' => array(
      'table',
      'filters',
      'not_in_progress',
    ),
    '#default_value' => $_SESSION[$pre . 'not_in_progress'],
  );
  $form['table'] = array(
    '#theme' => 'quiz_results_browser',
    '#tree' => TRUE,
  );
  $browser =& $form['table'];

  // Ahah targets
  $browser['ahah_target'] = array(
    '#markup' => '<DIV ID = "ahah-target"></DIV>',
  );
  $browser['ahah_target_all'] = array(
    '#markup' => '<DIV ID = "all-ahah-target">',
  );

  // js use this field to send extra query strings to drupal(sorting, paging etc)
  $browser['add_to_get'] = array(
    '#type' => 'hidden',
    '#default_value' => '',
  );

  // Build filter part of form:
  _quiz_results_mr_add_filter_fields($browser, $quiz);

  // Add querystring recieved via ajax to the $_GET array...
  if (isset($form_state['values'])) {
    _quiz_add_to_get($form_state['values']['table']['add_to_get']);
  }

  // Browsers table header
  $browser['#header'] = array(
    array(
      'data' => t('Username'),
      'field' => 'u.uid',
    ),
    array(
      'data' => t('Started'),
      'field' => 'started',
    ),
    array(
      'data' => t('Finished'),
      'field' => 'finished',
    ),
    array(
      'data' => t('Score'),
      'field' => 'score',
    ),
    array(
      'data' => t('Evaluated'),
      'field' => 'evaluated',
    ),
  );
  $res = _quiz_results_mr_data_provider($browser['#header'], $quiz);

  // build data part of form
  $options = array();

  //while ($res_o = db_fetch_object($res)) {
  foreach ($res as $res_o) {
    $id = $quiz->nid . '-' . $res_o->result_id;

    // build options array for checkboxes for item
    if (empty($res_o->name)) {
      if ($res_o->uid == '0') {
        $options[$id] = variable_get('anonymous', t('Anonymous'));
      }
      else {
        $options[$id] = t('ORPHAN %uid', array(
          '%uid' => '#' . $res_o->uid,
        ));
      }
    }
    else {
      $options[$id] = check_plain($res_o->name);
    }

    // Build hover menu for users who want to act on a single result
    $browser['hover_menu'][$id]['#value'] = _quiz_results_mr_get_hover($quiz, $res_o->result_id);

    // Add data for the table columns
    $browser['started'][$id]['#value'] = format_date($res_o->started, 'short');
    $browser['finished'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : format_date($res_o->finished, 'short');
    $browser['duration'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : _quiz_format_duration($res_o->duration);
    $browser['score'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : check_plain($res_o->score);
    $browser['evaluated'][$id]['#value'] = $res_o->evaluated == 0 ? t('No') : t('Yes');
    $browser['pass_rate'][$id]['#value'] = $res_o->pass_rate;
  }

  // We copy the checkboxes that have been chosen in the previous stage, and unset them to avoid having them loaded again.
  $default_value = isset($form_state['storage']['del']) ? $form_state['storage']['del'] : array(
    $quiz->nid . '-' . (isset($_GET['del']) ? $_GET['del'] : ''),
  );
  unset($form_state['storage']['del']);
  $browser['name'] = array(
    '#title' => t('Name'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'quiz-browser-checkbox',
      ),
    ),
    '#default_value' => $default_value,
  );
  $browser['pager'] = array(
    '#markup' => '<DIV ID ="browser-pager">' . theme('pager', array(
      'tags' => NULL,
    )) . '</DIV>',
  );
  $browser['ahah_target_all_end'] = array(
    '#markup' => '</DIV>',
  );
  $form['#submit'][] = 'quiz_results_mr_form_submit';
  return $form;
}