[Poll] Refactoring and minor bug fixes

This commit is contained in:
Diogo Cordeiro 2019-06-23 00:55:40 +01:00
parent f2705180e0
commit 44653d339d
14 changed files with 330 additions and 250 deletions

View File

@ -47,7 +47,7 @@ class FormAction extends ManagedAction
protected $needLogin = true;
protected $canPost = true;
protected function prepare(array $args=array()) {
protected function prepare(array $args = []) {
parent::prepare($args);
$this->form = $this->form ?: ucfirst($this->action);

View File

@ -32,7 +32,7 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class ManagedAction extends Action
{
protected function prepare(array $args=array())
protected function prepare(array $args = [])
{
if (!parent::prepare($args)) {
return false;

View File

@ -63,7 +63,7 @@ class Widget
* @param Action $out output helper, defaults to null
*/
function __construct(Action $out=null, array $widgetOpts=array())
function __construct(Action $out = null, array $widgetOpts = [])
{
$this->out = $out;
if (!array_key_exists('scoped', $widgetOpts)) {

View File

@ -45,23 +45,23 @@ if (!defined('STATUSNET')) {
*/
class PollPlugin extends MicroAppPlugin
{
const PLUGIN_VERSION = '0.1.0';
const PLUGIN_VERSION = '0.1.1';
// @fixme which domain should we use for these namespaces?
const POLL_OBJECT = 'http://activityschema.org/object/poll';
const POLL_OBJECT = 'http://activityschema.org/object/poll';
const POLL_RESPONSE_OBJECT = 'http://activityschema.org/object/poll-response';
var $oldSaveNew = true;
public $oldSaveNew = true;
/**
* Database schema setup
*
* @see Schema
* @return boolean hook value; true means continue processing, false means stop.
* @see ColumnDef
*
* @return boolean hook value; true means continue processing, false means stop.
* @see Schema
*/
function onCheckSchema()
public function onCheckSchema()
{
$schema = Schema::get();
$schema->ensureTable('poll', Poll::schemaDef());
@ -77,7 +77,7 @@ class PollPlugin extends MicroAppPlugin
*
* @return boolean hook value
*/
function onEndShowStyles($action)
public function onEndShowStyles($action)
{
$action->cssLink($this->path('css/poll.css'));
return true;
@ -92,23 +92,33 @@ class PollPlugin extends MicroAppPlugin
*/
public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/poll/new',
array('action' => 'newpoll'));
$m->connect(
'main/poll/new',
array('action' => 'newpoll')
);
$m->connect('main/poll/:id',
array('action' => 'showpoll'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
$m->connect(
'main/poll/:id',
array('action' => 'showpoll'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
);
$m->connect('main/poll/response/:id',
array('action' => 'showpollresponse'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
$m->connect(
'main/poll/response/:id',
array('action' => 'showpollresponse'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
);
$m->connect('main/poll/:id/respond',
array('action' => 'respondpoll'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
$m->connect(
'main/poll/:id/respond',
array('action' => 'respondpoll'),
array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
);
$m->connect('settings/poll',
array('action' => 'pollsettings'));
$m->connect(
'settings/poll',
array('action' => 'pollsettings')
);
return true;
}
@ -118,21 +128,22 @@ class PollPlugin extends MicroAppPlugin
*
* @param array &$versions array of version data
*
* @return value
* @return bool true hook value
* @throws Exception
*/
function onPluginVersion(array &$versions)
public function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Poll',
'version' => self::PLUGIN_VERSION,
'author' => 'Brion Vibber',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Poll',
'rawdescription' =>
// TRANS: Plugin description.
_m('Simple extension for supporting basic polls.'));
'version' => self::PLUGIN_VERSION,
'author' => 'Brion Vibber',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Poll',
'rawdescription' =>
// TRANS: Plugin description.
_m('Simple extension for supporting basic polls.'));
return true;
}
function types()
public function types()
{
return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
}
@ -144,7 +155,7 @@ class PollPlugin extends MicroAppPlugin
*
* @return boolean hook value
*/
function deleteRelated(Notice $notice)
public function deleteRelated(Notice $notice)
{
$p = Poll::getByNotice($notice);
@ -158,13 +169,14 @@ class PollPlugin extends MicroAppPlugin
/**
* 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
* @param Profile $profile Profile to use as author
* @param array $options Options to pass to bookmark-saving code
*
* @return Notice resulting notice
* @throws Exception if it failed
*/
function saveNoticeFromActivity(Activity $activity, Profile $profile, array $options=array())
public function saveNoticeFromActivity(Activity $activity, Profile $profile, array $options = array())
{
// @fixme
common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true));
@ -178,7 +190,7 @@ class PollPlugin extends MicroAppPlugin
$responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
if ($pollElements->length) {
$question = '';
$opts = array();
$opts = [];
$data = $pollElements->item(0);
foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) {
@ -194,7 +206,7 @@ class PollPlugin extends MicroAppPlugin
} catch (Exception $e) {
common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage());
}
} else if ($responseElements->length) {
} elseif ($responseElements->length) {
$data = $responseElements->item(0);
$pollUri = $data->getAttribute('poll');
$selection = intval($data->getAttribute('selection'));
@ -213,38 +225,41 @@ class PollPlugin extends MicroAppPlugin
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());
common_log(LOG_DEBUG, "Poll response save fail: " . $e->getMessage());
// TRANS: Exception thrown trying to respond to a non-existing poll.
}
} else {
common_log(LOG_DEBUG, "YYY no poll data");
}
}
// If it didn't return before
throw new ServerException(_m('Failed to save Poll response.'));
}
function activityObjectFromNotice(Notice $notice)
public function activityObjectFromNotice(Notice $notice)
{
assert($this->isMyNotice($notice));
switch ($notice->object_type) {
case self::POLL_OBJECT:
return $this->activityObjectFromNoticePoll($notice);
case self::POLL_RESPONSE_OBJECT:
return $this->activityObjectFromNoticePollResponse($notice);
default:
// TRANS: Exception thrown when performing an unexpected action on a poll.
// TRANS: %s is the unexpected object type.
throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
case self::POLL_OBJECT:
return $this->activityObjectFromNoticePoll($notice);
case self::POLL_RESPONSE_OBJECT:
return $this->activityObjectFromNoticePollResponse($notice);
default:
// TRANS: Exception thrown when performing an unexpected action on a poll.
// TRANS: %s is the unexpected object type.
throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
}
}
function activityObjectFromNoticePollResponse(Notice $notice)
public function activityObjectFromNoticePollResponse(Notice $notice)
{
$object = new ActivityObject();
$object->id = $notice->uri;
$object->type = self::POLL_RESPONSE_OBJECT;
$object->title = $notice->content;
$object->id = $notice->uri;
$object->type = self::POLL_RESPONSE_OBJECT;
$object->title = $notice->content;
$object->summary = $notice->content;
$object->link = $notice->getUrl();
$object->link = $notice->getUrl();
$response = Poll_response::getByNotice($notice);
if ($response) {
@ -260,14 +275,14 @@ class PollPlugin extends MicroAppPlugin
return $object;
}
function activityObjectFromNoticePoll(Notice $notice)
public function activityObjectFromNoticePoll(Notice $notice)
{
$object = new ActivityObject();
$object->id = $notice->uri;
$object->type = self::POLL_OBJECT;
$object->title = $notice->content;
$object->id = $notice->uri;
$object->type = self::POLL_OBJECT;
$object->title = $notice->content;
$object->summary = $notice->content;
$object->link = $notice->getUrl();
$object->link = $notice->getUrl();
$poll = Poll::getByNotice($notice);
if ($poll) {
@ -295,7 +310,7 @@ class PollPlugin extends MicroAppPlugin
* @param ActivityObject $obj
* @param XMLOutputter $out to add elements at end of object
*/
function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
public function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
{
if (isset($obj->pollQuestion)) {
/**
@ -321,8 +336,8 @@ class PollPlugin extends MicroAppPlugin
* selection="3" />
*/
$data = array('xmlns:poll' => self::POLL_OBJECT,
'poll' => $obj->pollUri,
'selection' => $obj->pollSelection);
'poll' => $obj->pollUri,
'selection' => $obj->pollSelection);
$out->element('poll:response', $data, '');
}
}
@ -356,7 +371,7 @@ class PollPlugin extends MicroAppPlugin
* }
*/
$data = array('question' => $obj->pollQuestion,
'options' => array());
'options' => array());
foreach ($obj->pollOptions as $opt) {
$data['options'][] = $opt;
}
@ -369,30 +384,30 @@ class PollPlugin extends MicroAppPlugin
* "selection": 3
* }
*/
$data = array('poll' => $obj->pollUri,
'selection' => $obj->pollSelection);
$data = array('poll' => $obj->pollUri,
'selection' => $obj->pollSelection);
$out['pollResponse'] = $data;
}
}
function entryForm($out)
public function entryForm($out)
{
return new NewPollForm($out);
}
// @fixme is this from parent?
function tag()
public function tag()
{
return 'poll';
}
function appTitle()
public function appTitle()
{
// TRANS: Application title.
return _m('APPTITLE','Poll');
return _m('APPTITLE', 'Poll');
}
function onStartAddNoticeReply($nli, $parent, $child)
public function onStartAddNoticeReply($nli, $parent, $child)
{
// Filter out any poll responses
if ($parent->object_type == self::POLL_OBJECT &&
@ -404,7 +419,8 @@ class PollPlugin extends MicroAppPlugin
// Hide poll responses for @chuck
function onEndNoticeWhoGets($notice, &$ni) {
public function onEndNoticeWhoGets($notice, &$ni)
{
if ($notice->object_type == self::POLL_RESPONSE_OBJECT) {
foreach ($ni as $id => $source) {
$user = User::getKV('id', $id);
@ -427,21 +443,23 @@ class PollPlugin extends MicroAppPlugin
* @return boolean hook return
*/
function onEndAccountSettingsNav($action)
public function onEndAccountSettingsNav($action)
{
$action_name = $action->trimmed('action');
$action->menuItem(common_local_url('pollsettings'),
// TRANS: Poll plugin menu item on user settings page.
_m('MENU', 'Polls'),
// TRANS: Poll plugin tooltip for user settings menu item.
_m('Configure poll behavior'),
$action_name === 'pollsettings');
$action->menuItem(
common_local_url('pollsettings'),
// TRANS: Poll plugin menu item on user settings page.
_m('MENU', 'Polls'),
// TRANS: Poll plugin tooltip for user settings menu item.
_m('Configure poll behavior'),
$action_name === 'pollsettings'
);
return true;
}
protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped = null)
{
if ($stored->object_type == self::POLL_RESPONSE_OBJECT) {
parent::showNoticeContent($stored, $out, $scoped);

View File

@ -45,19 +45,20 @@ if (!defined('STATUSNET')) {
*/
class NewPollAction extends Action
{
protected $user = null;
protected $error = null;
protected $complete = null;
protected $user = null;
protected $error = null;
protected $complete = null;
protected $question = null;
protected $options = array();
protected $question = null;
protected $options = array();
/**
* Returns the title of the action
*
* @return string Action title
* @throws Exception
*/
function title()
public function title()
{
// TRANS: Title for poll page.
return _m('New poll');
@ -71,7 +72,7 @@ class NewPollAction extends Action
* @return boolean true
* @throws ClientException
*/
function prepare(array $args = [])
public function prepare(array $args = [])
{
parent::prepare($args);
@ -79,8 +80,10 @@ class NewPollAction extends Action
if (empty($this->user)) {
// TRANS: Client exception thrown trying to create a poll while not logged in.
throw new ClientException(_m('You must be logged in to post a poll.'),
403);
throw new ClientException(
_m('You must be logged in to post a poll.'),
403
);
}
if ($this->isPost()) {
@ -102,8 +105,12 @@ class NewPollAction extends Action
* Handler method
*
* @return void
* @throws ClientException
* @throws InvalidUrlException
* @throws ReflectionException
* @throws ServerException
*/
function handle()
public function handle()
{
parent::handle();
@ -120,15 +127,19 @@ class NewPollAction extends Action
* Add a new Poll
*
* @return void
* @throws ClientException
* @throws InvalidUrlException
* @throws ReflectionException
* @throws ServerException
*/
function newPoll()
public function newPoll()
{
if ($this->boolean('ajax')) {
GNUsocial::setApi(true);
}
try {
if (empty($this->question)) {
// TRANS: Client exception thrown trying to create a poll without a question.
// TRANS: Client exception thrown trying to create a poll without a question.
throw new ClientException(_m('Poll must have a question.'));
}
@ -145,11 +156,12 @@ class NewPollAction extends Action
ToSelector::fillOptions($this, $options);
$saved = Poll::saveNew($this->user->getProfile(),
$this->question,
$this->options,
$options);
$saved = Poll::saveNew(
$this->user->getProfile(),
$this->question,
$this->options,
$options
);
} catch (ClientException $ce) {
$this->error = $ce->getMessage();
$this->showPage();
@ -180,7 +192,7 @@ class NewPollAction extends Action
*
* @return void
*/
function showNotice(Notice $notice)
public function showNotice(Notice $notice)
{
class_exists('NoticeList'); // @fixme hack for autoloader
$nli = new NoticeListItem($notice, $this);
@ -192,15 +204,17 @@ class NewPollAction extends Action
*
* @return void
*/
function showContent()
public function showContent()
{
if (!empty($this->error)) {
$this->element('p', 'error', $this->error);
}
$form = new NewPollForm($this,
$this->question,
$this->options);
$form = new NewPollForm(
$this,
$this->question,
$this->options
);
$form->show();
@ -216,7 +230,7 @@ class NewPollAction extends Action
*
* @return boolean is read only action?
*/
function isReadOnly($args)
public function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {

View File

@ -27,7 +27,9 @@
* @link http://status.net/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
if (!defined('GNUSOCIAL')) {
exit(1);
}
class PollSettingsAction extends SettingsAction
{
@ -36,7 +38,7 @@ class PollSettingsAction extends SettingsAction
*
* @return string Page title
*/
function title()
public function title()
{
// TRANS: Page title.
return _m('Poll settings');
@ -48,7 +50,7 @@ class PollSettingsAction extends SettingsAction
* @return string Instructions for use
*/
function getInstructions()
public function getInstructions()
{
// TRANS: Page instructions.
return _m('Set your poll preferences');
@ -57,7 +59,7 @@ class PollSettingsAction extends SettingsAction
protected function getForm()
{
$prefs = User_poll_prefs::getKV('user_id', $this->scoped->getID());
$form = new PollPrefsForm($this, $prefs);
$form = new PollPrefsForm($this, $prefs ? $prefs : null);
return $form;
}
@ -75,7 +77,7 @@ class PollSettingsAction extends SettingsAction
}
$upp->hide_responses = $this->boolean('hide_responses');
$upp->modified = common_sql_now();
$upp->modified = common_sql_now();
if ($orig instanceof User_poll_prefs) {
$upp->update($orig);

View File

@ -56,8 +56,9 @@ class RespondPollAction extends Action
* Returns the title of the action
*
* @return string Action title
* @throws Exception
*/
function title()
public function title()
{
// TRANS: Page title for poll response.
return _m('Poll response');
@ -70,8 +71,10 @@ class RespondPollAction extends Action
*
* @return boolean true
* @throws ClientException
* @throws Exception
* @throws Exception
*/
function prepare(array $args = [])
public function prepare(array $args = [])
{
parent::prepare($args);
if ($this->boolean('ajax')) {
@ -82,8 +85,10 @@ class RespondPollAction extends Action
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);
throw new ClientException(
_m('You must be logged in to respond to a poll.'),
403
);
}
if ($this->isPost()) {
@ -111,8 +116,11 @@ class RespondPollAction extends Action
* Handler method
*
* @return void
* @throws ClientException
* @throws ReflectionException
* @throws ServerException
*/
function handle()
public function handle()
{
parent::handle();
@ -129,13 +137,18 @@ class RespondPollAction extends Action
* Add a new Poll
*
* @return void
* @throws ClientException
* @throws ReflectionException
* @throws ServerException
*/
function respondPoll()
public function respondPoll()
{
try {
$notice = Poll_response::saveNew($this->user->getProfile(),
Poll_response::saveNew(
$this->user->getProfile(),
$this->poll,
$this->selection);
$this->selection
);
} catch (ClientException $ce) {
$this->error = $ce->getMessage();
$this->showPage();
@ -163,7 +176,7 @@ class RespondPollAction extends Action
*
* @return void
*/
function showContent()
public function showContent()
{
if (!empty($this->error)) {
$this->element('p', 'error', $this->error);
@ -185,7 +198,7 @@ class RespondPollAction extends Action
*
* @return boolean is read only action?
*/
function isReadOnly($args)
public function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {

View File

@ -48,7 +48,7 @@ class ShowPollAction extends ShownoticeAction
{
protected $poll = null;
function getNotice()
public function getNotice()
{
$this->id = $this->trimmed('id');
@ -77,19 +77,21 @@ class ShowPollAction extends ShownoticeAction
*
* @return string page tile
*/
function title()
public function title()
{
// TRANS: Page title for a poll.
// TRANS: %1$s is the nickname of the user that created the poll, %2$s is the poll question.
return sprintf(_m('%1$s\'s poll: %2$s'),
$this->user->nickname,
$this->poll->question);
return sprintf(
_m('%1$s\'s poll: %2$s'),
$this->user->nickname,
$this->poll->question
);
}
/**
* @fixme combine the notice time with poll update time
*/
function lastModified()
public function lastModified()
{
return Action::lastModified();
}
@ -98,7 +100,7 @@ class ShowPollAction extends ShownoticeAction
/**
* @fixme combine the notice time with poll update time
*/
function etag()
public function etag()
{
return Action::etag();
}

View File

@ -42,7 +42,6 @@ if (!defined('STATUSNET')) {
*
* @see DB_DataObject
*/
class Poll extends Managed_DataObject
{
public $__table = 'poll'; // table name
@ -80,14 +79,14 @@ class Poll extends Managed_DataObject
*
* @param Notice $notice Notice to check for
*
* @return Poll found poll or null
* @return get_called_class found poll or null
*/
static function getByNotice($notice)
public static function getByNotice($notice)
{
return self::getKV('uri', $notice->uri);
}
function getOptions()
public function getOptions()
{
return explode("\n", $this->options);
}
@ -95,10 +94,10 @@ class Poll extends Managed_DataObject
/**
* Is this a valid selection index?
*
* @param numeric $selection (1-based)
* @param int $selection (1-based)
* @return boolean
*/
function isValidSelection($selection)
public function isValidSelection($selection)
{
if ($selection != intval($selection)) {
return false;
@ -109,12 +108,12 @@ class Poll extends Managed_DataObject
return true;
}
function getNotice()
public function getNotice()
{
return Notice::getKV('uri', $this->uri);
}
function getUrl()
public function getUrl()
{
return $this->getNotice()->getUrl();
}
@ -123,16 +122,16 @@ class Poll extends Managed_DataObject
* Get the response of a particular user to this poll, if any.
*
* @param Profile $profile
* @return Poll_response object or null
* @return get_called_class object or null
*/
function getResponse(Profile $profile)
public function getResponse(Profile $profile)
{
$pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
'profile_id' => $profile->id));
return $pr;
$pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
'profile_id' => $profile->id));
return $pr;
}
function countResponses()
public function countResponses()
{
$pr = new Poll_response();
$pr->poll_id = $this->id;
@ -162,12 +161,14 @@ class Poll extends Managed_DataObject
* Save a new poll notice
*
* @param Profile $profile
* @param string $question
* @param array $opts (poll responses)
* @param string $question
* @param array $opts (poll responses)
*
* @param null $options
* @return Notice saved notice
* @throws ClientException
*/
static function saveNew($profile, $question, $opts, $options=null)
public static function saveNew($profile, $question, $opts, $options = null)
{
if (empty($options)) {
$options = array();
@ -175,10 +176,10 @@ class Poll extends Managed_DataObject
$p = new Poll();
$p->id = UUID::gen();
$p->profile_id = $profile->id;
$p->question = $question;
$p->options = implode("\n", $opts);
$p->id = UUID::gen();
$p->profile_id = $profile->id;
$p->question = $question;
$p->options = implode("\n", $opts);
if (array_key_exists('created', $options)) {
$p->created = $options['created'];
@ -189,8 +190,10 @@ class Poll extends Managed_DataObject
if (array_key_exists('uri', $options)) {
$p->uri = $options['uri'];
} else {
$p->uri = common_local_url('showpoll',
array('id' => $p->id));
$p->uri = common_local_url(
'showpoll',
array('id' => $p->id)
);
}
common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
@ -198,33 +201,39 @@ class Poll extends Managed_DataObject
// TRANS: Notice content creating a poll.
// TRANS: %1$s is the poll question, %2$s is a link to the poll.
$content = sprintf(_m('Poll: %1$s %2$s'),
$question,
$p->uri);
$content = sprintf(
_m('Poll: %1$s %2$s'),
$question,
$p->uri
);
$link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
// TRANS: Rendered version of the notice content creating a poll.
// TRANS: %s is a link to the poll with the question as link description.
$rendered = sprintf(_m('Poll: %s'), $link);
$tags = array('poll');
$tags = array('poll');
$replies = array();
$options = array_merge(array('urls' => array(),
'rendered' => $rendered,
'tags' => $tags,
'replies' => $replies,
'object_type' => PollPlugin::POLL_OBJECT),
$options);
$options = array_merge(
array('urls' => array(),
'rendered' => $rendered,
'tags' => $tags,
'replies' => $replies,
'object_type' => PollPlugin::POLL_OBJECT),
$options
);
if (!array_key_exists('uri', $options)) {
$options['uri'] = $p->uri;
}
$saved = Notice::saveNew($profile->id,
$content,
array_key_exists('source', $options) ?
$options['source'] : 'web',
$options);
$saved = Notice::saveNew(
$profile->id,
$content,
array_key_exists('source', $options) ?
$options['source'] : 'web',
$options
);
return $saved;
}

View File

@ -83,9 +83,9 @@ class Poll_response extends Managed_DataObject
*
* @param Notice $notice Notice to check for
*
* @return Poll_response found response or null
* @return get_called_class found response or null
*/
static function getByNotice($notice)
public static function getByNotice($notice)
{
return self::getKV('uri', $notice->uri);
}
@ -93,40 +93,41 @@ class Poll_response extends Managed_DataObject
/**
* Get the notice that belongs to this response...
*
* @return Notice
* @return get_called_class
*/
function getNotice()
public function getNotice()
{
return Notice::getKV('uri', $this->uri);
}
function getUrl()
public function getUrl()
{
return $this->getNotice()->getUrl();
}
/**
*
* @return Poll
* @return get_called_class
*/
function getPoll()
public function getPoll()
{
return Poll::getKV('id', $this->poll_id);
}
/**
* Save a new poll notice
*
* @param Profile $profile
* @param Poll $poll the poll being responded to
* @param int $selection (1-based)
* @param array $opts (poll responses)
*
* @param Poll $poll the poll being responded to
* @param int $selection (1-based)
* @param null $options
* @return Notice saved notice
* @throws ClientException
*/
static function saveNew($profile, $poll, $selection, $options=null)
public static function saveNew($profile, $poll, $selection, $options = null)
{
if (empty($options)) {
$options = array();
$options = [];
}
if (!$poll->isValidSelection($selection)) {
@ -137,10 +138,10 @@ class Poll_response extends Managed_DataObject
$answer = $opts[$selection - 1];
$pr = new Poll_response();
$pr->id = UUID::gen();
$pr->profile_id = $profile->id;
$pr->poll_id = $poll->id;
$pr->selection = $selection;
$pr->id = UUID::gen();
$pr->profile_id = $profile->id;
$pr->poll_id = $poll->id;
$pr->selection = $selection;
if (array_key_exists('created', $options)) {
$pr->created = $options['created'];
@ -151,8 +152,10 @@ class Poll_response extends Managed_DataObject
if (array_key_exists('uri', $options)) {
$pr->uri = $options['uri'];
} else {
$pr->uri = common_local_url('showpollresponse',
array('id' => $pr->id));
$pr->uri = common_local_url(
'showpollresponse',
array('id' => $pr->id)
);
}
common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
@ -160,31 +163,37 @@ class Poll_response extends Managed_DataObject
// TRANS: Notice content voting for a poll.
// TRANS: %s is the chosen option in the poll.
$content = sprintf(_m('voted for "%s"'),
$answer);
$content = sprintf(
_m('voted for "%s"'),
$answer
);
$link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
// TRANS: Rendered version of the notice content voting for a poll.
// TRANS: %s a link to the poll with the chosen option as link description.
$rendered = sprintf(_m('voted for "%s"'), $link);
$tags = array();
$tags = array();
$options = array_merge(array('urls' => array(),
'rendered' => $rendered,
'tags' => $tags,
'reply_to' => $poll->getNotice()->id,
'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
$options);
$options = array_merge(
array('urls' => array(),
'rendered' => $rendered,
'tags' => $tags,
'reply_to' => $poll->getNotice()->id,
'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
$options
);
if (!array_key_exists('uri', $options)) {
$options['uri'] = $pr->uri;
}
$saved = Notice::saveNew($profile->id,
$content,
array_key_exists('source', $options) ?
$options['source'] : 'web',
$options);
$saved = Notice::saveNew(
$profile->id,
$content,
array_key_exists('source', $options) ?
$options['source'] : 'web',
$options
);
return $saved;
}

View File

@ -52,11 +52,12 @@ class NewpollForm extends Form
/**
* Construct a new poll form
*
* @param HTMLOutputter $out output channel
* @param HTMLOutputter $out output channel
*
* @return void
* @param null $question
* @param null $options
*/
function __construct($out=null, $question=null, $options=null)
public function __construct(HTMLOutputter $out = null, $question = null, $options = null)
{
parent::__construct($out);
}
@ -66,7 +67,7 @@ class NewpollForm extends Form
*
* @return int ID of the form
*/
function id()
public function id()
{
return 'newpoll-form';
}
@ -76,7 +77,7 @@ class NewpollForm extends Form
*
* @return string class of the form
*/
function formClass()
public function formClass()
{
return 'form_settings ajax-notice';
}
@ -86,7 +87,7 @@ class NewpollForm extends Form
*
* @return string URL of the action
*/
function action()
public function action()
{
return common_local_url('newpoll');
}
@ -96,20 +97,22 @@ class NewpollForm extends Form
*
* @return void
*/
function formData()
public function formData()
{
$this->out->elementStart('fieldset', array('id' => 'newpoll-data'));
$this->out->elementStart('ul', 'form_data');
$this->li();
$this->out->input('question',
// TRANS: Field label on the page to create a poll.
_m('Question'),
$this->question,
// TRANS: Field title on the page to create a poll.
_m('What question are people answering?'),
'question',
true); // HTML5 "required" attribute
$this->out->input(
'question',
// TRANS: Field label on the page to create a poll.
_m('Question'),
$this->question,
// TRANS: Field title on the page to create a poll.
_m('What question are people answering?'),
'question',
true
); // HTML5 "required" attribute
$this->unli();
$max = 5;
@ -124,22 +127,26 @@ class NewpollForm extends Form
$default = '';
}
$this->li();
$this->out->input('poll-option' . ($i + 1),
// TRANS: Field label for an answer option on the page to create a poll.
// TRANS: %d is the option number.
sprintf(_m('Option %d'), $i + 1),
$default,
null,
'option' . ($i + 1),
$i<2); // HTML5 "required" attribute for 2 options
$this->out->input(
'poll-option' . ($i + 1),
// TRANS: Field label for an answer option on the page to create a poll.
// TRANS: %d is the option number.
sprintf(_m('Option %d'), $i + 1),
$default,
null,
'option' . ($i + 1),
$i < 2
); // HTML5 "required" attribute for 2 options
$this->unli();
}
$this->out->elementEnd('ul');
$toWidget = new ToSelector($this->out,
common_current_user(),
null);
$toWidget = new ToSelector(
$this->out,
common_current_user(),
null
);
$toWidget->show();
$this->out->elementEnd('fieldset');
@ -150,7 +157,7 @@ class NewpollForm extends Form
*
* @return void
*/
function formActions()
public function formActions()
{
// TRANS: Button text for saving a new poll.
$this->out->submit('poll-submit', _m('BUTTON', 'Save'), 'submit', 'submit');

View File

@ -1,10 +1,12 @@
<?php
if (!defined('GNUSOCIAL')) { exit(1); }
if (!defined('GNUSOCIAL')) {
exit(1);
}
class PollPrefsForm extends Form
{
function __construct(Action $out, User_poll_prefs $prefs=null)
public function __construct(Action $out, User_poll_prefs $prefs = null)
{
parent::__construct($out);
$this->prefs = $prefs;
@ -19,14 +21,16 @@ class PollPrefsForm extends Form
* @return void
*/
function formData()
public function formData()
{
$this->elementStart('fieldset');
$this->elementStart('ul', 'form_data');
$this->elementStart('li');
$this->checkbox('hide_responses',
_('Do not deliver poll responses to my home timeline'),
($this->prefs instanceof User_poll_prefs && $this->prefs->hide_responses));
$this->checkbox(
'hide_responses',
_('Do not deliver poll responses to my home timeline'),
($this->prefs instanceof User_poll_prefs && $this->prefs->hide_responses)
);
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
@ -41,7 +45,7 @@ class PollPrefsForm extends Form
* @return void
*/
function formActions()
public function formActions()
{
$this->submit('submit', _('Save'));
}
@ -55,7 +59,7 @@ class PollPrefsForm extends Form
* @return int ID of the form
*/
function id()
public function id()
{
return 'form_poll_prefs';
}
@ -69,7 +73,7 @@ class PollPrefsForm extends Form
* @return string URL to post to
*/
function action()
public function action()
{
return common_local_url('pollsettings');
}
@ -80,7 +84,7 @@ class PollPrefsForm extends Form
* @return string the form's class
*/
function formClass()
public function formClass()
{
return 'form_settings';
}

View File

@ -52,11 +52,11 @@ class PollResponseForm extends Form
* Construct a new poll form
*
* @param Poll $poll
* @param HTMLOutputter $out output channel
* @param HTMLOutputter $out output channel
*
* @return void
*/
function __construct(Poll $poll, HTMLOutputter $out)
public function __construct(Poll $poll, HTMLOutputter $out)
{
parent::__construct($out);
$this->poll = $poll;
@ -67,7 +67,7 @@ class PollResponseForm extends Form
*
* @return int ID of the form
*/
function id()
public function id()
{
return 'pollresponse-form';
}
@ -77,7 +77,7 @@ class PollResponseForm extends Form
*
* @return string class of the form
*/
function formClass()
public function formClass()
{
return 'form_settings ajax';
}
@ -87,7 +87,7 @@ class PollResponseForm extends Form
*
* @return string URL of the action
*/
function action()
public function action()
{
return common_local_url('respondpoll', array('id' => $this->poll->id));
}
@ -97,18 +97,17 @@ class PollResponseForm extends Form
*
* @return void
*/
function formData()
public function formData()
{
$poll = $this->poll;
$out = $this->out;
$id = "poll-" . $poll->id;
$out->element('p', 'poll-question', $poll->question);
$out->elementStart('ul', 'poll-options');
foreach ($poll->getOptions() as $i => $opt) {
$out->elementStart('li');
$out->elementStart('label');
$out->element('input', array('type' => 'radio', 'name' => 'pollselection', 'value' => $i + 1), '');
$out->element('input', ['type' => 'radio', 'name' => 'pollselection', 'value' => $i + 1], '');
$out->text(' ' . $opt);
$out->elementEnd('label');
$out->elementEnd('li');
@ -121,7 +120,7 @@ class PollResponseForm extends Form
*
* @return void
*/
function formActions()
public function formActions()
{
// TRANS: Button text for submitting a poll response.
$this->out->submit('poll-response-submit', _m('BUTTON', 'Submit'), 'submit', 'submit');

View File

@ -52,11 +52,11 @@ class PollResultForm extends Form
* Construct a new poll form
*
* @param Poll $poll
* @param HTMLOutputter $out output channel
* @param HTMLOutputter $out output channel
*
* @return void
*/
function __construct(Poll $poll, HTMLOutputter $out)
public function __construct(Poll $poll, HTMLOutputter $out)
{
parent::__construct($out);
$this->poll = $poll;