You are here

function faq_ask_nodeapi in FAQ_Ask 6.2

Implementation of hook_nodeapi().

Checks if the node being updated is a question that has been answered

File

./faq_ask.module, line 328
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_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'faq') {
    switch ($op) {
      case 'update':

        // If node is not to be published by default, then issue a message with instructions to publish when ready
        $options = variable_get('node_options_faq', array(
          'status',
          'promote',
        ));
        if (!in_array('status', $options) && $node->status == '0') {
          $token = _faq_ask_get_token('faq_ask/answer/' . $node->nid);
          $options = array(
            'query' => "token={$token}&forced=TRUE",
          );
          $url = url("faq_ask/answer/" . $node->nid, $options);
          drupal_set_message(t('This question is by default NOT published. To publish the answered question when you are ready,
                        <a href="@url">click this link</a>', array(
            '@url' => $url,
          )), 'warning');
        }

        // return if the asker notification should be done by cron
        if (variable_get('faq_ask_notify_by_cron', TRUE)) {
          return;
        }

        // Check if the node is published and asker notified
        $email = _faq_ask_get_faq_notification_email($node->nid);
        if ($node->status == '1' && $email != '') {

          // Get the asker account
          $account = user_load(array(
            'mail' => $email,
          ));

          // If the account is not anonymous
          if ($account) {
            $params['account'] = $account;
          }
          else {
            $params['account'] = NULL;
          }
          $params['question'] = $node->title;
          $params['nid'] = $node->nid;

          // Send the e-mail to the asker. Drupal calls hook_mail() via this
          $mail_sent = drupal_mail('faq_ask', 'notify_asker', $email, user_preferred_language($params['account']), $params);

          // Handle sending result
          if ($mail_sent) {
            watchdog('FAQ_Ask', 'Asker notification email sent to @to for question @quest', array(
              '@to' => $email,
              '@quest' => check_plain($node->title),
            ), WATCHDOG_NOTICE);

            // If email sent, remove the notification from the queue
            _faq_ask_delete_faq_notification($node->nid);
          }
          else {
            watchdog('FAQ_Ask', 'Asker notification email to @to failed for the "@quest" question.', array(
              '@to' => $email,
              '@quest' => check_plain($node->title),
            ), WATCHDOG_ERROR);
            drupal_set_message(t('Asker notification email to @to failed for the "@quest" question.', array(
              '@to' => $email,
              '@quest' => check_plain($node->title),
            )));
          }
        }
        break;
      case 'delete':

        // Remove notification if the question is deleted
        _faq_ask_delete_faq_notification($node->nid);
        break;
      case 'prepare':

        // Node is being shown on the add/edit form
        // Check if the node nid exists. It does not when creating a new question
        if (isset($node->nid) && ($mail = _faq_ask_get_faq_notification_email($node->nid))) {
          drupal_set_message(t('Notification will be sent to <strong>@mail</strong> when answered.', array(
            '@mail' => $mail,
          )));
        }
        break;
      default:
        break;
    }
  }
}