'node/add/multichoice', 'title' => t('multichoice'), 'access' => user_access('administer multichoice')); } return $items; } function multichoice_form(&$node) { $output = ''; if (function_exists('taxonomy_node_form')) { $output .= implode('', taxonomy_node_form('multichoice', $node)); } $output .= form_textarea(t('multichoice'), 'body', $node->body, 60, 5, '', NULL, TRUE); $output .= filter_form('format', $node->format); $output .= form_checkbox(t('Multiple answers'), 'multiple', 1, $node->multiple); $rows = array(); $answers = $node->answers; for ($i = 0; $i < $node->rows; $i++) { $rows[] = array( form_checkbox('', 'answers]['. $i .'][0', 1, $answers[$i][0]), form_textarea('', 'answers]['. $i .'][1', $answers[$i][1], 20, 5), form_textarea('', 'answers]['. $i .'][2', $answers[$i][2], 20, 5)); } $header = array( array('data' => t('Correct')), array('data' => t('Answer'), 'style' => 'width:250px;'), array('data' => t('Feedback'), 'style' => 'width:250px;')); $output .= theme('table', $header, $rows); $output .= form_checkbox(t('I need more answers'), 'more'); return $output; } function multichoice_validate(&$node) { if (!$node->rows) { $node->rows = $node->answers ? count($node->answers) : 5; } if ($node->more) { $node->rows += 5; } if (!$node->nid && empty($_POST)) return; if (!$node->body) { form_set_error('body', t('multichoice is empty')); } $corrects = 0; foreach ($node->answers as $key => $answer) { if ($answer[0]) { if ($corrects && !$node->multiple) { form_set_error('multiple', t('Single choice yet multiple correct answers are present')); } $corrects++; } if (empty($answer[0]) && empty($answer[1]) && empty($answer[2])) { unset($node->answers[$key]); } } /* nincs valasz, de van mas illetve correctnek van jelolve de nincs valasz */ if (!$corrects) { // untested form_set_error('answers][0][0', t('No correct choice(s)')); } if (!count($node->answers)) { // untested form_set_error('answers][0][0', t('No answers.')); } } function multichoice_insert($node) { db_query("INSERT INTO multichoice (nid, multiple_answers, answers) VALUES (%d, %d, '%s')", $node->nid, $node->multiple, serialize($node->answers)); } function multichoice_update($node) { db_query("UPDATE multichoice SET multiple_answers = %d, answers = '%s' WHERE nid = %d", $node->multiple, serialize($node->answers), $node->nid); } function multichoice_load($node) { $additions = db_fetch_object(db_query('SELECT * FROM multichoice WHERE nid = %d', $node->nid)); $additions->answers = unserialize($additions->answers); return $additions; } function multichoice_view(&$node, $teaser = FALSE, $page = FALSE) { if (user_access('administer multichoice')) { if (!$teaser) { $submit_message = t('Submit'); if ($_POST['quiz_op'] == $submit_message) { $points = multichoice_evaluate_question($node); } $question = multichoice_render_question($node); $question .= form_submit($submit_message, 'quiz_op'); $node->body = form($question); if (isset($points)) { $node->body .= ($points ? 1 : 0) .' point'; } } } else if ($teaser) { $node->teaser = t('This is a quiz question, not to be viewed independently.'); $node->body = $node->teaser; // we do not need Read more... } else { drupal_access_denied(); } } function multichoice_render_question($node) { $rows = array(); $submit_message = t('Submit'); foreach ($node->answers as $key => $answer) { $rows[] = array(form_checkbox('', 'tries]['. $key, 1), $answer[1]); } $header = header('', ''); $form = theme('table', $header, $rows); return $node->body . $form; } function multichoice_evaluate_question($node) { $points = 0; foreach ($_POST['edit']['tries'] as $key => $try) { $points += ($try == $node->answers[$key][0]); } return ($points == count($node->answers)); } function multichoice_list_questions() { $result = db_query("SELECT nid, body, format FROM node WHERE type='%s'", 'multichoice'); $questions = array(); while ($node = db_fetch_object($result)) { $question =& new stdClass(); $question->question = check_output($node->body, $node->format); $question->nid = $node->nid; $questions[] = $question; } return $questions; } ?>