[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/slowvote/view/ -> SlowvoteEmbedView.php (source)

   1  <?php
   2  
   3  final class SlowvoteEmbedView extends AphrontView {
   4  
   5    private $poll;
   6    private $handles;
   7    private $headless;
   8  
   9    public function setHeadless($headless) {
  10      $this->headless = $headless;
  11      return $this;
  12    }
  13  
  14    public function setPoll(PhabricatorSlowvotePoll $poll) {
  15      $this->poll = $poll;
  16      return $this;
  17    }
  18  
  19    public function getPoll() {
  20      return $this->poll;
  21    }
  22  
  23    public function render() {
  24      if (!$this->poll) {
  25        throw new Exception('Call setPoll() before render()!');
  26      }
  27  
  28      $poll = $this->poll;
  29  
  30      $phids = array();
  31      foreach ($poll->getChoices() as $choice) {
  32        $phids[] = $choice->getAuthorPHID();
  33      }
  34      $phids[] = $poll->getAuthorPHID();
  35  
  36      $this->handles = id(new PhabricatorHandleQuery())
  37        ->setViewer($this->getUser())
  38        ->withPHIDs($phids)
  39        ->execute();
  40  
  41      $options = $poll->getOptions();
  42  
  43      if ($poll->getShuffle()) {
  44        shuffle($options);
  45      }
  46  
  47      require_celerity_resource('phabricator-slowvote-css');
  48      require_celerity_resource('javelin-behavior-slowvote-embed');
  49  
  50      $config = array(
  51        'pollID' => $poll->getID(),
  52      );
  53      Javelin::initBehavior('slowvote-embed', $config);
  54  
  55      $user_choices = $poll->getViewerChoices($this->getUser());
  56      $user_choices = mpull($user_choices, 'getOptionID', 'getOptionID');
  57  
  58      $out = array();
  59      foreach ($options as $option) {
  60        $is_selected = isset($user_choices[$option->getID()]);
  61        $out[] = $this->renderLabel($option, $is_selected);
  62      }
  63  
  64      $link_to_slowvote = phutil_tag(
  65        'a',
  66        array(
  67          'href' => '/V'.$poll->getID(),
  68        ),
  69        $poll->getQuestion());
  70  
  71      if ($this->headless) {
  72        $header = null;
  73      } else {
  74        $header = phutil_tag(
  75          'div',
  76          array(
  77            'class' => 'slowvote-header',
  78          ),
  79          phutil_tag(
  80            'div',
  81            array(
  82              'class' => 'slowvote-header-content',
  83            ),
  84            array(
  85              'V'.$poll->getID(),
  86              ' ',
  87              $link_to_slowvote,
  88            )));
  89  
  90        $description = null;
  91        if ($poll->getDescription()) {
  92          $description = PhabricatorMarkupEngine::renderOneObject(
  93            id(new PhabricatorMarkupOneOff())->setContent(
  94              $poll->getDescription()),
  95            'default',
  96            $this->getUser());
  97          $description = phutil_tag(
  98            'div',
  99            array(
 100              'class' => 'slowvote-description',
 101            ),
 102            $description);
 103        }
 104  
 105        $header = array(
 106          $header,
 107          $description,
 108        );
 109      }
 110  
 111      $vis = $poll->getResponseVisibility();
 112      if ($this->areResultsVisible()) {
 113        if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
 114          $quip = pht('Only you can see the results.');
 115        } else {
 116          $quip = pht('Voting improves cardiovascular endurance.');
 117        }
 118      } else if ($vis == PhabricatorSlowvotePoll::RESPONSES_VOTERS) {
 119        $quip = pht('You must vote to see the results.');
 120      } else if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
 121        $quip = pht('Only the author can see the results.');
 122      }
 123  
 124      $hint = phutil_tag(
 125        'span',
 126        array(
 127          'class' => 'slowvote-hint',
 128        ),
 129        $quip);
 130  
 131      if ($poll->getIsClosed()) {
 132        $submit = null;
 133      } else {
 134        $submit = phutil_tag(
 135          'div',
 136          array(
 137            'class' => 'slowvote-footer',
 138          ),
 139          phutil_tag(
 140            'div',
 141            array(
 142              'class' => 'slowvote-footer-content',
 143            ),
 144            array(
 145              $hint,
 146              phutil_tag(
 147                'button',
 148                array(
 149                ),
 150                pht('Engage in Deliberations')),
 151            )));
 152      }
 153  
 154      $body = phabricator_form(
 155        $this->getUser(),
 156        array(
 157          'action'  => '/vote/'.$poll->getID().'/',
 158          'method'  => 'POST',
 159          'class'   => 'slowvote-body',
 160        ),
 161        array(
 162          phutil_tag(
 163            'div',
 164            array(
 165              'class' => 'slowvote-body-content',
 166            ),
 167            $out),
 168          $submit,
 169        ));
 170  
 171      return javelin_tag(
 172        'div',
 173        array(
 174          'class' => 'slowvote-embed',
 175          'sigil' => 'slowvote-embed',
 176          'meta' => array(
 177            'pollID' => $poll->getID(),
 178          ),
 179        ),
 180        array($header, $body));
 181    }
 182  
 183    private function renderLabel(PhabricatorSlowvoteOption $option, $selected) {
 184      $classes = array();
 185      $classes[] = 'slowvote-option-label';
 186  
 187      $status = $this->renderStatus($option);
 188      $voters = $this->renderVoters($option);
 189  
 190      return phutil_tag(
 191        'div',
 192        array(
 193          'class' => 'slowvote-option-label-group',
 194        ),
 195        array(
 196          phutil_tag(
 197            'label',
 198            array(
 199              'class' => implode(' ', $classes),
 200            ),
 201            array(
 202              phutil_tag(
 203                'div',
 204                array(
 205                  'class' => 'slowvote-control-offset',
 206                ),
 207                $option->getName()),
 208              $this->renderBar($option),
 209              phutil_tag(
 210                'div',
 211                array(
 212                  'class' => 'slowvote-above-the-bar',
 213                ),
 214                array(
 215                  $this->renderControl($option, $selected),
 216                )),
 217            )),
 218          $status,
 219          $voters,
 220        ));
 221    }
 222  
 223    private function renderBar(PhabricatorSlowvoteOption $option) {
 224      if (!$this->areResultsVisible()) {
 225        return null;
 226      }
 227  
 228      $poll = $this->getPoll();
 229  
 230      $choices = mgroup($poll->getChoices(), 'getOptionID');
 231      $choices = count(idx($choices, $option->getID(), array()));
 232      $count = count(mgroup($poll->getChoices(), 'getAuthorPHID'));
 233  
 234      return phutil_tag(
 235        'div',
 236        array(
 237          'class' => 'slowvote-bar',
 238          'style' => sprintf(
 239            'width: %.1f%%;',
 240            $count ? 100 * ($choices / $count) : 0),
 241        ),
 242        array(
 243          phutil_tag(
 244            'div',
 245            array(
 246              'class' => 'slowvote-control-offset',
 247            ),
 248            $option->getName()),
 249        ));
 250    }
 251  
 252    private function renderControl(PhabricatorSlowvoteOption $option, $selected) {
 253      $types = array(
 254        PhabricatorSlowvotePoll::METHOD_PLURALITY => 'radio',
 255        PhabricatorSlowvotePoll::METHOD_APPROVAL => 'checkbox',
 256      );
 257  
 258      $closed = $this->getPoll()->getIsClosed();
 259  
 260      return phutil_tag(
 261        'input',
 262        array(
 263          'type' => idx($types, $this->getPoll()->getMethod()),
 264          'name' => 'vote[]',
 265          'value' => $option->getID(),
 266          'checked' => ($selected ? 'checked' : null),
 267          'disabled' => ($closed ? 'disabled' : null),
 268        ));
 269    }
 270  
 271    private function renderVoters(PhabricatorSlowvoteOption $option) {
 272      if (!$this->areResultsVisible()) {
 273        return null;
 274      }
 275  
 276      $poll = $this->getPoll();
 277  
 278      $choices = mgroup($poll->getChoices(), 'getOptionID');
 279      $choices = idx($choices, $option->getID(), array());
 280  
 281      if (!$choices) {
 282        return null;
 283      }
 284  
 285      $handles = $this->handles;
 286      $authors = mpull($choices, 'getAuthorPHID', 'getAuthorPHID');
 287  
 288      $viewer_phid = $this->getUser()->getPHID();
 289  
 290      // Put the viewer first if they've voted for this option.
 291      $authors = array_select_keys($authors, array($viewer_phid))
 292               + $authors;
 293  
 294      $voters = array();
 295      foreach ($authors as $author_phid) {
 296        $handle = $handles[$author_phid];
 297  
 298        $voters[] = javelin_tag(
 299          'div',
 300          array(
 301            'class' => 'slowvote-voter',
 302            'style' => 'background-image: url('.$handle->getImageURI().')',
 303            'sigil' => 'has-tooltip',
 304            'meta' => array(
 305              'tip' => $handle->getName(),
 306            ),
 307          ));
 308      }
 309  
 310      return phutil_tag(
 311        'div',
 312        array(
 313          'class' => 'slowvote-voters',
 314        ),
 315        $voters);
 316    }
 317  
 318    private function renderStatus(PhabricatorSlowvoteOption $option) {
 319      if (!$this->areResultsVisible()) {
 320        return null;
 321      }
 322  
 323      $poll = $this->getPoll();
 324  
 325      $choices = mgroup($poll->getChoices(), 'getOptionID');
 326      $choices = count(idx($choices, $option->getID(), array()));
 327      $count = count(mgroup($poll->getChoices(), 'getAuthorPHID'));
 328  
 329      $percent = sprintf('%d%%', $count ? 100 * $choices / $count : 0);
 330  
 331      switch ($poll->getMethod()) {
 332        case PhabricatorSlowvotePoll::METHOD_PLURALITY:
 333          $status = pht('%s (%d / %d)', $percent, $choices, $count);
 334          break;
 335        case PhabricatorSlowvotePoll::METHOD_APPROVAL:
 336          $status = pht('%s Approval (%d / %d)', $percent, $choices, $count);
 337          break;
 338      }
 339  
 340      return phutil_tag(
 341        'div',
 342        array(
 343          'class' => 'slowvote-status',
 344        ),
 345        $status);
 346    }
 347  
 348    private function areResultsVisible() {
 349      $poll = $this->getPoll();
 350  
 351      $vis = $poll->getResponseVisibility();
 352      if ($vis == PhabricatorSlowvotePoll::RESPONSES_VISIBLE) {
 353        return true;
 354      } else if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
 355        return ($poll->getAuthorPHID() == $this->getUser()->getPHID());
 356      } else {
 357        $choices = mgroup($poll->getChoices(), 'getAuthorPHID');
 358        return (bool)idx($choices, $this->getUser()->getPHID());
 359      }
 360    }
 361  
 362  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1