Пример #1
0
 /**
  * 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 $activity, Profile $profile, array $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::getKV('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");
             }
         }
     }
 }
Пример #2
0
 /**
  * Add a new Poll
  *
  * @return void
  */
 function newPoll()
 {
     if ($this->boolean('ajax')) {
         StatusNet::setApi(true);
     }
     try {
         if (empty($this->question)) {
             // TRANS: Client exception thrown trying to create a poll without a question.
             throw new ClientException(_m('Poll must have a question.'));
         }
         if (count($this->options) < 2) {
             // TRANS: Client exception thrown trying to create a poll with fewer than two options.
             throw new ClientException(_m('Poll must have at least two options.'));
         }
         // Notice options; distinct from choices for the poll
         $options = array();
         // Does the heavy-lifting for getting "To:" information
         ToSelector::fillOptions($this, $options);
         $saved = Poll::saveNew($this->user->getProfile(), $this->question, $this->options, $options);
     } catch (ClientException $ce) {
         $this->error = $ce->getMessage();
         $this->showPage();
         return;
     }
     if ($this->boolean('ajax')) {
         header('Content-Type: text/xml;charset=utf-8');
         $this->xw->startDocument('1.0', 'UTF-8');
         $this->elementStart('html');
         $this->elementStart('head');
         // TRANS: Page title after sending a notice.
         $this->element('title', null, _m('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->showNotice($saved);
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         common_redirect($saved->bestUrl(), 303);
     }
 }