Ejemplo n.º 1
0
 /**
  * Display all responses for a given question
  *
  * @return  void
  */
 public function displayTask()
 {
     // Filters
     $filters = array('search' => Request::getState($this->_option . '.' . $this->_controller . '.search', 'search', ''), 'state' => Request::getState($this->_option . '.' . $this->_controller . '.filterby', 'state', -1, 'int'), 'question_id' => Request::getState($this->_option . '.' . $this->_controller . '.qid', 'qid', 0, 'int'), 'sort' => Request::getState($this->_option . '.' . $this->_controller . '.sort', 'filter_order', 'created'), 'sort_Dir' => Request::getState($this->_option . '.' . $this->_controller . '.sortdir', 'filter_order_Dir', 'DESC'));
     $records = Response::all();
     $question = new Question();
     if ($filters['question_id'] >= 0) {
         $question = Question::oneOrFail($filters['question_id']);
         $records->whereEquals('question_id', $filters['question_id']);
     }
     if ($filters['state'] >= 0) {
         $records->whereEquals('state', $filters['state']);
     }
     if ($filters['search']) {
         $filters['search'] = strtolower((string) $filters['search']);
         $records->whereLike('answer', $filters['search'], 1)->resetDepth();
     }
     $rows = $records->ordered('filter_order', 'filter_order_Dir')->paginated();
     // Output the HTML
     $this->view->set('rows', $rows)->set('filters', $filters)->set('question', $question)->display();
 }
Ejemplo n.º 2
0
 /**
  * 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);
     $reward = Transaction::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
     $results = Response::all()->whereEquals('question_id', $qid)->where('state', '!=', 2)->rows();
     $n = $results->count();
     $eligible = array();
     if ($n > 1) {
         // More than one answer found
         foreach ($results as $result) {
             // Check if a regular answer has a good rating (at least 50% of positive votes)
             if ($result->get('helpful') + $result->get('nothelpful') >= 3 && $result->get('helpful') >= $result->get('nothelpful') && $result->get('state') == 0) {
                 $eligible[] = $result->get('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($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 = User::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($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($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 = Transaction::deleteRecords('answers', 'hold', $qid);
     }
 }