/** * Get a list of chosen responses * * @param string $rtrn Data type to return [count, list] * @param array $filters Filters to apply to query * @return mixed Returns an integer or array depending upon format chosen */ public function chosen($rtrn = 'list', $filters = array()) { $tbl = new Tables\Response($this->_db); if (!isset($filters['question_id'])) { $filters['question_id'] = $this->get('id'); } $filters['state'] = 1; $filters['filterby'] = 'accepted'; $filters['sort'] = 'created'; $filters['sort_Dir'] = 'DESC'; switch (strtolower($rtrn)) { case 'count': if ($this->get('chosen_count', null) === null) { $this->set('chosen_count', $tbl->find('count', $filters)); } return $this->get('chosen_count'); break; case 'list': case 'results': default: if ($this->get('chosen', null) === null || !$this->get('chosen') instanceof ItemList) { if ($results = $tbl->find('list', $filters)) { foreach ($results as $key => $result) { $results[$key] = new Response($result); } } else { $results = array(); } $this->set('chosen', new ItemList($results)); } return $this->get('chosen'); break; } }
/** * 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); }
/** * Distribute points * * @param integer $qid Question ID * @param integer $Q_owner Question owner * @param integer $BA_owner Account owner * @param string $type Transaction type * @return void */ public function distribute_points($qid, $Q_owner, $BA_owner, $type) { if ($qid === NULL) { $qid = $this->qid; } $cat = 'answers'; require_once dirname(__DIR__) . DS . 'models' . DS . 'question.php'; $points = $this->calculate_marketvalue($qid, $type); $BT = new Transaction($this->_db); $reward = $BT->getAmount($cat, 'hold', $qid); $reward = $reward ? $reward : '0'; $share = $points / 3; $BA_owner_share = $share + $reward; $A_owner_share = 0; // Calculate commissions for other answers $ar = new Tables\Response($this->_db); $result = $ar->getActions($qid); $n = count($result); $eligible = array(); if ($n > 1) { // More than one answer found for ($i = 0; $i < $n; $i++) { // Check if a regular answer has a good rating (at least 50% of positive votes) if ($result[$i]->helpful + $result[$i]->nothelpful >= 3 && $result[$i]->helpful >= $result[$i]->nothelpful && $result[$i]->state == '0') { $eligible[] = $result[$i]->created_by; } } if (count($eligible) > 0) { // We have eligible answers $A_owner_share = $share / $n; } else { // Best A owner gets remaining thrid $BA_owner_share += $share; } } else { // Best A owner gets remaining 3rd $BA_owner_share += $share; } // Reward asker $q_user = User::getInstance($Q_owner); if (is_object($q_user) && $q_user->get('id')) { $BTL_Q = new Teller($this->_db, $q_user->get('id')); //$BTL_Q->deposit($Q_owner_share, 'Commission for posting a question', $cat, $qid); // Separate comission and reward payment // Remove credit $credit = $BTL_Q->credit_summary(); $adjusted = $credit - $reward; $BTL_Q->credit_adjustment($adjusted); if (intval($share) > 0) { $share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for posting question #%s', $qid) : Lang::txt('Commission for posting question #%s', $qid); $BTL_Q->deposit($share, $share_msg, $cat, $qid); } // withdraw reward amount if ($reward) { $BTL_Q->withdraw($reward, Lang::txt('Reward payment for your question #%s', $qid), $cat, $qid); } } // Reward others $ba_user = User::getInstance($BA_owner); if (is_object($ba_user) && $ba_user->get('id')) { // Reward other responders if (count($eligible) > 0) { foreach ($eligible as $e) { $auser = Profile::getInstance($e); if (is_object($auser) && $auser->get('id') && is_object($ba_user) && $ba_user->get('id') && $ba_user->get('id') != $auser->get('id')) { $BTL_A = new Teller($this->_db, $auser->get('id')); if (intval($A_owner_share) > 0) { $A_owner_share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for answering question #%s', $qid) : Lang::txt('Answered question #%s that was recently closed', $qid); $BTL_A->deposit($A_owner_share, $A_owner_share_msg, $cat, $qid); } } // is best answer eligible for extra points? if (is_object($auser) && $auser->get('id') && is_object($ba_user) && $ba_user->get('id') && $ba_user->get('id') == $auser->get('id')) { $ba_extra = 1; } } } // Reward best answer $BTL_BA = new Teller($this->_db, $ba_user->get('id')); if (isset($ba_extra)) { $BA_owner_share += $A_owner_share; } if (intval($BA_owner_share) > 0) { $BA_owner_share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for answering question #%s', $qid) : Lang::txt('Answer for question #%s was accepted', $qid); $BTL_BA->deposit($BA_owner_share, $BA_owner_share_msg, $cat, $qid); } } // Remove hold if exists if ($reward) { $BT = new Transaction($this->_db); $BT->deleteRecords('answers', 'hold', $qid); } }