/** * 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); } }
/** * Calculate royalties for a contributor and distribute * * @param object $con Resources Contributor * @param string $type Point calculation type * @return boolean False if errors, true on success */ public function distribute_points($con, $type = 'royalty') { if (!is_object($con)) { return false; } $cat = 'resource'; $points = round($con->ranking); // Get qualifying users $user = User::getInstance($con->authorid); // Reward review author if (is_object($user) && $user->get('id')) { $BTL = new Teller($this->_db, $user->get('id')); if (intval($points) > 0) { $msg = $type == 'royalty' ? Lang::txt('Royalty payment for your resource contributions') : ''; $BTL->deposit($points, $msg, $cat, 0); } } return true; }
/** * Distribute points * * @param integer $wishid Wish ID * @param string $type Transaction type * @param number $points Points to distribute * @return void */ public function distribute_points($wishid, $type = 'grant', $points = 0) { if (!$wishid) { return null; } require_once dirname(__DIR__) . DS . 'models' . DS . 'wishlist.php'; $objWish = new Tables\Wish($this->_db); $wish = $objWish->get_wish($wishid); $points = !$points ? $wish->bonus : $points; // Points for list owners if ($points > 0 && $type != 'royalty') { // Get the component parameters $wconfig = Component::params('com_wishlist'); $admingroup = $wconfig->get('group', 'hubadmin'); // get list owners $objOwner = new Tables\Owner($this->_db); $owners = $objOwner->get_owners($wish->wishlist, $admingroup, '', 0, $wishid); $owners = $owners['individuals']; $mainshare = $wish->assigned ? $points * 0.8 : 0; //80% $commonshare = $mainshare ? ($points - $mainshare) / count($owners) : $points / count($owners); // give the remaining 20% if ($owners && $commonshare) { foreach ($owners as $owner) { $o = User::getInstance($owner); if (!is_object($o) || !$o->get('id')) { continue; } $BTLO = new Teller($this->_db, $owner); if ($wish->assigned && $wish->assigned == $owner) { //$BTLO->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish').' #'.$wishid.' '.Lang::txt('on list').' #'.$wish->wishlist, 'wish', $wishid); $mainshare += $commonshare; } else { $BTLO->deposit($commonshare, Lang::txt('Bonus for fulfilling wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } else { $mainshare += $commonshare; } // give main share if ($wish->assigned && $mainshare) { $o = User::getInstance($wish->assigned); if (is_object($o) && $o->get('id')) { $BTLM = new Teller($this->_db, $wish->assigned); $BTLM->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } // Adjust credits $payees = $this->getPayees($wishid); if ($payees) { foreach ($payees as $p) { $o = User::getInstance($p->uid); if (!is_object($o) || !$o->get('id')) { continue; } $BTL = new Teller($this->_db, $p->uid); $hold = $this->getTotalPayment($wishid, $p->uid); if ($hold) { $credit = $BTL->credit_summary(); $adjusted = $credit - $hold; $BTL->credit_adjustment($adjusted); // withdraw bonus amount $BTL->withdraw($hold, Lang::txt('Bonus payment for granted wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } // Remove holds if exist if ($wish->bonus) { $BT = new Transaction($this->_db); $BT->deleteRecords('wish', 'hold', $wishid); } } // Points for wish author (needs to be granted by another person) if ($wish->ranking > 0 && $wish->proposed_by != User::get('id') && $wish->proposed_by) { $o = User::getInstance($wish->proposed_by); if (is_object($o) && $o->get('id')) { $BTLA = new Teller($this->_db, $wish->proposed_by); $BTLA->deposit($wish->ranking, Lang::txt('Your wish #%s on list #%s was granted', $wishid, $wish->wishlist), 'wish', $wishid); } } }
/** * Calculate royalties for a review and distribute * * @param object $review ResourcesReview * @param string $type Point calculation type * @return boolean False if errors, true on success */ public function distribute_points($review, $type = 'royalty') { if (!is_object($review)) { return false; } $cat = 'review'; $points = $this->calculate_marketvalue($review, $type); // Get qualifying users $user = User::getInstance($review->author); // Reward review author if (is_object($user)) { $BTL = new Teller($this->_db, $user->get('id')); if (intval($points) > 0) { $msg = $type == 'royalty' ? Lang::txt('Royalty payment for posting a review on publication #%s', $review->pid) : Lang::txt('Commission for posting a review on publication #%s', $review->pid); $BTL->deposit($points, $msg, $cat, $review->id); } } return true; }
/** * Mark an entry as opted out * * @return mixed */ public function award() { if (!$this->uid) { return NULL; } $opts = new Options(); $awardPer = $opts->getAwardPerField(); $fieldMap = array('name' => 'Fullname', 'orgtype' => 'Employment', 'organization' => 'Organization', 'countryorigin' => 'Citizenship', 'countryresident' => 'Residency', 'gender' => 'Sex', 'url' => 'URL', 'reason' => 'Reason', 'race' => 'Race', 'phone' => 'Phone', 'disability' => 'Disability'); $alreadyComplete = 0; $eligible = array(); $newAmount = 0; $completeSql = 'UPDATE `#__profile_completion_awards` SET edited_profile = 1'; $optedOut = NULL; foreach ($this->awards as $k => $complete) { if ($k === 'opted_out') { $optedOut = $complete; continue; } if ($complete) { continue; } if ($k === 'picture') { self::$dbh->setQuery('SELECT picture FROM `#__xprofiles` WHERE uidNumber = ' . $this->uid); if (self::$dbh->loadResult()) { $completeSql .= ', ' . $k . ' = 1'; $alreadyComplete += $awardPer; } else { $eligible['picture'] = 1; } continue; } $regField = $fieldMap[$k]; if ((bool) $this->profile->get($k)) { $completeSql .= ', ' . $k . ' = 1'; $alreadyComplete += $awardPer; } else { $eligible[$k == 'url' ? 'web' : $k] = 1; } } self::$dbh->setQuery('SELECT SUM(amount) AS amount FROM `#__users_transactions` WHERE type = \'deposit\' AND category = \'registration\' AND uid = ' . $this->uid); $prior = self::$dbh->loadResult(); self::$dbh->setQuery($completeSql . ' WHERE user_id = ' . $this->uid); self::$dbh->execute(); if ($alreadyComplete) { self::$dbh->setQuery('SELECT COALESCE((SELECT balance FROM `#__users_transactions` WHERE uid = ' . $this->uid . ' AND id = (SELECT MAX(id) FROM `#__users_transactions` WHERE uid = ' . $this->uid . ')), 0)'); $newAmount = self::$dbh->loadResult() + $alreadyComplete; $BTL = new Teller($this->uid); $BTL->deposit($alreadyComplete, 'Profile completion award', 'registration', 0); } return array('prior' => $prior, 'new' => $alreadyComplete, 'eligible' => $eligible, 'opted_out' => $optedOut); }