You are here

quizfileupload.classes.inc in Quiz File Upload 6

The main classes for the multichoice question type.

These inherit or implement code found in quiz_question.classes.inc. Code: LogicMedia

Based on: Other question types in the quiz framework.

Question type, enabling the creation of multiple choice and multiple answer questions.

File

quizfileupload.classes.inc
View source
<?php

/**
 * The main classes for the multichoice question type.
 *
 * These inherit or implement code found in quiz_question.classes.inc.
 * Code: LogicMedia
 *
 * Based on:
 * Other question types in the quiz framework.
 *
 *
 *
 * @file
 * Question type, enabling the creation of multiple choice and multiple answer questions.
 */

/**
 * Extension of QuizQuestion.
 */
class QuizfileuploadQuestion extends QuizQuestion {

  /**
   * Implementation of save
   *
   * Stores the question in the database.
   *
   * @param 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) {
    $is_new = $is_new || $this->node->revision == 1;
    if ($is_new) {
      $sql = 'INSERT INTO {quiz_fileupload_node_properties} (nid, vid, filetypes) VALUES (%d, %d, "%s")';
      db_query($sql, $this->node->nid, $this->node->vid, $this->node->filetypes);
    }
    else {
      $sql = 'UPDATE {quiz_fileupload_node_properties} SET filetypes = "%s" WHERE nid = %d AND vid = %d';
      db_query($sql, $this->node->filetypes, $this->node->nid, $this->node->vid);
    }
  }

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

    //no validation required
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestion#delete()
   */
  public function delete($only_this_version = FALSE) {
    $delete_properties = 'DELETE FROM {quiz_fileupload_node_properties} WHERE nid = %d';
    $delete_answers = 'DELETE FROM {quiz_fileupload_user_answers} WHERE question_nid = %d';
    if ($only_this_version) {
      $delete_properties .= ' AND vid = %d';
      $delete_answers .= ' AND question_vid = %d';
      $delete_results .= ' AND question_vid = %d';
    }
    db_query($delete_properties, $this->node->nid, $this->node->vid);
    db_query($delete_answers, $this->node->nid, $this->node->vid);
    db_query($delete_results, $this->node->nid, $this->node->vid);
    parent::delete($only_this_version);
  }

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

    // Load the properties
    $sql = 'SELECT filetypes FROM {quiz_fileupload_node_properties} WHERE nid = %d AND vid = %d';
    $res = db_query($sql, $this->node->nid, $this->node->vid);
    $res_a = db_fetch_array($res);
    if (is_array($res_a)) {
      $props = array_merge($props, $res_a);
    }
    $this->nodeProperties = $props;
    return $props;
  }

  /**
   * Implementation of getNodeView
   *
   * @see QuizQuestion#getNodeView()
   */
  public function getNodeView() {
    $content = parent::getNodeView();
    $content['filetypes'] = array(
      '#type' => 'markup',
      '#value' => '<pre>' . check_plain($this->node->filetypes) . '</pre>',
    );
    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);
    $form['#attributes'] = array(
      'enctype' => 'multipart/form-data',
    );
    $file = db_fetch_object(db_query('SELECT filename, filepath FROM {files} f
      INNER JOIN {quiz_fileupload_user_answers} qf ON (f.fid = qf.fid)
      WHERE question_nid = %d AND question_vid = %d AND result_id = %d', $this->node->nid, $this->node->vid, $this->node->rid));
    if (is_object($file)) {
      $form['previous_upload'] = array(
        '#title' => t('Previous upload'),
        '#type' => 'item',
        '#value' => is_object($file) ? l($file->filename, file_create_url($file->filepath)) : t('n/a'),
        '#description' => t('<strong>Upload a new file to replace previous upload.</strong>'),
      );
    }
    $form['tries'] = array(
      '#type' => 'file',
      '#title' => t('Upload'),
      '#description' => t('Allowed extensions !ext', array(
        '!ext' => $this->node->filetypes,
      )),
    );
    return $form;
  }

  /**
   * Implementation of getCreationForm
   *
   * @see QuizQuestion#getCreationForm()
   */
  public function getCreationForm(array $form_state = NULL) {
    $allowed = variable_get('quizfileupload_default_extensions', QUIZFILEUPLOAD_DEFAULT_EXTENSIONS);
    $form['filetypes'] = array(
      '#type' => 'textfield',
      '#title' => t('Allowed extension'),
      '#description' => t('Enter the allowed file extensions one per line.'),
      '#default_value' => isset($this->node->filetypes) ? $this->node->filetypes : $allowed,
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * Implementation of getMaximumScore
   *
   * @see QuizQuestion#getMaximumScore()
   */
  public function getMaximumScore() {
    return variable_get('quizfileupload_default_score', 0);
  }

}

/**
 * Extension of QuizQuestionResponse
 */
class QuizfileuploadResponse extends QuizQuestionResponse {

  /**
   * Constructor
   */
  public function __construct($result_id, stdClass $question_node, $tries = NULL) {
    $tries = $_FILES;
    $this->answer = $tries;
    $result->is_correct = TRUE;
    $this->question->score_weight = NULL;
    parent::__construct($result_id, $question_node, $tries);
    if (!isset($answer)) {

      // Question has been answered allready. We fetch the answer data from the database.
      $sql = 'SELECT * FROM {quiz_fileupload_user_answers}
        WHERE question_nid = %d AND question_vid = %d AND result_id = %d';
      $r = db_fetch_object(db_query($sql, $question_node->nid, $question_node->vid, $result_id));
      if (!empty($r)) {
        $this->score = $r->score;
        $this->evaluated = $r->is_evaluated;
        $this->answer_id = $r->answer_id;
      }
    }
    else {
      $this->score = variable_get('quizfileupload_default_score', 0);
      $this->evaluated = TRUE;
    }
  }

  /**
   * Implementation of isValid
   *
   * @see QuizQuestionResponse#isValid()
   */
  public function isValid() {
    $dir = variable_get('file_directory_path', 'sites/default/files');
    $is_writable = file_check_directory($dir, 1);
    if (!$is_writable) {
      return t('File system path is not writable. Contact administrator to check settings at url /admin/settings/file-system');
    }
    if (isset($this->answer['files']['type']['tries']) && empty($this->answer['files']['type']['tries'])) {
      $file = db_fetch_object(db_query('SELECT filename, filepath FROM {files} f
        INNER JOIN {quiz_fileupload_user_answers} qf ON (f.fid = qf.fid)
        WHERE answer_id = %d', $this->answer_id));
      if (is_object($file)) {

        // Don't throw error as the file has been already attached.
        return TRUE;
      }
      else {
        return t('Please upload a valid file.');
      }
    }
    if (isset($this->answer['files']['type']['tries']) && !empty($this->answer['files']['type']['tries'])) {
      $file = file_save_upload('tries', array(), file_directory_path());
      if (!$file) {
        form_set_error('field', 'Error uploading file.');
        return;
      }
      $message = quizfileupload_validate_extensions($file, $this->question->filetypes);
      return empty($message) ? TRUE : $message[0];
    }
    return FALSE;
  }

  /**
   * Implementation of save
   *
   * @see QuizQuestionResponse#save()
   */
  public function save() {
    if ($this
      ->isValid() !== TRUE) {
      return;
    }
    $file = file_save_upload('tries', array(), file_directory_path());
    file_set_status($file, FILE_STATUS_PERMANENT);
    $sql = 'INSERT INTO {quiz_fileupload_user_answers}
          (result_id, question_vid, question_nid, fid, score)
          VALUES (%d, %d, %d, "%s", %d)';
    db_query($sql, $this->rid, $this->question->vid, $this->question->nid, $file->fid, $this
      ->score());
    $this->answer_id = db_last_insert_id('quiz_fileupload_user_answers', 'answer_id');
  }

  /**
   * Implementation of delete
   *
   * @see QuizQuestionResponse#delete()
   */
  public function delete() {
    $sql = 'DELETE FROM {quiz_fileupload_user_answers} WHERE result_id = %d AND question_nid = %d AND question_vid = %d';
    db_query($sql, $this->rid, $this->question->nid, $this->question->vid);
  }

  /**
   * Implementation of score
   *
   * @return uint
   *
   * @see QuizQuestionResponse#score()
   */
  public function score() {
    return variable_get('quizfileupload_default_score', 0);
  }

  /**
   * Implementation of getResponse
   *
   * @return answer
   *
   * @see QuizQuestionResponse#getResponse()
   */
  public function getResponse() {
    return $this->answer;
  }

  /**
   * Implementation of getReportFormResponse
   *
   * @see getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
   */
  public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
    $answer_id = $this->answer_id;
    $file = db_fetch_object(db_query('SELECT filename, filepath FROM {files} f
      INNER JOIN {quiz_fileupload_user_answers} qf ON (f.fid = qf.fid)
      WHERE answer_id = %d', $answer_id));

    // Return themed report
    return array(
      '#type' => 'markup',
      '#value' => is_object($file) ? l($file->filename, file_create_url($file->filepath)) : t('n/a'),
    );
  }

}

Classes

Namesort descending Description
QuizfileuploadQuestion Extension of QuizQuestion.
QuizfileuploadResponse Extension of QuizQuestionResponse