You are here

function answers_userpoints_form_alter in Answers 7.4

Implements hook_form_alter().

File

answers_userpoints/answers_userpoints.module, line 21
The answers_userpoints module.

Code

function answers_userpoints_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {

    // Add settings for answers_best_answer to the answers settings form.
    case 'answers_settings':
      $rules_config = array();
      $rules = array(
        'rules_voteup_question',
        'rules_votedown_question',
        'answers_userpoints_votedown_question_voter',
        'rules_voteup_answer',
        'rules_votedown_answer',
        'answers_userpoints_votedown_answer_voter',
        'rules_flag_answers_best_answer',
        'answers_userpoints_flag_answers_best_answer_flagger',
        'answers_userpoints_flag_answers_own_best_answer',
      );
      foreach ($rules as $key) {
        $rule = rules_config_load($key);
        if ($key == 'answers_userpoints_flag_answers_own_best_answer') {
          $rules_config[$key . '_enabled'] = $rule->active;
        }
        foreach ($rule
          ->actions() as $action) {
          if ($action->settings['operation'] == 'addPoints') {
            if ($key == 'answers_userpoints_flag_answers_own_best_answer') {
              $rules_config[$key] = $action->settings['points'];
            }
            elseif (in_array($action->settings['user:select'], array(
              'node:author',
              'flagged-node:author',
            ))) {
              $rules_config[$key] = $action->settings['points'];
            }
            elseif ($action->settings['user:select'] == 'flagging-user') {
              $rules_config[$key . '_user'] = $action->settings['points'];
            }
          }
          elseif ($action->settings['operation'] == 'deletePoints' && $action->settings['user:select'] == 'vote:user') {
            $rules_config[$key . '_user'] = $action->settings['points'];
          }
          elseif ($action->settings['operation'] == 'deletePoints' && $action->settings['user:select'] == 'node:author') {
            $rules_config[$key] = $action->settings['points'];
          }
          elseif ($action->settings['operation'] == 'removePoints') {
            if ($action->settings['user:select'] == 'node:author') {
              $rules_config[$key] = $action->settings['points'];
            }
            elseif ($action->settings['user:select'] == 'vote:user') {
              $rules_config[$key] = $action->settings['points'];
            }
          }
        }
      }
      $form['additional_settings']['#attached']['js'][] = drupal_get_path('module', 'answers_userpoints') . '/js/answers_userpoints_admin.js';
      $form['answers_userpoints_answer_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('!Points settings', userpoints_translation()),
        '#weight' => 10,
        '#collapsible' => TRUE,
        '#group' => 'additional_settings',
      );
      $form['answers_userpoints_answer_settings']['question'] = array(
        '#type' => 'fieldset',
        '#title' => t('!Questions', answers_translation()),
        '#weight' => -1,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['answers_userpoints_answer_settings']['question']['rules_voteup_question'] = array(
        '#type' => 'textfield',
        '#title' => t('Up vote reward'),
        '#description' => t('The amount of !points to award on question author on Up Vote question', userpoints_translation()),
        '#default_value' => $rules_config['rules_voteup_question'],
      );
      $form['answers_userpoints_answer_settings']['question']['rules_votedown_question'] = array(
        '#type' => 'textfield',
        '#title' => t('Down vote penalty'),
        '#description' => t('The amount of !points to remove from the question author on Down Vote', userpoints_translation()),
        '#default_value' => abs($rules_config['rules_votedown_question']),
      );
      $form['answers_userpoints_answer_settings']['question']['rules_votedown_question_cost'] = array(
        '#type' => 'textfield',
        '#title' => t('Down vote cost'),
        '#description' => t('The amount of !points to remove from the voter on Down Vote question', userpoints_translation()),
        '#default_value' => abs($rules_config['answers_userpoints_votedown_question_voter']),
      );
      $form['answers_userpoints_answer_settings']['answer'] = array(
        '#type' => 'fieldset',
        '#title' => t('Answers'),
        '#weight' => 0,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['answers_userpoints_answer_settings']['answer']['rules_voteup_answer'] = array(
        '#type' => 'textfield',
        '#title' => t('Up vote reward'),
        '#description' => t('The amount of !points to award on the answer author on Up Vote answer', userpoints_translation()),
        '#default_value' => $rules_config['rules_voteup_answer'],
      );
      $form['answers_userpoints_answer_settings']['answer']['rules_votedown_answer'] = array(
        '#type' => 'textfield',
        '#title' => t('Down vote penalty'),
        '#description' => t('The amount of !points to remove from the answer author on Down Vote', userpoints_translation()),
        '#default_value' => abs($rules_config['rules_votedown_answer']),
      );
      $form['answers_userpoints_answer_settings']['answer']['rules_votedown_answer_cost'] = array(
        '#type' => 'textfield',
        '#title' => t('Down vote cost'),
        '#description' => t('The amount of !points to remove from the voter on Down Vote answer', userpoints_translation()),
        '#default_value' => abs($rules_config['answers_userpoints_votedown_answer_voter_user']),
      );
      $form['answers_userpoints_answer_settings']['best_answer'] = array(
        '#type' => 'fieldset',
        '#title' => t('Best Answer'),
        '#weight' => 1,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['answers_userpoints_answer_settings']['best_answer']['rules_best_answer'] = array(
        '#type' => 'textfield',
        '#title' => t('Author reward'),
        '#description' => t('The amount of !points to award to the author of the best answer', userpoints_translation()),
        '#default_value' => $rules_config['rules_flag_answers_best_answer'],
      );
      $form['answers_userpoints_answer_settings']['best_answer']['rules_best_answer_user'] = array(
        '#type' => 'textfield',
        '#title' => t('Flagger reward', userpoints_translation()),
        '#description' => t('The amount of !points to award to the !question author for flagging the best answer', userpoints_translation(), answers_translation()),
        '#default_value' => $rules_config['answers_userpoints_flag_answers_best_answer_flagger_user'],
      );
      $form['answers_userpoints_answer_settings']['best_answer']['rules_own_best_answer'] = array(
        '#type' => 'textfield',
        '#title' => t('Own best answer reward'),
        '#description' => t('The amount of !points to award when the !question author is the best answer author', userpoints_translation(), answers_translation()),
        '#default_value' => $rules_config['answers_userpoints_flag_answers_own_best_answer'],
      );

      // Add in a submit handler *before* the standard handler.
      $form['#submit'][] = 'answers_userpoints_answer_settings_form_submit';
      break;
  }
}