/** * Delete the record and all associated data * * @return boolean False if error, True on success */ public function delete() { // Can't delete what doesn't exist if (!$this->exists()) { return true; } // Remove comments foreach ($this->replies('list') as $comment) { if (!$comment->delete()) { $this->setError($comment->getError()); return false; } } // Clear the history of "helpful" clicks $al = new Tables\Log($this->_db); if (!$al->deleteLog($this->get('id'))) { $this->setError($al->getError()); return false; } return parent::delete(); }
/** * Rate an item * * @return void */ public function rateitemTask() { $no_html = Request::getInt('no_html', 0); // Is the user logged in? if (User::isGuest()) { if (!$no_html) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN_TO_VOTE')); $this->loginTask(); } return; } // Incoming $id = Request::getInt('refid', 0); $cat = Request::getVar('category', ''); $vote = Request::getVar('vote', ''); $ip = Request::ip(); // Check for reference ID if (!$id) { // cannot proceed if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('No ID provided.'), 'error'); } return; } // load answer $row = new Response($id); $qid = $row->get('question_id'); // Can't vote for your own comment if ($row->get('created_by') == User::get('username')) { if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid), Lang::txt('Cannot vote for your own entries.'), 'warning'); } return; } // Can't vote for your own comment if (!$vote) { if (!$no_html) { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid), Lang::txt('No vote provided.'), 'warning'); } return; } // Get vote log $al = new Tables\Log($this->database); $al->loadByIp($id, $ip); if (!$al->id) { // new vote; // record if it was helpful or not switch ($vote) { case 'yes': case 'like': case 'up': case 1: $row->set('helpful', $row->get('helpful') + 1); break; case 'no': case 'dislike': case 'down': case -1: $row->set('nothelpful', $row->get('nothelpful') + 1); break; } } else { if ($al->helpful != $vote) { // changing vote; // Adjust values to reflect vote change switch ($vote) { case 'yes': case 'like': case 'up': case 1: $row->set('helpful', $row->get('helpful') + 1); $row->set('nothelpful', $row->get('nothelpful') - 1); break; case 'no': case 'dislike': case 'down': case -1: $row->set('helpful', $row->get('helpful') - 1); $row->set('nothelpful', $row->get('nothelpful') + 1); break; } } else { // no vote change; } } if (!$row->store(false)) { $this->setError($row->getError()); return; } // Record user's vote (old way) $al->response_id = $row->get('id'); $al->ip = $ip; $al->helpful = $vote; if (!$al->check()) { echo $al->getError(); $this->setError($al->getError()); return; } if (!$al->store()) { echo $al->getError(); $this->setError($al->getError()); return; } // Record user's vote (new way) if ($cat) { require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'vote.php'; $v = new Tables\Vote($this->database); $v->referenceid = $row->get('id'); $v->category = $cat; $v->voter = User::get('id'); $v->ip = $ip; $v->voted = Date::toSql(); $v->helpful = $vote; if (!$v->check()) { echo $v->getError(); $this->setError($v->getError()); return; } if (!$v->store()) { echo $v->getError(); $this->setError($v->getError()); return; } } // update display if ($no_html) { $row->set('vote', $vote); $this->view->option = $this->_option; $this->view->item = $row; foreach ($this->getErrors() as $error) { $this->view->setError($error); } $this->view->display(); } else { App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $qid)); } }