/** * Add vote * * @param integer $option_id The id of the option selected * @return boolean */ public function vote($option_id) { $option_id = (int) $option_id; $option = Option::oneOrFail($option_id); $option->set('hits', (int) $option->get('hits', 0) + 1); if (!$option->save()) { $this->setError($option->getError()); return false; } $this->set('voters', (int) $this->get('voters', 0) + 1); if (!$this->save()) { return false; } $dt = new Date(); $dt->set(['date' => \Date::toSql(), 'vote_id' => (int) $option_id, 'poll_id' => (int) $this->get('id')]); if (!$dt->save()) { $this->setError($dt->getError()); return false; } return true; }
/** * Save an entry * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option)) { App::abort(403, Lang::txt('JERROR_ALERTNOAUTHOR')); } $fields = Request::getVar('fields', array(), 'post'); // Save the poll parent information $row = Poll::oneOrNew($fields['id'])->set($fields); if (!$row->save()) { Notify::error($row->getError()); return $this->editTask($row); } $row->checkin(); // Save the poll options $options = Request::getVar('polloption', array(), 'post'); foreach ($options as $i => $text) { $option = new Option(); $option->set('poll_id', (int) $row->get('id')); $option->set('text', htmlspecialchars($text, ENT_QUOTES, 'UTF-8')); if ($fields['id']) { $option->set('id', (int) $i); } if (!$option->save()) { Notify::error($option->getError()); return $this->editTask($row); } } Notify::success(Lang::txt('COM_POLL_ITEM_SAVED')); if ($this->getTask() == 'apply') { return $this->editTask($row); } $this->cancelTask(); }