You are here

class DragDropQuestion in Quiz Drag Drop 7

Extension of QuizQuestion.

Hierarchy

Expanded class hierarchy of DragDropQuestion

1 string reference to 'DragDropQuestion'
quiz_drag_drop_quiz_question_info in ./quiz_drag_drop.module
Implements hook_quiz_question_info().

File

./quiz_drag_drop.classes.inc, line 18
Question type, enabling the creation of drag drop type of questions.

View source
class DragDropQuestion extends QuizQuestion {

  /**
   * Run check_markup() on the field of the specified choice alternative.
   *
   * @param int $alternativeIndex
   *   The index of the alternative in the alternatives array.
   * @param varchar $field
   *   The name of the field we want to check markup on.
   * @param Boolean $check_user_access
   *   Whether or not to check for user access to the filter we're trying to
   * apply.
   *
   * @return HTML
   *   HTML markup.
   */
  private function checkMarkup($alternativeIndex, $field, $check_user_access = FALSE) {
    $alternative = $this->node->alternatives[$alternativeIndex];
    return check_markup($alternative[$field]['value'], $alternative[$field]['format']);
  }

  /**
   * Implementation of save.
   *
   * Stores the question in the database.
   *
   * @param Boolean $is_new
   *   if - if the node is a new node...
   * (non-PHPdoc)
   *
   * @see sites/all/modules/quiz-HEAD/question_types/quiz_question/QuizQuestion#save()
   */
  public function saveNodeProperties($is_new = FALSE) {

    // TODO.
  }

  /**
   * Implementation of validate.
   *
   * QuizQuestion#validate()
   */
  public function validateNode(array &$form) {

    // For node level validation.
    $title_error = FALSE;
    $file_upload_delta = $form['field_dragdrop_image'][LANGUAGE_NONE]['#file_upload_delta'];
    if ($file_upload_delta > 0) {
      for ($i = 0; $i < $file_upload_delta; $i++) {
        if ($form['field_dragdrop_image'][LANGUAGE_NONE][$i]['#value']['title'] == '') {
          $title_error = TRUE;
        }
      }
    }
    if ($title_error === TRUE) {
      form_set_error(t('image_uploaded_title'), t('Please provide title for all the images uploaded. All the titles will become placeholders.'));
    }
  }

  /**
   * Implementation of delete.
   *
   * @see QuizQuestion#delete()
   */
  public function delete($only_this_version = FALSE) {
    if ($only_this_version) {
      db_delete('quiz_drag_drop_user_answers')
        ->condition('question_nid', $this->node->nid)
        ->condition('question_vid', $this->node->vid)
        ->execute();
    }
    else {
      db_delete('quiz_drag_drop_user_answers')
        ->condition('question_nid', $this->node->nid)
        ->execute();
    }
    parent::delete($only_this_version);
  }

  /**
   * Implementation of getNodeProperties.
   *
   * @see QuizQuestion#getNodeProperties()
   */
  public function getNodeProperties() {
    if (isset($this->nodeProperties) && !empty($this->nodeProperties)) {
      return $this->nodeProperties;
    }
    $props = parent::getNodeProperties();
    return $props;
  }

  /**
   * Implementation of getNodeView.
   *
   * @see QuizQuestion#getNodeView()
   */
  public function getNodeView() {
    $content = parent::getNodeView();
    return $content;
  }

  /**
   * Generates the question form.
   *
   * This is called whenever a question is rendered, either
   * to an administrator or to a quiz taker.
   */
  public function getAnsweringForm(array $form_state = NULL, $rid) {
    $form = parent::getAnsweringForm($form_state, $rid);
    list($matches, $images) = $this
      ->getSubquestions();
    $matches = $this
      ->customShuffle($matches);
    $images = $this
      ->customShuffle($images);
    $data = array(
      'placeholder' => $matches,
      'images' => $images,
    );
    $form['drag_drop_answer'] = array(
      '#markup' => theme('quiz_drag_drop_answer_form', array(
        'data' => $data,
      )),
    );
    $form['answerCount'] = array(
      '#type' => 'hidden',
      '#value' => '',
      '#name' => 'answerCount',
      '#attributes' => array(
        'id' => 'answerCount',
      ),
    );
    $form['dropCount'] = array(
      '#type' => 'hidden',
      '#value' => '',
      '#name' => 'dropCount',
      '#attributes' => array(
        'id' => 'dropCount',
      ),
    );
    $form['tries'] = array(
      '#type' => 'hidden',
      '#value' => 0,
    );
    $form['reset'] = array(
      '#type' => 'button',
      '#value' => t('Reset'),
      '#attributes' => array(
        'class' => array(
          'reset_btn',
        ),
      ),
      '#id' => 'btnReset',
    );
    return $form;
  }

  /**
   * Helper function to generate question images.
   *
   * @param array $variables
   *   Holds image related settings.
   *
   * @return HTML
   *   HTML of image.
   */
  private function imageStyle($variables) {

    // Determine the dimensions of the styled image.
    $dimensions = array(
      'width' => '',
      'height' => 70,
    );
    image_style_transform_dimensions($variables['style_name'], $dimensions);
    $variables['width'] = $dimensions['width'];
    $variables['height'] = $dimensions['height'];
    $variables['attributes'] = array(
      'class' => 'draggable',
      'id' => 'image_' . $variables['fid'],
    );

    // Determine the url for the styled image.
    $variables['path'] = image_style_url($variables['style_name'], $variables['path']);
    return theme('image', $variables);
  }

  /**
   * Helper function to fetch subquestions.
   *
   * @return array
   *   Array with two arrays, matches and selected options
   */
  private function getSubquestions() {
    $title = $image = array();
    $node_detail = node_load($this->node->nid);
    foreach ($node_detail->field_dragdrop_image[LANGUAGE_NONE] as $key => $val) {
      $style = 'thumbnail';
      $path = $val['uri'];
      $fid = $val['fid'];
      $data = array(
        'style_name' => $style,
        'path' => $path,
        'fid' => $fid,
      );
      $title[] = array(
        'fid' => $val['fid'],
        'title' => $val['title'],
        'uri' => $this
          ->imageStyle($data),
      );
      $image[] = array(
        'fid' => $val['fid'],
        'title' => $val['title'],
        'uri' => $this
          ->imageStyle($data),
      );
    }
    return array(
      $title,
      $image,
    );
  }

  /**
   * Shuffles an array and makes sure the first element is the default element.
   *
   * @param array $array
   *   Array to be shuffled
   *
   * @return array
   *   A shuffled version of the array with $array['def'] = '' as the first
   * element.
   */
  private function customShuffle(array $array = array()) {
    $new_array = array();
    while (count($array)) {
      $element = array_rand($array);
      $new_array[$element] = $array[$element];
      unset($array[$element]);
    }
    return $new_array;
  }

  /**
   * Implementation of getCreationForm.
   *
   * @see QuizQuestion#getCreationForm()
   */
  public function getCreationForm(array &$form_state = NULL) {
    $form = array();
    return $form;
  }

  /**
   * Implementation of getMaximumScore.
   *
   * @see QuizQuestion#getMaximumScore()
   */
  public function getMaximumScore() {
    return 1;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DragDropQuestion::checkMarkup private function Run check_markup() on the field of the specified choice alternative.
DragDropQuestion::customShuffle private function Shuffles an array and makes sure the first element is the default element.
DragDropQuestion::delete public function Implementation of delete.
DragDropQuestion::getAnsweringForm public function Generates the question form.
DragDropQuestion::getCreationForm public function Implementation of getCreationForm.
DragDropQuestion::getMaximumScore public function Implementation of getMaximumScore.
DragDropQuestion::getNodeProperties public function Implementation of getNodeProperties.
DragDropQuestion::getNodeView public function Implementation of getNodeView.
DragDropQuestion::getSubquestions private function Helper function to fetch subquestions.
DragDropQuestion::imageStyle private function Helper function to generate question images.
DragDropQuestion::saveNodeProperties public function Implementation of save.
DragDropQuestion::validateNode public function Implementation of validate.