Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ResultsWidget.php
1 <?php
2 
3 /**
4  * Widget for pooling questions. Show question and available answers.
5  *
6  **/
7 class ResultsWidget extends CWidget
8 {
9  /**
10  * Unique question identification string
11  *
12  * @var string
13  **/
14  public $id;
15 
16  /**
17  * If display statistics in percents
18  *
19  * @public bool
20  **/
21  public $inPercents = false;
22 
23  /**
24  * Precision when display votes in percents
25  *
26  * @public string
27  **/
28  public $precision = 1;
29 
30  /**
31  * If show results for user which havent vote(by checking session)
32  *
33  * @public boolean
34  **/
35  public $unvotedShow = true;
36  /**
37  * Precision when result displays above vote form
38  *
39  * @public string
40  **/
41  public $firstResult = false;
42 
43  public function run()
44  {
45  if(!$this->unvotedShow && !$this->getIsVoted() && !$this->firstResult) {
46  return;
47  }
48  $question = Question::model()
49  ->with(array('answers' => array('condition' => 'active = true', 'with' => array('userAnswersCount'))))
50  ->findByPk($this->id);
51  $answers = $question->answers;
52  usort($answers, function($a, $b){ return $a->order - $b->order;});
53  $statistics = array();
54  $totalAnswers = 0;
55  foreach($answers as $answ) {
56  $totalAnswers += $answ->userAnswersCount;
57  $statistics[] = array('title' => $answ->text, 'value' => (int)$answ->userAnswersCount);
58  }
59  if($this->inPercents) {
60  foreach($statistics as &$row) {
61  $row['value'] = round($totalAnswers === 0 ? 0 : ($row['value'] / $totalAnswers * 100), $this->precision);
62  }
63  }
64  $this->render('ResultsWidget', array('question' => $question, 'statistics' => $statistics));
65  }
66 
67  public function getIsVoted()
68  {
69  return Yii::app()->user->getState('vote-voted-'.$this->id) === true;
70  }
71 }