Example #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");
             }
         }
     }
 }
Example #2
0
 /**
  * Add a new Poll
  *
  * @return void
  */
 function respondPoll()
 {
     try {
         $notice = Poll_response::saveNew($this->user->getProfile(), $this->poll, $this->selection);
     } catch (ClientException $ce) {
         $this->error = $ce->getMessage();
         $this->showPage();
         return;
     }
     if ($this->boolean('ajax')) {
         $this->startHTML('text/xml;charset=utf-8');
         $this->elementStart('head');
         // TRANS: Page title after sending a poll response.
         $this->element('title', null, _m('Poll results'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $form = new PollResultForm($this->poll, $this);
         $form->show();
         $this->elementEnd('body');
         $this->endHTML();
     } else {
         common_redirect($this->poll->getUrl(), 303);
     }
 }