/** * 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; }
/** * Create a new question * * @apiMethod POST * @apiUri /answers/questions * @apiParameter { * "name": "email", * "description": "Notify user of responses", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "anonymous", * "description": "List author as anonymous or not", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "subject", * "description": "Short, one-line question", * "type": "string", * "required": true, * "default": null * } * @apiParameter { * "name": "question", * "description": "Longer, detailed question", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "created", * "description": "Created timestamp (YYYY-MM-DD HH:mm:ss)", * "type": "string", * "required": false, * "default": "now" * } * @apiParameter { * "name": "crated_by", * "description": "User ID of entry creator", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "state", * "description": "Published state (0 = unpublished, 1 = published)", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "reward", * "description": "Reward points", * "type": "integer", * "required": false, * "default": 0 * } * @apiParameter { * "name": "tags", * "description": "Comma-separated list of tags", * "type": "string", * "required": true, * "default": null * } * @return void */ public function createTask() { $this->requiresAuthentication(); $fields = array('email' => Request::getInt('email', 0, 'post'), 'anonymous' => Request::getInt('anonymous', 0, 'post'), 'subject' => Request::getVar('subject', null, 'post', 'none', 2), 'question' => Request::getVar('question', null, 'post', 'none', 2), 'created' => Request::getVar('created', new Date('now'), 'post'), 'created_by' => Request::getInt('created_by', 0, 'post'), 'state' => Request::getInt('state', 0, 'post'), 'reward' => Request::getInt('reward', 0, 'post'), 'tags' => Request::getVar('tags', null, 'post')); $row = new Question(); if (!$row->set($fields)) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_BINDING_DATA'), 500); } $row->set('email', isset($fields['email']) ? 1 : 0); $row->set('anonymous', isset($fields['anonymous']) ? 1 : 0); if (!$row->save()) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_DATA'), 500); } if (isset($fields['tags'])) { if (!$row->tag($fields['tags'], User::get('id'))) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_TAGS'), 500); } } $this->send($row->toObject()); }
/** * Update a question * * @apiMethod PUT * @apiUri /answers/questions/{id} * @apiParameter { * "name": "id", * "description": "Question identifier", * "type": "integer", * "required": true, * "default": null * } * @apiParameter { * "name": "email", * "description": "Notify user of responses", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "anonymous", * "description": "List author as anonymous or not", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "subject", * "description": "Short, one-line question", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "question", * "description": "Longer, detailed question", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "created", * "description": "Created timestamp (YYYY-MM-DD HH:mm:ss)", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "crated_by", * "description": "User ID of entry creator", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "state", * "description": "Published state (0 = unpublished, 1 = published)", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "reward", * "description": "Reward points", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "tags", * "description": "Comma-separated list of tags", * "type": "string", * "required": false, * "default": null * } * @return void */ public function updateTask() { $this->requiresAuthentication(); $fields = array('id' => Request::getInt('id', 0, 'post'), 'email' => Request::getInt('email', null), 'anonymous' => Request::getInt('anonymous', null), 'subject' => Request::getVar('subject', null, '', 'none', 2), 'question' => Request::getVar('question', null, '', 'none', 2), 'created' => Request::getVar('created', null), 'created_by' => Request::getInt('created_by', null), 'state' => Request::getInt('state', null), 'reward' => Request::getInt('reward', null), 'tags' => Request::getVar('tags', null)); $row = new Question($fields['id']); if (!$row->exists()) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_MISSING_RECORD'), 404); } if (!$row->bind($fields)) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_BINDING_DATA'), 422); } $row->set('email', isset($fields['email']) ? 1 : 0); $row->set('anonymous', isset($fields['anonymous']) ? 1 : 0); if (!$row->store(true)) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_DATA'), 500); } if (isset($fields['tags'])) { if (!$row->tag($fields['tags'], User::get('id'))) { throw new Exception(Lang::txt('COM_ANSWERS_ERROR_SAVING_TAGS'), 500); } } $this->send($row); }
/** * 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); }
/** * 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)); }