'. t("This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.") .'
'.
''. t("The module shows an abbreviated version of the FAQ form without an answer field. The node is created without the 'published' attribute. There is a block that will show the unanswered questions to the 'expert' (generally, this requires a separate role).") .'
'.
''. t("Viewing of the completed question and answer pair is done by the FAQ module.") .'
'.
''. t("Simply adding the 'FAQ' content type to a vocabulary will not make it eligible for experts; you must go to the settings page and add it there.") .'
';
return $output;
case 'faq_ask/'. arg(1):
case 'faq_ask':
return t("Add a question for our expert to answer. After being answered, your question and the answer will be displayed in the FAQ pages. If the question will not fit in the box below, please try to rephrase it.");
}
}
/**
* Implementation of hook_perm()
* Define the permissions this module uses
*/
function faq_ask_perm() {
return array('ask question', 'answer question');
}
/**
* Implementation of hook_access()
*/
function faq_ask_access($op, $node) {
global $user;
if ($op == 'create') { return user_access(t('ask_question')) || user_access('answer question'); }
else { return user_access('answer question') || user_access('edit_own_faq'); }
}
/**
* Implementation of hook_enable()
* This function reminds the user to configure the module.
*/
function faq_ask_enable() {
drupal_set_message(t('The Faq_Ask module has been enabled.') .' '. t('Please go to the settings page to configure this module.', array('!url' => url('admin/settings/faq/ask'))), 'notice');
}
/**
* Implementation of hook_menu()
*/
function faq_ask_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/faq/ask',
'title' => t('Experts'),
'callback' => 'drupal_get_form',
'callback arguments' => array('faq_ask_settings_form'),
'access' => user_access('administer faq'),
'description' => t('Allows the user to configure the Ask_FAQ module.'),
'type' => MENU_LOCAL_TASK,
'weight' => -2,
);
$items[] = array(
'path' => 'faq_ask',
'title' => t('Ask a question'),
'callback' => 'faq_ask_page',
'access' => user_access('ask question'),
);
}
else {
$items[] = array(
'path' => 'faq_ask/answer',
'title' => t('Answer a question'),
'callback' => 'faq_ask_answer',
'access' => user_access('answer question'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'faq_ask/edit',
'title' => t('Edit a question'),
'callback' => 'faq_ask_edit',
'access' => user_access('answer question'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'faq_ask/more',
'title' => t('List more unanswered questions'),
'callback' => 'faq_ask_list_more',
'access' => user_access('answer question') || user_access('ask question'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Get the ask question form.
*/
function faq_ask_page($tid = null) {
$output = null;
if ($tid) {
$term = taxonomy_get_term($tid);
$taxo_image = null;
if (module_exists('taxonomy_image')) {
$taxo_image = taxonomy_image_display($tid, 'align="left"');
}
$output .= '| '. $taxo_image . $term->description .' |
';
$output .= '';
}
$output .= drupal_get_form('faq_ask_form', $tid, null);
return $output;
}
/**
* Get the edit question form.
*/
function faq_ask_edit($nid = null) {
$form = drupal_get_form('faq_ask_form', null, $nid);
return $form;
}
/**
* Implementation of hook_form()
* This is the "ask question" form.
*/
function faq_ask_form($tid, $nid, $form_values = null) {
$form = array();
// Find all the categories for which we have experts.
$cat_list = array_flip(db_result_array(db_query('SELECT DISTINCT(name), e.tid FROM {faq_expert} e JOIN {term_data} t USING (tid)')));
if (count($cat_list) == 0) {
$msg = t('Currently, there are no categories defined. ') .' ';
if (user_access('administer faq')) {
$msg .= t('Please go to the settings page to configure this module.', array('!url' => url('admin/settings/faq/ask')));
}
else {
$msg .= t('Please ask your site administrator to set up this feature.', array('!url' => url('user/1/contact')));
}
drupal_set_message($msg, 'error');
}
// We will allow term suggestions only if the setting is chosen
// AND there is a vocabulary named "FAQ."
$vid = db_result(db_query_range("SELECT vid FROM {vocabulary} WHERE name='FAQ'", 0, 1));
$suggest = variable_get('faq_ask_suggest', false) && $vid;
if ($suggest) {
$cat_list[0] = t('');
$form['#multistep'] = true;
$form['#redirect'] = false;
$form['faq_vid'] = array(
'#type' => 'value',
'#value' => $vid,
);
if (!$tid) {
$default_tid = 0;
}
}
// If a nid exists, then get the existing values.
if ($nid) {
$node = node_load($nid);
$title = $node->title;
$taxo = array_keys($node->taxonomy);
$default_tid = $taxo[0];
}
else {
$default_tid = $tid;
$title = null;
}
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
// Category.
$form['category'] = array(
'#title' => t('Category'),
'#type' => 'select',
'#options' => $cat_list,
'#default_value' => $default_tid,
'#required' => true,
'#weight' => -3,
'#description' => t('Please select the correct category for your question.'),
);
if (isset($form_values)) {
if ($form_values['category'] == 0) {
$form['new_cat'] = array(
'#title' => t('Suggested Category'),
'#type' => 'textfield',
'#weight' => -1,
'#default_value' => null,
'#description' => t('Please enter your suggested category for the question.'),
);
}
}
// Question.
$cols = variable_get('faq_ask_title_len', 60);
$form['title'] = array(
'#type' => 'textarea',
'#title' => t('Question'),
'#required' => true,
'#default_value' => $title,
'#weight' => 0,
'#cols' => $cols,
'#rows' => 5,
'#maxlength' => 999999,
'#description' => t('Enter your question here. It will be answered as soon as possible.'),
);
$form['send'] = array(
'#type' => 'submit',
'#value' => t('Send my question'),
'#weight' => 3,
);
return $form;
}
/**
* Implementation of hook_form_submit.
* This function gets the entered question and category and creates an unpublished FAQ node.
*/
function faq_ask_form_submit($form_id, $form_values) {
global $user;
$category = $form_values['category'];
if ($category == 0 && empty($form_values['new_cat'])) {
// Can only be 0 if allowing category suggestion.
}
else {
if ($category == 0) {
// We have a new category to process.
$new_cat = $form_values['new_cat'];
$term = array('name' => $new_cat,
'description' => 'suggested by '. $user->name .' ('. $user->uid .') on '. date('F j, Y'),
'vid' => $form_values['faq_vid'],
'weight' => 0,
'parent' => 0,
'tid' => NULL,
);
taxonomy_save_term($term);
$term = (object)$term;
// Setting up the expert will be done in hook_taxonomy.
}
$term = taxonomy_get_term($category);
$node = array(
'type' => 'faq',
'body' => '', /* Empty string rather than null. */
'title' => $form_values['title'],
'taxonomy' => array($category => $term),
'created' => time(),
'uid' => $user->uid,
'name' => $user->name,
'status' => 0, /* Unpublished. */
'format' => 1, /* Default filter (filtered HTML) */
'comment' => variable_get('comment_faq', 0),
);
$node_options = variable_get('node_options_'. $node['type'], array('status', 'promote'));
foreach (array('promote', 'sticky', 'revision') as $key) {
$node[$key] = in_array($key, $node_options) ? 1 : 0;
}
if ($form_values['nid']) { $node['nid'] = $form_values['nid']; }
// Okay, let's get it done. Node_submit will prepare it and make it an object.
$node = node_submit($node);
node_save($node);
// Are we notifying the expert(s)?
if (variable_get('faq_ask_notify', false)) {
// Find out who the experts are.
$result = db_query("SELECT u.mail FROM {faq_expert} e JOIN {users} u USING (uid) WHERE e.tid=%d", $category);
$to_addresses = array();
while ($expert = db_fetch_array($result)) {
$to_addresses[] = $expert['mail'];
}
$to = implode(', ', $to_addresses);
$from = variable_get('site_name', 'Drupal') .'<'. variable_get('site_mail', ini_get('sendmail_from')) .'>';
// Let's make this HTML mail because of the links we'll use.
$header = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
$body = t('The following question has been posted in the "!cat" category.', array('!cat' => $term->name));
$body .= '
'. $form_values['title'];
// The URLs in the following are contructed as absolute addresses.
$body .= '
'. t('In order to answer it you will first need to login to the site.', array('!url' => url('user', null, null, true)));
$body .= '
'. t('Once logged in, you may proceed directly to the question to answer it.', array('!url' => url('faq_ask/answer/'. $node->nid, null, null, true)));
$mail_sent = drupal_mail('expert-notify', $to, 'You have a question waiting', $body, $from, $header);
if ($mail_sent) {
watchdog('FAQ_Ask', t('Expert notification email sent.') .' '. $to, WATCHDOG_NOTICE);
}
else {
$msg = t('Expert notification email failed for the "!cat" category.', array('!cat' => $term->name));
watchdog('FAQ_Ask', $msg .' '. $to, WATCHDOG_ERROR);
drupal_set_message($msg, 'notice');
}
}
// Quick detour to see if the block is enabled.
// Slight flaw here: There may be more than one theme enabled, but this just gets the first one,
// which may not be the current theme.
$region = db_result(db_query_range("SELECT region FROM {blocks} WHERE module='faq_ask' AND delta=0 AND status=1", 0, 1));
if ($region) {
switch ($region) {
case 'left':
$region_name = t('left side');
break;
case 'right':
$region_name = t('right side');
break;
case 'header':
$region_name = t('top');
break;
case 'footer':
$region_name = t('bottom');
break;
default:
$region_name = '';
break;
}
if ($region_name) {
$msg = t('It should appear in the list at the @region of the screen momentarily.', array('@region' => $region_name));
}
else {
$msg = t('It should appear in the list momentarily.');
}
}
else { $msg = null; }
drupal_set_message(t('Your question has been submitted.') .' '. $msg, 'notice');
drupal_goto('faq/'. $category);
}
}
/**
* Implementation of hook_form.
* This form allows the users to select the expert roles and to which categories the users in those roles are assigned.
* Note, the expert/category table attempts to use the least horizontal space,
* so it can "flip" based on whether there are more categories or experts.
*/
function faq_ask_settings_form($op = null, $aid = null) {
$form = array();
$faq_use_categories = variable_get('faq_use_categories', false);
if (!$faq_use_categories) {
drupal_set_message(t('The Faq_Ask module requires that FAQ "Categorize questions."') .' '. t('Please go to the settings page to configure this module.', array('!url' => url('admin/settings/faq/categories'))), 'error');
}
// Get the admin's name.
$admin = ucwords(db_result(db_query('SELECT name FROM {users} WHERE uid=1')));
$form['faq_ask_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify experts'),
'#description' => t('If this box is checked, the expert(s) for the question will be notified via email that a question awaits them. If you do not choose this option, the "Unanswered Questions" block will be the only way they will know they have questions to answer.'),
'#default_value' => variable_get('faq_ask_notify', 0),
);
$form['faq_ask_suggest'] = array(
'#type' => 'checkbox',
'#title' => t('Allow "Suggest a category"'),
'#description' => t('If this box is checked, the person asking a question will have an option to enter a new category. It will have the default expert assigned to it until you return here to correct the assignment.'),
'#default_value' => variable_get('faq_ask_suggest', 0),
);
$title_opts = drupal_map_assoc(array(20, 30, 40, 50, 60, 70, 80, 90, 100, 128));
$form['faq_ask_title_len'] = array(
'#type' => 'select',
'#options' => $title_opts,
'#title' => t('Question Box Length'),
'#description' => t('This sets the length of the question box (not the question itself). This affects only the FAQ_Ask module, not the FAQ display. The recommendation is to set it as wide as your theme will allow it. Note: If resizable textareas are enabled, this setting will have no effect.'),
'#default_value' => variable_get('faq_ask_title_len', 60),
);
// Get the list of vocabularies that apply to FAQ s.
$vocabs = taxonomy_get_vocabularies('faq');
if (count($vocabs) == 1) {
// Single vocabulary, don't bother with a selection box, just set it.
$vid = key($vocabs);
variable_set('faq_ask_vocabularies', array($vid => $vid));
$vobj = $vocabs[$vid];
$free = $vobj->tags;
}
else {
// Multiple vocabs available.
$voc_list = array();
$def_vid = 0;
$free = false;
foreach ($vocabs as $vid => $vobj) {
$voc_list[$vid] = $vobj->name;
if ($vobj->name == 'FAQ') { $def_vid = $vid; }
$free = $free || ($vobj->tags);
}
$form['faq_ask_vocabularies'] = array(
'#type' => 'select',
'#options' => $voc_list,
'#title' => t('Use these vocabularies'),
'#multiple' => true,
'#default_value' => variable_get('faq_ask_vocabularies', $def_vid),
'#description' => t('Only the terms from the selected vocabularies will be included in the list below.')
.' '. t("Simply adding the 'FAQ' content type to a vocabulary will not make it eligible for experts; you must return to here to add it.")
.'
'. t('If you select different vocabularies, you must save the configuration BEFORE selecting users below.') .'',
);
} // End multiple vocabs.
if ($free) {
$form['error'] = array(
'#type' => 'item',
'#value' => ''. t('Free tagging vocabularies are not allowed for Faq_Ask.') .''
);
return $form;
}
// Changed query and loop because it failed if 'answer' was the first perm in list.
// This should be faster any way.
$role_list = db_result_array(db_query("SELECT r.rid, r.name FROM {role} r JOIN {permission} p USING (rid) WHERE p.perm LIKE '%%answer question%%'"));
if (empty($role_list)) {
drupal_set_message(t('No roles with "answer question" permission were found; only !admin is currently eligible to be an expert. You may want to go to the Access Control page to update your permissions.', array('!access' => url('admin/user/access'), '!admin' => $admin)), 'warning');
}
// Get all terms associated with FAQ.
$vocabs = variable_get('faq_ask_vocabularies', $def_vid);
$result = db_query("SELECT td.tid, td.name, td.description FROM {term_data} td WHERE td.vid IN (". db_placeholders($vocabs) .") ORDER BY td.weight ASC, td.name ASC", $vocabs);
$faq_terms = array();
while ($term = db_fetch_array($result)) {
// Show term hierarchy?
$term_name = /* str_repeat('--', $term['depth']) . */ $term['name'];
if (substr($term['description'], 0, 9) == 'suggested') {
$faq_terms[$term['tid']] = $term_name .'
--'. strip_tags($term['description']) .'';
}
else {
$faq_terms[$term['tid']] = $term_name;
}
}
if (count($faq_terms) == 0) {
drupal_set_message(t('No vocabularies or terms were found for the "faq" content type . Please go to the Categories page to update your vocabulary.', array('!access' => url('admin/content/taxonomy'))), 'warning');
}
// Get all users associated with the roles.
$faq_expert_names = array();
// User/1 typically is not assigned roles, but should be in the list.
$faq_expert_names[1] = $admin;
$rids = variable_get('faq_expert_role', array());
if (!empty($rids)) {
if (in_array(DRUPAL_AUTHENTICATED_RID, $rids)) {
// Authenticated users may be experts, so get all active users.
// No other roles matter.
$result = db_query("SELECT u.uid, u.name FROM {users} u WHERE status=1");
}
else {
// Only specific roles may be experts.
$result = db_query("SELECT DISTINCT(u.uid), u.name FROM {users_roles} ur JOIN {users} u USING (uid) WHERE ur.rid IN (". db_placeholders($rids) .")", $rids);
}
while ($user = db_fetch_array($result)) {
if ($user['uid'] != 1) {
$faq_expert_names[$user['uid']] = ucwords($user['name']);
}
}
// Put them in alphabetical order.
asort($faq_expert_names);
}
if (!empty($role_list)) {
$form['faq_expert_role'] = array(
'#type' => 'select',
'#title' => t('Expert Roles'),
'#options' => $role_list,
'#multiple' => true,
'#default_value' => variable_get('faq_expert_role', '2'),
'#description' => t('User 1 (@admin) will always be in the list, regardless of roles.', array('@admin' => $admin)) .'
'. t('If you select different roles, you must save the configuration BEFORE selecting users below.') .'',
);
}
$more_experts_than_terms = count($faq_expert_names) > count($faq_terms);
// If there is only one eligible expert, we might as well preset all categories.
$expert_msg = null;
$only_one_expert = (count($faq_expert_names) == 1);
$count = 0;
if ($more_experts_than_terms) {
// Experts go down the left; terms go across the top.
$top = null;
if ($only_one_expert) {
$top .= ''. t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') .'
';
}
$top .= '| | '. implode(' | ', $faq_terms) .' |
';
if ($only_one_expert) {
$top .= '| '. t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') .' |
';
}
foreach ($faq_expert_names as $uid => $name) {
++$count;
$class = $count & 1 ? 'odd' : 'even';
$left = '| '. $name .' | ';
foreach ($faq_terms as $tid => $term_name) {
$box_name = 'expert_'. $uid .'_'. $tid;
$form[$box_name] = array(
'#type' => 'checkbox',
'#default_value' => $only_one_expert,
'#prefix' => $top . $left .'',
'#suffix' => ' | ',
);
$top = null;
$left = null;
}
$form[$box_name]['#suffix'] .= '
';
}
$form[$box_name]['#suffix'] .= '
';
}
else {
// Experts go across the top; terms go down the left.
$top = null;
if ($only_one_expert) {
$top .= ''. t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') .'
';
}
$top .= '| | '. implode(' | ', $faq_expert_names) .' |
';
foreach ($faq_terms as $tid => $term_name) {
++$count;
$class = $count & 1 ? 'odd' : 'even';
$left = '| '. $term_name .' | ';
foreach ($faq_expert_names as $uid => $name) {
$box_name = 'expert_'. $uid .'_'. $tid;
$form[$box_name] = array(
'#type' => 'checkbox',
'#default_value' => $only_one_expert,
'#prefix' => $top . $left .'',
'#suffix' => ' | ',
);
$top = null;
$left = null;
}
$form[$box_name]['#suffix'] .= '
';
}
$form[$box_name]['#suffix'] .= '
';
}
$result = db_query("SELECT * FROM {faq_expert}");
while ($expert = db_fetch_array($result)) {
$box_name = 'expert_'. $expert['uid'] .'_'. $expert['tid'];
if (isset($form[$box_name])) { // Might not be present any more.
$form[$box_name]['#default_value'] = true;
}
else { drupal_set_message(t("!name doesn't exist. If you have just changed your role selections this may be okay.", array('!name' => $box_name)), 'warning'); }
}
if ($only_one_expert) {
// Create a form value to set default expert to admin.
$form['faq_ask_default_expert'] = array(
'#type' => 'value',
'#value' => 1,
);
}
else {
$form['faq_ask_default_expert'] = array(
'#type' => 'select',
'#options' => $faq_expert_names,
'#multiple' => false,
'#title' => t('Default expert'),
'#description' => t('The selected user will be assigned as the expert for all terms that are added to the selected vocabularies until you return to this page and update it.'),
'#default_value' => variable_get('faq_ask_default_expert', 1),
);
}
$form['save']['update'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
'#weight' => 5,
);
return $form;
}
/**
* Implementation of hook_form_submit.
* It saves the expert roles that were selected, then rebuilds the expert/category table.
*/
function faq_ask_settings_form_submit($form_id, $form_values) {
// Save the simple stuff.
variable_set('faq_expert_role', $form_values['faq_expert_role']);
variable_set('faq_ask_vocabularies', $form_values['faq_ask_vocabularies']);
variable_set('faq_ask_title_len', $form_values['faq_ask_title_len']);
variable_set('faq_ask_suggest', $form_values['faq_ask_suggest']);
variable_set('faq_ask_notify', $form_values['faq_ask_notify']);
variable_set('faq_ask_default_expert', $form_values['faq_ask_default_expert']);
// Get all the selected expert/category options.
$values = array();
foreach ($form_values as $name => $value) {
if (substr($name, 0, 7) == 'expert_') {
list($junk, $uid, $tid) = explode('_', $name);
if ($value) {
$values[] = "(". $uid .", ". $tid .")";
}
}
}
$vals = implode(', ', $values);
// Delete the current values and save the new ones.
if (!empty($values)) {
db_query('TRUNCATE {faq_expert}');
db_query("INSERT INTO {faq_expert} (uid, tid) VALUES %s", $vals);
}
drupal_set_message(t('Configuration has been updated.'), 'notice');
}
/**
* This function is called when an expert selects a question to answer.
* It changes the status option to "published" then goes to the regular FAQ edit function.
*/
function faq_ask_answer($nid) {
// Change the status to published.
db_query("UPDATE {node} SET status=1 WHERE nid=%d", $nid);
// Need to invoke node/##/edit.
drupal_goto('node/'. $nid .'/edit');
}
/**
* Implementation of hook_taxonomy.
* @param:
* op: 'insert', 'update, 'delete'
* type: 'term', 'vocabulary'
* array: depends on other two.
*/
function faq_ask_taxonomy($op, $type, $array = NULL) {
global $user;
$default_expert = variable_get('faq_ask_default_expert', 1);
$my_vocs = variable_get('faq_ask_vocabularies', array());
$vid = $array['vid'];
// See if it's one of our vocabularies.
$our_vocab = in_array($vid, $my_vocs);
switch ($op) {
case 'insert':
switch ($type) {
case 'term':
// term: set default expert.
if ($our_vocab) {
$insert = db_query("INSERT INTO {faq_expert} (uid, tid) VALUES (%d, %d)", $default_expert, $array['tid']);
if ($insert === false) {
drupal_set_message(t('Attempt to assign expert failed.'), 'error');
}
else {
drupal_set_message(t('Assigned expert @expert to @name (@tid).', array('@expert' => $default_expert, '@name' => $array['name'], '@tid' => $array['tid'])), 'notice');
}
}
break;
case 'vocabulary':
// New vocabulary created. It will not show on the ask page until the user
// goes to the settings page, so we don't need to do anything.
break;
} // End insert switch type.
break;
case 'delete':
switch ($type) {
case 'term':
// Delete term: remove experts.
if ($our_vocab) {
_faq_ask_delete_expert($array['tid'], $array['name']);
}
break;
case 'vocabulary':
// Each term gets deleted first, so all we have to do is remove it from our vocab list.
if ($our_vocab) {
_faq_ask_delete_vocabulary($vid, $array, $my_vocs);
}
break;
} // End delete switch type.
break;
case 'update':
// Two cases for vocabulary:
// 1) FAQ is added to the vocab. -- see insert comment.
// 2) FAQ is removed from the vocab. -- need to delete all experts for all terms and remove from voc list?
// $array['nodes'] contains an array of content types for the vocab.
switch ($type) {
case 'term':
// Term update: nothing to do.
break;
case 'vocabulary':
if (in_array('faq', $array['nodes'])) {
// If it's there now, we're done.
break;
}
// Not there now, so we need to see if it was.
if ($our_vocab) {
$tree = taxonomy_get_tree($vid);
foreach ($tree as $term) {
$my_tid = $term->tid;
$my_tname = $term->name;
_faq_ask_delete_expert($my_tid, $my_tname);
} // End foreach tree.
_faq_ask_delete_vocabulary($vid, $array, $my_vocs);
}
break;
} // End update switch type.
break;
default:
drupal_set_message(t('Faq_ask_taxonomy: Unknown op (@op) encountered', array('@op' => $op)), 'warning');
} // End switch $op
}
/**
* Helper function to delete a vocabulary.
* @param.
* $vid - the taxonomy vocabulary id.
* $array - the array provided in hook_taxonomy.
* $my_vocs - the array of faq_ask vocabularies.
*
* @return: none.
*/
function _faq_ask_delete_vocabulary($vid, $array, $my_vocs) {
global $user;
$name = $array['name'];
$uname = $user->name;
drupal_set_message("Vocabulary '$name' is being removed from the Faq_Ask list.", 'notice');
watchdog('Faq_Ask', "Vocabulary $name was deleted from Faq_Ask by $uname.", WATCHDOG-NOTICE);
unset($my_vocs[$vid]);
variable_set('faq_ask_vocabularies', $my_vocs);
}
/**
* Helper function to delete experts.
* @param.
* $tid - the taxonomy term id for the experts.
*
* @return: none.
*/
function _faq_ask_delete_expert($tid, $name=null) {
$delete = db_query("DELETE FROM {faq_expert} WHERE tid=%d", $tid);
if ($delete === false) {
drupal_set_message(t('Attempt to delete expert failed.'), 'error');
}
else {
drupal_set_message(t("Deleted experts for '@name'.", array('@name' => $name)), 'notice');
}
}
/**
* Helper function to find the FAQ vocabulary.
* This function locates a node of the 'faq' type and gets the taxonomy term associated with it.
* It then uses the term to determine the vocabulary to which it belongs.
*
* @param: none.
*
* @return:
* vocabulary object from taxonomy module, or a boolean false if no faq nodes were found.
*/
function faq_ask_get_vocabulary() {
$vid = db_result(db_query_range("SELECT td.vid FROM {node} n LEFT JOIN {term_node} tn USING (nid) LEFT JOIN {term_data} td USING(tid) WHERE n.type='faq' AND n.status=1", 0, 1));
if ($vid) {
return taxonomy_get_vocabulary($vid);
}
else {
drupal_set_message(t("Faq_Ask can't determine the correct vocabulary because no FAQ nodes exist yet."), 'notice');
return false;
}
}
/**
* Implementation of hook_block().
* This creates and populates the "unanswered questions" block.
*/
function faq_ask_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Unanswered Questions');
return $blocks;
case 'view':
switch ($delta) {
case 0:
// Unanswered Questions.
$block['content'] = faq_ask_list_unanswered(variable_get('faq_unanswered_count', 3));
break;
} // end switch($delta).
return $block;
case 'configure':
switch ($delta) {
case 0:
// Unanswered Questions.
$form['faq_unanswered_count'] = array(
'#type' => 'textfield',
'#title' => t('Number of questions to show'),
'#description' => t("This controls the number of questions that appear in the 'Unanswered Questions' block."),
'#size' => 7,
'#default_value' => variable_get('faq_unanswered_count', 3),
);
break;
} // end switch($delta)
return $form;
case 'save':
switch ($delta) {
case 0:
variable_set('faq_unanswered_count', $edit['faq_unanswered_count']);
break;
} // end switch($delta)
return;
} // end switch($op)
}
/**
* This is the code to select the Unanswered Questions for the block.
*/
function faq_ask_list_unanswered($limit) {
global $user;
// Bounce anonymous users.
if ($user->uid == 0) { return null; }
// What permissions does this user have?
$can_edit = user_access('administer faq') || user_access('administer nodes');
$is_expert = user_access('answer question');
$only_own = user_access('edit own faq');
$mode = 'edit';
$extra_msg = null;
// Note: If the admin is also an expert, the expert-ness prevails.
if ($is_expert) {
$mode = 'answer';
$extra_msg = ''. t('If you select a question, you must answer it.') .'';
// Get the expert's terms.
$result = db_query('SELECT * FROM {faq_expert} WHERE uid=%d', $user->uid);
$terms = array();
while ($expert = db_fetch_array($result)) {
$terms[] = $expert['tid'];
}
// Check if this expert has any categories.
if (count($terms) == 0) {
return null;
}
// Join the term_data table to select based on tid.
$result = db_query("SELECT n.title, n.nid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND tn.tid IN (". db_placeholders($terms) .") ORDER BY n.created ASC", $terms);
}
else { if ($can_edit) {
$result = db_query("SELECT title, nid FROM {node} n WHERE type='faq' AND status=0 ORDER BY n.created ASC");
}
else { // Edit own.
// Removed $join - not defined.
$result = db_query("SELECT title, nid FROM {node} n WHERE n.type='faq' AND n.status=0 AND n.uid=%d ORDER BY n.created ASC", $user->uid);
}
}
// Get unpublished nodes that are type='faq'. Stop at the limit given.
$items = array();
$i = 0;
while ($node = db_fetch_array($result)) {
++$i;
if ($i > $limit) {
$items[] = l(''. t('more...') .'', 'faq_ask/more');
break;
}
$items[] = l($node['title'], 'faq_ask/'. $mode .'/'. $node['nid']);
}
if (count($items)) {
return theme('item_list', $items) . $extra_msg;
}
else { return null; }
}
/*
* This function lists all the unanswered questiond the user is allowed to see.
* It is used by the "more..." link from the block, but can also be called independently.
*/
function faq_ask_list_more() {
global $user;
// Bounce anonymous users.
if ($user->uid == 0) { return null; }
drupal_set_title(t('All Unanswered Questions'));
if (user_access('administer blocks')) {
$output = ''. t('You may go here to change the block limit.', array('!setting' => url('admin/build/block/configure/faq_ask/0'))) .'
';
}
else { $output = null; }
$output .= '
';
// What permissions does this user have?
$can_edit = user_access('administer faq') || user_access('administer nodes');
$is_expert = user_access('answer question');
$only_own = user_access('edit own faq');
$mode = 'edit';
if ($can_edit) {
$result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 ORDER BY tn.tid ASC, created ASC", $terms);
}
else { if ($is_expert) {
$mode = 'answer';
// Get the expert's terms.
$result = db_query('SELECT * FROM {faq_expert} WHERE uid=%d ORDER BY n.created ASC', $user->uid);
$terms = array();
while ($expert = db_fetch_array($result)) {
$terms[] = $expert['tid'];
}
// Check if this expert has any categories.
if (count($terms) == 0) {
return ''. t("For some strange reason, I couldn't find any categories for you.") .'
';
}
// Join the term_data table to select based on tid.
$result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND tn.tid IN (". db_placeholders($terms) .") ORDER BY tn.tid ASC, created ASC", $terms);
}
else { // Edit own.
$result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND n.uid=%d ORDER BY tn.tid ASC, created ASC", $user->uid);
}
}
// Get unpublished nodes that are type='faq'.
$items = array();
$prev_cat = -1;
while ($node = db_fetch_array($result)) {
$tid = $node['tid'];
if ($prev_cat == $tid) {
$items[] = l($node['title'], 'faq_ask/'. $mode .'/'. $node['nid']);
}
else {
if (count($items) > 0) {
$output .= theme('item_list', $items);
$items = array();
}
$term = taxonomy_get_term($tid);
$output .= '
'. $term->name .'
';
$prev_cat = $tid;
$items[] = l($node['title'], 'faq_ask/'. $mode .'/'. $node['nid']);
}
}
if (count($items)) {
$output .= theme('item_list', $items);
}
else {
$output .= ''. t('Currently there are no unanswered questions for you to view.') .'
';
}
return $output;
}