You are here

function quiz_results_manage_results_form in Quiz 8.4

Same name and namespace in other branches
  1. 6.4 quiz.admin.inc \quiz_results_manage_results_form()
  2. 7 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'
QuizController::quizResults in lib/Drupal/quiz/Controller/QuizController.php

File

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

Code

function quiz_results_manage_results_form($form, &$form_state, $quiz) {
  if ($form_state['rebuild']) {

    // We remember the filters for the next time the same user visits the result browser
    _quiz_results_mr_store_filters($form_state);
  }
  $form = array();
  $pre = 'quiz_results_mr_';
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'quiz') . '/templates/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;
  }

  // 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'), current_path(), 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',
      'header',
      'filters',
      'best_results',
    ),
    '#default_value' => $_SESSION[$pre . 'best_results'],
    '#ajax' => array(
      'callback' => 'quiz_browser_body_callback',
      'effect' => 'slide',
      'wrapper' => 'quiz-browser-body',
      'method' => 'replace',
    ),
  );
  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',
      'header',
      'filters',
      'not_in_progress',
    ),
    '#default_value' => $_SESSION[$pre . 'not_in_progress'],
    '#ajax' => array(
      'callback' => 'quiz_browser_body_callback',
      'effect' => 'slide',
      'wrapper' => 'quiz-browser-body',
      'method' => 'replace',
    ),
  );
  $form['table'] = array(
    '#theme' => 'quiz_browser',
    '#tree' => TRUE,
  );
  $browser =& $form['table'];

  // 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:
  $browser['header'] = array(
    '#theme' => 'quiz_results_browser_header',
  );

  // Browsers table header
  $browser['header']['#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',
    ),
  );
  _quiz_results_mr_add_filter_fields($browser['header'], $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']);
  }
  $browser['body'] = array(
    '#theme' => 'quiz_results_browser_body',
  );
  $res = _quiz_results_mr_data_provider($browser['header']['#header'], $quiz);

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

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

    // build options array for checkboxes for item
    if (empty($res_o->name)) {
      if ($res_o->uid == '0') {
        $options[$id] = \Drupal::config('quiz.settings')
          ->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['body']['hover_menu'][$id]['#value'] = _quiz_results_mr_get_hover($quiz, $res_o->result_id);

    // Add data for the table columns
    $browser['body']['started'][$id]['#value'] = format_date($res_o->started, 'short');
    $browser['body']['finished'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : format_date($res_o->finished, 'short');
    $browser['body']['duration'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : _quiz_format_duration($res_o->duration);
    $browser['body']['score'][$id]['#value'] = $res_o->finished == 0 ? t('In progress') : check_plain($res_o->score);
    $browser['body']['evaluated'][$id]['#value'] = $res_o->evaluated == 0 ? t('No') : t('Yes');
    $browser['body']['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
      ->id() . '-' . (isset($_GET['del']) ? $_GET['del'] : ''),
  );
  unset($form_state['storage']['del']);
  $browser['body']['name'] = array(
    '#title' => t('Name'),
    '#type' => 'checkboxes',
    '#options' => $options,
    '#attributes' => array(
      'class' => array(
        'quiz-browser-checkbox',
      ),
    ),
    '#default_value' => $default_value,
  );
  $form['pager'] = array(
    '#markup' => '<DIV ID ="browser-pager">' . theme('pager', array(
      'tags' => NULL,
    )) . '</DIV>',
  );
  $form['#submit'][] = 'quiz_results_mr_form_submit';
  return $form;
}