You are here

quizfileupload.module in Quiz File Upload 7.4

The main file for quizfileupload.

File

quizfileupload.module
View source
<?php

/**
 * The main file for quizfileupload.
 *
 * @file
 */

/**
 * define default allowed extensions
 */
define('QUIZFILEUPLOAD_DEFAULT_EXTENSIONS', 'png pdf odf doc docx');

/**
 * Implements hook_help().
 */
function quizfileupload_help($path, $args) {
  if ($path == 'admin/help#quizfileupload') {
    return t('This module provides a fileupload choice question type for Quiz.');
  }
}

/**
 * Implements hook_quiz_question_info().
 */
function quizfileupload_quiz_question_info() {
  return array(
    'quizfileupload' => array(
      'name' => t('Fileupload question'),
      'description' => t('This provides fileupload questions for use by the Quiz module.'),
      'question provider' => 'QuizfileuploadQuestion',
      'response provider' => 'QuizfileuploadResponse',
      'module' => 'quiz_question',
    ),
  );
}
function quizfileupload_config() {
  $form['quizfileupload_default_score'] = array(
    '#type' => 'textfield',
    '#title' => t('Default score'),
    '#required' => TRUE,
    '#default_value' => variable_get('quizfileupload_default_score', 1),
  );
  $form['quizfileupload_default_extensions'] = array(
    '#type' => 'textarea',
    '#title' => t('Allowed extension'),
    '#description' => t('Enter the allowed file extensions one per line.'),
    '#default_value' => variable_get('quizfileupload_default_extensions', QUIZFILEUPLOAD_DEFAULT_EXTENSIONS),
    '#required' => TRUE,
  );
  $form['quizfileupload_upload_location'] = array(
    '#type' => 'select',
    '#title' => t('Upload location'),
    '#required' => TRUE,
    '#options' => drupal_map_assoc(array_keys(file_get_stream_wrappers())),
    '#default_value' => variable_get('quizfileupload_upload_location', 'public'),
  );
  $form['#validate'][] = 'quizfileupload_config_validate';
  return $form;
}

/**
 * Validate the long_answer config form values
 */
function quizfileupload_config_validate($form, $form_state) {
  if ($form_state['values']['quizfileupload_default_score'] <= 0) {
    form_set_error('quizfileupload_default_score', t('The default max score must be greater than 0'));
  }
}

/**
 * Fork of file_validate_extensions().
 */
function quizfileupload_validate_extensions($file, $extensions) {
  $errors = array();
  $regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
  if (!preg_match($regex, $file->filename)) {
    $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array(
      '%files-allowed' => $extensions,
    ));
  }
  return $errors;
}

/**
 * Validate the result report for short answer
 */
function quizfileupload_report_validate($values, $form_key) {

  // Check to make sure that entered score is not higher than max allowed score.
  if (!_quiz_is_int($values['score'], 0, (int) $values['max_score'])) {
    form_set_error($form_key . '][score', t('The score needs to be a number between 0 and @max', array(
      '@max' => (int) $values['max_score'],
    )));
  }
}

/**
 * Submit the result report for short answer
 */
function quizfileupload_report_submit($values) {
  quizfileupload_score_an_answer($values, FALSE);
}
function quizfileupload_score_an_answer($values, $update_total = TRUE) {
  extract($values);

  // When we set the score we make sure that the max score in the quiz the question belongs to is considered
  $question_max_score = db_query('SELECT max_score FROM {quiz_question_properties} WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $nid,
    ':vid' => $vid,
  ))
    ->FetchField();
  $quiz_max_score = db_query('SELECT max_score FROM {quiz_node_relationship} WHERE parent_vid = :pvid AND child_vid = :cvid', array(
    ':pvid' => $quiz->vid,
    ':cvid' => $vid,
  ))
    ->fetchField();
  $db_update_fields = array(
    'score' => $score * $question_max_score / $quiz_max_score,
    'is_evaluated' => 1,
  );
  if (isset($answer_feedback)) {
    $db_update_fields['answer_feedback'] = empty($answer_feedback['value']) ? '' : $answer_feedback['value'];
    $db_update_fields['answer_feedback_format'] = $answer_feedback['format'];
  }
  $changed = db_update('quiz_fileupload_user_answers')
    ->fields($db_update_fields)
    ->condition('question_nid', $nid)
    ->condition('question_vid', $vid)
    ->condition('result_id', $rid)
    ->execute();

  // Now the short answer user data has been updated. We also need to update the data in the quiz tables
  if ($changed > 0) {
    $max = db_query('SELECT max_score FROM {quiz_question_properties} WHERE vid = :vid', array(
      ':vid' => $vid,
    ))
      ->fetchField();
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score / $max > 0.5 ? 1 : 0;
      $points_awarded = $score;
    }
    db_update('quiz_node_results_answers')
      ->fields(array(
      'points_awarded' => $points_awarded,
      'is_correct' => $is_correct,
    ))
      ->condition('question_vid', $vid)
      ->condition('result_id', $rid)
      ->execute();

    // Third, we update the main quiz results table
    if ($update_total) {
      quiz_update_total_score($quiz, $rid);
    }
  }
  return $changed;
}

Functions

Namesort descending Description
quizfileupload_config
quizfileupload_config_validate Validate the long_answer config form values
quizfileupload_help Implements hook_help().
quizfileupload_quiz_question_info Implements hook_quiz_question_info().
quizfileupload_report_submit Submit the result report for short answer
quizfileupload_report_validate Validate the result report for short answer
quizfileupload_score_an_answer
quizfileupload_validate_extensions Fork of file_validate_extensions().

Constants

Namesort descending Description
QUIZFILEUPLOAD_DEFAULT_EXTENSIONS define default allowed extensions