/** * Generate and return various links to the entry * Link will vary depending upon action desired, such as edit, delete, etc. * * @param string $type The type of link to return * @return string */ public function link($type = '') { if (!isset($this->base)) { if (!$this->get('question_id')) { $answer = Response::oneOrNew($this->get('item_id')); $this->set('question_id', $answer->get('question_id')); } $this->base = 'index.php?option=com_answers&task=question&id=' . $this->get('question_id'); } $link = $this->base; // If it doesn't exist or isn't published switch (strtolower($type)) { case 'edit': $link .= '&action=edit&comment=' . $this->get('id'); break; case 'delete': $link .= '&action=delete&comment=' . $this->get('id'); break; case 'reply': $link .= '&reply=' . $this->get('id') . '#c' . $this->get('id'); break; case 'report': $link = 'index.php?option=com_support&task=reportabuse&category=itemcomment&id=' . $this->get('id') . '&parent=' . $this->get('question_id'); break; case 'permalink': default: $link .= '#c' . $this->get('id'); break; } return $link; }
/** * 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); } }
/** * Reset the vote count for an entry * * @return void */ public function resetTask() { // Check for request forgeries Request::checkToken(); // Incoming $answer = Request::getVar('answer', array()); // Reset some values $model = new Response(intval($answer['id'])); if (!$model->reset()) { throw new Exception($ar->getError(), 500); } // Redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_VOTE_LOG_RESET')); }
/** * Mark an answer as accepted * * @return void */ public function acceptTask() { // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); return $this->loginTask(); } // Incoming $id = Request::getInt('id', 0); $rid = Request::getInt('rid', 0); $question = Question::oneOrFail($id); // verify the orignial poster is the only one accepting the answer if ($question->get('created_by') != User::get('id')) { App::redirect(Route::url($question->link()), Lang::txt('COM_ANSWERS_ERROR_MUST_BE_ASKER'), 'error'); } // Check changes if (!$question->accept($rid)) { $this->setError($question->getError()); } // Log the activity if (!$this->getError()) { $answer = Response::oneOrFail($rid); $recipients = array($answer->get('created_by')); if ($answer->get('created_by') != $question->get('created_by')) { $recipients[] = $question->get('created_by'); } Event::trigger('system.logActivity', ['activity' => ['action' => 'accepted', 'scope' => 'question.answer', 'scope_id' => $rid, 'description' => Lang::txt('COM_ANSWERS_ACTIVITY_ANSWER_ACCEPTED', $rid, '<a href="' . Route::url($question->link() . '#a' . $rid) . '">' . $question->get('subject') . '</a>'), 'details' => array('title' => $question->get('title'), 'question_id' => $question->get('id'), 'url' => $question->link())], 'recipients' => $recipients]); } // Redirect to the question App::redirect(Route::url($question->link()), $this->getError() ? $this->getError() : Lang::txt('COM_ANSWERS_NOTICE_QUESTION_CLOSED'), $this->getError() ? 'error' : 'success'); }
/** * Release a reported item * * @param string $refid ID of the database table row * @param string $parent If the element has a parent element * @param string $category Element type (determines table to look in) * @return array */ public function releaseReportedItem($refid, $parent, $category) { if (!$this->_canHandle($category)) { return null; } require_once PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'models' . DS . 'question.php'; $database = App::get('db'); $state = 1; switch ($category) { case 'answer': $comment = \Components\Answers\Models\Response::oneOrFail($refid); $state = 0; break; case 'question': $comment = \Components\Answers\Models\Question::oneOrFail($refid); break; case 'answercomment': $comment = \Components\Answers\Models\Comment::oneOrFail($refid); break; } $comment->set('state', $state); $comment->save(); return ''; }
/** * Accept a response as the chosen answer * * @param integer $answer_id ID of response to be chosen * @return boolean False if error, True on success */ public function accept($answer_id = 0) { if (!$answer_id) { $this->setError(Lang::txt('No answer ID provided.')); return false; } // Load the answer $answer = new Response($answer_id); if (!$answer->exists()) { $this->setError(Lang::txt('Answer not found.')); return false; } // Mark it at the chosen one $answer->set('state', 1); if (!$answer->store(true)) { $this->setError($answer->getError()); return false; } // Mark the question as answered $this->set('state', 1); // If banking is enabled if ($this->config('banking')) { // Accepted answer is same person as question submitter? if ($this->get('created_by') == $answer->get('created_by')) { $BT = new Transaction($this->_db); $reward = $BT->getAmount('answers', 'hold', $this->get('id')); // Remove hold $BT->deleteRecords('answers', 'hold', $this->get('id')); // Make credit adjustment $BTL_Q = new Teller($this->_db, User::get('id')); $BTL_Q->credit_adjustment($BTL_Q->credit_summary() - $reward); } else { // Calculate and distribute earned points $AE = new Helpers\Economy($this->_db); $AE->distribute_points($this->get('id'), $this->get('created_by'), $answer->get('created_by'), 'closure'); } // Set the reward value $this->set('reward', 0); } // Save changes return $this->store(true); }
/** * Reset the vote count for an entry * * @return void */ public function resetTask() { // Check for request forgeries Request::checkToken(); if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.edit.state', $this->_option)) { App::abort(403, Lang::txt('JERROR_ALERTNOAUTHOR')); } // Incoming $answer = Request::getVar('answer', array()); // Reset some values $model = Response::oneOrFail(intval($answer['id'])); if (!$model->reset()) { Notify::error($ar->getError()); } else { Notify::success(Lang::txt('COM_ANSWERS_VOTE_LOG_RESET')); } // Redirect $this->cancelTask(); }
/** * Save an answer (reply to question) * * @return void */ public function saveaTask() { // Check for request forgeries Request::checkToken(); // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); $this->loginTask(); return; } // Incoming $response = Request::getVar('response', array(), 'post', 'none', 2); // clean input array_walk($response, function (&$field, $key) { $field = \Hubzero\Utility\Sanitize::clean($field); }); // Initiate class and bind posted items to database fields $row = new Response($response['id']); if (!$row->bind($response)) { throw new Exception($row->getError(), 500); } // Store new content if (!$row->store(true)) { throw new Exception($row->getError(), 500); } // Load the question $question = new Question($row->get('question_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') . ' #' . $question->get('id') . ' ' . Lang::txt('COM_ANSWERS_RESPONSE'); $message = array(); // Plain text message $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'response_plaintext')); $eview->option = $this->_option; $eview->sitename = Config::get('sitename'); $eview->question = $question; $eview->row = $row; $eview->id = $response['question_id']; $eview->boundary = $from['multipart']; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML message $eview->setLayout('response_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); // --- $authorid = $question->creator('id'); $apu = $this->config->get('notify_users', ''); $apu = explode(',', $apu); $apu = array_map('trim', $apu); $receivers = array(); if (!empty($apu)) { foreach ($apu as $u) { $user = User::getInstance($u); if ($user) { $receivers[] = $user->get('id'); } } $receivers = array_unique($receivers); } // Send the message if (!in_array($authorid, $receivers) && $question->get('email')) { // Flag to mask identity of anonymous question asker // MCRN Ticket #134 if ($question->get('anonymous') == '1') { $messageType = 'answers_reply_submitted_anonymous'; } else { $messageType = 'answers_reply_submitted'; } if (!Event::trigger('xmessage.onSendMessage', array($messageType, $subject, $message, $from, array($authorid), $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Send the answers admins message if (!empty($receivers)) { if (!Event::trigger('xmessage.onSendMessage', array('new_answer_admin', $subject, $message, $from, $receivers, $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Redirect to the question App::redirect(Route::url($question->link()), Lang::txt('COM_ANSWERS_NOTICE_POSTED_THANKS'), 'success'); }