You are here

function faq_ask_settings_form in FAQ_Ask 6.2

Same name and namespace in other branches
  1. 6 faq_ask.module \faq_ask_settings_form()
  2. 7 faq_ask.module \faq_ask_settings_form()

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.

1 string reference to 'faq_ask_settings_form'
faq_ask_menu in ./faq_ask.module
Implementation of hook_menu().

File

./faq_ask.module, line 820
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.

Code

function faq_ask_settings_form(&$form_state, $op = NULL, $aid = NULL) {
  $form = array();

  // Set a basic message that will be unset once we pass the error checking.
  $form['error'] = array(
    '#value' => t('Errors were found, please correct them before proceeding.'),
    '#weight' => -10,
  );
  $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 <a href="@url">settings page</a> to configure this module.', array(
      '@url' => url('admin/settings/faq/categories'),
    )), 'error');
    return $form;
  }

  // Get the list of vocabularies that apply to FAQ s.
  $vocabs = taxonomy_get_vocabularies('faq');
  if (count($vocabs) == 0) {
    drupal_set_message(t('The Faq_Ask module requires that at least one vocabulary apply to the "faq" content type. Please go to the Taxonomy <a href="@taxo_uri">configuration page</a> to do this.', array(
      '@taxo_uri' => url('admin/content/taxonomy'),
    )), 'error');
    return $form;
  }

  // Get the admin's name.
  $admin = ucwords(db_result(db_query('SELECT name FROM {users} WHERE uid=1')));

  // Get the Simplenews newsletters if they exists
  $newsletters = array(
    '0' => t('No newsletter'),
  );
  if (module_exists('simplenews')) {
    if (!function_exists('simplenews_get_newsletters')) {
      drupal_set_message(t('The Simplenews integration is not compatible with this version of Simplenews. Please download a later version.'), 'error');
    }
    else {
      $list = simplenews_get_newsletters(variable_get('simplenews_vid', ''));
      foreach ($list as $key => $object) {
        $list[$key] = $object->name;
      }
      $newsletters += $list;
    }
  }
  $form['notification'] = array(
    '#type' => 'fieldset',
    '#title' => t('Notifications'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['notification']['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['notification']['notify_asker'] = array(
    '#type' => 'fieldset',
    '#title' => T('Asker notification'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['notification']['notify_asker']['faq_ask_asker_notify'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify askers'),
    '#description' => t('If this box is checked, the asker creating the question will be notified via email that their question is answered.'),
    '#default_value' => variable_get('faq_ask_notify_asker', 0),
  );

  //Issue #1479362 by stenjo: Indication of asker notification
  $form['notification']['notify_asker']['faq_ask_asker_notify_indication'] = array(
    '#type' => 'textfield',
    '#title' => t('Notify asker indication'),
    '#description' => t('If this box is not blank the titles on the unanswered list will be added the string in this box to indicate an asker notification.'),
    '#size' => 5,
    '#maxlength' => 5,
    '#default_value' => variable_get('faq_ask_asker_notify_indication', ' [n]'),
  );
  $form['notification']['notify_asker']['faq_ask_asker_notify_cron'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use cron for asker notification'),
    '#description' => t('If this box is checked, the asker notifications will be sendt via cron.'),
    '#default_value' => variable_get('faq_ask_notify_by_cron', TRUE),
    '#disabled' => !variable_get('faq_ask_notify_asker', 0),
  );

  // If the Simplenews module is loaded we can add functionality to add anonymous askers to a newsletter
  $form['notification']['notify_asker']['faq_ask_notify_asker_simplenews'] = array(
    '#type' => 'select',
    '#title' => t('Add anonymous asker to newsletter'),
    '#default_value' => variable_get('faq_ask_notify_asker_simplenews_tid', '0'),
    '#options' => $newsletters,
    '#description' => module_exists('simplenews') ? t('Select a newsletter you want anonymous askers to be assigned to.') : t('This functionality needs the <a href="http://drupal.org/project/simplenews">Simplenews module</a> to be activated.'),
    '#disabled' => !module_exists('simplenews'),
  );
  $form['notification']['notify_asker']['faq_ask_notify_asker_simplenews_confirm'] = array(
    '#type' => 'checkbox',
    '#title' => t('Confirm subscription to newsletter'),
    '#description' => t('If this box is checked, the asker creating the question will be asked to confirm the subscription of the newsletter.'),
    '#default_value' => variable_get('faq_ask_notify_asker_simplenews_confirm', 1),
    '#disabled' => !module_exists('simplenews'),
  );
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['options']['faq_ask_categorize'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only expert can categorize'),
    '#description' => t('If this box is checked, only an expert answering a question can add a category.'),
    '#default_value' => variable_get('faq_ask_categorize', FALSE),
  );

  // Issue #1479352 by stenjo: Compact display of unanswered questions
  $form['options']['faq_ask_compact_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Display compact unanswered list threshold'),
    '#description' => t('Set this to a number that will cause the unanswered list to appear in compact form. Leave this blank or set to zero for no compact display.'),
    '#size' => 5,
    '#maxlength' => 5,
    '#default_value' => variable_get('faq_ask_compact_limit', ''),
  );
  $give_options = array(
    0 => t('Asker retains ownerhsip'),
    1 => t('Anonymous questions reassigned to expert'),
    2 => t('All questions reassigned to expert'),
  );
  $form['options']['faq_ask_expert_own'] = array(
    '#type' => 'radios',
    '#options' => $give_options,
    '#title' => t('Give ownership to the expert'),
    '#description' => t('This determines if questions will be reassigned to the expert when answered.'),
    '#default_value' => variable_get('faq_ask_expert_own', 0),
  );
  $form['options']['faq_ask_unanswered'] = array(
    '#type' => 'textarea',
    '#title' => t('Default unanswered body text'),
    '#cols' => 60,
    '#rows' => 1,
    '#description' => t('This text will be inserted into the body of questions when they are asked. This helps make editing easier'),
    '#default_value' => variable_get('faq_ask_unanswered', t('Not answered yet.')),
  );
  $form['options']['faq_ask_expert_advice'] = array(
    '#type' => 'textarea',
    '#title' => t('Answer advice for the expert'),
    '#cols' => 60,
    '#rows' => 1,
    '#description' => t('This text will be shown at the bottom of the "Unanswered questions" block.'),
    '#default_value' => variable_get('faq_ask_expert_advice', _faq_ask_advice_default()),
  );
  $form['options']['advice']['faq_ask_admin_advice'] = array(
    '#type' => 'textarea',
    '#title' => t('Advice for an administrator/editor'),
    '#cols' => 60,
    '#rows' => 1,
    '#default_value' => variable_get('faq_ask_admin_advice', _faq_ask_advice_default('admin')),
  );
  $form['options']['advice']['faq_ask_asker_advice'] = array(
    '#type' => 'textarea',
    '#title' => t('Advice for an asker'),
    '#cols' => 60,
    '#rows' => 1,
    '#default_value' => variable_get('faq_ask_asker_advice', _faq_ask_advice_default('asker')),
  );
  $help_default = variable_get('faq_ask_help_text', _faq_ask_help_default());
  $form['options']['faq_ask_help_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Help text for the asker'),
    '#cols' => 60,
    '#rows' => drupal_strlen($help_default) / 60,
    '#description' => t('This text will be shown at the top of the "Ask a Question" page.'),
    '#default_value' => $help_default,
  );
  $form['experts'] = array(
    '#type' => 'fieldset',
    '#title' => t('Experts'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Use the list of vocabularies from above.
  if (count($vocabs) == 1) {

    // Single vocabulary, don't bother with a selection box, just set it.
    $vid = key($vocabs);
    $def_vid = $vid;
    variable_set('faq_ask_vocabularies', array(
      $vid => $vid,
    ));
    $vobj = $vocabs[$vid];
    $free = $vobj->tags;
  }
  else {

    // Multiple vocabs available.
    $voc_list = array();
    $def_vid = 0;

    // variable_get('faq_ask_vocabularies', 0)?
    foreach ($vocabs as $vid => $vobj) {
      $voc_list[$vid] = $vobj->name;
      if ($vobj->name == 'FAQ') {
        $def_vid = $vid;
      }
    }
    $form['experts']['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.") . '<br/><big>' . t('If you select different vocabularies, you must save the configuration BEFORE selecting users below.') . '</big>',
    );
  }

  // End multiple vocabs.
  // Changed query and loop because it failed if 'answer' was the first perm in list.
  // This should be faster any way.
  $role_list = array();
  $r_res = db_query("SELECT r.rid, r.name FROM {role} r JOIN {permission} p USING (rid) WHERE p.perm LIKE '%%answer question%%'");
  while ($row = db_fetch_array($r_res)) {
    $role_list[$row['rid']] = $row['name'];
  }
  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 <a href="@access">Permissions page</a> to update your permissions.', array(
      '@access' => url('admin/user/permissions'),
      '@admin' => $admin,
    )), 'error');
  }

  // Get all terms associated with FAQ.

  //$vocabs = variable_get('faq_ask_vocabularies', $def_vid);
  $vocabs_array = array();
  foreach ($vocabs as $vocab) {
    $vocabs_array[$vocab->vid] = $vocab->vid;
  }
  $result = db_query('SELECT td.tid, td.name, td.description FROM {term_data} td WHERE td.vid IN (' . db_placeholders($vocabs_array) . ') ORDER BY td.weight ASC, td.name ASC', $vocabs_array);

  //  $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 = check_plain($term['name']);
    if (substr($term['description'], 0, 9) == 'suggested') {
      $faq_terms[$term['tid']] = $term_name . '<br/>--<small>' . strip_tags($term['description']) . '</small>';
    }
    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 <a href="@access">Categories page</a> to update your vocabulary.', array(
      '@access' => url('admin/content/taxonomy'),
    )), 'error');
    return $form;
  }

  // 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['experts']['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,
      )) . '<br/><big>' . t('If you select different roles, you must save the configuration BEFORE selecting users below.') . '</big>',
    );
  }
  $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 .= '<p>' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</p>';
    }
    $top .= '<table id="faq_experts"><tr><th> </th><th>' . implode('</th><th>', $faq_terms) . '</th></tr>';
    if ($only_one_expert) {
      $top .= '<tr><td colspan="100">' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</td></tr>';
    }
    foreach ($faq_expert_names as $uid => $name) {
      ++$count;
      $class = $count & 1 ? 'odd' : 'even';
      $left = '<tr class="' . $class . '"><td><strong>' . $name . '</strong></td>';
      foreach ($faq_terms as $tid => $term_name) {
        $box_name = 'expert_' . $uid . '_' . $tid;
        $form['experts'][$box_name] = array(
          '#type' => 'checkbox',
          '#default_value' => $only_one_expert,
          '#prefix' => $top . $left . '<td align="center">',
          '#suffix' => '</td>',
        );
        $top = NULL;
        $left = NULL;
      }
      $form['experts'][$box_name]['#suffix'] .= '</tr>';
    }
    $form['experts'][$box_name]['#suffix'] .= '</table>';
  }
  else {

    // Experts go across the top; terms go down the left.
    $top = NULL;
    if ($only_one_expert) {
      $top .= '<p>' . t('Note: Even though the check boxes below are checked, you must still click the "Save configuration" button to save the expert settings.') . '</p>';
    }
    $top .= '<table id="faq_experts"><tr><th> </th><th>' . implode('</th><th>', $faq_expert_names) . '</th></tr>';
    foreach ($faq_terms as $tid => $term_name) {
      ++$count;
      $class = $count & 1 ? 'odd' : 'even';
      $left = '<tr class="' . $class . '"><td><strong>' . $term_name . '</strong></td>';
      foreach ($faq_expert_names as $uid => $name) {
        $box_name = 'expert_' . $uid . '_' . $tid;
        $form['experts'][$box_name] = array(
          '#type' => 'checkbox',
          '#default_value' => $only_one_expert,
          '#prefix' => $top . $left . '<td align="center">',
          '#suffix' => '</td>',
        );
        $top = NULL;
        $left = NULL;
      }
      $form['experts'][$box_name]['#suffix'] .= '</tr>';
    }
    $form['experts'][$box_name]['#suffix'] .= '</table>';
  }
  $form['experts'][$box_name]['#suffix'] .= t('Those who will be answering questions will need both "answer question" and "edit faq" permissions.');
  $result = db_query("SELECT * FROM {faq_expert}");
  while ($expert = db_fetch_array($result)) {
    $box_name = 'expert_' . $expert['uid'] . '_' . $expert['tid'];
    if (isset($form['experts'][$box_name])) {

      // Might not be present any more.
      $form['experts'][$box_name]['#default_value'] = TRUE;
    }
    else {

      // Expert 0 means default expert; overlook it.
      if ($expert['tid'] != 0) {
        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['experts']['faq_ask_default_expert'] = array(
      '#type' => 'value',
      '#value' => 1,
    );
  }
  else {
    $form['experts']['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),
    );
  }

  // Get rid of error element.
  unset($form['error']);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
    '#weight' => 5,
  );
  return $form;
}