View source
<?php
function cloze_help($path, $args) {
if ($path == 'admin/help#cloze') {
return t('This module provides a cloze question type for Quiz.');
}
}
function cloze_quiz_question_info() {
return array(
'cloze' => array(
'name' => t('Cloze question'),
'description' => t('Quiz questions that allow a user to fill-in the missing words.'),
'question provider' => 'ClozeQuestion',
'response provider' => 'ClozeResponse',
'module' => 'quiz_question',
),
);
}
function cloze_config() {
return FALSE;
}
function cloze_autoload_info() {
return array(
'ClozeQuestion' => array(
'file' => 'cloze.classes.inc',
),
'ClozeResponse' => array(
'file' => 'cloze.classes.inc',
),
);
}
function cloze_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && 'cloze_node_form' == $form_id) {
$form['body_field']['format'] = NULL;
}
}
function cloze_theme() {
$path = drupal_get_path('module', 'cloze') . '/theme';
return array(
'cloze_response_form' => array(
'arguments' => array(
'form' => NULL,
),
'path' => $path,
'file' => 'cloze.theme.inc',
),
'cloze_user_answer' => array(
'arguments' => array(
'answer' => NULL,
'correct' => NULL,
),
'path' => $path,
'file' => 'cloze.theme.inc',
),
'cloze_answering_form' => array(
'arguments' => array(
'form' => NULL,
),
'path' => $path,
'file' => 'cloze.theme.inc',
),
);
}
function _cloze_shuffle_choices($choices) {
$new_array = array();
$new_array[''] = '';
while (count($choices)) {
$element = array_rand($choices);
$new_array[$element] = $choices[$element];
unset($choices[$element]);
}
return $new_array;
}
function _cloze_get_question_chunks($question) {
$chunks = array();
while (strlen($question) > 0) {
$match = FALSE;
$pos = strpos($question, '[');
if ($pos) {
$substring = substr($question, 0, $pos - 1);
$question = str_replace($substring, '', $question);
$chunks[] = $substring;
$match = TRUE;
}
$pos = strpos($question, ']');
if ($pos !== FALSE) {
$substring = substr($question, 0, $pos + 1);
$question = str_replace($substring, '', $question);
$chunks[] = $substring;
$match = TRUE;
}
if (!$match) {
$chunks[] = $question;
$question = '';
}
}
return $chunks;
}
function _cloze_get_correct_answer_chunks($question) {
$correct_answer = array();
$chunks = _cloze_get_question_chunks($question);
foreach ($chunks as $key => $value) {
if (strpos($value, '[') === FALSE) {
continue;
}
else {
$answer_chunk = str_replace(array(
'[',
']',
), '', $value);
$choice = explode(',', $answer_chunk);
if (count($choice) == 1) {
$correct_answer[$key] = $answer_chunk;
}
else {
$correct_answer[$key] = $choice[0];
}
}
}
return $correct_answer;
}
function _cloze_get_correct_answer($question) {
$output = '';
$chunks = _cloze_get_question_chunks($question);
$answer = _cloze_get_correct_answer_chunks($question);
$correct_answer = array();
foreach ($chunks as $key => $chunk) {
if (isset($answer[$key])) {
$correct_answer[] = '<span class="answer correct correct-answer">' . $answer[$key] . '</span>';
}
else {
$correct_answer[] = $chunk;
}
}
$output = implode(' ', $correct_answer);
return str_replace("\n", "<br/>", $output);
}
function _cloze_get_user_answer($question, $answer) {
$output = '';
$user_answer = $chunks = _cloze_get_question_chunks($question);
$correct_answer_chunks = _cloze_get_correct_answer_chunks($question);
foreach ($chunks as $key => $value) {
if (isset($answer[$key]) && !empty($answer[$key])) {
$class = drupal_strtolower($correct_answer_chunks[$key]) == drupal_strtolower($answer[$key]) ? 'correct' : 'incorrect';
$class .= ' answer user-answer';
$user_answer[$key] = '<span class="' . $class . '">' . $answer[$key] . '</span>';
}
elseif (isset($answer[$key])) {
$user_answer[$key] = '<span class="incorrect answer user-answer">' . str_repeat('_', strlen($correct_answer_chunks[$key])) . '</span>';
}
}
$output = implode(' ', $user_answer);
return str_replace("\n", "<br/>", $output);
}