View source
<?php
module_load_include('inc', 'quiz', 'quiz.admin');
function qcollection_items($node) {
$path = drupal_get_path('module', 'quiz') . '/quiz.admin.js';
drupal_add_js($path, 'module');
return drupal_get_form('qcollection_items_form', $node);
}
function qcollection_items_form($context, $quiz) {
$form['ahah_target'] = array(
'#type' => 'markup',
'#value' => '<div id="questions-always-target"></div>',
);
$form['additional_questions'] = array(
'#type' => 'fieldset',
'#title' => t('Add new question'),
'#theme' => 'additional_questions',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$types = _quiz_get_question_types();
foreach ($types as $type => $info) {
$url_type = str_replace('_', '-', $type);
$form['additional_questions'][$type] = array(
'#type' => 'markup',
'#value' => '<div class="add-questions">' . l(t($info['name']), 'node/add/' . $url_type . '/' . $quiz->nid, array(
'title' => t('Go to @name administration', array(
'@name' => $info['name'],
)),
)) . '</div>',
);
}
$form['filtered_question_list_always'] = array(
'#type' => 'fieldset',
'#title' => t('Items in collection'),
'#theme' => 'question_selection_table',
'question_status' => array(
'#tree' => TRUE,
),
);
$form['filtered_question_list_always']['remove_from_quiz'] = array(
'#type' => 'hidden',
'#default_value' => '',
);
$form['add_existing_question'] = array(
'#type' => 'fieldset',
'#title' => '<strong>' . t('Find and add a question') . '</strong>',
'#description' => t('Begin typing a question title or keyword. Suggestions will be presented based on your typing. ') . '<strong>' . t('You must choose one of the suggested questions.') . '</strong> ' . t('To add a new question, expand the "Add new question" section at top.'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['add_existing_question']['always_autocomplete'] = array(
'#type' => 'textfield',
'#default_value' => '',
'#size' => 60,
'#maxlength' => 256,
'#required' => FALSE,
'#autocomplete_path' => 'admin/quiz/listquestions',
);
$form['add_existing_question']['add_to_list'] = array(
'#type' => 'submit',
'#value' => t('Add to quiz'),
'#submit' => 'quiz_questions_form_submit',
'#ahah' => array(
'path' => 'admin/quiz/newquestion',
'wrapper' => 'questions-always-target',
'progress' => array(
'type' => 'bar',
'message' => t('Adding question...'),
),
),
);
$questions = _quiz_get_questions($quiz->nid, $quiz->vid);
$rows = array();
$form['filtered_question_list_random']['weights'] = array(
'#tree' => TRUE,
);
$form['filtered_question_list_always']['weights'] = array(
'#tree' => TRUE,
);
foreach ($questions as $question) {
$id_mod = $question->question_status == QUESTION_RANDOM ? 'random' : 'always';
$fieldset = 'filtered_question_list_' . $id_mod;
$id = $id_mod . '-' . $question->nid;
$form[$fieldset]['weights'][$id] = array(
'#type' => 'textfield',
'#size' => 3,
'#maxlength' => 4,
'#default_value' => isset($question->weight) ? $question->weight : 0,
);
$form[$fieldset]['titles'][$id] = array(
'#value' => $question->title,
);
$form[$fieldset]['types'][$id] = array(
'#value' => $question->type,
);
$form[$fieldset]['view_links'][$id] = array(
'#value' => l('View', 'node/' . $question->nid),
);
$form[$fieldset]['remove_links'][$id] = array(
'#value' => '<a href="#" class="rem-link">' . t('Remove') . '</a>',
);
}
$always_count = isset($form['filtered_question_list_always']['titles']) ? count($form['filtered_question_list_always']['titles']) : 0;
$form['filtered_question_list_always']['#title'] .= ' (' . $always_count . ')';
$form['new_revision'] = array(
'#type' => 'checkbox',
'#default_value' => in_array('revision', variable_get('node_options_quiz', array())),
'#title' => t('New revision'),
'#description' => t('Allow question status changes to create a new revision of the collection?'),
);
$form['timestamp'] = array(
'#type' => 'hidden',
'#value' => time(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit questions'),
);
return $form;
}
function qcollection_questions_form_validate($form, $form_state) {
quiz_questions_form_validate($form, $form_state);
}
function qcollection_questions_form_submit($form, &$form_state) {
$quiz = node_load(arg(1));
$is_new_revision = (bool) $form_state['values']['new_revision'];
$removed = explode(',', $form_state['values']['remove_from_quiz']);
$weight_map = $form_state['values']['weights'];
_quiz_update_items($quiz, $weight_map, $removed, $is_new_revision);
}
function qcollection_export_quiz($collection_node) {
global $user;
$quiz_node = new stdClass();
$quiz_node->type = 'quiz';
$quiz_node->title = t("@quiz of '@qcollection_title' by @user at @unix_time", array(
'@quiz' => QUIZ_NAME,
'@qcollection_title' => $collection_node->title,
'@user' => $user->name,
'@unix_time' => time(),
));
$quiz_node->teaser = $node->body = $mq->questiontext;
$quiz_node->uid = $user->uid;
$quiz_node->status = 0;
$quiz_node->log = "Autogenerated from item collection nid:{$collection_node->nid},vid:{$collection_node->vid}";
$quiz_node->pass_rate = variable_get('quiz_default_pass_rate', 75);
node_save($quiz_node);
$sql = 'INSERT INTO {quiz_node_relationship} (parent_nid, parent_vid, child_nid, child_vid, question_status, weight)
SELECT %d as parent_nid, %d AS parent_vid, child_nid, child_vid, question_status, weight FROM {quiz_node_relationship} qnr
WHERE qnr.parent_nid = %d and qnr.parent_vid = %d';
db_query($sql, $quiz_node->nid, $quiz_node->vid, $collection_node->nid, $collection_node->vid);
$new_nid = $quiz_node->nid;
drupal_goto('node/' . $new_nid . '/edit');
}
function qcollection_download($node) {
return drupal_get_form('qcollection_download_form', $node);
}
function qcollection_download_form() {
module_load_include('inc', 'questions_export', 'questions_export.admin');
$form['exporter'] = array(
'#type' => 'select',
'#title' => t('Export format'),
'#options' => _questions_exporters(),
'#description' => t('Select the data format to export into.'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Download'),
);
return $form;
}
function qcollection_download_form_submit(&$form, &$form_state) {
$collection_node = $form['#parameters'][2];
$exporter = $form_state['values']['exporter'];
_questions_export_download($collection_node, $exporter);
}