You are here

function faq_ask_submit in FAQ_Ask 6.2

Same name and namespace in other branches
  1. 8 faq_ask.module \faq_ask_submit()
  2. 7 faq_ask.module \faq_ask_submit()
1 string reference to 'faq_ask_submit'
faq_ask_form_alter in ./faq_ask.module
Implementation of hook_form_alter(). This is how we build the "ask question" form.

File

./faq_ask.module, line 406
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_submit($form, &$form_state) {

  // Use only the first term entered in the correct vocabulary.
  // OK, so here we have a set of vocabualries with associated array of selected terms
  // Get the vocab first
  $terms = array();
  $vocabs = variable_get('faq_ask_vocabularies', 0);

  // Get the vocabularies used by faq experts
  // Issue #879442 comment 13 by mishutka90: Invalid argument due to only expert can categorize
  // If expert categorizes - no handling of terms in saving unanswered question
  if (FALSE == variable_get('faq_ask_categorize', FALSE)) {

    // Issue #879442 by Anita Sølver (soelver): Mails for experts
    // for every vocabulary in the form submitted
    foreach ($form_state['values']['taxonomy'] as $vid => $term_array) {
      if (in_array($vid, $vocabs)) {

        // If vocabulary is used by faq ask experts
        $terms = array_merge($terms, $term_array);
      }
      elseif ($vid == 'tags') {

        // If we're using Tags - there will be no vocabulary id on it
        foreach ($term_array as $termname) {
          $t = taxonomy_get_term_by_name($termname);
          foreach ($t as $term_obj) {
            $terms[$term_obj->tid] = $term_obj->tid;
          }
        }
      }
    }
  }

  // All valid terms are found
  // Are we notifying the expert(s)?
  if (variable_get('faq_ask_notify', FALSE)) {

    // Prepare the data structure for sending messages to experts
    $params = array(
      'category' => -1,
      // Default expert if no terms
      'question' => $form_state['values']['title'],
      'question_details' => $form_state['values']['detailed_question'],
      'nid' => $form_state['nid'],
      'creator' => theme('username', node_load($form_state['nid']), array(
        'plain' => TRUE,
      )),
    );
    if (empty($terms)) {

      // If no terms, send to default expert
      $expert = db_fetch_object(db_query("SELECT uid FROM {faq_expert} WHERE tid='0'"));
      faq_ask_notify_expert($expert->uid, $params);
    }
    else {

      // Find out who the experts are.
      $result = db_query("SELECT uid FROM {faq_expert} WHERE tid IN ('%s')", implode(",", $terms));
      while ($expert = db_fetch_object($result)) {

        // Find all terms belonging to this expert
        $expert_terms = db_query("SELECT tid FROM {faq_expert} WHERE uid='%d'", $expert->uid);
        $params['category'] = '';

        // Empty term name
        // Find the first term that is both assigned to this expert and is tagged to this question
        while ($params['category'] == '' && ($term = db_fetch_object($expert_terms))) {
          if (in_array($term->tid, $terms)) {
            $term_node = taxonomy_get_term($term->tid);
            $params['category'] = $term_node->name;
          }
        }

        // Send the message
        faq_ask_notify_expert($expert->uid, $params);
      }
    }
  }

  // Handle the notification of asker
  if (isset($form_state['values']['faq_email'])) {
    if (strlen($form_state['values']['faq_email']) > 4) {

      // length of e-mail must be more than a@b.c
      _faq_ask_set_faq_notification($form_state['nid'], $form_state['values']['faq_email']);
      drupal_set_message(t('Your question has been submitted. An e-mail will be sent to <i>@mail</i> when answered.', array(
        '@mail' => $form_state['values']['faq_email'],
      )), 'status');

      // If this user is not registered as a user before - check if all asking anonymous users should be added to the newsletter list
      if (module_exists('simplenews') && ($tid = variable_get('faq_ask_notify_asker_simplenews_tid', '0'))) {

        // If we have selected a newsletter to add
        if (function_exists('simplenews_subscribe_user')) {
          simplenews_subscribe_user($form_state['values']['faq_email'], $tid, variable_get('faq_ask_notify_asker_simplenews_confirm', 1), 'FAQ-Ask');
        }
      }
    }
  }
  elseif (isset($form_state['values']['faq_notify'])) {
    if ($form_state['values']['faq_notify']) {
      global $user;
      $email = $user->mail;
      _faq_ask_set_faq_notification($form_state['nid'], $email);
      drupal_set_message(t('Your question has been submitted. An e-mail will be sent to <i>@mail</i> when answered.', array(
        '@mail' => $email,
      )), 'status');
    }
    else {

      // Issue #1479382 by stenjo: Not able to turn off asker notification when editing own question
      _faq_ask_delete_faq_notification($form_state['nid']);
    }
  }
  else {
    drupal_set_message(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'status');
  }
}