Example #1
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);
     $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);
     }
 }