/** * 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); }
/** * Mark an entry as "accepted" and unmark any previously accepted entry * * @return void */ public function acceptTask() { // Check for request forgeries Request::checkToken(['get', 'post']); // Incoming $qid = Request::getInt('qid', 0); $id = Request::getVar('id', array(0)); if (!is_array($id)) { $id = array($id); } $publish = $this->getTask() == 'accept' ? 1 : 0; // Check for an ID if (count($id) < 1) { $action = $publish == 1 ? 'accept' : 'reject'; App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_SELECT_ANSWER_TO', $action), 'error'); return; } else { if (count($id) > 1) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_ONLY_ONE_ACCEPTED_ANSWER'), 'error'); return; } } $ar = new Response($id[0]); if (!$ar->exists()) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); return; } if ($publish == 1) { // Unmark all other entries $tbl = new Tables\Response($this->database); if ($results = $tbl->find('list', array('question_id' => $ar->get('question_id')))) { foreach ($results as $result) { $result = new Response($result); if ($result->get('state') != 0 && $result->get('state') != 1) { continue; } $result->set('state', 0); $result->store(false); } } } // Mark this entry $ar->set('state', $publish); if (!$ar->store(false)) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $ar->getError(), 'error'); return; } // Set message if ($publish == '1') { $message = Lang::txt('COM_ANSWERS_ANSWER_ACCEPTED'); } else { if ($publish == '0') { $message = Lang::txt('COM_ANSWERS_ANSWER_REJECTED'); } } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $message); }
/** * 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'); }