You are here

function answers_best_answer_flag_flag in Answers 7.4

Implements hook_flag_flag().

Flag API 3

File

answers_best_answer/answers_best_answer.module, line 131
Support selection of 'Best Answers' for the 'Answers' module.

Code

function answers_best_answer_flag_flag($flag, $content_id, $account, $flagging) {
  module_load_include('inc', 'answers', 'includes/answers.lock');
  if ($flag->name == "best_answer") {
    $flagged_answer = entity_metadata_wrapper('node', $content_id);
    $question = answers_answer_question($content_id);
    $answers = answers_question_answers($question);

    // Unflag a prior best answer if one exists.
    $old_answer = NULL;
    foreach ($answers as $answer) {
      if ($answer->nid != $content_id && $flag
        ->is_flagged($answer->nid)) {
        $flag
          ->flag('unflag', $answer->nid);
        $old_answer = $answer;
        answers_question_lock_unset($old_answer, 'answers_best_answer');
      }
    }
    answers_question_lock_set($question, 'answers_best_answer');
    module_invoke_all('answers_best_answer_set', $question, $flagged_answer
      ->value(), $old_answer);
  }
}