'node/add/question', 'title' => t('question'), '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')); } return $items; } /** * Implementation of hook_form(). * */ 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['qid'] = array( '#type' => 'hidden', '#value' => 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['questioner'] = array( '#type' => 'textfield', '#title' => t('Questioner'), '#default_value' => $node->questioner, '#size' => 60, '#maxlength' => 128, '#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['question'] = array( '#type' => 'textarea', '#title' => t('Question'), '#default_value' => $node->question, '#cols' => 60, '#rows' => 20, ); $output .= filter_form('q_format', $node->q_format); $output .= $form['answer'] = array( '#type' => 'textarea', '#title' => t('Answer'), '#default_value' => $node->answer, '#cols' => 60, '#rows' => 20, ); $output .= filter_form('a_format', $node->a_format); return $output; } /** * Implementation of hook_validate(). * */ function question_validate(&$node) { $node->body = ''; // make body for node previews and whatnot //$node->body = ''.$node->question."
\n //".$node->answer; //$node->format = $node->a_format; //check validity of questioner $req = (variable_get('question_require_registered', FALSE)); if (is_numeric($node->questioner)) { // is it a uid? if (!$account = user_load(array('uid'=>$node->questioner))) { // it is not a uid if ($req) { form_set_error('questioner', t('This is not a valid user id.')); } } else { // it is a uid $node->quid = $account->uid; $node->questioner = $account->name; } } else { if (valid_email_address($node->questioner)) { // it's an email address if ($account = user_load(array('mail'=>$node->questioner))) { // they're "one of us" $node->quid = $account->uid; $node->questioner = $account->name; } } elseif ($account = user_load(array('name'=>$node->questioner))) { // it's a user $node->quid = $account->uid; } else { // it's neither an email nor a user if ($req) { form_set_error('questioner', t('This is neither an email address nor a valid user name.')); } } } } function question_settings() { // require users to be registered in order to ask questions? $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?'), ); $output .= $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 $output; } function question_list_page($op = NULL, $qid = NULL) { if ($_POST['edit']['confirm']) { question_queue_item_delete($_POST['edit']['qid']); unset($_POST); drupal_goto('admin/question'); } switch ($op) { case 'delete': $hidden = $form['qid'] = array( '#type' => 'hidden', '#value' => $qid, ); $output = theme('confirm', t('Are you sure you want to delete this item?'), 'admin/question', NULL, NULL, NULL, $hidden); 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']['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;'; } $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); } } function question_queue_item_delete($qid) { db_query('DELETE FROM {question_queue} WHERE qid="%d"', $qid); drupal_set_message(t('Item deleted')); } /** * Implementation of hook_insert(). * */ function question_insert($node) { db_query("INSERT INTO {question_node} (nid, questioner, quid, question, q_format, answer, a_format) VALUES (%d, '%s', %d, '%s', %d, '%s', %d)", $node->nid, $node->questioner, $node->quid, $node->question, $node->q_format, $node->answer, $node->a_format); // if this node came from the queue, delete the queue item... if (isset($node->qid)) { db_query("DELETE FROM {question_queue} WHERE qid = %d", $node->qid); } } /** * Implementation of hook_update(). * */ function question_update($node) { db_query("UPDATE {question_node} SET questioner='%s', quid=%d, question='%s', q_format=%d, answer='%s', a_format=%d WHERE nid = %d", $node->questioner, $node->quid, $node->question, $node->q_format, $node->answer, $node->a_format, $node->nid); } /** * Implementation of hook_delete(). * */ function question_delete($node) { db_query('DELETE FROM {question_node} WHERE nid = %d', $node->nid); } /** * Implementation of hook_load(). * */ function question_load($node) { $additions = db_fetch_object(db_query('SELECT questioner, quid, question, q_format, answer, a_format FROM {question_node} WHERE nid = %d', $node->nid)); return $additions; } /** * Implementation of hook_view(). * */ function question_view(&$node, $teaser = FALSE, $page = FALSE) { $node = node_prepare($node, $teaser); $node->body = theme('question_body', $node); $node->teaser = theme('question_teaser', $node); } function theme_question_body($node) { if ($node->quid) { $node->questioner = l($node->questioner, 'user/'.$node->quid); } $output = '
'; if ($node->questioner) { $output .= '
'.$node->questioner.t(" asks:")."
"; } $output .= '
'.t('

Question

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

Answer

').$node->answer.'
'; $output .= '
'; return $output; } function theme_question_teaser($node) { // just the question for the teasers... $output = '
'; $output .= '
'.$node->question.'
'; $output .= '
'; return $output; } function theme_question_qform($edit = NULL) { $output = '
'; global $user; 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['questioner'] = array( '#type' => 'textfield', '#title' => 'Your name', '#default_value' => $edit->name ? $edit->name : $user->name, '#size' => 40, '#maxlength' => 60, '#description' => t('Your name, username, or email address'), ); } else { $form .= $form['questioner'] = array( '#type' => 'hidden', '#value' => $user->name, ); } $form .= $form['question'] = array( '#type' => 'textarea', '#title' => 'Question', '#default_value' => $edit->question, '#cols' => 60, '#rows' => 10, '#description' => NULL, '#attributes' => NULL, '#required' => TRUE, ); $form .= $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit Question'), ); $output .= $form['#method'] = 'post'; $form['#action'] = url('question', drupal_get_destination()); $output = drupal_get_form('question', $form); //javascript for above: //, array('onsubmit'=>"if(getElementById('edit-question').value==''){alert('Please enter a question.');return false;}") } else { // if we are requiring registered users, but user is not logged in $output .= t('

Please login/register to submit questions.

', array("%login"=>url('user/login', drupal_get_destination()))); } $output .= '
'; return $output; } /** * These functions work with macrotags.module to allow users to enter * "[qform]" into a node and have the questionnaire displayed * */ function mt_qform() { return theme('question_qform'); } function mt_qform_help($section = 'short-tip') { switch ($section) { case 'short-tip': case 'long-tip': $output = t('
[qform]
'); $output .= t('

Include the question.module questionnaire.

'); return $output; } } function question_word_truncate($text, $count) { if (!count(explode(" ", $text)) > $count) { return $text; } else { return trim(implode(" ", array_slice(explode(" ", $text), 0, $count))); } } /** * Returns node object for latest question (based on node's creation date) * */ 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)); return $node; } /** * Returns rendered listing of questions * $teaser is a true/false indicating whether the teaser version * of the node should be displayed * $links is a true/false indicating whether links should be displayed * $joins is a string containing JOIN arguments for SQL call * example: 'INNER JOIN {term_node} t ON t.nid = q.nid' * $wheres is a string containing additional WHERE arguments * example: 'AND t.tid = 12' * $order is a string containing ORDER BY * * The above example arguments would return the node ids of all question nodes * that are tagged with taxonomy term 12 */ 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)); $output .= node_view($node, $teaser, FALSE, $links); } return $output; } ?>