/** * Store changes to this offering * * @param boolean $check Perform data validation check? * @return boolean False if error, True on success */ public function store($check = true) { $res = parent::store($check); // If marked as chosen answer if ($res && $this->get('state') == 1) { require_once __DIR__ . DS . 'question.php'; $aq = new Question($this->get('question_id')); if ($aq->exists() && $aq->get('state') != 1) { $aq->set('state', 1); //$aq->set('reward', 0); // This was giving out points twice for one closed question /* if ($aq->config('banking')) { // Calculate and distribute earned points $AE = new Economy($this->_db); $AE->distribute_points($this->get('question_id'), $aq->get('created_by'), $this->get('created_by'), 'closure'); // Call the plugin if ( !Event::trigger('xmessage.onTakeAction', array( 'answers_reply_submitted', array($aq->creator('id')), 'com_answers', $this->get('question_id') )) ) { $this->setError(Lang::txt('Failed to remove alert.')); } } */ if (!$aq->store()) { $this->setError($aq->getError()); return false; } } } return $res; }
/** * Delete a question * * @apiMethod DELETE * @apiUri /answers/questions/{id} * @apiParameter { * "name": "id", * "description": "Question identifier", * "type": "integer", * "required": true, * "default": 0 * } * @return void */ public function deleteTask() { $this->requiresAuthentication(); $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; if (count($ids) <= 0) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_ID'), 500); } foreach ($ids as $id) { $row = new Question(intval($id)); if (!$row->exists()) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_RECORD'), 404); } if (!$row->delete()) { throw new Exception($row->getError(), 500); } } $this->send(null, 204); }
/** * Set the state of one or more questions * * @return void */ public function stateTask() { // Check for request forgeries Request::checkToken(['get', 'post']); // Incoming $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; $publish = $this->getTask() == 'close' ? 1 : 0; // Check for an ID if (count($ids) < 1) { $action = $publish == 1 ? Lang::txt('COM_ANSWERS_SET_STATE_CLOSE') : Lang::txt('COM_ANSWERS_SET_STATE_OPEN'); App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_SELECT_QUESTION_TO', $action), 'error'); return; } foreach ($ids as $id) { // Update record(s) $aq = new Question(intval($id)); if (!$aq->exists()) { continue; } $aq->set('state', $publish); if ($publish == 1) { $aq->adjustCredits(); } if (!$aq->store()) { throw new Exception($aq->getError(), 500); } } // Set message if ($publish == 1) { $message = Lang::txt('COM_ANSWERS_QUESTIONS_CLOSED', count($ids)); } else { if ($publish == 0) { $message = Lang::txt('COM_ANSWERS_QUESTIONS_OPENED', count($ids)); } } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $message); }
/** * Vote for an item * * @return void */ public function voteTask() { $no_html = Request::getInt('no_html', 0); $id = Request::getInt('id', 0); $vote = Request::getInt('vote', 0); // Login required if (User::isGuest()) { if (!$no_html) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE')); $this->loginTask(); } return; } // Load the question $row = new Question($id); // Record the vote if (!$row->vote($vote)) { if ($no_html) { $response = new \stdClass(); $response->success = false; $response->message = $row->getError(); echo json_encode($response); return; } else { App::redirect(Route::url($row->link()), $row->getError(), 'warning'); return; } } // Update display if ($no_html) { $this->qid = $id; $this->view->question = $row; $this->view->voted = $vote; foreach ($this->getErrors() as $error) { $this->view->setError($error); } $this->view->display(); } else { App::redirect(Route::url($row->link())); } }