[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 $needLogin = true;
protected $canPost = true; protected $canPost = true;
protected function prepare(array $args=array()) { protected function prepare(array $args = []) {
parent::prepare($args); parent::prepare($args);
$this->form = $this->form ?: ucfirst($this->action); $this->form = $this->form ?: ucfirst($this->action);

View File

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

View File

@ -63,7 +63,7 @@ class Widget
* @param Action $out output helper, defaults to null * @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; $this->out = $out;
if (!array_key_exists('scoped', $widgetOpts)) { if (!array_key_exists('scoped', $widgetOpts)) {

View File

@ -45,23 +45,23 @@ if (!defined('STATUSNET')) {
*/ */
class PollPlugin extends MicroAppPlugin 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? // @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'; const POLL_RESPONSE_OBJECT = 'http://activityschema.org/object/poll-response';
var $oldSaveNew = true; public $oldSaveNew = true;
/** /**
* Database schema setup * Database schema setup
* *
* @see Schema * @return boolean hook value; true means continue processing, false means stop.
* @see ColumnDef * @see ColumnDef
* *
* @return boolean hook value; true means continue processing, false means stop. * @see Schema
*/ */
function onCheckSchema() public function onCheckSchema()
{ {
$schema = Schema::get(); $schema = Schema::get();
$schema->ensureTable('poll', Poll::schemaDef()); $schema->ensureTable('poll', Poll::schemaDef());
@ -77,7 +77,7 @@ class PollPlugin extends MicroAppPlugin
* *
* @return boolean hook value * @return boolean hook value
*/ */
function onEndShowStyles($action) public function onEndShowStyles($action)
{ {
$action->cssLink($this->path('css/poll.css')); $action->cssLink($this->path('css/poll.css'));
return true; return true;
@ -92,23 +92,33 @@ class PollPlugin extends MicroAppPlugin
*/ */
public function onRouterInitialized(URLMapper $m) public function onRouterInitialized(URLMapper $m)
{ {
$m->connect('main/poll/new', $m->connect(
array('action' => 'newpoll')); 'main/poll/new',
array('action' => 'newpoll')
);
$m->connect('main/poll/:id', $m->connect(
'main/poll/:id',
array('action' => 'showpoll'), 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}')); 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', $m->connect(
'main/poll/response/:id',
array('action' => 'showpollresponse'), 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}')); 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', $m->connect(
'main/poll/:id/respond',
array('action' => 'respondpoll'), 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}')); 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', $m->connect(
array('action' => 'pollsettings')); 'settings/poll',
array('action' => 'pollsettings')
);
return true; return true;
} }
@ -118,9 +128,10 @@ class PollPlugin extends MicroAppPlugin
* *
* @param array &$versions array of version data * @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', $versions[] = array('name' => 'Poll',
'version' => self::PLUGIN_VERSION, 'version' => self::PLUGIN_VERSION,
@ -132,7 +143,7 @@ class PollPlugin extends MicroAppPlugin
return true; return true;
} }
function types() public function types()
{ {
return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT); return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
} }
@ -144,7 +155,7 @@ class PollPlugin extends MicroAppPlugin
* *
* @return boolean hook value * @return boolean hook value
*/ */
function deleteRelated(Notice $notice) public function deleteRelated(Notice $notice)
{ {
$p = Poll::getByNotice($notice); $p = Poll::getByNotice($notice);
@ -158,13 +169,14 @@ class PollPlugin extends MicroAppPlugin
/** /**
* Save a poll from an activity * Save a poll from an activity
* *
* @param Profile $profile Profile to use as author
* @param Activity $activity Activity to save * @param Activity $activity Activity to save
* @param Profile $profile Profile to use as author
* @param array $options Options to pass to bookmark-saving code * @param array $options Options to pass to bookmark-saving code
* *
* @return Notice resulting notice * @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 // @fixme
common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true)); 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'); $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
if ($pollElements->length) { if ($pollElements->length) {
$question = ''; $question = '';
$opts = array(); $opts = [];
$data = $pollElements->item(0); $data = $pollElements->item(0);
foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) { foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) {
@ -194,7 +206,7 @@ class PollPlugin extends MicroAppPlugin
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage()); common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage());
} }
} else if ($responseElements->length) { } elseif ($responseElements->length) {
$data = $responseElements->item(0); $data = $responseElements->item(0);
$pollUri = $data->getAttribute('poll'); $pollUri = $data->getAttribute('poll');
$selection = intval($data->getAttribute('selection')); $selection = intval($data->getAttribute('selection'));
@ -214,14 +226,17 @@ class PollPlugin extends MicroAppPlugin
return $notice; return $notice;
} catch (Exception $e) { } 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 { } else {
common_log(LOG_DEBUG, "YYY no poll data"); 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)); assert($this->isMyNotice($notice));
@ -237,7 +252,7 @@ class PollPlugin extends MicroAppPlugin
} }
} }
function activityObjectFromNoticePollResponse(Notice $notice) public function activityObjectFromNoticePollResponse(Notice $notice)
{ {
$object = new ActivityObject(); $object = new ActivityObject();
$object->id = $notice->uri; $object->id = $notice->uri;
@ -260,7 +275,7 @@ class PollPlugin extends MicroAppPlugin
return $object; return $object;
} }
function activityObjectFromNoticePoll(Notice $notice) public function activityObjectFromNoticePoll(Notice $notice)
{ {
$object = new ActivityObject(); $object = new ActivityObject();
$object->id = $notice->uri; $object->id = $notice->uri;
@ -295,7 +310,7 @@ class PollPlugin extends MicroAppPlugin
* @param ActivityObject $obj * @param ActivityObject $obj
* @param XMLOutputter $out to add elements at end of object * @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)) { if (isset($obj->pollQuestion)) {
/** /**
@ -375,24 +390,24 @@ class PollPlugin extends MicroAppPlugin
} }
} }
function entryForm($out) public function entryForm($out)
{ {
return new NewPollForm($out); return new NewPollForm($out);
} }
// @fixme is this from parent? // @fixme is this from parent?
function tag() public function tag()
{ {
return 'poll'; return 'poll';
} }
function appTitle() public function appTitle()
{ {
// TRANS: Application title. // 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 // Filter out any poll responses
if ($parent->object_type == self::POLL_OBJECT && if ($parent->object_type == self::POLL_OBJECT &&
@ -404,7 +419,8 @@ class PollPlugin extends MicroAppPlugin
// Hide poll responses for @chuck // Hide poll responses for @chuck
function onEndNoticeWhoGets($notice, &$ni) { public function onEndNoticeWhoGets($notice, &$ni)
{
if ($notice->object_type == self::POLL_RESPONSE_OBJECT) { if ($notice->object_type == self::POLL_RESPONSE_OBJECT) {
foreach ($ni as $id => $source) { foreach ($ni as $id => $source) {
$user = User::getKV('id', $id); $user = User::getKV('id', $id);
@ -427,21 +443,23 @@ class PollPlugin extends MicroAppPlugin
* @return boolean hook return * @return boolean hook return
*/ */
function onEndAccountSettingsNav($action) public function onEndAccountSettingsNav($action)
{ {
$action_name = $action->trimmed('action'); $action_name = $action->trimmed('action');
$action->menuItem(common_local_url('pollsettings'), $action->menuItem(
common_local_url('pollsettings'),
// TRANS: Poll plugin menu item on user settings page. // TRANS: Poll plugin menu item on user settings page.
_m('MENU', 'Polls'), _m('MENU', 'Polls'),
// TRANS: Poll plugin tooltip for user settings menu item. // TRANS: Poll plugin tooltip for user settings menu item.
_m('Configure poll behavior'), _m('Configure poll behavior'),
$action_name === 'pollsettings'); $action_name === 'pollsettings'
);
return true; 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) { if ($stored->object_type == self::POLL_RESPONSE_OBJECT) {
parent::showNoticeContent($stored, $out, $scoped); parent::showNoticeContent($stored, $out, $scoped);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -56,7 +56,7 @@ class PollResponseForm extends Form
* *
* @return void * @return void
*/ */
function __construct(Poll $poll, HTMLOutputter $out) public function __construct(Poll $poll, HTMLOutputter $out)
{ {
parent::__construct($out); parent::__construct($out);
$this->poll = $poll; $this->poll = $poll;
@ -67,7 +67,7 @@ class PollResponseForm extends Form
* *
* @return int ID of the form * @return int ID of the form
*/ */
function id() public function id()
{ {
return 'pollresponse-form'; return 'pollresponse-form';
} }
@ -77,7 +77,7 @@ class PollResponseForm extends Form
* *
* @return string class of the form * @return string class of the form
*/ */
function formClass() public function formClass()
{ {
return 'form_settings ajax'; return 'form_settings ajax';
} }
@ -87,7 +87,7 @@ class PollResponseForm extends Form
* *
* @return string URL of the action * @return string URL of the action
*/ */
function action() public function action()
{ {
return common_local_url('respondpoll', array('id' => $this->poll->id)); return common_local_url('respondpoll', array('id' => $this->poll->id));
} }
@ -97,18 +97,17 @@ class PollResponseForm extends Form
* *
* @return void * @return void
*/ */
function formData() public function formData()
{ {
$poll = $this->poll; $poll = $this->poll;
$out = $this->out; $out = $this->out;
$id = "poll-" . $poll->id;
$out->element('p', 'poll-question', $poll->question); $out->element('p', 'poll-question', $poll->question);
$out->elementStart('ul', 'poll-options'); $out->elementStart('ul', 'poll-options');
foreach ($poll->getOptions() as $i => $opt) { foreach ($poll->getOptions() as $i => $opt) {
$out->elementStart('li'); $out->elementStart('li');
$out->elementStart('label'); $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->text(' ' . $opt);
$out->elementEnd('label'); $out->elementEnd('label');
$out->elementEnd('li'); $out->elementEnd('li');
@ -121,7 +120,7 @@ class PollResponseForm extends Form
* *
* @return void * @return void
*/ */
function formActions() public function formActions()
{ {
// TRANS: Button text for submitting a poll response. // TRANS: Button text for submitting a poll response.
$this->out->submit('poll-response-submit', _m('BUTTON', 'Submit'), 'submit', 'submit'); $this->out->submit('poll-response-submit', _m('BUTTON', 'Submit'), 'submit', 'submit');

View File

@ -56,7 +56,7 @@ class PollResultForm extends Form
* *
* @return void * @return void
*/ */
function __construct(Poll $poll, HTMLOutputter $out) public function __construct(Poll $poll, HTMLOutputter $out)
{ {
parent::__construct($out); parent::__construct($out);
$this->poll = $poll; $this->poll = $poll;
@ -67,7 +67,7 @@ class PollResultForm extends Form
* *
* @return int ID of the form * @return int ID of the form
*/ */
function id() public function id()
{ {
return 'pollresult-form'; return 'pollresult-form';
} }
@ -77,7 +77,7 @@ class PollResultForm extends Form
* *
* @return string class of the form * @return string class of the form
*/ */
function formClass() public function formClass()
{ {
return 'form_settings ajax'; return 'form_settings ajax';
} }
@ -87,7 +87,7 @@ class PollResultForm extends Form
* *
* @return string URL of the action * @return string URL of the action
*/ */
function action() public function action()
{ {
return common_local_url('respondpoll', array('id' => $this->poll->id)); return common_local_url('respondpoll', array('id' => $this->poll->id));
} }
@ -97,7 +97,7 @@ class PollResultForm extends Form
* *
* @return void * @return void
*/ */
function formData() public function formData()
{ {
$poll = $this->poll; $poll = $this->poll;
$out = $this->out; $out = $this->out;
@ -121,9 +121,12 @@ class PollResultForm extends Form
$out->elementEnd('td'); $out->elementEnd('td');
$out->elementStart('td'); $out->elementStart('td');
$out->element('span', array('class' => 'poll-block', $out->element(
'span',
array('class' => 'poll-block',
'style' => "width: {$w}px"), 'style' => "width: {$w}px"),
"\xc2\xa0"); // nbsp "\xc2\xa0"
); // nbsp
$out->text($counts[$i]); $out->text($counts[$i]);
$out->elementEnd('td'); $out->elementEnd('td');
@ -137,7 +140,7 @@ class PollResultForm extends Form
* *
* @return void * @return void
*/ */
function formActions() public function formActions()
{ {
} }
} }