/** * 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); }
/** * Get a list of chosen responses * * @param string $rtrn Data type to return [count, list] * @param array $filters Filters to apply to query * @return mixed Returns an integer or array depending upon format chosen */ public function chosen($rtrn = 'list', $filters = array()) { $tbl = new Tables\Response($this->_db); if (!isset($filters['question_id'])) { $filters['question_id'] = $this->get('id'); } $filters['state'] = 1; $filters['filterby'] = 'accepted'; $filters['sort'] = 'created'; $filters['sort_Dir'] = 'DESC'; switch (strtolower($rtrn)) { case 'count': if ($this->get('chosen_count', null) === null) { $this->set('chosen_count', $tbl->find('count', $filters)); } return $this->get('chosen_count'); break; case 'list': case 'results': default: if ($this->get('chosen', null) === null || !$this->get('chosen') instanceof ItemList) { if ($results = $tbl->find('list', $filters)) { foreach ($results as $key => $result) { $results[$key] = new Response($result); } } else { $results = array(); } $this->set('chosen', new ItemList($results)); } return $this->get('chosen'); break; } }