--- question.module 2006-04-16 01:40:44.000000000 -0500 +++ question.module.ak 2006-04-16 01:40:35.000000000 -0500 @@ -53,11 +53,11 @@ } /** - * Implementation of hook_node_name(). + * Implementation of hook_node_info(). * */ -function question_node_name($node) { - return t('question'); +function question_node_info() { + return array('question' => array('name' => t('question'), 'base' => 'question')); } /** @@ -91,7 +91,8 @@ 'access' => user_access('manage questions')); $items[] = array('path' => 'admin/question', 'title' => t('questions'), 'access' => user_access('manage questions'), 'callback'=>'question_list_page'); - $items[] = array('path' => 'question', 'access' => TRUE, 'callback'=>'question_submit', 'title' => t('ask a question')); + $items[] = array('path' => 'question', 'title' => t('ask a question'), + 'access' => TRUE, 'callback'=>'question_add'); } return $items; @@ -102,30 +103,57 @@ * */ function question_form(&$node) { - $output = ''; + if (arg(2)=='question' && is_numeric(arg(3))) { $que = db_fetch_object(db_query('SELECT * FROM {question_queue} WHERE qid = %d', arg(3))); $node->questioner = $que->questioner; $node->question = $que->question; - $output .= form_hidden('qid', arg(3)); - } - - // In order to be able to attach taxonomy terms to this node, we need - // to display the appropriate form elements. - if (function_exists('taxonomy_node_form')) { - $output .= implode('', taxonomy_node_form('question', $node)); - } - - $output .= form_textfield(t('Questioner'), 'questioner', $node->questioner, 60, 128, t("The person asking the question. Can be the user's id (uid), username, or an email address.")); + $form['qid'] = array( + '#type' => 'hidden', + '#value' => arg(3), + ); + } + + $form['title'] = array('#type' => 'textfield', + '#title' => t('Subject'), + '#default_value' => $node->title, + '#size' => 60, + '#maxlength' => 128, + '#weight' => -8, + '#required' => TRUE); + + $form['questioner'] = array( + '#type' => 'textfield', + '#title' => t('Questioner'), + '#default_value' => $node->questioner, + '#size' => 60, + '#maxlength' => 128, + '#weight' => -7, + '#description' => t("The person asking the question. Can be the user's id (uid), username, or an email address."), + ); // Now we define the form elements specific to our node type. - $output .= form_textarea(t('Question'), 'question', $node->question, 60, 20); - $output .= filter_form('q_format', $node->q_format); - - $output .= form_textarea(t('Answer'), 'answer', $node->answer, 60, 20); - $output .= filter_form('a_format', $node->a_format); + $form['question'] = array('#weight' => -6); + $form['question']['question'] = array( + '#type' => 'textarea', + '#title' => t('Question'), + '#default_value' => $node->question, + '#cols' => 60, + '#rows' => 20 + ); + $form['question']['q_format'] = filter_form($node->q_format, 10, array('q_format')); + + $form['answer'] = array('#weight' => -5); + $form['answer']['answer'] = array( + '#type' => 'textarea', + '#title' => t('Answer'), + '#default_value' => $node->answer, + '#cols' => 60, + '#rows' => 20 + ); + $form['answer']['a_format'] = filter_form($node->a_format, 10, array('a_format')); - return $output; + return $form; } /** @@ -182,9 +210,22 @@ function question_settings() { // require users to be registered in order to ask questions? - $output .= form_checkbox(t('Require registered users?'), 'question_require_registered', 1, variable_get('question_require_registered', FALSE), t('Should we require users to be authenticated in order to submit questions?')); - $output .= form_textfield(t('Path to "Thank You" node'), 'question_thanks', variable_get('question_thanks', FALSE), 40, 100, t('This is where users will end up after they submit the question form. Example: "node/454".
Leave blank and user will be returned to the form page with a thank you message.')); - return $output; + $form['question_require_registered'] = array( + '#type' => 'checkbox', + '#title' => t('Require registered users?'), + '#return_value' => 1, + '#default_value' => variable_get('question_require_registered', FALSE), + '#description' => t('Should we require users to be authenticated in order to submit questions?'), +); + $form['question_thanks'] = array( + '#type' => 'textfield', + '#title' => t('Path to "Thank You" node'), + '#default_value' => variable_get('question_thanks', FALSE), + '#size' => 40, + '#maxlength' => 100, + '#description' => t('This is where users will end up after they submit the question form. Example: "node/454".
Leave blank and user will be returned to the form page with a thank you message.'), + ); + return $form; } @@ -196,15 +237,18 @@ } switch ($op) { case 'delete': - $hidden = form_hidden('qid', $qid); - $output = theme('confirm', t('Are you sure you want to delete this item?'), 'admin/question', NULL, NULL, NULL, $hidden); + $form['qid'] = array( + '#type' => 'hidden', + '#value' => $qid + ); + return confirm_form('question_queue_item_delete', $form, t('delete question'), 'admin/question', t('Are you sure you want to delete this question?')); break; default: $headers = array(t('Question'), t('Operations')); $sql = 'SELECT * FROM {question_queue} ORDER BY qid DESC'; $result = pager_query($sql); while ($r = db_fetch_object($result)) { - $rows[$r->qid]['question']['data'] = ''.$r->questioner.'
'.check_output($r->question); + $rows[$r->qid]['question']['data'] = ''.$r->questioner.'
'.check_markup($r->question); $rows[$r->qid]['question']['style'] = 'vertical-align:top;border-bottom:solid 1px #666;'; $rows[$r->qid]['operations']['data'] = l(t('delete'), 'admin/question/delete/'.$r->qid).' '.l(t('promote'), 'node/add/question/'.$r->qid, array('title'=>t('create a question node based on this submission'))); $rows[$r->qid]['operations']['style'] = 'vertical-align:top;border-bottom:solid 1px #666;'; @@ -212,59 +256,7 @@ $output = theme('table', $headers, $rows, array('style'=>'width:100%', 'cellpadding'=>'5')); $output .= theme('pager'); } - print theme('page', $output); -} - -/** - * This is the callback for question forms submitted to the 'question' url - * Submitted questions are inserted into the question queue database table - * Other stuff could happen here like emailing admin, actions, etc. - * - */ - -function question_submit() { - if ($_POST['op']['Submit Question']) { - $edit = $_POST['edit']; - - // validate... - if ($edit['question']==''){ - //dude, it's required - form_set_error('question', t('Please enter a question.')); - $output = theme('question_qform', $edit); - print theme('page', $output); - return; // end this function - } - - $qid = db_next_id('question_queue'); - global $user; - $quid = $user->uid; - db_query('INSERT INTO {question_queue} (qid, questioner, quid, question) VALUES (%d, "%s", %d, "%s")', $qid, $edit['questioner'], $quid, $edit['question']); - $path = variable_get('question_thanks', ''); - $dest = $_REQUEST['destination']; - unset($_REQUEST['destination']); - if (strlen(trim($path))) { - // if the 'question thank you node' variable was set... - drupal_goto($path); - } - else { - // if not... - drupal_set_message(t('Thank you for submitting your question.')); - if ($dest) { - //go back to the original question node... - drupal_goto($dest); - } - else { - // last resort - drupal_goto('node'); - } - } - } - else { - drupal_set_title(t("Submit a Question")); - // initial form... - $output = theme('question_qform'); - print theme('page', $output); - } + return $output; } function question_queue_item_delete($qid) { @@ -327,8 +319,8 @@ if ($node->questioner) { $output .= '
'.$node->questioner.t(" asks:")."
"; } - $output .= '
'.t('

Question

').$node->question.'
'; - $output .= '
'.t('

Answer

').$node->answer.'
'; + $output .= '
'.t('

Question

').check_markup($node->question, $node->q_format, FALSE).'
'; + $output .= '
'.t('

Answer

').check_markup($node->answer, $node->a_format, FALSE).'
'; $output .= ''; return $output; } @@ -336,27 +328,57 @@ function theme_question_teaser($node) { // just the question for the teasers... $output = '
'; - $output .= '
'.$node->question.'
'; + $output .= '
'.check_markup($node->question, $node->q_format, FALSE).'
'; $output .= '
'; return $output; } -function theme_question_qform($edit = NULL) { - $output = '
'; +function question_add() { + // initial form... + drupal_set_title(t("Submit a Question")); + $output = theme('question_qform'); + print theme('page', $output); + return; +} + +function theme_question_qform() { global $user; + $output = '
'; if ($user->uid > 0 || !variable_get('question_require_registered', FALSE)) { // if user is logged in or we're not requiring registered users if (!variable_get('question_require_registered', FALSE)) { - $form .= form_textfield('Your name', 'questioner', $edit->name ? $edit->name : $user->name, 40, 60, t('Your name, username, or email address')); + $form['questioner'] = array( + '#type' => 'textfield', + '#title' => 'Your name', + '#default_value' => $user->name, + '#size' => 40, + '#maxlength' => 60, + '#description' => t('Your name, username, or email address'), + ); } else { - $form .= form_hidden('questioner', $user->name); + $form['questioner'] = array( + '#type' => 'hidden', + '#value' => $user->name, + ); } - $form .= form_textarea('Question', 'question', $edit->question, 60, 10, NULL, NULL, TRUE); - $form .= form_submit(t('Submit Question')); - $output .= form($form, 'post', url('question', drupal_get_destination())); - //javascript for above: - //, array('onsubmit'=>"if(getElementById('edit-question').value==''){alert('Please enter a question.');return false;}") + $form['question'] = array( + '#type' => 'textarea', + '#title' => 'Question', + '#default_value' => $_POST['question'], + '#cols' => 60, + '#rows' => 10, + '#description' => NULL, + '#attributes' => NULL, + '#required' => TRUE, + ); + $form[] = array( + '#type' => 'submit', + '#value' => t('Submit Question'), + ); + $form['#method'] = 'post'; + $form['#action'] = url('question', drupal_get_destination()); + $output = drupal_get_form('question_q_form', $form); } else { // if we are requiring registered users, but user is not logged in @@ -366,6 +388,40 @@ return $output; } +function question_q_form_validate ($form_id, $form_values) { + if ($form_values['question'] == '') { + form_set_error('question', t('Please enter a question.')); + } +} + +function question_q_form_submit ($form_id, $form_values) { + $edit = $_POST['edit']; + $qid = db_next_id('question_queue'); + global $user; + $quid = $user->uid; + db_query('INSERT INTO {question_queue} (qid, questioner, quid, question) VALUES (%d, "%s", %d, "%s")', $qid, $edit['questioner'], $quid, $edit['question']); + $path = variable_get('question_thanks', ''); + $dest = $_REQUEST['destination']; + unset($_REQUEST['destination']); + + if (strlen(trim($path))) { + // if the 'question thank you node' variable was set... + drupal_goto($path); + } + else { + drupal_set_message(t('Your question was submitted.')); + + // if not... + if ($dest) { + //go back to the original question node... + drupal_goto($dest); + } + else { + // last resort + drupal_goto('node'); + } + } +} /** * These functions work with macrotags.module to allow users to enter @@ -403,7 +459,7 @@ function question_get_latest() { $obj = db_fetch_object(db_query('SELECT n.nid FROM {question_node} q INNER JOIN {node} n ON n.nid = q.nid WHERE n.status = 1 ORDER BY n.created DESC')); - $node = node_load(array('nid'=>$obj->nid)); + $node = node_load($obj->nid); return $node; } @@ -425,7 +481,7 @@ function question_list_questions($teaser = TRUE, $links = FALSE, $joins = '', $wheres = '', $order = 'ORDER BY n.created DESC') { $result = db_query("SELECT n.nid FROM {node} n ".$joins." WHERE n.type = 'question' AND n.status = 1 ".$wheres." ".$order); while ($row = db_fetch_object($result)) { - $node = node_load(array('nid' => $row->nid)); + $node = node_load($row->nid); $output .= node_view($node, $teaser, FALSE, $links); } return $output;