/** * Remove all user blog entries for the given user ID * * Method is called after user data is deleted from the database * * @param array $user Holds the user data * @param boolean $success True if user was succesfully stored in the database * @param string $msg Message * @return boolean */ public function onMemberAfterDelete($user, $success, $msg) { if (!$success) { return false; } $userId = \Hubzero\Utility\Arr::getValue($user, 'id', 0, 'int'); if ($userId) { try { $entry = \Hubzero\Bank\Account::oneByUserId($user['id']); if (!$entry->destroy()) { throw new Exception($entry->getError()); } $transactions = \Hubzero\Bank\Transaction::all()->whereEquals('uid', $user['id']); foreach ($transactions->rows() as $row) { if (!$row->destroy()) { throw new Exception($row->getError()); } } } catch (Exception $e) { return false; } } return true; }
/** * 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); } }
/** * Retrieves a row from the database * * @param string $refid ID of the database table row * @param string $category Element type (determines table to look in) * @param string $parent If the element has a parent element * @return array */ public function transferItem($from_type, $from_id, $to_type, $rid = 0, $deactivate = 1) { $upconfig = Component::params('com_members'); $this->banking = $upconfig->get('bankAccounts'); $database = App::get('db'); if ($from_type == NULL or $from_id == NULL or $to_type == NULL) { $this->setError(Lang::txt('PLG_SUPPORT_TRANSFER_ERROR_MISSING_INFO')); return false; } if ($from_type == $to_type) { $this->setError(Lang::txt('PLG_SUPPORT_TRANSFER_ERROR_CATEGORIES_MUST_BE_DIFFERENT')); return false; } // collectors $author = ''; $subject = ''; $body = ''; $tags = ''; $owner = ''; // name of group owning the item $anonymous = 0; // get needed scripts include_once PATH_CORE . DS . 'components' . DS . 'com_support' . DS . 'models' . DS . 'ticket.php'; include_once PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'models' . DS . 'question.php'; include_once PATH_CORE . DS . 'components' . DS . 'com_wishlist' . DS . 'models' . DS . 'wishlist.php'; $wconfig = Component::params('com_wishlist'); $admingroup = $wconfig->get('group') ? $wconfig->get('group') : 'hubadmin'; // Get needed scripts & initial data switch ($from_type) { // Transfer from a Support Ticket case 'ticket': $row = new \Components\Support\Models\Ticket($from_id); if ($row->exists()) { $author = $row->get('login'); $subject = $row->content('raw', 200); // max 200 characters $body = $row->get('summary'); $owner = $row->get('group'); // If we are de-activating original item if ($deactivate) { $row->set('status', 2); $row->set('resolved', 'transfered'); } $tags = $row->tags('string'); } else { $this->setError(Lang::txt('PLG_SUPPORT_TRANSFER_ERROR_ITEM_NOT_FOUND')); return false; } break; // Transfer from a Question // Transfer from a Question case 'question': $row = new \Components\Answers\Models\Question($from_id); if ($row->exists()) { $author = $row->get('created_by'); $subject = $row->subject('raw', 200); // max 200 characters $body = $row->get('question'); $anonymous = $row->get('anonymous'); // If we are de-activating original item if ($deactivate) { $row->set('state', 2); $row->set('reward', 0); } $tags = $row->tags('string'); } else { $this->setError(Lang::txt('PLG_SUPPORT_TRANSFER_ERROR_ITEM_NOT_FOUND')); return false; } break; // Transfer from a Wish // Transfer from a Wish case 'wish': $row = new \Components\Wishlist\Tables\Wish($database); $row->load($from_id); if ($row->id) { $author = $row->proposed_by; $subject = \Hubzero\Utility\String::truncate($row->subject, 200); // max 200 characters $body = $row->about; $anonymous = $row->anonymous; // If we are de-activating original item if ($deactivate) { $row->status = 2; $row->ranking = 0; // also delete all previous votes for this wish $objR = new \Components\Wishlist\Tables\Rank($database); $objR->remove_vote($from_id); } // get owner $objG = new \Components\Wishlist\Tables\OwnerGroup($database); $nativegroups = $objG->get_owner_groups($row->wishlist, $admingroup, '', 1); $owner = count($nativegroups) > 0 && $nativegroups[0] != $admingroup ? $nativegroups[0] : ''; // tool group $objWishlist = new \Components\Wishlist\Tables\Wishlist($database); $wishlist = $objWishlist->get_wishlist($row->wishlist); if (isset($wishlist->resource) && isset($wishlist->resource->alias)) { $tags = $wishlist->resource->type == 7 ? 'tool:' : 'resource:'; $tags .= $wishlist->resource->alias ? $wishlist->resource->alias : $wishlist->referenceid; } } else { $this->setError(Lang::txt('PLG_SUPPORT_TRANSFER_ERROR_ITEM_NOT_FOUND')); return false; } break; } // if no author can be found, use current administrator $author = User::getInstance($author); if (!is_object($author)) { $author = User::getInstance(User::get('id')); } $today = Date::toSql(); // Where do we transfer? switch ($to_type) { // Transfer to a Support Ticket case 'ticket': $newrow = new \Components\Support\Models\Ticket(); $newrow->set('open', 1); $newrow->set('status', 0); $newrow->set('created', $today); $newrow->set('login', $author->get('username')); $newrow->set('severity', 'normal'); $newrow->set('summary', $subject); $newrow->set('report', $body ? $body : $subject); $newrow->set('section', 1); $newrow->set('type', 0); $newrow->set('instances', 1); $newrow->set('email', $author->get('email')); $newrow->set('name', $author->get('name')); // do we have an owner group? $newrow->set('group', $owner ? $owner : ''); break; case 'question': $newrow = new \Components\Answers\Models\Question(); $newrow->set('subject', $subject); $newrow->set('question', $body); $newrow->set('created', $today); $newrow->set('created_by', $author->get('id')); $newrow->set('state', 0); $newrow->set('anonymous', $anonymous); break; case 'wish': $newrow = new \Components\Wishlist\Models\Wish(); $newrow->set('subject', $subject); $newrow->set('about', $body); $newrow->set('proposed', $today); $newrow->set('proposed_by', $author->get('id')); $newrow->set('status', 0); $newrow->set('anonymous', $anonymous); // which wishlist? $objWishlist = new \Components\Wishlist\Tables\Wishlist($database); $mainlist = $objWishlist->get_wishlistID(1, 'general'); $listid = 0; if (!$rid && $owner) { $rid = $this->getResourceIdFromGroup($owner); } if ($rid) { $listid = $objWishlist->get_wishlistID($rid); } $newrow->set('wishlist', $listid ? $listid : $mainlist); break; } // Save new information if (!$newrow->store()) { $this->setError($newrow->getError()); return; } else { // Checkin ticket //$newrow->checkin(); // Extras if ($newrow->exists()) { switch ($to_type) { case 'ticket': // Tag new ticket if ($tags) { $newrow->tag($tags, User::get('id'), 0); } break; case 'question': // Tag new question if ($tags) { $newrow->tag($tags, User::get('id'), 0); } break; } } } // If we are de-activating original item if ($deactivate) { // overwrite old entry if (!$row->store()) { $this->setError($row->getError()); exit; } // Clean up rewards if banking if ($this->banking) { switch ($from_type) { case 'ticket': // no banking yet break; case 'question': $reward = \Hubzero\Bank\Transaction::getAmount('answers', 'hold', $from_id, $author->get('id')); // Remove hold if ($reward) { \Hubzero\Bank\Transaction::deleteRecords('answers', 'hold', $from_id); // Make credit adjustment $BTL_Q = new \Hubzero\Bank\Teller($author->get('id')); $credit = $BTL_Q->credit_summary(); $adjusted = $credit - $reward; $BTL_Q->credit_adjustment($adjusted); } break; case 'wish': include_once PATH_CORE . DS . 'components' . DS . 'com_wishlist' . DS . 'helpers' . DS . 'economy.php'; $WE = new \Components\Wishlist\Helpers\Economy($database); $WE->cleanupBonus($from_id); break; } } } return $newrow->get('id'); }
/** * Delete a question * * @return void */ public function deleteqTask() { // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); return $this->loginTask(); } if (!User::authorise('core.delete', $this->_option) && !User::authorise('core.manage', $this->_option)) { App::abort(403, Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN')); } // Incoming $id = Request::getInt('qid', 0); $ip = !User::isGuest() ? Request::ip() : ''; $reward = 0; if ($this->config->get('banking')) { $reward = Transaction::getAmount('answers', 'hold', $id); } $question = Question::oneOrFail($id); $question->set('state', Question::STATE_DELETED); $question->set('reward', 0); // Store new content if (!$question->save()) { App::abort(500, $question->getError()); } if ($reward && $this->config->get('banking')) { /* // Get all the answers for this question $responses = $question->responses()->rows(); if ($responses->count()) { $users = array(); foreach ($responses as $r) { $users[] = $r->get('created_by'); } // Build the "from" info $from = array( 'email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U')) ); // Build the message subject $subject = Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('COM_ANSWERS_QUESTION') . ' #' . $id . ' ' . Lang::txt('COM_ANSWERS_WAS_REMOVED'); $message = array(); // Plain text message $eview = new \Hubzero\Mail\View(array( 'name' => 'emails', 'layout' => 'removed_plaintext' )); $eview->option = $this->_option; $eview->sitename = Config::get('sitename'); $eview->question = $question; $eview->id = $question->get('id'); $eview->boundary = $from['multipart']; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML message $eview->setLayout('removed_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); // Send the message if (!Event::trigger('xmessage.onSendMessage', array('answers_question_deleted', $subject, $message, $from, $users, $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } */ // Remove hold $transaction->deleteRecords('answers', 'hold', $id); // Make credit adjustment $teller = new Teller(User::get('id')); $adjusted = $teller->credit_summary() - $reward; $teller->credit_adjustment($adjusted); } // Log activity $recipients = array($question->get('created_by')); $recipients = $this->recipients($recipients); Event::trigger('system.logActivity', ['activity' => ['action' => 'deleted', 'scope' => 'question', 'scope_id' => $question->get('id'), 'description' => Lang::txt('COM_ANSWERS_ACTIVITY_QUESTION_DELETED', '<a href="' . Route::url($question->link()) . '">' . $question->get('subject') . '</a>'), 'details' => array('title' => $question->get('title'), 'url' => $question->link())], 'recipients' => $recipients]); // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->_option)); }
/** * Record the transaction * * @param string $type Record type (inserting or updating) * @param integer $amount Amount to process * @param string $desc Transaction description * @param string $cat Transaction category * @param integer $ref ID of item transaction references * @return boolean True on success */ public function _saveTransaction($type, $amount, $desc, $cat, $ref) { $transaction = Transaction::blank()->set(array('uid' => $this->uid, 'type' => $type, 'amount' => $amount, 'description' => $desc, 'category' => $cat, 'referenceid' => $ref, 'balance' => $this->balance)); if (!$transaction->save()) { $this->setError($transaction->getError()); return false; } return true; }
/** * Save an entry * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); $account = Request::getVar('account', array(), 'post'); $row = \Hubzero\Bank\Account::blank()->set($account); $row->set('uid', intval($row->get('uid'))); $row->set('balance', intval($row->get('balance'))); $row->set('earnings', intval($row->get('earnings'))); $data = Request::getVar('transaction', array(), 'post'); if (isset($data['amount']) && intval($data['amount']) > 0) { $data['uid'] = $row->get('uid'); $data['created'] = Date::toSql(); $data['amount'] = intval($data['amount']); if (!isset($data['category']) || !$data['category']) { $data['category'] = 'general'; } if (!isset($data['description']) || !$data['description']) { $data['description'] = 'Reason unspecified'; } if (!isset($data['type']) || !$data['type']) { $data['type'] = ''; } switch ($data['type']) { case 'withdraw': $row->balance -= $data['amount']; break; case 'deposit': $row->balance += $data['amount']; $row->earnings += $data['amount']; break; case 'creation': $row->balance = $data['amount']; $row->earnings = $data['amount']; break; } $data['balance'] = $row->balance; $BT = Transaction::blank()->set($data); if (!$BT->save()) { App::abort(500, $row->getError()); return; } } if (!$row->save()) { App::abort(500, $row->getError()); return; } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=edit&uid=' . $row->uid, false), Lang::txt('User info saved')); }
/** * Distribute points * * @return void */ public function adjustCredits() { if ($this->get('reward')) { // Adjust credits // Remove hold $BT = new Transaction($this->_db); $reward = $BT->getAmount('answers', 'hold', $this->get('id')); $BT->deleteRecords('answers', 'hold', $this->get('id')); // Make credit adjustment if (is_object($this->creator())) { $BTL = new Teller($this->_db, $this->creator('id')); $credit = $BTL->credit_summary(); $adjusted = $credit - $reward; $BTL->credit_adjustment($adjusted); } $this->set('reward', 0); } }
/** * Record the transaction * * @param string $type Record type (inserting or updating) * @param integer $amount Amount to process * @param string $desc Transaction description * @param string $cat Transaction category * @param integer $ref ID of item transaction references * @return boolean True on success */ public function _saveTransaction($type, $amount, $desc, $cat, $ref) { $data = array(); $data['uid'] = $this->uid; $data['type'] = $type; $data['amount'] = $amount; $data['description'] = $desc; $data['category'] = $cat; $data['referenceid'] = $ref; $data['created'] = with(new Date('now'))->toSql(); $data['balance'] = $this->balance; $BT = new Transaction($this->_db); if (!$BT->bind($data)) { $this->setError($BT->getError()); return false; } if (!$BT->check()) { $this->setError($BT->getError()); return false; } if (!$BT->store()) { $this->setError($BT->getError()); return false; } 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); } } }
/** * Delete a question * * @return void */ public function deleteqTask() { // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); $this->loginTask(); return; } if (!User::authorise('core.delete', $this->_option) && !User::authorise('core.manage', $this->_option)) { throw new Exception(Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } // Incoming $id = Request::getInt('qid', 0); $ip = !User::isGuest() ? Request::ip() : ''; $reward = 0; if ($this->config->get('banking')) { $BT = new Transaction($this->database); $reward = $BT->getAmount('answers', 'hold', $id); } $email = 0; $question = new Question($id); // Check if user is authorized to delete if ($question->get('created_by') != User::get('id')) { App::redirect(Route::url($question->link() . '¬e=3')); return; } if ($question->get('state') == 1) { App::redirect(Route::url($question->link() . '¬e=2')); return; } $question->set('state', 2); // Deleted by user $question->set('reward', 0); // Store new content if (!$question->store(false)) { throw new Exception($question->getError(), 500); } if ($reward && $this->config->get('banking')) { // Get all the answers for this question if ($question->comments('list', array('filterby' => 'all'))) { $users = array(); foreach ($responses as $r) { $users[] = $r->creator('id'); } // Build the "from" info $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U'))); // Build the message subject $subject = Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('COM_ANSWERS_QUESTION') . ' #' . $id . ' ' . Lang::txt('COM_ANSWERS_WAS_REMOVED'); $message = array(); // Plain text message $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'removed_plaintext')); $eview->option = $this->_option; $eview->sitename = Config::get('sitename'); $eview->question = $question; $eview->id = $question->get('id'); $eview->boundary = $from['multipart']; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML message $eview->setLayout('removed_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); // Send the message if (!Event::trigger('xmessage.onSendMessage', array('answers_question_deleted', $subject, $message, $from, $users, $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Remove hold $BT->deleteRecords('answers', 'hold', $id); // Make credit adjustment $BTL_Q = new Teller($this->database, User::get('id')); $adjusted = $BTL_Q->credit_summary() - $reward; $BTL_Q->credit_adjustment($adjusted); } // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->_option)); }