/** * Accept a response as the chosen answer * * @param integer $answer_id ID of response to be chosen * @return boolean False if error, True on success */ public function accept($answer_id = 0) { if (!$answer_id) { $this->setError(Lang::txt('No answer ID provided.')); return false; } // Load the answer $answer = new Response($answer_id); if (!$answer->exists()) { $this->setError(Lang::txt('Answer not found.')); return false; } // Mark it at the chosen one $answer->set('state', 1); if (!$answer->store(true)) { $this->setError($answer->getError()); return false; } // Mark the question as answered $this->set('state', 1); // If banking is enabled if ($this->config('banking')) { // Accepted answer is same person as question submitter? if ($this->get('created_by') == $answer->get('created_by')) { $BT = new Transaction($this->_db); $reward = $BT->getAmount('answers', 'hold', $this->get('id')); // Remove hold $BT->deleteRecords('answers', 'hold', $this->get('id')); // Make credit adjustment $BTL_Q = new Teller($this->_db, User::get('id')); $BTL_Q->credit_adjustment($BTL_Q->credit_summary() - $reward); } else { // Calculate and distribute earned points $AE = new Helpers\Economy($this->_db); $AE->distribute_points($this->get('id'), $this->get('created_by'), $answer->get('created_by'), 'closure'); } // Set the reward value $this->set('reward', 0); } // Save changes return $this->store(true); }
/** * Mark an entry as "accepted" and unmark any previously accepted entry * * @return void */ public function acceptTask() { // Check for request forgeries Request::checkToken(['get', 'post']); // Incoming $qid = Request::getInt('qid', 0); $id = Request::getVar('id', array(0)); if (!is_array($id)) { $id = array($id); } $publish = $this->getTask() == 'accept' ? 1 : 0; // Check for an ID if (count($id) < 1) { $action = $publish == 1 ? 'accept' : 'reject'; App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_SELECT_ANSWER_TO', $action), 'error'); return; } else { if (count($id) > 1) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_ONLY_ONE_ACCEPTED_ANSWER'), 'error'); return; } } $ar = new Response($id[0]); if (!$ar->exists()) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); return; } if ($publish == 1) { // Unmark all other entries $tbl = new Tables\Response($this->database); if ($results = $tbl->find('list', array('question_id' => $ar->get('question_id')))) { foreach ($results as $result) { $result = new Response($result); if ($result->get('state') != 0 && $result->get('state') != 1) { continue; } $result->set('state', 0); $result->store(false); } } } // Mark this entry $ar->set('state', $publish); if (!$ar->store(false)) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $ar->getError(), 'error'); return; } // Set message if ($publish == '1') { $message = Lang::txt('COM_ANSWERS_ANSWER_ACCEPTED'); } else { if ($publish == '0') { $message = Lang::txt('COM_ANSWERS_ANSWER_REJECTED'); } } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $message); }
/** * Rate an item * * @return void */ public function rateitemTask() { $no_html = Request::getInt('no_html', 0); // Is the user logged in? if (User::isGuest()) { if (!$no_html) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE')); $this->loginTask(); } return; } // Incoming $id = Request::getInt('refid', 0); $cat = Request::getVar('category', ''); $vote = Request::getVar('vote', ''); $ip = Request::ip(); // Check for reference ID if (!$id) { // cannot proceed if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('No ID provided.'), 'error'); } return; } // load answer $row = new Response($id); $qid = $row->get('question_id'); // Can't vote for your own comment if ($row->get('created_by') == User::get('username')) { if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid), Lang::txt('Cannot vote for your own entries.'), 'warning'); } return; } // Can't vote for your own comment if (!$vote) { if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid), Lang::txt('No vote provided.'), 'warning'); } return; } // Get vote log $al = new Tables\Log($this->database); $al->loadByIp($id, $ip); if (!$al->id) { // new vote; // record if it was helpful or not switch ($vote) { case 'yes': case 'like': case 'up': case 1: $row->set('helpful', $row->get('helpful') + 1); break; case 'no': case 'dislike': case 'down': case -1: $row->set('nothelpful', $row->get('nothelpful') + 1); break; } } else { if ($al->helpful != $vote) { // changing vote; // Adjust values to reflect vote change switch ($vote) { case 'yes': case 'like': case 'up': case 1: $row->set('helpful', $row->get('helpful') + 1); $row->set('nothelpful', $row->get('nothelpful') - 1); break; case 'no': case 'dislike': case 'down': case -1: $row->set('helpful', $row->get('helpful') - 1); $row->set('nothelpful', $row->get('nothelpful') + 1); break; } } else { // no vote change; } } if (!$row->store(false)) { $this->setError($row->getError()); return; } // Record user's vote (old way) $al->response_id = $row->get('id'); $al->ip = $ip; $al->helpful = $vote; if (!$al->check()) { echo $al->getError(); $this->setError($al->getError()); return; } if (!$al->store()) { echo $al->getError(); $this->setError($al->getError()); return; } // Record user's vote (new way) if ($cat) { require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'vote.php'; $v = new Tables\Vote($this->database); $v->referenceid = $row->get('id'); $v->category = $cat; $v->voter = User::get('id'); $v->ip = $ip; $v->voted = Date::toSql(); $v->helpful = $vote; if (!$v->check()) { echo $v->getError(); $this->setError($v->getError()); return; } if (!$v->store()) { echo $v->getError(); $this->setError($v->getError()); return; } } // update display if ($no_html) { $row->set('vote', $vote); $this->view->option = $this->_option; $this->view->item = $row; foreach ($this->getErrors() as $error) { $this->view->setError($error); } $this->view->display(); } else { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid)); } }