2011-07-08 20:13:11 +02:00
|
|
|
<?php
|
|
|
|
|
2011-09-14 17:02:31 +02:00
|
|
|
/**
|
|
|
|
* @group slowvote
|
|
|
|
*/
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorSlowvotePollController
|
2011-07-08 20:13:11 +02:00
|
|
|
extends PhabricatorSlowvoteController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
$viewer_phid = $user->getPHID();
|
|
|
|
|
|
|
|
$poll = id(new PhabricatorSlowvotePoll())->load($this->id);
|
|
|
|
if (!$poll) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = id(new PhabricatorSlowvoteOption())->loadAllWhere(
|
|
|
|
'pollID = %d',
|
|
|
|
$poll->getID());
|
|
|
|
$choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
|
|
|
|
'pollID = %d',
|
|
|
|
$poll->getID());
|
|
|
|
$comments = id(new PhabricatorSlowvoteComment())->loadAllWhere(
|
|
|
|
'pollID = %d',
|
|
|
|
$poll->getID());
|
|
|
|
|
|
|
|
$choices_by_option = mgroup($choices, 'getOptionID');
|
|
|
|
$comments_by_user = mpull($comments, null, 'getAuthorPHID');
|
|
|
|
$choices_by_user = mgroup($choices, 'getAuthorPHID');
|
|
|
|
$viewer_choices = idx($choices_by_user, $viewer_phid, array());
|
|
|
|
$viewer_comment = idx($comments_by_user, $viewer_phid, null);
|
|
|
|
|
|
|
|
$comment_text = null;
|
|
|
|
if ($viewer_comment) {
|
|
|
|
$comment_text = $viewer_comment->getCommentText();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$comment = idx($comments_by_user, $viewer_phid, null);
|
|
|
|
if ($comment) {
|
|
|
|
$comment->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$comment_text = $request->getStr('comments');
|
|
|
|
if (strlen($comment_text)) {
|
|
|
|
id(new PhabricatorSlowvoteComment())
|
|
|
|
->setAuthorPHID($viewer_phid)
|
|
|
|
->setPollID($poll->getID())
|
|
|
|
->setCommentText($comment_text)
|
|
|
|
->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$votes = $request->getArr('vote');
|
|
|
|
|
|
|
|
switch ($poll->getMethod()) {
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_PLURALITY:
|
|
|
|
// Enforce only one vote.
|
|
|
|
$votes = array_slice($votes, 0, 1);
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_APPROVAL:
|
|
|
|
// No filtering.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown poll method!");
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($viewer_choices as $viewer_choice) {
|
|
|
|
$viewer_choice->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($votes as $vote) {
|
|
|
|
id(new PhabricatorSlowvoteChoice())
|
|
|
|
->setAuthorPHID($viewer_phid)
|
|
|
|
->setPollID($poll->getID())
|
|
|
|
->setOptionID($vote)
|
|
|
|
->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI('/V'.$poll->getID());
|
|
|
|
}
|
|
|
|
|
|
|
|
require_celerity_resource('phabricator-slowvote-css');
|
|
|
|
|
|
|
|
$phids = array_merge(
|
|
|
|
mpull($choices, 'getAuthorPHID'),
|
|
|
|
mpull($comments, 'getAuthorPHID'),
|
|
|
|
array(
|
|
|
|
$poll->getAuthorPHID(),
|
|
|
|
));
|
|
|
|
|
2011-07-09 19:22:11 +02:00
|
|
|
$query = new PhabricatorObjectHandleData($phids);
|
|
|
|
$handles = $query->loadHandles();
|
|
|
|
$objects = $query->loadObjects();
|
2011-07-08 20:13:11 +02:00
|
|
|
|
|
|
|
if ($poll->getShuffle()) {
|
|
|
|
shuffle($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
$option_markup = array();
|
|
|
|
foreach ($options as $option) {
|
2011-07-08 22:52:11 +02:00
|
|
|
$option_markup[] = $this->renderPollOption(
|
|
|
|
$poll,
|
|
|
|
$viewer_choices,
|
|
|
|
$option);
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$comments_by_option = array();
|
|
|
|
switch ($poll->getMethod()) {
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_PLURALITY:
|
|
|
|
$choice_ids = array();
|
2011-07-09 19:22:11 +02:00
|
|
|
foreach ($choices_by_user as $user_phid => $user_choices) {
|
|
|
|
$choice_ids[$user_phid] = head($user_choices)->getOptionID();
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
$choice = idx($choice_ids, $comment->getAuthorPHID());
|
|
|
|
if ($choice) {
|
|
|
|
$comments_by_option[$choice][] = $comment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_APPROVAL:
|
|
|
|
// All comments are grouped in approval voting.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown poll method!");
|
|
|
|
}
|
|
|
|
|
2011-07-08 22:52:11 +02:00
|
|
|
$result_markup = $this->renderResultMarkup(
|
|
|
|
$poll,
|
|
|
|
$options,
|
|
|
|
$choices,
|
|
|
|
$comments,
|
|
|
|
$viewer_choices,
|
|
|
|
$choices_by_option,
|
|
|
|
$comments_by_option,
|
2011-07-09 19:22:11 +02:00
|
|
|
$handles,
|
|
|
|
$objects);
|
2011-07-08 20:13:11 +02:00
|
|
|
|
|
|
|
if ($viewer_choices) {
|
|
|
|
$instructions =
|
|
|
|
'Your vote has been recorded... but there is still ample time to '.
|
|
|
|
'rethink your position. Have you thoroughly considered all possible '.
|
|
|
|
'eventualities?';
|
|
|
|
} else {
|
|
|
|
$instructions =
|
|
|
|
'This is a weighty matter indeed. Consider your choices with the '.
|
|
|
|
'greatest of care.';
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2013-02-07 23:39:04 +01:00
|
|
|
->appendChild(hsprintf(
|
|
|
|
'<p class="aphront-form-instructions">%s</p>',
|
|
|
|
$instructions))
|
2011-07-08 20:13:11 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
|
|
|
->setLabel('Vote')
|
|
|
|
->setValue($option_markup))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel('Comments')
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_SHORT)
|
|
|
|
->setName('comments')
|
|
|
|
->setValue($comment_text))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue('Cautiously Engage in Deliberations'));
|
|
|
|
|
|
|
|
|
|
|
|
$panel = new AphrontPanelView();
|
|
|
|
$panel->setHeader(phutil_escape_html($poll->getQuestion()));
|
|
|
|
$panel->setWidth(AphrontPanelView::WIDTH_WIDE);
|
|
|
|
|
|
|
|
$panel->appendChild($form);
|
|
|
|
$panel->appendChild('<br /><br />');
|
|
|
|
$panel->appendChild($result_markup);
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
$panel,
|
|
|
|
array(
|
|
|
|
'title' => 'V'.$poll->getID().' '.$poll->getQuestion(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renderComments(array $comments, array $handles) {
|
2012-04-03 21:10:45 +02:00
|
|
|
assert_instances_of($comments, 'PhabricatorSlowvoteComment');
|
|
|
|
assert_instances_of($handles, 'PhabricatorObjectHandle');
|
|
|
|
|
2011-07-08 20:13:11 +02:00
|
|
|
$viewer = $this->getRequest()->getUser();
|
|
|
|
|
Generalize the markup engine factory
Summary:
This thing services every app but it lives inside Differential right now. Pull
it out, and separate the factory interfaces per-application.
This will let us accommodate changes we need to make for Phriction to support
wiki linking.
Test Plan: Tested remarkup in differential, diffusion, maniphest, people,
slowvote.
Reviewed By: hsb
Reviewers: hsb, codeblock, jungejason, tuomaspelkonen, aran
CC: aran, hsb
Differential Revision: 646
2011-07-12 00:58:32 +02:00
|
|
|
$engine = PhabricatorMarkupEngine::newSlowvoteMarkupEngine();
|
2011-07-08 20:13:11 +02:00
|
|
|
|
|
|
|
$comment_markup = array();
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
$handle = $handles[$comment->getAuthorPHID()];
|
|
|
|
|
2013-01-29 03:44:15 +01:00
|
|
|
$markup = phutil_safe_html(
|
|
|
|
$engine->markupText($comment->getCommentText()));
|
2011-07-08 20:13:11 +02:00
|
|
|
|
|
|
|
require_celerity_resource('phabricator-remarkup-css');
|
|
|
|
|
2013-02-08 00:18:34 +01:00
|
|
|
$comment_markup[] = hsprintf(
|
2011-07-08 20:13:11 +02:00
|
|
|
'<tr>'.
|
|
|
|
'<th>'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'%s'.
|
|
|
|
'<div class="phabricator-slowvote-datestamp">%s</div>'.
|
|
|
|
'</th>'.
|
2011-07-08 20:13:11 +02:00
|
|
|
'<td>'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'<div class="phabricator-remarkup">%s</div>'.
|
2011-07-08 20:13:11 +02:00
|
|
|
'</td>'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'</tr>',
|
|
|
|
$handle->renderLink(),
|
|
|
|
phabricator_datetime($comment->getDateCreated(), $viewer),
|
|
|
|
$markup);
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($comment_markup) {
|
2013-01-18 09:32:58 +01:00
|
|
|
$comment_markup = phutil_tag(
|
2011-07-08 20:13:11 +02:00
|
|
|
'table',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-slowvote-comments',
|
|
|
|
),
|
2013-02-08 00:18:34 +01:00
|
|
|
$comment_markup);
|
2011-07-08 20:13:11 +02:00
|
|
|
} else {
|
|
|
|
$comment_markup = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $comment_markup;
|
|
|
|
}
|
|
|
|
|
2011-07-08 22:52:11 +02:00
|
|
|
private function renderPollOption(
|
|
|
|
PhabricatorSlowvotePoll $poll,
|
|
|
|
array $viewer_choices,
|
|
|
|
PhabricatorSlowvoteOption $option) {
|
2012-04-03 21:10:45 +02:00
|
|
|
assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice');
|
2011-07-08 22:52:11 +02:00
|
|
|
|
|
|
|
$id = $option->getID();
|
|
|
|
switch ($poll->getMethod()) {
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_PLURALITY:
|
|
|
|
|
|
|
|
// Render a radio button.
|
|
|
|
|
|
|
|
$selected_option = head($viewer_choices);
|
|
|
|
if ($selected_option) {
|
|
|
|
$selected = $selected_option->getOptionID();
|
|
|
|
} else {
|
|
|
|
$selected = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($selected == $id) {
|
|
|
|
$checked = "checked";
|
|
|
|
} else {
|
|
|
|
$checked = null;
|
|
|
|
}
|
|
|
|
|
2013-01-18 03:39:02 +01:00
|
|
|
$input = phutil_tag(
|
2011-07-08 22:52:11 +02:00
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'radio',
|
|
|
|
'name' => 'vote[]',
|
|
|
|
'value' => $id,
|
|
|
|
'checked' => $checked,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_APPROVAL:
|
|
|
|
|
|
|
|
// Render a check box.
|
|
|
|
|
|
|
|
$checked = null;
|
|
|
|
foreach ($viewer_choices as $choice) {
|
|
|
|
if ($choice->getOptionID() == $id) {
|
|
|
|
$checked = 'checked';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 03:39:02 +01:00
|
|
|
$input = phutil_tag(
|
2011-07-08 22:52:11 +02:00
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'name' => 'vote[]',
|
|
|
|
'checked' => $checked,
|
|
|
|
'value' => $id,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown poll method!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($checked) {
|
|
|
|
$checked_class = 'phabricator-slowvote-checked';
|
|
|
|
} else {
|
|
|
|
$checked_class = null;
|
|
|
|
}
|
|
|
|
|
2013-01-18 09:32:58 +01:00
|
|
|
return phutil_tag(
|
2011-07-08 22:52:11 +02:00
|
|
|
'label',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-slowvote-label '.$checked_class,
|
|
|
|
),
|
2013-01-18 09:32:58 +01:00
|
|
|
array($input, $option->getName()));
|
2011-07-08 22:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function renderVoteCount(
|
|
|
|
PhabricatorSlowvotePoll $poll,
|
|
|
|
array $choices,
|
|
|
|
array $chosen) {
|
2012-04-03 21:10:45 +02:00
|
|
|
assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
|
|
|
|
assert_instances_of($chosen, 'PhabricatorSlowvoteChoice');
|
2011-07-08 22:52:11 +02:00
|
|
|
|
|
|
|
switch ($poll->getMethod()) {
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_PLURALITY:
|
|
|
|
$out_of_total = count($choices);
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::METHOD_APPROVAL:
|
|
|
|
// Count unique respondents for approval votes.
|
|
|
|
$out_of_total = count(mpull($choices, null, 'getAuthorPHID'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown poll method!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'%d / %d (%d%%)',
|
|
|
|
number_format(count($chosen)),
|
|
|
|
number_format($out_of_total),
|
|
|
|
$out_of_total
|
|
|
|
? round(100 * count($chosen) / $out_of_total)
|
|
|
|
: 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renderResultMarkup(
|
|
|
|
PhabricatorSlowvotePoll $poll,
|
|
|
|
array $options,
|
|
|
|
array $choices,
|
|
|
|
array $comments,
|
|
|
|
array $viewer_choices,
|
|
|
|
array $choices_by_option,
|
|
|
|
array $comments_by_option,
|
2011-07-09 19:22:11 +02:00
|
|
|
array $handles,
|
|
|
|
array $objects) {
|
2012-04-03 21:10:45 +02:00
|
|
|
assert_instances_of($options, 'PhabricatorSlowvoteOption');
|
|
|
|
assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
|
|
|
|
assert_instances_of($comments, 'PhabricatorSlowvoteComment');
|
|
|
|
assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice');
|
|
|
|
assert_instances_of($handles, 'PhabricatorObjectHandle');
|
|
|
|
assert_instances_of($objects, 'PhabricatorLiskDAO');
|
2011-07-08 22:52:11 +02:00
|
|
|
|
|
|
|
$viewer_phid = $this->getRequest()->getUser()->getPHID();
|
|
|
|
|
|
|
|
$can_see_responses = false;
|
|
|
|
$need_vote = false;
|
|
|
|
switch ($poll->getResponseVisibility()) {
|
|
|
|
case PhabricatorSlowvotePoll::RESPONSES_VISIBLE:
|
|
|
|
$can_see_responses = true;
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::RESPONSES_VOTERS:
|
|
|
|
$can_see_responses = (bool)$viewer_choices;
|
|
|
|
$need_vote = true;
|
|
|
|
break;
|
|
|
|
case PhabricatorSlowvotePoll::RESPONSES_OWNER:
|
|
|
|
$can_see_responses = ($viewer_phid == $poll->getAuthorPHID());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result_markup = id(new AphrontFormLayoutView())
|
2013-02-08 00:18:34 +01:00
|
|
|
->appendChild(phutil_tag('h1', array(), 'Ongoing Deliberation'));
|
2011-07-08 22:52:11 +02:00
|
|
|
|
|
|
|
if (!$can_see_responses) {
|
|
|
|
if ($need_vote) {
|
|
|
|
$reason = "You must vote to see the results.";
|
|
|
|
} else {
|
|
|
|
$reason = "The results are not public.";
|
|
|
|
}
|
|
|
|
$result_markup
|
2013-02-08 00:18:34 +01:00
|
|
|
->appendChild(hsprintf(
|
|
|
|
'<p class="aphront-form-instructions"><em>%s</em></p>',
|
|
|
|
$reason));
|
2011-07-08 22:52:11 +02:00
|
|
|
return $result_markup;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($options as $option) {
|
|
|
|
$id = $option->getID();
|
|
|
|
|
|
|
|
$chosen = idx($choices_by_option, $id, array());
|
|
|
|
$users = array_select_keys($handles, mpull($chosen, 'getAuthorPHID'));
|
|
|
|
if ($users) {
|
|
|
|
$user_markup = array();
|
|
|
|
foreach ($users as $handle) {
|
2011-07-09 19:22:11 +02:00
|
|
|
$object = idx($objects, $handle->getPHID());
|
|
|
|
if (!$object) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-10 23:48:55 +01:00
|
|
|
$profile_image = $handle->getImageURI();
|
2011-07-09 19:22:11 +02:00
|
|
|
|
2013-01-18 03:47:13 +01:00
|
|
|
$user_markup[] = phutil_tag(
|
2011-07-09 19:22:11 +02:00
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $handle->getURI(),
|
|
|
|
'class' => 'phabricator-slowvote-facepile',
|
|
|
|
),
|
2013-01-18 03:39:02 +01:00
|
|
|
phutil_tag(
|
2011-07-09 19:22:11 +02:00
|
|
|
'img',
|
|
|
|
array(
|
|
|
|
'src' => $profile_image,
|
|
|
|
)));
|
2011-07-08 22:52:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$user_markup = 'This option has failed to appeal to anyone.';
|
|
|
|
}
|
|
|
|
|
|
|
|
$comment_markup = $this->renderComments(
|
|
|
|
idx($comments_by_option, $id, array()),
|
|
|
|
$handles);
|
|
|
|
|
|
|
|
$vote_count = $this->renderVoteCount(
|
|
|
|
$poll,
|
|
|
|
$choices,
|
|
|
|
$chosen);
|
|
|
|
|
2013-02-08 00:18:34 +01:00
|
|
|
$result_markup->appendChild(hsprintf(
|
2011-07-08 22:52:11 +02:00
|
|
|
'<div>'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'<div class="phabricator-slowvote-count">%s</div>'.
|
|
|
|
'<h1>%s</h1>'.
|
2011-07-08 22:52:11 +02:00
|
|
|
'<hr class="phabricator-slowvote-hr" />'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'%s'.
|
|
|
|
'<div style="clear: both;" />'.
|
2011-07-08 22:52:11 +02:00
|
|
|
'<hr class="phabricator-slowvote-hr" />'.
|
2013-02-08 00:18:34 +01:00
|
|
|
'%s'.
|
|
|
|
'</div>',
|
|
|
|
$vote_count,
|
|
|
|
$option->getName(),
|
|
|
|
phutil_tag('div', array(), $user_markup),
|
|
|
|
$comment_markup));
|
2011-07-08 22:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($poll->getMethod() == PhabricatorSlowvotePoll::METHOD_APPROVAL &&
|
|
|
|
$comments) {
|
|
|
|
$comment_markup = $this->renderComments(
|
|
|
|
$comments,
|
|
|
|
$handles);
|
|
|
|
$result_markup->appendChild(
|
2013-02-08 00:18:34 +01:00
|
|
|
phutil_tag('h1', array(), 'Motions Proposed for Consideration'));
|
2011-07-08 22:52:11 +02:00
|
|
|
$result_markup->appendChild($comment_markup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result_markup;
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|