/** * For initializing members of the class. * * @param array $argarray misc. arguments * * @return boolean true */ function prepare($argarray) { parent::prepare($argarray); if ($this->boolean('ajax')) { StatusNet::setApi(true); } $this->user = common_current_user(); if (empty($this->user)) { // TRANS: Client exception thrown trying to respond to a poll while not logged in. throw new ClientException(_m('You must be logged in to respond to a poll.'), 403); } if ($this->isPost()) { $this->checkSessionToken(); } $id = $this->trimmed('id'); $this->poll = Poll::staticGet('id', $id); if (empty($this->poll)) { // TRANS: Client exception thrown trying to respond to a non-existing poll. throw new ClientException(_m('Invalid or missing poll.'), 404); } $selection = intval($this->trimmed('pollselection')); if ($selection < 1 || $selection > count($this->poll->getOptions())) { // TRANS: Client exception thrown responding to a poll with an invalid answer. throw new ClientException(_m('Invalid poll selection.')); } $this->selection = $selection; return true; }
function getNotice() { $this->id = $this->trimmed('id'); $this->poll = Poll::staticGet('id', $this->id); if (empty($this->poll)) { // TRANS: Client exception thrown trying to view a non-existing poll. throw new ClientException(_m('No such poll.'), 404); } $notice = $this->poll->getNotice(); if (empty($notice)) { // Did we used to have it, and it got deleted? // TRANS: Client exception thrown trying to view a non-existing poll notice. throw new ClientException(_m('No such poll notice.'), 404); } return $notice; }
/** * * @return Poll */ function getPoll() { return Poll::staticGet('id', $this->poll_id); }
/** * Save a poll from an activity * * @param Profile $profile Profile to use as author * @param Activity $activity Activity to save * @param array $options Options to pass to bookmark-saving code * * @return Notice resulting notice */ function saveNoticeFromActivity($activity, $profile, $options = array()) { // @fixme common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true)); common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true)); common_log(LOG_DEBUG, "XXX options: " . var_export($options, true)); // Ok for now, we can grab stuff from the XML entry directly. // This won't work when reading from JSON source if ($activity->entry) { $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll'); $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response'); if ($pollElements->length) { $question = ''; $opts = array(); $data = $pollElements->item(0); foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) { $question = $node->textContent; } foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'option') as $node) { $opts[] = $node->textContent; } try { $notice = Poll::saveNew($profile, $question, $opts, $options); common_log(LOG_DEBUG, "Saved Poll from ActivityStream data ok: notice id " . $notice->id); return $notice; } catch (Exception $e) { common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage()); } } else { if ($responseElements->length) { $data = $responseElements->item(0); $pollUri = $data->getAttribute('poll'); $selection = intval($data->getAttribute('selection')); if (!$pollUri) { // TRANS: Exception thrown trying to respond to a poll without a poll reference. throw new Exception(_m('Invalid poll response: No poll reference.')); } $poll = Poll::staticGet('uri', $pollUri); if (!$poll) { // TRANS: Exception thrown trying to respond to a non-existing poll. throw new Exception(_m('Invalid poll response: Poll is unknown.')); } try { $notice = Poll_response::saveNew($profile, $poll, $selection, $options); common_log(LOG_DEBUG, "Saved Poll_response ok, notice id: " . $notice->id); return $notice; } catch (Exception $e) { common_log(LOG_DEBUG, "Poll response save fail: " . $e->getMessage()); } } else { common_log(LOG_DEBUG, "YYY no poll data"); } } } }