You are here

answers_userpoints.module in Answers 7.4

The answers_userpoints module.

File

answers_userpoints/answers_userpoints.module
View source
<?php

/**
 * @file
 * The answers_userpoints module.
 */

/**
 * Implements hook_views_api().
 */
function answers_userpoints_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'answers_userpoints'),
  );
}

/**
 * Implements hook_form_alter().
 */
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;
  }
}

/**
 * Process best answer configuration settings.
 */
function answers_userpoints_answer_settings_form_submit($form, &$form_state) {
  $rules_voteup_question = $form_state['values']['rules_voteup_question'];
  $rules_votedown_question = $form_state['values']['rules_votedown_question'];
  $rules_votedown_question_cost = $form_state['values']['rules_votedown_question_cost'];
  $rules_voteup_answer = $form_state['values']['rules_voteup_answer'];
  $rules_votedown_answer = $form_state['values']['rules_votedown_answer'];
  $rules_votedown_answer_cost = $form_state['values']['rules_votedown_answer_cost'];
  $rules_best_answer = $form_state['values']['rules_best_answer'];
  $rules_best_answer_user = $form_state['values']['rules_best_answer_user'];
  $rules_own_best_answer = $form_state['values']['rules_own_best_answer'];
  $rules = array(
    'rules_voteup_question',
    'rules_voteup_question_cancel',
    'rules_votedown_question',
    'rules_votedown_question_cancel',
    'answers_userpoints_votedown_question_voter',
    'answers_userpoints_votedown_question_voter_cancel',
    'rules_voteup_answer',
    'rules_voteup_answer_cancel',
    'rules_votedown_answer',
    'answers_userpoints_votedown_answer_cancel',
    'answers_userpoints_votedown_answer_voter',
    'answers_userpoints_votedown_answer_voter_cancel',
    'rules_flag_answers_best_answer',
    'rules_unflag_answers_best_answer',
    'answers_userpoints_flag_answers_best_answer_flagger',
    'answers_userpoints_unflag_answers_best_answer_flagger',
    'answers_userpoints_flag_answers_own_best_answer',
    'answers_userpoints_unflag_answers_own_best_answer',
  );
  foreach ($rules as $key) {
    $update = FALSE;
    $rule = rules_config_load($key);
    foreach ($rule
      ->actions() as $action) {
      switch ($key) {
        case 'rules_voteup_question':
          if ($rule->active != ($rules_voteup_question != 0)) {
            $rule->active = $rules_voteup_question != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints' && $action->settings['user:select'] == 'node:author') {
            if ($action->settings['points'] != $rules_voteup_question) {
              $action->settings['points'] = $rules_voteup_question;
              $update = TRUE;
            }
          }
          break;
        case 'rules_voteup_question_cancel':
          if ($rule->active != ($rules_voteup_question != 0)) {
            $rule->active = $rules_voteup_question != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints' && $action->settings['user:select'] == 'node:author') {
            if ($action->settings['points'] != $rules_voteup_question * -1) {
              $action->settings['points'] = $rules_voteup_question * -1;
              $update = TRUE;
            }
          }
          break;
        case 'rules_votedown_question':
          if ($rule->active != ($rules_votedown_question != 0)) {
            $rule->active = $rules_votedown_question != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'removePoints') {
            if ($action->settings['user:select'] == 'node:author') {
              if ($action->settings['points'] != $rules_votedown_question * -1) {
                $action->settings['points'] = $rules_votedown_question * -1;
                $update = TRUE;
              }
            }
          }
          break;
        case 'rules_votedown_question_cancel':
          if ($rule->active != ($rules_votedown_question != 0)) {
            $rule->active = $rules_votedown_question != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'deletePoints') {
            if ($action->settings['user:select'] == 'node:author') {
              if ($action->settings['points'] != $rules_votedown_question) {
                $action->settings['points'] = $rules_votedown_question;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_votedown_question_voter':
          if ($rule->active != ($rules_votedown_question_cost != 0)) {
            $rule->active = $rules_votedown_question_cost != 0;
            $update = TRUE;
          }
          if ($action->settings['user:select'] == 'vote:user') {
            if ($action->settings['points'] != $rules_votedown_question_cost * -1) {
              $action->settings['points'] = $rules_votedown_question_cost * -1;
              $update = TRUE;
            }
          }
          break;
        case 'answers_userpoints_votedown_question_voter_cancel':
          if ($rule->active != ($rules_votedown_question_cost != 0)) {
            $rule->active = $rules_votedown_question_cost != 0;
            $update = TRUE;
          }
          if ($action->settings['user:select'] == 'vote:user') {
            if ($action->settings['points'] != $rules_votedown_question_cost) {
              $action->settings['points'] = $rules_votedown_question_cost;
              $update = TRUE;
            }
          }
          break;
        case 'rules_voteup_answer':
          if ($rule->active != ($rules_voteup_answer != 0)) {
            $rule->active = $rules_voteup_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints' && $action->settings['user:select'] == 'node:author') {
            if ($action->settings['points'] != $rules_voteup_answer) {
              $action->settings['points'] = $rules_voteup_answer;
              $update = TRUE;
            }
          }
          break;
        case 'rules_voteup_answer_cancel':
          if ($rule->active != ($rules_voteup_answer != 0)) {
            $rule->active = $rules_voteup_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints' && $action->settings['user:select'] == 'node:author') {
            if ($action->settings['points'] != $rules_voteup_answer * -1) {
              $action->settings['points'] = $rules_voteup_answer * -1;
              $update = TRUE;
            }
          }
          break;
        case 'rules_votedown_answer':
          if ($rule->active != ($rules_votedown_answer != 0)) {
            $rule->active = $rules_votedown_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'deletePoints') {
            if ($action->settings['user:select'] == 'node:author') {
              if ($action->settings['points'] != $rules_votedown_answer * -1) {
                $action->settings['points'] = $rules_votedown_answer * -1;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_votedown_answer_cancel':
          if ($rule->active != ($rules_votedown_answer != 0)) {
            $rule->active = $rules_votedown_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints') {
            if ($action->settings['user:select'] == 'node:author') {
              if ($action->settings['points'] != $rules_votedown_answer) {
                $action->settings['points'] = $rules_votedown_answer;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_votedown_answer_voter':
          if ($rule->active != ($rules_votedown_answer_cost != 0)) {
            $rule->active = $rules_votedown_answer_cost != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'deletePoints') {
            if ($action->settings['user:select'] == 'vote:user') {
              if ($action->settings['points'] != $rules_votedown_answer_cost * -1) {
                $action->settings['points'] = $rules_votedown_answer_cost * -1;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_votedown_answer_voter_cancel':
          if ($rule->active != ($rules_votedown_answer_cost != 0)) {
            $rule->active = $rules_votedown_answer_cost != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints') {
            if ($action->settings['user:select'] == 'vote:user') {
              if ($action->settings['points'] != $rules_votedown_answer_cost) {
                $action->settings['points'] = $rules_votedown_answer_cost;
                $update = TRUE;
              }
            }
          }
          break;
        case 'rules_flag_answers_best_answer':
          if ($rule->active != ($rules_best_answer != 0)) {
            $rule->active = $rules_best_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints') {
            if ($action->settings['user:select'] == 'flagged-node:author') {
              if ($action->settings['points'] != $rules_best_answer) {
                $action->settings['points'] = $rules_best_answer;
                $update = TRUE;
              }
            }
          }
          break;
        case 'rules_unflag_answers_best_answer':
          if ($rule->active != ($rules_best_answer != 0)) {
            $rule->active = $rules_best_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints') {
            if ($action->settings['user:select'] == 'flagged-node:author') {
              if ($action->settings['points'] != $rules_best_answer * -1) {
                $action->settings['points'] = $rules_best_answer * -1;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_flag_answers_best_answer_flagger':
          if ($rule->active != ($rules_best_answer_user != 0)) {
            $rule->active = $rules_best_answer_user != 0;
            $update = TRUE;
          }
          if ($action->settings['user:select'] == 'flagging-user') {
            if ($action->settings['points'] != $rules_best_answer_user) {
              $action->settings['points'] = $rules_best_answer_user;
              $update = TRUE;
            }
          }
          break;
        case 'answers_userpoints_unflag_answers_best_answer_flagger':
          if ($rule->active != ($rules_best_answer_user != 0)) {
            $rule->active = $rules_best_answer_user != 0;
            $update = TRUE;
          }
          if ($action->settings['user:select'] == 'flagging-user') {
            if ($action->settings['points'] != $rules_best_answer_user * -1) {
              $action->settings['points'] = $rules_best_answer_user * -1;
              $update = TRUE;
            }
          }
          break;
        case 'answers_userpoints_flag_answers_own_best_answer':
          if ($rule->active != ($rules_own_best_answer != 0)) {
            $rule->active = $rules_own_best_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'addPoints') {
            if ($action->settings['user:select'] == 'flagging-user') {
              if ($action->settings['points'] != $rules_own_best_answer) {
                $action->settings['points'] = $rules_own_best_answer;
                $update = TRUE;
              }
            }
          }
          break;
        case 'answers_userpoints_unflag_answers_own_best_answer':
          if ($rule->active != ($rules_own_best_answer != 0)) {
            $rule->active = $rules_own_best_answer != 0;
            $update = TRUE;
          }
          if ($action->settings['operation'] == 'removePoints') {
            if ($action->settings['user:select'] == 'flagging-user') {
              if ($action->settings['points'] != $rules_own_best_answer * -1) {
                $action->settings['points'] = $rules_own_best_answer * -1;
                $update = TRUE;
              }
            }
          }
          break;
      }
      if ($update) {
        $rule
          ->save();
        drupal_set_message(check_plain(format_string('Updated !rule', array(
          '!rule' => $key,
        ))));
      }
    }
  }
}

Functions

Namesort descending Description
answers_userpoints_answer_settings_form_submit Process best answer configuration settings.
answers_userpoints_form_alter Implements hook_form_alter().
answers_userpoints_views_api Implements hook_views_api().