You are here

faq.module in Frequently Asked Questions 5.2

Same filename and directory in other branches
  1. 8 faq.module
  2. 5 faq.module
  3. 6 faq.module
  4. 7.2 faq.module
  5. 7 faq.module

The FAQ module allows users to create a FAQ page, with questions and answers displayed in different styles, according to the settings.

File

faq.module
View source
<?php

/**
 * @file
 * The FAQ module allows users to create a FAQ page, with questions and answers
 * displayed in different styles, according to the settings.
 */

/**
 * Implementation of hook_help().
 */
function faq_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/help#faq":
      $output .= '<p>' . t("This module allows users with the 'create faq' permission to create question and answer pairs which will be displayed on the 'faq' page.  The 'faq' page is automatically generated from the FAQ nodes configured and the layout of this page can be modified on the settings page.  Users will need the 'view faq page' permission in order to view the 'faq' page.") . '</p>' . '<p>' . t("To create a question and answer, the user must create a 'FAQ' node (Create content >> FAQ).  This screen allows the user to edit the question and answer text.  If the 'Taxonomy' module is enabled and there are some terms configured for the FAQ node type, it will also be possible to put the questions into different categories when editing.") . '</p>' . '<p>' . t("The 'Frequently Asked Questions' settings configuration screen will allow users with 'administer faq' permissions to specify different layouts of the questions and answers.") . '</p>' . '<p>' . t("All users with 'view faq page' permissions will be able to view the generated FAQ page at 'www.example.com/faq'.") . '</p>';
      return $output;
    case "admin/modules#description":
      return t('Allows the user to configure the layout of questions and answers on a FAQ page.');
    case "node/add/faq":
      return t('Add a question and answer to a FAQ list.');
  }
}

/**
 * Implementation of hook_perm().
 */
function faq_perm() {
  return array(
    'administer faq',
    'view faq page',
    'edit own faq',
    'edit faq',
    'create faq',
  );
}

/**
 * Implementation of hook_access().
 */
function faq_access($op, $node) {
  global $user;
  if ($op != 'create') {
    $node = (object) $node;
  }
  if ($op == 'create') {
    if (user_access('create faq')) {
      return TRUE;
    }
  }
  elseif ($op == 'update' || $op == 'delete') {
    if (user_access('edit faq')) {
      return TRUE;
    }
    elseif (user_access('edit own faq') && $user->uid == $node->uid) {
      return TRUE;
    }
  }
}

/**
 * Implementation of hook_menu().
 */
function faq_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'faq',
      'title' => variable_get('faq_title', 'Frequently Asked Questions'),
      'callback' => 'faq_page',
      'access' => user_access('view faq page'),
      'weight' => 1,
    );
    if (arg(0) == 'faq' && is_numeric(arg(1))) {
      $items[] = array(
        'path' => 'faq/' . arg(1),
        'title' => variable_get('faq_title', 'Frequently Asked Questions'),
        'callback' => 'faq_page',
        'callback arguments' => array(
          arg(1),
        ),
        'access' => user_access('view faq page'),
        'type' => MENU_CALLBACK,
      );
    }
    $items[] = array(
      'path' => 'admin/settings/faq',
      'title' => t('Frequently Asked Questions'),
      'callback' => 'faq_settings_page',
      'access' => user_access('administer faq'),
      'description' => t('Allows the user to configure the layout of questions and answers on a FAQ page.'),
    );
    $items[] = array(
      'path' => 'admin/settings/faq/general',
      'title' => t('General'),
      'description' => t('Allows the user to configure the header and descriptive text for the FAQ page.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'faq_general_settings_form',
      ),
      'access' => user_access('administer faq'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'admin/settings/faq/questions',
      'title' => t('Questions'),
      'description' => t('Allows the user to configure the layout of questions and answers on a FAQ page.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'faq_questions_settings_form',
      ),
      'access' => user_access('administer faq'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -9,
    );
    $items[] = array(
      'path' => 'admin/settings/faq/categories',
      'title' => t('Categories'),
      'description' => t('Allows the user to configure the layout of questions and answers using categories on a FAQ page.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'faq_categories_settings_form',
      ),
      'access' => user_access('administer faq'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -8,
    );
    $items[] = array(
      'path' => 'admin/settings/faq/weight',
      'title' => t('Weight'),
      'description' => t('Allows the user to configure the order of questions and answers on a FAQ page.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'faq_weight_settings_form',
      ),
      'access' => user_access('administer faq'),
      'type' => MENU_LOCAL_TASK,
      'weight' => -8,
    );
    $items[] = array(
      'path' => 'node/add/faq',
      'title' => t('FAQ'),
      'access' => user_access('create faq'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_node_info().
 *
 * Defines the FAQ node/content type.
 * @return
 *   An array, containing the title, module name and the description.
 */
function faq_node_info() {
  return array(
    'faq' => array(
      'name' => t('FAQ'),
      'module' => 'faq',
      'description' => t('A frequently asked question and its answer.'),
    ),
  );
}

/**
 * Defines the form where new questions and answers are written.
 *
 * @param &$node
 *   The node object.
 * @return
 *   The form elements in the $form array.
 */
function faq_form(&$node) {
  $type = node_get_types('type', $node);

  // Question.
  $form['title'] = array(
    '#type' => 'textarea',
    '#title' => t('Question'),
    '#default_value' => empty($node->question) ? $node->title : $node->question,
    '#required' => TRUE,
    '#weight' => -1,
    '#rows' => 3,
    '#description' => t('Question to be answered'),
  );

  // Answer.
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Answer'),
    '#default_value' => $node->body,
    '#rows' => 20,
    '#weight' => 0,
    '#required' => TRUE,
    '#description' => t('This is that answer to the question.  It will be filtered according to the input format.'),
  );
  $form['body_filter']['format'] = filter_form($node->format);
  return $form;
}

/**
 * Inserts the faq node question text into the 'faq_questions' table.
 *
 * @param $node
 *   The node object.
 */
function faq_insert($node) {
  $ret = db_query("INSERT INTO {faq_questions} (nid, vid, question) VALUES(%d, %d, '%s')", $node->nid, $node->vid, $node->title);
}

/**
 * Updates the faq node question text in the 'faq_questions' table.
 *
 * @param $node
 *   The node object.
 */
function faq_update($node) {
  if ($node->revision) {
    faq_insert($node);
  }
  else {
    db_query("UPDATE {faq_questions} SET question = '%s' WHERE nid = %d AND vid = %d", $node->title, $node->nid, $node->vid);
  }
}

/**
 * Deletes an FAQ node from the database.
 *
 * @param &$node
 *   Which node to delete.
 */
function faq_delete(&$node) {
  db_query('DELETE FROM {faq_weights} WHERE nid = %d', $node->nid);
  db_query('DELETE FROM {faq_questions} WHERE nid = %d', $node->nid);
}

/**
 * Implementation of hook_load().
 *
 * Initialises $node->question using the value in the 'faq_questions' table.
 *
 * @param $node
 *   The node object.
 */
function faq_load($node) {
  $result = db_fetch_object(db_query('SELECT question FROM {faq_questions} WHERE nid = %d AND vid = %d', $node->nid, $node->vid));
  if ($result) {
    $result->title = $result->question;
  }
  return $result;
}

/**
 * Implementation of hook_nodeapi().
 */
function faq_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'delete revision':
      db_query('DELETE FROM {faq_questions} WHERE nid = %d AND vid = %d', $node->nid, $node->vid);
      break;
  }
}

/**
 * Display a FAQ node.
 *
 * @param $node
 *   Which node to show.
 * @param $teaser
 *   Boolean variable, if set to TRUE, it will show only a short part of the
 *   content.
 * @param $page
 *   Boolean variable, if set to TRUE, it will setup the breadcrumbs.
 * @return
 *   The node object.
 */
function faq_view($node, $teaser = FALSE, $page = FALSE) {
  if ($page) {
    $breadcrumb = array();
    $breadcrumb[] = array(
      'path' => 'node/' . $node->nid,
    );
    if (module_exists("taxonomy") && $node->taxonomy) {
      foreach ($node->taxonomy as $term) {
        $current = $term;
        continue;
      }
      $breadcrumb[] = array(
        'path' => 'faq/' . $current->tid,
        'title' => $current->name,
      );
      while ($parents = taxonomy_get_parents($current->tid)) {
        $current = array_shift($parents);
        $breadcrumb[] = array(
          'path' => 'faq/' . $current->tid,
          'title' => $current->name,
        );
      }
    }
    $breadcrumb[] = array(
      'path' => 'faq',
      'title' => variable_get('faq_title', 'Frequently Asked Questions'),
    );
    $breadcrumb = array_reverse($breadcrumb);
    menu_set_location($breadcrumb);
  }
  $node = node_prepare($node, $teaser);
  return $node;
}

/**
 * Generates the settings form for the FAQ module.
 *
 * @param $op
 *   Default value is NULL; determines what are the permissions of the current
 *   user on the FAQ.
 * @return
 *   The output, which contains the HTML code for the settings form generated by *   drupal_get_form() function.
 */
function faq_settings_page($op = NULL) {
  $output .= drupal_get_form('faq_general_settings_form');
  return $output;
}

/**
 * Define a form to edit the page header and descriptive text.
 *
 * @return
 *   The general settings form code stored in the $form variable, before
 *   converted to HTML.
 */
function faq_general_settings_form() {
  $form['faq_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => variable_get('faq_title', 'Frequently Asked Questions'),
  );
  $form['body_filter']['faq_description'] = array(
    '#type' => 'textarea',
    '#title' => t('FAQ Description'),
    '#default_value' => variable_get('faq_description', ''),
    '#description' => t('Your FAQ description.  This will be placed at the top of the page, above the questions and can serve as an introductory text.'),
    '#rows' => 5,
  );
  $form['body_filter']['faq_description_format'] = filter_form(variable_get('faq_description_format', ''), NULL, array(
    'faq_description_format',
  ));
  return system_settings_form($form);
}

/**
 * Define the elements for the FAQ Settings page - Questions tab.
 *
 * @return
 *   The form code inside the $form array.
 */
function faq_questions_settings_form() {
  drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');
  $display_options['questions_inline'] = t('Questions inline');
  $display_options['questions_top'] = t('Clicking on question takes user to answer further down the page');
  $display_options['hide_answer'] = t('Clicking on question opens/hides answer under question');
  $display_options['new_page'] = t('Clicking on question opens the answer in a new page');
  $form['faq_display'] = array(
    '#type' => 'radios',
    '#options' => $display_options,
    '#title' => t('Page layout'),
    '#description' => t('This controls how the questions and answers are displayed on the page and what happens when someone clicks on the question.'),
    '#default_value' => variable_get('faq_display', 'questions_top'),
  );
  $form['faq_questions_misc'] = array(
    '#type' => 'fieldset',
    '#title' => t('Miscellaneous layout settings'),
    '#collapsible' => TRUE,
  );
  $form['faq_questions_misc']['faq_question_listing'] = array(
    '#type' => 'select',
    '#options' => array(
      'ol' => t('Ordered list'),
      'ul' => t('Unordered list'),
    ),
    '#title' => t('Questions listing style'),
    '#description' => t("This allows to select how the questions listing is presented.  It only applies to the layouts: 'Clicking on question takes user to answer further down the page' and 'Clicking on question opens the answer in a new page'.  An ordered listing would number the questions, whereas an unordered list will have a bullet to the left of each question."),
    '#default_value' => variable_get('faq_question_listing', 'ul'),
  );
  $form['faq_questions_misc']['faq_qa_mark'] = array(
    '#type' => 'checkbox',
    '#title' => t('Label questions and answers'),
    '#description' => t('This option is only valid for the "Questions Inline" layout.  It labels all questions on the faq page with the "question label" setting and all answers with the "answer label" setting.  For example these could be set to "Q:" and "A:".'),
    '#default_value' => variable_get('faq_qa_mark', FALSE),
  );
  $form['faq_questions_misc']['faq_question_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Question Label'),
    '#description' => t('The label to pre-pend to the question text in the "Questions Inline" layout if labelling is enabled.'),
    '#default_value' => variable_get('faq_question_label', 'Q:'),
  );
  $form['faq_questions_misc']['faq_answer_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Answer Label'),
    '#description' => t('The label to pre-pend to the answer text in the "Questions Inline" layout if labelling is enabled.'),
    '#default_value' => variable_get('faq_answer_label', 'A:'),
  );
  $form['faq_questions_misc']['faq_use_teaser'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use answer teaser'),
    '#description' => t("This enables the display of the answer teaser text instead of the full answer when using the 'Questions inline' or 'Clicking on question takes user to answer further down the page' display options.  This is useful when you have long descriptive text.  The user can see the full answer by clicking on the question."),
    '#default_value' => variable_get('faq_use_teaser', FALSE),
  );
  $form['faq_questions_misc']['faq_show_node_links'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show node links'),
    '#description' => t('This enables the display of links under the answer text on the faq page.  Examples of these links include "Read more", "Add comment".'),
    '#default_value' => variable_get('faq_show_node_links', FALSE),
  );
  $form['faq_questions_misc']['faq_back_to_top'] = array(
    '#type' => 'textfield',
    '#title' => t('"Back to Top" link text'),
    '#description' => t('This allows the user to change the text displayed for the links which return the user to the top of the page on certain page layouts.  Defaults to "Back to Top".  Leave blank to have no link.'),
    '#default_value' => variable_get('faq_back_to_top', 'Back to Top'),
  );
  $form['faq_questions_misc']['faq_disable_node_links'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable question links to nodes'),
    '#description' => t('This allows the user to prevent the questions being links to the faq node in all layouts except "Clicking on question opens the answer in a new page".'),
    '#default_value' => variable_get('faq_disable_node_links', FALSE),
  );
  $form['faq_questions_misc']['faq_default_sorting'] = array(
    '#type' => 'select',
    '#title' => t('Default Sorting for Non-Weighted FAQs'),
    '#options' => array(
      'DESC' => t('Date Descending'),
      'ASC' => t('Date Ascending'),
    ),
    '#description' => t("This controls the default weighting behaviour for new FAQ nodes which haven't been assigned a weight."),
    '#default_value' => variable_get('faq_default_sorting', 'DESC'),
  );
  return system_settings_form($form);
}

/**
 * Define the elements for the FAQ Settings page - categories tab.
 *
 * @return
 *   The form code inside the $form array.
 */
function faq_categories_settings_form() {
  if (!module_exists("taxonomy")) {
    drupal_set_message(t('Categorization of questions will not work without the "taxonomy" module being enabled.'), 'error');
  }
  drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');

  // Set up a hidden variable.
  $form['faq_display'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('faq_display', 'questions_top'),
  );
  $form['faq_use_categories'] = array(
    '#type' => 'checkbox',
    '#title' => t('Categorize questions'),
    '#description' => t('This allows the user to display the questions according to the categories configured on the add/edit FAQ page.  Use of sub-categories is only recommended for large lists of questions.  The Taxonomy module must be enabled.'),
    '#default_value' => variable_get('faq_use_categories', FALSE),
  );
  $category_options['categories_inline'] = t('Categories inline');
  $category_options['hide_qa'] = t('Clicking on category opens/hides questions and answers under category');
  $category_options['new_page'] = t('Clicking on category opens the questions/answers in a new page');
  $form['faq_category_display'] = array(
    '#type' => 'radios',
    '#options' => $category_options,
    '#title' => t('Categories layout'),
    '#description' => t('This controls how the categories are displayed on the page and what happens when someone clicks on the category.'),
    '#default_value' => variable_get('faq_category_display', 'categories_inline'),
  );
  $form['faq_category_misc'] = array(
    '#type' => 'fieldset',
    '#title' => t('Miscellaneous layout settings'),
    '#collapsible' => TRUE,
  );
  $form['faq_category_misc']['faq_category_listing'] = array(
    '#type' => 'select',
    '#options' => array(
      'ol' => t('Ordered list'),
      'ul' => t('Unordered list'),
    ),
    '#title' => t('Categories listing style'),
    '#description' => t("This allows to select how the categories listing is presented.  It only applies to the 'Clicking on category opens the questions/answers in a new page' layout.  An ordered listing would number the categories, whereas an unordered list will have a bullet to the left of each category."),
    '#default_value' => variable_get('faq_category_listing', 'ul'),
  );
  $form['faq_category_misc']['faq_count'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show FAQ count'),
    '#description' => t('This displays the number of questions in a category after the category name.'),
    '#default_value' => variable_get('faq_count', FALSE),
  );
  $form['faq_category_misc']['faq_answer_category_name'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display category name for answers'),
    '#description' => t("This allows the user to toggle the visibility of the category name above each answer section for the 'Clicking on question takes user to answer further down the page' question/answer display."),
    '#default_value' => variable_get('faq_answer_category_name', FALSE),
  );
  $form['faq_category_misc']['faq_group_questions_top'] = array(
    '#type' => 'checkbox',
    '#title' => t("Group questions and answers for 'Categories inline'"),
    '#description' => t("This controls how categories are implemented with the 'Clicking on question takes user to answer further down the page' question/answer display."),
    '#default_value' => variable_get('faq_group_questions_top', FALSE),
  );
  $form['faq_category_misc']['faq_hide_child_terms'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only show sub-categories when parent category is selected'),
    '#description' => t("This allows the user more control over how and when sub-categories are displayed.  It does not affect the 'Categories inline' display."),
    '#default_value' => variable_get('faq_hide_child_terms', FALSE),
  );
  $form['faq_category_misc']['faq_show_term_page_children'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show sub-categories on FAQ category pages'),
    '#description' => t("Sub-categories with 'faq' nodes will be displayed on the per category FAQ page.  This will also happen if 'Only show sub-categories when parent category is selected' is set."),
    '#default_value' => variable_get('faq_show_term_page_children', FALSE),
  );
  if (module_exists('taxonomy')) {
    $form['faq_category_advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced category settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $vocab_options = array();
    $vocabularies = taxonomy_get_vocabularies('faq');
    foreach ($vocabularies as $vid => $vobj) {
      $vocab_options[$vid] = $vobj->name;
    }
    $form['faq_category_advanced']['faq_omit_vocabulary'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Omit vocabulary'),
      '#description' => t('Terms from these vocabularies will be <em>excluded</em> from the FAQ pages.'),
      '#default_value' => variable_get('faq_omit_vocabulary', 0),
      '#options' => $vocab_options,
      '#multiple' => TRUE,
    );
    $form['faq_category_advanced']['faq_enable_term_links'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable FAQ taxonomy links'),
      '#description' => t('Node links to taxonomy terms will be re-written to point at the FAQ pages instead.'),
      '#default_value' => variable_get('faq_enable_term_links', 1),
    );
  }
  return system_settings_form($form);
}

/**
 * Define the elements for the FAQ Settings page - weight tab.
 *
 * @param $form_values
 *   The submitted form values.
 * @return
 *   The form code, before being converted to HTML format.
 */
function faq_weight_settings_form($form_values = NULL) {
  drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');
  drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  $use_categories = variable_get('faq_use_categories', FALSE);
  if (!$use_categories) {
    $step = "order";
  }
  elseif (!isset($form_values)) {
    $step = "categories";
  }
  else {
    $step = "order";
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;

  // Categorized q/a.
  if ($step == "categories") {

    // Get list of categories.
    $vocabularies = taxonomy_get_vocabularies('faq');
    $options = array();
    foreach ($vocabularies as $vid => $vobj) {
      $tree = taxonomy_get_tree($vid);
      foreach ($tree as $term) {
        if (!taxonomy_term_count_nodes($term->tid, 'faq')) {
          continue;
        }
        $options[$term->tid] = $term->name;
        $form['choose_cat']['faq_category'] = array(
          '#type' => 'select',
          '#title' => t('Choose a category'),
          '#description' => t('Choose a category that you wish to order the questions for.'),
          '#options' => $options,
          '#multiple' => FALSE,
        );
        $form['choose_cat']['search'] = array(
          '#type' => 'submit',
          '#value' => t('Search'),
        );
      }
    }
  }
  else {
    $options = array();
    $category = $form_values['faq_category'];
    $default_sorting = variable_get('faq_default_sorting', 'DESC');
    $default_weight = 0;
    if ($default_sorting != 'DESC') {
      $default_weight = 1000000;
    }
    if (empty($category)) {
      $category = 0;
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category);
      }
    }
    else {
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category, $category);
      }
    }
    while ($node = db_fetch_object($result)) {
      $title = drupal_strlen($node->title) <= 64 ? $node->title : drupal_substr($node->title, 0, 63) . '...';
      $options[$node->nid] = $title;
      $order .= "{$node->nid},";
    }
    $order = rtrim($order, ",");
    while ($node = db_fetch_object($date_result)) {
      $date_options[$node->nid] = $node->title;
      $date_order .= "{$node->nid},";
    }
    $date_order = rtrim($date_order, ",");
    $form['weight']['faq_node_order'] = array(
      '#type' => 'hidden',
      '#default_value' => $order,
    );
    $form['weight']['faq_node_date_order'] = array(
      '#type' => 'hidden',
      '#default_value' => $date_order,
    );
    $form['weight']['faq_category'] = array(
      '#type' => 'value',
      '#value' => $category,
    );
    $asc = '<a href="#" onclick="faq_order_by_date(\'ASC\');">ascending</a>';
    $desc = '<a href="#" onclick="faq_order_by_date(\'DESC\');">descending</a>';
    $form['weight']['order_no_cats'] = array(
      '#type' => 'select',
      '#title' => t('Question Order'),
      '#description' => t("This determines the order of the questions and answers on the FAQ page.  Just select one or more questions and use the arrows to change their position in the list.  You can also order the list by the question creation date !desc or !asc.", array(
        '!desc' => $desc,
        '!asc' => $asc,
      )),
      '#options' => $options,
      '#multiple' => TRUE,
      '#size' => min(20, count($options)),
    );
    $form['weight']['move_up'] = array(
      '#type' => 'markup',
      '#value' => '<input type="button" onclick="faq_move_selected_item_up();"
      value="&uarr;" class="faq-arrow"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
    );
    $form['weight']['move_down'] = array(
      '#type' => 'markup',
      '#value' => '<input type="button" onclick="faq_move_selected_item_down();"
      value="&darr;" class="faq-arrow"/><br />',
    );
    $form['update']['attach'] = array(
      '#type' => 'submit',
      '#value' => t('Save order'),
      '#weight' => 3,
      '#attributes' => array(
        'onclick' => 'faq_update_order();',
      ),
    );
    $form['#redirect'] = "admin/settings/faq/weight";
  }
  return $form;
}

/**
 * Save the options set by the user in the FAQ Settings - Weight tab.
 *
 * @param $form_id
 *   The id of the form.
 * @param &$form_values
 *   Array reference containing the form values submitted.
 */
function faq_weight_settings_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Save order')) {
    $order = explode(",", $form_values['faq_node_order']);
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        foreach ($order as $index => $nid) {
          $result = db_query("REPLACE INTO {faq_weights} (tid, nid, weight) VALUES(%d, %d, %d)", $form_values['faq_category'], $nid, $index);
        }
        break;
      case 'pgsql':
        foreach ($order as $index => $nid) {
          $result = db_query("DELETE FROM {faq_weights} WHERE tid = %d AND nid = %d", $form_values['faq_category'], $nid);
          $result = db_query("INSERT INTO {faq_weights} (tid, nid, weight) VALUES(%d, %d, %d)", $form_values['faq_category'], $nid, $index);
        }
        break;
    }
    drupal_set_message(t('Configuration has been updated.'));
  }
  else {
  }
}

/**
 * Function to display the faq page.
 *
 * @param $tid
 *   Default is 0, determines if the questions and answers on the page
 *   will be shown according to a category or non-categorized.
 * @return
 *   The output variable which contains an HTML formatted page with FAQ
 *   questions and answers.
 */
function faq_page($tid = 0) {
  if (module_exists('pathauto')) {
    _pathauto_include();
  }

  // Things to provide translations for.
  $default_values = array(
    t('Frequently Asked Questions'),
    t('Back to Top'),
    t('Q:'),
    t('A:'),
  );
  drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  if (arg(0) == 'faq') {
    drupal_set_title(check_plain(variable_get('faq_title', 'Frequently Asked Questions')));
  }
  if (!module_exists("taxonomy")) {
    $tid = 0;
  }

  // Configure the breadcrumb trail.
  $breadcrumb = array();
  if (!empty($tid)) {
    if (!drupal_lookup_path('alias', arg(0) . '/' . arg(1)) && module_exists('pathauto')) {
      $placeholders = pathauto_get_placeholders('taxonomy', taxonomy_get_term($tid));
      if ($alias = pathauto_create_alias('faq', 'insert', $placeholders, arg(0) . '/' . arg(1), arg(1))) {
        drupal_goto($alias);
      }
    }
    $current = taxonomy_get_term($tid);
    $breadcrumb[] = array(
      'path' => 'faq/' . $tid,
      'title' => $current->name,
    );
    while ($parents = taxonomy_get_parents($current->tid)) {
      $current = array_shift($parents);
      $breadcrumb[] = array(
        'path' => 'faq/' . $current->tid,
        'title' => $current->name,
      );
    }
    $breadcrumb[] = array(
      'path' => 'faq',
      'title' => variable_get('faq_title', 'Frequently Asked Questions'),
    );
    $breadcrumb = array_reverse($breadcrumb);
    menu_set_location($breadcrumb);
  }
  $faq_display = variable_get('faq_display', 'questions_top');
  $use_categories = variable_get('faq_use_categories', FALSE);
  if (!module_exists("taxonomy")) {
    $use_categories = FALSE;
  }
  $faq_path = drupal_get_path('module', 'faq') . '/includes';

  // Non-categorized questions and answers.
  if (!$use_categories) {
    if (!empty($tid)) {
      drupal_not_found();
      return;
    }
    $default_sorting = variable_get('faq_default_sorting', 'DESC');
    $default_weight = 0;
    if ($default_sorting != 'DESC') {
      $default_weight = 1000000;
      $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON w.nid = n.nid WHERE n.type='faq' AND n.status = 1 AND (w.tid = 0 OR w.tid IS NULL) ORDER BY weight, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight);
    }
    else {
      $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON w.nid = n.nid WHERE n.type='faq' AND n.status = 1 AND (w.tid = 0 OR w.tid IS NULL) ORDER BY weight, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight);
    }
    while ($row = db_fetch_object($result)) {
      $node = node_load($row->nid);
      if (node_access("view", $node)) {
        $nodes[] = $node;
      }
    }
    if (count($nodes)) {
      switch ($faq_display) {
        case 'questions_top':
          include_once $faq_path . '/faq.questions_top.inc';
          $output = theme('faq_questions_top', $nodes);
          break;
        case 'hide_answer':
          include_once $faq_path . '/faq.hide_answer.inc';
          $output = theme('faq_hide_answer', $nodes);
          break;
        case 'questions_inline':
          include_once $faq_path . '/faq.questions_inline.inc';
          $output = theme('faq_questions_inline', $nodes);
          break;
        case 'new_page':
          include_once $faq_path . '/faq.new_page.inc';
          $output = theme('faq_new_page', $nodes);
          break;
      }

      // End of switch.
    }
  }
  else {
    $output = '<a name="top"></a>';
    $category_display = variable_get('faq_category_display', 'categories_inline');
    $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
    $output .= "<br />";

    // If we're viewing a specific category/term.
    if ($tid != 0) {
      if ($term = taxonomy_get_term($tid)) {
        $title = variable_get('faq_title', 'Frequently Asked Questions');
        if (arg(0) == 'faq' && is_numeric(arg(1))) {
          drupal_set_title(check_plain($title . ($title ? ' - ' : '') . $term->name));
        }
        if ($category_display == 'hide_qa') {
          drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');
        }
        _display_faq_by_category($faq_display, $category_display, $term, 0, $output, $output_answers);
        $output = '<div class="content"><div class="faq">' . $output;
        $output .= $output_answers . '</div></div>';
        return $output;
      }
      else {
        drupal_not_found();
        return;
      }
    }
    $list_style = variable_get('faq_category_listing', 'ul');
    $vocabularies = taxonomy_get_vocabularies('faq');
    $vocab_omit = variable_get('faq_omit_vocabulary', array());
    foreach ($vocab_omit as $vid => $set) {
      if ($set) {
        unset($vocab_omit[$vid]);
      }
    }
    $vocabularies = array_diff_key($vocabularies, $vocab_omit);
    $items = array();
    $vocab_items = array();
    foreach ($vocabularies as $vid => $vobj) {
      if ($category_display == "new_page") {
        $vocab_items = _get_indented_faq_terms($vid, 0);
        $items = array_merge($items, $vocab_items);
      }
      else {
        if ($hide_child_terms && $category_display == 'hide_qa') {
          $tree = taxonomy_get_tree($vid, 0, -1, 1);
        }
        else {
          $tree = taxonomy_get_tree($vid);
        }
        foreach ($tree as $term) {
          switch ($category_display) {
            case 'hide_qa':
              drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');
            case 'categories_inline':
              if (taxonomy_term_count_nodes($term->tid, 'faq')) {
                _display_faq_by_category($faq_display, $category_display, $term, 1, $output, $output_answers);
              }
              break;
          }

          // End of switch (category_display).
        }

        // End of foreach term.
      }

      // End of foreach vocab.
    }

    // End of if $category_display != new_page.
    if ($category_display == "new_page") {
      $output = theme('item_list', $items, NULL, $list_style);
    }
  }
  $desc = '';
  $faq_description = variable_get('faq_description', '');
  $format = variable_get('faq_description_format', 0);
  if ($format) {
    $faq_description = check_markup($faq_description, $format, FALSE);
  }
  if (!empty($faq_description)) {
    $desc = '<div class="faq-description">' . $faq_description . "</div>\n";
  }
  $output = '<div class="content"><div class="faq">' . $desc . $output;
  $output .= $output_answers . "</div></div>\n";
  return $output;
}

/**
 * Display FAQ questions and answers filtered by category.
 *
 * @param $faq_display
 *   Define the way the FAQ is being shown; can have the values:
 *   'questions top',hide answers','questions inline','new page'.
 * @param $category_display
 *   The layout of categories which should be used.
 * @param $term
 *   The category / term to display FAQs for.
 * @param $display_header
 *   Set if the header will be shown or not.
 * @param &$output
 *   Reference which holds the content of the page, HTML formatted.
 * @param &$output_answer
 *   Reference which holds the answers from the FAQ, when showing questions
 *   on top.
 */
function _display_faq_by_category($faq_display, $category_display, $term, $display_header, &$output, &$output_answers) {
  $nodes = array();
  $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
  $default_sorting = variable_get('faq_default_sorting', 'DESC');
  $default_weight = 0;
  if ($default_sorting != 'DESC') {
    $default_weight = 1000000;
    $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON w.tid = tn.tid AND n.nid = w.nid WHERE n.type='faq' AND n.status = 1 AND tn.tid = '%d' ORDER BY weight, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $term->tid);
  }
  else {
    $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON w.tid = tn.tid AND n.nid = w.nid WHERE n.type='faq' AND n.status = 1 AND tn.tid = '%d' ORDER BY weight, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $term->tid);
  }

  // Get number of questions, and account for hidden sub-categories.
  $node_count = db_num_rows($result);
  if ($hide_child_terms) {
    $node_count = taxonomy_term_count_nodes($term->tid, 'faq');
  }

  // Save the nodes for output.
  while ($row = db_fetch_object($result)) {
    $node = node_load($row->nid);
    if (node_access("view", $node)) {
      $nodes[] = $node;
    }
    else {
      $node_count--;
    }
  }

  // Handle indenting of categories.
  $depth = 0;
  while ($depth < $term->depth) {
    $display_header = 1;
    $indent = '<div class="faq-category-indent">';
    $output .= $indent;
    $depth++;
  }

  // Set up the class name for hiding the q/a for a category if required.
  $faq_class = "faq-qa";
  if ($category_display == "hide_qa") {
    $faq_class = "faq-qa-hide";
  }

  // Get the taxonomy image.
  $term_image = '';
  if (module_exists('taxonomy_image')) {
    $term_image = taxonomy_image_display($term->tid, array(
      'class' => 'faq-tax-image',
    ));
  }
  $faq_path = drupal_get_path('module', 'faq') . '/includes';
  switch ($faq_display) {
    case 'questions_top':
      include_once $faq_path . '/faq.questions_top.inc';
      $output .= theme('faq_category_questions_top', $nodes, $node_count, $display_header, $category_display, $term, $faq_class, $term_image);
      $group_questions_top = variable_get('faq_group_questions_top', FALSE);
      if ($category_display != 'hide_qa' && $group_questions_top == FALSE) {
        $output_answers .= theme('faq_category_questions_top_answers', $nodes, $node_count, $display_header, $category_display, $term, $faq_class, $term_image);
      }
      break;
    case 'hide_answer':
      include_once $faq_path . '/faq.hide_answer.inc';
      $output .= theme('faq_category_hide_answer', $nodes, $node_count, $display_header, $category_display, $term, $faq_class, $term_image);
      break;
    case 'questions_inline':
      include_once $faq_path . '/faq.questions_inline.inc';
      $output .= theme('faq_category_questions_inline', $nodes, $node_count, $display_header, $category_display, $term, $faq_class, $term_image);
      break;
    case 'new_page':
      include_once $faq_path . '/faq.new_page.inc';
      $output .= theme('faq_category_new_page', $nodes, $node_count, $display_header, $category_display, $term, $faq_class, $term_image);
      break;
  }

  // End of switch (faq_display).
  // Handle indenting of categories.
  while ($depth > 0) {
    $output .= '</div>';
    $depth--;
  }
}

/**
 * Implementation of hook_block().
 *
 * Create the code of the FAQ page providing three block types: FAQ Categories,
 * Recent FAQs and Random FAQs.
 */
function faq_block($op = 'list', $delta = 0, $edit = array()) {
  static $vocabularies, $terms;
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('FAQ Categories');
      $blocks[1]['info'] = t('Recent FAQs');
      $blocks[2]['info'] = t('Random FAQs');
      return $blocks;
    case 'view':
      switch ($delta) {
        case 0:

          // FAQ Categories.
          if (module_exists("taxonomy")) {
            if (!isset($terms)) {
              $terms = array();
              $vocabularies = taxonomy_get_vocabularies('faq');
              $vocab_omit = variable_get('faq_omit_vocabulary', array());
              $vocabularies = array_diff_key($vocabularies, $vocab_omit);
              foreach ($vocabularies as $vocab) {
                foreach (taxonomy_get_tree($vocab->vid) as $term) {
                  if (taxonomy_term_count_nodes($term->tid, 'faq')) {
                    $terms[$term->name] = $term->tid;
                  }
                }
              }
            }
            if (count($terms) > 0) {
              $block['subject'] = t('FAQ Categories');
              $items = array();
              foreach ($terms as $name => $tid) {
                $items[] = l($name, 'faq/' . $tid);
              }
              $list_style = variable_get('faq_category_listing', 'ul');
              $block['content'] = theme('item_list', $items, NULL, $list_style);
            }
          }
          break;
        case 1:

          // Recent FAQs.
          $block['subject'] = t('Recent FAQs');
          $block['content'] = faq_highlights_block(variable_get('faq_block_recent_faq_count', 5));
          break;
        case 2:

          // Random FAQs.
          $block['subject'] = t('Random FAQs');
          $block['content'] = faq_random_highlights_block(variable_get('faq_block_random_faq_count', 5));
          break;
      }

      // End switch($delta).
      return $block;
    case 'configure':
      switch ($delta) {
        case 0:
          break;
        case 1:

          // Recent FAQs.
          $form['faq_block_recent_faq_count'] = array(
            '#type' => 'textfield',
            '#title' => t('Number of FAQs to show'),
            '#description' => t("This controls the number of FAQs that appear in the 'Recent FAQs' block"),
            '#default_value' => variable_get('faq_block_recent_faq_count', 5),
          );
          break;
        case 2:

          // Random FAQs.
          $form['faq_block_random_faq_count'] = array(
            '#type' => 'textfield',
            '#title' => t('Number of FAQs to show'),
            '#description' => t("This controls the number of FAQs that appear in the 'Random FAQs' block"),
            '#default_value' => variable_get('faq_block_random_faq_count', 5),
          );
          break;
      }

      // End switch($delta).
      return $form;
    case 'save':
      switch ($delta) {
        case 0:
          break;
        case 1:
          variable_set('faq_block_recent_faq_count', $edit['faq_block_recent_faq_count']);
          break;
        case 2:
          variable_set('faq_block_random_faq_count', $edit['faq_block_random_faq_count']);
          break;
      }

      // End switch($delta).
      return;
  }

  // End switch($op).
}

/**
 * Create the HTML output for the Recent FAQs block.
 *
 * @param $num
 *   The default value is 5; determines the number of FAQ entries to be shown.
 * @return
 *   The HTML-formatted code displaying the Recent FAQs.
 */
function faq_highlights_block($num = 5) {
  $disable_node_links = variable_get('faq_disable_node_links', FALSE);
  $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type='faq' AND n.status = 1 ORDER BY n.created DESC", "n", "nid"), 0, $num);
  $items = array();
  while ($row = db_fetch_object($result)) {
    $node = node_load(array(
      'nid' => $row->nid,
    ));
    $node = node_prepare($node);
    if (node_access("view", $node)) {
      if ($disable_node_links) {
        $items[] = check_plain($node->question);
      }
      else {
        $items[] = l($node->question, 'node/' . $node->nid);
      }
    }
  }
  $list_style = variable_get('faq_question_listing', 'ul');
  $output = theme('item_list', $items, NULL, $list_style);
  $output .= '<div class="faq-all-faqs-link">' . l(t('All FAQs'), 'faq') . '</div>';
  return $output;
}

/**
 * Create the HTML output for the Random FAQs block.
 *
 * @param $num
 *   The default value is 5; determines the number of FAQ entries to be shown.
 * @return
 *   The HTML-formatted code displaying the Random FAQs.
 */
function faq_random_highlights_block($num = 5) {
  $disable_node_links = variable_get('faq_disable_node_links', FALSE);
  $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type='faq' AND n.status = 1 ORDER BY RAND()", "n", "nid"), 0, $num);
  $items = array();
  while ($row = db_fetch_object($result)) {
    $node = node_load(array(
      'nid' => $row->nid,
    ));
    $node = node_prepare($node);
    if (node_access("view", $node)) {
      if ($disable_node_links) {
        $items[] = check_plain($node->question);
      }
      else {
        $items[] = l($node->question, 'node/' . $node->nid);
      }
    }
  }
  $list_style = variable_get('faq_question_listing', 'ul');
  $output = theme('item_list', $items, NULL, $list_style);
  $output .= '<div class="faq-all-faqs-link">' . l(t('All FAQs'), 'faq') . '</div>';
  return $output;
}

/**
 * Return a HTML formatted list of terms indented according to the term depth.
 *
 * @param $vid
 *   Vocabulary id.
 * @param $tid
 *   Term id.
 * @return
 *   Return a HTML formatted list of terms indented according to the term depth.
 */
function _get_indented_faq_terms($vid, $tid) {
  if (module_exists('pathauto')) {
    _pathauto_include();
  }
  $items = array();
  $faq_count = variable_get('faq_count', FALSE);
  $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
  $tree = taxonomy_get_tree($vid, $tid, -1, 1);
  foreach ($tree as $term) {
    $tree_count = taxonomy_term_count_nodes($term->tid, 'faq');
    if ($tree_count) {

      // Get taxonomy image.
      $term_image = '';
      if (module_exists('taxonomy_image')) {
        $term_image = taxonomy_image_display($term->tid, array(
          'class' => 'faq-tax-image',
        ));
      }

      // Get term description.
      $desc = theme('faq_category_description', check_markup($term->description));

      // See if this term has any nodes itself, should it be a link?
      $result = db_query(db_rewrite_sql("SELECT COUNT(n.nid) AS c FROM {term_node} tn INNER JOIN {node} n ON n.nid = tn.nid WHERE n.status = 1 AND n.type = 'faq' AND tn.tid = '%d' ", "n", "nid"), $term->tid);
      $term_node_count = db_fetch_object($result);
      if ($term_node_count->c > 0) {
        $path = "faq/{$term->tid}";
        if (!drupal_lookup_path('alias', arg(0) . '/' . $term->tid) && module_exists('pathauto')) {
          $placeholders = pathauto_get_placeholders('taxonomy', $term);
          $path = pathauto_create_alias('faq', 'insert', $placeholders, arg(0) . '/' . $term->tid, $term->tid);
        }
        if ($faq_count) {
          $node_count = $term_node_count->c;
          if ($hide_child_terms) {
            $node_count = $tree_count;
          }
          $cur_item = $term_image . l($term->name, $path) . " ({$node_count}) " . $desc;
        }
        else {
          $cur_item = $term_image . l($term->name, $path) . $desc;
        }
      }
      else {
        $cur_item = $term_image . check_plain($term->name) . $desc;
      }
      if (!empty($term_image)) {
        $cur_item .= '<div class="clear-block"></div>';
      }
      if (!$hide_child_terms) {
        $term_items = _get_indented_faq_terms($vid, $term->tid);
      }
      $items[] = array(
        "data" => $cur_item,
        "children" => $term_items,
      );
    }
  }
  return $items;
}

/**
 * Get a list of terms associated with the FAQ nodes.
 *
 * @return
 *   Return the HTML-formatted content.
 */
function faq_get_terms() {
  $items = array();
  $vocabularies = taxonomy_get_vocabularies('faq');
  $vocab_omit = variable_get('faq_omit_vocabulary', array());
  $vocabularies = array_diff_key($vocabularies, $vocab_omit);
  foreach ($vocabularies as $vid => $vobj) {
    $vocab_items = _get_indented_faq_terms($vid, 0);
    $items = array_merge($items, $vocab_items);
  }
  return theme('item_list', $items);
}

/**
 * Format the output for the faq_site_map() function.
 *
 * @return
 *   Return a list of FAQ categories if categorization is enabled, otherwise
 *   return a list of faq nodes.
 */
function faq_get_faq_list() {

  // Return list of vocab terms if categories are configured.
  $use_categories = variable_get('faq_use_categories', FALSE);
  if ($use_categories) {
    return faq_get_terms();
  }

  // Otherwise return list of weighted FAQ nodes.
  $items = array();
  $default_sorting = variable_get('faq_default_sorting', 'DESC');
  $default_weight = 0;
  if ($default_sorting != 'DESC') {
    $default_weight = 1000000;
    $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON w.nid = n.nid WHERE n.type='faq' AND n.status = 1 AND (w.tid = 0 OR w.tid IS NULL) ORDER BY weight, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight);
  }
  else {
    $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON w.nid = n.nid WHERE n.type='faq' AND n.status = 1 AND (w.tid = 0 OR w.tid IS NULL) ORDER BY weight, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight);
  }
  while ($row = db_fetch_object($result)) {
    $node = node_load($row->nid);
    if (node_access("view", $node)) {
      $items[] = l($node->question, "node/{$node->nid}");
    }
  }
  return theme('item_list', $items);
}

/**
 * Implementation of hook_site_map().
 *
 * Not needed by the latest site_map module as it implements a _site_map_faq()
 * function itself.  Leaving this code here in case this changes again.
 *
 * Create a sitemap, by showing all the FAQ entries list - categorized or not -
 * using faq_get_faq_list() function.
 * @return
 *   Return a list of FAQ categories if categorization is enabled, otherwise
 *   return a list of faq nodes.
 */

/*
function faq_site_map() {
 $title = variable_get('faq_title', 'Frequently Asked Questions');
 $output = faq_get_faq_list();
 return theme('box', $title, $output);
}
*/

/**
 * Implementation of hook_link_alter().
 *
 * Change the term links on a node to point at the appropriate faq page.
 */
function faq_link_alter(&$node, &$links) {
  static $vocabularies;
  if ($node->type != 'faq' || !variable_get('faq_use_categories', FALSE) || !module_exists("taxonomy") || !variable_get('faq_enable_term_links', 1)) {
    return;
  }
  if (!isset($vocabularies)) {
    $vocabularies = taxonomy_get_vocabularies('faq');
    $vocab_omit = array_flip(variable_get('faq_omit_vocabulary', array()));
    $vocabularies = array_diff_key($vocabularies, $vocab_omit);
  }
  foreach ($links as $module => $link) {
    if (strpos($module, 'taxonomy_term') !== FALSE) {

      // Link back to the faq and not the taxonomy term page. We'll only
      // do this if the taxonomy term in question belongs to faq vocab.
      $tid = str_replace('taxonomy/term/', '', $link['href']);
      $term = taxonomy_get_term($tid);
      foreach ($vocabularies as $vid => $vobj) {
        if ($term->vid == $vid && taxonomy_term_count_nodes($term->tid, 'faq')) {
          $links[$module]['href'] = str_replace('taxonomy/term', 'faq', $link['href']);
          break;
        }
      }
    }
  }
}
if (!function_exists('array_diff_key')) {
  function array_diff_key() {
    $arrs = func_get_args();
    $result = array_shift($arrs);
    foreach ($arrs as $array) {
      foreach ($result as $key => $v) {
        if (array_key_exists($key, $array)) {
          unset($result[$key]);
        }
      }
    }
    return $result;
  }
}

/**
 * Implementation of hook_term_path().
 */
function faq_term_path($term) {
  return 'faq/' . $term->tid;
}

/**
 * Theme function to format a question.
 *
 * @param $node
 *   The node object.
 * @param $path
 *   The path/url which the question should link to if links are disabled.
 * @param $anchor
 *   Link anchor to use in question links; for "questions top" layout.
 * @param $question_label
 *   The question label, if set.
 * @param $class
 *   An additional class name to assign the div containing the question.
 * @return
 *   HTML formatted string containing the question.
 */
function theme_faq_format_question($node, $path = NULL, $anchor = NULL, $question_label = '', $class = NULL) {
  $disable_node_links = variable_get('faq_disable_node_links', FALSE);

  // Setup the question div.
  $question = '<div class="faq-question">';
  if (!empty($class)) {
    $question = '<div class="faq-question ' . $class . '">';
  }

  // Don't link to faq node, instead provide no link, or link to current page.
  if ($disable_node_links) {
    if (empty($path) && empty($anchor)) {
      $question .= check_plain($question_label) . check_plain($node->question);
    }
    elseif (empty($path)) {
      $question .= '<a name="' . $anchor . '" id="' . $anchor . '">' . check_plain($question_label) . check_plain($node->question) . '</a>';
    }
    else {
      $question .= l($question_label . $node->question, $path);
    }
  }
  else {
    if (empty($anchor)) {
      $question .= l($question_label . $node->question, "node/{$node->nid}");
    }
    else {
      $question .= l($question_label . $node->question, "node/{$node->nid}", array(
        "name" => "{$anchor}",
        "id" => "{$anchor}",
      ));
    }
  }
  $question .= "</div>\n";
  return $question;
}

/**
 * Helper function to setup the faq answer.
 *
 * @param $node
 *   The node object.
 * @param $back_to_top
 *   An array containing the "back to top" link.
 * @param $teaser
 *   Whether or not to use teasers.
 * @param $links
 *   Whether or not to show node links.
 * @param $answer_label
 *   Answer label, if set.
 * @param $class
 *   An additional class name to assign the div containing the answer.
 * @return
 *   HTML formatted string containing the answer.
 */
function faq_view_answer($node, $back_to_top, $teaser, $links, $answer_label = '', $class = NULL) {

  // Build the faq node content and invoke other modules' links, etc, functions.
  $node = (object) $node;
  $node = node_build_content($node, $teaser, 0);
  if ($links) {
    $node->links = module_invoke_all('link', 'node', $node, $teaser);
    foreach (module_implements('link_alter') as $module) {
      $function = $module . '_link_alter';
      $function($node, $node->links);
    }
  }

  // Add "edit answer" link if they have the correct permissions.
  if (node_access('update', $node)) {
    $node->links['faq_edit_link'] = array(
      'title' => t('Edit answer'),
      'href' => "node/{$node->nid}/edit",
      'query' => drupal_get_destination(),
      'attributes' => array(
        'title' => t('Edit answer'),
      ),
    );
  }

  // Add "back to top" link.
  if (!empty($back_to_top)) {
    $node->links['faq_back_to_top'] = $back_to_top;
  }
  $content = drupal_render($node->content);
  node_invoke_nodeapi($node, 'alter', $teaser, 0);

  // Unset unused $node text so that a bad theme can not open a security hole.
  $node->body = $content;
  $node->teaser = NULL;
  return theme('faq_format_answer', $node, check_plain($answer_label), $class);
}

/**
 * Theme function to format an answer.
 *
 * @param $node
 *   The node object.
 * @param $answer_label
 *   Answer label, if set.
 * @param $class
 *   An additional class name to assign the div containing the answer.
 * @return
 *   HTML formatted string containing the answer.
 */
function theme_faq_format_answer($node, $answer_label = '', $class = NULL) {

  // Setup the HTML formatted answer.
  $answer = '<div class="faq-answer">';
  if ($class) {
    $answer = '<div class="faq-answer ' . $class . '">';
  }
  $answer .= '<strong>' . $answer_label . '</strong>' . $node->body;
  if ($node->links) {
    $answer .= '<div class="links">' . theme('links', $node->links) . '</div>';
  }
  $answer .= "</div>\n";
  return $answer;
}

/**
 * Helper function to setup the "back to top" link.
 *
 * @param $path
 *   The path/url where the "back to top" link should bring the user too.  This
 *   could be the 'faq' page or one of the categorized faq pages, e.g 'faq/123'
 *   where 123 is the tid.
 * @return
 *   An array containing the "back to top" link.
 */
function faq_init_back_to_top($path) {
  $back_to_top_text = trim(variable_get('faq_back_to_top', 'Back to Top'));
  if (!empty($back_to_top_text)) {
    $back_to_top = array(
      'title' => check_plain($back_to_top_text),
      'href' => $path,
      'attributes' => array(
        'title' => t('Go back to the top of the page.'),
      ),
      'fragment' => 'top',
      'html' => TRUE,
    );
  }
  return $back_to_top;
}

/**
 * Theme function for formatting the faq category headers.
 *
 * @param $term
 *   The term which a header should be generated for.
 * @param $term_image
 *   HTML output containing any taxonomy images attached to the taxonomy term.
 * @param $display_header
 *   Set if the header will be shown or not.
 * @param $faq_count
 *   Boolean value controlling whether the number of faq nodes in a category
 *   should be displayed or not.
 * @param $show_term_page_children
 *   Boolean value controlling if sub-categories should be displayed on FAQ
 *   category pages.
 * @param $hide_child_terms
 *   If true, sub-categories are only shown when parent category is selected.
 * @param $category_display
 *   The layout of categories which should be used.
 * @param $class
 *   Optional class name to assign the header div; overrides the default.
 */
function theme_faq_category_header($term, $term_image, $display_header, $faq_count, $show_term_page_children, $hide_child_terms, $category_display, $class = "faq-category-group") {

  // Configure header.
  $hdr = "h5";
  if ($term->depth > 0) {
    $hdr = "h6";
  }
  $header = $term_image;
  $header .= "<{$hdr} class=\"faq-header\">";
  if ($category_display == 'hide_qa') {
    $header .= l($term->name, "faq/{$term->tid}");
  }
  else {
    $header .= check_plain($term->name);
  }
  if ($faq_count) {
    $header .= " (%%FAQ_COUNT%%)";
  }
  $header .= '</' . $hdr . ">\n";

  // #298153 - separated out for Dreamweaver bug.
  // Configure category description.
  $desc = theme('faq_category_description', check_markup($term->description));

  // Get list of sub-categories if necessary.
  $child_terms = '';
  if (($show_term_page_children || $hide_child_terms) && $category_display == 'new_page') {
    $child_categories = faq_view_child_category_headers($term, $faq_count);
    $list_style = variable_get('faq_category_listing', 'ul');
    $child_terms .= theme('item_list', $child_categories, NULL, $list_style, array(
      "class" => "faq-category-list",
    ));
  }
  $output = '<div class="' . $class . '">' . "\n";
  if ($display_header == 1) {
    $output .= '<div class="faq-qa-header">' . $header . $desc . "</div>\n";
  }
  elseif (!empty($term_image) || !empty($desc)) {
    $output .= '<div class="faq-qa-header">' . $term_image . $desc . "</div>\n";
  }
  $output .= $child_terms;
  return $output;
}

/**
 * Helper function for retrieving the sub-categories faqs.
 *
 * @param $term
 *   The category / term to display FAQs for.
 * @param $theme_function
 *   Theme function to use to format the Q/A layout for sub-categories.
 * @param $default_weight
 *   Is 0 for $default_sorting = DESC; is 1000000 for $default_sorting = ASC.
 * @param $default_sorting
 *   If 'DESC', nodes are sorted by creation date descending; if 'ASC', nodes
 *   are sorted by creation date ascending.
 * @param $category_display
 *   The layout of categories which should be used.
 * @param $class
 *   CSS class which the HTML div will be using. A special class name is
 *   required in order to hide and questions / answers.
 */
function faq_get_child_categories_faqs($term, $theme_function, $default_weight, $default_sorting, $category_display, $class) {
  $output = '';
  $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
  $list = taxonomy_get_children($term->tid);
  foreach ($list as $tid => $child_term) {
    if (taxonomy_term_count_nodes($child_term->tid, 'faq')) {
      $nodes = array();
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON w.tid = tn.tid AND n.nid = w.nid WHERE n.type='faq' AND n.status = 1 AND tn.tid = '%d' ORDER BY weight, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $child_term->tid);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON w.tid = tn.tid AND n.nid = w.nid WHERE n.type='faq' AND n.status = 1 AND tn.tid = '%d' ORDER BY weight, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $child_term->tid);
      }

      // Get number of questions, and account for hidden sub-categories.
      $node_count = db_num_rows($result);
      if ($hide_child_terms) {
        $node_count = taxonomy_term_count_nodes($child_term->tid, 'faq');
      }

      // Save the nodes for output.
      while ($row = db_fetch_object($result)) {
        $node = node_load($row->nid);
        if (node_access("view", $node)) {
          $nodes[] = $node;
        }
      }

      // Get the taxonomy image.
      $term_image = '';
      if (module_exists('taxonomy_image')) {
        $term_image = taxonomy_image_display($child_term->tid, array(
          'class' => 'faq-tax-image',
        ));
      }

      // Format the sub-category listing.
      $output .= '<div class="faq-category-indent">';
      $output .= theme($theme_function, $nodes, $node_count, 1, $category_display, $child_term, $class, $term_image);
      $output .= '</div>';
    }
  }
  return $output;
}

/**
 * Helper function to setup the list of sub-categories for the header.
 *
 * @param $term
 *   The term to setup the list of child terms for.
 * @param $faq_count
 *   Boolean value controlling whether the number of faq nodes in a category
 *   should be displayed or not.
 * @return
 *   An array of sub-categories.
 */
function faq_view_child_category_headers($term, $faq_count) {
  $list = taxonomy_get_children($term->tid);
  $child_categories = array();
  foreach ($list as $tid => $child_term) {
    $term_node_count = taxonomy_term_count_nodes($child_term->tid, 'faq');
    if ($term_node_count) {

      // Get taxonomy image.
      $term_image = '';
      if (module_exists('taxonomy_image')) {
        $term_image = taxonomy_image_display($child_term->tid, array(
          'class' => 'faq-tax-image',
        ));
      }

      // Configure child term description.
      $child_term_desc = theme('faq_category_description', check_markup($child_term->description));

      // Configure child term header.
      $child_categories[] = theme('faq_child_category_header', $child_term, $term_image, $child_term_desc, $faq_count, $term_node_count);
    }
  }
  return $child_categories;
}

/**
 * Theme function to format the category description.
 *
 * @param $term_description
 *   Description associated with the taxonomy term / category.
 * @return
 *   HTML formatted description text.
 */
function theme_faq_category_description($term_description = '') {
  $description = '';
  if (!empty($term_description)) {
    $description = '<div class="faq-qa-description">';
    $description .= $term_description . "</div>\n";
  }
  return $description;
}

/**
 * Theme function to format the sub-category header.
 *
 * @param $term
 *   The child term which a header should be generated for.
 * @param $term_image
 *   HTML output containing any taxonomy images attached to the taxonomy term.
 * @param $description
 *   Description associated with the taxonomy term / category.
 * @param $faq_count
 *   Boolean value controlling whether the number of faq nodes in a category
 *   should be displayed or not.
 * @param $node_count
 *   Number of nodes for this term, and possibly also the sub-terms.
 * @return
 *   HTML formatted header text, with images, node count, etc.
 */
function theme_faq_child_category_header($term, $term_image, $description, $faq_count, $node_count) {
  if ($faq_count) {
    $header = $term_image . l($term->name, "faq/{$term->tid}") . " ({$node_count}) {$description}";
  }
  else {
    $header = $term_image . l($term->name, "faq/{$term->tid}") . $description;
  }
  if (!empty($term_image)) {
    $header .= '<div class="clear-block"></div>';
  }
  return $header;
}

/**
 * Theme function to format the sub-category header to display with the answers.
 *
 * @param $term
 *   The child term which a header should be generated for.
 * @param $term_image
 *   HTML output containing any taxonomy images attached to the taxonomy term.
 * @return
 *   HTML formatted string containing the category header for answer sections in
 *   the "questions top" layout.
 */
function theme_faq_answer_category_header($term, $term_image) {
  $hdr = "h5";
  if ($term->depth > 0) {
    $hdr = "h6";
  }
  $answer_header = $term_image;
  $answer_header .= "<{$hdr} class=\"faq-header\">";
  $answer_header .= check_plain($term->name);
  $answer_header .= '</' . $hdr . ">\n";

  // #298153 - separated for Dreamweaver.
  return $answer_header;
}

/**
 * Implementation of hook_pathauto().
 */
function faq_pathauto($op) {
  switch ($op) {
    case 'settings':
      $settings = array();
      $settings['module'] = 'faq';
      $settings['token_type'] = 'taxonomy';
      $settings['groupheader'] = t('FAQ category path settings');
      $settings['patterndescr'] = t('Default path pattern (applies to all FAQ categories with blank patterns below)');
      $settings['patterndefault'] = t('faq/[catpath-raw]');
      $patterns = token_get_list('taxonomy');
      foreach ($patterns as $type => $pattern_set) {
        if ($type != 'global') {
          foreach ($pattern_set as $pattern => $description) {
            $settings['placeholders']['[' . $pattern . ']'] = $description;
          }
        }
      }
      return (object) $settings;
    default:
      break;
  }
}

Functions

Namesort descending Description
faq_access Implementation of hook_access().
faq_block Implementation of hook_block().
faq_categories_settings_form Define the elements for the FAQ Settings page - categories tab.
faq_delete Deletes an FAQ node from the database.
faq_form Defines the form where new questions and answers are written.
faq_general_settings_form Define a form to edit the page header and descriptive text.
faq_get_child_categories_faqs Helper function for retrieving the sub-categories faqs.
faq_get_faq_list Format the output for the faq_site_map() function.
faq_get_terms Get a list of terms associated with the FAQ nodes.
faq_help Implementation of hook_help().
faq_highlights_block Create the HTML output for the Recent FAQs block.
faq_init_back_to_top Helper function to setup the "back to top" link.
faq_insert Inserts the faq node question text into the 'faq_questions' table.
faq_link_alter Implementation of hook_link_alter().
faq_load Implementation of hook_load().
faq_menu Implementation of hook_menu().
faq_nodeapi Implementation of hook_nodeapi().
faq_node_info Implementation of hook_node_info().
faq_page Function to display the faq page.
faq_pathauto Implementation of hook_pathauto().
faq_perm Implementation of hook_perm().
faq_questions_settings_form Define the elements for the FAQ Settings page - Questions tab.
faq_random_highlights_block Create the HTML output for the Random FAQs block.
faq_settings_page Generates the settings form for the FAQ module.
faq_term_path Implementation of hook_term_path().
faq_update Updates the faq node question text in the 'faq_questions' table.
faq_view Display a FAQ node.
faq_view_answer Helper function to setup the faq answer.
faq_view_child_category_headers Helper function to setup the list of sub-categories for the header.
faq_weight_settings_form Define the elements for the FAQ Settings page - weight tab.
faq_weight_settings_form_submit Save the options set by the user in the FAQ Settings - Weight tab.
theme_faq_answer_category_header Theme function to format the sub-category header to display with the answers.
theme_faq_category_description Theme function to format the category description.
theme_faq_category_header Theme function for formatting the faq category headers.
theme_faq_child_category_header Theme function to format the sub-category header.
theme_faq_format_answer Theme function to format an answer.
theme_faq_format_question Theme function to format a question.
_display_faq_by_category Display FAQ questions and answers filtered by category.
_get_indented_faq_terms Return a HTML formatted list of terms indented according to the term depth.